3 * A driver for Nokia Connectivity Card DTL-1 devices
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 Nokia Connectivity Card DTL-1");
62 MODULE_LICENSE("GPL");
66 /* ======================== Local structures ======================== */
69 typedef struct dtl1_info_t {
70 struct pcmcia_device *p_dev;
75 spinlock_t lock; /* For serializing operations */
77 unsigned long flowmask; /* HCI flow mask */
80 struct sk_buff_head txq;
81 unsigned long tx_state;
83 unsigned long rx_state;
84 unsigned long rx_count;
85 struct sk_buff *rx_skb;
89 static int dtl1_config(struct pcmcia_device *link);
90 static void dtl1_release(struct pcmcia_device *link);
92 static void dtl1_detach(struct pcmcia_device *p_dev);
96 #define XMIT_SENDING 1
98 #define XMIT_WAITING 8
100 /* Receiver States */
101 #define RECV_WAIT_NSH 0
102 #define RECV_WAIT_DATA 1
109 } __attribute__ ((packed)) nsh_t; /* Nokia Specific Header */
111 #define NSHL 4 /* Nokia Specific Header Length */
115 /* ======================== Interrupt handling ======================== */
118 static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
122 /* Tx FIFO should be empty */
123 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
126 /* Fill FIFO with current frame */
127 while ((fifo_size-- > 0) && (actual < len)) {
128 /* Transmit next byte */
129 outb(buf[actual], iobase + UART_TX);
137 static void dtl1_write_wakeup(dtl1_info_t *info)
140 BT_ERR("Unknown device");
144 if (test_bit(XMIT_WAITING, &(info->tx_state))) {
145 set_bit(XMIT_WAKEUP, &(info->tx_state));
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->p_dev->io.BasePort1;
156 register struct sk_buff *skb;
159 clear_bit(XMIT_WAKEUP, &(info->tx_state));
161 if (!pcmcia_dev_present(info->p_dev))
164 if (!(skb = skb_dequeue(&(info->txq))))
168 len = dtl1_write(iobase, 32, skb->data, skb->len);
170 if (len == skb->len) {
171 set_bit(XMIT_WAITING, &(info->tx_state));
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 dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
188 u8 flowmask = *(u8 *)skb->data;
191 printk(KERN_INFO "Bluetooth: Nokia control data =");
192 for (i = 0; i < skb->len; i++) {
193 printk(" %02x", skb->data[i]);
197 /* transition to active state */
198 if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
199 clear_bit(XMIT_WAITING, &(info->tx_state));
200 dtl1_write_wakeup(info);
203 info->flowmask = flowmask;
209 static void dtl1_receive(dtl1_info_t *info)
216 BT_ERR("Unknown device");
220 iobase = info->p_dev->io.BasePort1;
223 info->hdev->stat.byte_rx++;
225 /* Allocate packet */
226 if (info->rx_skb == NULL)
227 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
228 BT_ERR("Can't allocate mem for new packet");
229 info->rx_state = RECV_WAIT_NSH;
230 info->rx_count = NSHL;
234 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
235 nsh = (nsh_t *)info->rx_skb->data;
239 if (info->rx_count == 0) {
241 switch (info->rx_state) {
243 info->rx_state = RECV_WAIT_DATA;
244 info->rx_count = nsh->len + (nsh->len & 0x0001);
247 bt_cb(info->rx_skb)->pkt_type = nsh->type;
249 /* remove PAD byte if it exists */
250 if (nsh->len & 0x0001) {
251 info->rx_skb->tail--;
256 skb_pull(info->rx_skb, NSHL);
258 switch (bt_cb(info->rx_skb)->pkt_type) {
260 /* control data for the Nokia Card */
261 dtl1_control(info, info->rx_skb);
266 /* send frame to the HCI layer */
267 info->rx_skb->dev = (void *) info->hdev;
268 bt_cb(info->rx_skb)->pkt_type &= 0x0f;
269 hci_recv_frame(info->rx_skb);
273 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
274 kfree_skb(info->rx_skb);
278 info->rx_state = RECV_WAIT_NSH;
279 info->rx_count = NSHL;
286 /* Make sure we don't stay here too long */
287 if (boguscount++ > 32)
290 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
294 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst)
296 dtl1_info_t *info = dev_inst;
302 if (!info || !info->hdev) {
303 BT_ERR("Call of irq %d for unknown device", irq);
307 iobase = info->p_dev->io.BasePort1;
309 spin_lock(&(info->lock));
311 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
314 /* Clear interrupt */
315 lsr = inb(iobase + UART_LSR);
322 /* Receive interrupt */
326 if (lsr & UART_LSR_THRE) {
327 /* Transmitter ready for data */
328 dtl1_write_wakeup(info);
332 BT_ERR("Unhandled IIR=%#x", iir);
336 /* Make sure we don't stay here too long */
337 if (boguscount++ > 100)
340 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
344 msr = inb(iobase + UART_MSR);
346 if (info->ri_latch ^ (msr & UART_MSR_RI)) {
347 info->ri_latch = msr & UART_MSR_RI;
348 clear_bit(XMIT_WAITING, &(info->tx_state));
349 dtl1_write_wakeup(info);
352 spin_unlock(&(info->lock));
359 /* ======================== HCI interface ======================== */
362 static int dtl1_hci_open(struct hci_dev *hdev)
364 set_bit(HCI_RUNNING, &(hdev->flags));
370 static int dtl1_hci_flush(struct hci_dev *hdev)
372 dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
375 skb_queue_purge(&(info->txq));
381 static int dtl1_hci_close(struct hci_dev *hdev)
383 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
386 dtl1_hci_flush(hdev);
392 static int dtl1_hci_send_frame(struct sk_buff *skb)
395 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
400 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
404 info = (dtl1_info_t *)(hdev->driver_data);
406 switch (bt_cb(skb)->pkt_type) {
407 case HCI_COMMAND_PKT:
411 case HCI_ACLDATA_PKT:
415 case HCI_SCODATA_PKT:
424 s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
428 skb_reserve(s, NSHL);
429 memcpy(skb_put(s, skb->len), skb->data, skb->len);
430 if (skb->len & 0x0001)
431 *skb_put(s, 1) = 0; /* PAD */
433 /* Prepend skb with Nokia frame header and queue */
434 memcpy(skb_push(s, NSHL), &nsh, NSHL);
435 skb_queue_tail(&(info->txq), s);
437 dtl1_write_wakeup(info);
445 static void dtl1_hci_destruct(struct hci_dev *hdev)
450 static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
457 /* ======================== Card services HCI interaction ======================== */
460 static int dtl1_open(dtl1_info_t *info)
463 unsigned int iobase = info->p_dev->io.BasePort1;
464 struct hci_dev *hdev;
466 spin_lock_init(&(info->lock));
468 skb_queue_head_init(&(info->txq));
470 info->rx_state = RECV_WAIT_NSH;
471 info->rx_count = NSHL;
474 set_bit(XMIT_WAITING, &(info->tx_state));
476 /* Initialize HCI device */
477 hdev = hci_alloc_dev();
479 BT_ERR("Can't allocate HCI device");
485 hdev->type = HCI_PCCARD;
486 hdev->driver_data = info;
487 SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
489 hdev->open = dtl1_hci_open;
490 hdev->close = dtl1_hci_close;
491 hdev->flush = dtl1_hci_flush;
492 hdev->send = dtl1_hci_send_frame;
493 hdev->destruct = dtl1_hci_destruct;
494 hdev->ioctl = dtl1_hci_ioctl;
496 hdev->owner = THIS_MODULE;
498 spin_lock_irqsave(&(info->lock), flags);
501 outb(0, iobase + UART_MCR);
503 /* Turn off interrupts */
504 outb(0, iobase + UART_IER);
506 /* Initialize UART */
507 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
508 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
510 info->ri_latch = inb(info->p_dev->io.BasePort1 + UART_MSR) & UART_MSR_RI;
512 /* Turn on interrupts */
513 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
515 spin_unlock_irqrestore(&(info->lock), flags);
517 /* Timeout before it is safe to send the first HCI packet */
520 /* Register HCI device */
521 if (hci_register_dev(hdev) < 0) {
522 BT_ERR("Can't register HCI device");
532 static int dtl1_close(dtl1_info_t *info)
535 unsigned int iobase = info->p_dev->io.BasePort1;
536 struct hci_dev *hdev = info->hdev;
541 dtl1_hci_close(hdev);
543 spin_lock_irqsave(&(info->lock), flags);
546 outb(0, iobase + UART_MCR);
548 /* Turn off interrupts */
549 outb(0, iobase + UART_IER);
551 spin_unlock_irqrestore(&(info->lock), flags);
553 if (hci_unregister_dev(hdev) < 0)
554 BT_ERR("Can't unregister HCI device %s", hdev->name);
561 static int dtl1_probe(struct pcmcia_device *link)
565 /* Create new info device */
566 info = kzalloc(sizeof(*info), GFP_KERNEL);
573 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
574 link->io.NumPorts1 = 8;
575 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
576 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
578 link->irq.Handler = dtl1_interrupt;
579 link->irq.Instance = info;
581 link->conf.Attributes = CONF_ENABLE_IRQ;
582 link->conf.IntType = INT_MEMORY_AND_IO;
584 return dtl1_config(link);
588 static void dtl1_detach(struct pcmcia_device *link)
590 dtl1_info_t *info = link->priv;
597 static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
601 i = pcmcia_get_tuple_data(handle, tuple);
605 return pcmcia_parse_tuple(handle, tuple, parse);
608 static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
610 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
611 return CS_NO_MORE_ITEMS;
612 return get_tuple(handle, tuple, parse);
615 static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
617 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
618 return CS_NO_MORE_ITEMS;
619 return get_tuple(handle, tuple, parse);
622 static int dtl1_config(struct pcmcia_device *link)
624 dtl1_info_t *info = link->priv;
628 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
629 int i, last_ret, last_fn;
631 tuple.TupleData = (cisdata_t *)buf;
632 tuple.TupleOffset = 0;
633 tuple.TupleDataMax = 255;
634 tuple.Attributes = 0;
636 /* Get configuration register information */
637 tuple.DesiredTuple = CISTPL_CONFIG;
638 last_ret = first_tuple(link, &tuple, &parse);
639 if (last_ret != CS_SUCCESS) {
640 last_fn = ParseTuple;
643 link->conf.ConfigBase = parse.config.base;
644 link->conf.Present = parse.config.rmask[0];
646 tuple.TupleData = (cisdata_t *)buf;
647 tuple.TupleOffset = 0;
648 tuple.TupleDataMax = 255;
649 tuple.Attributes = 0;
650 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
652 /* Look for a generic full-sized window */
653 link->io.NumPorts1 = 8;
654 i = first_tuple(link, &tuple, &parse);
655 while (i != CS_NO_MORE_ITEMS) {
656 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
657 link->conf.ConfigIndex = cf->index;
658 link->io.BasePort1 = cf->io.win[0].base;
659 link->io.NumPorts1 = cf->io.win[0].len; /*yo */
660 link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
661 i = pcmcia_request_io(link, &link->io);
665 i = next_tuple(link, &tuple, &parse);
668 if (i != CS_SUCCESS) {
669 cs_error(link, RequestIO, i);
673 i = pcmcia_request_irq(link, &link->irq);
674 if (i != CS_SUCCESS) {
675 cs_error(link, RequestIRQ, i);
676 link->irq.AssignedIRQ = 0;
679 i = pcmcia_request_configuration(link, &link->conf);
680 if (i != CS_SUCCESS) {
681 cs_error(link, RequestConfiguration, i);
685 if (dtl1_open(info) != 0)
688 strcpy(info->node.dev_name, info->hdev->name);
689 link->dev_node = &info->node;
694 cs_error(link, last_fn, last_ret);
702 static void dtl1_release(struct pcmcia_device *link)
704 dtl1_info_t *info = link->priv;
708 pcmcia_disable_device(link);
712 static struct pcmcia_device_id dtl1_ids[] = {
713 PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
714 PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-4", 0xe1bfdd64, 0x9102bc82),
715 PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
716 PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
719 MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
721 static struct pcmcia_driver dtl1_driver = {
722 .owner = THIS_MODULE,
727 .remove = dtl1_detach,
728 .id_table = dtl1_ids,
731 static int __init init_dtl1_cs(void)
733 return pcmcia_register_driver(&dtl1_driver);
737 static void __exit exit_dtl1_cs(void)
739 pcmcia_unregister_driver(&dtl1_driver);
742 module_init(init_dtl1_cs);
743 module_exit(exit_dtl1_cs);