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/kmod.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/types.h>
33 #include <linux/sched.h>
34 #include <linux/errno.h>
35 #include <linux/ptrace.h>
36 #include <linux/ioport.h>
37 #include <linux/spinlock.h>
39 #include <linux/skbuff.h>
40 #include <linux/string.h>
41 #include <linux/serial.h>
42 #include <linux/serial_reg.h>
43 #include <asm/system.h>
44 #include <asm/bitops.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 u_int irq_mask = 0xffff;
68 static int irq_list[4] = { -1 };
70 MODULE_PARM(irq_mask, "i");
71 MODULE_PARM(irq_list, "1-4i");
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 void bt3c_config(dev_link_t *link);
100 void bt3c_release(dev_link_t *link);
101 int bt3c_event(event_t event, int priority, event_callback_args_t *args);
103 static dev_info_t dev_info = "bt3c_cs";
105 dev_link_t *bt3c_attach(void);
106 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 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 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 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 inline unsigned short bt3c_get(unsigned int iobase)
158 unsigned short value = inb(iobase + DATA_L);
160 value |= inb(iobase + DATA_H) << 8;
166 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, int from)
202 BT_ERR("Unknown device");
206 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
209 spin_lock_irqsave(&(info->lock), flags);
212 register unsigned int iobase = info->link.io.BasePort1;
213 register struct sk_buff *skb;
216 if (!(info->link.state & DEV_PRESENT))
220 if (!(skb = skb_dequeue(&(info->txq)))) {
221 clear_bit(XMIT_SENDING, &(info->tx_state));
226 len = bt3c_write(iobase, 256, skb->data, skb->len);
228 if (len != skb->len) {
229 BT_ERR("Very strange");
234 info->hdev->stat.byte_tx += len;
238 spin_unlock_irqrestore(&(info->lock), flags);
242 static void bt3c_receive(bt3c_info_t *info)
248 BT_ERR("Unknown device");
252 iobase = info->link.io.BasePort1;
254 avail = bt3c_read(iobase, 0x7006);
255 //printk("bt3c_cs: receiving %d bytes\n", avail);
257 bt3c_address(iobase, 0x7480);
258 while (size < avail) {
260 info->hdev->stat.byte_rx++;
262 /* Allocate packet */
263 if (info->rx_skb == NULL) {
264 info->rx_state = RECV_WAIT_PACKET_TYPE;
266 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
267 BT_ERR("Can't allocate mem for new packet");
273 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
275 info->rx_skb->dev = (void *) info->hdev;
276 info->rx_skb->pkt_type = inb(iobase + DATA_L);
277 inb(iobase + DATA_H);
278 //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
280 switch (info->rx_skb->pkt_type) {
283 info->rx_state = RECV_WAIT_EVENT_HEADER;
284 info->rx_count = HCI_EVENT_HDR_SIZE;
287 case HCI_ACLDATA_PKT:
288 info->rx_state = RECV_WAIT_ACL_HEADER;
289 info->rx_count = HCI_ACL_HDR_SIZE;
292 case HCI_SCODATA_PKT:
293 info->rx_state = RECV_WAIT_SCO_HEADER;
294 info->rx_count = HCI_SCO_HDR_SIZE;
299 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
300 info->hdev->stat.err_rx++;
301 clear_bit(HCI_RUNNING, &(info->hdev->flags));
303 kfree_skb(info->rx_skb);
311 __u8 x = inb(iobase + DATA_L);
313 *skb_put(info->rx_skb, 1) = x;
314 inb(iobase + DATA_H);
317 if (info->rx_count == 0) {
320 struct hci_event_hdr *eh;
321 struct hci_acl_hdr *ah;
322 struct hci_sco_hdr *sh;
324 switch (info->rx_state) {
326 case RECV_WAIT_EVENT_HEADER:
327 eh = (struct hci_event_hdr *)(info->rx_skb->data);
328 info->rx_state = RECV_WAIT_DATA;
329 info->rx_count = eh->plen;
332 case RECV_WAIT_ACL_HEADER:
333 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
334 dlen = __le16_to_cpu(ah->dlen);
335 info->rx_state = RECV_WAIT_DATA;
336 info->rx_count = dlen;
339 case RECV_WAIT_SCO_HEADER:
340 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
341 info->rx_state = RECV_WAIT_DATA;
342 info->rx_count = sh->dlen;
346 hci_recv_frame(info->rx_skb);
358 bt3c_io_write(iobase, 0x7006, 0x0000);
362 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
364 bt3c_info_t *info = dev_inst;
368 if (!info || !info->hdev) {
369 BT_ERR("Call of irq %d for unknown device", irq);
373 iobase = info->link.io.BasePort1;
375 spin_lock(&(info->lock));
377 iir = inb(iobase + CONTROL);
379 int stat = bt3c_read(iobase, 0x7001);
381 if ((stat & 0xff) == 0x7f) {
382 BT_ERR("Very strange (stat=0x%04x)", stat);
383 } else if ((stat & 0xff) != 0xff) {
385 int stat = bt3c_read(iobase, 0x7002) & 0x10;
386 BT_INFO("%s: Antenna %s", info->hdev->name,
387 stat ? "out" : "in");
392 //BT_ERR("Ack (stat=0x%04x)", stat);
393 clear_bit(XMIT_SENDING, &(info->tx_state));
394 bt3c_write_wakeup(info, 1);
397 bt3c_io_write(iobase, 0x7001, 0x0000);
399 outb(iir, iobase + CONTROL);
403 spin_unlock(&(info->lock));
410 /* ======================== HCI interface ======================== */
413 static int bt3c_hci_flush(struct hci_dev *hdev)
415 bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
418 skb_queue_purge(&(info->txq));
424 static int bt3c_hci_open(struct hci_dev *hdev)
426 set_bit(HCI_RUNNING, &(hdev->flags));
432 static int bt3c_hci_close(struct hci_dev *hdev)
434 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
437 bt3c_hci_flush(hdev);
443 static int bt3c_hci_send_frame(struct sk_buff *skb)
446 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
449 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
453 info = (bt3c_info_t *) (hdev->driver_data);
455 switch (skb->pkt_type) {
456 case HCI_COMMAND_PKT:
459 case HCI_ACLDATA_PKT:
462 case HCI_SCODATA_PKT:
467 /* Prepend skb with frame type */
468 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
469 skb_queue_tail(&(info->txq), skb);
471 bt3c_write_wakeup(info, 0);
477 static void bt3c_hci_destruct(struct hci_dev *hdev)
482 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
489 /* ======================== Card services HCI interaction ======================== */
492 static struct device bt3c_device = {
497 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
499 char *ptr = (char *) firmware;
501 unsigned int iobase, size, addr, fcs, tmp;
504 iobase = info->link.io.BasePort1;
507 bt3c_io_write(iobase, 0x8040, 0x0404);
508 bt3c_io_write(iobase, 0x8040, 0x0400);
512 bt3c_io_write(iobase, 0x8040, 0x0404);
519 BT_ERR("Bad address in firmware");
524 memset(b, 0, sizeof(b));
525 memcpy(b, ptr + 2, 2);
526 size = simple_strtol(b, NULL, 16);
528 memset(b, 0, sizeof(b));
529 memcpy(b, ptr + 4, 8);
530 addr = simple_strtol(b, NULL, 16);
532 memset(b, 0, sizeof(b));
533 memcpy(b, ptr + (size * 2) + 2, 2);
534 fcs = simple_strtol(b, NULL, 16);
536 memset(b, 0, sizeof(b));
537 for (tmp = 0, i = 0; i < size; i++) {
538 memcpy(b, ptr + (i * 2) + 2, 2);
539 tmp += simple_strtol(b, NULL, 16);
542 if (((tmp + fcs) & 0xff) != 0xff) {
543 BT_ERR("Checksum error in firmware");
549 bt3c_address(iobase, addr);
551 memset(b, 0, sizeof(b));
552 for (i = 0; i < (size - 4) / 2; i++) {
553 memcpy(b, ptr + (i * 4) + 12, 4);
554 tmp = simple_strtol(b, NULL, 16);
555 bt3c_put(iobase, tmp);
559 ptr += (size * 2) + 6;
560 count -= (size * 2) + 6;
566 bt3c_address(iobase, 0x3000);
567 outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
573 bt3c_io_write(iobase, 0x7006, 0x0000);
574 bt3c_io_write(iobase, 0x7005, 0x0000);
575 bt3c_io_write(iobase, 0x7001, 0x0000);
581 int bt3c_open(bt3c_info_t *info)
583 const struct firmware *firmware;
584 struct hci_dev *hdev;
587 spin_lock_init(&(info->lock));
589 skb_queue_head_init(&(info->txq));
591 info->rx_state = RECV_WAIT_PACKET_TYPE;
595 /* Initialize HCI device */
596 hdev = hci_alloc_dev();
598 BT_ERR("Can't allocate HCI device");
604 hdev->type = HCI_PCCARD;
605 hdev->driver_data = info;
607 hdev->open = bt3c_hci_open;
608 hdev->close = bt3c_hci_close;
609 hdev->flush = bt3c_hci_flush;
610 hdev->send = bt3c_hci_send_frame;
611 hdev->destruct = bt3c_hci_destruct;
612 hdev->ioctl = bt3c_hci_ioctl;
614 hdev->owner = THIS_MODULE;
617 err = request_firmware(&firmware, "BT3CPCC.bin", &bt3c_device);
619 BT_ERR("Firmware request failed");
623 err = bt3c_load_firmware(info, firmware->data, firmware->size);
625 release_firmware(firmware);
628 BT_ERR("Firmware loading failed");
632 /* Timeout before it is safe to send the first HCI packet */
633 set_current_state(TASK_INTERRUPTIBLE);
634 schedule_timeout(HZ);
636 /* Register HCI device */
637 err = hci_register_dev(hdev);
639 BT_ERR("Can't register HCI device");
652 int bt3c_close(bt3c_info_t *info)
654 struct hci_dev *hdev = info->hdev;
659 bt3c_hci_close(hdev);
661 if (hci_unregister_dev(hdev) < 0)
662 BT_ERR("Can't unregister HCI device %s", hdev->name);
669 dev_link_t *bt3c_attach(void)
672 client_reg_t client_reg;
676 /* Create new info device */
677 info = kmalloc(sizeof(*info), GFP_KERNEL);
680 memset(info, 0, sizeof(*info));
685 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
686 link->io.NumPorts1 = 8;
687 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
688 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
690 if (irq_list[0] == -1)
691 link->irq.IRQInfo2 = irq_mask;
693 for (i = 0; i < 4; i++)
694 link->irq.IRQInfo2 |= 1 << irq_list[i];
696 link->irq.Handler = bt3c_interrupt;
697 link->irq.Instance = info;
699 link->conf.Attributes = CONF_ENABLE_IRQ;
701 link->conf.IntType = INT_MEMORY_AND_IO;
703 /* Register with Card Services */
704 link->next = dev_list;
706 client_reg.dev_info = &dev_info;
707 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
708 client_reg.EventMask =
709 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
710 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
711 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
712 client_reg.event_handler = &bt3c_event;
713 client_reg.Version = 0x0210;
714 client_reg.event_callback_args.client_data = link;
716 ret = pcmcia_register_client(&link->handle, &client_reg);
717 if (ret != CS_SUCCESS) {
718 cs_error(link->handle, RegisterClient, ret);
727 void bt3c_detach(dev_link_t *link)
729 bt3c_info_t *info = link->priv;
733 /* Locate device structure */
734 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
741 if (link->state & DEV_CONFIG)
745 ret = pcmcia_deregister_client(link->handle);
746 if (ret != CS_SUCCESS)
747 cs_error(link->handle, DeregisterClient, ret);
750 /* Unlink device structure, free bits */
756 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
760 i = pcmcia_get_tuple_data(handle, tuple);
764 return pcmcia_parse_tuple(handle, tuple, parse);
767 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
769 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
770 return CS_NO_MORE_ITEMS;
771 return get_tuple(handle, tuple, parse);
774 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
776 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
777 return CS_NO_MORE_ITEMS;
778 return get_tuple(handle, tuple, parse);
781 void bt3c_config(dev_link_t *link)
783 static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
784 client_handle_t handle = link->handle;
785 bt3c_info_t *info = link->priv;
789 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
790 config_info_t config;
791 int i, j, try, last_ret, last_fn;
793 tuple.TupleData = (cisdata_t *)buf;
794 tuple.TupleOffset = 0;
795 tuple.TupleDataMax = 255;
796 tuple.Attributes = 0;
798 /* Get configuration register information */
799 tuple.DesiredTuple = CISTPL_CONFIG;
800 last_ret = first_tuple(handle, &tuple, &parse);
801 if (last_ret != CS_SUCCESS) {
802 last_fn = ParseTuple;
805 link->conf.ConfigBase = parse.config.base;
806 link->conf.Present = parse.config.rmask[0];
809 link->state |= DEV_CONFIG;
810 i = pcmcia_get_configuration_info(handle, &config);
811 link->conf.Vcc = config.Vcc;
813 /* First pass: look for a config entry that looks normal. */
814 tuple.TupleData = (cisdata_t *)buf;
815 tuple.TupleOffset = 0;
816 tuple.TupleDataMax = 255;
817 tuple.Attributes = 0;
818 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
819 /* Two tries: without IO aliases, then with aliases */
820 for (try = 0; try < 2; try++) {
821 i = first_tuple(handle, &tuple, &parse);
822 while (i != CS_NO_MORE_ITEMS) {
825 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
826 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
827 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
828 link->conf.ConfigIndex = cf->index;
829 link->io.BasePort1 = cf->io.win[0].base;
830 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
831 i = pcmcia_request_io(link->handle, &link->io);
836 i = next_tuple(handle, &tuple, &parse);
840 /* Second pass: try to find an entry that isn't picky about
841 its base address, then try to grab any standard serial port
842 address, and finally try to get any free port. */
843 i = first_tuple(handle, &tuple, &parse);
844 while (i != CS_NO_MORE_ITEMS) {
845 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
846 link->conf.ConfigIndex = cf->index;
847 for (j = 0; j < 5; j++) {
848 link->io.BasePort1 = base[j];
849 link->io.IOAddrLines = base[j] ? 16 : 3;
850 i = pcmcia_request_io(link->handle, &link->io);
855 i = next_tuple(handle, &tuple, &parse);
859 if (i != CS_SUCCESS) {
860 BT_ERR("No usable port range found");
861 cs_error(link->handle, RequestIO, i);
865 i = pcmcia_request_irq(link->handle, &link->irq);
866 if (i != CS_SUCCESS) {
867 cs_error(link->handle, RequestIRQ, i);
868 link->irq.AssignedIRQ = 0;
871 i = pcmcia_request_configuration(link->handle, &link->conf);
872 if (i != CS_SUCCESS) {
873 cs_error(link->handle, RequestConfiguration, i);
877 if (bt3c_open(info) != 0)
880 strcpy(info->node.dev_name, info->hdev->name);
881 link->dev = &info->node;
882 link->state &= ~DEV_CONFIG_PENDING;
887 cs_error(link->handle, last_fn, last_ret);
894 void bt3c_release(dev_link_t *link)
896 bt3c_info_t *info = link->priv;
898 if (link->state & DEV_PRESENT)
903 pcmcia_release_configuration(link->handle);
904 pcmcia_release_io(link->handle, &link->io);
905 pcmcia_release_irq(link->handle, &link->irq);
907 link->state &= ~DEV_CONFIG;
911 int bt3c_event(event_t event, int priority, event_callback_args_t *args)
913 dev_link_t *link = args->client_data;
914 bt3c_info_t *info = link->priv;
917 case CS_EVENT_CARD_REMOVAL:
918 link->state &= ~DEV_PRESENT;
919 if (link->state & DEV_CONFIG) {
924 case CS_EVENT_CARD_INSERTION:
925 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
928 case CS_EVENT_PM_SUSPEND:
929 link->state |= DEV_SUSPEND;
930 /* Fall through... */
931 case CS_EVENT_RESET_PHYSICAL:
932 if (link->state & DEV_CONFIG)
933 pcmcia_release_configuration(link->handle);
935 case CS_EVENT_PM_RESUME:
936 link->state &= ~DEV_SUSPEND;
937 /* Fall through... */
938 case CS_EVENT_CARD_RESET:
940 pcmcia_request_configuration(link->handle, &link->conf);
947 static struct pcmcia_driver bt3c_driver = {
948 .owner = THIS_MODULE,
952 .attach = bt3c_attach,
953 .detach = bt3c_detach,
956 static int __init init_bt3c_cs(void)
958 return pcmcia_register_driver(&bt3c_driver);
962 static void __exit exit_bt3c_cs(void)
964 pcmcia_unregister_driver(&bt3c_driver);
966 /* XXX: this really needs to move into generic code.. */
967 while (dev_list != NULL)
968 bt3c_detach(dev_list);
971 module_init(init_bt3c_cs);
972 module_exit(exit_bt3c_cs);