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