3 * Driver for Bluetooth PCMCIA cards with HCI UART interface
5 * Copyright (C) 2001-2002 Marcel Holtmann <marcel@holtmann.org>
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;
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.
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.
23 #include <linux/config.h>
24 #include <linux/module.h>
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/errno.h>
33 #include <linux/ptrace.h>
34 #include <linux/ioport.h>
35 #include <linux/spinlock.h>
36 #include <linux/moduleparam.h>
38 #include <linux/skbuff.h>
39 #include <linux/string.h>
40 #include <linux/serial.h>
41 #include <linux/serial_reg.h>
42 #include <linux/bitops.h>
43 #include <asm/system.h>
46 #include <pcmcia/cs_types.h>
47 #include <pcmcia/cs.h>
48 #include <pcmcia/cistpl.h>
49 #include <pcmcia/ciscode.h>
50 #include <pcmcia/ds.h>
51 #include <pcmcia/cisreg.h>
53 #include <net/bluetooth/bluetooth.h>
54 #include <net/bluetooth/hci_core.h>
58 /* ======================== Module parameters ======================== */
61 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
62 MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface");
63 MODULE_LICENSE("GPL");
67 /* ======================== Local structures ======================== */
70 typedef struct btuart_info_t {
71 struct pcmcia_device *p_dev;
76 spinlock_t lock; /* For serializing operations */
78 struct sk_buff_head txq;
79 unsigned long tx_state;
81 unsigned long rx_state;
82 unsigned long rx_count;
83 struct sk_buff *rx_skb;
87 static int btuart_config(struct pcmcia_device *link);
88 static void btuart_release(struct pcmcia_device *link);
90 static void btuart_detach(struct pcmcia_device *p_dev);
93 /* Maximum baud rate */
94 #define SPEED_MAX 115200
96 /* Default baud rate: 57600, 115200, 230400 or 460800 */
97 #define DEFAULT_BAUD_RATE 115200
100 /* Transmit states */
101 #define XMIT_SENDING 1
102 #define XMIT_WAKEUP 2
103 #define XMIT_WAITING 8
105 /* Receiver states */
106 #define RECV_WAIT_PACKET_TYPE 0
107 #define RECV_WAIT_EVENT_HEADER 1
108 #define RECV_WAIT_ACL_HEADER 2
109 #define RECV_WAIT_SCO_HEADER 3
110 #define RECV_WAIT_DATA 4
114 /* ======================== Interrupt handling ======================== */
117 static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
121 /* Tx FIFO should be empty */
122 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
125 /* Fill FIFO with current frame */
126 while ((fifo_size-- > 0) && (actual < len)) {
127 /* Transmit next byte */
128 outb(buf[actual], iobase + UART_TX);
136 static void btuart_write_wakeup(btuart_info_t *info)
139 BT_ERR("Unknown device");
143 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
144 set_bit(XMIT_WAKEUP, &(info->tx_state));
149 register unsigned int iobase = info->p_dev->io.BasePort1;
150 register struct sk_buff *skb;
153 clear_bit(XMIT_WAKEUP, &(info->tx_state));
155 if (!(info->p_dev->state & DEV_PRESENT))
158 if (!(skb = skb_dequeue(&(info->txq))))
162 len = btuart_write(iobase, 16, skb->data, skb->len);
163 set_bit(XMIT_WAKEUP, &(info->tx_state));
165 if (len == skb->len) {
169 skb_queue_head(&(info->txq), skb);
172 info->hdev->stat.byte_tx += len;
174 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
176 clear_bit(XMIT_SENDING, &(info->tx_state));
180 static void btuart_receive(btuart_info_t *info)
186 BT_ERR("Unknown device");
190 iobase = info->p_dev->io.BasePort1;
193 info->hdev->stat.byte_rx++;
195 /* Allocate packet */
196 if (info->rx_skb == NULL) {
197 info->rx_state = RECV_WAIT_PACKET_TYPE;
199 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
200 BT_ERR("Can't allocate mem for new packet");
205 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
207 info->rx_skb->dev = (void *) info->hdev;
208 bt_cb(info->rx_skb)->pkt_type = inb(iobase + UART_RX);
210 switch (bt_cb(info->rx_skb)->pkt_type) {
213 info->rx_state = RECV_WAIT_EVENT_HEADER;
214 info->rx_count = HCI_EVENT_HDR_SIZE;
217 case HCI_ACLDATA_PKT:
218 info->rx_state = RECV_WAIT_ACL_HEADER;
219 info->rx_count = HCI_ACL_HDR_SIZE;
222 case HCI_SCODATA_PKT:
223 info->rx_state = RECV_WAIT_SCO_HEADER;
224 info->rx_count = HCI_SCO_HDR_SIZE;
229 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
230 info->hdev->stat.err_rx++;
231 clear_bit(HCI_RUNNING, &(info->hdev->flags));
233 kfree_skb(info->rx_skb);
241 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
244 if (info->rx_count == 0) {
247 struct hci_event_hdr *eh;
248 struct hci_acl_hdr *ah;
249 struct hci_sco_hdr *sh;
252 switch (info->rx_state) {
254 case RECV_WAIT_EVENT_HEADER:
255 eh = (struct hci_event_hdr *)(info->rx_skb->data);
256 info->rx_state = RECV_WAIT_DATA;
257 info->rx_count = eh->plen;
260 case RECV_WAIT_ACL_HEADER:
261 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
262 dlen = __le16_to_cpu(ah->dlen);
263 info->rx_state = RECV_WAIT_DATA;
264 info->rx_count = dlen;
267 case RECV_WAIT_SCO_HEADER:
268 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
269 info->rx_state = RECV_WAIT_DATA;
270 info->rx_count = sh->dlen;
274 hci_recv_frame(info->rx_skb);
284 /* Make sure we don't stay here too long */
285 if (boguscount++ > 16)
288 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
292 static irqreturn_t btuart_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
294 btuart_info_t *info = dev_inst;
299 if (!info || !info->hdev) {
300 BT_ERR("Call of irq %d for unknown device", irq);
304 iobase = info->p_dev->io.BasePort1;
306 spin_lock(&(info->lock));
308 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
311 /* Clear interrupt */
312 lsr = inb(iobase + UART_LSR);
319 /* Receive interrupt */
320 btuart_receive(info);
323 if (lsr & UART_LSR_THRE) {
324 /* Transmitter ready for data */
325 btuart_write_wakeup(info);
329 BT_ERR("Unhandled IIR=%#x", iir);
333 /* Make sure we don't stay here too long */
334 if (boguscount++ > 100)
337 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
341 spin_unlock(&(info->lock));
347 static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
351 int fcr; /* FIFO control reg */
352 int lcr; /* Line control reg */
356 BT_ERR("Unknown device");
360 iobase = info->p_dev->io.BasePort1;
362 spin_lock_irqsave(&(info->lock), flags);
364 /* Turn off interrupts */
365 outb(0, iobase + UART_IER);
367 divisor = SPEED_MAX / speed;
369 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
372 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
373 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
374 * about this timeout since it will always be fast enough.
378 fcr |= UART_FCR_TRIGGER_1;
380 fcr |= UART_FCR_TRIGGER_14;
382 /* Bluetooth cards use 8N1 */
383 lcr = UART_LCR_WLEN8;
385 outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */
386 outb(divisor & 0xff, iobase + UART_DLL); /* Set speed */
387 outb(divisor >> 8, iobase + UART_DLM);
388 outb(lcr, iobase + UART_LCR); /* Set 8N1 */
389 outb(fcr, iobase + UART_FCR); /* Enable FIFO's */
391 /* Turn on interrups */
392 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
394 spin_unlock_irqrestore(&(info->lock), flags);
399 /* ======================== HCI interface ======================== */
402 static int btuart_hci_flush(struct hci_dev *hdev)
404 btuart_info_t *info = (btuart_info_t *)(hdev->driver_data);
407 skb_queue_purge(&(info->txq));
413 static int btuart_hci_open(struct hci_dev *hdev)
415 set_bit(HCI_RUNNING, &(hdev->flags));
421 static int btuart_hci_close(struct hci_dev *hdev)
423 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
426 btuart_hci_flush(hdev);
432 static int btuart_hci_send_frame(struct sk_buff *skb)
435 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
438 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
442 info = (btuart_info_t *)(hdev->driver_data);
444 switch (bt_cb(skb)->pkt_type) {
445 case HCI_COMMAND_PKT:
448 case HCI_ACLDATA_PKT:
451 case HCI_SCODATA_PKT:
456 /* Prepend skb with frame type */
457 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
458 skb_queue_tail(&(info->txq), skb);
460 btuart_write_wakeup(info);
466 static void btuart_hci_destruct(struct hci_dev *hdev)
471 static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
478 /* ======================== Card services HCI interaction ======================== */
481 static int btuart_open(btuart_info_t *info)
484 unsigned int iobase = info->p_dev->io.BasePort1;
485 struct hci_dev *hdev;
487 spin_lock_init(&(info->lock));
489 skb_queue_head_init(&(info->txq));
491 info->rx_state = RECV_WAIT_PACKET_TYPE;
495 /* Initialize HCI device */
496 hdev = hci_alloc_dev();
498 BT_ERR("Can't allocate HCI device");
504 hdev->type = HCI_PCCARD;
505 hdev->driver_data = info;
507 hdev->open = btuart_hci_open;
508 hdev->close = btuart_hci_close;
509 hdev->flush = btuart_hci_flush;
510 hdev->send = btuart_hci_send_frame;
511 hdev->destruct = btuart_hci_destruct;
512 hdev->ioctl = btuart_hci_ioctl;
514 hdev->owner = THIS_MODULE;
516 spin_lock_irqsave(&(info->lock), flags);
519 outb(0, iobase + UART_MCR);
521 /* Turn off interrupts */
522 outb(0, iobase + UART_IER);
524 /* Initialize UART */
525 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
526 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
528 /* Turn on interrupts */
529 // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
531 spin_unlock_irqrestore(&(info->lock), flags);
533 btuart_change_speed(info, DEFAULT_BAUD_RATE);
535 /* Timeout before it is safe to send the first HCI packet */
538 /* Register HCI device */
539 if (hci_register_dev(hdev) < 0) {
540 BT_ERR("Can't register HCI device");
550 static int btuart_close(btuart_info_t *info)
553 unsigned int iobase = info->p_dev->io.BasePort1;
554 struct hci_dev *hdev = info->hdev;
559 btuart_hci_close(hdev);
561 spin_lock_irqsave(&(info->lock), flags);
564 outb(0, iobase + UART_MCR);
566 /* Turn off interrupts */
567 outb(0, iobase + UART_IER);
569 spin_unlock_irqrestore(&(info->lock), flags);
571 if (hci_unregister_dev(hdev) < 0)
572 BT_ERR("Can't unregister HCI device %s", hdev->name);
579 static int btuart_probe(struct pcmcia_device *link)
583 /* Create new info device */
584 info = kzalloc(sizeof(*info), GFP_KERNEL);
591 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
592 link->io.NumPorts1 = 8;
593 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
594 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
596 link->irq.Handler = btuart_interrupt;
597 link->irq.Instance = info;
599 link->conf.Attributes = CONF_ENABLE_IRQ;
600 link->conf.IntType = INT_MEMORY_AND_IO;
602 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
603 return btuart_config(link);
607 static void btuart_detach(struct pcmcia_device *link)
609 btuart_info_t *info = link->priv;
611 if (link->state & DEV_CONFIG)
612 btuart_release(link);
617 static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
621 i = pcmcia_get_tuple_data(handle, tuple);
625 return pcmcia_parse_tuple(handle, tuple, parse);
628 static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
630 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
631 return CS_NO_MORE_ITEMS;
632 return get_tuple(handle, tuple, parse);
635 static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
637 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
638 return CS_NO_MORE_ITEMS;
639 return get_tuple(handle, tuple, parse);
642 static int btuart_config(struct pcmcia_device *link)
644 static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
645 btuart_info_t *info = link->priv;
649 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
650 int i, j, try, last_ret, last_fn;
652 tuple.TupleData = (cisdata_t *)buf;
653 tuple.TupleOffset = 0;
654 tuple.TupleDataMax = 255;
655 tuple.Attributes = 0;
657 /* Get configuration register information */
658 tuple.DesiredTuple = CISTPL_CONFIG;
659 last_ret = first_tuple(link, &tuple, &parse);
660 if (last_ret != CS_SUCCESS) {
661 last_fn = ParseTuple;
664 link->conf.ConfigBase = parse.config.base;
665 link->conf.Present = parse.config.rmask[0];
668 link->state |= DEV_CONFIG;
670 /* First pass: look for a config entry that looks normal. */
671 tuple.TupleData = (cisdata_t *) buf;
672 tuple.TupleOffset = 0;
673 tuple.TupleDataMax = 255;
674 tuple.Attributes = 0;
675 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
676 /* Two tries: without IO aliases, then with aliases */
677 for (try = 0; try < 2; try++) {
678 i = first_tuple(link, &tuple, &parse);
679 while (i != CS_NO_MORE_ITEMS) {
682 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
683 link->conf.Vpp = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
684 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
685 link->conf.ConfigIndex = cf->index;
686 link->io.BasePort1 = cf->io.win[0].base;
687 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
688 i = pcmcia_request_io(link, &link->io);
693 i = next_tuple(link, &tuple, &parse);
697 /* Second pass: try to find an entry that isn't picky about
698 its base address, then try to grab any standard serial port
699 address, and finally try to get any free port. */
700 i = first_tuple(link, &tuple, &parse);
701 while (i != CS_NO_MORE_ITEMS) {
702 if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
703 && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
704 link->conf.ConfigIndex = cf->index;
705 for (j = 0; j < 5; j++) {
706 link->io.BasePort1 = base[j];
707 link->io.IOAddrLines = base[j] ? 16 : 3;
708 i = pcmcia_request_io(link, &link->io);
713 i = next_tuple(link, &tuple, &parse);
717 if (i != CS_SUCCESS) {
718 BT_ERR("No usable port range found");
719 cs_error(link, RequestIO, i);
723 i = pcmcia_request_irq(link, &link->irq);
724 if (i != CS_SUCCESS) {
725 cs_error(link, RequestIRQ, i);
726 link->irq.AssignedIRQ = 0;
729 i = pcmcia_request_configuration(link, &link->conf);
730 if (i != CS_SUCCESS) {
731 cs_error(link, RequestConfiguration, i);
735 if (btuart_open(info) != 0)
738 strcpy(info->node.dev_name, info->hdev->name);
739 link->dev_node = &info->node;
740 link->state &= ~DEV_CONFIG_PENDING;
745 cs_error(link, last_fn, last_ret);
748 btuart_release(link);
753 static void btuart_release(struct pcmcia_device *link)
755 btuart_info_t *info = link->priv;
757 if (link->state & DEV_PRESENT)
760 pcmcia_disable_device(link);
763 static struct pcmcia_device_id btuart_ids[] = {
764 /* don't use this driver. Use serial_cs + hci_uart instead */
767 MODULE_DEVICE_TABLE(pcmcia, btuart_ids);
769 static struct pcmcia_driver btuart_driver = {
770 .owner = THIS_MODULE,
774 .probe = btuart_probe,
775 .remove = btuart_detach,
776 .id_table = btuart_ids,
779 static int __init init_btuart_cs(void)
781 return pcmcia_register_driver(&btuart_driver);
785 static void __exit exit_btuart_cs(void)
787 pcmcia_unregister_driver(&btuart_driver);
790 module_init(init_btuart_cs);
791 module_exit(exit_btuart_cs);