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/version.h>
47 #include <pcmcia/cs_types.h>
48 #include <pcmcia/cs.h>
49 #include <pcmcia/cistpl.h>
50 #include <pcmcia/ciscode.h>
51 #include <pcmcia/ds.h>
52 #include <pcmcia/cisreg.h>
54 #include <net/bluetooth/bluetooth.h>
55 #include <net/bluetooth/hci_core.h>
59 /* ======================== Module parameters ======================== */
62 /* Bit map of interrupts to choose from */
63 static unsigned int irq_mask = 0xffff;
64 static int irq_list[4] = { -1 };
66 module_param(irq_mask, uint, 0);
67 module_param_array(irq_list, int, NULL, 0);
69 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
70 MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
71 MODULE_LICENSE("GPL");
75 /* ======================== Local structures ======================== */
78 typedef struct dtl1_info_t {
84 spinlock_t lock; /* For serializing operations */
86 unsigned long flowmask; /* HCI flow mask */
89 struct sk_buff_head txq;
90 unsigned long tx_state;
92 unsigned long rx_state;
93 unsigned long rx_count;
94 struct sk_buff *rx_skb;
98 static void dtl1_config(dev_link_t *link);
99 static void dtl1_release(dev_link_t *link);
100 static int dtl1_event(event_t event, int priority, event_callback_args_t *args);
102 static dev_info_t dev_info = "dtl1_cs";
104 static dev_link_t *dtl1_attach(void);
105 static void dtl1_detach(dev_link_t *);
107 static dev_link_t *dev_list = NULL;
110 /* Transmit states */
111 #define XMIT_SENDING 1
112 #define XMIT_WAKEUP 2
113 #define XMIT_WAITING 8
115 /* Receiver States */
116 #define RECV_WAIT_NSH 0
117 #define RECV_WAIT_DATA 1
124 } __attribute__ ((packed)) nsh_t; /* Nokia Specific Header */
126 #define NSHL 4 /* Nokia Specific Header Length */
130 /* ======================== Interrupt handling ======================== */
133 static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
137 /* Tx FIFO should be empty */
138 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
141 /* Fill FIFO with current frame */
142 while ((fifo_size-- > 0) && (actual < len)) {
143 /* Transmit next byte */
144 outb(buf[actual], iobase + UART_TX);
152 static void dtl1_write_wakeup(dtl1_info_t *info)
155 BT_ERR("Unknown device");
159 if (test_bit(XMIT_WAITING, &(info->tx_state))) {
160 set_bit(XMIT_WAKEUP, &(info->tx_state));
164 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
165 set_bit(XMIT_WAKEUP, &(info->tx_state));
170 register unsigned int iobase = info->link.io.BasePort1;
171 register struct sk_buff *skb;
174 clear_bit(XMIT_WAKEUP, &(info->tx_state));
176 if (!(info->link.state & DEV_PRESENT))
179 if (!(skb = skb_dequeue(&(info->txq))))
183 len = dtl1_write(iobase, 32, skb->data, skb->len);
185 if (len == skb->len) {
186 set_bit(XMIT_WAITING, &(info->tx_state));
190 skb_queue_head(&(info->txq), skb);
193 info->hdev->stat.byte_tx += len;
195 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
197 clear_bit(XMIT_SENDING, &(info->tx_state));
201 static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
203 u8 flowmask = *(u8 *)skb->data;
206 printk(KERN_INFO "Bluetooth: Nokia control data =");
207 for (i = 0; i < skb->len; i++) {
208 printk(" %02x", skb->data[i]);
212 /* transition to active state */
213 if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
214 clear_bit(XMIT_WAITING, &(info->tx_state));
215 dtl1_write_wakeup(info);
218 info->flowmask = flowmask;
224 static void dtl1_receive(dtl1_info_t *info)
231 BT_ERR("Unknown device");
235 iobase = info->link.io.BasePort1;
238 info->hdev->stat.byte_rx++;
240 /* Allocate packet */
241 if (info->rx_skb == NULL)
242 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
243 BT_ERR("Can't allocate mem for new packet");
244 info->rx_state = RECV_WAIT_NSH;
245 info->rx_count = NSHL;
249 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
250 nsh = (nsh_t *)info->rx_skb->data;
254 if (info->rx_count == 0) {
256 switch (info->rx_state) {
258 info->rx_state = RECV_WAIT_DATA;
259 info->rx_count = nsh->len + (nsh->len & 0x0001);
262 info->rx_skb->pkt_type = nsh->type;
264 /* remove PAD byte if it exists */
265 if (nsh->len & 0x0001) {
266 info->rx_skb->tail--;
271 skb_pull(info->rx_skb, NSHL);
273 switch (info->rx_skb->pkt_type) {
275 /* control data for the Nokia Card */
276 dtl1_control(info, info->rx_skb);
281 /* send frame to the HCI layer */
282 info->rx_skb->dev = (void *) info->hdev;
283 info->rx_skb->pkt_type &= 0x0f;
284 hci_recv_frame(info->rx_skb);
288 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
289 kfree_skb(info->rx_skb);
293 info->rx_state = RECV_WAIT_NSH;
294 info->rx_count = NSHL;
301 /* Make sure we don't stay here too long */
302 if (boguscount++ > 32)
305 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
309 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
311 dtl1_info_t *info = dev_inst;
317 if (!info || !info->hdev) {
318 BT_ERR("Call of irq %d for unknown device", irq);
322 iobase = info->link.io.BasePort1;
324 spin_lock(&(info->lock));
326 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
329 /* Clear interrupt */
330 lsr = inb(iobase + UART_LSR);
337 /* Receive interrupt */
341 if (lsr & UART_LSR_THRE) {
342 /* Transmitter ready for data */
343 dtl1_write_wakeup(info);
347 BT_ERR("Unhandled IIR=%#x", iir);
351 /* Make sure we don't stay here too long */
352 if (boguscount++ > 100)
355 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
359 msr = inb(iobase + UART_MSR);
361 if (info->ri_latch ^ (msr & UART_MSR_RI)) {
362 info->ri_latch = msr & UART_MSR_RI;
363 clear_bit(XMIT_WAITING, &(info->tx_state));
364 dtl1_write_wakeup(info);
367 spin_unlock(&(info->lock));
374 /* ======================== HCI interface ======================== */
377 static int dtl1_hci_open(struct hci_dev *hdev)
379 set_bit(HCI_RUNNING, &(hdev->flags));
385 static int dtl1_hci_flush(struct hci_dev *hdev)
387 dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
390 skb_queue_purge(&(info->txq));
396 static int dtl1_hci_close(struct hci_dev *hdev)
398 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
401 dtl1_hci_flush(hdev);
407 static int dtl1_hci_send_frame(struct sk_buff *skb)
410 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
415 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
419 info = (dtl1_info_t *)(hdev->driver_data);
421 switch (skb->pkt_type) {
422 case HCI_COMMAND_PKT:
426 case HCI_ACLDATA_PKT:
430 case HCI_SCODATA_PKT:
439 s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
440 skb_reserve(s, NSHL);
441 memcpy(skb_put(s, skb->len), skb->data, skb->len);
442 if (skb->len & 0x0001)
443 *skb_put(s, 1) = 0; /* PAD */
445 /* Prepend skb with Nokia frame header and queue */
446 memcpy(skb_push(s, NSHL), &nsh, NSHL);
447 skb_queue_tail(&(info->txq), s);
449 dtl1_write_wakeup(info);
457 static void dtl1_hci_destruct(struct hci_dev *hdev)
462 static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
469 /* ======================== Card services HCI interaction ======================== */
472 static int dtl1_open(dtl1_info_t *info)
475 unsigned int iobase = info->link.io.BasePort1;
476 struct hci_dev *hdev;
478 spin_lock_init(&(info->lock));
480 skb_queue_head_init(&(info->txq));
482 info->rx_state = RECV_WAIT_NSH;
483 info->rx_count = NSHL;
486 set_bit(XMIT_WAITING, &(info->tx_state));
488 /* Initialize HCI device */
489 hdev = hci_alloc_dev();
491 BT_ERR("Can't allocate HCI device");
497 hdev->type = HCI_PCCARD;
498 hdev->driver_data = info;
500 hdev->open = dtl1_hci_open;
501 hdev->close = dtl1_hci_close;
502 hdev->flush = dtl1_hci_flush;
503 hdev->send = dtl1_hci_send_frame;
504 hdev->destruct = dtl1_hci_destruct;
505 hdev->ioctl = dtl1_hci_ioctl;
507 hdev->owner = THIS_MODULE;
509 spin_lock_irqsave(&(info->lock), flags);
512 outb(0, iobase + UART_MCR);
514 /* Turn off interrupts */
515 outb(0, iobase + UART_IER);
517 /* Initialize UART */
518 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
519 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
521 info->ri_latch = inb(info->link.io.BasePort1 + UART_MSR) & UART_MSR_RI;
523 /* Turn on interrupts */
524 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
526 spin_unlock_irqrestore(&(info->lock), flags);
528 /* Timeout before it is safe to send the first HCI packet */
531 /* Register HCI device */
532 if (hci_register_dev(hdev) < 0) {
533 BT_ERR("Can't register HCI device");
543 static int dtl1_close(dtl1_info_t *info)
546 unsigned int iobase = info->link.io.BasePort1;
547 struct hci_dev *hdev = info->hdev;
552 dtl1_hci_close(hdev);
554 spin_lock_irqsave(&(info->lock), flags);
557 outb(0, iobase + UART_MCR);
559 /* Turn off interrupts */
560 outb(0, iobase + UART_IER);
562 spin_unlock_irqrestore(&(info->lock), flags);
564 if (hci_unregister_dev(hdev) < 0)
565 BT_ERR("Can't unregister HCI device %s", hdev->name);
572 static dev_link_t *dtl1_attach(void)
575 client_reg_t client_reg;
579 /* Create new info device */
580 info = kmalloc(sizeof(*info), GFP_KERNEL);
583 memset(info, 0, sizeof(*info));
588 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
589 link->io.NumPorts1 = 8;
590 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
591 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
593 if (irq_list[0] == -1)
594 link->irq.IRQInfo2 = irq_mask;
596 for (i = 0; i < 4; i++)
597 link->irq.IRQInfo2 |= 1 << irq_list[i];
599 link->irq.Handler = dtl1_interrupt;
600 link->irq.Instance = info;
602 link->conf.Attributes = CONF_ENABLE_IRQ;
604 link->conf.IntType = INT_MEMORY_AND_IO;
606 /* Register with Card Services */
607 link->next = dev_list;
609 client_reg.dev_info = &dev_info;
610 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
611 client_reg.EventMask =
612 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
613 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
614 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
615 client_reg.event_handler = &dtl1_event;
616 client_reg.Version = 0x0210;
617 client_reg.event_callback_args.client_data = link;
619 ret = pcmcia_register_client(&link->handle, &client_reg);
620 if (ret != CS_SUCCESS) {
621 cs_error(link->handle, RegisterClient, ret);
630 static void dtl1_detach(dev_link_t *link)
632 dtl1_info_t *info = link->priv;
636 /* Locate device structure */
637 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
644 if (link->state & DEV_CONFIG)
648 ret = pcmcia_deregister_client(link->handle);
649 if (ret != CS_SUCCESS)
650 cs_error(link->handle, DeregisterClient, ret);
653 /* Unlink device structure, free bits */
659 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
663 i = pcmcia_get_tuple_data(handle, tuple);
667 return pcmcia_parse_tuple(handle, tuple, parse);
670 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
672 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
673 return CS_NO_MORE_ITEMS;
674 return get_tuple(handle, tuple, parse);
677 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
679 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
680 return CS_NO_MORE_ITEMS;
681 return get_tuple(handle, tuple, parse);
684 static void dtl1_config(dev_link_t *link)
686 client_handle_t handle = link->handle;
687 dtl1_info_t *info = link->priv;
691 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
692 config_info_t config;
693 int i, last_ret, last_fn;
695 tuple.TupleData = (cisdata_t *)buf;
696 tuple.TupleOffset = 0;
697 tuple.TupleDataMax = 255;
698 tuple.Attributes = 0;
700 /* Get configuration register information */
701 tuple.DesiredTuple = CISTPL_CONFIG;
702 last_ret = first_tuple(handle, &tuple, &parse);
703 if (last_ret != CS_SUCCESS) {
704 last_fn = ParseTuple;
707 link->conf.ConfigBase = parse.config.base;
708 link->conf.Present = parse.config.rmask[0];
711 link->state |= DEV_CONFIG;
712 i = pcmcia_get_configuration_info(handle, &config);
713 link->conf.Vcc = config.Vcc;
715 tuple.TupleData = (cisdata_t *)buf;
716 tuple.TupleOffset = 0;
717 tuple.TupleDataMax = 255;
718 tuple.Attributes = 0;
719 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
721 /* Look for a generic full-sized window */
722 link->io.NumPorts1 = 8;
723 i = first_tuple(handle, &tuple, &parse);
724 while (i != CS_NO_MORE_ITEMS) {
725 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
726 link->conf.ConfigIndex = cf->index;
727 link->io.BasePort1 = cf->io.win[0].base;
728 link->io.NumPorts1 = cf->io.win[0].len; /*yo */
729 link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
730 i = pcmcia_request_io(link->handle, &link->io);
734 i = next_tuple(handle, &tuple, &parse);
737 if (i != CS_SUCCESS) {
738 cs_error(link->handle, RequestIO, i);
742 i = pcmcia_request_irq(link->handle, &link->irq);
743 if (i != CS_SUCCESS) {
744 cs_error(link->handle, RequestIRQ, i);
745 link->irq.AssignedIRQ = 0;
748 i = pcmcia_request_configuration(link->handle, &link->conf);
749 if (i != CS_SUCCESS) {
750 cs_error(link->handle, RequestConfiguration, i);
754 if (dtl1_open(info) != 0)
757 strcpy(info->node.dev_name, info->hdev->name);
758 link->dev = &info->node;
759 link->state &= ~DEV_CONFIG_PENDING;
764 cs_error(link->handle, last_fn, last_ret);
771 static void dtl1_release(dev_link_t *link)
773 dtl1_info_t *info = link->priv;
775 if (link->state & DEV_PRESENT)
780 pcmcia_release_configuration(link->handle);
781 pcmcia_release_io(link->handle, &link->io);
782 pcmcia_release_irq(link->handle, &link->irq);
784 link->state &= ~DEV_CONFIG;
788 static int dtl1_event(event_t event, int priority, event_callback_args_t *args)
790 dev_link_t *link = args->client_data;
791 dtl1_info_t *info = link->priv;
794 case CS_EVENT_CARD_REMOVAL:
795 link->state &= ~DEV_PRESENT;
796 if (link->state & DEV_CONFIG) {
801 case CS_EVENT_CARD_INSERTION:
802 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
805 case CS_EVENT_PM_SUSPEND:
806 link->state |= DEV_SUSPEND;
807 /* Fall through... */
808 case CS_EVENT_RESET_PHYSICAL:
809 if (link->state & DEV_CONFIG)
810 pcmcia_release_configuration(link->handle);
812 case CS_EVENT_PM_RESUME:
813 link->state &= ~DEV_SUSPEND;
814 /* Fall through... */
815 case CS_EVENT_CARD_RESET:
817 pcmcia_request_configuration(link->handle, &link->conf);
824 static struct pcmcia_driver dtl1_driver = {
825 .owner = THIS_MODULE,
829 .attach = dtl1_attach,
830 .detach = dtl1_detach,
833 static int __init init_dtl1_cs(void)
835 return pcmcia_register_driver(&dtl1_driver);
839 static void __exit exit_dtl1_cs(void)
841 pcmcia_unregister_driver(&dtl1_driver);
843 /* XXX: this really needs to move into generic code.. */
844 while (dev_list != NULL)
845 dtl1_detach(dev_list);
848 module_init(init_dtl1_cs);
849 module_exit(exit_dtl1_cs);