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 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
67 MODULE_DESCRIPTION("Bluetooth driver for the 3Com Bluetooth PCMCIA card");
68 MODULE_LICENSE("GPL");
72 /* ======================== Local structures ======================== */
75 typedef struct bt3c_info_t {
81 spinlock_t lock; /* For serializing operations */
83 struct sk_buff_head txq;
84 unsigned long tx_state;
86 unsigned long rx_state;
87 unsigned long rx_count;
88 struct sk_buff *rx_skb;
92 static void bt3c_config(dev_link_t *link);
93 static void bt3c_release(dev_link_t *link);
94 static int bt3c_event(event_t event, int priority, event_callback_args_t *args);
96 static dev_info_t dev_info = "bt3c_cs";
98 static dev_link_t *bt3c_attach(void);
99 static void bt3c_detach(dev_link_t *);
101 static dev_link_t *dev_list = NULL;
104 /* Transmit states */
105 #define XMIT_SENDING 1
106 #define XMIT_WAKEUP 2
107 #define XMIT_WAITING 8
109 /* Receiver states */
110 #define RECV_WAIT_PACKET_TYPE 0
111 #define RECV_WAIT_EVENT_HEADER 1
112 #define RECV_WAIT_ACL_HEADER 2
113 #define RECV_WAIT_SCO_HEADER 3
114 #define RECV_WAIT_DATA 4
118 /* ======================== Special I/O functions ======================== */
128 static inline void bt3c_address(unsigned int iobase, unsigned short addr)
130 outb(addr & 0xff, iobase + ADDR_L);
131 outb((addr >> 8) & 0xff, iobase + ADDR_H);
135 static inline void bt3c_put(unsigned int iobase, unsigned short value)
137 outb(value & 0xff, iobase + DATA_L);
138 outb((value >> 8) & 0xff, iobase + DATA_H);
142 static inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
144 bt3c_address(iobase, addr);
145 bt3c_put(iobase, value);
149 static inline unsigned short bt3c_get(unsigned int iobase)
151 unsigned short value = inb(iobase + DATA_L);
153 value |= inb(iobase + DATA_H) << 8;
159 static inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
161 bt3c_address(iobase, addr);
163 return bt3c_get(iobase);
168 /* ======================== Interrupt handling ======================== */
171 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
175 bt3c_address(iobase, 0x7080);
177 /* Fill FIFO with current frame */
178 while (actual < len) {
179 /* Transmit next byte */
180 bt3c_put(iobase, buf[actual]);
184 bt3c_io_write(iobase, 0x7005, actual);
190 static void bt3c_write_wakeup(bt3c_info_t *info)
193 BT_ERR("Unknown device");
197 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
201 register unsigned int iobase = info->link.io.BasePort1;
202 register struct sk_buff *skb;
205 if (!(info->link.state & DEV_PRESENT))
209 if (!(skb = skb_dequeue(&(info->txq)))) {
210 clear_bit(XMIT_SENDING, &(info->tx_state));
215 len = bt3c_write(iobase, 256, skb->data, skb->len);
217 if (len != skb->len) {
218 BT_ERR("Very strange");
223 info->hdev->stat.byte_tx += len;
229 static void bt3c_receive(bt3c_info_t *info)
235 BT_ERR("Unknown device");
239 iobase = info->link.io.BasePort1;
241 avail = bt3c_read(iobase, 0x7006);
242 //printk("bt3c_cs: receiving %d bytes\n", avail);
244 bt3c_address(iobase, 0x7480);
245 while (size < avail) {
247 info->hdev->stat.byte_rx++;
249 /* Allocate packet */
250 if (info->rx_skb == NULL) {
251 info->rx_state = RECV_WAIT_PACKET_TYPE;
253 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
254 BT_ERR("Can't allocate mem for new packet");
260 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
262 info->rx_skb->dev = (void *) info->hdev;
263 info->rx_skb->pkt_type = inb(iobase + DATA_L);
264 inb(iobase + DATA_H);
265 //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
267 switch (info->rx_skb->pkt_type) {
270 info->rx_state = RECV_WAIT_EVENT_HEADER;
271 info->rx_count = HCI_EVENT_HDR_SIZE;
274 case HCI_ACLDATA_PKT:
275 info->rx_state = RECV_WAIT_ACL_HEADER;
276 info->rx_count = HCI_ACL_HDR_SIZE;
279 case HCI_SCODATA_PKT:
280 info->rx_state = RECV_WAIT_SCO_HEADER;
281 info->rx_count = HCI_SCO_HDR_SIZE;
286 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
287 info->hdev->stat.err_rx++;
288 clear_bit(HCI_RUNNING, &(info->hdev->flags));
290 kfree_skb(info->rx_skb);
298 __u8 x = inb(iobase + DATA_L);
300 *skb_put(info->rx_skb, 1) = x;
301 inb(iobase + DATA_H);
304 if (info->rx_count == 0) {
307 struct hci_event_hdr *eh;
308 struct hci_acl_hdr *ah;
309 struct hci_sco_hdr *sh;
311 switch (info->rx_state) {
313 case RECV_WAIT_EVENT_HEADER:
314 eh = (struct hci_event_hdr *)(info->rx_skb->data);
315 info->rx_state = RECV_WAIT_DATA;
316 info->rx_count = eh->plen;
319 case RECV_WAIT_ACL_HEADER:
320 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
321 dlen = __le16_to_cpu(ah->dlen);
322 info->rx_state = RECV_WAIT_DATA;
323 info->rx_count = dlen;
326 case RECV_WAIT_SCO_HEADER:
327 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
328 info->rx_state = RECV_WAIT_DATA;
329 info->rx_count = sh->dlen;
333 hci_recv_frame(info->rx_skb);
345 bt3c_io_write(iobase, 0x7006, 0x0000);
349 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
351 bt3c_info_t *info = dev_inst;
355 if (!info || !info->hdev) {
356 BT_ERR("Call of irq %d for unknown device", irq);
360 iobase = info->link.io.BasePort1;
362 spin_lock(&(info->lock));
364 iir = inb(iobase + CONTROL);
366 int stat = bt3c_read(iobase, 0x7001);
368 if ((stat & 0xff) == 0x7f) {
369 BT_ERR("Very strange (stat=0x%04x)", stat);
370 } else if ((stat & 0xff) != 0xff) {
372 int stat = bt3c_read(iobase, 0x7002) & 0x10;
373 BT_INFO("%s: Antenna %s", info->hdev->name,
374 stat ? "out" : "in");
379 //BT_ERR("Ack (stat=0x%04x)", stat);
380 clear_bit(XMIT_SENDING, &(info->tx_state));
381 bt3c_write_wakeup(info);
384 bt3c_io_write(iobase, 0x7001, 0x0000);
386 outb(iir, iobase + CONTROL);
390 spin_unlock(&(info->lock));
397 /* ======================== HCI interface ======================== */
400 static int bt3c_hci_flush(struct hci_dev *hdev)
402 bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
405 skb_queue_purge(&(info->txq));
411 static int bt3c_hci_open(struct hci_dev *hdev)
413 set_bit(HCI_RUNNING, &(hdev->flags));
419 static int bt3c_hci_close(struct hci_dev *hdev)
421 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
424 bt3c_hci_flush(hdev);
430 static int bt3c_hci_send_frame(struct sk_buff *skb)
433 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
437 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
441 info = (bt3c_info_t *) (hdev->driver_data);
443 switch (skb->pkt_type) {
444 case HCI_COMMAND_PKT:
447 case HCI_ACLDATA_PKT:
450 case HCI_SCODATA_PKT:
455 /* Prepend skb with frame type */
456 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
457 skb_queue_tail(&(info->txq), skb);
459 spin_lock_irqsave(&(info->lock), flags);
461 bt3c_write_wakeup(info);
463 spin_unlock_irqrestore(&(info->lock), flags);
469 static void bt3c_hci_destruct(struct hci_dev *hdev)
474 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
481 /* ======================== Card services HCI interaction ======================== */
484 static struct device *bt3c_device(void)
486 static struct device dev = {
489 kobject_set_name(&dev.kobj, "bt3c");
490 kobject_init(&dev.kobj);
496 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
498 char *ptr = (char *) firmware;
500 unsigned int iobase, size, addr, fcs, tmp;
503 iobase = info->link.io.BasePort1;
506 bt3c_io_write(iobase, 0x8040, 0x0404);
507 bt3c_io_write(iobase, 0x8040, 0x0400);
511 bt3c_io_write(iobase, 0x8040, 0x0404);
518 BT_ERR("Bad address in firmware");
523 memset(b, 0, sizeof(b));
524 memcpy(b, ptr + 2, 2);
525 size = simple_strtol(b, NULL, 16);
527 memset(b, 0, sizeof(b));
528 memcpy(b, ptr + 4, 8);
529 addr = simple_strtol(b, NULL, 16);
531 memset(b, 0, sizeof(b));
532 memcpy(b, ptr + (size * 2) + 2, 2);
533 fcs = simple_strtol(b, NULL, 16);
535 memset(b, 0, sizeof(b));
536 for (tmp = 0, i = 0; i < size; i++) {
537 memcpy(b, ptr + (i * 2) + 2, 2);
538 tmp += simple_strtol(b, NULL, 16);
541 if (((tmp + fcs) & 0xff) != 0xff) {
542 BT_ERR("Checksum error in firmware");
548 bt3c_address(iobase, addr);
550 memset(b, 0, sizeof(b));
551 for (i = 0; i < (size - 4) / 2; i++) {
552 memcpy(b, ptr + (i * 4) + 12, 4);
553 tmp = simple_strtol(b, NULL, 16);
554 bt3c_put(iobase, tmp);
558 ptr += (size * 2) + 6;
559 count -= (size * 2) + 6;
565 bt3c_address(iobase, 0x3000);
566 outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
572 bt3c_io_write(iobase, 0x7006, 0x0000);
573 bt3c_io_write(iobase, 0x7005, 0x0000);
574 bt3c_io_write(iobase, 0x7001, 0x0000);
580 static int bt3c_open(bt3c_info_t *info)
582 const struct firmware *firmware;
583 struct hci_dev *hdev;
586 spin_lock_init(&(info->lock));
588 skb_queue_head_init(&(info->txq));
590 info->rx_state = RECV_WAIT_PACKET_TYPE;
594 /* Initialize HCI device */
595 hdev = hci_alloc_dev();
597 BT_ERR("Can't allocate HCI device");
603 hdev->type = HCI_PCCARD;
604 hdev->driver_data = info;
606 hdev->open = bt3c_hci_open;
607 hdev->close = bt3c_hci_close;
608 hdev->flush = bt3c_hci_flush;
609 hdev->send = bt3c_hci_send_frame;
610 hdev->destruct = bt3c_hci_destruct;
611 hdev->ioctl = bt3c_hci_ioctl;
613 hdev->owner = THIS_MODULE;
616 err = request_firmware(&firmware, "BT3CPCC.bin", bt3c_device());
618 BT_ERR("Firmware request failed");
622 err = bt3c_load_firmware(info, firmware->data, firmware->size);
624 release_firmware(firmware);
627 BT_ERR("Firmware loading failed");
631 /* Timeout before it is safe to send the first HCI packet */
634 /* Register HCI device */
635 err = hci_register_dev(hdev);
637 BT_ERR("Can't register HCI device");
650 static int bt3c_close(bt3c_info_t *info)
652 struct hci_dev *hdev = info->hdev;
657 bt3c_hci_close(hdev);
659 if (hci_unregister_dev(hdev) < 0)
660 BT_ERR("Can't unregister HCI device %s", hdev->name);
667 static dev_link_t *bt3c_attach(void)
670 client_reg_t client_reg;
674 /* Create new info device */
675 info = kmalloc(sizeof(*info), GFP_KERNEL);
678 memset(info, 0, sizeof(*info));
683 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
684 link->io.NumPorts1 = 8;
685 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
686 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
688 link->irq.Handler = bt3c_interrupt;
689 link->irq.Instance = info;
691 link->conf.Attributes = CONF_ENABLE_IRQ;
693 link->conf.IntType = INT_MEMORY_AND_IO;
695 /* Register with Card Services */
696 link->next = dev_list;
698 client_reg.dev_info = &dev_info;
699 client_reg.EventMask =
700 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
701 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
702 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
703 client_reg.event_handler = &bt3c_event;
704 client_reg.Version = 0x0210;
705 client_reg.event_callback_args.client_data = link;
707 ret = pcmcia_register_client(&link->handle, &client_reg);
708 if (ret != CS_SUCCESS) {
709 cs_error(link->handle, RegisterClient, ret);
718 static void bt3c_detach(dev_link_t *link)
720 bt3c_info_t *info = link->priv;
724 /* Locate device structure */
725 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
732 if (link->state & DEV_CONFIG)
736 ret = pcmcia_deregister_client(link->handle);
737 if (ret != CS_SUCCESS)
738 cs_error(link->handle, DeregisterClient, ret);
741 /* Unlink device structure, free bits */
747 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
751 i = pcmcia_get_tuple_data(handle, tuple);
755 return pcmcia_parse_tuple(handle, tuple, parse);
758 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
760 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
761 return CS_NO_MORE_ITEMS;
762 return get_tuple(handle, tuple, parse);
765 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
767 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
768 return CS_NO_MORE_ITEMS;
769 return get_tuple(handle, tuple, parse);
772 static void bt3c_config(dev_link_t *link)
774 static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
775 client_handle_t handle = link->handle;
776 bt3c_info_t *info = link->priv;
780 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
781 config_info_t config;
782 int i, j, try, last_ret, last_fn;
784 tuple.TupleData = (cisdata_t *)buf;
785 tuple.TupleOffset = 0;
786 tuple.TupleDataMax = 255;
787 tuple.Attributes = 0;
789 /* Get configuration register information */
790 tuple.DesiredTuple = CISTPL_CONFIG;
791 last_ret = first_tuple(handle, &tuple, &parse);
792 if (last_ret != CS_SUCCESS) {
793 last_fn = ParseTuple;
796 link->conf.ConfigBase = parse.config.base;
797 link->conf.Present = parse.config.rmask[0];
800 link->state |= DEV_CONFIG;
801 i = pcmcia_get_configuration_info(handle, &config);
802 link->conf.Vcc = config.Vcc;
804 /* First pass: look for a config entry that looks normal. */
805 tuple.TupleData = (cisdata_t *)buf;
806 tuple.TupleOffset = 0;
807 tuple.TupleDataMax = 255;
808 tuple.Attributes = 0;
809 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
810 /* Two tries: without IO aliases, then with aliases */
811 for (try = 0; try < 2; try++) {
812 i = first_tuple(handle, &tuple, &parse);
813 while (i != CS_NO_MORE_ITEMS) {
816 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
817 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
818 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
819 link->conf.ConfigIndex = cf->index;
820 link->io.BasePort1 = cf->io.win[0].base;
821 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
822 i = pcmcia_request_io(link->handle, &link->io);
827 i = next_tuple(handle, &tuple, &parse);
831 /* Second pass: try to find an entry that isn't picky about
832 its base address, then try to grab any standard serial port
833 address, and finally try to get any free port. */
834 i = first_tuple(handle, &tuple, &parse);
835 while (i != CS_NO_MORE_ITEMS) {
836 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
837 link->conf.ConfigIndex = cf->index;
838 for (j = 0; j < 5; j++) {
839 link->io.BasePort1 = base[j];
840 link->io.IOAddrLines = base[j] ? 16 : 3;
841 i = pcmcia_request_io(link->handle, &link->io);
846 i = next_tuple(handle, &tuple, &parse);
850 if (i != CS_SUCCESS) {
851 BT_ERR("No usable port range found");
852 cs_error(link->handle, RequestIO, i);
856 i = pcmcia_request_irq(link->handle, &link->irq);
857 if (i != CS_SUCCESS) {
858 cs_error(link->handle, RequestIRQ, i);
859 link->irq.AssignedIRQ = 0;
862 i = pcmcia_request_configuration(link->handle, &link->conf);
863 if (i != CS_SUCCESS) {
864 cs_error(link->handle, RequestConfiguration, i);
868 if (bt3c_open(info) != 0)
871 strcpy(info->node.dev_name, info->hdev->name);
872 link->dev = &info->node;
873 link->state &= ~DEV_CONFIG_PENDING;
878 cs_error(link->handle, last_fn, last_ret);
885 static void bt3c_release(dev_link_t *link)
887 bt3c_info_t *info = link->priv;
889 if (link->state & DEV_PRESENT)
894 pcmcia_release_configuration(link->handle);
895 pcmcia_release_io(link->handle, &link->io);
896 pcmcia_release_irq(link->handle, &link->irq);
898 link->state &= ~DEV_CONFIG;
902 static int bt3c_event(event_t event, int priority, event_callback_args_t *args)
904 dev_link_t *link = args->client_data;
905 bt3c_info_t *info = link->priv;
908 case CS_EVENT_CARD_REMOVAL:
909 link->state &= ~DEV_PRESENT;
910 if (link->state & DEV_CONFIG) {
915 case CS_EVENT_CARD_INSERTION:
916 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
919 case CS_EVENT_PM_SUSPEND:
920 link->state |= DEV_SUSPEND;
921 /* Fall through... */
922 case CS_EVENT_RESET_PHYSICAL:
923 if (link->state & DEV_CONFIG)
924 pcmcia_release_configuration(link->handle);
926 case CS_EVENT_PM_RESUME:
927 link->state &= ~DEV_SUSPEND;
928 /* Fall through... */
929 case CS_EVENT_CARD_RESET:
931 pcmcia_request_configuration(link->handle, &link->conf);
938 static struct pcmcia_driver bt3c_driver = {
939 .owner = THIS_MODULE,
943 .attach = bt3c_attach,
944 .detach = bt3c_detach,
947 static int __init init_bt3c_cs(void)
949 return pcmcia_register_driver(&bt3c_driver);
953 static void __exit exit_bt3c_cs(void)
955 pcmcia_unregister_driver(&bt3c_driver);
956 BUG_ON(dev_list != NULL);
959 module_init(init_bt3c_cs);
960 module_exit(exit_bt3c_cs);