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/delay.h>
30 #include <linux/errno.h>
31 #include <linux/ptrace.h>
32 #include <linux/ioport.h>
33 #include <linux/spinlock.h>
34 #include <linux/moduleparam.h>
36 #include <linux/skbuff.h>
37 #include <linux/string.h>
38 #include <linux/serial.h>
39 #include <linux/serial_reg.h>
40 #include <linux/bitops.h>
41 #include <asm/system.h>
44 #include <pcmcia/cs_types.h>
45 #include <pcmcia/cs.h>
46 #include <pcmcia/cistpl.h>
47 #include <pcmcia/ciscode.h>
48 #include <pcmcia/ds.h>
49 #include <pcmcia/cisreg.h>
51 #include <net/bluetooth/bluetooth.h>
52 #include <net/bluetooth/hci_core.h>
56 /* ======================== Module parameters ======================== */
59 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
60 MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface");
61 MODULE_LICENSE("GPL");
65 /* ======================== Local structures ======================== */
68 typedef struct btuart_info_t
{
69 struct pcmcia_device
*p_dev
;
73 spinlock_t lock
; /* For serializing operations */
75 struct sk_buff_head txq
;
76 unsigned long tx_state
;
78 unsigned long rx_state
;
79 unsigned long rx_count
;
80 struct sk_buff
*rx_skb
;
84 static int btuart_config(struct pcmcia_device
*link
);
85 static void btuart_release(struct pcmcia_device
*link
);
87 static void btuart_detach(struct pcmcia_device
*p_dev
);
90 /* Maximum baud rate */
91 #define SPEED_MAX 115200
93 /* Default baud rate: 57600, 115200, 230400 or 460800 */
94 #define DEFAULT_BAUD_RATE 115200
98 #define XMIT_SENDING 1
100 #define XMIT_WAITING 8
102 /* Receiver states */
103 #define RECV_WAIT_PACKET_TYPE 0
104 #define RECV_WAIT_EVENT_HEADER 1
105 #define RECV_WAIT_ACL_HEADER 2
106 #define RECV_WAIT_SCO_HEADER 3
107 #define RECV_WAIT_DATA 4
111 /* ======================== Interrupt handling ======================== */
114 static int btuart_write(unsigned int iobase
, int fifo_size
, __u8
*buf
, int len
)
118 /* Tx FIFO should be empty */
119 if (!(inb(iobase
+ UART_LSR
) & UART_LSR_THRE
))
122 /* Fill FIFO with current frame */
123 while ((fifo_size
-- > 0) && (actual
< len
)) {
124 /* Transmit next byte */
125 outb(buf
[actual
], iobase
+ UART_TX
);
133 static void btuart_write_wakeup(btuart_info_t
*info
)
136 BT_ERR("Unknown device");
140 if (test_and_set_bit(XMIT_SENDING
, &(info
->tx_state
))) {
141 set_bit(XMIT_WAKEUP
, &(info
->tx_state
));
146 register unsigned int iobase
= info
->p_dev
->io
.BasePort1
;
147 register struct sk_buff
*skb
;
150 clear_bit(XMIT_WAKEUP
, &(info
->tx_state
));
152 if (!pcmcia_dev_present(info
->p_dev
))
155 if (!(skb
= skb_dequeue(&(info
->txq
))))
159 len
= btuart_write(iobase
, 16, skb
->data
, skb
->len
);
160 set_bit(XMIT_WAKEUP
, &(info
->tx_state
));
162 if (len
== skb
->len
) {
166 skb_queue_head(&(info
->txq
), skb
);
169 info
->hdev
->stat
.byte_tx
+= len
;
171 } while (test_bit(XMIT_WAKEUP
, &(info
->tx_state
)));
173 clear_bit(XMIT_SENDING
, &(info
->tx_state
));
177 static void btuart_receive(btuart_info_t
*info
)
183 BT_ERR("Unknown device");
187 iobase
= info
->p_dev
->io
.BasePort1
;
190 info
->hdev
->stat
.byte_rx
++;
192 /* Allocate packet */
193 if (info
->rx_skb
== NULL
) {
194 info
->rx_state
= RECV_WAIT_PACKET_TYPE
;
196 if (!(info
->rx_skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
))) {
197 BT_ERR("Can't allocate mem for new packet");
202 if (info
->rx_state
== RECV_WAIT_PACKET_TYPE
) {
204 info
->rx_skb
->dev
= (void *) info
->hdev
;
205 bt_cb(info
->rx_skb
)->pkt_type
= inb(iobase
+ UART_RX
);
207 switch (bt_cb(info
->rx_skb
)->pkt_type
) {
210 info
->rx_state
= RECV_WAIT_EVENT_HEADER
;
211 info
->rx_count
= HCI_EVENT_HDR_SIZE
;
214 case HCI_ACLDATA_PKT
:
215 info
->rx_state
= RECV_WAIT_ACL_HEADER
;
216 info
->rx_count
= HCI_ACL_HDR_SIZE
;
219 case HCI_SCODATA_PKT
:
220 info
->rx_state
= RECV_WAIT_SCO_HEADER
;
221 info
->rx_count
= HCI_SCO_HDR_SIZE
;
226 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info
->rx_skb
)->pkt_type
);
227 info
->hdev
->stat
.err_rx
++;
228 clear_bit(HCI_RUNNING
, &(info
->hdev
->flags
));
230 kfree_skb(info
->rx_skb
);
238 *skb_put(info
->rx_skb
, 1) = inb(iobase
+ UART_RX
);
241 if (info
->rx_count
== 0) {
244 struct hci_event_hdr
*eh
;
245 struct hci_acl_hdr
*ah
;
246 struct hci_sco_hdr
*sh
;
249 switch (info
->rx_state
) {
251 case RECV_WAIT_EVENT_HEADER
:
252 eh
= hci_event_hdr(info
->rx_skb
);
253 info
->rx_state
= RECV_WAIT_DATA
;
254 info
->rx_count
= eh
->plen
;
257 case RECV_WAIT_ACL_HEADER
:
258 ah
= hci_acl_hdr(info
->rx_skb
);
259 dlen
= __le16_to_cpu(ah
->dlen
);
260 info
->rx_state
= RECV_WAIT_DATA
;
261 info
->rx_count
= dlen
;
264 case RECV_WAIT_SCO_HEADER
:
265 sh
= hci_sco_hdr(info
->rx_skb
);
266 info
->rx_state
= RECV_WAIT_DATA
;
267 info
->rx_count
= sh
->dlen
;
271 hci_recv_frame(info
->rx_skb
);
281 /* Make sure we don't stay here too long */
282 if (boguscount
++ > 16)
285 } while (inb(iobase
+ UART_LSR
) & UART_LSR_DR
);
289 static irqreturn_t
btuart_interrupt(int irq
, void *dev_inst
)
291 btuart_info_t
*info
= dev_inst
;
295 irqreturn_t r
= IRQ_NONE
;
297 if (!info
|| !info
->hdev
)
298 /* our irq handler is shared */
301 iobase
= info
->p_dev
->io
.BasePort1
;
303 spin_lock(&(info
->lock
));
305 iir
= inb(iobase
+ UART_IIR
) & UART_IIR_ID
;
309 /* Clear interrupt */
310 lsr
= inb(iobase
+ UART_LSR
);
317 /* Receive interrupt */
318 btuart_receive(info
);
321 if (lsr
& UART_LSR_THRE
) {
322 /* Transmitter ready for data */
323 btuart_write_wakeup(info
);
327 BT_ERR("Unhandled IIR=%#x", iir
);
331 /* Make sure we don't stay here too long */
332 if (boguscount
++ > 100)
335 iir
= inb(iobase
+ UART_IIR
) & UART_IIR_ID
;
339 spin_unlock(&(info
->lock
));
345 static void btuart_change_speed(btuart_info_t
*info
, unsigned int speed
)
349 int fcr
; /* FIFO control reg */
350 int lcr
; /* Line control reg */
354 BT_ERR("Unknown device");
358 iobase
= info
->p_dev
->io
.BasePort1
;
360 spin_lock_irqsave(&(info
->lock
), flags
);
362 /* Turn off interrupts */
363 outb(0, iobase
+ UART_IER
);
365 divisor
= SPEED_MAX
/ speed
;
367 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
;
370 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
371 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
372 * about this timeout since it will always be fast enough.
376 fcr
|= UART_FCR_TRIGGER_1
;
378 fcr
|= UART_FCR_TRIGGER_14
;
380 /* Bluetooth cards use 8N1 */
381 lcr
= UART_LCR_WLEN8
;
383 outb(UART_LCR_DLAB
| lcr
, iobase
+ UART_LCR
); /* Set DLAB */
384 outb(divisor
& 0xff, iobase
+ UART_DLL
); /* Set speed */
385 outb(divisor
>> 8, iobase
+ UART_DLM
);
386 outb(lcr
, iobase
+ UART_LCR
); /* Set 8N1 */
387 outb(fcr
, iobase
+ UART_FCR
); /* Enable FIFO's */
389 /* Turn on interrupts */
390 outb(UART_IER_RLSI
| UART_IER_RDI
| UART_IER_THRI
, iobase
+ UART_IER
);
392 spin_unlock_irqrestore(&(info
->lock
), flags
);
397 /* ======================== HCI interface ======================== */
400 static int btuart_hci_flush(struct hci_dev
*hdev
)
402 btuart_info_t
*info
= (btuart_info_t
*)(hdev
->driver_data
);
405 skb_queue_purge(&(info
->txq
));
411 static int btuart_hci_open(struct hci_dev
*hdev
)
413 set_bit(HCI_RUNNING
, &(hdev
->flags
));
419 static int btuart_hci_close(struct hci_dev
*hdev
)
421 if (!test_and_clear_bit(HCI_RUNNING
, &(hdev
->flags
)))
424 btuart_hci_flush(hdev
);
430 static int btuart_hci_send_frame(struct sk_buff
*skb
)
433 struct hci_dev
*hdev
= (struct hci_dev
*)(skb
->dev
);
436 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
440 info
= (btuart_info_t
*)(hdev
->driver_data
);
442 switch (bt_cb(skb
)->pkt_type
) {
443 case HCI_COMMAND_PKT
:
446 case HCI_ACLDATA_PKT
:
449 case HCI_SCODATA_PKT
:
454 /* Prepend skb with frame type */
455 memcpy(skb_push(skb
, 1), &bt_cb(skb
)->pkt_type
, 1);
456 skb_queue_tail(&(info
->txq
), skb
);
458 btuart_write_wakeup(info
);
464 static void btuart_hci_destruct(struct hci_dev
*hdev
)
469 static int btuart_hci_ioctl(struct hci_dev
*hdev
, unsigned int cmd
, unsigned long arg
)
476 /* ======================== Card services HCI interaction ======================== */
479 static int btuart_open(btuart_info_t
*info
)
482 unsigned int iobase
= info
->p_dev
->io
.BasePort1
;
483 struct hci_dev
*hdev
;
485 spin_lock_init(&(info
->lock
));
487 skb_queue_head_init(&(info
->txq
));
489 info
->rx_state
= RECV_WAIT_PACKET_TYPE
;
493 /* Initialize HCI device */
494 hdev
= hci_alloc_dev();
496 BT_ERR("Can't allocate HCI device");
502 hdev
->bus
= HCI_PCCARD
;
503 hdev
->driver_data
= info
;
504 SET_HCIDEV_DEV(hdev
, &info
->p_dev
->dev
);
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;
593 link
->conf
.Attributes
= CONF_ENABLE_IRQ
;
594 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
596 return btuart_config(link
);
600 static void btuart_detach(struct pcmcia_device
*link
)
602 btuart_info_t
*info
= link
->priv
;
604 btuart_release(link
);
608 static int btuart_check_config(struct pcmcia_device
*p_dev
,
609 cistpl_cftable_entry_t
*cf
,
610 cistpl_cftable_entry_t
*dflt
,
614 int *try = priv_data
;
616 if (cf
->vpp1
.present
& (1 << CISTPL_POWER_VNOM
))
617 p_dev
->conf
.Vpp
= cf
->vpp1
.param
[CISTPL_POWER_VNOM
] / 10000;
618 if ((cf
->io
.nwin
> 0) && (cf
->io
.win
[0].len
== 8) &&
619 (cf
->io
.win
[0].base
!= 0)) {
620 p_dev
->io
.BasePort1
= cf
->io
.win
[0].base
;
621 p_dev
->io
.IOAddrLines
= (*try == 0) ? 16 :
622 cf
->io
.flags
& CISTPL_IO_LINES_MASK
;
623 if (!pcmcia_request_io(p_dev
, &p_dev
->io
))
629 static int btuart_check_config_notpicky(struct pcmcia_device
*p_dev
,
630 cistpl_cftable_entry_t
*cf
,
631 cistpl_cftable_entry_t
*dflt
,
635 static unsigned int base
[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
638 if ((cf
->io
.nwin
> 0) && ((cf
->io
.flags
& CISTPL_IO_LINES_MASK
) <= 3)) {
639 for (j
= 0; j
< 5; j
++) {
640 p_dev
->io
.BasePort1
= base
[j
];
641 p_dev
->io
.IOAddrLines
= base
[j
] ? 16 : 3;
642 if (!pcmcia_request_io(p_dev
, &p_dev
->io
))
649 static int btuart_config(struct pcmcia_device
*link
)
651 btuart_info_t
*info
= link
->priv
;
655 /* First pass: look for a config entry that looks normal.
656 Two tries: without IO aliases, then with aliases */
657 for (try = 0; try < 2; try++)
658 if (!pcmcia_loop_config(link
, btuart_check_config
, &try))
661 /* Second pass: try to find an entry that isn't picky about
662 its base address, then try to grab any standard serial port
663 address, and finally try to get any free port. */
664 if (!pcmcia_loop_config(link
, btuart_check_config_notpicky
, NULL
))
667 BT_ERR("No usable port range found");
671 i
= pcmcia_request_irq(link
, btuart_interrupt
);
675 i
= pcmcia_request_configuration(link
, &link
->conf
);
679 if (btuart_open(info
) != 0)
685 btuart_release(link
);
690 static void btuart_release(struct pcmcia_device
*link
)
692 btuart_info_t
*info
= link
->priv
;
696 pcmcia_disable_device(link
);
699 static struct pcmcia_device_id btuart_ids
[] = {
700 /* don't use this driver. Use serial_cs + hci_uart instead */
703 MODULE_DEVICE_TABLE(pcmcia
, btuart_ids
);
705 static struct pcmcia_driver btuart_driver
= {
706 .owner
= THIS_MODULE
,
710 .probe
= btuart_probe
,
711 .remove
= btuart_detach
,
712 .id_table
= btuart_ids
,
715 static int __init
init_btuart_cs(void)
717 return pcmcia_register_driver(&btuart_driver
);
721 static void __exit
exit_btuart_cs(void)
723 pcmcia_unregister_driver(&btuart_driver
);
726 module_init(init_btuart_cs
);
727 module_exit(exit_btuart_cs
);