Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / bluetooth / bluecard_cs.c
1 /*
2  *
3  *  Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)
4  *
5  *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation;
11  *
12  *  Software distributed under the License is distributed on an "AS
13  *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  *  implied. See the License for the specific language governing
15  *  rights and limitations under the License.
16  *
17  *  The initial developer of the original code is David A. Hinds
18  *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
19  *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
20  *
21  */
22
23 #include <linux/config.h>
24 #include <linux/module.h>
25
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/timer.h>
33 #include <linux/errno.h>
34 #include <linux/ptrace.h>
35 #include <linux/ioport.h>
36 #include <linux/spinlock.h>
37 #include <linux/moduleparam.h>
38 #include <linux/wait.h>
39
40 #include <linux/skbuff.h>
41 #include <asm/io.h>
42
43 #include <pcmcia/version.h>
44 #include <pcmcia/cs_types.h>
45 #include <pcmcia/cs.h>
46 #include <pcmcia/cistpl.h>
47 #include <pcmcia/ciscode.h>
48 #include <pcmcia/ds.h>
49 #include <pcmcia/cisreg.h>
50
51 #include <net/bluetooth/bluetooth.h>
52 #include <net/bluetooth/hci_core.h>
53
54
55
56 /* ======================== Module parameters ======================== */
57
58
59 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
60 MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
61 MODULE_LICENSE("GPL");
62
63
64
65 /* ======================== Local structures ======================== */
66
67
68 typedef struct bluecard_info_t {
69         dev_link_t link;
70         dev_node_t node;
71
72         struct hci_dev *hdev;
73
74         spinlock_t lock;                /* For serializing operations */
75         struct timer_list timer;        /* For LED control */
76
77         struct sk_buff_head txq;
78         unsigned long tx_state;
79
80         unsigned long rx_state;
81         unsigned long rx_count;
82         struct sk_buff *rx_skb;
83
84         unsigned char ctrl_reg;
85         unsigned long hw_state;         /* Status of the hardware and LED control */
86 } bluecard_info_t;
87
88
89 static void bluecard_config(dev_link_t *link);
90 static void bluecard_release(dev_link_t *link);
91 static int bluecard_event(event_t event, int priority, event_callback_args_t *args);
92
93 static dev_info_t dev_info = "bluecard_cs";
94
95 static dev_link_t *bluecard_attach(void);
96 static void bluecard_detach(dev_link_t *);
97
98 static dev_link_t *dev_list = NULL;
99
100
101 /* Default baud rate: 57600, 115200, 230400 or 460800 */
102 #define DEFAULT_BAUD_RATE  230400
103
104
105 /* Hardware states */
106 #define CARD_READY             1
107 #define CARD_HAS_PCCARD_ID     4
108 #define CARD_HAS_POWER_LED     5
109 #define CARD_HAS_ACTIVITY_LED  6
110
111 /* Transmit states  */
112 #define XMIT_SENDING         1
113 #define XMIT_WAKEUP          2
114 #define XMIT_BUFFER_NUMBER   5  /* unset = buffer one, set = buffer two */
115 #define XMIT_BUF_ONE_READY   6
116 #define XMIT_BUF_TWO_READY   7
117 #define XMIT_SENDING_READY   8
118
119 /* Receiver states */
120 #define RECV_WAIT_PACKET_TYPE   0
121 #define RECV_WAIT_EVENT_HEADER  1
122 #define RECV_WAIT_ACL_HEADER    2
123 #define RECV_WAIT_SCO_HEADER    3
124 #define RECV_WAIT_DATA          4
125
126 /* Special packet types */
127 #define PKT_BAUD_RATE_57600   0x80
128 #define PKT_BAUD_RATE_115200  0x81
129 #define PKT_BAUD_RATE_230400  0x82
130 #define PKT_BAUD_RATE_460800  0x83
131
132
133 /* These are the register offsets */
134 #define REG_COMMAND     0x20
135 #define REG_INTERRUPT   0x21
136 #define REG_CONTROL     0x22
137 #define REG_RX_CONTROL  0x24
138 #define REG_CARD_RESET  0x30
139 #define REG_LED_CTRL    0x30
140
141 /* REG_COMMAND */
142 #define REG_COMMAND_TX_BUF_ONE  0x01
143 #define REG_COMMAND_TX_BUF_TWO  0x02
144 #define REG_COMMAND_RX_BUF_ONE  0x04
145 #define REG_COMMAND_RX_BUF_TWO  0x08
146 #define REG_COMMAND_RX_WIN_ONE  0x00
147 #define REG_COMMAND_RX_WIN_TWO  0x10
148
149 /* REG_CONTROL */
150 #define REG_CONTROL_BAUD_RATE_57600   0x00
151 #define REG_CONTROL_BAUD_RATE_115200  0x01
152 #define REG_CONTROL_BAUD_RATE_230400  0x02
153 #define REG_CONTROL_BAUD_RATE_460800  0x03
154 #define REG_CONTROL_RTS               0x04
155 #define REG_CONTROL_BT_ON             0x08
156 #define REG_CONTROL_BT_RESET          0x10
157 #define REG_CONTROL_BT_RES_PU         0x20
158 #define REG_CONTROL_INTERRUPT         0x40
159 #define REG_CONTROL_CARD_RESET        0x80
160
161 /* REG_RX_CONTROL */
162 #define RTS_LEVEL_SHIFT_BITS  0x02
163
164
165
166 /* ======================== LED handling routines ======================== */
167
168
169 static void bluecard_activity_led_timeout(u_long arg)
170 {
171         bluecard_info_t *info = (bluecard_info_t *)arg;
172         unsigned int iobase = info->link.io.BasePort1;
173
174         if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
175                 return;
176
177         if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
178                 /* Disable activity LED */
179                 outb(0x08 | 0x20, iobase + 0x30);
180         } else {
181                 /* Disable power LED */
182                 outb(0x00, iobase + 0x30);
183         }
184 }
185
186
187 static void bluecard_enable_activity_led(bluecard_info_t *info)
188 {
189         unsigned int iobase = info->link.io.BasePort1;
190
191         if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
192                 return;
193
194         if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
195                 /* Enable activity LED */
196                 outb(0x10 | 0x40, iobase + 0x30);
197
198                 /* Stop the LED after HZ/4 */
199                 mod_timer(&(info->timer), jiffies + HZ / 4);
200         } else {
201                 /* Enable power LED */
202                 outb(0x08 | 0x20, iobase + 0x30);
203
204                 /* Stop the LED after HZ/2 */
205                 mod_timer(&(info->timer), jiffies + HZ / 2);
206         }
207 }
208
209
210
211 /* ======================== Interrupt handling ======================== */
212
213
214 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
215 {
216         int i, actual;
217
218         actual = (len > 15) ? 15 : len;
219
220         outb_p(actual, iobase + offset);
221
222         for (i = 0; i < actual; i++)
223                 outb_p(buf[i], iobase + offset + i + 1);
224
225         return actual;
226 }
227
228
229 static void bluecard_write_wakeup(bluecard_info_t *info)
230 {
231         if (!info) {
232                 BT_ERR("Unknown device");
233                 return;
234         }
235
236         if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
237                 return;
238
239         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
240                 set_bit(XMIT_WAKEUP, &(info->tx_state));
241                 return;
242         }
243
244         do {
245                 register unsigned int iobase = info->link.io.BasePort1;
246                 register unsigned int offset;
247                 register unsigned char command;
248                 register unsigned long ready_bit;
249                 register struct sk_buff *skb;
250                 register int len;
251
252                 clear_bit(XMIT_WAKEUP, &(info->tx_state));
253
254                 if (!(info->link.state & DEV_PRESENT))
255                         return;
256
257                 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
258                         if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
259                                 break;
260                         offset = 0x10;
261                         command = REG_COMMAND_TX_BUF_TWO;
262                         ready_bit = XMIT_BUF_TWO_READY;
263                 } else {
264                         if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
265                                 break;
266                         offset = 0x00;
267                         command = REG_COMMAND_TX_BUF_ONE;
268                         ready_bit = XMIT_BUF_ONE_READY;
269                 }
270
271                 if (!(skb = skb_dequeue(&(info->txq))))
272                         break;
273
274                 if (skb->pkt_type & 0x80) {
275                         /* Disable RTS */
276                         info->ctrl_reg |= REG_CONTROL_RTS;
277                         outb(info->ctrl_reg, iobase + REG_CONTROL);
278                 }
279
280                 /* Activate LED */
281                 bluecard_enable_activity_led(info);
282
283                 /* Send frame */
284                 len = bluecard_write(iobase, offset, skb->data, skb->len);
285
286                 /* Tell the FPGA to send the data */
287                 outb_p(command, iobase + REG_COMMAND);
288
289                 /* Mark the buffer as dirty */
290                 clear_bit(ready_bit, &(info->tx_state));
291
292                 if (skb->pkt_type & 0x80) {
293                         DECLARE_WAIT_QUEUE_HEAD(wq);
294                         DEFINE_WAIT(wait);
295
296                         unsigned char baud_reg;
297
298                         switch (skb->pkt_type) {
299                         case PKT_BAUD_RATE_460800:
300                                 baud_reg = REG_CONTROL_BAUD_RATE_460800;
301                                 break;
302                         case PKT_BAUD_RATE_230400:
303                                 baud_reg = REG_CONTROL_BAUD_RATE_230400;
304                                 break;
305                         case PKT_BAUD_RATE_115200:
306                                 baud_reg = REG_CONTROL_BAUD_RATE_115200;
307                                 break;
308                         case PKT_BAUD_RATE_57600:
309                                 /* Fall through... */
310                         default:
311                                 baud_reg = REG_CONTROL_BAUD_RATE_57600;
312                                 break;
313                         }
314
315                         /* Wait until the command reaches the baseband */
316                         prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
317                         schedule_timeout(HZ/10);
318                         finish_wait(&wq, &wait);
319
320                         /* Set baud on baseband */
321                         info->ctrl_reg &= ~0x03;
322                         info->ctrl_reg |= baud_reg;
323                         outb(info->ctrl_reg, iobase + REG_CONTROL);
324
325                         /* Enable RTS */
326                         info->ctrl_reg &= ~REG_CONTROL_RTS;
327                         outb(info->ctrl_reg, iobase + REG_CONTROL);
328
329                         /* Wait before the next HCI packet can be send */
330                         prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
331                         schedule_timeout(HZ);
332                         finish_wait(&wq, &wait);
333                 }
334
335                 if (len == skb->len) {
336                         kfree_skb(skb);
337                 } else {
338                         skb_pull(skb, len);
339                         skb_queue_head(&(info->txq), skb);
340                 }
341
342                 info->hdev->stat.byte_tx += len;
343
344                 /* Change buffer */
345                 change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
346
347         } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
348
349         clear_bit(XMIT_SENDING, &(info->tx_state));
350 }
351
352
353 static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
354 {
355         int i, n, len;
356
357         outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
358
359         len = inb(iobase + offset);
360         n = 0;
361         i = 1;
362
363         while (n < len) {
364
365                 if (i == 16) {
366                         outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
367                         i = 0;
368                 }
369
370                 buf[n] = inb(iobase + offset + i);
371
372                 n++;
373                 i++;
374
375         }
376
377         return len;
378 }
379
380
381 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
382 {
383         unsigned int iobase;
384         unsigned char buf[31];
385         int i, len;
386
387         if (!info) {
388                 BT_ERR("Unknown device");
389                 return;
390         }
391
392         iobase = info->link.io.BasePort1;
393
394         if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
395                 bluecard_enable_activity_led(info);
396
397         len = bluecard_read(iobase, offset, buf, sizeof(buf));
398
399         for (i = 0; i < len; i++) {
400
401                 /* Allocate packet */
402                 if (info->rx_skb == NULL) {
403                         info->rx_state = RECV_WAIT_PACKET_TYPE;
404                         info->rx_count = 0;
405                         if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
406                                 BT_ERR("Can't allocate mem for new packet");
407                                 return;
408                         }
409                 }
410
411                 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
412
413                         info->rx_skb->dev = (void *) info->hdev;
414                         info->rx_skb->pkt_type = buf[i];
415
416                         switch (info->rx_skb->pkt_type) {
417
418                         case 0x00:
419                                 /* init packet */
420                                 if (offset != 0x00) {
421                                         set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
422                                         set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
423                                         set_bit(XMIT_SENDING_READY, &(info->tx_state));
424                                         bluecard_write_wakeup(info);
425                                 }
426
427                                 kfree_skb(info->rx_skb);
428                                 info->rx_skb = NULL;
429                                 break;
430
431                         case HCI_EVENT_PKT:
432                                 info->rx_state = RECV_WAIT_EVENT_HEADER;
433                                 info->rx_count = HCI_EVENT_HDR_SIZE;
434                                 break;
435
436                         case HCI_ACLDATA_PKT:
437                                 info->rx_state = RECV_WAIT_ACL_HEADER;
438                                 info->rx_count = HCI_ACL_HDR_SIZE;
439                                 break;
440
441                         case HCI_SCODATA_PKT:
442                                 info->rx_state = RECV_WAIT_SCO_HEADER;
443                                 info->rx_count = HCI_SCO_HDR_SIZE;
444                                 break;
445
446                         default:
447                                 /* unknown packet */
448                                 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
449                                 info->hdev->stat.err_rx++;
450
451                                 kfree_skb(info->rx_skb);
452                                 info->rx_skb = NULL;
453                                 break;
454
455                         }
456
457                 } else {
458
459                         *skb_put(info->rx_skb, 1) = buf[i];
460                         info->rx_count--;
461
462                         if (info->rx_count == 0) {
463
464                                 int dlen;
465                                 struct hci_event_hdr *eh;
466                                 struct hci_acl_hdr *ah;
467                                 struct hci_sco_hdr *sh;
468
469                                 switch (info->rx_state) {
470
471                                 case RECV_WAIT_EVENT_HEADER:
472                                         eh = (struct hci_event_hdr *)(info->rx_skb->data);
473                                         info->rx_state = RECV_WAIT_DATA;
474                                         info->rx_count = eh->plen;
475                                         break;
476
477                                 case RECV_WAIT_ACL_HEADER:
478                                         ah = (struct hci_acl_hdr *)(info->rx_skb->data);
479                                         dlen = __le16_to_cpu(ah->dlen);
480                                         info->rx_state = RECV_WAIT_DATA;
481                                         info->rx_count = dlen;
482                                         break;
483
484                                 case RECV_WAIT_SCO_HEADER:
485                                         sh = (struct hci_sco_hdr *)(info->rx_skb->data);
486                                         info->rx_state = RECV_WAIT_DATA;
487                                         info->rx_count = sh->dlen;
488                                         break;
489
490                                 case RECV_WAIT_DATA:
491                                         hci_recv_frame(info->rx_skb);
492                                         info->rx_skb = NULL;
493                                         break;
494
495                                 }
496
497                         }
498
499                 }
500
501
502         }
503
504         info->hdev->stat.byte_rx += len;
505 }
506
507
508 static irqreturn_t bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
509 {
510         bluecard_info_t *info = dev_inst;
511         unsigned int iobase;
512         unsigned char reg;
513
514         if (!info || !info->hdev) {
515                 BT_ERR("Call of irq %d for unknown device", irq);
516                 return IRQ_NONE;
517         }
518
519         if (!test_bit(CARD_READY, &(info->hw_state)))
520                 return IRQ_HANDLED;
521
522         iobase = info->link.io.BasePort1;
523
524         spin_lock(&(info->lock));
525
526         /* Disable interrupt */
527         info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
528         outb(info->ctrl_reg, iobase + REG_CONTROL);
529
530         reg = inb(iobase + REG_INTERRUPT);
531
532         if ((reg != 0x00) && (reg != 0xff)) {
533
534                 if (reg & 0x04) {
535                         bluecard_receive(info, 0x00);
536                         outb(0x04, iobase + REG_INTERRUPT);
537                         outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
538                 }
539
540                 if (reg & 0x08) {
541                         bluecard_receive(info, 0x10);
542                         outb(0x08, iobase + REG_INTERRUPT);
543                         outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
544                 }
545
546                 if (reg & 0x01) {
547                         set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
548                         outb(0x01, iobase + REG_INTERRUPT);
549                         bluecard_write_wakeup(info);
550                 }
551
552                 if (reg & 0x02) {
553                         set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
554                         outb(0x02, iobase + REG_INTERRUPT);
555                         bluecard_write_wakeup(info);
556                 }
557
558         }
559
560         /* Enable interrupt */
561         info->ctrl_reg |= REG_CONTROL_INTERRUPT;
562         outb(info->ctrl_reg, iobase + REG_CONTROL);
563
564         spin_unlock(&(info->lock));
565
566         return IRQ_HANDLED;
567 }
568
569
570
571 /* ======================== Device specific HCI commands ======================== */
572
573
574 static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
575 {
576         bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
577         struct sk_buff *skb;
578
579         /* Ericsson baud rate command */
580         unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
581
582         if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
583                 BT_ERR("Can't allocate mem for new packet");
584                 return -1;
585         }
586
587         switch (baud) {
588         case 460800:
589                 cmd[4] = 0x00;
590                 skb->pkt_type = PKT_BAUD_RATE_460800;
591                 break;
592         case 230400:
593                 cmd[4] = 0x01;
594                 skb->pkt_type = PKT_BAUD_RATE_230400;
595                 break;
596         case 115200:
597                 cmd[4] = 0x02;
598                 skb->pkt_type = PKT_BAUD_RATE_115200;
599                 break;
600         case 57600:
601                 /* Fall through... */
602         default:
603                 cmd[4] = 0x03;
604                 skb->pkt_type = PKT_BAUD_RATE_57600;
605                 break;
606         }
607
608         memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
609
610         skb_queue_tail(&(info->txq), skb);
611
612         bluecard_write_wakeup(info);
613
614         return 0;
615 }
616
617
618
619 /* ======================== HCI interface ======================== */
620
621
622 static int bluecard_hci_flush(struct hci_dev *hdev)
623 {
624         bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
625
626         /* Drop TX queue */
627         skb_queue_purge(&(info->txq));
628
629         return 0;
630 }
631
632
633 static int bluecard_hci_open(struct hci_dev *hdev)
634 {
635         bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
636         unsigned int iobase = info->link.io.BasePort1;
637
638         if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
639                 bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
640
641         if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
642                 return 0;
643
644         if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
645                 /* Enable LED */
646                 outb(0x08 | 0x20, iobase + 0x30);
647         }
648
649         return 0;
650 }
651
652
653 static int bluecard_hci_close(struct hci_dev *hdev)
654 {
655         bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
656         unsigned int iobase = info->link.io.BasePort1;
657
658         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
659                 return 0;
660
661         bluecard_hci_flush(hdev);
662
663         if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
664                 /* Disable LED */
665                 outb(0x00, iobase + 0x30);
666         }
667
668         return 0;
669 }
670
671
672 static int bluecard_hci_send_frame(struct sk_buff *skb)
673 {
674         bluecard_info_t *info;
675         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
676
677         if (!hdev) {
678                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
679                 return -ENODEV;
680         }
681
682         info = (bluecard_info_t *)(hdev->driver_data);
683
684         switch (skb->pkt_type) {
685         case HCI_COMMAND_PKT:
686                 hdev->stat.cmd_tx++;
687                 break;
688         case HCI_ACLDATA_PKT:
689                 hdev->stat.acl_tx++;
690                 break;
691         case HCI_SCODATA_PKT:
692                 hdev->stat.sco_tx++;
693                 break;
694         };
695
696         /* Prepend skb with frame type */
697         memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
698         skb_queue_tail(&(info->txq), skb);
699
700         bluecard_write_wakeup(info);
701
702         return 0;
703 }
704
705
706 static void bluecard_hci_destruct(struct hci_dev *hdev)
707 {
708 }
709
710
711 static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
712 {
713         return -ENOIOCTLCMD;
714 }
715
716
717
718 /* ======================== Card services HCI interaction ======================== */
719
720
721 static int bluecard_open(bluecard_info_t *info)
722 {
723         unsigned int iobase = info->link.io.BasePort1;
724         struct hci_dev *hdev;
725         unsigned char id;
726
727         spin_lock_init(&(info->lock));
728
729         init_timer(&(info->timer));
730         info->timer.function = &bluecard_activity_led_timeout;
731         info->timer.data = (u_long)info;
732
733         skb_queue_head_init(&(info->txq));
734
735         info->rx_state = RECV_WAIT_PACKET_TYPE;
736         info->rx_count = 0;
737         info->rx_skb = NULL;
738
739         /* Initialize HCI device */
740         hdev = hci_alloc_dev();
741         if (!hdev) {
742                 BT_ERR("Can't allocate HCI device");
743                 return -ENOMEM;
744         }
745
746         info->hdev = hdev;
747
748         hdev->type = HCI_PCCARD;
749         hdev->driver_data = info;
750
751         hdev->open     = bluecard_hci_open;
752         hdev->close    = bluecard_hci_close;
753         hdev->flush    = bluecard_hci_flush;
754         hdev->send     = bluecard_hci_send_frame;
755         hdev->destruct = bluecard_hci_destruct;
756         hdev->ioctl    = bluecard_hci_ioctl;
757
758         hdev->owner = THIS_MODULE;
759
760         id = inb(iobase + 0x30);
761
762         if ((id & 0x0f) == 0x02)
763                 set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
764
765         if (id & 0x10)
766                 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
767
768         if (id & 0x20)
769                 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
770
771         /* Reset card */
772         info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
773         outb(info->ctrl_reg, iobase + REG_CONTROL);
774
775         /* Turn FPGA off */
776         outb(0x80, iobase + 0x30);
777
778         /* Wait some time */
779         msleep(10);
780
781         /* Turn FPGA on */
782         outb(0x00, iobase + 0x30);
783
784         /* Activate card */
785         info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
786         outb(info->ctrl_reg, iobase + REG_CONTROL);
787
788         /* Enable interrupt */
789         outb(0xff, iobase + REG_INTERRUPT);
790         info->ctrl_reg |= REG_CONTROL_INTERRUPT;
791         outb(info->ctrl_reg, iobase + REG_CONTROL);
792
793         if ((id & 0x0f) == 0x03) {
794                 /* Disable RTS */
795                 info->ctrl_reg |= REG_CONTROL_RTS;
796                 outb(info->ctrl_reg, iobase + REG_CONTROL);
797
798                 /* Set baud rate */
799                 info->ctrl_reg |= 0x03;
800                 outb(info->ctrl_reg, iobase + REG_CONTROL);
801
802                 /* Enable RTS */
803                 info->ctrl_reg &= ~REG_CONTROL_RTS;
804                 outb(info->ctrl_reg, iobase + REG_CONTROL);
805
806                 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
807                 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
808                 set_bit(XMIT_SENDING_READY, &(info->tx_state));
809         }
810
811         /* Start the RX buffers */
812         outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
813         outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
814
815         /* Signal that the hardware is ready */
816         set_bit(CARD_READY, &(info->hw_state));
817
818         /* Drop TX queue */
819         skb_queue_purge(&(info->txq));
820
821         /* Control the point at which RTS is enabled */
822         outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
823
824         /* Timeout before it is safe to send the first HCI packet */
825         msleep(1250);
826
827         /* Register HCI device */
828         if (hci_register_dev(hdev) < 0) {
829                 BT_ERR("Can't register HCI device");
830                 info->hdev = NULL;
831                 hci_free_dev(hdev);
832                 return -ENODEV;
833         }
834
835         return 0;
836 }
837
838
839 static int bluecard_close(bluecard_info_t *info)
840 {
841         unsigned int iobase = info->link.io.BasePort1;
842         struct hci_dev *hdev = info->hdev;
843
844         if (!hdev)
845                 return -ENODEV;
846
847         bluecard_hci_close(hdev);
848
849         clear_bit(CARD_READY, &(info->hw_state));
850
851         /* Reset card */
852         info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
853         outb(info->ctrl_reg, iobase + REG_CONTROL);
854
855         /* Turn FPGA off */
856         outb(0x80, iobase + 0x30);
857
858         if (hci_unregister_dev(hdev) < 0)
859                 BT_ERR("Can't unregister HCI device %s", hdev->name);
860
861         hci_free_dev(hdev);
862
863         return 0;
864 }
865
866 static dev_link_t *bluecard_attach(void)
867 {
868         bluecard_info_t *info;
869         client_reg_t client_reg;
870         dev_link_t *link;
871         int ret;
872
873         /* Create new info device */
874         info = kmalloc(sizeof(*info), GFP_KERNEL);
875         if (!info)
876                 return NULL;
877         memset(info, 0, sizeof(*info));
878
879         link = &info->link;
880         link->priv = info;
881
882         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
883         link->io.NumPorts1 = 8;
884         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
885         link->irq.IRQInfo1 = IRQ_LEVEL_ID;
886
887         link->irq.Handler = bluecard_interrupt;
888         link->irq.Instance = info;
889
890         link->conf.Attributes = CONF_ENABLE_IRQ;
891         link->conf.Vcc = 50;
892         link->conf.IntType = INT_MEMORY_AND_IO;
893
894         /* Register with Card Services */
895         link->next = dev_list;
896         dev_list = link;
897         client_reg.dev_info = &dev_info;
898         client_reg.EventMask =
899                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
900                 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
901                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
902         client_reg.event_handler = &bluecard_event;
903         client_reg.Version = 0x0210;
904         client_reg.event_callback_args.client_data = link;
905
906         ret = pcmcia_register_client(&link->handle, &client_reg);
907         if (ret != CS_SUCCESS) {
908                 cs_error(link->handle, RegisterClient, ret);
909                 bluecard_detach(link);
910                 return NULL;
911         }
912
913         return link;
914 }
915
916
917 static void bluecard_detach(dev_link_t *link)
918 {
919         bluecard_info_t *info = link->priv;
920         dev_link_t **linkp;
921         int ret;
922
923         /* Locate device structure */
924         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
925                 if (*linkp == link)
926                         break;
927
928         if (*linkp == NULL)
929                 return;
930
931         if (link->state & DEV_CONFIG)
932                 bluecard_release(link);
933
934         if (link->handle) {
935                 ret = pcmcia_deregister_client(link->handle);
936                 if (ret != CS_SUCCESS)
937                         cs_error(link->handle, DeregisterClient, ret);
938         }
939
940         /* Unlink device structure, free bits */
941         *linkp = link->next;
942
943         kfree(info);
944 }
945
946
947 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
948 {
949         int i;
950
951         i = pcmcia_get_first_tuple(handle, tuple);
952         if (i != CS_SUCCESS)
953                 return CS_NO_MORE_ITEMS;
954
955         i = pcmcia_get_tuple_data(handle, tuple);
956         if (i != CS_SUCCESS)
957                 return i;
958
959         return pcmcia_parse_tuple(handle, tuple, parse);
960 }
961
962 static void bluecard_config(dev_link_t *link)
963 {
964         client_handle_t handle = link->handle;
965         bluecard_info_t *info = link->priv;
966         tuple_t tuple;
967         u_short buf[256];
968         cisparse_t parse;
969         config_info_t config;
970         int i, n, last_ret, last_fn;
971
972         tuple.TupleData = (cisdata_t *)buf;
973         tuple.TupleOffset = 0;
974         tuple.TupleDataMax = 255;
975         tuple.Attributes = 0;
976
977         /* Get configuration register information */
978         tuple.DesiredTuple = CISTPL_CONFIG;
979         last_ret = first_tuple(handle, &tuple, &parse);
980         if (last_ret != CS_SUCCESS) {
981                 last_fn = ParseTuple;
982                 goto cs_failed;
983         }
984         link->conf.ConfigBase = parse.config.base;
985         link->conf.Present = parse.config.rmask[0];
986
987         /* Configure card */
988         link->state |= DEV_CONFIG;
989         i = pcmcia_get_configuration_info(handle, &config);
990         link->conf.Vcc = config.Vcc;
991
992         link->conf.ConfigIndex = 0x20;
993         link->io.NumPorts1 = 64;
994         link->io.IOAddrLines = 6;
995
996         for (n = 0; n < 0x400; n += 0x40) {
997                 link->io.BasePort1 = n ^ 0x300;
998                 i = pcmcia_request_io(link->handle, &link->io);
999                 if (i == CS_SUCCESS)
1000                         break;
1001         }
1002
1003         if (i != CS_SUCCESS) {
1004                 cs_error(link->handle, RequestIO, i);
1005                 goto failed;
1006         }
1007
1008         i = pcmcia_request_irq(link->handle, &link->irq);
1009         if (i != CS_SUCCESS) {
1010                 cs_error(link->handle, RequestIRQ, i);
1011                 link->irq.AssignedIRQ = 0;
1012         }
1013
1014         i = pcmcia_request_configuration(link->handle, &link->conf);
1015         if (i != CS_SUCCESS) {
1016                 cs_error(link->handle, RequestConfiguration, i);
1017                 goto failed;
1018         }
1019
1020         if (bluecard_open(info) != 0)
1021                 goto failed;
1022
1023         strcpy(info->node.dev_name, info->hdev->name);
1024         link->dev = &info->node;
1025         link->state &= ~DEV_CONFIG_PENDING;
1026
1027         return;
1028
1029 cs_failed:
1030         cs_error(link->handle, last_fn, last_ret);
1031
1032 failed:
1033         bluecard_release(link);
1034 }
1035
1036
1037 static void bluecard_release(dev_link_t *link)
1038 {
1039         bluecard_info_t *info = link->priv;
1040
1041         if (link->state & DEV_PRESENT)
1042                 bluecard_close(info);
1043
1044         del_timer(&(info->timer));
1045
1046         link->dev = NULL;
1047
1048         pcmcia_release_configuration(link->handle);
1049         pcmcia_release_io(link->handle, &link->io);
1050         pcmcia_release_irq(link->handle, &link->irq);
1051
1052         link->state &= ~DEV_CONFIG;
1053 }
1054
1055
1056 static int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1057 {
1058         dev_link_t *link = args->client_data;
1059         bluecard_info_t *info = link->priv;
1060
1061         switch (event) {
1062         case CS_EVENT_CARD_REMOVAL:
1063                 link->state &= ~DEV_PRESENT;
1064                 if (link->state & DEV_CONFIG) {
1065                         bluecard_close(info);
1066                         bluecard_release(link);
1067                 }
1068                 break;
1069         case CS_EVENT_CARD_INSERTION:
1070                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1071                 bluecard_config(link);
1072                 break;
1073         case CS_EVENT_PM_SUSPEND:
1074                 link->state |= DEV_SUSPEND;
1075                 /* Fall through... */
1076         case CS_EVENT_RESET_PHYSICAL:
1077                 if (link->state & DEV_CONFIG)
1078                         pcmcia_release_configuration(link->handle);
1079                 break;
1080         case CS_EVENT_PM_RESUME:
1081                 link->state &= ~DEV_SUSPEND;
1082                 /* Fall through... */
1083         case CS_EVENT_CARD_RESET:
1084                 if (DEV_OK(link))
1085                         pcmcia_request_configuration(link->handle, &link->conf);
1086                 break;
1087         }
1088
1089         return 0;
1090 }
1091
1092 static struct pcmcia_driver bluecard_driver = {
1093         .owner          = THIS_MODULE,
1094         .drv            = {
1095                 .name   = "bluecard_cs",
1096         },
1097         .attach         = bluecard_attach,
1098         .detach         = bluecard_detach,
1099 };
1100
1101 static int __init init_bluecard_cs(void)
1102 {
1103         return pcmcia_register_driver(&bluecard_driver);
1104 }
1105
1106
1107 static void __exit exit_bluecard_cs(void)
1108 {
1109         pcmcia_unregister_driver(&bluecard_driver);
1110         BUG_ON(dev_list != NULL);
1111 }
1112
1113 module_init(init_bluecard_cs);
1114 module_exit(exit_bluecard_cs);