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(dev_link_t
*);
96 static dev_link_t
*dev_list
= NULL
;
99 /* Maximum baud rate */
100 #define SPEED_MAX 115200
102 /* Default baud rate: 57600, 115200, 230400 or 460800 */
103 #define DEFAULT_BAUD_RATE 115200
106 /* Transmit states */
107 #define XMIT_SENDING 1
108 #define XMIT_WAKEUP 2
109 #define XMIT_WAITING 8
111 /* Receiver states */
112 #define RECV_WAIT_PACKET_TYPE 0
113 #define RECV_WAIT_EVENT_HEADER 1
114 #define RECV_WAIT_ACL_HEADER 2
115 #define RECV_WAIT_SCO_HEADER 3
116 #define RECV_WAIT_DATA 4
120 /* ======================== Interrupt handling ======================== */
123 static int btuart_write(unsigned int iobase
, int fifo_size
, __u8
*buf
, int len
)
127 /* Tx FIFO should be empty */
128 if (!(inb(iobase
+ UART_LSR
) & UART_LSR_THRE
))
131 /* Fill FIFO with current frame */
132 while ((fifo_size
-- > 0) && (actual
< len
)) {
133 /* Transmit next byte */
134 outb(buf
[actual
], iobase
+ UART_TX
);
142 static void btuart_write_wakeup(btuart_info_t
*info
)
145 BT_ERR("Unknown device");
149 if (test_and_set_bit(XMIT_SENDING
, &(info
->tx_state
))) {
150 set_bit(XMIT_WAKEUP
, &(info
->tx_state
));
155 register unsigned int iobase
= info
->link
.io
.BasePort1
;
156 register struct sk_buff
*skb
;
159 clear_bit(XMIT_WAKEUP
, &(info
->tx_state
));
161 if (!(info
->link
.state
& DEV_PRESENT
))
164 if (!(skb
= skb_dequeue(&(info
->txq
))))
168 len
= btuart_write(iobase
, 16, skb
->data
, skb
->len
);
169 set_bit(XMIT_WAKEUP
, &(info
->tx_state
));
171 if (len
== skb
->len
) {
175 skb_queue_head(&(info
->txq
), skb
);
178 info
->hdev
->stat
.byte_tx
+= len
;
180 } while (test_bit(XMIT_WAKEUP
, &(info
->tx_state
)));
182 clear_bit(XMIT_SENDING
, &(info
->tx_state
));
186 static void btuart_receive(btuart_info_t
*info
)
192 BT_ERR("Unknown device");
196 iobase
= info
->link
.io
.BasePort1
;
199 info
->hdev
->stat
.byte_rx
++;
201 /* Allocate packet */
202 if (info
->rx_skb
== NULL
) {
203 info
->rx_state
= RECV_WAIT_PACKET_TYPE
;
205 if (!(info
->rx_skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
))) {
206 BT_ERR("Can't allocate mem for new packet");
211 if (info
->rx_state
== RECV_WAIT_PACKET_TYPE
) {
213 info
->rx_skb
->dev
= (void *) info
->hdev
;
214 info
->rx_skb
->pkt_type
= inb(iobase
+ UART_RX
);
216 switch (info
->rx_skb
->pkt_type
) {
219 info
->rx_state
= RECV_WAIT_EVENT_HEADER
;
220 info
->rx_count
= HCI_EVENT_HDR_SIZE
;
223 case HCI_ACLDATA_PKT
:
224 info
->rx_state
= RECV_WAIT_ACL_HEADER
;
225 info
->rx_count
= HCI_ACL_HDR_SIZE
;
228 case HCI_SCODATA_PKT
:
229 info
->rx_state
= RECV_WAIT_SCO_HEADER
;
230 info
->rx_count
= HCI_SCO_HDR_SIZE
;
235 BT_ERR("Unknown HCI packet with type 0x%02x received", info
->rx_skb
->pkt_type
);
236 info
->hdev
->stat
.err_rx
++;
237 clear_bit(HCI_RUNNING
, &(info
->hdev
->flags
));
239 kfree_skb(info
->rx_skb
);
247 *skb_put(info
->rx_skb
, 1) = inb(iobase
+ UART_RX
);
250 if (info
->rx_count
== 0) {
253 struct hci_event_hdr
*eh
;
254 struct hci_acl_hdr
*ah
;
255 struct hci_sco_hdr
*sh
;
258 switch (info
->rx_state
) {
260 case RECV_WAIT_EVENT_HEADER
:
261 eh
= (struct hci_event_hdr
*)(info
->rx_skb
->data
);
262 info
->rx_state
= RECV_WAIT_DATA
;
263 info
->rx_count
= eh
->plen
;
266 case RECV_WAIT_ACL_HEADER
:
267 ah
= (struct hci_acl_hdr
*)(info
->rx_skb
->data
);
268 dlen
= __le16_to_cpu(ah
->dlen
);
269 info
->rx_state
= RECV_WAIT_DATA
;
270 info
->rx_count
= dlen
;
273 case RECV_WAIT_SCO_HEADER
:
274 sh
= (struct hci_sco_hdr
*)(info
->rx_skb
->data
);
275 info
->rx_state
= RECV_WAIT_DATA
;
276 info
->rx_count
= sh
->dlen
;
280 hci_recv_frame(info
->rx_skb
);
290 /* Make sure we don't stay here too long */
291 if (boguscount
++ > 16)
294 } while (inb(iobase
+ UART_LSR
) & UART_LSR_DR
);
298 static irqreturn_t
btuart_interrupt(int irq
, void *dev_inst
, struct pt_regs
*regs
)
300 btuart_info_t
*info
= dev_inst
;
305 if (!info
|| !info
->hdev
) {
306 BT_ERR("Call of irq %d for unknown device", irq
);
310 iobase
= info
->link
.io
.BasePort1
;
312 spin_lock(&(info
->lock
));
314 iir
= inb(iobase
+ UART_IIR
) & UART_IIR_ID
;
317 /* Clear interrupt */
318 lsr
= inb(iobase
+ UART_LSR
);
325 /* Receive interrupt */
326 btuart_receive(info
);
329 if (lsr
& UART_LSR_THRE
) {
330 /* Transmitter ready for data */
331 btuart_write_wakeup(info
);
335 BT_ERR("Unhandled IIR=%#x", iir
);
339 /* Make sure we don't stay here too long */
340 if (boguscount
++ > 100)
343 iir
= inb(iobase
+ UART_IIR
) & UART_IIR_ID
;
347 spin_unlock(&(info
->lock
));
353 static void btuart_change_speed(btuart_info_t
*info
, unsigned int speed
)
357 int fcr
; /* FIFO control reg */
358 int lcr
; /* Line control reg */
362 BT_ERR("Unknown device");
366 iobase
= info
->link
.io
.BasePort1
;
368 spin_lock_irqsave(&(info
->lock
), flags
);
370 /* Turn off interrupts */
371 outb(0, iobase
+ UART_IER
);
373 divisor
= SPEED_MAX
/ speed
;
375 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
;
378 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
379 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
380 * about this timeout since it will always be fast enough.
384 fcr
|= UART_FCR_TRIGGER_1
;
386 fcr
|= UART_FCR_TRIGGER_14
;
388 /* Bluetooth cards use 8N1 */
389 lcr
= UART_LCR_WLEN8
;
391 outb(UART_LCR_DLAB
| lcr
, iobase
+ UART_LCR
); /* Set DLAB */
392 outb(divisor
& 0xff, iobase
+ UART_DLL
); /* Set speed */
393 outb(divisor
>> 8, iobase
+ UART_DLM
);
394 outb(lcr
, iobase
+ UART_LCR
); /* Set 8N1 */
395 outb(fcr
, iobase
+ UART_FCR
); /* Enable FIFO's */
397 /* Turn on interrups */
398 outb(UART_IER_RLSI
| UART_IER_RDI
| UART_IER_THRI
, iobase
+ UART_IER
);
400 spin_unlock_irqrestore(&(info
->lock
), flags
);
405 /* ======================== HCI interface ======================== */
408 static int btuart_hci_flush(struct hci_dev
*hdev
)
410 btuart_info_t
*info
= (btuart_info_t
*)(hdev
->driver_data
);
413 skb_queue_purge(&(info
->txq
));
419 static int btuart_hci_open(struct hci_dev
*hdev
)
421 set_bit(HCI_RUNNING
, &(hdev
->flags
));
427 static int btuart_hci_close(struct hci_dev
*hdev
)
429 if (!test_and_clear_bit(HCI_RUNNING
, &(hdev
->flags
)))
432 btuart_hci_flush(hdev
);
438 static int btuart_hci_send_frame(struct sk_buff
*skb
)
441 struct hci_dev
*hdev
= (struct hci_dev
*)(skb
->dev
);
444 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
448 info
= (btuart_info_t
*)(hdev
->driver_data
);
450 switch (skb
->pkt_type
) {
451 case HCI_COMMAND_PKT
:
454 case HCI_ACLDATA_PKT
:
457 case HCI_SCODATA_PKT
:
462 /* Prepend skb with frame type */
463 memcpy(skb_push(skb
, 1), &(skb
->pkt_type
), 1);
464 skb_queue_tail(&(info
->txq
), skb
);
466 btuart_write_wakeup(info
);
472 static void btuart_hci_destruct(struct hci_dev
*hdev
)
477 static int btuart_hci_ioctl(struct hci_dev
*hdev
, unsigned int cmd
, unsigned long arg
)
484 /* ======================== Card services HCI interaction ======================== */
487 static int btuart_open(btuart_info_t
*info
)
490 unsigned int iobase
= info
->link
.io
.BasePort1
;
491 struct hci_dev
*hdev
;
493 spin_lock_init(&(info
->lock
));
495 skb_queue_head_init(&(info
->txq
));
497 info
->rx_state
= RECV_WAIT_PACKET_TYPE
;
501 /* Initialize HCI device */
502 hdev
= hci_alloc_dev();
504 BT_ERR("Can't allocate HCI device");
510 hdev
->type
= HCI_PCCARD
;
511 hdev
->driver_data
= info
;
513 hdev
->open
= btuart_hci_open
;
514 hdev
->close
= btuart_hci_close
;
515 hdev
->flush
= btuart_hci_flush
;
516 hdev
->send
= btuart_hci_send_frame
;
517 hdev
->destruct
= btuart_hci_destruct
;
518 hdev
->ioctl
= btuart_hci_ioctl
;
520 hdev
->owner
= THIS_MODULE
;
522 spin_lock_irqsave(&(info
->lock
), flags
);
525 outb(0, iobase
+ UART_MCR
);
527 /* Turn off interrupts */
528 outb(0, iobase
+ UART_IER
);
530 /* Initialize UART */
531 outb(UART_LCR_WLEN8
, iobase
+ UART_LCR
); /* Reset DLAB */
532 outb((UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
), iobase
+ UART_MCR
);
534 /* Turn on interrupts */
535 // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
537 spin_unlock_irqrestore(&(info
->lock
), flags
);
539 btuart_change_speed(info
, DEFAULT_BAUD_RATE
);
541 /* Timeout before it is safe to send the first HCI packet */
544 /* Register HCI device */
545 if (hci_register_dev(hdev
) < 0) {
546 BT_ERR("Can't register HCI device");
556 static int btuart_close(btuart_info_t
*info
)
559 unsigned int iobase
= info
->link
.io
.BasePort1
;
560 struct hci_dev
*hdev
= info
->hdev
;
565 btuart_hci_close(hdev
);
567 spin_lock_irqsave(&(info
->lock
), flags
);
570 outb(0, iobase
+ UART_MCR
);
572 /* Turn off interrupts */
573 outb(0, iobase
+ UART_IER
);
575 spin_unlock_irqrestore(&(info
->lock
), flags
);
577 if (hci_unregister_dev(hdev
) < 0)
578 BT_ERR("Can't unregister HCI device %s", hdev
->name
);
585 static dev_link_t
*btuart_attach(void)
588 client_reg_t client_reg
;
592 /* Create new info device */
593 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
596 memset(info
, 0, sizeof(*info
));
601 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_8
;
602 link
->io
.NumPorts1
= 8;
603 link
->irq
.Attributes
= IRQ_TYPE_EXCLUSIVE
| IRQ_HANDLE_PRESENT
;
604 link
->irq
.IRQInfo1
= IRQ_LEVEL_ID
;
606 link
->irq
.Handler
= btuart_interrupt
;
607 link
->irq
.Instance
= info
;
609 link
->conf
.Attributes
= CONF_ENABLE_IRQ
;
611 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
613 /* Register with Card Services */
614 link
->next
= dev_list
;
616 client_reg
.dev_info
= &dev_info
;
617 client_reg
.Version
= 0x0210;
618 client_reg
.event_callback_args
.client_data
= link
;
620 ret
= pcmcia_register_client(&link
->handle
, &client_reg
);
621 if (ret
!= CS_SUCCESS
) {
622 cs_error(link
->handle
, RegisterClient
, ret
);
631 static void btuart_detach(dev_link_t
*link
)
633 btuart_info_t
*info
= link
->priv
;
637 /* Locate device structure */
638 for (linkp
= &dev_list
; *linkp
; linkp
= &(*linkp
)->next
)
645 if (link
->state
& DEV_CONFIG
)
646 btuart_release(link
);
649 ret
= pcmcia_deregister_client(link
->handle
);
650 if (ret
!= CS_SUCCESS
)
651 cs_error(link
->handle
, DeregisterClient
, ret
);
654 /* Unlink device structure, free bits */
660 static int get_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
664 i
= pcmcia_get_tuple_data(handle
, tuple
);
668 return pcmcia_parse_tuple(handle
, tuple
, parse
);
671 static int first_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
673 if (pcmcia_get_first_tuple(handle
, tuple
) != CS_SUCCESS
)
674 return CS_NO_MORE_ITEMS
;
675 return get_tuple(handle
, tuple
, parse
);
678 static int next_tuple(client_handle_t handle
, tuple_t
*tuple
, cisparse_t
*parse
)
680 if (pcmcia_get_next_tuple(handle
, tuple
) != CS_SUCCESS
)
681 return CS_NO_MORE_ITEMS
;
682 return get_tuple(handle
, tuple
, parse
);
685 static void btuart_config(dev_link_t
*link
)
687 static kio_addr_t base
[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
688 client_handle_t handle
= link
->handle
;
689 btuart_info_t
*info
= link
->priv
;
693 cistpl_cftable_entry_t
*cf
= &parse
.cftable_entry
;
694 config_info_t config
;
695 int i
, j
, try, last_ret
, last_fn
;
697 tuple
.TupleData
= (cisdata_t
*)buf
;
698 tuple
.TupleOffset
= 0;
699 tuple
.TupleDataMax
= 255;
700 tuple
.Attributes
= 0;
702 /* Get configuration register information */
703 tuple
.DesiredTuple
= CISTPL_CONFIG
;
704 last_ret
= first_tuple(handle
, &tuple
, &parse
);
705 if (last_ret
!= CS_SUCCESS
) {
706 last_fn
= ParseTuple
;
709 link
->conf
.ConfigBase
= parse
.config
.base
;
710 link
->conf
.Present
= parse
.config
.rmask
[0];
713 link
->state
|= DEV_CONFIG
;
714 i
= pcmcia_get_configuration_info(handle
, &config
);
715 link
->conf
.Vcc
= config
.Vcc
;
717 /* First pass: look for a config entry that looks normal. */
718 tuple
.TupleData
= (cisdata_t
*) buf
;
719 tuple
.TupleOffset
= 0;
720 tuple
.TupleDataMax
= 255;
721 tuple
.Attributes
= 0;
722 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
723 /* Two tries: without IO aliases, then with aliases */
724 for (try = 0; try < 2; try++) {
725 i
= first_tuple(handle
, &tuple
, &parse
);
726 while (i
!= CS_NO_MORE_ITEMS
) {
729 if (cf
->vpp1
.present
& (1 << CISTPL_POWER_VNOM
))
730 link
->conf
.Vpp1
= link
->conf
.Vpp2
= cf
->vpp1
.param
[CISTPL_POWER_VNOM
] / 10000;
731 if ((cf
->io
.nwin
> 0) && (cf
->io
.win
[0].len
== 8) && (cf
->io
.win
[0].base
!= 0)) {
732 link
->conf
.ConfigIndex
= cf
->index
;
733 link
->io
.BasePort1
= cf
->io
.win
[0].base
;
734 link
->io
.IOAddrLines
= (try == 0) ? 16 : cf
->io
.flags
& CISTPL_IO_LINES_MASK
;
735 i
= pcmcia_request_io(link
->handle
, &link
->io
);
740 i
= next_tuple(handle
, &tuple
, &parse
);
744 /* Second pass: try to find an entry that isn't picky about
745 its base address, then try to grab any standard serial port
746 address, and finally try to get any free port. */
747 i
= first_tuple(handle
, &tuple
, &parse
);
748 while (i
!= CS_NO_MORE_ITEMS
) {
749 if ((i
== CS_SUCCESS
) && (cf
->io
.nwin
> 0)
750 && ((cf
->io
.flags
& CISTPL_IO_LINES_MASK
) <= 3)) {
751 link
->conf
.ConfigIndex
= cf
->index
;
752 for (j
= 0; j
< 5; j
++) {
753 link
->io
.BasePort1
= base
[j
];
754 link
->io
.IOAddrLines
= base
[j
] ? 16 : 3;
755 i
= pcmcia_request_io(link
->handle
, &link
->io
);
760 i
= next_tuple(handle
, &tuple
, &parse
);
764 if (i
!= CS_SUCCESS
) {
765 BT_ERR("No usable port range found");
766 cs_error(link
->handle
, RequestIO
, i
);
770 i
= pcmcia_request_irq(link
->handle
, &link
->irq
);
771 if (i
!= CS_SUCCESS
) {
772 cs_error(link
->handle
, RequestIRQ
, i
);
773 link
->irq
.AssignedIRQ
= 0;
776 i
= pcmcia_request_configuration(link
->handle
, &link
->conf
);
777 if (i
!= CS_SUCCESS
) {
778 cs_error(link
->handle
, RequestConfiguration
, i
);
782 if (btuart_open(info
) != 0)
785 strcpy(info
->node
.dev_name
, info
->hdev
->name
);
786 link
->dev
= &info
->node
;
787 link
->state
&= ~DEV_CONFIG_PENDING
;
792 cs_error(link
->handle
, last_fn
, last_ret
);
795 btuart_release(link
);
799 static void btuart_release(dev_link_t
*link
)
801 btuart_info_t
*info
= link
->priv
;
803 if (link
->state
& DEV_PRESENT
)
808 pcmcia_release_configuration(link
->handle
);
809 pcmcia_release_io(link
->handle
, &link
->io
);
810 pcmcia_release_irq(link
->handle
, &link
->irq
);
812 link
->state
&= ~DEV_CONFIG
;
816 static int btuart_event(event_t event
, int priority
, event_callback_args_t
*args
)
818 dev_link_t
*link
= args
->client_data
;
819 btuart_info_t
*info
= link
->priv
;
822 case CS_EVENT_CARD_REMOVAL
:
823 link
->state
&= ~DEV_PRESENT
;
824 if (link
->state
& DEV_CONFIG
) {
826 btuart_release(link
);
829 case CS_EVENT_CARD_INSERTION
:
830 link
->state
|= DEV_PRESENT
| DEV_CONFIG_PENDING
;
833 case CS_EVENT_PM_SUSPEND
:
834 link
->state
|= DEV_SUSPEND
;
835 /* Fall through... */
836 case CS_EVENT_RESET_PHYSICAL
:
837 if (link
->state
& DEV_CONFIG
)
838 pcmcia_release_configuration(link
->handle
);
840 case CS_EVENT_PM_RESUME
:
841 link
->state
&= ~DEV_SUSPEND
;
842 /* Fall through... */
843 case CS_EVENT_CARD_RESET
:
845 pcmcia_request_configuration(link
->handle
, &link
->conf
);
852 static struct pcmcia_device_id btuart_ids
[] = {
853 /* don't use this driver. Use serial_cs + hci_uart instead */
856 MODULE_DEVICE_TABLE(pcmcia
, btuart_ids
);
858 static struct pcmcia_driver btuart_driver
= {
859 .owner
= THIS_MODULE
,
863 .attach
= btuart_attach
,
864 .event
= btuart_event
,
865 .detach
= btuart_detach
,
866 .id_table
= btuart_ids
,
869 static int __init
init_btuart_cs(void)
871 return pcmcia_register_driver(&btuart_driver
);
875 static void __exit
exit_btuart_cs(void)
877 pcmcia_unregister_driver(&btuart_driver
);
878 BUG_ON(dev_list
!= NULL
);
881 module_init(init_btuart_cs
);
882 module_exit(exit_btuart_cs
);