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