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 {
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 void btuart_config(dev_link_t *link);
88 static void btuart_release(dev_link_t *link);
89 static int btuart_event(event_t event, int priority, event_callback_args_t *args);
91 static dev_info_t dev_info = "btuart_cs";
93 static dev_link_t *btuart_attach(void);
94 static void btuart_detach(struct pcmcia_device *p_dev);
97 /* Maximum baud rate */
98 #define SPEED_MAX 115200
100 /* Default baud rate: 57600, 115200, 230400 or 460800 */
101 #define DEFAULT_BAUD_RATE 115200
104 /* Transmit states */
105 #define XMIT_SENDING 1
106 #define XMIT_WAKEUP 2
107 #define XMIT_WAITING 8
109 /* Receiver states */
110 #define RECV_WAIT_PACKET_TYPE 0
111 #define RECV_WAIT_EVENT_HEADER 1
112 #define RECV_WAIT_ACL_HEADER 2
113 #define RECV_WAIT_SCO_HEADER 3
114 #define RECV_WAIT_DATA 4
118 /* ======================== Interrupt handling ======================== */
121 static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
125 /* Tx FIFO should be empty */
126 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
129 /* Fill FIFO with current frame */
130 while ((fifo_size-- > 0) && (actual < len)) {
131 /* Transmit next byte */
132 outb(buf[actual], iobase + UART_TX);
140 static void btuart_write_wakeup(btuart_info_t *info)
143 BT_ERR("Unknown device");
147 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
148 set_bit(XMIT_WAKEUP, &(info->tx_state));
153 register unsigned int iobase = info->link.io.BasePort1;
154 register struct sk_buff *skb;
157 clear_bit(XMIT_WAKEUP, &(info->tx_state));
159 if (!(info->link.state & DEV_PRESENT))
162 if (!(skb = skb_dequeue(&(info->txq))))
166 len = btuart_write(iobase, 16, skb->data, skb->len);
167 set_bit(XMIT_WAKEUP, &(info->tx_state));
169 if (len == skb->len) {
173 skb_queue_head(&(info->txq), skb);
176 info->hdev->stat.byte_tx += len;
178 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
180 clear_bit(XMIT_SENDING, &(info->tx_state));
184 static void btuart_receive(btuart_info_t *info)
190 BT_ERR("Unknown device");
194 iobase = info->link.io.BasePort1;
197 info->hdev->stat.byte_rx++;
199 /* Allocate packet */
200 if (info->rx_skb == NULL) {
201 info->rx_state = RECV_WAIT_PACKET_TYPE;
203 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
204 BT_ERR("Can't allocate mem for new packet");
209 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
211 info->rx_skb->dev = (void *) info->hdev;
212 bt_cb(info->rx_skb)->pkt_type = inb(iobase + UART_RX);
214 switch (bt_cb(info->rx_skb)->pkt_type) {
217 info->rx_state = RECV_WAIT_EVENT_HEADER;
218 info->rx_count = HCI_EVENT_HDR_SIZE;
221 case HCI_ACLDATA_PKT:
222 info->rx_state = RECV_WAIT_ACL_HEADER;
223 info->rx_count = HCI_ACL_HDR_SIZE;
226 case HCI_SCODATA_PKT:
227 info->rx_state = RECV_WAIT_SCO_HEADER;
228 info->rx_count = HCI_SCO_HDR_SIZE;
233 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
234 info->hdev->stat.err_rx++;
235 clear_bit(HCI_RUNNING, &(info->hdev->flags));
237 kfree_skb(info->rx_skb);
245 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
248 if (info->rx_count == 0) {
251 struct hci_event_hdr *eh;
252 struct hci_acl_hdr *ah;
253 struct hci_sco_hdr *sh;
256 switch (info->rx_state) {
258 case RECV_WAIT_EVENT_HEADER:
259 eh = (struct hci_event_hdr *)(info->rx_skb->data);
260 info->rx_state = RECV_WAIT_DATA;
261 info->rx_count = eh->plen;
264 case RECV_WAIT_ACL_HEADER:
265 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
266 dlen = __le16_to_cpu(ah->dlen);
267 info->rx_state = RECV_WAIT_DATA;
268 info->rx_count = dlen;
271 case RECV_WAIT_SCO_HEADER:
272 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
273 info->rx_state = RECV_WAIT_DATA;
274 info->rx_count = sh->dlen;
278 hci_recv_frame(info->rx_skb);
288 /* Make sure we don't stay here too long */
289 if (boguscount++ > 16)
292 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
296 static irqreturn_t btuart_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
298 btuart_info_t *info = dev_inst;
303 if (!info || !info->hdev) {
304 BT_ERR("Call of irq %d for unknown device", irq);
308 iobase = info->link.io.BasePort1;
310 spin_lock(&(info->lock));
312 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
315 /* Clear interrupt */
316 lsr = inb(iobase + UART_LSR);
323 /* Receive interrupt */
324 btuart_receive(info);
327 if (lsr & UART_LSR_THRE) {
328 /* Transmitter ready for data */
329 btuart_write_wakeup(info);
333 BT_ERR("Unhandled IIR=%#x", iir);
337 /* Make sure we don't stay here too long */
338 if (boguscount++ > 100)
341 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
345 spin_unlock(&(info->lock));
351 static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
355 int fcr; /* FIFO control reg */
356 int lcr; /* Line control reg */
360 BT_ERR("Unknown device");
364 iobase = info->link.io.BasePort1;
366 spin_lock_irqsave(&(info->lock), flags);
368 /* Turn off interrupts */
369 outb(0, iobase + UART_IER);
371 divisor = SPEED_MAX / speed;
373 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
376 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
377 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
378 * about this timeout since it will always be fast enough.
382 fcr |= UART_FCR_TRIGGER_1;
384 fcr |= UART_FCR_TRIGGER_14;
386 /* Bluetooth cards use 8N1 */
387 lcr = UART_LCR_WLEN8;
389 outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */
390 outb(divisor & 0xff, iobase + UART_DLL); /* Set speed */
391 outb(divisor >> 8, iobase + UART_DLM);
392 outb(lcr, iobase + UART_LCR); /* Set 8N1 */
393 outb(fcr, iobase + UART_FCR); /* Enable FIFO's */
395 /* Turn on interrups */
396 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
398 spin_unlock_irqrestore(&(info->lock), flags);
403 /* ======================== HCI interface ======================== */
406 static int btuart_hci_flush(struct hci_dev *hdev)
408 btuart_info_t *info = (btuart_info_t *)(hdev->driver_data);
411 skb_queue_purge(&(info->txq));
417 static int btuart_hci_open(struct hci_dev *hdev)
419 set_bit(HCI_RUNNING, &(hdev->flags));
425 static int btuart_hci_close(struct hci_dev *hdev)
427 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
430 btuart_hci_flush(hdev);
436 static int btuart_hci_send_frame(struct sk_buff *skb)
439 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
442 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
446 info = (btuart_info_t *)(hdev->driver_data);
448 switch (bt_cb(skb)->pkt_type) {
449 case HCI_COMMAND_PKT:
452 case HCI_ACLDATA_PKT:
455 case HCI_SCODATA_PKT:
460 /* Prepend skb with frame type */
461 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
462 skb_queue_tail(&(info->txq), skb);
464 btuart_write_wakeup(info);
470 static void btuart_hci_destruct(struct hci_dev *hdev)
475 static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
482 /* ======================== Card services HCI interaction ======================== */
485 static int btuart_open(btuart_info_t *info)
488 unsigned int iobase = info->link.io.BasePort1;
489 struct hci_dev *hdev;
491 spin_lock_init(&(info->lock));
493 skb_queue_head_init(&(info->txq));
495 info->rx_state = RECV_WAIT_PACKET_TYPE;
499 /* Initialize HCI device */
500 hdev = hci_alloc_dev();
502 BT_ERR("Can't allocate HCI device");
508 hdev->type = HCI_PCCARD;
509 hdev->driver_data = info;
511 hdev->open = btuart_hci_open;
512 hdev->close = btuart_hci_close;
513 hdev->flush = btuart_hci_flush;
514 hdev->send = btuart_hci_send_frame;
515 hdev->destruct = btuart_hci_destruct;
516 hdev->ioctl = btuart_hci_ioctl;
518 hdev->owner = THIS_MODULE;
520 spin_lock_irqsave(&(info->lock), flags);
523 outb(0, iobase + UART_MCR);
525 /* Turn off interrupts */
526 outb(0, iobase + UART_IER);
528 /* Initialize UART */
529 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
530 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
532 /* Turn on interrupts */
533 // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
535 spin_unlock_irqrestore(&(info->lock), flags);
537 btuart_change_speed(info, DEFAULT_BAUD_RATE);
539 /* Timeout before it is safe to send the first HCI packet */
542 /* Register HCI device */
543 if (hci_register_dev(hdev) < 0) {
544 BT_ERR("Can't register HCI device");
554 static int btuart_close(btuart_info_t *info)
557 unsigned int iobase = info->link.io.BasePort1;
558 struct hci_dev *hdev = info->hdev;
563 btuart_hci_close(hdev);
565 spin_lock_irqsave(&(info->lock), flags);
568 outb(0, iobase + UART_MCR);
570 /* Turn off interrupts */
571 outb(0, iobase + UART_IER);
573 spin_unlock_irqrestore(&(info->lock), flags);
575 if (hci_unregister_dev(hdev) < 0)
576 BT_ERR("Can't unregister HCI device %s", hdev->name);
583 static dev_link_t *btuart_attach(void)
586 client_reg_t client_reg;
590 /* Create new info device */
591 info = kzalloc(sizeof(*info), GFP_KERNEL);
598 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
599 link->io.NumPorts1 = 8;
600 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
601 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
603 link->irq.Handler = btuart_interrupt;
604 link->irq.Instance = info;
606 link->conf.Attributes = CONF_ENABLE_IRQ;
608 link->conf.IntType = INT_MEMORY_AND_IO;
610 /* Register with Card Services */
612 client_reg.dev_info = &dev_info;
613 client_reg.Version = 0x0210;
614 client_reg.event_callback_args.client_data = link;
616 ret = pcmcia_register_client(&link->handle, &client_reg);
617 if (ret != CS_SUCCESS) {
618 cs_error(link->handle, RegisterClient, ret);
619 btuart_detach(link->handle);
627 static void btuart_detach(struct pcmcia_device *p_dev)
629 dev_link_t *link = dev_to_instance(p_dev);
630 btuart_info_t *info = link->priv;
632 if (link->state & DEV_CONFIG)
633 btuart_release(link);
638 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
642 i = pcmcia_get_tuple_data(handle, tuple);
646 return pcmcia_parse_tuple(handle, tuple, parse);
649 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
651 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
652 return CS_NO_MORE_ITEMS;
653 return get_tuple(handle, tuple, parse);
656 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
658 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
659 return CS_NO_MORE_ITEMS;
660 return get_tuple(handle, tuple, parse);
663 static void btuart_config(dev_link_t *link)
665 static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
666 client_handle_t handle = link->handle;
667 btuart_info_t *info = link->priv;
671 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
672 config_info_t config;
673 int i, j, try, last_ret, last_fn;
675 tuple.TupleData = (cisdata_t *)buf;
676 tuple.TupleOffset = 0;
677 tuple.TupleDataMax = 255;
678 tuple.Attributes = 0;
680 /* Get configuration register information */
681 tuple.DesiredTuple = CISTPL_CONFIG;
682 last_ret = first_tuple(handle, &tuple, &parse);
683 if (last_ret != CS_SUCCESS) {
684 last_fn = ParseTuple;
687 link->conf.ConfigBase = parse.config.base;
688 link->conf.Present = parse.config.rmask[0];
691 link->state |= DEV_CONFIG;
692 i = pcmcia_get_configuration_info(handle, &config);
693 link->conf.Vcc = config.Vcc;
695 /* First pass: look for a config entry that looks normal. */
696 tuple.TupleData = (cisdata_t *) buf;
697 tuple.TupleOffset = 0;
698 tuple.TupleDataMax = 255;
699 tuple.Attributes = 0;
700 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
701 /* Two tries: without IO aliases, then with aliases */
702 for (try = 0; try < 2; try++) {
703 i = first_tuple(handle, &tuple, &parse);
704 while (i != CS_NO_MORE_ITEMS) {
707 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
708 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
709 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
710 link->conf.ConfigIndex = cf->index;
711 link->io.BasePort1 = cf->io.win[0].base;
712 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
713 i = pcmcia_request_io(link->handle, &link->io);
718 i = next_tuple(handle, &tuple, &parse);
722 /* Second pass: try to find an entry that isn't picky about
723 its base address, then try to grab any standard serial port
724 address, and finally try to get any free port. */
725 i = first_tuple(handle, &tuple, &parse);
726 while (i != CS_NO_MORE_ITEMS) {
727 if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
728 && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
729 link->conf.ConfigIndex = cf->index;
730 for (j = 0; j < 5; j++) {
731 link->io.BasePort1 = base[j];
732 link->io.IOAddrLines = base[j] ? 16 : 3;
733 i = pcmcia_request_io(link->handle, &link->io);
738 i = next_tuple(handle, &tuple, &parse);
742 if (i != CS_SUCCESS) {
743 BT_ERR("No usable port range found");
744 cs_error(link->handle, RequestIO, i);
748 i = pcmcia_request_irq(link->handle, &link->irq);
749 if (i != CS_SUCCESS) {
750 cs_error(link->handle, RequestIRQ, i);
751 link->irq.AssignedIRQ = 0;
754 i = pcmcia_request_configuration(link->handle, &link->conf);
755 if (i != CS_SUCCESS) {
756 cs_error(link->handle, RequestConfiguration, i);
760 if (btuart_open(info) != 0)
763 strcpy(info->node.dev_name, info->hdev->name);
764 link->dev = &info->node;
765 link->state &= ~DEV_CONFIG_PENDING;
770 cs_error(link->handle, last_fn, last_ret);
773 btuart_release(link);
777 static void btuart_release(dev_link_t *link)
779 btuart_info_t *info = link->priv;
781 if (link->state & DEV_PRESENT)
786 pcmcia_release_configuration(link->handle);
787 pcmcia_release_io(link->handle, &link->io);
788 pcmcia_release_irq(link->handle, &link->irq);
790 link->state &= ~DEV_CONFIG;
793 static int btuart_suspend(struct pcmcia_device *dev)
795 dev_link_t *link = dev_to_instance(dev);
797 link->state |= DEV_SUSPEND;
798 if (link->state & DEV_CONFIG)
799 pcmcia_release_configuration(link->handle);
804 static int btuart_resume(struct pcmcia_device *dev)
806 dev_link_t *link = dev_to_instance(dev);
808 link->state &= ~DEV_SUSPEND;
810 pcmcia_request_configuration(link->handle, &link->conf);
816 static int btuart_event(event_t event, int priority, event_callback_args_t *args)
818 dev_link_t *link = args->client_data;
821 case CS_EVENT_CARD_INSERTION:
822 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
830 static struct pcmcia_device_id btuart_ids[] = {
831 /* don't use this driver. Use serial_cs + hci_uart instead */
834 MODULE_DEVICE_TABLE(pcmcia, btuart_ids);
836 static struct pcmcia_driver btuart_driver = {
837 .owner = THIS_MODULE,
841 .attach = btuart_attach,
842 .event = btuart_event,
843 .remove = btuart_detach,
844 .id_table = btuart_ids,
845 .suspend = btuart_suspend,
846 .resume = btuart_resume,
849 static int __init init_btuart_cs(void)
851 return pcmcia_register_driver(&btuart_driver);
855 static void __exit exit_btuart_cs(void)
857 pcmcia_unregister_driver(&btuart_driver);
860 module_init(init_btuart_cs);
861 module_exit(exit_btuart_cs);