3 * Driver for the 3Com Bluetooth PCMCIA card
5 * Copyright (C) 2001-2002 Marcel Holtmann <marcel@holtmann.org>
6 * Jose Orlando Pereira <jop@di.uminho.pt>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation;
13 * Software distributed under the License is distributed on an "AS
14 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15 * implied. See the License for the specific language governing
16 * rights and limitations under the License.
18 * The initial developer of the original code is David A. Hinds
19 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
20 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
24 #include <linux/config.h>
25 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <linux/sched.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/ptrace.h>
35 #include <linux/ioport.h>
36 #include <linux/spinlock.h>
37 #include <linux/moduleparam.h>
39 #include <linux/skbuff.h>
40 #include <linux/string.h>
41 #include <linux/serial.h>
42 #include <linux/serial_reg.h>
43 #include <linux/bitops.h>
44 #include <asm/system.h>
47 #include <linux/device.h>
48 #include <linux/firmware.h>
50 #include <pcmcia/version.h>
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/ciscode.h>
55 #include <pcmcia/ds.h>
56 #include <pcmcia/cisreg.h>
58 #include <net/bluetooth/bluetooth.h>
59 #include <net/bluetooth/hci_core.h>
63 /* ======================== Module parameters ======================== */
66 /* Bit map of interrupts to choose from */
67 static unsigned int irq_mask = 0xffff;
68 static int irq_list[4] = { -1 };
70 module_param(irq_mask, uint, 0);
71 module_param_array(irq_list, int, NULL, 0);
73 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
74 MODULE_DESCRIPTION("Bluetooth driver for the 3Com Bluetooth PCMCIA card");
75 MODULE_LICENSE("GPL");
79 /* ======================== Local structures ======================== */
82 typedef struct bt3c_info_t {
88 spinlock_t lock; /* For serializing operations */
90 struct sk_buff_head txq;
91 unsigned long tx_state;
93 unsigned long rx_state;
94 unsigned long rx_count;
95 struct sk_buff *rx_skb;
99 static void bt3c_config(dev_link_t *link);
100 static void bt3c_release(dev_link_t *link);
101 static int bt3c_event(event_t event, int priority, event_callback_args_t *args);
103 static dev_info_t dev_info = "bt3c_cs";
105 static dev_link_t *bt3c_attach(void);
106 static void bt3c_detach(dev_link_t *);
108 static dev_link_t *dev_list = NULL;
111 /* Transmit states */
112 #define XMIT_SENDING 1
113 #define XMIT_WAKEUP 2
114 #define XMIT_WAITING 8
116 /* Receiver states */
117 #define RECV_WAIT_PACKET_TYPE 0
118 #define RECV_WAIT_EVENT_HEADER 1
119 #define RECV_WAIT_ACL_HEADER 2
120 #define RECV_WAIT_SCO_HEADER 3
121 #define RECV_WAIT_DATA 4
125 /* ======================== Special I/O functions ======================== */
135 static inline void bt3c_address(unsigned int iobase, unsigned short addr)
137 outb(addr & 0xff, iobase + ADDR_L);
138 outb((addr >> 8) & 0xff, iobase + ADDR_H);
142 static inline void bt3c_put(unsigned int iobase, unsigned short value)
144 outb(value & 0xff, iobase + DATA_L);
145 outb((value >> 8) & 0xff, iobase + DATA_H);
149 static inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
151 bt3c_address(iobase, addr);
152 bt3c_put(iobase, value);
156 static inline unsigned short bt3c_get(unsigned int iobase)
158 unsigned short value = inb(iobase + DATA_L);
160 value |= inb(iobase + DATA_H) << 8;
166 static inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
168 bt3c_address(iobase, addr);
170 return bt3c_get(iobase);
175 /* ======================== Interrupt handling ======================== */
178 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
182 bt3c_address(iobase, 0x7080);
184 /* Fill FIFO with current frame */
185 while (actual < len) {
186 /* Transmit next byte */
187 bt3c_put(iobase, buf[actual]);
191 bt3c_io_write(iobase, 0x7005, actual);
197 static void bt3c_write_wakeup(bt3c_info_t *info)
200 BT_ERR("Unknown device");
204 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
208 register unsigned int iobase = info->link.io.BasePort1;
209 register struct sk_buff *skb;
212 if (!(info->link.state & DEV_PRESENT))
216 if (!(skb = skb_dequeue(&(info->txq)))) {
217 clear_bit(XMIT_SENDING, &(info->tx_state));
222 len = bt3c_write(iobase, 256, skb->data, skb->len);
224 if (len != skb->len) {
225 BT_ERR("Very strange");
230 info->hdev->stat.byte_tx += len;
236 static void bt3c_receive(bt3c_info_t *info)
242 BT_ERR("Unknown device");
246 iobase = info->link.io.BasePort1;
248 avail = bt3c_read(iobase, 0x7006);
249 //printk("bt3c_cs: receiving %d bytes\n", avail);
251 bt3c_address(iobase, 0x7480);
252 while (size < avail) {
254 info->hdev->stat.byte_rx++;
256 /* Allocate packet */
257 if (info->rx_skb == NULL) {
258 info->rx_state = RECV_WAIT_PACKET_TYPE;
260 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
261 BT_ERR("Can't allocate mem for new packet");
267 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
269 info->rx_skb->dev = (void *) info->hdev;
270 info->rx_skb->pkt_type = inb(iobase + DATA_L);
271 inb(iobase + DATA_H);
272 //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
274 switch (info->rx_skb->pkt_type) {
277 info->rx_state = RECV_WAIT_EVENT_HEADER;
278 info->rx_count = HCI_EVENT_HDR_SIZE;
281 case HCI_ACLDATA_PKT:
282 info->rx_state = RECV_WAIT_ACL_HEADER;
283 info->rx_count = HCI_ACL_HDR_SIZE;
286 case HCI_SCODATA_PKT:
287 info->rx_state = RECV_WAIT_SCO_HEADER;
288 info->rx_count = HCI_SCO_HDR_SIZE;
293 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
294 info->hdev->stat.err_rx++;
295 clear_bit(HCI_RUNNING, &(info->hdev->flags));
297 kfree_skb(info->rx_skb);
305 __u8 x = inb(iobase + DATA_L);
307 *skb_put(info->rx_skb, 1) = x;
308 inb(iobase + DATA_H);
311 if (info->rx_count == 0) {
314 struct hci_event_hdr *eh;
315 struct hci_acl_hdr *ah;
316 struct hci_sco_hdr *sh;
318 switch (info->rx_state) {
320 case RECV_WAIT_EVENT_HEADER:
321 eh = (struct hci_event_hdr *)(info->rx_skb->data);
322 info->rx_state = RECV_WAIT_DATA;
323 info->rx_count = eh->plen;
326 case RECV_WAIT_ACL_HEADER:
327 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
328 dlen = __le16_to_cpu(ah->dlen);
329 info->rx_state = RECV_WAIT_DATA;
330 info->rx_count = dlen;
333 case RECV_WAIT_SCO_HEADER:
334 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
335 info->rx_state = RECV_WAIT_DATA;
336 info->rx_count = sh->dlen;
340 hci_recv_frame(info->rx_skb);
352 bt3c_io_write(iobase, 0x7006, 0x0000);
356 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
358 bt3c_info_t *info = dev_inst;
362 if (!info || !info->hdev) {
363 BT_ERR("Call of irq %d for unknown device", irq);
367 iobase = info->link.io.BasePort1;
369 spin_lock(&(info->lock));
371 iir = inb(iobase + CONTROL);
373 int stat = bt3c_read(iobase, 0x7001);
375 if ((stat & 0xff) == 0x7f) {
376 BT_ERR("Very strange (stat=0x%04x)", stat);
377 } else if ((stat & 0xff) != 0xff) {
379 int stat = bt3c_read(iobase, 0x7002) & 0x10;
380 BT_INFO("%s: Antenna %s", info->hdev->name,
381 stat ? "out" : "in");
386 //BT_ERR("Ack (stat=0x%04x)", stat);
387 clear_bit(XMIT_SENDING, &(info->tx_state));
388 bt3c_write_wakeup(info);
391 bt3c_io_write(iobase, 0x7001, 0x0000);
393 outb(iir, iobase + CONTROL);
397 spin_unlock(&(info->lock));
404 /* ======================== HCI interface ======================== */
407 static int bt3c_hci_flush(struct hci_dev *hdev)
409 bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
412 skb_queue_purge(&(info->txq));
418 static int bt3c_hci_open(struct hci_dev *hdev)
420 set_bit(HCI_RUNNING, &(hdev->flags));
426 static int bt3c_hci_close(struct hci_dev *hdev)
428 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
431 bt3c_hci_flush(hdev);
437 static int bt3c_hci_send_frame(struct sk_buff *skb)
440 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
444 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
448 info = (bt3c_info_t *) (hdev->driver_data);
450 switch (skb->pkt_type) {
451 case HCI_COMMAND_PKT:
454 case HCI_ACLDATA_PKT:
457 case HCI_SCODATA_PKT:
462 /* Prepend skb with frame type */
463 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
464 skb_queue_tail(&(info->txq), skb);
466 spin_lock_irqsave(&(info->lock), flags);
468 bt3c_write_wakeup(info);
470 spin_unlock_irqrestore(&(info->lock), flags);
476 static void bt3c_hci_destruct(struct hci_dev *hdev)
481 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
488 /* ======================== Card services HCI interaction ======================== */
491 static struct device *bt3c_device(void)
493 static struct device dev = {
496 kobject_set_name(&dev.kobj, "bt3c");
497 kobject_init(&dev.kobj);
503 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
505 char *ptr = (char *) firmware;
507 unsigned int iobase, size, addr, fcs, tmp;
510 iobase = info->link.io.BasePort1;
513 bt3c_io_write(iobase, 0x8040, 0x0404);
514 bt3c_io_write(iobase, 0x8040, 0x0400);
518 bt3c_io_write(iobase, 0x8040, 0x0404);
525 BT_ERR("Bad address in firmware");
530 memset(b, 0, sizeof(b));
531 memcpy(b, ptr + 2, 2);
532 size = simple_strtol(b, NULL, 16);
534 memset(b, 0, sizeof(b));
535 memcpy(b, ptr + 4, 8);
536 addr = simple_strtol(b, NULL, 16);
538 memset(b, 0, sizeof(b));
539 memcpy(b, ptr + (size * 2) + 2, 2);
540 fcs = simple_strtol(b, NULL, 16);
542 memset(b, 0, sizeof(b));
543 for (tmp = 0, i = 0; i < size; i++) {
544 memcpy(b, ptr + (i * 2) + 2, 2);
545 tmp += simple_strtol(b, NULL, 16);
548 if (((tmp + fcs) & 0xff) != 0xff) {
549 BT_ERR("Checksum error in firmware");
555 bt3c_address(iobase, addr);
557 memset(b, 0, sizeof(b));
558 for (i = 0; i < (size - 4) / 2; i++) {
559 memcpy(b, ptr + (i * 4) + 12, 4);
560 tmp = simple_strtol(b, NULL, 16);
561 bt3c_put(iobase, tmp);
565 ptr += (size * 2) + 6;
566 count -= (size * 2) + 6;
572 bt3c_address(iobase, 0x3000);
573 outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
579 bt3c_io_write(iobase, 0x7006, 0x0000);
580 bt3c_io_write(iobase, 0x7005, 0x0000);
581 bt3c_io_write(iobase, 0x7001, 0x0000);
587 static int bt3c_open(bt3c_info_t *info)
589 const struct firmware *firmware;
590 struct hci_dev *hdev;
593 spin_lock_init(&(info->lock));
595 skb_queue_head_init(&(info->txq));
597 info->rx_state = RECV_WAIT_PACKET_TYPE;
601 /* Initialize HCI device */
602 hdev = hci_alloc_dev();
604 BT_ERR("Can't allocate HCI device");
610 hdev->type = HCI_PCCARD;
611 hdev->driver_data = info;
613 hdev->open = bt3c_hci_open;
614 hdev->close = bt3c_hci_close;
615 hdev->flush = bt3c_hci_flush;
616 hdev->send = bt3c_hci_send_frame;
617 hdev->destruct = bt3c_hci_destruct;
618 hdev->ioctl = bt3c_hci_ioctl;
620 hdev->owner = THIS_MODULE;
623 err = request_firmware(&firmware, "BT3CPCC.bin", bt3c_device());
625 BT_ERR("Firmware request failed");
629 err = bt3c_load_firmware(info, firmware->data, firmware->size);
631 release_firmware(firmware);
634 BT_ERR("Firmware loading failed");
638 /* Timeout before it is safe to send the first HCI packet */
641 /* Register HCI device */
642 err = hci_register_dev(hdev);
644 BT_ERR("Can't register HCI device");
657 static int bt3c_close(bt3c_info_t *info)
659 struct hci_dev *hdev = info->hdev;
664 bt3c_hci_close(hdev);
666 if (hci_unregister_dev(hdev) < 0)
667 BT_ERR("Can't unregister HCI device %s", hdev->name);
674 static dev_link_t *bt3c_attach(void)
677 client_reg_t client_reg;
681 /* Create new info device */
682 info = kmalloc(sizeof(*info), GFP_KERNEL);
685 memset(info, 0, sizeof(*info));
690 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
691 link->io.NumPorts1 = 8;
692 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
693 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
695 if (irq_list[0] == -1)
696 link->irq.IRQInfo2 = irq_mask;
698 for (i = 0; i < 4; i++)
699 link->irq.IRQInfo2 |= 1 << irq_list[i];
701 link->irq.Handler = bt3c_interrupt;
702 link->irq.Instance = info;
704 link->conf.Attributes = CONF_ENABLE_IRQ;
706 link->conf.IntType = INT_MEMORY_AND_IO;
708 /* Register with Card Services */
709 link->next = dev_list;
711 client_reg.dev_info = &dev_info;
712 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
713 client_reg.EventMask =
714 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
715 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
716 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
717 client_reg.event_handler = &bt3c_event;
718 client_reg.Version = 0x0210;
719 client_reg.event_callback_args.client_data = link;
721 ret = pcmcia_register_client(&link->handle, &client_reg);
722 if (ret != CS_SUCCESS) {
723 cs_error(link->handle, RegisterClient, ret);
732 static void bt3c_detach(dev_link_t *link)
734 bt3c_info_t *info = link->priv;
738 /* Locate device structure */
739 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
746 if (link->state & DEV_CONFIG)
750 ret = pcmcia_deregister_client(link->handle);
751 if (ret != CS_SUCCESS)
752 cs_error(link->handle, DeregisterClient, ret);
755 /* Unlink device structure, free bits */
761 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
765 i = pcmcia_get_tuple_data(handle, tuple);
769 return pcmcia_parse_tuple(handle, tuple, parse);
772 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
774 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
775 return CS_NO_MORE_ITEMS;
776 return get_tuple(handle, tuple, parse);
779 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
781 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
782 return CS_NO_MORE_ITEMS;
783 return get_tuple(handle, tuple, parse);
786 static void bt3c_config(dev_link_t *link)
788 static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
789 client_handle_t handle = link->handle;
790 bt3c_info_t *info = link->priv;
794 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
795 config_info_t config;
796 int i, j, try, last_ret, last_fn;
798 tuple.TupleData = (cisdata_t *)buf;
799 tuple.TupleOffset = 0;
800 tuple.TupleDataMax = 255;
801 tuple.Attributes = 0;
803 /* Get configuration register information */
804 tuple.DesiredTuple = CISTPL_CONFIG;
805 last_ret = first_tuple(handle, &tuple, &parse);
806 if (last_ret != CS_SUCCESS) {
807 last_fn = ParseTuple;
810 link->conf.ConfigBase = parse.config.base;
811 link->conf.Present = parse.config.rmask[0];
814 link->state |= DEV_CONFIG;
815 i = pcmcia_get_configuration_info(handle, &config);
816 link->conf.Vcc = config.Vcc;
818 /* First pass: look for a config entry that looks normal. */
819 tuple.TupleData = (cisdata_t *)buf;
820 tuple.TupleOffset = 0;
821 tuple.TupleDataMax = 255;
822 tuple.Attributes = 0;
823 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
824 /* Two tries: without IO aliases, then with aliases */
825 for (try = 0; try < 2; try++) {
826 i = first_tuple(handle, &tuple, &parse);
827 while (i != CS_NO_MORE_ITEMS) {
830 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
831 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
832 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
833 link->conf.ConfigIndex = cf->index;
834 link->io.BasePort1 = cf->io.win[0].base;
835 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
836 i = pcmcia_request_io(link->handle, &link->io);
841 i = next_tuple(handle, &tuple, &parse);
845 /* Second pass: try to find an entry that isn't picky about
846 its base address, then try to grab any standard serial port
847 address, and finally try to get any free port. */
848 i = first_tuple(handle, &tuple, &parse);
849 while (i != CS_NO_MORE_ITEMS) {
850 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
851 link->conf.ConfigIndex = cf->index;
852 for (j = 0; j < 5; j++) {
853 link->io.BasePort1 = base[j];
854 link->io.IOAddrLines = base[j] ? 16 : 3;
855 i = pcmcia_request_io(link->handle, &link->io);
860 i = next_tuple(handle, &tuple, &parse);
864 if (i != CS_SUCCESS) {
865 BT_ERR("No usable port range found");
866 cs_error(link->handle, RequestIO, i);
870 i = pcmcia_request_irq(link->handle, &link->irq);
871 if (i != CS_SUCCESS) {
872 cs_error(link->handle, RequestIRQ, i);
873 link->irq.AssignedIRQ = 0;
876 i = pcmcia_request_configuration(link->handle, &link->conf);
877 if (i != CS_SUCCESS) {
878 cs_error(link->handle, RequestConfiguration, i);
882 if (bt3c_open(info) != 0)
885 strcpy(info->node.dev_name, info->hdev->name);
886 link->dev = &info->node;
887 link->state &= ~DEV_CONFIG_PENDING;
892 cs_error(link->handle, last_fn, last_ret);
899 static void bt3c_release(dev_link_t *link)
901 bt3c_info_t *info = link->priv;
903 if (link->state & DEV_PRESENT)
908 pcmcia_release_configuration(link->handle);
909 pcmcia_release_io(link->handle, &link->io);
910 pcmcia_release_irq(link->handle, &link->irq);
912 link->state &= ~DEV_CONFIG;
916 static int bt3c_event(event_t event, int priority, event_callback_args_t *args)
918 dev_link_t *link = args->client_data;
919 bt3c_info_t *info = link->priv;
922 case CS_EVENT_CARD_REMOVAL:
923 link->state &= ~DEV_PRESENT;
924 if (link->state & DEV_CONFIG) {
929 case CS_EVENT_CARD_INSERTION:
930 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
933 case CS_EVENT_PM_SUSPEND:
934 link->state |= DEV_SUSPEND;
935 /* Fall through... */
936 case CS_EVENT_RESET_PHYSICAL:
937 if (link->state & DEV_CONFIG)
938 pcmcia_release_configuration(link->handle);
940 case CS_EVENT_PM_RESUME:
941 link->state &= ~DEV_SUSPEND;
942 /* Fall through... */
943 case CS_EVENT_CARD_RESET:
945 pcmcia_request_configuration(link->handle, &link->conf);
952 static struct pcmcia_driver bt3c_driver = {
953 .owner = THIS_MODULE,
957 .attach = bt3c_attach,
958 .detach = bt3c_detach,
961 static int __init init_bt3c_cs(void)
963 return pcmcia_register_driver(&bt3c_driver);
967 static void __exit exit_bt3c_cs(void)
969 pcmcia_unregister_driver(&bt3c_driver);
971 /* XXX: this really needs to move into generic code.. */
972 while (dev_list != NULL)
973 bt3c_detach(dev_list);
976 module_init(init_bt3c_cs);
977 module_exit(exit_bt3c_cs);