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/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 Nokia Connectivity Card DTL-1");
63 MODULE_LICENSE("GPL");
67 /* ======================== Local structures ======================== */
70 typedef struct dtl1_info_t {
76 spinlock_t lock; /* For serializing operations */
78 unsigned long flowmask; /* HCI flow mask */
81 struct sk_buff_head txq;
82 unsigned long tx_state;
84 unsigned long rx_state;
85 unsigned long rx_count;
86 struct sk_buff *rx_skb;
90 static void dtl1_config(dev_link_t *link);
91 static void dtl1_release(dev_link_t *link);
93 static void dtl1_detach(struct pcmcia_device *p_dev);
97 #define XMIT_SENDING 1
99 #define XMIT_WAITING 8
101 /* Receiver States */
102 #define RECV_WAIT_NSH 0
103 #define RECV_WAIT_DATA 1
110 } __attribute__ ((packed)) nsh_t; /* Nokia Specific Header */
112 #define NSHL 4 /* Nokia Specific Header Length */
116 /* ======================== Interrupt handling ======================== */
119 static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
123 /* Tx FIFO should be empty */
124 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
127 /* Fill FIFO with current frame */
128 while ((fifo_size-- > 0) && (actual < len)) {
129 /* Transmit next byte */
130 outb(buf[actual], iobase + UART_TX);
138 static void dtl1_write_wakeup(dtl1_info_t *info)
141 BT_ERR("Unknown device");
145 if (test_bit(XMIT_WAITING, &(info->tx_state))) {
146 set_bit(XMIT_WAKEUP, &(info->tx_state));
150 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
151 set_bit(XMIT_WAKEUP, &(info->tx_state));
156 register unsigned int iobase = info->link.io.BasePort1;
157 register struct sk_buff *skb;
160 clear_bit(XMIT_WAKEUP, &(info->tx_state));
162 if (!(info->link.state & DEV_PRESENT))
165 if (!(skb = skb_dequeue(&(info->txq))))
169 len = dtl1_write(iobase, 32, skb->data, skb->len);
171 if (len == skb->len) {
172 set_bit(XMIT_WAITING, &(info->tx_state));
176 skb_queue_head(&(info->txq), skb);
179 info->hdev->stat.byte_tx += len;
181 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
183 clear_bit(XMIT_SENDING, &(info->tx_state));
187 static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
189 u8 flowmask = *(u8 *)skb->data;
192 printk(KERN_INFO "Bluetooth: Nokia control data =");
193 for (i = 0; i < skb->len; i++) {
194 printk(" %02x", skb->data[i]);
198 /* transition to active state */
199 if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
200 clear_bit(XMIT_WAITING, &(info->tx_state));
201 dtl1_write_wakeup(info);
204 info->flowmask = flowmask;
210 static void dtl1_receive(dtl1_info_t *info)
217 BT_ERR("Unknown device");
221 iobase = info->link.io.BasePort1;
224 info->hdev->stat.byte_rx++;
226 /* Allocate packet */
227 if (info->rx_skb == NULL)
228 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
229 BT_ERR("Can't allocate mem for new packet");
230 info->rx_state = RECV_WAIT_NSH;
231 info->rx_count = NSHL;
235 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
236 nsh = (nsh_t *)info->rx_skb->data;
240 if (info->rx_count == 0) {
242 switch (info->rx_state) {
244 info->rx_state = RECV_WAIT_DATA;
245 info->rx_count = nsh->len + (nsh->len & 0x0001);
248 bt_cb(info->rx_skb)->pkt_type = nsh->type;
250 /* remove PAD byte if it exists */
251 if (nsh->len & 0x0001) {
252 info->rx_skb->tail--;
257 skb_pull(info->rx_skb, NSHL);
259 switch (bt_cb(info->rx_skb)->pkt_type) {
261 /* control data for the Nokia Card */
262 dtl1_control(info, info->rx_skb);
267 /* send frame to the HCI layer */
268 info->rx_skb->dev = (void *) info->hdev;
269 bt_cb(info->rx_skb)->pkt_type &= 0x0f;
270 hci_recv_frame(info->rx_skb);
274 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
275 kfree_skb(info->rx_skb);
279 info->rx_state = RECV_WAIT_NSH;
280 info->rx_count = NSHL;
287 /* Make sure we don't stay here too long */
288 if (boguscount++ > 32)
291 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
295 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
297 dtl1_info_t *info = dev_inst;
303 if (!info || !info->hdev) {
304 BT_ERR("Call of irq %d for unknown device", irq);
308 iobase = info->link.io.BasePort1;
310 spin_lock(&(info->lock));
312 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
315 /* Clear interrupt */
316 lsr = inb(iobase + UART_LSR);
323 /* Receive interrupt */
327 if (lsr & UART_LSR_THRE) {
328 /* Transmitter ready for data */
329 dtl1_write_wakeup(info);
333 BT_ERR("Unhandled IIR=%#x", iir);
337 /* Make sure we don't stay here too long */
338 if (boguscount++ > 100)
341 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
345 msr = inb(iobase + UART_MSR);
347 if (info->ri_latch ^ (msr & UART_MSR_RI)) {
348 info->ri_latch = msr & UART_MSR_RI;
349 clear_bit(XMIT_WAITING, &(info->tx_state));
350 dtl1_write_wakeup(info);
353 spin_unlock(&(info->lock));
360 /* ======================== HCI interface ======================== */
363 static int dtl1_hci_open(struct hci_dev *hdev)
365 set_bit(HCI_RUNNING, &(hdev->flags));
371 static int dtl1_hci_flush(struct hci_dev *hdev)
373 dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
376 skb_queue_purge(&(info->txq));
382 static int dtl1_hci_close(struct hci_dev *hdev)
384 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
387 dtl1_hci_flush(hdev);
393 static int dtl1_hci_send_frame(struct sk_buff *skb)
396 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
401 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
405 info = (dtl1_info_t *)(hdev->driver_data);
407 switch (bt_cb(skb)->pkt_type) {
408 case HCI_COMMAND_PKT:
412 case HCI_ACLDATA_PKT:
416 case HCI_SCODATA_PKT:
425 s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
426 skb_reserve(s, NSHL);
427 memcpy(skb_put(s, skb->len), skb->data, skb->len);
428 if (skb->len & 0x0001)
429 *skb_put(s, 1) = 0; /* PAD */
431 /* Prepend skb with Nokia frame header and queue */
432 memcpy(skb_push(s, NSHL), &nsh, NSHL);
433 skb_queue_tail(&(info->txq), s);
435 dtl1_write_wakeup(info);
443 static void dtl1_hci_destruct(struct hci_dev *hdev)
448 static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
455 /* ======================== Card services HCI interaction ======================== */
458 static int dtl1_open(dtl1_info_t *info)
461 unsigned int iobase = info->link.io.BasePort1;
462 struct hci_dev *hdev;
464 spin_lock_init(&(info->lock));
466 skb_queue_head_init(&(info->txq));
468 info->rx_state = RECV_WAIT_NSH;
469 info->rx_count = NSHL;
472 set_bit(XMIT_WAITING, &(info->tx_state));
474 /* Initialize HCI device */
475 hdev = hci_alloc_dev();
477 BT_ERR("Can't allocate HCI device");
483 hdev->type = HCI_PCCARD;
484 hdev->driver_data = info;
486 hdev->open = dtl1_hci_open;
487 hdev->close = dtl1_hci_close;
488 hdev->flush = dtl1_hci_flush;
489 hdev->send = dtl1_hci_send_frame;
490 hdev->destruct = dtl1_hci_destruct;
491 hdev->ioctl = dtl1_hci_ioctl;
493 hdev->owner = THIS_MODULE;
495 spin_lock_irqsave(&(info->lock), flags);
498 outb(0, iobase + UART_MCR);
500 /* Turn off interrupts */
501 outb(0, iobase + UART_IER);
503 /* Initialize UART */
504 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
505 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
507 info->ri_latch = inb(info->link.io.BasePort1 + UART_MSR) & UART_MSR_RI;
509 /* Turn on interrupts */
510 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
512 spin_unlock_irqrestore(&(info->lock), flags);
514 /* Timeout before it is safe to send the first HCI packet */
517 /* Register HCI device */
518 if (hci_register_dev(hdev) < 0) {
519 BT_ERR("Can't register HCI device");
529 static int dtl1_close(dtl1_info_t *info)
532 unsigned int iobase = info->link.io.BasePort1;
533 struct hci_dev *hdev = info->hdev;
538 dtl1_hci_close(hdev);
540 spin_lock_irqsave(&(info->lock), flags);
543 outb(0, iobase + UART_MCR);
545 /* Turn off interrupts */
546 outb(0, iobase + UART_IER);
548 spin_unlock_irqrestore(&(info->lock), flags);
550 if (hci_unregister_dev(hdev) < 0)
551 BT_ERR("Can't unregister HCI device %s", hdev->name);
558 static int dtl1_attach(struct pcmcia_device *p_dev)
563 /* Create new info device */
564 info = kzalloc(sizeof(*info), GFP_KERNEL);
571 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
572 link->io.NumPorts1 = 8;
573 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
574 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
576 link->irq.Handler = dtl1_interrupt;
577 link->irq.Instance = info;
579 link->conf.Attributes = CONF_ENABLE_IRQ;
581 link->conf.IntType = INT_MEMORY_AND_IO;
583 link->handle = p_dev;
584 p_dev->instance = link;
586 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
593 static void dtl1_detach(struct pcmcia_device *p_dev)
595 dev_link_t *link = dev_to_instance(p_dev);
596 dtl1_info_t *info = link->priv;
598 if (link->state & DEV_CONFIG)
604 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
608 i = pcmcia_get_tuple_data(handle, tuple);
612 return pcmcia_parse_tuple(handle, tuple, parse);
615 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
617 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
618 return CS_NO_MORE_ITEMS;
619 return get_tuple(handle, tuple, parse);
622 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
624 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
625 return CS_NO_MORE_ITEMS;
626 return get_tuple(handle, tuple, parse);
629 static void dtl1_config(dev_link_t *link)
631 client_handle_t handle = link->handle;
632 dtl1_info_t *info = link->priv;
636 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
637 config_info_t config;
638 int i, last_ret, last_fn;
640 tuple.TupleData = (cisdata_t *)buf;
641 tuple.TupleOffset = 0;
642 tuple.TupleDataMax = 255;
643 tuple.Attributes = 0;
645 /* Get configuration register information */
646 tuple.DesiredTuple = CISTPL_CONFIG;
647 last_ret = first_tuple(handle, &tuple, &parse);
648 if (last_ret != CS_SUCCESS) {
649 last_fn = ParseTuple;
652 link->conf.ConfigBase = parse.config.base;
653 link->conf.Present = parse.config.rmask[0];
656 link->state |= DEV_CONFIG;
657 i = pcmcia_get_configuration_info(handle, &config);
658 link->conf.Vcc = config.Vcc;
660 tuple.TupleData = (cisdata_t *)buf;
661 tuple.TupleOffset = 0;
662 tuple.TupleDataMax = 255;
663 tuple.Attributes = 0;
664 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
666 /* Look for a generic full-sized window */
667 link->io.NumPorts1 = 8;
668 i = first_tuple(handle, &tuple, &parse);
669 while (i != CS_NO_MORE_ITEMS) {
670 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
671 link->conf.ConfigIndex = cf->index;
672 link->io.BasePort1 = cf->io.win[0].base;
673 link->io.NumPorts1 = cf->io.win[0].len; /*yo */
674 link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
675 i = pcmcia_request_io(link->handle, &link->io);
679 i = next_tuple(handle, &tuple, &parse);
682 if (i != CS_SUCCESS) {
683 cs_error(link->handle, RequestIO, i);
687 i = pcmcia_request_irq(link->handle, &link->irq);
688 if (i != CS_SUCCESS) {
689 cs_error(link->handle, RequestIRQ, i);
690 link->irq.AssignedIRQ = 0;
693 i = pcmcia_request_configuration(link->handle, &link->conf);
694 if (i != CS_SUCCESS) {
695 cs_error(link->handle, RequestConfiguration, i);
699 if (dtl1_open(info) != 0)
702 strcpy(info->node.dev_name, info->hdev->name);
703 link->dev = &info->node;
704 link->state &= ~DEV_CONFIG_PENDING;
709 cs_error(link->handle, last_fn, last_ret);
716 static void dtl1_release(dev_link_t *link)
718 dtl1_info_t *info = link->priv;
720 if (link->state & DEV_PRESENT)
725 pcmcia_release_configuration(link->handle);
726 pcmcia_release_io(link->handle, &link->io);
727 pcmcia_release_irq(link->handle, &link->irq);
729 link->state &= ~DEV_CONFIG;
732 static int dtl1_suspend(struct pcmcia_device *dev)
734 dev_link_t *link = dev_to_instance(dev);
736 link->state |= DEV_SUSPEND;
737 if (link->state & DEV_CONFIG)
738 pcmcia_release_configuration(link->handle);
743 static int dtl1_resume(struct pcmcia_device *dev)
745 dev_link_t *link = dev_to_instance(dev);
747 link->state &= ~DEV_SUSPEND;
749 pcmcia_request_configuration(link->handle, &link->conf);
755 static struct pcmcia_device_id dtl1_ids[] = {
756 PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
757 PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
758 PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
761 MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
763 static struct pcmcia_driver dtl1_driver = {
764 .owner = THIS_MODULE,
768 .probe = dtl1_attach,
769 .remove = dtl1_detach,
770 .id_table = dtl1_ids,
771 .suspend = dtl1_suspend,
772 .resume = dtl1_resume,
775 static int __init init_dtl1_cs(void)
777 return pcmcia_register_driver(&dtl1_driver);
781 static void __exit exit_dtl1_cs(void)
783 pcmcia_unregister_driver(&dtl1_driver);
786 module_init(init_dtl1_cs);
787 module_exit(exit_dtl1_cs);