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/module.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/sched.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
32 #include <linux/ptrace.h>
33 #include <linux/ioport.h>
34 #include <linux/spinlock.h>
35 #include <linux/moduleparam.h>
37 #include <linux/skbuff.h>
38 #include <linux/string.h>
39 #include <linux/serial.h>
40 #include <linux/serial_reg.h>
41 #include <linux/bitops.h>
42 #include <asm/system.h>
45 #include <pcmcia/cs_types.h>
46 #include <pcmcia/cs.h>
47 #include <pcmcia/cistpl.h>
48 #include <pcmcia/ciscode.h>
49 #include <pcmcia/ds.h>
50 #include <pcmcia/cisreg.h>
52 #include <net/bluetooth/bluetooth.h>
53 #include <net/bluetooth/hci_core.h>
57 /* ======================== Module parameters ======================== */
60 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
61 MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface");
62 MODULE_LICENSE("GPL");
66 /* ======================== Local structures ======================== */
69 typedef struct btuart_info_t {
70 struct pcmcia_device *p_dev;
75 spinlock_t lock; /* For serializing operations */
77 struct sk_buff_head txq;
78 unsigned long tx_state;
80 unsigned long rx_state;
81 unsigned long rx_count;
82 struct sk_buff *rx_skb;
86 static int btuart_config(struct pcmcia_device *link);
87 static void btuart_release(struct pcmcia_device *link);
89 static void btuart_detach(struct pcmcia_device *p_dev);
92 /* Maximum baud rate */
93 #define SPEED_MAX 115200
95 /* Default baud rate: 57600, 115200, 230400 or 460800 */
96 #define DEFAULT_BAUD_RATE 115200
100 #define XMIT_SENDING 1
101 #define XMIT_WAKEUP 2
102 #define XMIT_WAITING 8
104 /* Receiver states */
105 #define RECV_WAIT_PACKET_TYPE 0
106 #define RECV_WAIT_EVENT_HEADER 1
107 #define RECV_WAIT_ACL_HEADER 2
108 #define RECV_WAIT_SCO_HEADER 3
109 #define RECV_WAIT_DATA 4
113 /* ======================== Interrupt handling ======================== */
116 static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
120 /* Tx FIFO should be empty */
121 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
124 /* Fill FIFO with current frame */
125 while ((fifo_size-- > 0) && (actual < len)) {
126 /* Transmit next byte */
127 outb(buf[actual], iobase + UART_TX);
135 static void btuart_write_wakeup(btuart_info_t *info)
138 BT_ERR("Unknown device");
142 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
143 set_bit(XMIT_WAKEUP, &(info->tx_state));
148 register unsigned int iobase = info->p_dev->io.BasePort1;
149 register struct sk_buff *skb;
152 clear_bit(XMIT_WAKEUP, &(info->tx_state));
154 if (!pcmcia_dev_present(info->p_dev))
157 if (!(skb = skb_dequeue(&(info->txq))))
161 len = btuart_write(iobase, 16, skb->data, skb->len);
162 set_bit(XMIT_WAKEUP, &(info->tx_state));
164 if (len == skb->len) {
168 skb_queue_head(&(info->txq), skb);
171 info->hdev->stat.byte_tx += len;
173 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
175 clear_bit(XMIT_SENDING, &(info->tx_state));
179 static void btuart_receive(btuart_info_t *info)
185 BT_ERR("Unknown device");
189 iobase = info->p_dev->io.BasePort1;
192 info->hdev->stat.byte_rx++;
194 /* Allocate packet */
195 if (info->rx_skb == NULL) {
196 info->rx_state = RECV_WAIT_PACKET_TYPE;
198 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
199 BT_ERR("Can't allocate mem for new packet");
204 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
206 info->rx_skb->dev = (void *) info->hdev;
207 bt_cb(info->rx_skb)->pkt_type = inb(iobase + UART_RX);
209 switch (bt_cb(info->rx_skb)->pkt_type) {
212 info->rx_state = RECV_WAIT_EVENT_HEADER;
213 info->rx_count = HCI_EVENT_HDR_SIZE;
216 case HCI_ACLDATA_PKT:
217 info->rx_state = RECV_WAIT_ACL_HEADER;
218 info->rx_count = HCI_ACL_HDR_SIZE;
221 case HCI_SCODATA_PKT:
222 info->rx_state = RECV_WAIT_SCO_HEADER;
223 info->rx_count = HCI_SCO_HDR_SIZE;
228 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
229 info->hdev->stat.err_rx++;
230 clear_bit(HCI_RUNNING, &(info->hdev->flags));
232 kfree_skb(info->rx_skb);
240 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
243 if (info->rx_count == 0) {
246 struct hci_event_hdr *eh;
247 struct hci_acl_hdr *ah;
248 struct hci_sco_hdr *sh;
251 switch (info->rx_state) {
253 case RECV_WAIT_EVENT_HEADER:
254 eh = (struct hci_event_hdr *)(info->rx_skb->data);
255 info->rx_state = RECV_WAIT_DATA;
256 info->rx_count = eh->plen;
259 case RECV_WAIT_ACL_HEADER:
260 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
261 dlen = __le16_to_cpu(ah->dlen);
262 info->rx_state = RECV_WAIT_DATA;
263 info->rx_count = dlen;
266 case RECV_WAIT_SCO_HEADER:
267 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
268 info->rx_state = RECV_WAIT_DATA;
269 info->rx_count = sh->dlen;
273 hci_recv_frame(info->rx_skb);
283 /* Make sure we don't stay here too long */
284 if (boguscount++ > 16)
287 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
291 static irqreturn_t btuart_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
293 btuart_info_t *info = dev_inst;
298 if (!info || !info->hdev) {
299 BT_ERR("Call of irq %d for unknown device", irq);
303 iobase = info->p_dev->io.BasePort1;
305 spin_lock(&(info->lock));
307 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
310 /* Clear interrupt */
311 lsr = inb(iobase + UART_LSR);
318 /* Receive interrupt */
319 btuart_receive(info);
322 if (lsr & UART_LSR_THRE) {
323 /* Transmitter ready for data */
324 btuart_write_wakeup(info);
328 BT_ERR("Unhandled IIR=%#x", iir);
332 /* Make sure we don't stay here too long */
333 if (boguscount++ > 100)
336 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
340 spin_unlock(&(info->lock));
346 static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
350 int fcr; /* FIFO control reg */
351 int lcr; /* Line control reg */
355 BT_ERR("Unknown device");
359 iobase = info->p_dev->io.BasePort1;
361 spin_lock_irqsave(&(info->lock), flags);
363 /* Turn off interrupts */
364 outb(0, iobase + UART_IER);
366 divisor = SPEED_MAX / speed;
368 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
371 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
372 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
373 * about this timeout since it will always be fast enough.
377 fcr |= UART_FCR_TRIGGER_1;
379 fcr |= UART_FCR_TRIGGER_14;
381 /* Bluetooth cards use 8N1 */
382 lcr = UART_LCR_WLEN8;
384 outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */
385 outb(divisor & 0xff, iobase + UART_DLL); /* Set speed */
386 outb(divisor >> 8, iobase + UART_DLM);
387 outb(lcr, iobase + UART_LCR); /* Set 8N1 */
388 outb(fcr, iobase + UART_FCR); /* Enable FIFO's */
390 /* Turn on interrups */
391 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
393 spin_unlock_irqrestore(&(info->lock), flags);
398 /* ======================== HCI interface ======================== */
401 static int btuart_hci_flush(struct hci_dev *hdev)
403 btuart_info_t *info = (btuart_info_t *)(hdev->driver_data);
406 skb_queue_purge(&(info->txq));
412 static int btuart_hci_open(struct hci_dev *hdev)
414 set_bit(HCI_RUNNING, &(hdev->flags));
420 static int btuart_hci_close(struct hci_dev *hdev)
422 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
425 btuart_hci_flush(hdev);
431 static int btuart_hci_send_frame(struct sk_buff *skb)
434 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
437 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
441 info = (btuart_info_t *)(hdev->driver_data);
443 switch (bt_cb(skb)->pkt_type) {
444 case HCI_COMMAND_PKT:
447 case HCI_ACLDATA_PKT:
450 case HCI_SCODATA_PKT:
455 /* Prepend skb with frame type */
456 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
457 skb_queue_tail(&(info->txq), skb);
459 btuart_write_wakeup(info);
465 static void btuart_hci_destruct(struct hci_dev *hdev)
470 static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
477 /* ======================== Card services HCI interaction ======================== */
480 static int btuart_open(btuart_info_t *info)
483 unsigned int iobase = info->p_dev->io.BasePort1;
484 struct hci_dev *hdev;
486 spin_lock_init(&(info->lock));
488 skb_queue_head_init(&(info->txq));
490 info->rx_state = RECV_WAIT_PACKET_TYPE;
494 /* Initialize HCI device */
495 hdev = hci_alloc_dev();
497 BT_ERR("Can't allocate HCI device");
503 hdev->type = HCI_PCCARD;
504 hdev->driver_data = info;
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;
513 hdev->owner = THIS_MODULE;
515 spin_lock_irqsave(&(info->lock), flags);
518 outb(0, iobase + UART_MCR);
520 /* Turn off interrupts */
521 outb(0, iobase + UART_IER);
523 /* Initialize UART */
524 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
525 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
527 /* Turn on interrupts */
528 // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
530 spin_unlock_irqrestore(&(info->lock), flags);
532 btuart_change_speed(info, DEFAULT_BAUD_RATE);
534 /* Timeout before it is safe to send the first HCI packet */
537 /* Register HCI device */
538 if (hci_register_dev(hdev) < 0) {
539 BT_ERR("Can't register HCI device");
549 static int btuart_close(btuart_info_t *info)
552 unsigned int iobase = info->p_dev->io.BasePort1;
553 struct hci_dev *hdev = info->hdev;
558 btuart_hci_close(hdev);
560 spin_lock_irqsave(&(info->lock), flags);
563 outb(0, iobase + UART_MCR);
565 /* Turn off interrupts */
566 outb(0, iobase + UART_IER);
568 spin_unlock_irqrestore(&(info->lock), flags);
570 if (hci_unregister_dev(hdev) < 0)
571 BT_ERR("Can't unregister HCI device %s", hdev->name);
578 static int btuart_probe(struct pcmcia_device *link)
582 /* Create new info device */
583 info = kzalloc(sizeof(*info), GFP_KERNEL);
590 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
591 link->io.NumPorts1 = 8;
592 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
593 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
595 link->irq.Handler = btuart_interrupt;
596 link->irq.Instance = info;
598 link->conf.Attributes = CONF_ENABLE_IRQ;
599 link->conf.IntType = INT_MEMORY_AND_IO;
601 return btuart_config(link);
605 static void btuart_detach(struct pcmcia_device *link)
607 btuart_info_t *info = link->priv;
609 btuart_release(link);
613 static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
617 i = pcmcia_get_tuple_data(handle, tuple);
621 return pcmcia_parse_tuple(handle, tuple, parse);
624 static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
626 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
627 return CS_NO_MORE_ITEMS;
628 return get_tuple(handle, tuple, parse);
631 static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
633 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
634 return CS_NO_MORE_ITEMS;
635 return get_tuple(handle, tuple, parse);
638 static int btuart_config(struct pcmcia_device *link)
640 static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
641 btuart_info_t *info = link->priv;
645 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
646 int i, j, try, last_ret, last_fn;
648 tuple.TupleData = (cisdata_t *)buf;
649 tuple.TupleOffset = 0;
650 tuple.TupleDataMax = 255;
651 tuple.Attributes = 0;
653 /* Get configuration register information */
654 tuple.DesiredTuple = CISTPL_CONFIG;
655 last_ret = first_tuple(link, &tuple, &parse);
656 if (last_ret != CS_SUCCESS) {
657 last_fn = ParseTuple;
660 link->conf.ConfigBase = parse.config.base;
661 link->conf.Present = parse.config.rmask[0];
663 /* First pass: look for a config entry that looks normal. */
664 tuple.TupleData = (cisdata_t *) buf;
665 tuple.TupleOffset = 0;
666 tuple.TupleDataMax = 255;
667 tuple.Attributes = 0;
668 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
669 /* Two tries: without IO aliases, then with aliases */
670 for (try = 0; try < 2; try++) {
671 i = first_tuple(link, &tuple, &parse);
672 while (i != CS_NO_MORE_ITEMS) {
675 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
676 link->conf.Vpp = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
677 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
678 link->conf.ConfigIndex = cf->index;
679 link->io.BasePort1 = cf->io.win[0].base;
680 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
681 i = pcmcia_request_io(link, &link->io);
686 i = next_tuple(link, &tuple, &parse);
690 /* Second pass: try to find an entry that isn't picky about
691 its base address, then try to grab any standard serial port
692 address, and finally try to get any free port. */
693 i = first_tuple(link, &tuple, &parse);
694 while (i != CS_NO_MORE_ITEMS) {
695 if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
696 && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
697 link->conf.ConfigIndex = cf->index;
698 for (j = 0; j < 5; j++) {
699 link->io.BasePort1 = base[j];
700 link->io.IOAddrLines = base[j] ? 16 : 3;
701 i = pcmcia_request_io(link, &link->io);
706 i = next_tuple(link, &tuple, &parse);
710 if (i != CS_SUCCESS) {
711 BT_ERR("No usable port range found");
712 cs_error(link, RequestIO, i);
716 i = pcmcia_request_irq(link, &link->irq);
717 if (i != CS_SUCCESS) {
718 cs_error(link, RequestIRQ, i);
719 link->irq.AssignedIRQ = 0;
722 i = pcmcia_request_configuration(link, &link->conf);
723 if (i != CS_SUCCESS) {
724 cs_error(link, RequestConfiguration, i);
728 if (btuart_open(info) != 0)
731 strcpy(info->node.dev_name, info->hdev->name);
732 link->dev_node = &info->node;
737 cs_error(link, last_fn, last_ret);
740 btuart_release(link);
745 static void btuart_release(struct pcmcia_device *link)
747 btuart_info_t *info = link->priv;
751 pcmcia_disable_device(link);
754 static struct pcmcia_device_id btuart_ids[] = {
755 /* don't use this driver. Use serial_cs + hci_uart instead */
758 MODULE_DEVICE_TABLE(pcmcia, btuart_ids);
760 static struct pcmcia_driver btuart_driver = {
761 .owner = THIS_MODULE,
765 .probe = btuart_probe,
766 .remove = btuart_detach,
767 .id_table = btuart_ids,
770 static int __init init_btuart_cs(void)
772 return pcmcia_register_driver(&btuart_driver);
776 static void __exit exit_btuart_cs(void)
778 pcmcia_unregister_driver(&btuart_driver);
781 module_init(init_btuart_cs);
782 module_exit(exit_btuart_cs);