Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[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/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>
36
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>
43 #include <asm/io.h>
44
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>
51
52 #include <net/bluetooth/bluetooth.h>
53 #include <net/bluetooth/hci_core.h>
54
55
56
57 /* ======================== Module parameters ======================== */
58
59
60 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
61 MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
62 MODULE_LICENSE("GPL");
63
64
65
66 /* ======================== Local structures ======================== */
67
68
69 typedef struct dtl1_info_t {
70         struct pcmcia_device *p_dev;
71         dev_node_t node;
72
73         struct hci_dev *hdev;
74
75         spinlock_t lock;                /* For serializing operations */
76
77         unsigned long flowmask;         /* HCI flow mask */
78         int ri_latch;
79
80         struct sk_buff_head txq;
81         unsigned long tx_state;
82
83         unsigned long rx_state;
84         unsigned long rx_count;
85         struct sk_buff *rx_skb;
86 } dtl1_info_t;
87
88
89 static int dtl1_config(struct pcmcia_device *link);
90 static void dtl1_release(struct pcmcia_device *link);
91
92 static void dtl1_detach(struct pcmcia_device *p_dev);
93
94
95 /* Transmit states  */
96 #define XMIT_SENDING  1
97 #define XMIT_WAKEUP   2
98 #define XMIT_WAITING  8
99
100 /* Receiver States */
101 #define RECV_WAIT_NSH   0
102 #define RECV_WAIT_DATA  1
103
104
105 typedef struct {
106         u8 type;
107         u8 zero;
108         u16 len;
109 } __attribute__ ((packed)) nsh_t;       /* Nokia Specific Header */
110
111 #define NSHL  4                         /* Nokia Specific Header Length */
112
113
114
115 /* ======================== Interrupt handling ======================== */
116
117
118 static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
119 {
120         int actual = 0;
121
122         /* Tx FIFO should be empty */
123         if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
124                 return 0;
125
126         /* Fill FIFO with current frame */
127         while ((fifo_size-- > 0) && (actual < len)) {
128                 /* Transmit next byte */
129                 outb(buf[actual], iobase + UART_TX);
130                 actual++;
131         }
132
133         return actual;
134 }
135
136
137 static void dtl1_write_wakeup(dtl1_info_t *info)
138 {
139         if (!info) {
140                 BT_ERR("Unknown device");
141                 return;
142         }
143
144         if (test_bit(XMIT_WAITING, &(info->tx_state))) {
145                 set_bit(XMIT_WAKEUP, &(info->tx_state));
146                 return;
147         }
148
149         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
150                 set_bit(XMIT_WAKEUP, &(info->tx_state));
151                 return;
152         }
153
154         do {
155                 register unsigned int iobase = info->p_dev->io.BasePort1;
156                 register struct sk_buff *skb;
157                 register int len;
158
159                 clear_bit(XMIT_WAKEUP, &(info->tx_state));
160
161                 if (!pcmcia_dev_present(info->p_dev))
162                         return;
163
164                 if (!(skb = skb_dequeue(&(info->txq))))
165                         break;
166
167                 /* Send frame */
168                 len = dtl1_write(iobase, 32, skb->data, skb->len);
169
170                 if (len == skb->len) {
171                         set_bit(XMIT_WAITING, &(info->tx_state));
172                         kfree_skb(skb);
173                 } else {
174                         skb_pull(skb, len);
175                         skb_queue_head(&(info->txq), skb);
176                 }
177
178                 info->hdev->stat.byte_tx += len;
179
180         } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
181
182         clear_bit(XMIT_SENDING, &(info->tx_state));
183 }
184
185
186 static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
187 {
188         u8 flowmask = *(u8 *)skb->data;
189         int i;
190
191         printk(KERN_INFO "Bluetooth: Nokia control data =");
192         for (i = 0; i < skb->len; i++) {
193                 printk(" %02x", skb->data[i]);
194         }
195         printk("\n");
196
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);
201         }
202
203         info->flowmask = flowmask;
204
205         kfree_skb(skb);
206 }
207
208
209 static void dtl1_receive(dtl1_info_t *info)
210 {
211         unsigned int iobase;
212         nsh_t *nsh;
213         int boguscount = 0;
214
215         if (!info) {
216                 BT_ERR("Unknown device");
217                 return;
218         }
219
220         iobase = info->p_dev->io.BasePort1;
221
222         do {
223                 info->hdev->stat.byte_rx++;
224
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;
231                                 return;
232                         }
233
234                 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
235                 nsh = (nsh_t *)info->rx_skb->data;
236
237                 info->rx_count--;
238
239                 if (info->rx_count == 0) {
240
241                         switch (info->rx_state) {
242                         case RECV_WAIT_NSH:
243                                 info->rx_state = RECV_WAIT_DATA;
244                                 info->rx_count = nsh->len + (nsh->len & 0x0001);
245                                 break;
246                         case RECV_WAIT_DATA:
247                                 bt_cb(info->rx_skb)->pkt_type = nsh->type;
248
249                                 /* remove PAD byte if it exists */
250                                 if (nsh->len & 0x0001) {
251                                         info->rx_skb->tail--;
252                                         info->rx_skb->len--;
253                                 }
254
255                                 /* remove NSH */
256                                 skb_pull(info->rx_skb, NSHL);
257
258                                 switch (bt_cb(info->rx_skb)->pkt_type) {
259                                 case 0x80:
260                                         /* control data for the Nokia Card */
261                                         dtl1_control(info, info->rx_skb);
262                                         break;
263                                 case 0x82:
264                                 case 0x83:
265                                 case 0x84:
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);
270                                         break;
271                                 default:
272                                         /* unknown packet */
273                                         BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
274                                         kfree_skb(info->rx_skb);
275                                         break;
276                                 }
277
278                                 info->rx_state = RECV_WAIT_NSH;
279                                 info->rx_count = NSHL;
280                                 info->rx_skb = NULL;
281                                 break;
282                         }
283
284                 }
285
286                 /* Make sure we don't stay here too long */
287                 if (boguscount++ > 32)
288                         break;
289
290         } while (inb(iobase + UART_LSR) & UART_LSR_DR);
291 }
292
293
294 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
295 {
296         dtl1_info_t *info = dev_inst;
297         unsigned int iobase;
298         unsigned char msr;
299         int boguscount = 0;
300         int iir, lsr;
301
302         if (!info || !info->hdev) {
303                 BT_ERR("Call of irq %d for unknown device", irq);
304                 return IRQ_NONE;
305         }
306
307         iobase = info->p_dev->io.BasePort1;
308
309         spin_lock(&(info->lock));
310
311         iir = inb(iobase + UART_IIR) & UART_IIR_ID;
312         while (iir) {
313
314                 /* Clear interrupt */
315                 lsr = inb(iobase + UART_LSR);
316
317                 switch (iir) {
318                 case UART_IIR_RLSI:
319                         BT_ERR("RLSI");
320                         break;
321                 case UART_IIR_RDI:
322                         /* Receive interrupt */
323                         dtl1_receive(info);
324                         break;
325                 case UART_IIR_THRI:
326                         if (lsr & UART_LSR_THRE) {
327                                 /* Transmitter ready for data */
328                                 dtl1_write_wakeup(info);
329                         }
330                         break;
331                 default:
332                         BT_ERR("Unhandled IIR=%#x", iir);
333                         break;
334                 }
335
336                 /* Make sure we don't stay here too long */
337                 if (boguscount++ > 100)
338                         break;
339
340                 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
341
342         }
343
344         msr = inb(iobase + UART_MSR);
345
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);
350         }
351
352         spin_unlock(&(info->lock));
353
354         return IRQ_HANDLED;
355 }
356
357
358
359 /* ======================== HCI interface ======================== */
360
361
362 static int dtl1_hci_open(struct hci_dev *hdev)
363 {
364         set_bit(HCI_RUNNING, &(hdev->flags));
365
366         return 0;
367 }
368
369
370 static int dtl1_hci_flush(struct hci_dev *hdev)
371 {
372         dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
373
374         /* Drop TX queue */
375         skb_queue_purge(&(info->txq));
376
377         return 0;
378 }
379
380
381 static int dtl1_hci_close(struct hci_dev *hdev)
382 {
383         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
384                 return 0;
385
386         dtl1_hci_flush(hdev);
387
388         return 0;
389 }
390
391
392 static int dtl1_hci_send_frame(struct sk_buff *skb)
393 {
394         dtl1_info_t *info;
395         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
396         struct sk_buff *s;
397         nsh_t nsh;
398
399         if (!hdev) {
400                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
401                 return -ENODEV;
402         }
403
404         info = (dtl1_info_t *)(hdev->driver_data);
405
406         switch (bt_cb(skb)->pkt_type) {
407         case HCI_COMMAND_PKT:
408                 hdev->stat.cmd_tx++;
409                 nsh.type = 0x81;
410                 break;
411         case HCI_ACLDATA_PKT:
412                 hdev->stat.acl_tx++;
413                 nsh.type = 0x82;
414                 break;
415         case HCI_SCODATA_PKT:
416                 hdev->stat.sco_tx++;
417                 nsh.type = 0x83;
418                 break;
419         };
420
421         nsh.zero = 0;
422         nsh.len = skb->len;
423
424         s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
425         if (!s)
426                 return -ENOMEM;
427
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 */
432
433         /* Prepend skb with Nokia frame header and queue */
434         memcpy(skb_push(s, NSHL), &nsh, NSHL);
435         skb_queue_tail(&(info->txq), s);
436
437         dtl1_write_wakeup(info);
438
439         kfree_skb(skb);
440
441         return 0;
442 }
443
444
445 static void dtl1_hci_destruct(struct hci_dev *hdev)
446 {
447 }
448
449
450 static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd,  unsigned long arg)
451 {
452         return -ENOIOCTLCMD;
453 }
454
455
456
457 /* ======================== Card services HCI interaction ======================== */
458
459
460 static int dtl1_open(dtl1_info_t *info)
461 {
462         unsigned long flags;
463         unsigned int iobase = info->p_dev->io.BasePort1;
464         struct hci_dev *hdev;
465
466         spin_lock_init(&(info->lock));
467
468         skb_queue_head_init(&(info->txq));
469
470         info->rx_state = RECV_WAIT_NSH;
471         info->rx_count = NSHL;
472         info->rx_skb = NULL;
473
474         set_bit(XMIT_WAITING, &(info->tx_state));
475
476         /* Initialize HCI device */
477         hdev = hci_alloc_dev();
478         if (!hdev) {
479                 BT_ERR("Can't allocate HCI device");
480                 return -ENOMEM;
481         }
482
483         info->hdev = hdev;
484
485         hdev->type = HCI_PCCARD;
486         hdev->driver_data = info;
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, last_ret, last_fn;
629
630         tuple.TupleData = (cisdata_t *)buf;
631         tuple.TupleOffset = 0;
632         tuple.TupleDataMax = 255;
633         tuple.Attributes = 0;
634
635         /* Get configuration register information */
636         tuple.DesiredTuple = CISTPL_CONFIG;
637         last_ret = first_tuple(link, &tuple, &parse);
638         if (last_ret != CS_SUCCESS) {
639                 last_fn = ParseTuple;
640                 goto cs_failed;
641         }
642         link->conf.ConfigBase = parse.config.base;
643         link->conf.Present = parse.config.rmask[0];
644
645         tuple.TupleData = (cisdata_t *)buf;
646         tuple.TupleOffset = 0;
647         tuple.TupleDataMax = 255;
648         tuple.Attributes = 0;
649         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
650
651         /* Look for a generic full-sized window */
652         link->io.NumPorts1 = 8;
653         i = first_tuple(link, &tuple, &parse);
654         while (i != CS_NO_MORE_ITEMS) {
655                 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
656                         link->conf.ConfigIndex = cf->index;
657                         link->io.BasePort1 = cf->io.win[0].base;
658                         link->io.NumPorts1 = cf->io.win[0].len; /*yo */
659                         link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
660                         i = pcmcia_request_io(link, &link->io);
661                         if (i == CS_SUCCESS)
662                                 break;
663                 }
664                 i = next_tuple(link, &tuple, &parse);
665         }
666
667         if (i != CS_SUCCESS) {
668                 cs_error(link, RequestIO, i);
669                 goto failed;
670         }
671
672         i = pcmcia_request_irq(link, &link->irq);
673         if (i != CS_SUCCESS) {
674                 cs_error(link, RequestIRQ, i);
675                 link->irq.AssignedIRQ = 0;
676         }
677
678         i = pcmcia_request_configuration(link, &link->conf);
679         if (i != CS_SUCCESS) {
680                 cs_error(link, RequestConfiguration, i);
681                 goto failed;
682         }
683
684         if (dtl1_open(info) != 0)
685                 goto failed;
686
687         strcpy(info->node.dev_name, info->hdev->name);
688         link->dev_node = &info->node;
689
690         return 0;
691
692 cs_failed:
693         cs_error(link, last_fn, last_ret);
694
695 failed:
696         dtl1_release(link);
697         return -ENODEV;
698 }
699
700
701 static void dtl1_release(struct pcmcia_device *link)
702 {
703         dtl1_info_t *info = link->priv;
704
705         dtl1_close(info);
706
707         pcmcia_disable_device(link);
708 }
709
710
711 static struct pcmcia_device_id dtl1_ids[] = {
712         PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
713         PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
714         PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
715         PCMCIA_DEVICE_NULL
716 };
717 MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
718
719 static struct pcmcia_driver dtl1_driver = {
720         .owner          = THIS_MODULE,
721         .drv            = {
722                 .name   = "dtl1_cs",
723         },
724         .probe          = dtl1_probe,
725         .remove         = dtl1_detach,
726         .id_table       = dtl1_ids,
727 };
728
729 static int __init init_dtl1_cs(void)
730 {
731         return pcmcia_register_driver(&dtl1_driver);
732 }
733
734
735 static void __exit exit_dtl1_cs(void)
736 {
737         pcmcia_unregister_driver(&dtl1_driver);
738 }
739
740 module_init(init_dtl1_cs);
741 module_exit(exit_dtl1_cs);