[PATCH] pcmcia: remove dev_list from drivers
[linux-2.6/linux-2.6-openrd.git] / drivers / bluetooth / dtl1_cs.c
blobc39d4576cfffaf4ac05199d42febe3380777c6e1
1 /*
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>
44 #include <asm/io.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 {
71 dev_link_t link;
72 dev_node_t node;
74 struct hci_dev *hdev;
76 spinlock_t lock; /* For serializing operations */
78 unsigned long flowmask; /* HCI flow mask */
79 int ri_latch;
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;
87 } dtl1_info_t;
90 static void dtl1_config(dev_link_t *link);
91 static void dtl1_release(dev_link_t *link);
92 static int dtl1_event(event_t event, int priority, event_callback_args_t *args);
94 static dev_info_t dev_info = "dtl1_cs";
96 static dev_link_t *dtl1_attach(void);
97 static void dtl1_detach(struct pcmcia_device *p_dev);
100 /* Transmit states */
101 #define XMIT_SENDING 1
102 #define XMIT_WAKEUP 2
103 #define XMIT_WAITING 8
105 /* Receiver States */
106 #define RECV_WAIT_NSH 0
107 #define RECV_WAIT_DATA 1
110 typedef struct {
111 u8 type;
112 u8 zero;
113 u16 len;
114 } __attribute__ ((packed)) nsh_t; /* Nokia Specific Header */
116 #define NSHL 4 /* Nokia Specific Header Length */
120 /* ======================== Interrupt handling ======================== */
123 static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
125 int actual = 0;
127 /* Tx FIFO should be empty */
128 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
129 return 0;
131 /* Fill FIFO with current frame */
132 while ((fifo_size-- > 0) && (actual < len)) {
133 /* Transmit next byte */
134 outb(buf[actual], iobase + UART_TX);
135 actual++;
138 return actual;
142 static void dtl1_write_wakeup(dtl1_info_t *info)
144 if (!info) {
145 BT_ERR("Unknown device");
146 return;
149 if (test_bit(XMIT_WAITING, &(info->tx_state))) {
150 set_bit(XMIT_WAKEUP, &(info->tx_state));
151 return;
154 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
155 set_bit(XMIT_WAKEUP, &(info->tx_state));
156 return;
159 do {
160 register unsigned int iobase = info->link.io.BasePort1;
161 register struct sk_buff *skb;
162 register int len;
164 clear_bit(XMIT_WAKEUP, &(info->tx_state));
166 if (!(info->link.state & DEV_PRESENT))
167 return;
169 if (!(skb = skb_dequeue(&(info->txq))))
170 break;
172 /* Send frame */
173 len = dtl1_write(iobase, 32, skb->data, skb->len);
175 if (len == skb->len) {
176 set_bit(XMIT_WAITING, &(info->tx_state));
177 kfree_skb(skb);
178 } else {
179 skb_pull(skb, len);
180 skb_queue_head(&(info->txq), skb);
183 info->hdev->stat.byte_tx += len;
185 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
187 clear_bit(XMIT_SENDING, &(info->tx_state));
191 static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
193 u8 flowmask = *(u8 *)skb->data;
194 int i;
196 printk(KERN_INFO "Bluetooth: Nokia control data =");
197 for (i = 0; i < skb->len; i++) {
198 printk(" %02x", skb->data[i]);
200 printk("\n");
202 /* transition to active state */
203 if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
204 clear_bit(XMIT_WAITING, &(info->tx_state));
205 dtl1_write_wakeup(info);
208 info->flowmask = flowmask;
210 kfree_skb(skb);
214 static void dtl1_receive(dtl1_info_t *info)
216 unsigned int iobase;
217 nsh_t *nsh;
218 int boguscount = 0;
220 if (!info) {
221 BT_ERR("Unknown device");
222 return;
225 iobase = info->link.io.BasePort1;
227 do {
228 info->hdev->stat.byte_rx++;
230 /* Allocate packet */
231 if (info->rx_skb == NULL)
232 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
233 BT_ERR("Can't allocate mem for new packet");
234 info->rx_state = RECV_WAIT_NSH;
235 info->rx_count = NSHL;
236 return;
239 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
240 nsh = (nsh_t *)info->rx_skb->data;
242 info->rx_count--;
244 if (info->rx_count == 0) {
246 switch (info->rx_state) {
247 case RECV_WAIT_NSH:
248 info->rx_state = RECV_WAIT_DATA;
249 info->rx_count = nsh->len + (nsh->len & 0x0001);
250 break;
251 case RECV_WAIT_DATA:
252 bt_cb(info->rx_skb)->pkt_type = nsh->type;
254 /* remove PAD byte if it exists */
255 if (nsh->len & 0x0001) {
256 info->rx_skb->tail--;
257 info->rx_skb->len--;
260 /* remove NSH */
261 skb_pull(info->rx_skb, NSHL);
263 switch (bt_cb(info->rx_skb)->pkt_type) {
264 case 0x80:
265 /* control data for the Nokia Card */
266 dtl1_control(info, info->rx_skb);
267 break;
268 case 0x82:
269 case 0x83:
270 case 0x84:
271 /* send frame to the HCI layer */
272 info->rx_skb->dev = (void *) info->hdev;
273 bt_cb(info->rx_skb)->pkt_type &= 0x0f;
274 hci_recv_frame(info->rx_skb);
275 break;
276 default:
277 /* unknown packet */
278 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
279 kfree_skb(info->rx_skb);
280 break;
283 info->rx_state = RECV_WAIT_NSH;
284 info->rx_count = NSHL;
285 info->rx_skb = NULL;
286 break;
291 /* Make sure we don't stay here too long */
292 if (boguscount++ > 32)
293 break;
295 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
299 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
301 dtl1_info_t *info = dev_inst;
302 unsigned int iobase;
303 unsigned char msr;
304 int boguscount = 0;
305 int iir, lsr;
307 if (!info || !info->hdev) {
308 BT_ERR("Call of irq %d for unknown device", irq);
309 return IRQ_NONE;
312 iobase = info->link.io.BasePort1;
314 spin_lock(&(info->lock));
316 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
317 while (iir) {
319 /* Clear interrupt */
320 lsr = inb(iobase + UART_LSR);
322 switch (iir) {
323 case UART_IIR_RLSI:
324 BT_ERR("RLSI");
325 break;
326 case UART_IIR_RDI:
327 /* Receive interrupt */
328 dtl1_receive(info);
329 break;
330 case UART_IIR_THRI:
331 if (lsr & UART_LSR_THRE) {
332 /* Transmitter ready for data */
333 dtl1_write_wakeup(info);
335 break;
336 default:
337 BT_ERR("Unhandled IIR=%#x", iir);
338 break;
341 /* Make sure we don't stay here too long */
342 if (boguscount++ > 100)
343 break;
345 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
349 msr = inb(iobase + UART_MSR);
351 if (info->ri_latch ^ (msr & UART_MSR_RI)) {
352 info->ri_latch = msr & UART_MSR_RI;
353 clear_bit(XMIT_WAITING, &(info->tx_state));
354 dtl1_write_wakeup(info);
357 spin_unlock(&(info->lock));
359 return IRQ_HANDLED;
364 /* ======================== HCI interface ======================== */
367 static int dtl1_hci_open(struct hci_dev *hdev)
369 set_bit(HCI_RUNNING, &(hdev->flags));
371 return 0;
375 static int dtl1_hci_flush(struct hci_dev *hdev)
377 dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
379 /* Drop TX queue */
380 skb_queue_purge(&(info->txq));
382 return 0;
386 static int dtl1_hci_close(struct hci_dev *hdev)
388 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
389 return 0;
391 dtl1_hci_flush(hdev);
393 return 0;
397 static int dtl1_hci_send_frame(struct sk_buff *skb)
399 dtl1_info_t *info;
400 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
401 struct sk_buff *s;
402 nsh_t nsh;
404 if (!hdev) {
405 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
406 return -ENODEV;
409 info = (dtl1_info_t *)(hdev->driver_data);
411 switch (bt_cb(skb)->pkt_type) {
412 case HCI_COMMAND_PKT:
413 hdev->stat.cmd_tx++;
414 nsh.type = 0x81;
415 break;
416 case HCI_ACLDATA_PKT:
417 hdev->stat.acl_tx++;
418 nsh.type = 0x82;
419 break;
420 case HCI_SCODATA_PKT:
421 hdev->stat.sco_tx++;
422 nsh.type = 0x83;
423 break;
426 nsh.zero = 0;
427 nsh.len = skb->len;
429 s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
430 skb_reserve(s, NSHL);
431 memcpy(skb_put(s, skb->len), skb->data, skb->len);
432 if (skb->len & 0x0001)
433 *skb_put(s, 1) = 0; /* PAD */
435 /* Prepend skb with Nokia frame header and queue */
436 memcpy(skb_push(s, NSHL), &nsh, NSHL);
437 skb_queue_tail(&(info->txq), s);
439 dtl1_write_wakeup(info);
441 kfree_skb(skb);
443 return 0;
447 static void dtl1_hci_destruct(struct hci_dev *hdev)
452 static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
454 return -ENOIOCTLCMD;
459 /* ======================== Card services HCI interaction ======================== */
462 static int dtl1_open(dtl1_info_t *info)
464 unsigned long flags;
465 unsigned int iobase = info->link.io.BasePort1;
466 struct hci_dev *hdev;
468 spin_lock_init(&(info->lock));
470 skb_queue_head_init(&(info->txq));
472 info->rx_state = RECV_WAIT_NSH;
473 info->rx_count = NSHL;
474 info->rx_skb = NULL;
476 set_bit(XMIT_WAITING, &(info->tx_state));
478 /* Initialize HCI device */
479 hdev = hci_alloc_dev();
480 if (!hdev) {
481 BT_ERR("Can't allocate HCI device");
482 return -ENOMEM;
485 info->hdev = hdev;
487 hdev->type = HCI_PCCARD;
488 hdev->driver_data = info;
490 hdev->open = dtl1_hci_open;
491 hdev->close = dtl1_hci_close;
492 hdev->flush = dtl1_hci_flush;
493 hdev->send = dtl1_hci_send_frame;
494 hdev->destruct = dtl1_hci_destruct;
495 hdev->ioctl = dtl1_hci_ioctl;
497 hdev->owner = THIS_MODULE;
499 spin_lock_irqsave(&(info->lock), flags);
501 /* Reset UART */
502 outb(0, iobase + UART_MCR);
504 /* Turn off interrupts */
505 outb(0, iobase + UART_IER);
507 /* Initialize UART */
508 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
509 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
511 info->ri_latch = inb(info->link.io.BasePort1 + UART_MSR) & UART_MSR_RI;
513 /* Turn on interrupts */
514 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
516 spin_unlock_irqrestore(&(info->lock), flags);
518 /* Timeout before it is safe to send the first HCI packet */
519 msleep(2000);
521 /* Register HCI device */
522 if (hci_register_dev(hdev) < 0) {
523 BT_ERR("Can't register HCI device");
524 info->hdev = NULL;
525 hci_free_dev(hdev);
526 return -ENODEV;
529 return 0;
533 static int dtl1_close(dtl1_info_t *info)
535 unsigned long flags;
536 unsigned int iobase = info->link.io.BasePort1;
537 struct hci_dev *hdev = info->hdev;
539 if (!hdev)
540 return -ENODEV;
542 dtl1_hci_close(hdev);
544 spin_lock_irqsave(&(info->lock), flags);
546 /* Reset UART */
547 outb(0, iobase + UART_MCR);
549 /* Turn off interrupts */
550 outb(0, iobase + UART_IER);
552 spin_unlock_irqrestore(&(info->lock), flags);
554 if (hci_unregister_dev(hdev) < 0)
555 BT_ERR("Can't unregister HCI device %s", hdev->name);
557 hci_free_dev(hdev);
559 return 0;
562 static dev_link_t *dtl1_attach(void)
564 dtl1_info_t *info;
565 client_reg_t client_reg;
566 dev_link_t *link;
567 int ret;
569 /* Create new info device */
570 info = kzalloc(sizeof(*info), GFP_KERNEL);
571 if (!info)
572 return NULL;
574 link = &info->link;
575 link->priv = info;
577 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
578 link->io.NumPorts1 = 8;
579 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
580 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
582 link->irq.Handler = dtl1_interrupt;
583 link->irq.Instance = info;
585 link->conf.Attributes = CONF_ENABLE_IRQ;
586 link->conf.Vcc = 50;
587 link->conf.IntType = INT_MEMORY_AND_IO;
589 /* Register with Card Services */
590 link->next = NULL;
591 client_reg.dev_info = &dev_info;
592 client_reg.Version = 0x0210;
593 client_reg.event_callback_args.client_data = link;
595 ret = pcmcia_register_client(&link->handle, &client_reg);
596 if (ret != CS_SUCCESS) {
597 cs_error(link->handle, RegisterClient, ret);
598 dtl1_detach(link->handle);
599 return NULL;
602 return link;
606 static void dtl1_detach(struct pcmcia_device *p_dev)
608 dev_link_t *link = dev_to_instance(p_dev);
609 dtl1_info_t *info = link->priv;
611 if (link->state & DEV_CONFIG)
612 dtl1_release(link);
614 kfree(info);
617 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
619 int i;
621 i = pcmcia_get_tuple_data(handle, tuple);
622 if (i != CS_SUCCESS)
623 return i;
625 return pcmcia_parse_tuple(handle, tuple, parse);
628 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
630 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
631 return CS_NO_MORE_ITEMS;
632 return get_tuple(handle, tuple, parse);
635 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
637 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
638 return CS_NO_MORE_ITEMS;
639 return get_tuple(handle, tuple, parse);
642 static void dtl1_config(dev_link_t *link)
644 client_handle_t handle = link->handle;
645 dtl1_info_t *info = link->priv;
646 tuple_t tuple;
647 u_short buf[256];
648 cisparse_t parse;
649 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
650 config_info_t config;
651 int i, last_ret, last_fn;
653 tuple.TupleData = (cisdata_t *)buf;
654 tuple.TupleOffset = 0;
655 tuple.TupleDataMax = 255;
656 tuple.Attributes = 0;
658 /* Get configuration register information */
659 tuple.DesiredTuple = CISTPL_CONFIG;
660 last_ret = first_tuple(handle, &tuple, &parse);
661 if (last_ret != CS_SUCCESS) {
662 last_fn = ParseTuple;
663 goto cs_failed;
665 link->conf.ConfigBase = parse.config.base;
666 link->conf.Present = parse.config.rmask[0];
668 /* Configure card */
669 link->state |= DEV_CONFIG;
670 i = pcmcia_get_configuration_info(handle, &config);
671 link->conf.Vcc = config.Vcc;
673 tuple.TupleData = (cisdata_t *)buf;
674 tuple.TupleOffset = 0;
675 tuple.TupleDataMax = 255;
676 tuple.Attributes = 0;
677 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
679 /* Look for a generic full-sized window */
680 link->io.NumPorts1 = 8;
681 i = first_tuple(handle, &tuple, &parse);
682 while (i != CS_NO_MORE_ITEMS) {
683 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
684 link->conf.ConfigIndex = cf->index;
685 link->io.BasePort1 = cf->io.win[0].base;
686 link->io.NumPorts1 = cf->io.win[0].len; /*yo */
687 link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
688 i = pcmcia_request_io(link->handle, &link->io);
689 if (i == CS_SUCCESS)
690 break;
692 i = next_tuple(handle, &tuple, &parse);
695 if (i != CS_SUCCESS) {
696 cs_error(link->handle, RequestIO, i);
697 goto failed;
700 i = pcmcia_request_irq(link->handle, &link->irq);
701 if (i != CS_SUCCESS) {
702 cs_error(link->handle, RequestIRQ, i);
703 link->irq.AssignedIRQ = 0;
706 i = pcmcia_request_configuration(link->handle, &link->conf);
707 if (i != CS_SUCCESS) {
708 cs_error(link->handle, RequestConfiguration, i);
709 goto failed;
712 if (dtl1_open(info) != 0)
713 goto failed;
715 strcpy(info->node.dev_name, info->hdev->name);
716 link->dev = &info->node;
717 link->state &= ~DEV_CONFIG_PENDING;
719 return;
721 cs_failed:
722 cs_error(link->handle, last_fn, last_ret);
724 failed:
725 dtl1_release(link);
729 static void dtl1_release(dev_link_t *link)
731 dtl1_info_t *info = link->priv;
733 if (link->state & DEV_PRESENT)
734 dtl1_close(info);
736 link->dev = NULL;
738 pcmcia_release_configuration(link->handle);
739 pcmcia_release_io(link->handle, &link->io);
740 pcmcia_release_irq(link->handle, &link->irq);
742 link->state &= ~DEV_CONFIG;
745 static int dtl1_suspend(struct pcmcia_device *dev)
747 dev_link_t *link = dev_to_instance(dev);
749 link->state |= DEV_SUSPEND;
750 if (link->state & DEV_CONFIG)
751 pcmcia_release_configuration(link->handle);
753 return 0;
756 static int dtl1_resume(struct pcmcia_device *dev)
758 dev_link_t *link = dev_to_instance(dev);
760 link->state &= ~DEV_SUSPEND;
761 if (DEV_OK(link))
762 pcmcia_request_configuration(link->handle, &link->conf);
764 return 0;
767 static int dtl1_event(event_t event, int priority, event_callback_args_t *args)
769 dev_link_t *link = args->client_data;
771 switch (event) {
772 case CS_EVENT_CARD_INSERTION:
773 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
774 dtl1_config(link);
775 break;
778 return 0;
781 static struct pcmcia_device_id dtl1_ids[] = {
782 PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
783 PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
784 PCMCIA_DEVICE_NULL
786 MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
788 static struct pcmcia_driver dtl1_driver = {
789 .owner = THIS_MODULE,
790 .drv = {
791 .name = "dtl1_cs",
793 .attach = dtl1_attach,
794 .event = dtl1_event,
795 .remove = dtl1_detach,
796 .id_table = dtl1_ids,
797 .suspend = dtl1_suspend,
798 .resume = dtl1_resume,
801 static int __init init_dtl1_cs(void)
803 return pcmcia_register_driver(&dtl1_driver);
807 static void __exit exit_dtl1_cs(void)
809 pcmcia_unregister_driver(&dtl1_driver);
812 module_init(init_dtl1_cs);
813 module_exit(exit_dtl1_cs);