Bluetooth: Provide hdev parameter to hci_recv_frame() driver callback
[linux-block.git] / drivers / bluetooth / btuart_cs.c
1 /*
2  *
3  *  Driver for Bluetooth PCMCIA cards with HCI UART interface
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/io.h>
42
43 #include <pcmcia/cistpl.h>
44 #include <pcmcia/ciscode.h>
45 #include <pcmcia/ds.h>
46 #include <pcmcia/cisreg.h>
47
48 #include <net/bluetooth/bluetooth.h>
49 #include <net/bluetooth/hci_core.h>
50
51
52
53 /* ======================== Module parameters ======================== */
54
55
56 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
57 MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface");
58 MODULE_LICENSE("GPL");
59
60
61
62 /* ======================== Local structures ======================== */
63
64
65 typedef struct btuart_info_t {
66         struct pcmcia_device *p_dev;
67
68         struct hci_dev *hdev;
69
70         spinlock_t lock;        /* For serializing operations */
71
72         struct sk_buff_head txq;
73         unsigned long tx_state;
74
75         unsigned long rx_state;
76         unsigned long rx_count;
77         struct sk_buff *rx_skb;
78 } btuart_info_t;
79
80
81 static int btuart_config(struct pcmcia_device *link);
82 static void btuart_release(struct pcmcia_device *link);
83
84 static void btuart_detach(struct pcmcia_device *p_dev);
85
86
87 /* Maximum baud rate */
88 #define SPEED_MAX  115200
89
90 /* Default baud rate: 57600, 115200, 230400 or 460800 */
91 #define DEFAULT_BAUD_RATE  115200
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_PACKET_TYPE   0
101 #define RECV_WAIT_EVENT_HEADER  1
102 #define RECV_WAIT_ACL_HEADER    2
103 #define RECV_WAIT_SCO_HEADER    3
104 #define RECV_WAIT_DATA          4
105
106
107
108 /* ======================== Interrupt handling ======================== */
109
110
111 static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
112 {
113         int actual = 0;
114
115         /* Tx FIFO should be empty */
116         if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
117                 return 0;
118
119         /* Fill FIFO with current frame */
120         while ((fifo_size-- > 0) && (actual < len)) {
121                 /* Transmit next byte */
122                 outb(buf[actual], iobase + UART_TX);
123                 actual++;
124         }
125
126         return actual;
127 }
128
129
130 static void btuart_write_wakeup(btuart_info_t *info)
131 {
132         if (!info) {
133                 BT_ERR("Unknown device");
134                 return;
135         }
136
137         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
138                 set_bit(XMIT_WAKEUP, &(info->tx_state));
139                 return;
140         }
141
142         do {
143                 unsigned int iobase = info->p_dev->resource[0]->start;
144                 register struct sk_buff *skb;
145                 int len;
146
147                 clear_bit(XMIT_WAKEUP, &(info->tx_state));
148
149                 if (!pcmcia_dev_present(info->p_dev))
150                         return;
151
152                 if (!(skb = skb_dequeue(&(info->txq))))
153                         break;
154
155                 /* Send frame */
156                 len = btuart_write(iobase, 16, skb->data, skb->len);
157                 set_bit(XMIT_WAKEUP, &(info->tx_state));
158
159                 if (len == skb->len) {
160                         kfree_skb(skb);
161                 } else {
162                         skb_pull(skb, len);
163                         skb_queue_head(&(info->txq), skb);
164                 }
165
166                 info->hdev->stat.byte_tx += len;
167
168         } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
169
170         clear_bit(XMIT_SENDING, &(info->tx_state));
171 }
172
173
174 static void btuart_receive(btuart_info_t *info)
175 {
176         unsigned int iobase;
177         int boguscount = 0;
178
179         if (!info) {
180                 BT_ERR("Unknown device");
181                 return;
182         }
183
184         iobase = info->p_dev->resource[0]->start;
185
186         do {
187                 info->hdev->stat.byte_rx++;
188
189                 /* Allocate packet */
190                 if (info->rx_skb == NULL) {
191                         info->rx_state = RECV_WAIT_PACKET_TYPE;
192                         info->rx_count = 0;
193                         if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
194                                 BT_ERR("Can't allocate mem for new packet");
195                                 return;
196                         }
197                 }
198
199                 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
200
201                         bt_cb(info->rx_skb)->pkt_type = inb(iobase + UART_RX);
202
203                         switch (bt_cb(info->rx_skb)->pkt_type) {
204
205                         case HCI_EVENT_PKT:
206                                 info->rx_state = RECV_WAIT_EVENT_HEADER;
207                                 info->rx_count = HCI_EVENT_HDR_SIZE;
208                                 break;
209
210                         case HCI_ACLDATA_PKT:
211                                 info->rx_state = RECV_WAIT_ACL_HEADER;
212                                 info->rx_count = HCI_ACL_HDR_SIZE;
213                                 break;
214
215                         case HCI_SCODATA_PKT:
216                                 info->rx_state = RECV_WAIT_SCO_HEADER;
217                                 info->rx_count = HCI_SCO_HDR_SIZE;
218                                 break;
219
220                         default:
221                                 /* Unknown packet */
222                                 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
223                                 info->hdev->stat.err_rx++;
224                                 clear_bit(HCI_RUNNING, &(info->hdev->flags));
225
226                                 kfree_skb(info->rx_skb);
227                                 info->rx_skb = NULL;
228                                 break;
229
230                         }
231
232                 } else {
233
234                         *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
235                         info->rx_count--;
236
237                         if (info->rx_count == 0) {
238
239                                 int dlen;
240                                 struct hci_event_hdr *eh;
241                                 struct hci_acl_hdr *ah;
242                                 struct hci_sco_hdr *sh;
243
244
245                                 switch (info->rx_state) {
246
247                                 case RECV_WAIT_EVENT_HEADER:
248                                         eh = hci_event_hdr(info->rx_skb);
249                                         info->rx_state = RECV_WAIT_DATA;
250                                         info->rx_count = eh->plen;
251                                         break;
252
253                                 case RECV_WAIT_ACL_HEADER:
254                                         ah = hci_acl_hdr(info->rx_skb);
255                                         dlen = __le16_to_cpu(ah->dlen);
256                                         info->rx_state = RECV_WAIT_DATA;
257                                         info->rx_count = dlen;
258                                         break;
259
260                                 case RECV_WAIT_SCO_HEADER:
261                                         sh = hci_sco_hdr(info->rx_skb);
262                                         info->rx_state = RECV_WAIT_DATA;
263                                         info->rx_count = sh->dlen;
264                                         break;
265
266                                 case RECV_WAIT_DATA:
267                                         hci_recv_frame(info->hdev, info->rx_skb);
268                                         info->rx_skb = NULL;
269                                         break;
270
271                                 }
272
273                         }
274
275                 }
276
277                 /* Make sure we don't stay here too long */
278                 if (boguscount++ > 16)
279                         break;
280
281         } while (inb(iobase + UART_LSR) & UART_LSR_DR);
282 }
283
284
285 static irqreturn_t btuart_interrupt(int irq, void *dev_inst)
286 {
287         btuart_info_t *info = dev_inst;
288         unsigned int iobase;
289         int boguscount = 0;
290         int iir, lsr;
291         irqreturn_t r = IRQ_NONE;
292
293         if (!info || !info->hdev)
294                 /* our irq handler is shared */
295                 return IRQ_NONE;
296
297         iobase = info->p_dev->resource[0]->start;
298
299         spin_lock(&(info->lock));
300
301         iir = inb(iobase + UART_IIR) & UART_IIR_ID;
302         while (iir) {
303                 r = IRQ_HANDLED;
304
305                 /* Clear interrupt */
306                 lsr = inb(iobase + UART_LSR);
307
308                 switch (iir) {
309                 case UART_IIR_RLSI:
310                         BT_ERR("RLSI");
311                         break;
312                 case UART_IIR_RDI:
313                         /* Receive interrupt */
314                         btuart_receive(info);
315                         break;
316                 case UART_IIR_THRI:
317                         if (lsr & UART_LSR_THRE) {
318                                 /* Transmitter ready for data */
319                                 btuart_write_wakeup(info);
320                         }
321                         break;
322                 default:
323                         BT_ERR("Unhandled IIR=%#x", iir);
324                         break;
325                 }
326
327                 /* Make sure we don't stay here too long */
328                 if (boguscount++ > 100)
329                         break;
330
331                 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
332
333         }
334
335         spin_unlock(&(info->lock));
336
337         return r;
338 }
339
340
341 static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
342 {
343         unsigned long flags;
344         unsigned int iobase;
345         int fcr;                /* FIFO control reg */
346         int lcr;                /* Line control reg */
347         int divisor;
348
349         if (!info) {
350                 BT_ERR("Unknown device");
351                 return;
352         }
353
354         iobase = info->p_dev->resource[0]->start;
355
356         spin_lock_irqsave(&(info->lock), flags);
357
358         /* Turn off interrupts */
359         outb(0, iobase + UART_IER);
360
361         divisor = SPEED_MAX / speed;
362
363         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
364
365         /* 
366          * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
367          * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
368          * about this timeout since it will always be fast enough. 
369          */
370
371         if (speed < 38400)
372                 fcr |= UART_FCR_TRIGGER_1;
373         else
374                 fcr |= UART_FCR_TRIGGER_14;
375
376         /* Bluetooth cards use 8N1 */
377         lcr = UART_LCR_WLEN8;
378
379         outb(UART_LCR_DLAB | lcr, iobase + UART_LCR);   /* Set DLAB */
380         outb(divisor & 0xff, iobase + UART_DLL);        /* Set speed */
381         outb(divisor >> 8, iobase + UART_DLM);
382         outb(lcr, iobase + UART_LCR);   /* Set 8N1  */
383         outb(fcr, iobase + UART_FCR);   /* Enable FIFO's */
384
385         /* Turn on interrupts */
386         outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
387
388         spin_unlock_irqrestore(&(info->lock), flags);
389 }
390
391
392
393 /* ======================== HCI interface ======================== */
394
395
396 static int btuart_hci_flush(struct hci_dev *hdev)
397 {
398         btuart_info_t *info = hci_get_drvdata(hdev);
399
400         /* Drop TX queue */
401         skb_queue_purge(&(info->txq));
402
403         return 0;
404 }
405
406
407 static int btuart_hci_open(struct hci_dev *hdev)
408 {
409         set_bit(HCI_RUNNING, &(hdev->flags));
410
411         return 0;
412 }
413
414
415 static int btuart_hci_close(struct hci_dev *hdev)
416 {
417         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
418                 return 0;
419
420         btuart_hci_flush(hdev);
421
422         return 0;
423 }
424
425
426 static int btuart_hci_send_frame(struct sk_buff *skb)
427 {
428         btuart_info_t *info;
429         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
430
431         if (!hdev) {
432                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
433                 return -ENODEV;
434         }
435
436         info = hci_get_drvdata(hdev);
437
438         switch (bt_cb(skb)->pkt_type) {
439         case HCI_COMMAND_PKT:
440                 hdev->stat.cmd_tx++;
441                 break;
442         case HCI_ACLDATA_PKT:
443                 hdev->stat.acl_tx++;
444                 break;
445         case HCI_SCODATA_PKT:
446                 hdev->stat.sco_tx++;
447                 break;
448         }
449
450         /* Prepend skb with frame type */
451         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
452         skb_queue_tail(&(info->txq), skb);
453
454         btuart_write_wakeup(info);
455
456         return 0;
457 }
458
459
460
461 /* ======================== Card services HCI interaction ======================== */
462
463
464 static int btuart_open(btuart_info_t *info)
465 {
466         unsigned long flags;
467         unsigned int iobase = info->p_dev->resource[0]->start;
468         struct hci_dev *hdev;
469
470         spin_lock_init(&(info->lock));
471
472         skb_queue_head_init(&(info->txq));
473
474         info->rx_state = RECV_WAIT_PACKET_TYPE;
475         info->rx_count = 0;
476         info->rx_skb = NULL;
477
478         /* Initialize HCI device */
479         hdev = hci_alloc_dev();
480         if (!hdev) {
481                 BT_ERR("Can't allocate HCI device");
482                 return -ENOMEM;
483         }
484
485         info->hdev = hdev;
486
487         hdev->bus = HCI_PCCARD;
488         hci_set_drvdata(hdev, info);
489         SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
490
491         hdev->open  = btuart_hci_open;
492         hdev->close = btuart_hci_close;
493         hdev->flush = btuart_hci_flush;
494         hdev->send  = btuart_hci_send_frame;
495
496         spin_lock_irqsave(&(info->lock), flags);
497
498         /* Reset UART */
499         outb(0, iobase + UART_MCR);
500
501         /* Turn off interrupts */
502         outb(0, iobase + UART_IER);
503
504         /* Initialize UART */
505         outb(UART_LCR_WLEN8, iobase + UART_LCR);        /* Reset DLAB */
506         outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
507
508         /* Turn on interrupts */
509         // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
510
511         spin_unlock_irqrestore(&(info->lock), flags);
512
513         btuart_change_speed(info, DEFAULT_BAUD_RATE);
514
515         /* Timeout before it is safe to send the first HCI packet */
516         msleep(1000);
517
518         /* Register HCI device */
519         if (hci_register_dev(hdev) < 0) {
520                 BT_ERR("Can't register HCI device");
521                 info->hdev = NULL;
522                 hci_free_dev(hdev);
523                 return -ENODEV;
524         }
525
526         return 0;
527 }
528
529
530 static int btuart_close(btuart_info_t *info)
531 {
532         unsigned long flags;
533         unsigned int iobase = info->p_dev->resource[0]->start;
534         struct hci_dev *hdev = info->hdev;
535
536         if (!hdev)
537                 return -ENODEV;
538
539         btuart_hci_close(hdev);
540
541         spin_lock_irqsave(&(info->lock), flags);
542
543         /* Reset UART */
544         outb(0, iobase + UART_MCR);
545
546         /* Turn off interrupts */
547         outb(0, iobase + UART_IER);
548
549         spin_unlock_irqrestore(&(info->lock), flags);
550
551         hci_unregister_dev(hdev);
552         hci_free_dev(hdev);
553
554         return 0;
555 }
556
557 static int btuart_probe(struct pcmcia_device *link)
558 {
559         btuart_info_t *info;
560
561         /* Create new info device */
562         info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
563         if (!info)
564                 return -ENOMEM;
565
566         info->p_dev = link;
567         link->priv = info;
568
569         link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_VPP |
570                 CONF_AUTO_SET_IO;
571
572         return btuart_config(link);
573 }
574
575
576 static void btuart_detach(struct pcmcia_device *link)
577 {
578         btuart_release(link);
579 }
580
581 static int btuart_check_config(struct pcmcia_device *p_dev, void *priv_data)
582 {
583         int *try = priv_data;
584
585         if (!try)
586                 p_dev->io_lines = 16;
587
588         if ((p_dev->resource[0]->end != 8) || (p_dev->resource[0]->start == 0))
589                 return -EINVAL;
590
591         p_dev->resource[0]->end = 8;
592         p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
593         p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
594
595         return pcmcia_request_io(p_dev);
596 }
597
598 static int btuart_check_config_notpicky(struct pcmcia_device *p_dev,
599                                         void *priv_data)
600 {
601         static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
602         int j;
603
604         if (p_dev->io_lines > 3)
605                 return -ENODEV;
606
607         p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
608         p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
609         p_dev->resource[0]->end = 8;
610
611         for (j = 0; j < 5; j++) {
612                 p_dev->resource[0]->start = base[j];
613                 p_dev->io_lines = base[j] ? 16 : 3;
614                 if (!pcmcia_request_io(p_dev))
615                         return 0;
616         }
617         return -ENODEV;
618 }
619
620 static int btuart_config(struct pcmcia_device *link)
621 {
622         btuart_info_t *info = link->priv;
623         int i;
624         int try;
625
626         /* First pass: look for a config entry that looks normal.
627            Two tries: without IO aliases, then with aliases */
628         for (try = 0; try < 2; try++)
629                 if (!pcmcia_loop_config(link, btuart_check_config, &try))
630                         goto found_port;
631
632         /* Second pass: try to find an entry that isn't picky about
633            its base address, then try to grab any standard serial port
634            address, and finally try to get any free port. */
635         if (!pcmcia_loop_config(link, btuart_check_config_notpicky, NULL))
636                 goto found_port;
637
638         BT_ERR("No usable port range found");
639         goto failed;
640
641 found_port:
642         i = pcmcia_request_irq(link, btuart_interrupt);
643         if (i != 0)
644                 goto failed;
645
646         i = pcmcia_enable_device(link);
647         if (i != 0)
648                 goto failed;
649
650         if (btuart_open(info) != 0)
651                 goto failed;
652
653         return 0;
654
655 failed:
656         btuart_release(link);
657         return -ENODEV;
658 }
659
660
661 static void btuart_release(struct pcmcia_device *link)
662 {
663         btuart_info_t *info = link->priv;
664
665         btuart_close(info);
666
667         pcmcia_disable_device(link);
668 }
669
670 static const struct pcmcia_device_id btuart_ids[] = {
671         /* don't use this driver. Use serial_cs + hci_uart instead */
672         PCMCIA_DEVICE_NULL
673 };
674 MODULE_DEVICE_TABLE(pcmcia, btuart_ids);
675
676 static struct pcmcia_driver btuart_driver = {
677         .owner          = THIS_MODULE,
678         .name           = "btuart_cs",
679         .probe          = btuart_probe,
680         .remove         = btuart_detach,
681         .id_table       = btuart_ids,
682 };
683 module_pcmcia_driver(btuart_driver);