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