vserver 1.9.5.x5
[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/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
39 #include <linux/skbuff.h>
40 #include <asm/io.h>
41
42 #include <pcmcia/version.h>
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 (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 (skb->pkt_type & 0x80) {
292
293                         wait_queue_head_t wait;
294                         unsigned char baud_reg;
295
296                         switch (skb->pkt_type) {
297                         case PKT_BAUD_RATE_460800:
298                                 baud_reg = REG_CONTROL_BAUD_RATE_460800;
299                                 break;
300                         case PKT_BAUD_RATE_230400:
301                                 baud_reg = REG_CONTROL_BAUD_RATE_230400;
302                                 break;
303                         case PKT_BAUD_RATE_115200:
304                                 baud_reg = REG_CONTROL_BAUD_RATE_115200;
305                                 break;
306                         case PKT_BAUD_RATE_57600:
307                                 /* Fall through... */
308                         default:
309                                 baud_reg = REG_CONTROL_BAUD_RATE_57600;
310                                 break;
311                         }
312
313                         /* Wait until the command reaches the baseband */
314                         init_waitqueue_head(&wait);
315                         interruptible_sleep_on_timeout(&wait, HZ / 10);
316
317                         /* Set baud on baseband */
318                         info->ctrl_reg &= ~0x03;
319                         info->ctrl_reg |= baud_reg;
320                         outb(info->ctrl_reg, iobase + REG_CONTROL);
321
322                         /* Enable RTS */
323                         info->ctrl_reg &= ~REG_CONTROL_RTS;
324                         outb(info->ctrl_reg, iobase + REG_CONTROL);
325
326                         /* Wait before the next HCI packet can be send */
327                         interruptible_sleep_on_timeout(&wait, HZ);
328
329                 }
330
331                 if (len == skb->len) {
332                         kfree_skb(skb);
333                 } else {
334                         skb_pull(skb, len);
335                         skb_queue_head(&(info->txq), skb);
336                 }
337
338                 info->hdev->stat.byte_tx += len;
339
340                 /* Change buffer */
341                 change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
342
343         } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
344
345         clear_bit(XMIT_SENDING, &(info->tx_state));
346 }
347
348
349 static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
350 {
351         int i, n, len;
352
353         outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
354
355         len = inb(iobase + offset);
356         n = 0;
357         i = 1;
358
359         while (n < len) {
360
361                 if (i == 16) {
362                         outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
363                         i = 0;
364                 }
365
366                 buf[n] = inb(iobase + offset + i);
367
368                 n++;
369                 i++;
370
371         }
372
373         return len;
374 }
375
376
377 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
378 {
379         unsigned int iobase;
380         unsigned char buf[31];
381         int i, len;
382
383         if (!info) {
384                 BT_ERR("Unknown device");
385                 return;
386         }
387
388         iobase = info->link.io.BasePort1;
389
390         if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
391                 bluecard_enable_activity_led(info);
392
393         len = bluecard_read(iobase, offset, buf, sizeof(buf));
394
395         for (i = 0; i < len; i++) {
396
397                 /* Allocate packet */
398                 if (info->rx_skb == NULL) {
399                         info->rx_state = RECV_WAIT_PACKET_TYPE;
400                         info->rx_count = 0;
401                         if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
402                                 BT_ERR("Can't allocate mem for new packet");
403                                 return;
404                         }
405                 }
406
407                 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
408
409                         info->rx_skb->dev = (void *) info->hdev;
410                         info->rx_skb->pkt_type = buf[i];
411
412                         switch (info->rx_skb->pkt_type) {
413
414                         case 0x00:
415                                 /* init packet */
416                                 if (offset != 0x00) {
417                                         set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
418                                         set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
419                                         set_bit(XMIT_SENDING_READY, &(info->tx_state));
420                                         bluecard_write_wakeup(info);
421                                 }
422
423                                 kfree_skb(info->rx_skb);
424                                 info->rx_skb = NULL;
425                                 break;
426
427                         case HCI_EVENT_PKT:
428                                 info->rx_state = RECV_WAIT_EVENT_HEADER;
429                                 info->rx_count = HCI_EVENT_HDR_SIZE;
430                                 break;
431
432                         case HCI_ACLDATA_PKT:
433                                 info->rx_state = RECV_WAIT_ACL_HEADER;
434                                 info->rx_count = HCI_ACL_HDR_SIZE;
435                                 break;
436
437                         case HCI_SCODATA_PKT:
438                                 info->rx_state = RECV_WAIT_SCO_HEADER;
439                                 info->rx_count = HCI_SCO_HDR_SIZE;
440                                 break;
441
442                         default:
443                                 /* unknown packet */
444                                 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
445                                 info->hdev->stat.err_rx++;
446
447                                 kfree_skb(info->rx_skb);
448                                 info->rx_skb = NULL;
449                                 break;
450
451                         }
452
453                 } else {
454
455                         *skb_put(info->rx_skb, 1) = buf[i];
456                         info->rx_count--;
457
458                         if (info->rx_count == 0) {
459
460                                 int dlen;
461                                 struct hci_event_hdr *eh;
462                                 struct hci_acl_hdr *ah;
463                                 struct hci_sco_hdr *sh;
464
465                                 switch (info->rx_state) {
466
467                                 case RECV_WAIT_EVENT_HEADER:
468                                         eh = (struct hci_event_hdr *)(info->rx_skb->data);
469                                         info->rx_state = RECV_WAIT_DATA;
470                                         info->rx_count = eh->plen;
471                                         break;
472
473                                 case RECV_WAIT_ACL_HEADER:
474                                         ah = (struct hci_acl_hdr *)(info->rx_skb->data);
475                                         dlen = __le16_to_cpu(ah->dlen);
476                                         info->rx_state = RECV_WAIT_DATA;
477                                         info->rx_count = dlen;
478                                         break;
479
480                                 case RECV_WAIT_SCO_HEADER:
481                                         sh = (struct hci_sco_hdr *)(info->rx_skb->data);
482                                         info->rx_state = RECV_WAIT_DATA;
483                                         info->rx_count = sh->dlen;
484                                         break;
485
486                                 case RECV_WAIT_DATA:
487                                         hci_recv_frame(info->rx_skb);
488                                         info->rx_skb = NULL;
489                                         break;
490
491                                 }
492
493                         }
494
495                 }
496
497
498         }
499
500         info->hdev->stat.byte_rx += len;
501 }
502
503
504 static irqreturn_t bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
505 {
506         bluecard_info_t *info = dev_inst;
507         unsigned int iobase;
508         unsigned char reg;
509
510         if (!info || !info->hdev) {
511                 BT_ERR("Call of irq %d for unknown device", irq);
512                 return IRQ_NONE;
513         }
514
515         if (!test_bit(CARD_READY, &(info->hw_state)))
516                 return IRQ_HANDLED;
517
518         iobase = info->link.io.BasePort1;
519
520         spin_lock(&(info->lock));
521
522         /* Disable interrupt */
523         info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
524         outb(info->ctrl_reg, iobase + REG_CONTROL);
525
526         reg = inb(iobase + REG_INTERRUPT);
527
528         if ((reg != 0x00) && (reg != 0xff)) {
529
530                 if (reg & 0x04) {
531                         bluecard_receive(info, 0x00);
532                         outb(0x04, iobase + REG_INTERRUPT);
533                         outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
534                 }
535
536                 if (reg & 0x08) {
537                         bluecard_receive(info, 0x10);
538                         outb(0x08, iobase + REG_INTERRUPT);
539                         outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
540                 }
541
542                 if (reg & 0x01) {
543                         set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
544                         outb(0x01, iobase + REG_INTERRUPT);
545                         bluecard_write_wakeup(info);
546                 }
547
548                 if (reg & 0x02) {
549                         set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
550                         outb(0x02, iobase + REG_INTERRUPT);
551                         bluecard_write_wakeup(info);
552                 }
553
554         }
555
556         /* Enable interrupt */
557         info->ctrl_reg |= REG_CONTROL_INTERRUPT;
558         outb(info->ctrl_reg, iobase + REG_CONTROL);
559
560         spin_unlock(&(info->lock));
561
562         return IRQ_HANDLED;
563 }
564
565
566
567 /* ======================== Device specific HCI commands ======================== */
568
569
570 static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
571 {
572         bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
573         struct sk_buff *skb;
574
575         /* Ericsson baud rate command */
576         unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
577
578         if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
579                 BT_ERR("Can't allocate mem for new packet");
580                 return -1;
581         }
582
583         switch (baud) {
584         case 460800:
585                 cmd[4] = 0x00;
586                 skb->pkt_type = PKT_BAUD_RATE_460800;
587                 break;
588         case 230400:
589                 cmd[4] = 0x01;
590                 skb->pkt_type = PKT_BAUD_RATE_230400;
591                 break;
592         case 115200:
593                 cmd[4] = 0x02;
594                 skb->pkt_type = PKT_BAUD_RATE_115200;
595                 break;
596         case 57600:
597                 /* Fall through... */
598         default:
599                 cmd[4] = 0x03;
600                 skb->pkt_type = PKT_BAUD_RATE_57600;
601                 break;
602         }
603
604         memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
605
606         skb_queue_tail(&(info->txq), skb);
607
608         bluecard_write_wakeup(info);
609
610         return 0;
611 }
612
613
614
615 /* ======================== HCI interface ======================== */
616
617
618 static int bluecard_hci_flush(struct hci_dev *hdev)
619 {
620         bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
621
622         /* Drop TX queue */
623         skb_queue_purge(&(info->txq));
624
625         return 0;
626 }
627
628
629 static int bluecard_hci_open(struct hci_dev *hdev)
630 {
631         bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
632         unsigned int iobase = info->link.io.BasePort1;
633
634         if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
635                 bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
636
637         if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
638                 return 0;
639
640         if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
641                 /* Enable LED */
642                 outb(0x08 | 0x20, iobase + 0x30);
643         }
644
645         return 0;
646 }
647
648
649 static int bluecard_hci_close(struct hci_dev *hdev)
650 {
651         bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
652         unsigned int iobase = info->link.io.BasePort1;
653
654         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
655                 return 0;
656
657         bluecard_hci_flush(hdev);
658
659         if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
660                 /* Disable LED */
661                 outb(0x00, iobase + 0x30);
662         }
663
664         return 0;
665 }
666
667
668 static int bluecard_hci_send_frame(struct sk_buff *skb)
669 {
670         bluecard_info_t *info;
671         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
672
673         if (!hdev) {
674                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
675                 return -ENODEV;
676         }
677
678         info = (bluecard_info_t *)(hdev->driver_data);
679
680         switch (skb->pkt_type) {
681         case HCI_COMMAND_PKT:
682                 hdev->stat.cmd_tx++;
683                 break;
684         case HCI_ACLDATA_PKT:
685                 hdev->stat.acl_tx++;
686                 break;
687         case HCI_SCODATA_PKT:
688                 hdev->stat.sco_tx++;
689                 break;
690         };
691
692         /* Prepend skb with frame type */
693         memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
694         skb_queue_tail(&(info->txq), skb);
695
696         bluecard_write_wakeup(info);
697
698         return 0;
699 }
700
701
702 static void bluecard_hci_destruct(struct hci_dev *hdev)
703 {
704 }
705
706
707 static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
708 {
709         return -ENOIOCTLCMD;
710 }
711
712
713
714 /* ======================== Card services HCI interaction ======================== */
715
716
717 static int bluecard_open(bluecard_info_t *info)
718 {
719         unsigned int iobase = info->link.io.BasePort1;
720         struct hci_dev *hdev;
721         unsigned char id;
722
723         spin_lock_init(&(info->lock));
724
725         init_timer(&(info->timer));
726         info->timer.function = &bluecard_activity_led_timeout;
727         info->timer.data = (u_long)info;
728
729         skb_queue_head_init(&(info->txq));
730
731         info->rx_state = RECV_WAIT_PACKET_TYPE;
732         info->rx_count = 0;
733         info->rx_skb = NULL;
734
735         /* Initialize HCI device */
736         hdev = hci_alloc_dev();
737         if (!hdev) {
738                 BT_ERR("Can't allocate HCI device");
739                 return -ENOMEM;
740         }
741
742         info->hdev = hdev;
743
744         hdev->type = HCI_PCCARD;
745         hdev->driver_data = info;
746
747         hdev->open     = bluecard_hci_open;
748         hdev->close    = bluecard_hci_close;
749         hdev->flush    = bluecard_hci_flush;
750         hdev->send     = bluecard_hci_send_frame;
751         hdev->destruct = bluecard_hci_destruct;
752         hdev->ioctl    = bluecard_hci_ioctl;
753
754         hdev->owner = THIS_MODULE;
755
756         id = inb(iobase + 0x30);
757
758         if ((id & 0x0f) == 0x02)
759                 set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
760
761         if (id & 0x10)
762                 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
763
764         if (id & 0x20)
765                 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
766
767         /* Reset card */
768         info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
769         outb(info->ctrl_reg, iobase + REG_CONTROL);
770
771         /* Turn FPGA off */
772         outb(0x80, iobase + 0x30);
773
774         /* Wait some time */
775         msleep(10);
776
777         /* Turn FPGA on */
778         outb(0x00, iobase + 0x30);
779
780         /* Activate card */
781         info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
782         outb(info->ctrl_reg, iobase + REG_CONTROL);
783
784         /* Enable interrupt */
785         outb(0xff, iobase + REG_INTERRUPT);
786         info->ctrl_reg |= REG_CONTROL_INTERRUPT;
787         outb(info->ctrl_reg, iobase + REG_CONTROL);
788
789         if ((id & 0x0f) == 0x03) {
790                 /* Disable RTS */
791                 info->ctrl_reg |= REG_CONTROL_RTS;
792                 outb(info->ctrl_reg, iobase + REG_CONTROL);
793
794                 /* Set baud rate */
795                 info->ctrl_reg |= 0x03;
796                 outb(info->ctrl_reg, iobase + REG_CONTROL);
797
798                 /* Enable RTS */
799                 info->ctrl_reg &= ~REG_CONTROL_RTS;
800                 outb(info->ctrl_reg, iobase + REG_CONTROL);
801
802                 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
803                 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
804                 set_bit(XMIT_SENDING_READY, &(info->tx_state));
805         }
806
807         /* Start the RX buffers */
808         outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
809         outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
810
811         /* Signal that the hardware is ready */
812         set_bit(CARD_READY, &(info->hw_state));
813
814         /* Drop TX queue */
815         skb_queue_purge(&(info->txq));
816
817         /* Control the point at which RTS is enabled */
818         outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
819
820         /* Timeout before it is safe to send the first HCI packet */
821         msleep(1250);
822
823         /* Register HCI device */
824         if (hci_register_dev(hdev) < 0) {
825                 BT_ERR("Can't register HCI device");
826                 info->hdev = NULL;
827                 hci_free_dev(hdev);
828                 return -ENODEV;
829         }
830
831         return 0;
832 }
833
834
835 static int bluecard_close(bluecard_info_t *info)
836 {
837         unsigned int iobase = info->link.io.BasePort1;
838         struct hci_dev *hdev = info->hdev;
839
840         if (!hdev)
841                 return -ENODEV;
842
843         bluecard_hci_close(hdev);
844
845         clear_bit(CARD_READY, &(info->hw_state));
846
847         /* Reset card */
848         info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
849         outb(info->ctrl_reg, iobase + REG_CONTROL);
850
851         /* Turn FPGA off */
852         outb(0x80, iobase + 0x30);
853
854         if (hci_unregister_dev(hdev) < 0)
855                 BT_ERR("Can't unregister HCI device %s", hdev->name);
856
857         hci_free_dev(hdev);
858
859         return 0;
860 }
861
862 static dev_link_t *bluecard_attach(void)
863 {
864         bluecard_info_t *info;
865         client_reg_t client_reg;
866         dev_link_t *link;
867         int ret;
868
869         /* Create new info device */
870         info = kmalloc(sizeof(*info), GFP_KERNEL);
871         if (!info)
872                 return NULL;
873         memset(info, 0, sizeof(*info));
874
875         link = &info->link;
876         link->priv = info;
877
878         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
879         link->io.NumPorts1 = 8;
880         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
881         link->irq.IRQInfo1 = IRQ_LEVEL_ID;
882
883         link->irq.Handler = bluecard_interrupt;
884         link->irq.Instance = info;
885
886         link->conf.Attributes = CONF_ENABLE_IRQ;
887         link->conf.Vcc = 50;
888         link->conf.IntType = INT_MEMORY_AND_IO;
889
890         /* Register with Card Services */
891         link->next = dev_list;
892         dev_list = link;
893         client_reg.dev_info = &dev_info;
894         client_reg.EventMask =
895                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
896                 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
897                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
898         client_reg.event_handler = &bluecard_event;
899         client_reg.Version = 0x0210;
900         client_reg.event_callback_args.client_data = link;
901
902         ret = pcmcia_register_client(&link->handle, &client_reg);
903         if (ret != CS_SUCCESS) {
904                 cs_error(link->handle, RegisterClient, ret);
905                 bluecard_detach(link);
906                 return NULL;
907         }
908
909         return link;
910 }
911
912
913 static void bluecard_detach(dev_link_t *link)
914 {
915         bluecard_info_t *info = link->priv;
916         dev_link_t **linkp;
917         int ret;
918
919         /* Locate device structure */
920         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
921                 if (*linkp == link)
922                         break;
923
924         if (*linkp == NULL)
925                 return;
926
927         if (link->state & DEV_CONFIG)
928                 bluecard_release(link);
929
930         if (link->handle) {
931                 ret = pcmcia_deregister_client(link->handle);
932                 if (ret != CS_SUCCESS)
933                         cs_error(link->handle, DeregisterClient, ret);
934         }
935
936         /* Unlink device structure, free bits */
937         *linkp = link->next;
938
939         kfree(info);
940 }
941
942
943 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
944 {
945         int i;
946
947         i = pcmcia_get_first_tuple(handle, tuple);
948         if (i != CS_SUCCESS)
949                 return CS_NO_MORE_ITEMS;
950
951         i = pcmcia_get_tuple_data(handle, tuple);
952         if (i != CS_SUCCESS)
953                 return i;
954
955         return pcmcia_parse_tuple(handle, tuple, parse);
956 }
957
958 static void bluecard_config(dev_link_t *link)
959 {
960         client_handle_t handle = link->handle;
961         bluecard_info_t *info = link->priv;
962         tuple_t tuple;
963         u_short buf[256];
964         cisparse_t parse;
965         config_info_t config;
966         int i, n, last_ret, last_fn;
967
968         tuple.TupleData = (cisdata_t *)buf;
969         tuple.TupleOffset = 0;
970         tuple.TupleDataMax = 255;
971         tuple.Attributes = 0;
972
973         /* Get configuration register information */
974         tuple.DesiredTuple = CISTPL_CONFIG;
975         last_ret = first_tuple(handle, &tuple, &parse);
976         if (last_ret != CS_SUCCESS) {
977                 last_fn = ParseTuple;
978                 goto cs_failed;
979         }
980         link->conf.ConfigBase = parse.config.base;
981         link->conf.Present = parse.config.rmask[0];
982
983         /* Configure card */
984         link->state |= DEV_CONFIG;
985         i = pcmcia_get_configuration_info(handle, &config);
986         link->conf.Vcc = config.Vcc;
987
988         link->conf.ConfigIndex = 0x20;
989         link->io.NumPorts1 = 64;
990         link->io.IOAddrLines = 6;
991
992         for (n = 0; n < 0x400; n += 0x40) {
993                 link->io.BasePort1 = n ^ 0x300;
994                 i = pcmcia_request_io(link->handle, &link->io);
995                 if (i == CS_SUCCESS)
996                         break;
997         }
998
999         if (i != CS_SUCCESS) {
1000                 cs_error(link->handle, RequestIO, i);
1001                 goto failed;
1002         }
1003
1004         i = pcmcia_request_irq(link->handle, &link->irq);
1005         if (i != CS_SUCCESS) {
1006                 cs_error(link->handle, RequestIRQ, i);
1007                 link->irq.AssignedIRQ = 0;
1008         }
1009
1010         i = pcmcia_request_configuration(link->handle, &link->conf);
1011         if (i != CS_SUCCESS) {
1012                 cs_error(link->handle, RequestConfiguration, i);
1013                 goto failed;
1014         }
1015
1016         if (bluecard_open(info) != 0)
1017                 goto failed;
1018
1019         strcpy(info->node.dev_name, info->hdev->name);
1020         link->dev = &info->node;
1021         link->state &= ~DEV_CONFIG_PENDING;
1022
1023         return;
1024
1025 cs_failed:
1026         cs_error(link->handle, last_fn, last_ret);
1027
1028 failed:
1029         bluecard_release(link);
1030 }
1031
1032
1033 static void bluecard_release(dev_link_t *link)
1034 {
1035         bluecard_info_t *info = link->priv;
1036
1037         if (link->state & DEV_PRESENT)
1038                 bluecard_close(info);
1039
1040         del_timer(&(info->timer));
1041
1042         link->dev = NULL;
1043
1044         pcmcia_release_configuration(link->handle);
1045         pcmcia_release_io(link->handle, &link->io);
1046         pcmcia_release_irq(link->handle, &link->irq);
1047
1048         link->state &= ~DEV_CONFIG;
1049 }
1050
1051
1052 static int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1053 {
1054         dev_link_t *link = args->client_data;
1055         bluecard_info_t *info = link->priv;
1056
1057         switch (event) {
1058         case CS_EVENT_CARD_REMOVAL:
1059                 link->state &= ~DEV_PRESENT;
1060                 if (link->state & DEV_CONFIG) {
1061                         bluecard_close(info);
1062                         bluecard_release(link);
1063                 }
1064                 break;
1065         case CS_EVENT_CARD_INSERTION:
1066                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1067                 bluecard_config(link);
1068                 break;
1069         case CS_EVENT_PM_SUSPEND:
1070                 link->state |= DEV_SUSPEND;
1071                 /* Fall through... */
1072         case CS_EVENT_RESET_PHYSICAL:
1073                 if (link->state & DEV_CONFIG)
1074                         pcmcia_release_configuration(link->handle);
1075                 break;
1076         case CS_EVENT_PM_RESUME:
1077                 link->state &= ~DEV_SUSPEND;
1078                 /* Fall through... */
1079         case CS_EVENT_CARD_RESET:
1080                 if (DEV_OK(link))
1081                         pcmcia_request_configuration(link->handle, &link->conf);
1082                 break;
1083         }
1084
1085         return 0;
1086 }
1087
1088 static struct pcmcia_driver bluecard_driver = {
1089         .owner          = THIS_MODULE,
1090         .drv            = {
1091                 .name   = "bluecard_cs",
1092         },
1093         .attach         = bluecard_attach,
1094         .detach         = bluecard_detach,
1095 };
1096
1097 static int __init init_bluecard_cs(void)
1098 {
1099         return pcmcia_register_driver(&bluecard_driver);
1100 }
1101
1102
1103 static void __exit exit_bluecard_cs(void)
1104 {
1105         pcmcia_unregister_driver(&bluecard_driver);
1106         BUG_ON(dev_list != NULL);
1107 }
1108
1109 module_init(init_bluecard_cs);
1110 module_exit(exit_bluecard_cs);