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