1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/types.h>
29#include <linux/delay.h>
30#include <linux/errno.h>
31#include <linux/ptrace.h>
32#include <linux/ioport.h>
33#include <linux/spinlock.h>
34#include <linux/moduleparam.h>
35
36#include <linux/skbuff.h>
37#include <linux/string.h>
38#include <linux/serial.h>
39#include <linux/serial_reg.h>
40#include <linux/bitops.h>
41#include <asm/system.h>
42#include <asm/io.h>
43
44#include <pcmcia/cs_types.h>
45#include <pcmcia/cs.h>
46#include <pcmcia/cistpl.h>
47#include <pcmcia/ciscode.h>
48#include <pcmcia/ds.h>
49#include <pcmcia/cisreg.h>
50
51#include <net/bluetooth/bluetooth.h>
52#include <net/bluetooth/hci_core.h>
53
54
55
56
57
58
59MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
60MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface");
61MODULE_LICENSE("GPL");
62
63
64
65
66
67
68typedef struct btuart_info_t {
69 struct pcmcia_device *p_dev;
70
71 struct hci_dev *hdev;
72
73 spinlock_t lock;
74
75 struct sk_buff_head txq;
76 unsigned long tx_state;
77
78 unsigned long rx_state;
79 unsigned long rx_count;
80 struct sk_buff *rx_skb;
81} btuart_info_t;
82
83
84static int btuart_config(struct pcmcia_device *link);
85static void btuart_release(struct pcmcia_device *link);
86
87static void btuart_detach(struct pcmcia_device *p_dev);
88
89
90
91#define SPEED_MAX 115200
92
93
94#define DEFAULT_BAUD_RATE 115200
95
96
97
98#define XMIT_SENDING 1
99#define XMIT_WAKEUP 2
100#define XMIT_WAITING 8
101
102
103#define RECV_WAIT_PACKET_TYPE 0
104#define RECV_WAIT_EVENT_HEADER 1
105#define RECV_WAIT_ACL_HEADER 2
106#define RECV_WAIT_SCO_HEADER 3
107#define RECV_WAIT_DATA 4
108
109
110
111
112
113
114static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
115{
116 int actual = 0;
117
118
119 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
120 return 0;
121
122
123 while ((fifo_size-- > 0) && (actual < len)) {
124
125 outb(buf[actual], iobase + UART_TX);
126 actual++;
127 }
128
129 return actual;
130}
131
132
133static void btuart_write_wakeup(btuart_info_t *info)
134{
135 if (!info) {
136 BT_ERR("Unknown device");
137 return;
138 }
139
140 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
141 set_bit(XMIT_WAKEUP, &(info->tx_state));
142 return;
143 }
144
145 do {
146 register unsigned int iobase = info->p_dev->io.BasePort1;
147 register struct sk_buff *skb;
148 register int len;
149
150 clear_bit(XMIT_WAKEUP, &(info->tx_state));
151
152 if (!pcmcia_dev_present(info->p_dev))
153 return;
154
155 if (!(skb = skb_dequeue(&(info->txq))))
156 break;
157
158
159 len = btuart_write(iobase, 16, skb->data, skb->len);
160 set_bit(XMIT_WAKEUP, &(info->tx_state));
161
162 if (len == skb->len) {
163 kfree_skb(skb);
164 } else {
165 skb_pull(skb, len);
166 skb_queue_head(&(info->txq), skb);
167 }
168
169 info->hdev->stat.byte_tx += len;
170
171 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
172
173 clear_bit(XMIT_SENDING, &(info->tx_state));
174}
175
176
177static void btuart_receive(btuart_info_t *info)
178{
179 unsigned int iobase;
180 int boguscount = 0;
181
182 if (!info) {
183 BT_ERR("Unknown device");
184 return;
185 }
186
187 iobase = info->p_dev->io.BasePort1;
188
189 do {
190 info->hdev->stat.byte_rx++;
191
192
193 if (info->rx_skb == NULL) {
194 info->rx_state = RECV_WAIT_PACKET_TYPE;
195 info->rx_count = 0;
196 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
197 BT_ERR("Can't allocate mem for new packet");
198 return;
199 }
200 }
201
202 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
203
204 info->rx_skb->dev = (void *) info->hdev;
205 bt_cb(info->rx_skb)->pkt_type = inb(iobase + UART_RX);
206
207 switch (bt_cb(info->rx_skb)->pkt_type) {
208
209 case HCI_EVENT_PKT:
210 info->rx_state = RECV_WAIT_EVENT_HEADER;
211 info->rx_count = HCI_EVENT_HDR_SIZE;
212 break;
213
214 case HCI_ACLDATA_PKT:
215 info->rx_state = RECV_WAIT_ACL_HEADER;
216 info->rx_count = HCI_ACL_HDR_SIZE;
217 break;
218
219 case HCI_SCODATA_PKT:
220 info->rx_state = RECV_WAIT_SCO_HEADER;
221 info->rx_count = HCI_SCO_HDR_SIZE;
222 break;
223
224 default:
225
226 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
227 info->hdev->stat.err_rx++;
228 clear_bit(HCI_RUNNING, &(info->hdev->flags));
229
230 kfree_skb(info->rx_skb);
231 info->rx_skb = NULL;
232 break;
233
234 }
235
236 } else {
237
238 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
239 info->rx_count--;
240
241 if (info->rx_count == 0) {
242
243 int dlen;
244 struct hci_event_hdr *eh;
245 struct hci_acl_hdr *ah;
246 struct hci_sco_hdr *sh;
247
248
249 switch (info->rx_state) {
250
251 case RECV_WAIT_EVENT_HEADER:
252 eh = hci_event_hdr(info->rx_skb);
253 info->rx_state = RECV_WAIT_DATA;
254 info->rx_count = eh->plen;
255 break;
256
257 case RECV_WAIT_ACL_HEADER:
258 ah = hci_acl_hdr(info->rx_skb);
259 dlen = __le16_to_cpu(ah->dlen);
260 info->rx_state = RECV_WAIT_DATA;
261 info->rx_count = dlen;
262 break;
263
264 case RECV_WAIT_SCO_HEADER:
265 sh = hci_sco_hdr(info->rx_skb);
266 info->rx_state = RECV_WAIT_DATA;
267 info->rx_count = sh->dlen;
268 break;
269
270 case RECV_WAIT_DATA:
271 hci_recv_frame(info->rx_skb);
272 info->rx_skb = NULL;
273 break;
274
275 }
276
277 }
278
279 }
280
281
282 if (boguscount++ > 16)
283 break;
284
285 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
286}
287
288
289static irqreturn_t btuart_interrupt(int irq, void *dev_inst)
290{
291 btuart_info_t *info = dev_inst;
292 unsigned int iobase;
293 int boguscount = 0;
294 int iir, lsr;
295 irqreturn_t r = IRQ_NONE;
296
297 if (!info || !info->hdev)
298
299 return IRQ_NONE;
300
301 iobase = info->p_dev->io.BasePort1;
302
303 spin_lock(&(info->lock));
304
305 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
306 while (iir) {
307 r = IRQ_HANDLED;
308
309
310 lsr = inb(iobase + UART_LSR);
311
312 switch (iir) {
313 case UART_IIR_RLSI:
314 BT_ERR("RLSI");
315 break;
316 case UART_IIR_RDI:
317
318 btuart_receive(info);
319 break;
320 case UART_IIR_THRI:
321 if (lsr & UART_LSR_THRE) {
322
323 btuart_write_wakeup(info);
324 }
325 break;
326 default:
327 BT_ERR("Unhandled IIR=%#x", iir);
328 break;
329 }
330
331
332 if (boguscount++ > 100)
333 break;
334
335 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
336
337 }
338
339 spin_unlock(&(info->lock));
340
341 return r;
342}
343
344
345static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
346{
347 unsigned long flags;
348 unsigned int iobase;
349 int fcr;
350 int lcr;
351 int divisor;
352
353 if (!info) {
354 BT_ERR("Unknown device");
355 return;
356 }
357
358 iobase = info->p_dev->io.BasePort1;
359
360 spin_lock_irqsave(&(info->lock), flags);
361
362
363 outb(0, iobase + UART_IER);
364
365 divisor = SPEED_MAX / speed;
366
367 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
368
369
370
371
372
373
374
375 if (speed < 38400)
376 fcr |= UART_FCR_TRIGGER_1;
377 else
378 fcr |= UART_FCR_TRIGGER_14;
379
380
381 lcr = UART_LCR_WLEN8;
382
383 outb(UART_LCR_DLAB | lcr, iobase + UART_LCR);
384 outb(divisor & 0xff, iobase + UART_DLL);
385 outb(divisor >> 8, iobase + UART_DLM);
386 outb(lcr, iobase + UART_LCR);
387 outb(fcr, iobase + UART_FCR);
388
389
390 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
391
392 spin_unlock_irqrestore(&(info->lock), flags);
393}
394
395
396
397
398
399
400static int btuart_hci_flush(struct hci_dev *hdev)
401{
402 btuart_info_t *info = (btuart_info_t *)(hdev->driver_data);
403
404
405 skb_queue_purge(&(info->txq));
406
407 return 0;
408}
409
410
411static int btuart_hci_open(struct hci_dev *hdev)
412{
413 set_bit(HCI_RUNNING, &(hdev->flags));
414
415 return 0;
416}
417
418
419static int btuart_hci_close(struct hci_dev *hdev)
420{
421 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
422 return 0;
423
424 btuart_hci_flush(hdev);
425
426 return 0;
427}
428
429
430static int btuart_hci_send_frame(struct sk_buff *skb)
431{
432 btuart_info_t *info;
433 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
434
435 if (!hdev) {
436 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
437 return -ENODEV;
438 }
439
440 info = (btuart_info_t *)(hdev->driver_data);
441
442 switch (bt_cb(skb)->pkt_type) {
443 case HCI_COMMAND_PKT:
444 hdev->stat.cmd_tx++;
445 break;
446 case HCI_ACLDATA_PKT:
447 hdev->stat.acl_tx++;
448 break;
449 case HCI_SCODATA_PKT:
450 hdev->stat.sco_tx++;
451 break;
452 };
453
454
455 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
456 skb_queue_tail(&(info->txq), skb);
457
458 btuart_write_wakeup(info);
459
460 return 0;
461}
462
463
464static void btuart_hci_destruct(struct hci_dev *hdev)
465{
466}
467
468
469static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
470{
471 return -ENOIOCTLCMD;
472}
473
474
475
476
477
478
479static int btuart_open(btuart_info_t *info)
480{
481 unsigned long flags;
482 unsigned int iobase = info->p_dev->io.BasePort1;
483 struct hci_dev *hdev;
484
485 spin_lock_init(&(info->lock));
486
487 skb_queue_head_init(&(info->txq));
488
489 info->rx_state = RECV_WAIT_PACKET_TYPE;
490 info->rx_count = 0;
491 info->rx_skb = NULL;
492
493
494 hdev = hci_alloc_dev();
495 if (!hdev) {
496 BT_ERR("Can't allocate HCI device");
497 return -ENOMEM;
498 }
499
500 info->hdev = hdev;
501
502 hdev->bus = HCI_PCCARD;
503 hdev->driver_data = info;
504 SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
505
506 hdev->open = btuart_hci_open;
507 hdev->close = btuart_hci_close;
508 hdev->flush = btuart_hci_flush;
509 hdev->send = btuart_hci_send_frame;
510 hdev->destruct = btuart_hci_destruct;
511 hdev->ioctl = btuart_hci_ioctl;
512
513 hdev->owner = THIS_MODULE;
514
515 spin_lock_irqsave(&(info->lock), flags);
516
517
518 outb(0, iobase + UART_MCR);
519
520
521 outb(0, iobase + UART_IER);
522
523
524 outb(UART_LCR_WLEN8, iobase + UART_LCR);
525 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
526
527
528
529
530 spin_unlock_irqrestore(&(info->lock), flags);
531
532 btuart_change_speed(info, DEFAULT_BAUD_RATE);
533
534
535 msleep(1000);
536
537
538 if (hci_register_dev(hdev) < 0) {
539 BT_ERR("Can't register HCI device");
540 info->hdev = NULL;
541 hci_free_dev(hdev);
542 return -ENODEV;
543 }
544
545 return 0;
546}
547
548
549static int btuart_close(btuart_info_t *info)
550{
551 unsigned long flags;
552 unsigned int iobase = info->p_dev->io.BasePort1;
553 struct hci_dev *hdev = info->hdev;
554
555 if (!hdev)
556 return -ENODEV;
557
558 btuart_hci_close(hdev);
559
560 spin_lock_irqsave(&(info->lock), flags);
561
562
563 outb(0, iobase + UART_MCR);
564
565
566 outb(0, iobase + UART_IER);
567
568 spin_unlock_irqrestore(&(info->lock), flags);
569
570 if (hci_unregister_dev(hdev) < 0)
571 BT_ERR("Can't unregister HCI device %s", hdev->name);
572
573 hci_free_dev(hdev);
574
575 return 0;
576}
577
578static int btuart_probe(struct pcmcia_device *link)
579{
580 btuart_info_t *info;
581
582
583 info = kzalloc(sizeof(*info), GFP_KERNEL);
584 if (!info)
585 return -ENOMEM;
586
587 info->p_dev = link;
588 link->priv = info;
589
590 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
591 link->io.NumPorts1 = 8;
592
593 link->conf.Attributes = CONF_ENABLE_IRQ;
594 link->conf.IntType = INT_MEMORY_AND_IO;
595
596 return btuart_config(link);
597}
598
599
600static void btuart_detach(struct pcmcia_device *link)
601{
602 btuart_info_t *info = link->priv;
603
604 btuart_release(link);
605 kfree(info);
606}
607
608static int btuart_check_config(struct pcmcia_device *p_dev,
609 cistpl_cftable_entry_t *cf,
610 cistpl_cftable_entry_t *dflt,
611 unsigned int vcc,
612 void *priv_data)
613{
614 int *try = priv_data;
615
616 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
617 p_dev->conf.Vpp = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
618 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) &&
619 (cf->io.win[0].base != 0)) {
620 p_dev->io.BasePort1 = cf->io.win[0].base;
621 p_dev->io.IOAddrLines = (*try == 0) ? 16 :
622 cf->io.flags & CISTPL_IO_LINES_MASK;
623 if (!pcmcia_request_io(p_dev, &p_dev->io))
624 return 0;
625 }
626 return -ENODEV;
627}
628
629static int btuart_check_config_notpicky(struct pcmcia_device *p_dev,
630 cistpl_cftable_entry_t *cf,
631 cistpl_cftable_entry_t *dflt,
632 unsigned int vcc,
633 void *priv_data)
634{
635 static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
636 int j;
637
638 if ((cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
639 for (j = 0; j < 5; j++) {
640 p_dev->io.BasePort1 = base[j];
641 p_dev->io.IOAddrLines = base[j] ? 16 : 3;
642 if (!pcmcia_request_io(p_dev, &p_dev->io))
643 return 0;
644 }
645 }
646 return -ENODEV;
647}
648
649static int btuart_config(struct pcmcia_device *link)
650{
651 btuart_info_t *info = link->priv;
652 int i;
653 int try;
654
655
656
657 for (try = 0; try < 2; try++)
658 if (!pcmcia_loop_config(link, btuart_check_config, &try))
659 goto found_port;
660
661
662
663
664 if (!pcmcia_loop_config(link, btuart_check_config_notpicky, NULL))
665 goto found_port;
666
667 BT_ERR("No usable port range found");
668 goto failed;
669
670found_port:
671 i = pcmcia_request_irq(link, btuart_interrupt);
672 if (i != 0)
673 goto failed;
674
675 i = pcmcia_request_configuration(link, &link->conf);
676 if (i != 0)
677 goto failed;
678
679 if (btuart_open(info) != 0)
680 goto failed;
681
682 return 0;
683
684failed:
685 btuart_release(link);
686 return -ENODEV;
687}
688
689
690static void btuart_release(struct pcmcia_device *link)
691{
692 btuart_info_t *info = link->priv;
693
694 btuart_close(info);
695
696 pcmcia_disable_device(link);
697}
698
699static struct pcmcia_device_id btuart_ids[] = {
700
701 PCMCIA_DEVICE_NULL
702};
703MODULE_DEVICE_TABLE(pcmcia, btuart_ids);
704
705static struct pcmcia_driver btuart_driver = {
706 .owner = THIS_MODULE,
707 .drv = {
708 .name = "btuart_cs",
709 },
710 .probe = btuart_probe,
711 .remove = btuart_detach,
712 .id_table = btuart_ids,
713};
714
715static int __init init_btuart_cs(void)
716{
717 return pcmcia_register_driver(&btuart_driver);
718}
719
720
721static void __exit exit_btuart_cs(void)
722{
723 pcmcia_unregister_driver(&btuart_driver);
724}
725
726module_init(init_btuart_cs);
727module_exit(exit_btuart_cs);
728