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 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 printk(KERN_WARNING "btuart_cs: Can't unregister HCI device %s.\n", hdev->name);
579 /* ======================== Card services ======================== */
582 static void cs_error(client_handle_t handle, int func, int ret)
584 error_info_t err = { func, ret };
586 CardServices(ReportError, handle, &err);
590 dev_link_t *btuart_attach(void)
593 client_reg_t client_reg;
597 /* Create new info device */
598 info = kmalloc(sizeof(*info), GFP_KERNEL);
601 memset(info, 0, sizeof(*info));
606 link->release.function = &btuart_release;
607 link->release.data = (u_long)link;
608 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
609 link->io.NumPorts1 = 8;
610 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
611 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
613 if (irq_list[0] == -1)
614 link->irq.IRQInfo2 = irq_mask;
616 for (i = 0; i < 4; i++)
617 link->irq.IRQInfo2 |= 1 << irq_list[i];
619 link->irq.Handler = btuart_interrupt;
620 link->irq.Instance = info;
622 link->conf.Attributes = CONF_ENABLE_IRQ;
624 link->conf.IntType = INT_MEMORY_AND_IO;
626 /* Register with Card Services */
627 link->next = dev_list;
629 client_reg.dev_info = &dev_info;
630 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
631 client_reg.EventMask =
632 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
633 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
634 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
635 client_reg.event_handler = &btuart_event;
636 client_reg.Version = 0x0210;
637 client_reg.event_callback_args.client_data = link;
639 ret = CardServices(RegisterClient, &link->handle, &client_reg);
640 if (ret != CS_SUCCESS) {
641 cs_error(link->handle, RegisterClient, ret);
650 void btuart_detach(dev_link_t *link)
652 btuart_info_t *info = link->priv;
656 /* Locate device structure */
657 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
664 del_timer(&link->release);
665 if (link->state & DEV_CONFIG)
666 btuart_release((u_long)link);
669 ret = CardServices(DeregisterClient, link->handle);
670 if (ret != CS_SUCCESS)
671 cs_error(link->handle, DeregisterClient, ret);
674 /* Unlink device structure, free bits */
681 static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
685 i = CardServices(fn, handle, tuple);
687 return CS_NO_MORE_ITEMS;
689 i = CardServices(GetTupleData, handle, tuple);
693 return CardServices(ParseTuple, handle, tuple, parse);
697 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
698 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
700 void btuart_config(dev_link_t *link)
702 static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
703 client_handle_t handle = link->handle;
704 btuart_info_t *info = link->priv;
708 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
709 config_info_t config;
710 int i, j, try, last_ret, last_fn;
712 tuple.TupleData = (cisdata_t *)buf;
713 tuple.TupleOffset = 0;
714 tuple.TupleDataMax = 255;
715 tuple.Attributes = 0;
717 /* Get configuration register information */
718 tuple.DesiredTuple = CISTPL_CONFIG;
719 last_ret = first_tuple(handle, &tuple, &parse);
720 if (last_ret != CS_SUCCESS) {
721 last_fn = ParseTuple;
724 link->conf.ConfigBase = parse.config.base;
725 link->conf.Present = parse.config.rmask[0];
728 link->state |= DEV_CONFIG;
729 i = CardServices(GetConfigurationInfo, handle, &config);
730 link->conf.Vcc = config.Vcc;
732 /* First pass: look for a config entry that looks normal. */
733 tuple.TupleData = (cisdata_t *) buf;
734 tuple.TupleOffset = 0;
735 tuple.TupleDataMax = 255;
736 tuple.Attributes = 0;
737 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
738 /* Two tries: without IO aliases, then with aliases */
739 for (try = 0; try < 2; try++) {
740 i = first_tuple(handle, &tuple, &parse);
741 while (i != CS_NO_MORE_ITEMS) {
744 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
745 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
746 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
747 link->conf.ConfigIndex = cf->index;
748 link->io.BasePort1 = cf->io.win[0].base;
749 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
750 i = CardServices(RequestIO, link->handle, &link->io);
755 i = next_tuple(handle, &tuple, &parse);
759 /* Second pass: try to find an entry that isn't picky about
760 its base address, then try to grab any standard serial port
761 address, and finally try to get any free port. */
762 i = first_tuple(handle, &tuple, &parse);
763 while (i != CS_NO_MORE_ITEMS) {
764 if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
765 && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
766 link->conf.ConfigIndex = cf->index;
767 for (j = 0; j < 5; j++) {
768 link->io.BasePort1 = base[j];
769 link->io.IOAddrLines = base[j] ? 16 : 3;
770 i = CardServices(RequestIO, link->handle, &link->io);
775 i = next_tuple(handle, &tuple, &parse);
779 if (i != CS_SUCCESS) {
780 printk(KERN_NOTICE "btuart_cs: No usable port range found. Giving up.\n");
781 cs_error(link->handle, RequestIO, i);
785 i = CardServices(RequestIRQ, link->handle, &link->irq);
786 if (i != CS_SUCCESS) {
787 cs_error(link->handle, RequestIRQ, i);
788 link->irq.AssignedIRQ = 0;
791 i = CardServices(RequestConfiguration, link->handle, &link->conf);
792 if (i != CS_SUCCESS) {
793 cs_error(link->handle, RequestConfiguration, i);
799 if (btuart_open(info) != 0)
802 strcpy(info->node.dev_name, info->hdev.name);
803 link->dev = &info->node;
804 link->state &= ~DEV_CONFIG_PENDING;
809 cs_error(link->handle, last_fn, last_ret);
812 btuart_release((u_long) link);
816 void btuart_release(u_long arg)
818 dev_link_t *link = (dev_link_t *)arg;
819 btuart_info_t *info = link->priv;
821 if (link->state & DEV_PRESENT)
828 CardServices(ReleaseConfiguration, link->handle);
829 CardServices(ReleaseIO, link->handle, &link->io);
830 CardServices(ReleaseIRQ, link->handle, &link->irq);
832 link->state &= ~DEV_CONFIG;
836 int btuart_event(event_t event, int priority, event_callback_args_t *args)
838 dev_link_t *link = args->client_data;
839 btuart_info_t *info = link->priv;
842 case CS_EVENT_CARD_REMOVAL:
843 link->state &= ~DEV_PRESENT;
844 if (link->state & DEV_CONFIG) {
846 mod_timer(&link->release, jiffies + HZ / 20);
849 case CS_EVENT_CARD_INSERTION:
850 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
853 case CS_EVENT_PM_SUSPEND:
854 link->state |= DEV_SUSPEND;
855 /* Fall through... */
856 case CS_EVENT_RESET_PHYSICAL:
857 if (link->state & DEV_CONFIG)
858 CardServices(ReleaseConfiguration, link->handle);
860 case CS_EVENT_PM_RESUME:
861 link->state &= ~DEV_SUSPEND;
862 /* Fall through... */
863 case CS_EVENT_CARD_RESET:
865 CardServices(RequestConfiguration, link->handle, &link->conf);
874 /* ======================== Module initialization ======================== */
877 int __init init_btuart_cs(void)
882 CardServices(GetCardServicesInfo, &serv);
883 if (serv.Revision != CS_RELEASE_CODE) {
884 printk(KERN_NOTICE "btuart_cs: Card Services release does not match!\n");
888 err = register_pccard_driver(&dev_info, &btuart_attach, &btuart_detach);
894 void __exit exit_btuart_cs(void)
896 unregister_pccard_driver(&dev_info);
898 while (dev_list != NULL)
899 btuart_detach(dev_list);
903 module_init(init_btuart_cs);
904 module_exit(exit_btuart_cs);