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
bluecard_activity_led_timeout(u_long arg)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
bluecard_enable_activity_led(bluecard_info_t * info)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
bluecard_write(unsigned int iobase,unsigned int offset,__u8 * buf,int len)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
bluecard_write_wakeup(bluecard_info_t * info)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
bluecard_read(unsigned int iobase,unsigned int offset,__u8 * buf,int size)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
bluecard_receive(bluecard_info_t * info,unsigned int offset)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
bluecard_interrupt(int irq,void * dev_inst,struct pt_regs * regs)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
bluecard_hci_set_baud_rate(struct hci_dev * hdev,int baud)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
bluecard_hci_flush(struct hci_dev * hdev)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
bluecard_hci_open(struct hci_dev * hdev)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
bluecard_hci_close(struct hci_dev * hdev)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
bluecard_hci_send_frame(struct sk_buff * skb)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
bluecard_hci_destruct(struct hci_dev * hdev)693 static void bluecard_hci_destruct(struct hci_dev *hdev)
694 {
695 }
696
697
bluecard_hci_ioctl(struct hci_dev * hdev,unsigned int cmd,unsigned long arg)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
bluecard_open(bluecard_info_t * info)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
bluecard_close(bluecard_info_t * info)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 if (info->link.state & DEV_CONFIG_PENDING)
807 return -ENODEV;
808
809 bluecard_hci_close(hdev);
810
811 clear_bit(CARD_READY, &(info->hw_state));
812
813 /* Reset card */
814 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
815 outb(info->ctrl_reg, iobase + REG_CONTROL);
816
817 /* Turn FPGA off */
818 outb(0x80, iobase + 0x30);
819
820 if (hci_unregister_dev(hdev) < 0)
821 printk(KERN_WARNING "bluecard_cs: Can't unregister HCI device %s.\n", hdev->name);
822
823 return 0;
824 }
825
826
827
828 /* ======================== Card services ======================== */
829
830
cs_error(client_handle_t handle,int func,int ret)831 static void cs_error(client_handle_t handle, int func, int ret)
832 {
833 error_info_t err = { func, ret };
834
835 CardServices(ReportError, handle, &err);
836 }
837
838
bluecard_attach(void)839 dev_link_t *bluecard_attach(void)
840 {
841 bluecard_info_t *info;
842 client_reg_t client_reg;
843 dev_link_t *link;
844 int i, ret;
845
846 /* Create new info device */
847 info = kmalloc(sizeof(*info), GFP_KERNEL);
848 if (!info)
849 return NULL;
850 memset(info, 0, sizeof(*info));
851
852 link = &info->link;
853 link->priv = info;
854
855 link->release.function = &bluecard_release;
856 link->release.data = (u_long)link;
857 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
858 link->io.NumPorts1 = 8;
859 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
860 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
861
862 if (irq_list[0] == -1)
863 link->irq.IRQInfo2 = irq_mask;
864 else
865 for (i = 0; i < 4; i++)
866 link->irq.IRQInfo2 |= 1 << irq_list[i];
867
868 link->irq.Handler = bluecard_interrupt;
869 link->irq.Instance = info;
870
871 link->conf.Attributes = CONF_ENABLE_IRQ;
872 link->conf.Vcc = 50;
873 link->conf.IntType = INT_MEMORY_AND_IO;
874
875 /* Register with Card Services */
876 link->next = dev_list;
877 dev_list = link;
878 client_reg.dev_info = &dev_info;
879 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
880 client_reg.EventMask =
881 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
882 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
883 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
884 client_reg.event_handler = &bluecard_event;
885 client_reg.Version = 0x0210;
886 client_reg.event_callback_args.client_data = link;
887
888 ret = CardServices(RegisterClient, &link->handle, &client_reg);
889 if (ret != CS_SUCCESS) {
890 cs_error(link->handle, RegisterClient, ret);
891 bluecard_detach(link);
892 return NULL;
893 }
894
895 return link;
896 }
897
898
bluecard_detach(dev_link_t * link)899 void bluecard_detach(dev_link_t *link)
900 {
901 bluecard_info_t *info = link->priv;
902 dev_link_t **linkp;
903 int ret;
904
905 /* Locate device structure */
906 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
907 if (*linkp == link)
908 break;
909
910 if (*linkp == NULL)
911 return;
912
913 del_timer(&link->release);
914 if (link->state & DEV_CONFIG)
915 bluecard_release((u_long)link);
916
917 if (link->handle) {
918 ret = CardServices(DeregisterClient, link->handle);
919 if (ret != CS_SUCCESS)
920 cs_error(link->handle, DeregisterClient, ret);
921 }
922
923 /* Unlink device structure, free bits */
924 *linkp = link->next;
925
926 kfree(info);
927 }
928
929
get_tuple(int fn,client_handle_t handle,tuple_t * tuple,cisparse_t * parse)930 static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
931 {
932 int i;
933
934 i = CardServices(fn, handle, tuple);
935 if (i != CS_SUCCESS)
936 return CS_NO_MORE_ITEMS;
937
938 i = CardServices(GetTupleData, handle, tuple);
939 if (i != CS_SUCCESS)
940 return i;
941
942 return CardServices(ParseTuple, handle, tuple, parse);
943 }
944
945
946 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
947 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
948
bluecard_config(dev_link_t * link)949 void bluecard_config(dev_link_t *link)
950 {
951 client_handle_t handle = link->handle;
952 bluecard_info_t *info = link->priv;
953 tuple_t tuple;
954 u_short buf[256];
955 cisparse_t parse;
956 config_info_t config;
957 int i, n, last_ret, last_fn;
958
959 tuple.TupleData = (cisdata_t *)buf;
960 tuple.TupleOffset = 0;
961 tuple.TupleDataMax = 255;
962 tuple.Attributes = 0;
963
964 /* Get configuration register information */
965 tuple.DesiredTuple = CISTPL_CONFIG;
966 last_ret = first_tuple(handle, &tuple, &parse);
967 if (last_ret != CS_SUCCESS) {
968 last_fn = ParseTuple;
969 goto cs_failed;
970 }
971 link->conf.ConfigBase = parse.config.base;
972 link->conf.Present = parse.config.rmask[0];
973
974 /* Configure card */
975 link->state |= DEV_CONFIG;
976 i = CardServices(GetConfigurationInfo, handle, &config);
977 link->conf.Vcc = config.Vcc;
978
979 link->conf.ConfigIndex = 0x20;
980 link->io.NumPorts1 = 64;
981 link->io.IOAddrLines = 6;
982
983 for (n = 0; n < 0x400; n += 0x40) {
984 link->io.BasePort1 = n ^ 0x300;
985 i = CardServices(RequestIO, link->handle, &link->io);
986 if (i == CS_SUCCESS)
987 break;
988 }
989
990 if (i != CS_SUCCESS) {
991 cs_error(link->handle, RequestIO, i);
992 goto failed;
993 }
994
995 i = CardServices(RequestIRQ, link->handle, &link->irq);
996 if (i != CS_SUCCESS) {
997 cs_error(link->handle, RequestIRQ, i);
998 link->irq.AssignedIRQ = 0;
999 }
1000
1001 i = CardServices(RequestConfiguration, link->handle, &link->conf);
1002 if (i != CS_SUCCESS) {
1003 cs_error(link->handle, RequestConfiguration, i);
1004 goto failed;
1005 }
1006
1007 MOD_INC_USE_COUNT;
1008
1009 if (bluecard_open(info) != 0)
1010 goto failed;
1011
1012 strcpy(info->node.dev_name, info->hdev.name);
1013 link->dev = &info->node;
1014 link->state &= ~DEV_CONFIG_PENDING;
1015
1016 return;
1017
1018 cs_failed:
1019 cs_error(link->handle, last_fn, last_ret);
1020
1021 failed:
1022 bluecard_release((u_long)link);
1023 }
1024
1025
bluecard_release(u_long arg)1026 void bluecard_release(u_long arg)
1027 {
1028 dev_link_t *link = (dev_link_t *)arg;
1029 bluecard_info_t *info = link->priv;
1030
1031 if (link->state & DEV_PRESENT)
1032 bluecard_close(info);
1033
1034 MOD_DEC_USE_COUNT;
1035
1036 link->dev = NULL;
1037
1038 CardServices(ReleaseConfiguration, link->handle);
1039 CardServices(ReleaseIO, link->handle, &link->io);
1040 CardServices(ReleaseIRQ, link->handle, &link->irq);
1041
1042 link->state &= ~DEV_CONFIG;
1043 }
1044
1045
bluecard_event(event_t event,int priority,event_callback_args_t * args)1046 int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1047 {
1048 dev_link_t *link = args->client_data;
1049 bluecard_info_t *info = link->priv;
1050
1051 switch (event) {
1052 case CS_EVENT_CARD_REMOVAL:
1053 link->state &= ~DEV_PRESENT;
1054 if (link->state & DEV_CONFIG) {
1055 bluecard_close(info);
1056 mod_timer(&link->release, jiffies + HZ / 20);
1057 }
1058 break;
1059 case CS_EVENT_CARD_INSERTION:
1060 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1061 bluecard_config(link);
1062 break;
1063 case CS_EVENT_PM_SUSPEND:
1064 link->state |= DEV_SUSPEND;
1065 /* Fall through... */
1066 case CS_EVENT_RESET_PHYSICAL:
1067 if (link->state & DEV_CONFIG)
1068 CardServices(ReleaseConfiguration, link->handle);
1069 break;
1070 case CS_EVENT_PM_RESUME:
1071 link->state &= ~DEV_SUSPEND;
1072 /* Fall through... */
1073 case CS_EVENT_CARD_RESET:
1074 if (DEV_OK(link))
1075 CardServices(RequestConfiguration, link->handle, &link->conf);
1076 break;
1077 }
1078
1079 return 0;
1080 }
1081
1082
1083
1084 /* ======================== Module initialization ======================== */
1085
1086
init_bluecard_cs(void)1087 int __init init_bluecard_cs(void)
1088 {
1089 servinfo_t serv;
1090 int err;
1091
1092 CardServices(GetCardServicesInfo, &serv);
1093 if (serv.Revision != CS_RELEASE_CODE) {
1094 printk(KERN_NOTICE "bluecard_cs: Card Services release does not match!\n");
1095 return -1;
1096 }
1097
1098 err = register_pccard_driver(&dev_info, &bluecard_attach, &bluecard_detach);
1099
1100 return err;
1101 }
1102
1103
exit_bluecard_cs(void)1104 void __exit exit_bluecard_cs(void)
1105 {
1106 unregister_pccard_driver(&dev_info);
1107
1108 while (dev_list != NULL)
1109 bluecard_detach(dev_list);
1110 }
1111
1112
1113 module_init(init_bluecard_cs);
1114 module_exit(exit_bluecard_cs);
1115
1116 EXPORT_NO_SYMBOLS;
1117