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