ixgbe: use different context for tso and offload
[linux-2.6/mini2440.git] / drivers / bluetooth / btuart_cs.c
blob68d1d258e6a4f5f4e8064a061d374ef1b632dca3
1 /*
3 * Driver for Bluetooth PCMCIA cards with HCI UART interface
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/module.h>
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>
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>
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>
51 #include <net/bluetooth/bluetooth.h>
52 #include <net/bluetooth/hci_core.h>
56 /* ======================== Module parameters ======================== */
59 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
60 MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface");
61 MODULE_LICENSE("GPL");
65 /* ======================== Local structures ======================== */
68 typedef struct btuart_info_t {
69 struct pcmcia_device *p_dev;
70 dev_node_t node;
72 struct hci_dev *hdev;
74 spinlock_t lock; /* For serializing operations */
76 struct sk_buff_head txq;
77 unsigned long tx_state;
79 unsigned long rx_state;
80 unsigned long rx_count;
81 struct sk_buff *rx_skb;
82 } btuart_info_t;
85 static int btuart_config(struct pcmcia_device *link);
86 static void btuart_release(struct pcmcia_device *link);
88 static void btuart_detach(struct pcmcia_device *p_dev);
91 /* Maximum baud rate */
92 #define SPEED_MAX 115200
94 /* Default baud rate: 57600, 115200, 230400 or 460800 */
95 #define DEFAULT_BAUD_RATE 115200
98 /* Transmit states */
99 #define XMIT_SENDING 1
100 #define XMIT_WAKEUP 2
101 #define XMIT_WAITING 8
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
112 /* ======================== Interrupt handling ======================== */
115 static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
117 int actual = 0;
119 /* Tx FIFO should be empty */
120 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
121 return 0;
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++;
130 return actual;
134 static void btuart_write_wakeup(btuart_info_t *info)
136 if (!info) {
137 BT_ERR("Unknown device");
138 return;
141 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
142 set_bit(XMIT_WAKEUP, &(info->tx_state));
143 return;
146 do {
147 register unsigned int iobase = info->p_dev->io.BasePort1;
148 register struct sk_buff *skb;
149 register int len;
151 clear_bit(XMIT_WAKEUP, &(info->tx_state));
153 if (!pcmcia_dev_present(info->p_dev))
154 return;
156 if (!(skb = skb_dequeue(&(info->txq))))
157 break;
159 /* Send frame */
160 len = btuart_write(iobase, 16, skb->data, skb->len);
161 set_bit(XMIT_WAKEUP, &(info->tx_state));
163 if (len == skb->len) {
164 kfree_skb(skb);
165 } else {
166 skb_pull(skb, len);
167 skb_queue_head(&(info->txq), skb);
170 info->hdev->stat.byte_tx += len;
172 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
174 clear_bit(XMIT_SENDING, &(info->tx_state));
178 static void btuart_receive(btuart_info_t *info)
180 unsigned int iobase;
181 int boguscount = 0;
183 if (!info) {
184 BT_ERR("Unknown device");
185 return;
188 iobase = info->p_dev->io.BasePort1;
190 do {
191 info->hdev->stat.byte_rx++;
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;
203 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
205 info->rx_skb->dev = (void *) info->hdev;
206 bt_cb(info->rx_skb)->pkt_type = inb(iobase + UART_RX);
208 switch (bt_cb(info->rx_skb)->pkt_type) {
210 case HCI_EVENT_PKT:
211 info->rx_state = RECV_WAIT_EVENT_HEADER;
212 info->rx_count = HCI_EVENT_HDR_SIZE;
213 break;
215 case HCI_ACLDATA_PKT:
216 info->rx_state = RECV_WAIT_ACL_HEADER;
217 info->rx_count = HCI_ACL_HDR_SIZE;
218 break;
220 case HCI_SCODATA_PKT:
221 info->rx_state = RECV_WAIT_SCO_HEADER;
222 info->rx_count = HCI_SCO_HDR_SIZE;
223 break;
225 default:
226 /* Unknown packet */
227 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
228 info->hdev->stat.err_rx++;
229 clear_bit(HCI_RUNNING, &(info->hdev->flags));
231 kfree_skb(info->rx_skb);
232 info->rx_skb = NULL;
233 break;
237 } else {
239 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
240 info->rx_count--;
242 if (info->rx_count == 0) {
244 int dlen;
245 struct hci_event_hdr *eh;
246 struct hci_acl_hdr *ah;
247 struct hci_sco_hdr *sh;
250 switch (info->rx_state) {
252 case RECV_WAIT_EVENT_HEADER:
253 eh = hci_event_hdr(info->rx_skb);
254 info->rx_state = RECV_WAIT_DATA;
255 info->rx_count = eh->plen;
256 break;
258 case RECV_WAIT_ACL_HEADER:
259 ah = hci_acl_hdr(info->rx_skb);
260 dlen = __le16_to_cpu(ah->dlen);
261 info->rx_state = RECV_WAIT_DATA;
262 info->rx_count = dlen;
263 break;
265 case RECV_WAIT_SCO_HEADER:
266 sh = hci_sco_hdr(info->rx_skb);
267 info->rx_state = RECV_WAIT_DATA;
268 info->rx_count = sh->dlen;
269 break;
271 case RECV_WAIT_DATA:
272 hci_recv_frame(info->rx_skb);
273 info->rx_skb = NULL;
274 break;
282 /* Make sure we don't stay here too long */
283 if (boguscount++ > 16)
284 break;
286 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
290 static irqreturn_t btuart_interrupt(int irq, void *dev_inst)
292 btuart_info_t *info = dev_inst;
293 unsigned int iobase;
294 int boguscount = 0;
295 int iir, lsr;
297 BUG_ON(!info->hdev);
299 iobase = info->p_dev->io.BasePort1;
301 spin_lock(&(info->lock));
303 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
304 while (iir) {
306 /* Clear interrupt */
307 lsr = inb(iobase + UART_LSR);
309 switch (iir) {
310 case UART_IIR_RLSI:
311 BT_ERR("RLSI");
312 break;
313 case UART_IIR_RDI:
314 /* Receive interrupt */
315 btuart_receive(info);
316 break;
317 case UART_IIR_THRI:
318 if (lsr & UART_LSR_THRE) {
319 /* Transmitter ready for data */
320 btuart_write_wakeup(info);
322 break;
323 default:
324 BT_ERR("Unhandled IIR=%#x", iir);
325 break;
328 /* Make sure we don't stay here too long */
329 if (boguscount++ > 100)
330 break;
332 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
336 spin_unlock(&(info->lock));
338 return IRQ_HANDLED;
342 static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
344 unsigned long flags;
345 unsigned int iobase;
346 int fcr; /* FIFO control reg */
347 int lcr; /* Line control reg */
348 int divisor;
350 if (!info) {
351 BT_ERR("Unknown device");
352 return;
355 iobase = info->p_dev->io.BasePort1;
357 spin_lock_irqsave(&(info->lock), flags);
359 /* Turn off interrupts */
360 outb(0, iobase + UART_IER);
362 divisor = SPEED_MAX / speed;
364 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
367 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
368 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
369 * about this timeout since it will always be fast enough.
372 if (speed < 38400)
373 fcr |= UART_FCR_TRIGGER_1;
374 else
375 fcr |= UART_FCR_TRIGGER_14;
377 /* Bluetooth cards use 8N1 */
378 lcr = UART_LCR_WLEN8;
380 outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */
381 outb(divisor & 0xff, iobase + UART_DLL); /* Set speed */
382 outb(divisor >> 8, iobase + UART_DLM);
383 outb(lcr, iobase + UART_LCR); /* Set 8N1 */
384 outb(fcr, iobase + UART_FCR); /* Enable FIFO's */
386 /* Turn on interrupts */
387 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
389 spin_unlock_irqrestore(&(info->lock), flags);
394 /* ======================== HCI interface ======================== */
397 static int btuart_hci_flush(struct hci_dev *hdev)
399 btuart_info_t *info = (btuart_info_t *)(hdev->driver_data);
401 /* Drop TX queue */
402 skb_queue_purge(&(info->txq));
404 return 0;
408 static int btuart_hci_open(struct hci_dev *hdev)
410 set_bit(HCI_RUNNING, &(hdev->flags));
412 return 0;
416 static int btuart_hci_close(struct hci_dev *hdev)
418 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
419 return 0;
421 btuart_hci_flush(hdev);
423 return 0;
427 static int btuart_hci_send_frame(struct sk_buff *skb)
429 btuart_info_t *info;
430 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
432 if (!hdev) {
433 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
434 return -ENODEV;
437 info = (btuart_info_t *)(hdev->driver_data);
439 switch (bt_cb(skb)->pkt_type) {
440 case HCI_COMMAND_PKT:
441 hdev->stat.cmd_tx++;
442 break;
443 case HCI_ACLDATA_PKT:
444 hdev->stat.acl_tx++;
445 break;
446 case HCI_SCODATA_PKT:
447 hdev->stat.sco_tx++;
448 break;
451 /* Prepend skb with frame type */
452 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
453 skb_queue_tail(&(info->txq), skb);
455 btuart_write_wakeup(info);
457 return 0;
461 static void btuart_hci_destruct(struct hci_dev *hdev)
466 static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
468 return -ENOIOCTLCMD;
473 /* ======================== Card services HCI interaction ======================== */
476 static int btuart_open(btuart_info_t *info)
478 unsigned long flags;
479 unsigned int iobase = info->p_dev->io.BasePort1;
480 struct hci_dev *hdev;
482 spin_lock_init(&(info->lock));
484 skb_queue_head_init(&(info->txq));
486 info->rx_state = RECV_WAIT_PACKET_TYPE;
487 info->rx_count = 0;
488 info->rx_skb = NULL;
490 /* Initialize HCI device */
491 hdev = hci_alloc_dev();
492 if (!hdev) {
493 BT_ERR("Can't allocate HCI device");
494 return -ENOMEM;
497 info->hdev = hdev;
499 hdev->type = HCI_PCCARD;
500 hdev->driver_data = info;
501 SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
503 hdev->open = btuart_hci_open;
504 hdev->close = btuart_hci_close;
505 hdev->flush = btuart_hci_flush;
506 hdev->send = btuart_hci_send_frame;
507 hdev->destruct = btuart_hci_destruct;
508 hdev->ioctl = btuart_hci_ioctl;
510 hdev->owner = THIS_MODULE;
512 spin_lock_irqsave(&(info->lock), flags);
514 /* Reset UART */
515 outb(0, iobase + UART_MCR);
517 /* Turn off interrupts */
518 outb(0, iobase + UART_IER);
520 /* Initialize UART */
521 outb(UART_LCR_WLEN8, iobase + UART_LCR); /* Reset DLAB */
522 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
524 /* Turn on interrupts */
525 // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
527 spin_unlock_irqrestore(&(info->lock), flags);
529 btuart_change_speed(info, DEFAULT_BAUD_RATE);
531 /* Timeout before it is safe to send the first HCI packet */
532 msleep(1000);
534 /* Register HCI device */
535 if (hci_register_dev(hdev) < 0) {
536 BT_ERR("Can't register HCI device");
537 info->hdev = NULL;
538 hci_free_dev(hdev);
539 return -ENODEV;
542 return 0;
546 static int btuart_close(btuart_info_t *info)
548 unsigned long flags;
549 unsigned int iobase = info->p_dev->io.BasePort1;
550 struct hci_dev *hdev = info->hdev;
552 if (!hdev)
553 return -ENODEV;
555 btuart_hci_close(hdev);
557 spin_lock_irqsave(&(info->lock), flags);
559 /* Reset UART */
560 outb(0, iobase + UART_MCR);
562 /* Turn off interrupts */
563 outb(0, iobase + UART_IER);
565 spin_unlock_irqrestore(&(info->lock), flags);
567 if (hci_unregister_dev(hdev) < 0)
568 BT_ERR("Can't unregister HCI device %s", hdev->name);
570 hci_free_dev(hdev);
572 return 0;
575 static int btuart_probe(struct pcmcia_device *link)
577 btuart_info_t *info;
579 /* Create new info device */
580 info = kzalloc(sizeof(*info), GFP_KERNEL);
581 if (!info)
582 return -ENOMEM;
584 info->p_dev = link;
585 link->priv = info;
587 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
588 link->io.NumPorts1 = 8;
589 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
590 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
592 link->irq.Handler = btuart_interrupt;
593 link->irq.Instance = info;
595 link->conf.Attributes = CONF_ENABLE_IRQ;
596 link->conf.IntType = INT_MEMORY_AND_IO;
598 return btuart_config(link);
602 static void btuart_detach(struct pcmcia_device *link)
604 btuart_info_t *info = link->priv;
606 btuart_release(link);
607 kfree(info);
610 static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
612 int i;
614 i = pcmcia_get_tuple_data(handle, tuple);
615 if (i != CS_SUCCESS)
616 return i;
618 return pcmcia_parse_tuple(handle, tuple, parse);
621 static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
623 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
624 return CS_NO_MORE_ITEMS;
625 return get_tuple(handle, tuple, parse);
628 static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
630 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
631 return CS_NO_MORE_ITEMS;
632 return get_tuple(handle, tuple, parse);
635 static int btuart_config(struct pcmcia_device *link)
637 static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
638 btuart_info_t *info = link->priv;
639 tuple_t tuple;
640 u_short buf[256];
641 cisparse_t parse;
642 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
643 int i, j, try;
645 /* First pass: look for a config entry that looks normal. */
646 tuple.TupleData = (cisdata_t *) buf;
647 tuple.TupleOffset = 0;
648 tuple.TupleDataMax = 255;
649 tuple.Attributes = 0;
650 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
651 /* Two tries: without IO aliases, then with aliases */
652 for (try = 0; try < 2; try++) {
653 i = first_tuple(link, &tuple, &parse);
654 while (i != CS_NO_MORE_ITEMS) {
655 if (i != CS_SUCCESS)
656 goto next_entry;
657 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
658 link->conf.Vpp = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
659 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
660 link->conf.ConfigIndex = cf->index;
661 link->io.BasePort1 = cf->io.win[0].base;
662 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
663 i = pcmcia_request_io(link, &link->io);
664 if (i == CS_SUCCESS)
665 goto found_port;
667 next_entry:
668 i = next_tuple(link, &tuple, &parse);
672 /* Second pass: try to find an entry that isn't picky about
673 its base address, then try to grab any standard serial port
674 address, and finally try to get any free port. */
675 i = first_tuple(link, &tuple, &parse);
676 while (i != CS_NO_MORE_ITEMS) {
677 if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
678 && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
679 link->conf.ConfigIndex = cf->index;
680 for (j = 0; j < 5; j++) {
681 link->io.BasePort1 = base[j];
682 link->io.IOAddrLines = base[j] ? 16 : 3;
683 i = pcmcia_request_io(link, &link->io);
684 if (i == CS_SUCCESS)
685 goto found_port;
688 i = next_tuple(link, &tuple, &parse);
691 found_port:
692 if (i != CS_SUCCESS) {
693 BT_ERR("No usable port range found");
694 cs_error(link, RequestIO, i);
695 goto failed;
698 i = pcmcia_request_irq(link, &link->irq);
699 if (i != CS_SUCCESS) {
700 cs_error(link, RequestIRQ, i);
701 link->irq.AssignedIRQ = 0;
704 i = pcmcia_request_configuration(link, &link->conf);
705 if (i != CS_SUCCESS) {
706 cs_error(link, RequestConfiguration, i);
707 goto failed;
710 if (btuart_open(info) != 0)
711 goto failed;
713 strcpy(info->node.dev_name, info->hdev->name);
714 link->dev_node = &info->node;
716 return 0;
718 failed:
719 btuart_release(link);
720 return -ENODEV;
724 static void btuart_release(struct pcmcia_device *link)
726 btuart_info_t *info = link->priv;
728 btuart_close(info);
730 pcmcia_disable_device(link);
733 static struct pcmcia_device_id btuart_ids[] = {
734 /* don't use this driver. Use serial_cs + hci_uart instead */
735 PCMCIA_DEVICE_NULL
737 MODULE_DEVICE_TABLE(pcmcia, btuart_ids);
739 static struct pcmcia_driver btuart_driver = {
740 .owner = THIS_MODULE,
741 .drv = {
742 .name = "btuart_cs",
744 .probe = btuart_probe,
745 .remove = btuart_detach,
746 .id_table = btuart_ids,
749 static int __init init_btuart_cs(void)
751 return pcmcia_register_driver(&btuart_driver);
755 static void __exit exit_btuart_cs(void)
757 pcmcia_unregister_driver(&btuart_driver);
760 module_init(init_btuart_cs);
761 module_exit(exit_btuart_cs);