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/cs_types.h>
51 #include <pcmcia/cs.h>
52 #include <pcmcia/cistpl.h>
53 #include <pcmcia/ciscode.h>
54 #include <pcmcia/ds.h>
55 #include <pcmcia/cisreg.h>
57 #include <net/bluetooth/bluetooth.h>
58 #include <net/bluetooth/hci_core.h>
62 /* ======================== Module parameters ======================== */
65 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
66 MODULE_DESCRIPTION("Bluetooth driver for the 3Com Bluetooth PCMCIA card");
67 MODULE_LICENSE("GPL");
71 /* ======================== Local structures ======================== */
74 typedef struct bt3c_info_t {
80 spinlock_t lock; /* For serializing operations */
82 struct sk_buff_head txq;
83 unsigned long tx_state;
85 unsigned long rx_state;
86 unsigned long rx_count;
87 struct sk_buff *rx_skb;
91 static void bt3c_config(dev_link_t *link);
92 static void bt3c_release(dev_link_t *link);
93 static int bt3c_event(event_t event, int priority, event_callback_args_t *args);
95 static dev_info_t dev_info = "bt3c_cs";
97 static dev_link_t *bt3c_attach(void);
98 static void bt3c_detach(dev_link_t *);
100 static dev_link_t *dev_list = NULL;
103 /* Transmit states */
104 #define XMIT_SENDING 1
105 #define XMIT_WAKEUP 2
106 #define XMIT_WAITING 8
108 /* Receiver states */
109 #define RECV_WAIT_PACKET_TYPE 0
110 #define RECV_WAIT_EVENT_HEADER 1
111 #define RECV_WAIT_ACL_HEADER 2
112 #define RECV_WAIT_SCO_HEADER 3
113 #define RECV_WAIT_DATA 4
117 /* ======================== Special I/O functions ======================== */
127 static inline void bt3c_address(unsigned int iobase, unsigned short addr)
129 outb(addr & 0xff, iobase + ADDR_L);
130 outb((addr >> 8) & 0xff, iobase + ADDR_H);
134 static inline void bt3c_put(unsigned int iobase, unsigned short value)
136 outb(value & 0xff, iobase + DATA_L);
137 outb((value >> 8) & 0xff, iobase + DATA_H);
141 static inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
143 bt3c_address(iobase, addr);
144 bt3c_put(iobase, value);
148 static inline unsigned short bt3c_get(unsigned int iobase)
150 unsigned short value = inb(iobase + DATA_L);
152 value |= inb(iobase + DATA_H) << 8;
158 static inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
160 bt3c_address(iobase, addr);
162 return bt3c_get(iobase);
167 /* ======================== Interrupt handling ======================== */
170 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
174 bt3c_address(iobase, 0x7080);
176 /* Fill FIFO with current frame */
177 while (actual < len) {
178 /* Transmit next byte */
179 bt3c_put(iobase, buf[actual]);
183 bt3c_io_write(iobase, 0x7005, actual);
189 static void bt3c_write_wakeup(bt3c_info_t *info)
192 BT_ERR("Unknown device");
196 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
200 register unsigned int iobase = info->link.io.BasePort1;
201 register struct sk_buff *skb;
204 if (!(info->link.state & DEV_PRESENT))
208 if (!(skb = skb_dequeue(&(info->txq)))) {
209 clear_bit(XMIT_SENDING, &(info->tx_state));
214 len = bt3c_write(iobase, 256, skb->data, skb->len);
216 if (len != skb->len) {
217 BT_ERR("Very strange");
222 info->hdev->stat.byte_tx += len;
228 static void bt3c_receive(bt3c_info_t *info)
234 BT_ERR("Unknown device");
238 iobase = info->link.io.BasePort1;
240 avail = bt3c_read(iobase, 0x7006);
241 //printk("bt3c_cs: receiving %d bytes\n", avail);
243 bt3c_address(iobase, 0x7480);
244 while (size < avail) {
246 info->hdev->stat.byte_rx++;
248 /* Allocate packet */
249 if (info->rx_skb == NULL) {
250 info->rx_state = RECV_WAIT_PACKET_TYPE;
252 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
253 BT_ERR("Can't allocate mem for new packet");
259 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
261 info->rx_skb->dev = (void *) info->hdev;
262 info->rx_skb->pkt_type = inb(iobase + DATA_L);
263 inb(iobase + DATA_H);
264 //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
266 switch (info->rx_skb->pkt_type) {
269 info->rx_state = RECV_WAIT_EVENT_HEADER;
270 info->rx_count = HCI_EVENT_HDR_SIZE;
273 case HCI_ACLDATA_PKT:
274 info->rx_state = RECV_WAIT_ACL_HEADER;
275 info->rx_count = HCI_ACL_HDR_SIZE;
278 case HCI_SCODATA_PKT:
279 info->rx_state = RECV_WAIT_SCO_HEADER;
280 info->rx_count = HCI_SCO_HDR_SIZE;
285 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
286 info->hdev->stat.err_rx++;
287 clear_bit(HCI_RUNNING, &(info->hdev->flags));
289 kfree_skb(info->rx_skb);
297 __u8 x = inb(iobase + DATA_L);
299 *skb_put(info->rx_skb, 1) = x;
300 inb(iobase + DATA_H);
303 if (info->rx_count == 0) {
306 struct hci_event_hdr *eh;
307 struct hci_acl_hdr *ah;
308 struct hci_sco_hdr *sh;
310 switch (info->rx_state) {
312 case RECV_WAIT_EVENT_HEADER:
313 eh = (struct hci_event_hdr *)(info->rx_skb->data);
314 info->rx_state = RECV_WAIT_DATA;
315 info->rx_count = eh->plen;
318 case RECV_WAIT_ACL_HEADER:
319 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
320 dlen = __le16_to_cpu(ah->dlen);
321 info->rx_state = RECV_WAIT_DATA;
322 info->rx_count = dlen;
325 case RECV_WAIT_SCO_HEADER:
326 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
327 info->rx_state = RECV_WAIT_DATA;
328 info->rx_count = sh->dlen;
332 hci_recv_frame(info->rx_skb);
344 bt3c_io_write(iobase, 0x7006, 0x0000);
348 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
350 bt3c_info_t *info = dev_inst;
354 if (!info || !info->hdev) {
355 BT_ERR("Call of irq %d for unknown device", irq);
359 iobase = info->link.io.BasePort1;
361 spin_lock(&(info->lock));
363 iir = inb(iobase + CONTROL);
365 int stat = bt3c_read(iobase, 0x7001);
367 if ((stat & 0xff) == 0x7f) {
368 BT_ERR("Very strange (stat=0x%04x)", stat);
369 } else if ((stat & 0xff) != 0xff) {
371 int stat = bt3c_read(iobase, 0x7002) & 0x10;
372 BT_INFO("%s: Antenna %s", info->hdev->name,
373 stat ? "out" : "in");
378 //BT_ERR("Ack (stat=0x%04x)", stat);
379 clear_bit(XMIT_SENDING, &(info->tx_state));
380 bt3c_write_wakeup(info);
383 bt3c_io_write(iobase, 0x7001, 0x0000);
385 outb(iir, iobase + CONTROL);
389 spin_unlock(&(info->lock));
396 /* ======================== HCI interface ======================== */
399 static int bt3c_hci_flush(struct hci_dev *hdev)
401 bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
404 skb_queue_purge(&(info->txq));
410 static int bt3c_hci_open(struct hci_dev *hdev)
412 set_bit(HCI_RUNNING, &(hdev->flags));
418 static int bt3c_hci_close(struct hci_dev *hdev)
420 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
423 bt3c_hci_flush(hdev);
429 static int bt3c_hci_send_frame(struct sk_buff *skb)
432 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
436 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
440 info = (bt3c_info_t *) (hdev->driver_data);
442 switch (skb->pkt_type) {
443 case HCI_COMMAND_PKT:
446 case HCI_ACLDATA_PKT:
449 case HCI_SCODATA_PKT:
454 /* Prepend skb with frame type */
455 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
456 skb_queue_tail(&(info->txq), skb);
458 spin_lock_irqsave(&(info->lock), flags);
460 bt3c_write_wakeup(info);
462 spin_unlock_irqrestore(&(info->lock), flags);
468 static void bt3c_hci_destruct(struct hci_dev *hdev)
473 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
480 /* ======================== Card services HCI interaction ======================== */
483 static struct device *bt3c_device(void)
485 static struct device dev = {
488 kobject_set_name(&dev.kobj, "bt3c");
489 kobject_init(&dev.kobj);
495 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
497 char *ptr = (char *) firmware;
499 unsigned int iobase, size, addr, fcs, tmp;
502 iobase = info->link.io.BasePort1;
505 bt3c_io_write(iobase, 0x8040, 0x0404);
506 bt3c_io_write(iobase, 0x8040, 0x0400);
510 bt3c_io_write(iobase, 0x8040, 0x0404);
517 BT_ERR("Bad address in firmware");
522 memset(b, 0, sizeof(b));
523 memcpy(b, ptr + 2, 2);
524 size = simple_strtol(b, NULL, 16);
526 memset(b, 0, sizeof(b));
527 memcpy(b, ptr + 4, 8);
528 addr = simple_strtol(b, NULL, 16);
530 memset(b, 0, sizeof(b));
531 memcpy(b, ptr + (size * 2) + 2, 2);
532 fcs = simple_strtol(b, NULL, 16);
534 memset(b, 0, sizeof(b));
535 for (tmp = 0, i = 0; i < size; i++) {
536 memcpy(b, ptr + (i * 2) + 2, 2);
537 tmp += simple_strtol(b, NULL, 16);
540 if (((tmp + fcs) & 0xff) != 0xff) {
541 BT_ERR("Checksum error in firmware");
547 bt3c_address(iobase, addr);
549 memset(b, 0, sizeof(b));
550 for (i = 0; i < (size - 4) / 2; i++) {
551 memcpy(b, ptr + (i * 4) + 12, 4);
552 tmp = simple_strtol(b, NULL, 16);
553 bt3c_put(iobase, tmp);
557 ptr += (size * 2) + 6;
558 count -= (size * 2) + 6;
564 bt3c_address(iobase, 0x3000);
565 outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
571 bt3c_io_write(iobase, 0x7006, 0x0000);
572 bt3c_io_write(iobase, 0x7005, 0x0000);
573 bt3c_io_write(iobase, 0x7001, 0x0000);
579 static int bt3c_open(bt3c_info_t *info)
581 const struct firmware *firmware;
582 struct hci_dev *hdev;
585 spin_lock_init(&(info->lock));
587 skb_queue_head_init(&(info->txq));
589 info->rx_state = RECV_WAIT_PACKET_TYPE;
593 /* Initialize HCI device */
594 hdev = hci_alloc_dev();
596 BT_ERR("Can't allocate HCI device");
602 hdev->type = HCI_PCCARD;
603 hdev->driver_data = info;
605 hdev->open = bt3c_hci_open;
606 hdev->close = bt3c_hci_close;
607 hdev->flush = bt3c_hci_flush;
608 hdev->send = bt3c_hci_send_frame;
609 hdev->destruct = bt3c_hci_destruct;
610 hdev->ioctl = bt3c_hci_ioctl;
612 hdev->owner = THIS_MODULE;
615 err = request_firmware(&firmware, "BT3CPCC.bin", bt3c_device());
617 BT_ERR("Firmware request failed");
621 err = bt3c_load_firmware(info, firmware->data, firmware->size);
623 release_firmware(firmware);
626 BT_ERR("Firmware loading failed");
630 /* Timeout before it is safe to send the first HCI packet */
633 /* Register HCI device */
634 err = hci_register_dev(hdev);
636 BT_ERR("Can't register HCI device");
649 static int bt3c_close(bt3c_info_t *info)
651 struct hci_dev *hdev = info->hdev;
656 bt3c_hci_close(hdev);
658 if (hci_unregister_dev(hdev) < 0)
659 BT_ERR("Can't unregister HCI device %s", hdev->name);
666 static dev_link_t *bt3c_attach(void)
669 client_reg_t client_reg;
673 /* Create new info device */
674 info = kmalloc(sizeof(*info), GFP_KERNEL);
677 memset(info, 0, sizeof(*info));
682 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
683 link->io.NumPorts1 = 8;
684 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
685 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
687 link->irq.Handler = bt3c_interrupt;
688 link->irq.Instance = info;
690 link->conf.Attributes = CONF_ENABLE_IRQ;
692 link->conf.IntType = INT_MEMORY_AND_IO;
694 /* Register with Card Services */
695 link->next = dev_list;
697 client_reg.dev_info = &dev_info;
698 client_reg.Version = 0x0210;
699 client_reg.event_callback_args.client_data = link;
701 ret = pcmcia_register_client(&link->handle, &client_reg);
702 if (ret != CS_SUCCESS) {
703 cs_error(link->handle, RegisterClient, ret);
712 static void bt3c_detach(dev_link_t *link)
714 bt3c_info_t *info = link->priv;
718 /* Locate device structure */
719 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
726 if (link->state & DEV_CONFIG)
730 ret = pcmcia_deregister_client(link->handle);
731 if (ret != CS_SUCCESS)
732 cs_error(link->handle, DeregisterClient, ret);
735 /* Unlink device structure, free bits */
741 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
745 i = pcmcia_get_tuple_data(handle, tuple);
749 return pcmcia_parse_tuple(handle, tuple, parse);
752 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
754 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
755 return CS_NO_MORE_ITEMS;
756 return get_tuple(handle, tuple, parse);
759 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
761 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
762 return CS_NO_MORE_ITEMS;
763 return get_tuple(handle, tuple, parse);
766 static void bt3c_config(dev_link_t *link)
768 static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
769 client_handle_t handle = link->handle;
770 bt3c_info_t *info = link->priv;
774 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
775 config_info_t config;
776 int i, j, try, last_ret, last_fn;
778 tuple.TupleData = (cisdata_t *)buf;
779 tuple.TupleOffset = 0;
780 tuple.TupleDataMax = 255;
781 tuple.Attributes = 0;
783 /* Get configuration register information */
784 tuple.DesiredTuple = CISTPL_CONFIG;
785 last_ret = first_tuple(handle, &tuple, &parse);
786 if (last_ret != CS_SUCCESS) {
787 last_fn = ParseTuple;
790 link->conf.ConfigBase = parse.config.base;
791 link->conf.Present = parse.config.rmask[0];
794 link->state |= DEV_CONFIG;
795 i = pcmcia_get_configuration_info(handle, &config);
796 link->conf.Vcc = config.Vcc;
798 /* First pass: look for a config entry that looks normal. */
799 tuple.TupleData = (cisdata_t *)buf;
800 tuple.TupleOffset = 0;
801 tuple.TupleDataMax = 255;
802 tuple.Attributes = 0;
803 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
804 /* Two tries: without IO aliases, then with aliases */
805 for (try = 0; try < 2; try++) {
806 i = first_tuple(handle, &tuple, &parse);
807 while (i != CS_NO_MORE_ITEMS) {
810 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
811 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
812 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
813 link->conf.ConfigIndex = cf->index;
814 link->io.BasePort1 = cf->io.win[0].base;
815 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
816 i = pcmcia_request_io(link->handle, &link->io);
821 i = next_tuple(handle, &tuple, &parse);
825 /* Second pass: try to find an entry that isn't picky about
826 its base address, then try to grab any standard serial port
827 address, and finally try to get any free port. */
828 i = first_tuple(handle, &tuple, &parse);
829 while (i != CS_NO_MORE_ITEMS) {
830 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
831 link->conf.ConfigIndex = cf->index;
832 for (j = 0; j < 5; j++) {
833 link->io.BasePort1 = base[j];
834 link->io.IOAddrLines = base[j] ? 16 : 3;
835 i = pcmcia_request_io(link->handle, &link->io);
840 i = next_tuple(handle, &tuple, &parse);
844 if (i != CS_SUCCESS) {
845 BT_ERR("No usable port range found");
846 cs_error(link->handle, RequestIO, i);
850 i = pcmcia_request_irq(link->handle, &link->irq);
851 if (i != CS_SUCCESS) {
852 cs_error(link->handle, RequestIRQ, i);
853 link->irq.AssignedIRQ = 0;
856 i = pcmcia_request_configuration(link->handle, &link->conf);
857 if (i != CS_SUCCESS) {
858 cs_error(link->handle, RequestConfiguration, i);
862 if (bt3c_open(info) != 0)
865 strcpy(info->node.dev_name, info->hdev->name);
866 link->dev = &info->node;
867 link->state &= ~DEV_CONFIG_PENDING;
872 cs_error(link->handle, last_fn, last_ret);
879 static void bt3c_release(dev_link_t *link)
881 bt3c_info_t *info = link->priv;
883 if (link->state & DEV_PRESENT)
888 pcmcia_release_configuration(link->handle);
889 pcmcia_release_io(link->handle, &link->io);
890 pcmcia_release_irq(link->handle, &link->irq);
892 link->state &= ~DEV_CONFIG;
896 static int bt3c_event(event_t event, int priority, event_callback_args_t *args)
898 dev_link_t *link = args->client_data;
899 bt3c_info_t *info = link->priv;
902 case CS_EVENT_CARD_REMOVAL:
903 link->state &= ~DEV_PRESENT;
904 if (link->state & DEV_CONFIG) {
909 case CS_EVENT_CARD_INSERTION:
910 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
913 case CS_EVENT_PM_SUSPEND:
914 link->state |= DEV_SUSPEND;
915 /* Fall through... */
916 case CS_EVENT_RESET_PHYSICAL:
917 if (link->state & DEV_CONFIG)
918 pcmcia_release_configuration(link->handle);
920 case CS_EVENT_PM_RESUME:
921 link->state &= ~DEV_SUSPEND;
922 /* Fall through... */
923 case CS_EVENT_CARD_RESET:
925 pcmcia_request_configuration(link->handle, &link->conf);
932 static struct pcmcia_device_id bt3c_ids[] = {
933 PCMCIA_DEVICE_PROD_ID13("3COM", "Bluetooth PC Card", 0xefce0a31, 0xd4ce9b02),
936 MODULE_DEVICE_TABLE(pcmcia, bt3c_ids);
938 static struct pcmcia_driver bt3c_driver = {
939 .owner = THIS_MODULE,
943 .attach = bt3c_attach,
945 .detach = bt3c_detach,
946 .id_table = bt3c_ids,
949 static int __init init_bt3c_cs(void)
951 return pcmcia_register_driver(&bt3c_driver);
955 static void __exit exit_bt3c_cs(void)
957 pcmcia_unregister_driver(&bt3c_driver);
958 BUG_ON(dev_list != NULL);
961 module_init(init_bt3c_cs);
962 module_exit(exit_bt3c_cs);