Pull now into release branch
[linux-2.6] / drivers / bluetooth / dtl1_cs.c
1 /*
2  *
3  *  A driver for Nokia Connectivity Card DTL-1 devices
4  *
5  *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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;
11  *
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.
16  *
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.
20  *
21  */
22
23 #include <linux/module.h>
24
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>
35
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>
42 #include <asm/io.h>
43
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>
50
51 #include <net/bluetooth/bluetooth.h>
52 #include <net/bluetooth/hci_core.h>
53
54
55
56 /* ======================== Module parameters ======================== */
57
58
59 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
60 MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
61 MODULE_LICENSE("GPL");
62
63
64
65 /* ======================== Local structures ======================== */
66
67
68 typedef struct dtl1_info_t {
69         struct pcmcia_device *p_dev;
70         dev_node_t node;
71
72         struct hci_dev *hdev;
73
74         spinlock_t lock;                /* For serializing operations */
75
76         unsigned long flowmask;         /* HCI flow mask */
77         int ri_latch;
78
79         struct sk_buff_head txq;
80         unsigned long tx_state;
81
82         unsigned long rx_state;
83         unsigned long rx_count;
84         struct sk_buff *rx_skb;
85 } dtl1_info_t;
86
87
88 static int dtl1_config(struct pcmcia_device *link);
89 static void dtl1_release(struct pcmcia_device *link);
90
91 static void dtl1_detach(struct pcmcia_device *p_dev);
92
93
94 /* Transmit states  */
95 #define XMIT_SENDING  1
96 #define XMIT_WAKEUP   2
97 #define XMIT_WAITING  8
98
99 /* Receiver States */
100 #define RECV_WAIT_NSH   0
101 #define RECV_WAIT_DATA  1
102
103
104 typedef struct {
105         u8 type;
106         u8 zero;
107         u16 len;
108 } __attribute__ ((packed)) nsh_t;       /* Nokia Specific Header */
109
110 #define NSHL  4                         /* Nokia Specific Header Length */
111
112
113
114 /* ======================== Interrupt handling ======================== */
115
116
117 static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
118 {
119         int actual = 0;
120
121         /* Tx FIFO should be empty */
122         if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
123                 return 0;
124
125         /* Fill FIFO with current frame */
126         while ((fifo_size-- > 0) && (actual < len)) {
127                 /* Transmit next byte */
128                 outb(buf[actual], iobase + UART_TX);
129                 actual++;
130         }
131
132         return actual;
133 }
134
135
136 static void dtl1_write_wakeup(dtl1_info_t *info)
137 {
138         if (!info) {
139                 BT_ERR("Unknown device");
140                 return;
141         }
142
143         if (test_bit(XMIT_WAITING, &(info->tx_state))) {
144                 set_bit(XMIT_WAKEUP, &(info->tx_state));
145                 return;
146         }
147
148         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
149                 set_bit(XMIT_WAKEUP, &(info->tx_state));
150                 return;
151         }
152
153         do {
154                 register unsigned int iobase = info->p_dev->io.BasePort1;
155                 register struct sk_buff *skb;
156                 register int len;
157
158                 clear_bit(XMIT_WAKEUP, &(info->tx_state));
159
160                 if (!pcmcia_dev_present(info->p_dev))
161                         return;
162
163                 if (!(skb = skb_dequeue(&(info->txq))))
164                         break;
165
166                 /* Send frame */
167                 len = dtl1_write(iobase, 32, skb->data, skb->len);
168
169                 if (len == skb->len) {
170                         set_bit(XMIT_WAITING, &(info->tx_state));
171                         kfree_skb(skb);
172                 } else {
173                         skb_pull(skb, len);
174                         skb_queue_head(&(info->txq), skb);
175                 }
176
177                 info->hdev->stat.byte_tx += len;
178
179         } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
180
181         clear_bit(XMIT_SENDING, &(info->tx_state));
182 }
183
184
185 static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
186 {
187         u8 flowmask = *(u8 *)skb->data;
188         int i;
189
190         printk(KERN_INFO "Bluetooth: Nokia control data =");
191         for (i = 0; i < skb->len; i++) {
192                 printk(" %02x", skb->data[i]);
193         }
194         printk("\n");
195
196         /* transition to active state */
197         if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
198                 clear_bit(XMIT_WAITING, &(info->tx_state));
199                 dtl1_write_wakeup(info);
200         }
201
202         info->flowmask = flowmask;
203
204         kfree_skb(skb);
205 }
206
207
208 static void dtl1_receive(dtl1_info_t *info)
209 {
210         unsigned int iobase;
211         nsh_t *nsh;
212         int boguscount = 0;
213
214         if (!info) {
215                 BT_ERR("Unknown device");
216                 return;
217         }
218
219         iobase = info->p_dev->io.BasePort1;
220
221         do {
222                 info->hdev->stat.byte_rx++;
223
224                 /* Allocate packet */
225                 if (info->rx_skb == NULL)
226                         if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
227                                 BT_ERR("Can't allocate mem for new packet");
228                                 info->rx_state = RECV_WAIT_NSH;
229                                 info->rx_count = NSHL;
230                                 return;
231                         }
232
233                 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
234                 nsh = (nsh_t *)info->rx_skb->data;
235
236                 info->rx_count--;
237
238                 if (info->rx_count == 0) {
239
240                         switch (info->rx_state) {
241                         case RECV_WAIT_NSH:
242                                 info->rx_state = RECV_WAIT_DATA;
243                                 info->rx_count = nsh->len + (nsh->len & 0x0001);
244                                 break;
245                         case RECV_WAIT_DATA:
246                                 bt_cb(info->rx_skb)->pkt_type = nsh->type;
247
248                                 /* remove PAD byte if it exists */
249                                 if (nsh->len & 0x0001) {
250                                         info->rx_skb->tail--;
251                                         info->rx_skb->len--;
252                                 }
253
254                                 /* remove NSH */
255                                 skb_pull(info->rx_skb, NSHL);
256
257                                 switch (bt_cb(info->rx_skb)->pkt_type) {
258                                 case 0x80:
259                                         /* control data for the Nokia Card */
260                                         dtl1_control(info, info->rx_skb);
261                                         break;
262                                 case 0x82:
263                                 case 0x83:
264                                 case 0x84:
265                                         /* send frame to the HCI layer */
266                                         info->rx_skb->dev = (void *) info->hdev;
267                                         bt_cb(info->rx_skb)->pkt_type &= 0x0f;
268                                         hci_recv_frame(info->rx_skb);
269                                         break;
270                                 default:
271                                         /* unknown packet */
272                                         BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
273                                         kfree_skb(info->rx_skb);
274                                         break;
275                                 }
276
277                                 info->rx_state = RECV_WAIT_NSH;
278                                 info->rx_count = NSHL;
279                                 info->rx_skb = NULL;
280                                 break;
281                         }
282
283                 }
284
285                 /* Make sure we don't stay here too long */
286                 if (boguscount++ > 32)
287                         break;
288
289         } while (inb(iobase + UART_LSR) & UART_LSR_DR);
290 }
291
292
293 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst)
294 {
295         dtl1_info_t *info = dev_inst;
296         unsigned int iobase;
297         unsigned char msr;
298         int boguscount = 0;
299         int iir, lsr;
300
301         if (!info || !info->hdev) {
302                 BT_ERR("Call of irq %d for unknown device", irq);
303                 return IRQ_NONE;
304         }
305
306         iobase = info->p_dev->io.BasePort1;
307
308         spin_lock(&(info->lock));
309
310         iir = inb(iobase + UART_IIR) & UART_IIR_ID;
311         while (iir) {
312
313                 /* Clear interrupt */
314                 lsr = inb(iobase + UART_LSR);
315
316                 switch (iir) {
317                 case UART_IIR_RLSI:
318                         BT_ERR("RLSI");
319                         break;
320                 case UART_IIR_RDI:
321                         /* Receive interrupt */
322                         dtl1_receive(info);
323                         break;
324                 case UART_IIR_THRI:
325                         if (lsr & UART_LSR_THRE) {
326                                 /* Transmitter ready for data */
327                                 dtl1_write_wakeup(info);
328                         }
329                         break;
330                 default:
331                         BT_ERR("Unhandled IIR=%#x", iir);
332                         break;
333                 }
334
335                 /* Make sure we don't stay here too long */
336                 if (boguscount++ > 100)
337                         break;
338
339                 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
340
341         }
342
343         msr = inb(iobase + UART_MSR);
344
345         if (info->ri_latch ^ (msr & UART_MSR_RI)) {
346                 info->ri_latch = msr & UART_MSR_RI;
347                 clear_bit(XMIT_WAITING, &(info->tx_state));
348                 dtl1_write_wakeup(info);
349         }
350
351         spin_unlock(&(info->lock));
352
353         return IRQ_HANDLED;
354 }
355
356
357
358 /* ======================== HCI interface ======================== */
359
360
361 static int dtl1_hci_open(struct hci_dev *hdev)
362 {
363         set_bit(HCI_RUNNING, &(hdev->flags));
364
365         return 0;
366 }
367
368
369 static int dtl1_hci_flush(struct hci_dev *hdev)
370 {
371         dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
372
373         /* Drop TX queue */
374         skb_queue_purge(&(info->txq));
375
376         return 0;
377 }
378
379
380 static int dtl1_hci_close(struct hci_dev *hdev)
381 {
382         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
383                 return 0;
384
385         dtl1_hci_flush(hdev);
386
387         return 0;
388 }
389
390
391 static int dtl1_hci_send_frame(struct sk_buff *skb)
392 {
393         dtl1_info_t *info;
394         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
395         struct sk_buff *s;
396         nsh_t nsh;
397
398         if (!hdev) {
399                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
400                 return -ENODEV;
401         }
402
403         info = (dtl1_info_t *)(hdev->driver_data);
404
405         switch (bt_cb(skb)->pkt_type) {
406         case HCI_COMMAND_PKT:
407                 hdev->stat.cmd_tx++;
408                 nsh.type = 0x81;
409                 break;
410         case HCI_ACLDATA_PKT:
411                 hdev->stat.acl_tx++;
412                 nsh.type = 0x82;
413                 break;
414         case HCI_SCODATA_PKT:
415                 hdev->stat.sco_tx++;
416                 nsh.type = 0x83;
417                 break;
418         };
419
420         nsh.zero = 0;
421         nsh.len = skb->len;
422
423         s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
424         if (!s)
425                 return -ENOMEM;
426
427         skb_reserve(s, NSHL);
428         skb_copy_from_linear_data(skb, skb_put(s, skb->len), skb->len);
429         if (skb->len & 0x0001)
430                 *skb_put(s, 1) = 0;     /* PAD */
431
432         /* Prepend skb with Nokia frame header and queue */
433         memcpy(skb_push(s, NSHL), &nsh, NSHL);
434         skb_queue_tail(&(info->txq), s);
435
436         dtl1_write_wakeup(info);
437
438         kfree_skb(skb);
439
440         return 0;
441 }
442
443
444 static void dtl1_hci_destruct(struct hci_dev *hdev)
445 {
446 }
447
448
449 static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd,  unsigned long arg)
450 {
451         return -ENOIOCTLCMD;
452 }
453
454
455
456 /* ======================== Card services HCI interaction ======================== */
457
458
459 static int dtl1_open(dtl1_info_t *info)
460 {
461         unsigned long flags;
462         unsigned int iobase = info->p_dev->io.BasePort1;
463         struct hci_dev *hdev;
464
465         spin_lock_init(&(info->lock));
466
467         skb_queue_head_init(&(info->txq));
468
469         info->rx_state = RECV_WAIT_NSH;
470         info->rx_count = NSHL;
471         info->rx_skb = NULL;
472
473         set_bit(XMIT_WAITING, &(info->tx_state));
474
475         /* Initialize HCI device */
476         hdev = hci_alloc_dev();
477         if (!hdev) {
478                 BT_ERR("Can't allocate HCI device");
479                 return -ENOMEM;
480         }
481
482         info->hdev = hdev;
483
484         hdev->type = HCI_PCCARD;
485         hdev->driver_data = info;
486         SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
487
488         hdev->open     = dtl1_hci_open;
489         hdev->close    = dtl1_hci_close;
490         hdev->flush    = dtl1_hci_flush;
491         hdev->send     = dtl1_hci_send_frame;
492         hdev->destruct = dtl1_hci_destruct;
493         hdev->ioctl    = dtl1_hci_ioctl;
494
495         hdev->owner = THIS_MODULE;
496
497         spin_lock_irqsave(&(info->lock), flags);
498
499         /* Reset UART */
500         outb(0, iobase + UART_MCR);
501
502         /* Turn off interrupts */
503         outb(0, iobase + UART_IER);
504
505         /* Initialize UART */
506         outb(UART_LCR_WLEN8, iobase + UART_LCR);        /* Reset DLAB */
507         outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
508
509         info->ri_latch = inb(info->p_dev->io.BasePort1 + UART_MSR) & UART_MSR_RI;
510
511         /* Turn on interrupts */
512         outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
513
514         spin_unlock_irqrestore(&(info->lock), flags);
515
516         /* Timeout before it is safe to send the first HCI packet */
517         msleep(2000);
518
519         /* Register HCI device */
520         if (hci_register_dev(hdev) < 0) {
521                 BT_ERR("Can't register HCI device");
522                 info->hdev = NULL;
523                 hci_free_dev(hdev);
524                 return -ENODEV;
525         }
526
527         return 0;
528 }
529
530
531 static int dtl1_close(dtl1_info_t *info)
532 {
533         unsigned long flags;
534         unsigned int iobase = info->p_dev->io.BasePort1;
535         struct hci_dev *hdev = info->hdev;
536
537         if (!hdev)
538                 return -ENODEV;
539
540         dtl1_hci_close(hdev);
541
542         spin_lock_irqsave(&(info->lock), flags);
543
544         /* Reset UART */
545         outb(0, iobase + UART_MCR);
546
547         /* Turn off interrupts */
548         outb(0, iobase + UART_IER);
549
550         spin_unlock_irqrestore(&(info->lock), flags);
551
552         if (hci_unregister_dev(hdev) < 0)
553                 BT_ERR("Can't unregister HCI device %s", hdev->name);
554
555         hci_free_dev(hdev);
556
557         return 0;
558 }
559
560 static int dtl1_probe(struct pcmcia_device *link)
561 {
562         dtl1_info_t *info;
563
564         /* Create new info device */
565         info = kzalloc(sizeof(*info), GFP_KERNEL);
566         if (!info)
567                 return -ENOMEM;
568
569         info->p_dev = link;
570         link->priv = info;
571
572         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
573         link->io.NumPorts1 = 8;
574         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
575         link->irq.IRQInfo1 = IRQ_LEVEL_ID;
576
577         link->irq.Handler = dtl1_interrupt;
578         link->irq.Instance = info;
579
580         link->conf.Attributes = CONF_ENABLE_IRQ;
581         link->conf.IntType = INT_MEMORY_AND_IO;
582
583         return dtl1_config(link);
584 }
585
586
587 static void dtl1_detach(struct pcmcia_device *link)
588 {
589         dtl1_info_t *info = link->priv;
590
591         dtl1_release(link);
592
593         kfree(info);
594 }
595
596 static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
597 {
598         int i;
599
600         i = pcmcia_get_tuple_data(handle, tuple);
601         if (i != CS_SUCCESS)
602                 return i;
603
604         return pcmcia_parse_tuple(handle, tuple, parse);
605 }
606
607 static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
608 {
609         if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
610                 return CS_NO_MORE_ITEMS;
611         return get_tuple(handle, tuple, parse);
612 }
613
614 static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
615 {
616         if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
617                 return CS_NO_MORE_ITEMS;
618         return get_tuple(handle, tuple, parse);
619 }
620
621 static int dtl1_config(struct pcmcia_device *link)
622 {
623         dtl1_info_t *info = link->priv;
624         tuple_t tuple;
625         u_short buf[256];
626         cisparse_t parse;
627         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
628         int i;
629
630         tuple.TupleData = (cisdata_t *)buf;
631         tuple.TupleOffset = 0;
632         tuple.TupleDataMax = 255;
633         tuple.Attributes = 0;
634         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
635
636         /* Look for a generic full-sized window */
637         link->io.NumPorts1 = 8;
638         i = first_tuple(link, &tuple, &parse);
639         while (i != CS_NO_MORE_ITEMS) {
640                 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
641                         link->conf.ConfigIndex = cf->index;
642                         link->io.BasePort1 = cf->io.win[0].base;
643                         link->io.NumPorts1 = cf->io.win[0].len; /*yo */
644                         link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
645                         i = pcmcia_request_io(link, &link->io);
646                         if (i == CS_SUCCESS)
647                                 break;
648                 }
649                 i = next_tuple(link, &tuple, &parse);
650         }
651
652         if (i != CS_SUCCESS) {
653                 cs_error(link, RequestIO, i);
654                 goto failed;
655         }
656
657         i = pcmcia_request_irq(link, &link->irq);
658         if (i != CS_SUCCESS) {
659                 cs_error(link, RequestIRQ, i);
660                 link->irq.AssignedIRQ = 0;
661         }
662
663         i = pcmcia_request_configuration(link, &link->conf);
664         if (i != CS_SUCCESS) {
665                 cs_error(link, RequestConfiguration, i);
666                 goto failed;
667         }
668
669         if (dtl1_open(info) != 0)
670                 goto failed;
671
672         strcpy(info->node.dev_name, info->hdev->name);
673         link->dev_node = &info->node;
674
675         return 0;
676
677 failed:
678         dtl1_release(link);
679         return -ENODEV;
680 }
681
682
683 static void dtl1_release(struct pcmcia_device *link)
684 {
685         dtl1_info_t *info = link->priv;
686
687         dtl1_close(info);
688
689         pcmcia_disable_device(link);
690 }
691
692
693 static struct pcmcia_device_id dtl1_ids[] = {
694         PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
695         PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-4", 0xe1bfdd64, 0x9102bc82),
696         PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
697         PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
698         PCMCIA_DEVICE_NULL
699 };
700 MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
701
702 static struct pcmcia_driver dtl1_driver = {
703         .owner          = THIS_MODULE,
704         .drv            = {
705                 .name   = "dtl1_cs",
706         },
707         .probe          = dtl1_probe,
708         .remove         = dtl1_detach,
709         .id_table       = dtl1_ids,
710 };
711
712 static int __init init_dtl1_cs(void)
713 {
714         return pcmcia_register_driver(&dtl1_driver);
715 }
716
717
718 static void __exit exit_dtl1_cs(void)
719 {
720         pcmcia_unregister_driver(&dtl1_driver);
721 }
722
723 module_init(init_dtl1_cs);
724 module_exit(exit_dtl1_cs);