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/timer.h>
32 #include <linux/errno.h>
33 #include <linux/ptrace.h>
34 #include <linux/ioport.h>
35 #include <linux/spinlock.h>
37 #include <linux/skbuff.h>
38 #include <linux/string.h>
39 #include <linux/serial.h>
40 #include <linux/serial_reg.h>
41 #include <asm/system.h>
42 #include <asm/bitops.h>
45 #include <pcmcia/version.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 /* Bit map of interrupts to choose from */
62 static u_int irq_mask = 0xffff;
63 static int irq_list[4] = { -1 };
65 MODULE_PARM(irq_mask, "i");
66 MODULE_PARM(irq_list, "1-4i");
68 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
69 MODULE_DESCRIPTION("BlueZ driver for Bluetooth PCMCIA cards with HCI UART interface");
70 MODULE_LICENSE("GPL");
74 /* ======================== Local structures ======================== */
77 typedef struct btuart_info_t {
83 spinlock_t lock; /* For serializing operations */
85 struct sk_buff_head txq;
86 unsigned long tx_state;
88 unsigned long rx_state;
89 unsigned long rx_count;
90 struct sk_buff *rx_skb;
94 void btuart_config(dev_link_t *link);
95 void btuart_release(u_long arg);
96 int btuart_event(event_t event, int priority, event_callback_args_t *args);
98 static dev_info_t dev_info = "btuart_cs";
100 dev_link_t *btuart_attach(void);
101 void btuart_detach(dev_link_t *);
103 static dev_link_t *dev_list = NULL;
106 /* Maximum baud rate */
107 #define SPEED_MAX 115200
109 /* Default baud rate: 57600, 115200, 230400 or 460800 */
110 #define DEFAULT_BAUD_RATE 115200
113 /* Transmit states */
114 #define XMIT_SENDING 1
115 #define XMIT_WAKEUP 2
116 #define XMIT_WAITING 8
118 /* Receiver states */
119 #define RECV_WAIT_PACKET_TYPE 0
120 #define RECV_WAIT_EVENT_HEADER 1
121 #define RECV_WAIT_ACL_HEADER 2
122 #define RECV_WAIT_SCO_HEADER 3
123 #define RECV_WAIT_DATA 4
127 /* ======================== Interrupt handling ======================== */
130 static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
134 /* Tx FIFO should be empty */
135 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
138 /* Fill FIFO with current frame */
139 while ((fifo_size-- > 0) && (actual < len)) {
140 /* Transmit next byte */
141 outb(buf[actual], iobase + UART_TX);
149 static void btuart_write_wakeup(btuart_info_t *info)
152 printk(KERN_WARNING "btuart_cs: Call of write_wakeup for unknown device.\n");
156 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
157 set_bit(XMIT_WAKEUP, &(info->tx_state));
162 register unsigned int iobase = info->link.io.BasePort1;
163 register struct sk_buff *skb;
166 clear_bit(XMIT_WAKEUP, &(info->tx_state));
168 if (!(info->link.state & DEV_PRESENT))
171 if (!(skb = skb_dequeue(&(info->txq))))
175 len = btuart_write(iobase, 16, skb->data, skb->len);
176 set_bit(XMIT_WAKEUP, &(info->tx_state));
178 if (len == skb->len) {
182 skb_queue_head(&(info->txq), skb);
185 info->hdev.stat.byte_tx += len;
187 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
189 clear_bit(XMIT_SENDING, &(info->tx_state));
193 static void btuart_receive(btuart_info_t *info)
199 printk(KERN_WARNING "btuart_cs: Call of receive for unknown device.\n");
203 iobase = info->link.io.BasePort1;
206 info->hdev.stat.byte_rx++;
208 /* Allocate packet */
209 if (info->rx_skb == NULL) {
210 info->rx_state = RECV_WAIT_PACKET_TYPE;
212 if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
213 printk(KERN_WARNING "btuart_cs: Can't allocate mem for new packet.\n");
218 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
220 info->rx_skb->dev = (void *)&(info->hdev);
221 info->rx_skb->pkt_type = inb(iobase + UART_RX);
223 switch (info->rx_skb->pkt_type) {
226 info->rx_state = RECV_WAIT_EVENT_HEADER;
227 info->rx_count = HCI_EVENT_HDR_SIZE;
230 case HCI_ACLDATA_PKT:
231 info->rx_state = RECV_WAIT_ACL_HEADER;
232 info->rx_count = HCI_ACL_HDR_SIZE;
235 case HCI_SCODATA_PKT:
236 info->rx_state = RECV_WAIT_SCO_HEADER;
237 info->rx_count = HCI_SCO_HDR_SIZE;
242 printk(KERN_WARNING "btuart_cs: Unknown HCI packet with type 0x%02x received.\n", info->rx_skb->pkt_type);
243 info->hdev.stat.err_rx++;
244 clear_bit(HCI_RUNNING, &(info->hdev.flags));
246 kfree_skb(info->rx_skb);
254 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
257 if (info->rx_count == 0) {
265 switch (info->rx_state) {
267 case RECV_WAIT_EVENT_HEADER:
268 eh = (hci_event_hdr *)(info->rx_skb->data);
269 info->rx_state = RECV_WAIT_DATA;
270 info->rx_count = eh->plen;
273 case RECV_WAIT_ACL_HEADER:
274 ah = (hci_acl_hdr *)(info->rx_skb->data);
275 dlen = __le16_to_cpu(ah->dlen);
276 info->rx_state = RECV_WAIT_DATA;
277 info->rx_count = dlen;
280 case RECV_WAIT_SCO_HEADER:
281 sh = (hci_sco_hdr *)(info->rx_skb->data);
282 info->rx_state = RECV_WAIT_DATA;
283 info->rx_count = sh->dlen;
287 hci_recv_frame(info->rx_skb);
297 /* Make sure we don't stay here to long */
298 if (boguscount++ > 16)
301 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
305 void btuart_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
307 btuart_info_t *info = dev_inst;
313 printk(KERN_WARNING "btuart_cs: Call of irq %d for unknown device.\n", irq);
317 iobase = info->link.io.BasePort1;
319 spin_lock(&(info->lock));
321 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
324 /* Clear interrupt */
325 lsr = inb(iobase + UART_LSR);
329 printk(KERN_NOTICE "btuart_cs: RLSI\n");
332 /* Receive interrupt */
333 btuart_receive(info);
336 if (lsr & UART_LSR_THRE) {
337 /* Transmitter ready for data */
338 btuart_write_wakeup(info);
342 printk(KERN_NOTICE "btuart_cs: Unhandled IIR=%#x\n", iir);
346 /* Make sure we don't stay here to long */
347 if (boguscount++ > 100)
350 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
354 spin_unlock(&(info->lock));
358 static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
362 int fcr; /* FIFO control reg */
363 int lcr; /* Line control reg */
367 printk(KERN_WARNING "btuart_cs: Call of change speed for unknown device.\n");
371 iobase = info->link.io.BasePort1;
373 spin_lock_irqsave(&(info->lock), flags);
375 /* Turn off interrupts */
376 outb(0, iobase + UART_IER);
378 divisor = SPEED_MAX / speed;
380 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
383 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
384 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
385 * about this timeout since it will always be fast enough.
389 fcr |= UART_FCR_TRIGGER_1;
391 fcr |= UART_FCR_TRIGGER_14;
393 /* Bluetooth cards use 8N1 */
394 lcr = UART_LCR_WLEN8;
396 outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */
397 outb(divisor & 0xff, iobase + UART_DLL); /* Set speed */
398 outb(divisor >> 8, iobase + UART_DLM);
399 outb(lcr, iobase + UART_LCR); /* Set 8N1 */
400 outb(fcr, iobase + UART_FCR); /* Enable FIFO's */
402 /* Turn on interrups */
403 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
405 spin_unlock_irqrestore(&(info->lock), flags);
410 /* ======================== HCI interface ======================== */
413 static int btuart_hci_flush(struct hci_dev *hdev)
415 btuart_info_t *info = (btuart_info_t *)(hdev->driver_data);
418 skb_queue_purge(&(info->txq));
424 static int btuart_hci_open(struct hci_dev *hdev)
426 set_bit(HCI_RUNNING, &(hdev->flags));
432 static int btuart_hci_close(struct hci_dev *hdev)
434 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
437 btuart_hci_flush(hdev);
443 static int btuart_hci_send_frame(struct sk_buff *skb)
446 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
449 printk(KERN_WARNING "btuart_cs: Frame for unknown HCI device (hdev=NULL).");
453 info = (btuart_info_t *)(hdev->driver_data);
455 switch (skb->pkt_type) {
456 case HCI_COMMAND_PKT:
459 case HCI_ACLDATA_PKT:
462 case HCI_SCODATA_PKT:
467 /* Prepend skb with frame type */
468 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
469 skb_queue_tail(&(info->txq), skb);
471 btuart_write_wakeup(info);
477 static void btuart_hci_destruct(struct hci_dev *hdev)
482 static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
489 /* ======================== Card services HCI interaction ======================== */
492 int btuart_open(btuart_info_t *info)
495 unsigned int iobase = info->link.io.BasePort1;
496 struct hci_dev *hdev;
498 spin_lock_init(&(info->lock));
500 skb_queue_head_init(&(info->txq));
502 info->rx_state = RECV_WAIT_PACKET_TYPE;
506 spin_lock_irqsave(&(info->lock), flags);
509 outb(0, iobase + UART_MCR);
511 /* Turn off interrupts */
512 outb(0, iobase + UART_IER);
514 /* Initialize UART */
515 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
516 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
518 /* Turn on interrupts */
519 // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
521 spin_unlock_irqrestore(&(info->lock), flags);
523 btuart_change_speed(info, DEFAULT_BAUD_RATE);
525 /* Timeout before it is safe to send the first HCI packet */
526 set_current_state(TASK_INTERRUPTIBLE);
527 schedule_timeout(HZ);
530 /* Initialize and register HCI device */
532 hdev = &(info->hdev);
534 hdev->type = HCI_PCCARD;
535 hdev->driver_data = info;
537 hdev->open = btuart_hci_open;
538 hdev->close = btuart_hci_close;
539 hdev->flush = btuart_hci_flush;
540 hdev->send = btuart_hci_send_frame;
541 hdev->destruct = btuart_hci_destruct;
542 hdev->ioctl = btuart_hci_ioctl;
544 if (hci_register_dev(hdev) < 0) {
545 printk(KERN_WARNING "btuart_cs: Can't register HCI device %s.\n", hdev->name);
553 int btuart_close(btuart_info_t *info)
556 unsigned int iobase = info->link.io.BasePort1;
557 struct hci_dev *hdev = &(info->hdev);
559 if (info->link.state & DEV_CONFIG_PENDING)
562 btuart_hci_close(hdev);
564 spin_lock_irqsave(&(info->lock), flags);
567 outb(0, iobase + UART_MCR);
569 /* Turn off interrupts */
570 outb(0, iobase + UART_IER);
572 spin_unlock_irqrestore(&(info->lock), flags);
574 if (hci_unregister_dev(hdev) < 0)
575 printk(KERN_WARNING "btuart_cs: Can't unregister HCI device %s.\n", hdev->name);
582 /* ======================== Card services ======================== */
585 static void cs_error(client_handle_t handle, int func, int ret)
587 error_info_t err = { func, ret };
589 CardServices(ReportError, handle, &err);
593 dev_link_t *btuart_attach(void)
596 client_reg_t client_reg;
600 /* Create new info device */
601 info = kmalloc(sizeof(*info), GFP_KERNEL);
604 memset(info, 0, sizeof(*info));
609 link->release.function = &btuart_release;
610 link->release.data = (u_long)link;
611 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
612 link->io.NumPorts1 = 8;
613 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
614 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
616 if (irq_list[0] == -1)
617 link->irq.IRQInfo2 = irq_mask;
619 for (i = 0; i < 4; i++)
620 link->irq.IRQInfo2 |= 1 << irq_list[i];
622 link->irq.Handler = btuart_interrupt;
623 link->irq.Instance = info;
625 link->conf.Attributes = CONF_ENABLE_IRQ;
627 link->conf.IntType = INT_MEMORY_AND_IO;
629 /* Register with Card Services */
630 link->next = dev_list;
632 client_reg.dev_info = &dev_info;
633 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
634 client_reg.EventMask =
635 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
636 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
637 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
638 client_reg.event_handler = &btuart_event;
639 client_reg.Version = 0x0210;
640 client_reg.event_callback_args.client_data = link;
642 ret = CardServices(RegisterClient, &link->handle, &client_reg);
643 if (ret != CS_SUCCESS) {
644 cs_error(link->handle, RegisterClient, ret);
653 void btuart_detach(dev_link_t *link)
655 btuart_info_t *info = link->priv;
659 /* Locate device structure */
660 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
667 del_timer(&link->release);
668 if (link->state & DEV_CONFIG)
669 btuart_release((u_long)link);
672 ret = CardServices(DeregisterClient, link->handle);
673 if (ret != CS_SUCCESS)
674 cs_error(link->handle, DeregisterClient, ret);
677 /* Unlink device structure, free bits */
684 static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
688 i = CardServices(fn, handle, tuple);
690 return CS_NO_MORE_ITEMS;
692 i = CardServices(GetTupleData, handle, tuple);
696 return CardServices(ParseTuple, handle, tuple, parse);
700 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
701 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
703 void btuart_config(dev_link_t *link)
705 static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
706 client_handle_t handle = link->handle;
707 btuart_info_t *info = link->priv;
711 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
712 config_info_t config;
713 int i, j, try, last_ret, last_fn;
715 tuple.TupleData = (cisdata_t *)buf;
716 tuple.TupleOffset = 0;
717 tuple.TupleDataMax = 255;
718 tuple.Attributes = 0;
720 /* Get configuration register information */
721 tuple.DesiredTuple = CISTPL_CONFIG;
722 last_ret = first_tuple(handle, &tuple, &parse);
723 if (last_ret != CS_SUCCESS) {
724 last_fn = ParseTuple;
727 link->conf.ConfigBase = parse.config.base;
728 link->conf.Present = parse.config.rmask[0];
731 link->state |= DEV_CONFIG;
732 i = CardServices(GetConfigurationInfo, handle, &config);
733 link->conf.Vcc = config.Vcc;
735 /* First pass: look for a config entry that looks normal. */
736 tuple.TupleData = (cisdata_t *) buf;
737 tuple.TupleOffset = 0;
738 tuple.TupleDataMax = 255;
739 tuple.Attributes = 0;
740 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
741 /* Two tries: without IO aliases, then with aliases */
742 for (try = 0; try < 2; try++) {
743 i = first_tuple(handle, &tuple, &parse);
744 while (i != CS_NO_MORE_ITEMS) {
747 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
748 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
749 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
750 link->conf.ConfigIndex = cf->index;
751 link->io.BasePort1 = cf->io.win[0].base;
752 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
753 i = CardServices(RequestIO, link->handle, &link->io);
758 i = next_tuple(handle, &tuple, &parse);
762 /* Second pass: try to find an entry that isn't picky about
763 its base address, then try to grab any standard serial port
764 address, and finally try to get any free port. */
765 i = first_tuple(handle, &tuple, &parse);
766 while (i != CS_NO_MORE_ITEMS) {
767 if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
768 && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
769 link->conf.ConfigIndex = cf->index;
770 for (j = 0; j < 5; j++) {
771 link->io.BasePort1 = base[j];
772 link->io.IOAddrLines = base[j] ? 16 : 3;
773 i = CardServices(RequestIO, link->handle, &link->io);
778 i = next_tuple(handle, &tuple, &parse);
782 if (i != CS_SUCCESS) {
783 printk(KERN_NOTICE "btuart_cs: No usable port range found. Giving up.\n");
784 cs_error(link->handle, RequestIO, i);
788 i = CardServices(RequestIRQ, link->handle, &link->irq);
789 if (i != CS_SUCCESS) {
790 cs_error(link->handle, RequestIRQ, i);
791 link->irq.AssignedIRQ = 0;
794 i = CardServices(RequestConfiguration, link->handle, &link->conf);
795 if (i != CS_SUCCESS) {
796 cs_error(link->handle, RequestConfiguration, i);
802 if (btuart_open(info) != 0)
805 strcpy(info->node.dev_name, info->hdev.name);
806 link->dev = &info->node;
807 link->state &= ~DEV_CONFIG_PENDING;
812 cs_error(link->handle, last_fn, last_ret);
815 btuart_release((u_long) link);
819 void btuart_release(u_long arg)
821 dev_link_t *link = (dev_link_t *)arg;
822 btuart_info_t *info = link->priv;
824 if (link->state & DEV_PRESENT)
831 CardServices(ReleaseConfiguration, link->handle);
832 CardServices(ReleaseIO, link->handle, &link->io);
833 CardServices(ReleaseIRQ, link->handle, &link->irq);
835 link->state &= ~DEV_CONFIG;
839 int btuart_event(event_t event, int priority, event_callback_args_t *args)
841 dev_link_t *link = args->client_data;
842 btuart_info_t *info = link->priv;
845 case CS_EVENT_CARD_REMOVAL:
846 link->state &= ~DEV_PRESENT;
847 if (link->state & DEV_CONFIG) {
849 mod_timer(&link->release, jiffies + HZ / 20);
852 case CS_EVENT_CARD_INSERTION:
853 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
856 case CS_EVENT_PM_SUSPEND:
857 link->state |= DEV_SUSPEND;
858 /* Fall through... */
859 case CS_EVENT_RESET_PHYSICAL:
860 if (link->state & DEV_CONFIG)
861 CardServices(ReleaseConfiguration, link->handle);
863 case CS_EVENT_PM_RESUME:
864 link->state &= ~DEV_SUSPEND;
865 /* Fall through... */
866 case CS_EVENT_CARD_RESET:
868 CardServices(RequestConfiguration, link->handle, &link->conf);
877 /* ======================== Module initialization ======================== */
880 int __init init_btuart_cs(void)
885 CardServices(GetCardServicesInfo, &serv);
886 if (serv.Revision != CS_RELEASE_CODE) {
887 printk(KERN_NOTICE "btuart_cs: Card Services release does not match!\n");
891 err = register_pccard_driver(&dev_info, &btuart_attach, &btuart_detach);
897 void __exit exit_btuart_cs(void)
899 unregister_pccard_driver(&dev_info);
901 while (dev_list != NULL)
902 btuart_detach(dev_list);
906 module_init(init_btuart_cs);
907 module_exit(exit_btuart_cs);