ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/timer.h>
32 #include <linux/errno.h>
33 #include <linux/ptrace.h>
34 #include <linux/ioport.h>
35 #include <linux/spinlock.h>
36 #include <linux/skbuff.h>
37 #include <asm/io.h>
38
39 #include <pcmcia/version.h>
40 #include <pcmcia/cs_types.h>
41 #include <pcmcia/cs.h>
42 #include <pcmcia/cistpl.h>
43 #include <pcmcia/ciscode.h>
44 #include <pcmcia/ds.h>
45 #include <pcmcia/cisreg.h>
46
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
49
50
51
52 /* ======================== Module parameters ======================== */
53
54
55 /* Bit map of interrupts to choose from */
56 static u_int irq_mask = 0x86bc;
57 static int irq_list[4] = { -1 };
58
59 MODULE_PARM(irq_mask, "i");
60 MODULE_PARM(irq_list, "1-4i");
61
62 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
63 MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
64 MODULE_LICENSE("GPL");
65
66
67
68 /* ======================== Local structures ======================== */
69
70
71 typedef struct bluecard_info_t {
72         dev_link_t link;
73         dev_node_t node;
74
75         struct hci_dev *hdev;
76
77         spinlock_t lock;                /* For serializing operations */
78         struct timer_list timer;        /* For LED control */
79
80         struct sk_buff_head txq;
81         unsigned long tx_state;
82
83         unsigned long rx_state;
84         unsigned long rx_count;
85         struct sk_buff *rx_skb;
86
87         unsigned char ctrl_reg;
88         unsigned long hw_state;         /* Status of the hardware and LED control */
89 } bluecard_info_t;
90
91
92 void bluecard_config(dev_link_t *link);
93 void bluecard_release(dev_link_t *link);
94 int bluecard_event(event_t event, int priority, event_callback_args_t *args);
95
96 static dev_info_t dev_info = "bluecard_cs";
97
98 dev_link_t *bluecard_attach(void);
99 void bluecard_detach(dev_link_t *);
100
101 static dev_link_t *dev_list = NULL;
102
103
104 /* Default baud rate: 57600, 115200, 230400 or 460800 */
105 #define DEFAULT_BAUD_RATE  230400
106
107
108 /* Hardware states */
109 #define CARD_READY             1
110 #define CARD_HAS_PCCARD_ID     4
111 #define CARD_HAS_POWER_LED     5
112 #define CARD_HAS_ACTIVITY_LED  6
113
114 /* Transmit states  */
115 #define XMIT_SENDING         1
116 #define XMIT_WAKEUP          2
117 #define XMIT_BUFFER_NUMBER   5  /* unset = buffer one, set = buffer two */
118 #define XMIT_BUF_ONE_READY   6
119 #define XMIT_BUF_TWO_READY   7
120 #define XMIT_SENDING_READY   8
121
122 /* Receiver states */
123 #define RECV_WAIT_PACKET_TYPE   0
124 #define RECV_WAIT_EVENT_HEADER  1
125 #define RECV_WAIT_ACL_HEADER    2
126 #define RECV_WAIT_SCO_HEADER    3
127 #define RECV_WAIT_DATA          4
128
129 /* Special packet types */
130 #define PKT_BAUD_RATE_57600   0x80
131 #define PKT_BAUD_RATE_115200  0x81
132 #define PKT_BAUD_RATE_230400  0x82
133 #define PKT_BAUD_RATE_460800  0x83
134
135
136 /* These are the register offsets */
137 #define REG_COMMAND     0x20
138 #define REG_INTERRUPT   0x21
139 #define REG_CONTROL     0x22
140 #define REG_RX_CONTROL  0x24
141 #define REG_CARD_RESET  0x30
142 #define REG_LED_CTRL    0x30
143
144 /* REG_COMMAND */
145 #define REG_COMMAND_TX_BUF_ONE  0x01
146 #define REG_COMMAND_TX_BUF_TWO  0x02
147 #define REG_COMMAND_RX_BUF_ONE  0x04
148 #define REG_COMMAND_RX_BUF_TWO  0x08
149 #define REG_COMMAND_RX_WIN_ONE  0x00
150 #define REG_COMMAND_RX_WIN_TWO  0x10
151
152 /* REG_CONTROL */
153 #define REG_CONTROL_BAUD_RATE_57600   0x00
154 #define REG_CONTROL_BAUD_RATE_115200  0x01
155 #define REG_CONTROL_BAUD_RATE_230400  0x02
156 #define REG_CONTROL_BAUD_RATE_460800  0x03
157 #define REG_CONTROL_RTS               0x04
158 #define REG_CONTROL_BT_ON             0x08
159 #define REG_CONTROL_BT_RESET          0x10
160 #define REG_CONTROL_BT_RES_PU         0x20
161 #define REG_CONTROL_INTERRUPT         0x40
162 #define REG_CONTROL_CARD_RESET        0x80
163
164 /* REG_RX_CONTROL */
165 #define RTS_LEVEL_SHIFT_BITS  0x02
166
167
168
169 /* ======================== LED handling routines ======================== */
170
171
172 void bluecard_activity_led_timeout(u_long arg)
173 {
174         bluecard_info_t *info = (bluecard_info_t *)arg;
175         unsigned int iobase = info->link.io.BasePort1;
176
177         if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
178                 return;
179
180         if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
181                 /* Disable activity LED */
182                 outb(0x08 | 0x20, iobase + 0x30);
183         } else {
184                 /* Disable power LED */
185                 outb(0x00, iobase + 0x30);
186         }
187 }
188
189
190 static void bluecard_enable_activity_led(bluecard_info_t *info)
191 {
192         unsigned int iobase = info->link.io.BasePort1;
193
194         if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
195                 return;
196
197         if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
198                 /* Enable activity LED */
199                 outb(0x10 | 0x40, iobase + 0x30);
200
201                 /* Stop the LED after HZ/4 */
202                 mod_timer(&(info->timer), jiffies + HZ / 4);
203         } else {
204                 /* Enable power LED */
205                 outb(0x08 | 0x20, iobase + 0x30);
206
207                 /* Stop the LED after HZ/2 */
208                 mod_timer(&(info->timer), jiffies + HZ / 2);
209         }
210 }
211
212
213
214 /* ======================== Interrupt handling ======================== */
215
216
217 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
218 {
219         int i, actual;
220
221         actual = (len > 15) ? 15 : len;
222
223         outb_p(actual, iobase + offset);
224
225         for (i = 0; i < actual; i++)
226                 outb_p(buf[i], iobase + offset + i + 1);
227
228         return actual;
229 }
230
231
232 static void bluecard_write_wakeup(bluecard_info_t *info)
233 {
234         if (!info) {
235                 BT_ERR("Unknown device");
236                 return;
237         }
238
239         if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
240                 return;
241
242         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
243                 set_bit(XMIT_WAKEUP, &(info->tx_state));
244                 return;
245         }
246
247         do {
248                 register unsigned int iobase = info->link.io.BasePort1;
249                 register unsigned int offset;
250                 register unsigned char command;
251                 register unsigned long ready_bit;
252                 register struct sk_buff *skb;
253                 register int len;
254
255                 clear_bit(XMIT_WAKEUP, &(info->tx_state));
256
257                 if (!(info->link.state & DEV_PRESENT))
258                         return;
259
260                 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
261                         if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
262                                 break;
263                         offset = 0x10;
264                         command = REG_COMMAND_TX_BUF_TWO;
265                         ready_bit = XMIT_BUF_TWO_READY;
266                 } else {
267                         if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
268                                 break;
269                         offset = 0x00;
270                         command = REG_COMMAND_TX_BUF_ONE;
271                         ready_bit = XMIT_BUF_ONE_READY;
272                 }
273
274                 if (!(skb = skb_dequeue(&(info->txq))))
275                         break;
276
277                 if (skb->pkt_type & 0x80) {
278                         /* Disable RTS */
279                         info->ctrl_reg |= REG_CONTROL_RTS;
280                         outb(info->ctrl_reg, iobase + REG_CONTROL);
281                 }
282
283                 /* Activate LED */
284                 bluecard_enable_activity_led(info);
285
286                 /* Send frame */
287                 len = bluecard_write(iobase, offset, skb->data, skb->len);
288
289                 /* Tell the FPGA to send the data */
290                 outb_p(command, iobase + REG_COMMAND);
291
292                 /* Mark the buffer as dirty */
293                 clear_bit(ready_bit, &(info->tx_state));
294
295                 if (skb->pkt_type & 0x80) {
296
297                         wait_queue_head_t wait;
298                         unsigned char baud_reg;
299
300                         switch (skb->pkt_type) {
301                         case PKT_BAUD_RATE_460800:
302                                 baud_reg = REG_CONTROL_BAUD_RATE_460800;
303                                 break;
304                         case PKT_BAUD_RATE_230400:
305                                 baud_reg = REG_CONTROL_BAUD_RATE_230400;
306                                 break;
307                         case PKT_BAUD_RATE_115200:
308                                 baud_reg = REG_CONTROL_BAUD_RATE_115200;
309                                 break;
310                         case PKT_BAUD_RATE_57600:
311                                 /* Fall through... */
312                         default:
313                                 baud_reg = REG_CONTROL_BAUD_RATE_57600;
314                                 break;
315                         }
316
317                         /* Wait until the command reaches the baseband */
318                         init_waitqueue_head(&wait);
319                         interruptible_sleep_on_timeout(&wait, HZ / 10);
320
321                         /* Set baud on baseband */
322                         info->ctrl_reg &= ~0x03;
323                         info->ctrl_reg |= baud_reg;
324                         outb(info->ctrl_reg, iobase + REG_CONTROL);
325
326                         /* Enable RTS */
327                         info->ctrl_reg &= ~REG_CONTROL_RTS;
328                         outb(info->ctrl_reg, iobase + REG_CONTROL);
329
330                         /* Wait before the next HCI packet can be send */
331                         interruptible_sleep_on_timeout(&wait, HZ);
332
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 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         set_current_state(TASK_INTERRUPTIBLE);
780         schedule_timeout(HZ / 100);
781
782         /* Turn FPGA on */
783         outb(0x00, iobase + 0x30);
784
785         /* Activate card */
786         info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
787         outb(info->ctrl_reg, iobase + REG_CONTROL);
788
789         /* Enable interrupt */
790         outb(0xff, iobase + REG_INTERRUPT);
791         info->ctrl_reg |= REG_CONTROL_INTERRUPT;
792         outb(info->ctrl_reg, iobase + REG_CONTROL);
793
794         if ((id & 0x0f) == 0x03) {
795                 /* Disable RTS */
796                 info->ctrl_reg |= REG_CONTROL_RTS;
797                 outb(info->ctrl_reg, iobase + REG_CONTROL);
798
799                 /* Set baud rate */
800                 info->ctrl_reg |= 0x03;
801                 outb(info->ctrl_reg, iobase + REG_CONTROL);
802
803                 /* Enable RTS */
804                 info->ctrl_reg &= ~REG_CONTROL_RTS;
805                 outb(info->ctrl_reg, iobase + REG_CONTROL);
806
807                 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
808                 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
809                 set_bit(XMIT_SENDING_READY, &(info->tx_state));
810         }
811
812         /* Start the RX buffers */
813         outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
814         outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
815
816         /* Signal that the hardware is ready */
817         set_bit(CARD_READY, &(info->hw_state));
818
819         /* Drop TX queue */
820         skb_queue_purge(&(info->txq));
821
822         /* Control the point at which RTS is enabled */
823         outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
824
825         /* Timeout before it is safe to send the first HCI packet */
826         set_current_state(TASK_INTERRUPTIBLE);
827         schedule_timeout((HZ * 5) / 4);         // or set it to 3/2
828
829         /* Register HCI device */
830         if (hci_register_dev(hdev) < 0) {
831                 BT_ERR("Can't register HCI device");
832                 info->hdev = NULL;
833                 hci_free_dev(hdev);
834                 return -ENODEV;
835         }
836
837         return 0;
838 }
839
840
841 int bluecard_close(bluecard_info_t *info)
842 {
843         unsigned int iobase = info->link.io.BasePort1;
844         struct hci_dev *hdev = info->hdev;
845
846         if (!hdev)
847                 return -ENODEV;
848
849         bluecard_hci_close(hdev);
850
851         clear_bit(CARD_READY, &(info->hw_state));
852
853         /* Reset card */
854         info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
855         outb(info->ctrl_reg, iobase + REG_CONTROL);
856
857         /* Turn FPGA off */
858         outb(0x80, iobase + 0x30);
859
860         if (hci_unregister_dev(hdev) < 0)
861                 BT_ERR("Can't unregister HCI device %s", hdev->name);
862
863         hci_free_dev(hdev);
864
865         return 0;
866 }
867
868 dev_link_t *bluecard_attach(void)
869 {
870         bluecard_info_t *info;
871         client_reg_t client_reg;
872         dev_link_t *link;
873         int i, ret;
874
875         /* Create new info device */
876         info = kmalloc(sizeof(*info), GFP_KERNEL);
877         if (!info)
878                 return NULL;
879         memset(info, 0, sizeof(*info));
880
881         link = &info->link;
882         link->priv = info;
883
884         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
885         link->io.NumPorts1 = 8;
886         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
887         link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
888
889         if (irq_list[0] == -1)
890                 link->irq.IRQInfo2 = irq_mask;
891         else
892                 for (i = 0; i < 4; i++)
893                         link->irq.IRQInfo2 |= 1 << irq_list[i];
894
895         link->irq.Handler = bluecard_interrupt;
896         link->irq.Instance = info;
897
898         link->conf.Attributes = CONF_ENABLE_IRQ;
899         link->conf.Vcc = 50;
900         link->conf.IntType = INT_MEMORY_AND_IO;
901
902         /* Register with Card Services */
903         link->next = dev_list;
904         dev_list = link;
905         client_reg.dev_info = &dev_info;
906         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
907         client_reg.EventMask =
908                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
909                 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
910                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
911         client_reg.event_handler = &bluecard_event;
912         client_reg.Version = 0x0210;
913         client_reg.event_callback_args.client_data = link;
914
915         ret = pcmcia_register_client(&link->handle, &client_reg);
916         if (ret != CS_SUCCESS) {
917                 cs_error(link->handle, RegisterClient, ret);
918                 bluecard_detach(link);
919                 return NULL;
920         }
921
922         return link;
923 }
924
925
926 void bluecard_detach(dev_link_t *link)
927 {
928         bluecard_info_t *info = link->priv;
929         dev_link_t **linkp;
930         int ret;
931
932         /* Locate device structure */
933         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
934                 if (*linkp == link)
935                         break;
936
937         if (*linkp == NULL)
938                 return;
939
940         if (link->state & DEV_CONFIG)
941                 bluecard_release(link);
942
943         if (link->handle) {
944                 ret = pcmcia_deregister_client(link->handle);
945                 if (ret != CS_SUCCESS)
946                         cs_error(link->handle, DeregisterClient, ret);
947         }
948
949         /* Unlink device structure, free bits */
950         *linkp = link->next;
951
952         kfree(info);
953 }
954
955
956 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
957 {
958         int i;
959
960         i = pcmcia_get_first_tuple(handle, tuple);
961         if (i != CS_SUCCESS)
962                 return CS_NO_MORE_ITEMS;
963
964         i = pcmcia_get_tuple_data(handle, tuple);
965         if (i != CS_SUCCESS)
966                 return i;
967
968         return pcmcia_parse_tuple(handle, tuple, parse);
969 }
970
971 void bluecard_config(dev_link_t *link)
972 {
973         client_handle_t handle = link->handle;
974         bluecard_info_t *info = link->priv;
975         tuple_t tuple;
976         u_short buf[256];
977         cisparse_t parse;
978         config_info_t config;
979         int i, n, last_ret, last_fn;
980
981         tuple.TupleData = (cisdata_t *)buf;
982         tuple.TupleOffset = 0;
983         tuple.TupleDataMax = 255;
984         tuple.Attributes = 0;
985
986         /* Get configuration register information */
987         tuple.DesiredTuple = CISTPL_CONFIG;
988         last_ret = first_tuple(handle, &tuple, &parse);
989         if (last_ret != CS_SUCCESS) {
990                 last_fn = ParseTuple;
991                 goto cs_failed;
992         }
993         link->conf.ConfigBase = parse.config.base;
994         link->conf.Present = parse.config.rmask[0];
995
996         /* Configure card */
997         link->state |= DEV_CONFIG;
998         i = pcmcia_get_configuration_info(handle, &config);
999         link->conf.Vcc = config.Vcc;
1000
1001         link->conf.ConfigIndex = 0x20;
1002         link->io.NumPorts1 = 64;
1003         link->io.IOAddrLines = 6;
1004
1005         for (n = 0; n < 0x400; n += 0x40) {
1006                 link->io.BasePort1 = n ^ 0x300;
1007                 i = pcmcia_request_io(link->handle, &link->io);
1008                 if (i == CS_SUCCESS)
1009                         break;
1010         }
1011
1012         if (i != CS_SUCCESS) {
1013                 cs_error(link->handle, RequestIO, i);
1014                 goto failed;
1015         }
1016
1017         i = pcmcia_request_irq(link->handle, &link->irq);
1018         if (i != CS_SUCCESS) {
1019                 cs_error(link->handle, RequestIRQ, i);
1020                 link->irq.AssignedIRQ = 0;
1021         }
1022
1023         i = pcmcia_request_configuration(link->handle, &link->conf);
1024         if (i != CS_SUCCESS) {
1025                 cs_error(link->handle, RequestConfiguration, i);
1026                 goto failed;
1027         }
1028
1029         if (bluecard_open(info) != 0)
1030                 goto failed;
1031
1032         strcpy(info->node.dev_name, info->hdev->name);
1033         link->dev = &info->node;
1034         link->state &= ~DEV_CONFIG_PENDING;
1035
1036         return;
1037
1038 cs_failed:
1039         cs_error(link->handle, last_fn, last_ret);
1040
1041 failed:
1042         bluecard_release(link);
1043 }
1044
1045
1046 void bluecard_release(dev_link_t *link)
1047 {
1048         bluecard_info_t *info = link->priv;
1049
1050         if (link->state & DEV_PRESENT)
1051                 bluecard_close(info);
1052
1053         del_timer(&(info->timer));
1054
1055         link->dev = NULL;
1056
1057         pcmcia_release_configuration(link->handle);
1058         pcmcia_release_io(link->handle, &link->io);
1059         pcmcia_release_irq(link->handle, &link->irq);
1060
1061         link->state &= ~DEV_CONFIG;
1062 }
1063
1064
1065 int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1066 {
1067         dev_link_t *link = args->client_data;
1068         bluecard_info_t *info = link->priv;
1069
1070         switch (event) {
1071         case CS_EVENT_CARD_REMOVAL:
1072                 link->state &= ~DEV_PRESENT;
1073                 if (link->state & DEV_CONFIG) {
1074                         bluecard_close(info);
1075                         bluecard_release(link);
1076                 }
1077                 break;
1078         case CS_EVENT_CARD_INSERTION:
1079                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1080                 bluecard_config(link);
1081                 break;
1082         case CS_EVENT_PM_SUSPEND:
1083                 link->state |= DEV_SUSPEND;
1084                 /* Fall through... */
1085         case CS_EVENT_RESET_PHYSICAL:
1086                 if (link->state & DEV_CONFIG)
1087                         pcmcia_release_configuration(link->handle);
1088                 break;
1089         case CS_EVENT_PM_RESUME:
1090                 link->state &= ~DEV_SUSPEND;
1091                 /* Fall through... */
1092         case CS_EVENT_CARD_RESET:
1093                 if (DEV_OK(link))
1094                         pcmcia_request_configuration(link->handle, &link->conf);
1095                 break;
1096         }
1097
1098         return 0;
1099 }
1100
1101 static struct pcmcia_driver bluecard_driver = {
1102         .owner          = THIS_MODULE,
1103         .drv            = {
1104                 .name   = "bluecard_cs",
1105         },
1106         .attach         = bluecard_attach,
1107         .detach         = bluecard_detach,
1108 };
1109
1110 static int __init init_bluecard_cs(void)
1111 {
1112         return pcmcia_register_driver(&bluecard_driver);
1113 }
1114
1115
1116 static void __exit exit_bluecard_cs(void)
1117 {
1118         pcmcia_unregister_driver(&bluecard_driver);
1119
1120         /* XXX: this really needs to move into generic code.. */
1121         while (dev_list != NULL)
1122                 bluecard_detach(dev_list);
1123 }
1124
1125 module_init(init_bluecard_cs);
1126 module_exit(exit_bluecard_cs);