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