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 #define __KERNEL_SYSCALLS__
29 #include <linux/kernel.h>
30 #include <linux/kmod.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <linux/sched.h>
35 #include <linux/delay.h>
36 #include <linux/timer.h>
37 #include <linux/errno.h>
38 #include <linux/unistd.h>
39 #include <linux/ptrace.h>
40 #include <linux/ioport.h>
41 #include <linux/spinlock.h>
43 #include <linux/skbuff.h>
44 #include <linux/string.h>
45 #include <linux/serial.h>
46 #include <linux/serial_reg.h>
47 #include <asm/system.h>
48 #include <asm/bitops.h>
51 #include <pcmcia/version.h>
52 #include <pcmcia/cs_types.h>
53 #include <pcmcia/cs.h>
54 #include <pcmcia/cistpl.h>
55 #include <pcmcia/ciscode.h>
56 #include <pcmcia/ds.h>
57 #include <pcmcia/cisreg.h>
59 #include <net/bluetooth/bluetooth.h>
60 #include <net/bluetooth/hci_core.h>
64 /* ======================== Module parameters ======================== */
67 /* Bit map of interrupts to choose from */
68 static u_int irq_mask = 0xffff;
69 static int irq_list[4] = { -1 };
71 MODULE_PARM(irq_mask, "i");
72 MODULE_PARM(irq_list, "1-4i");
74 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
75 MODULE_DESCRIPTION("BlueZ driver for the 3Com Bluetooth PCMCIA card");
76 MODULE_LICENSE("GPL");
80 /* ======================== Local structures ======================== */
83 typedef struct bt3c_info_t {
89 spinlock_t lock; /* For serializing operations */
91 struct sk_buff_head txq;
92 unsigned long tx_state;
94 unsigned long rx_state;
95 unsigned long rx_count;
96 struct sk_buff *rx_skb;
100 void bt3c_config(dev_link_t *link);
101 void bt3c_release(u_long arg);
102 int bt3c_event(event_t event, int priority, event_callback_args_t *args);
104 static dev_info_t dev_info = "bt3c_cs";
106 dev_link_t *bt3c_attach(void);
107 void bt3c_detach(dev_link_t *);
109 static dev_link_t *dev_list = NULL;
112 /* Transmit states */
113 #define XMIT_SENDING 1
114 #define XMIT_WAKEUP 2
115 #define XMIT_WAITING 8
117 /* Receiver states */
118 #define RECV_WAIT_PACKET_TYPE 0
119 #define RECV_WAIT_EVENT_HEADER 1
120 #define RECV_WAIT_ACL_HEADER 2
121 #define RECV_WAIT_SCO_HEADER 3
122 #define RECV_WAIT_DATA 4
126 /* ======================== Special I/O functions ======================== */
136 inline void bt3c_address(unsigned int iobase, unsigned short addr)
138 outb(addr & 0xff, iobase + ADDR_L);
139 outb((addr >> 8) & 0xff, iobase + ADDR_H);
143 inline void bt3c_put(unsigned int iobase, unsigned short value)
145 outb(value & 0xff, iobase + DATA_L);
146 outb((value >> 8) & 0xff, iobase + DATA_H);
150 inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
152 bt3c_address(iobase, addr);
153 bt3c_put(iobase, value);
157 inline unsigned short bt3c_get(unsigned int iobase)
159 unsigned short value = inb(iobase + DATA_L);
161 value |= inb(iobase + DATA_H) << 8;
167 inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
169 bt3c_address(iobase, addr);
171 return bt3c_get(iobase);
176 /* ======================== Interrupt handling ======================== */
179 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
183 bt3c_address(iobase, 0x7080);
185 /* Fill FIFO with current frame */
186 while (actual < len) {
187 /* Transmit next byte */
188 bt3c_put(iobase, buf[actual]);
192 bt3c_io_write(iobase, 0x7005, actual);
198 static void bt3c_write_wakeup(bt3c_info_t *info, int from)
203 printk(KERN_WARNING "bt3c_cs: Call of write_wakeup for unknown device.\n");
207 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
210 spin_lock_irqsave(&(info->lock), flags);
213 register unsigned int iobase = info->link.io.BasePort1;
214 register struct sk_buff *skb;
217 if (!(info->link.state & DEV_PRESENT))
221 if (!(skb = skb_dequeue(&(info->txq)))) {
222 clear_bit(XMIT_SENDING, &(info->tx_state));
227 len = bt3c_write(iobase, 256, skb->data, skb->len);
229 if (len != skb->len) {
230 printk(KERN_WARNING "bt3c_cs: very strange\n");
235 info->hdev.stat.byte_tx += len;
239 spin_unlock_irqrestore(&(info->lock), flags);
243 static void bt3c_receive(bt3c_info_t *info)
249 printk(KERN_WARNING "bt3c_cs: Call of receive for unknown device.\n");
253 iobase = info->link.io.BasePort1;
255 avail = bt3c_read(iobase, 0x7006);
256 //printk("bt3c_cs: receiving %d bytes\n", avail);
258 bt3c_address(iobase, 0x7480);
259 while (size < avail) {
261 info->hdev.stat.byte_rx++;
263 /* Allocate packet */
264 if (info->rx_skb == NULL) {
265 info->rx_state = RECV_WAIT_PACKET_TYPE;
267 if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
268 printk(KERN_WARNING "bt3c_cs: Can't allocate mem for new packet.\n");
274 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
276 info->rx_skb->dev = (void *)&(info->hdev);
277 info->rx_skb->pkt_type = inb(iobase + DATA_L);
278 inb(iobase + DATA_H);
279 //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
281 switch (info->rx_skb->pkt_type) {
284 info->rx_state = RECV_WAIT_EVENT_HEADER;
285 info->rx_count = HCI_EVENT_HDR_SIZE;
288 case HCI_ACLDATA_PKT:
289 info->rx_state = RECV_WAIT_ACL_HEADER;
290 info->rx_count = HCI_ACL_HDR_SIZE;
293 case HCI_SCODATA_PKT:
294 info->rx_state = RECV_WAIT_SCO_HEADER;
295 info->rx_count = HCI_SCO_HDR_SIZE;
300 printk(KERN_WARNING "bt3c_cs: Unknown HCI packet with type 0x%02x received.\n", info->rx_skb->pkt_type);
301 info->hdev.stat.err_rx++;
302 clear_bit(HCI_RUNNING, &(info->hdev.flags));
304 kfree_skb(info->rx_skb);
312 __u8 x = inb(iobase + DATA_L);
314 *skb_put(info->rx_skb, 1) = x;
315 inb(iobase + DATA_H);
318 if (info->rx_count == 0) {
325 switch (info->rx_state) {
327 case RECV_WAIT_EVENT_HEADER:
328 eh = (hci_event_hdr *)(info->rx_skb->data);
329 info->rx_state = RECV_WAIT_DATA;
330 info->rx_count = eh->plen;
333 case RECV_WAIT_ACL_HEADER:
334 ah = (hci_acl_hdr *)(info->rx_skb->data);
335 dlen = __le16_to_cpu(ah->dlen);
336 info->rx_state = RECV_WAIT_DATA;
337 info->rx_count = dlen;
340 case RECV_WAIT_SCO_HEADER:
341 sh = (hci_sco_hdr *)(info->rx_skb->data);
342 info->rx_state = RECV_WAIT_DATA;
343 info->rx_count = sh->dlen;
347 hci_recv_frame(info->rx_skb);
359 bt3c_io_write(iobase, 0x7006, 0x0000);
363 void bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
365 bt3c_info_t *info = dev_inst;
370 printk(KERN_WARNING "bt3c_cs: Call of irq %d for unknown device.\n", irq);
374 iobase = info->link.io.BasePort1;
376 spin_lock(&(info->lock));
378 iir = inb(iobase + CONTROL);
380 int stat = bt3c_read(iobase, 0x7001);
382 if ((stat & 0xff) == 0x7f) {
383 printk(KERN_WARNING "bt3c_cs: STRANGE stat=%04x\n", stat);
384 } else if ((stat & 0xff) != 0xff) {
386 int stat = bt3c_read(iobase, 0x7002) & 0x10;
387 printk(KERN_WARNING "bt3c_cs: antena %s\n", stat ? "OUT" : "IN");
392 //printk("bt3c_cs: ACK %04x\n", 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));
409 /* ======================== HCI interface ======================== */
412 static int bt3c_hci_flush(struct hci_dev *hdev)
414 bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
417 skb_queue_purge(&(info->txq));
423 static int bt3c_hci_open(struct hci_dev *hdev)
425 set_bit(HCI_RUNNING, &(hdev->flags));
431 static int bt3c_hci_close(struct hci_dev *hdev)
433 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
436 bt3c_hci_flush(hdev);
442 static int bt3c_hci_send_frame(struct sk_buff *skb)
445 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
448 printk(KERN_WARNING "bt3c_cs: Frame for unknown HCI device (hdev=NULL).");
452 info = (bt3c_info_t *) (hdev->driver_data);
454 switch (skb->pkt_type) {
455 case HCI_COMMAND_PKT:
458 case HCI_ACLDATA_PKT:
461 case HCI_SCODATA_PKT:
466 /* Prepend skb with frame type */
467 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
468 skb_queue_tail(&(info->txq), skb);
470 bt3c_write_wakeup(info, 0);
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 /* ======================== User mode firmware loader ======================== */
491 #define FW_LOADER "/sbin/bluefw"
495 static int bt3c_fw_loader_exec(void *dev)
497 char *argv[] = { FW_LOADER, "pccard", dev, NULL };
498 char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
501 err = exec_usermodehelper(FW_LOADER, argv, envp);
503 printk(KERN_WARNING "bt3c_cs: Failed to exec \"%s pccard %s\".\n", FW_LOADER, (char *)dev);
509 static int bt3c_firmware_load(bt3c_info_t *info)
516 /* Check if root fs is mounted */
517 if (!current->fs->root) {
518 printk(KERN_WARNING "bt3c_cs: Root filesystem is not mounted.\n");
522 sprintf(dev, "%04x", info->link.io.BasePort1);
524 pid = kernel_thread(bt3c_fw_loader_exec, (void *)dev, 0);
526 printk(KERN_WARNING "bt3c_cs: Forking of kernel thread failed (errno=%d).\n", -pid);
530 /* Block signals, everything but SIGKILL/SIGSTOP */
531 spin_lock_irq(¤t->sigmask_lock);
532 tmpsig = current->blocked;
533 siginitsetinv(¤t->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP));
534 recalc_sigpending(current);
535 spin_unlock_irq(¤t->sigmask_lock);
537 result = waitpid(pid, NULL, __WCLONE);
539 /* Allow signals again */
540 spin_lock_irq(¤t->sigmask_lock);
541 current->blocked = tmpsig;
542 recalc_sigpending(current);
543 spin_unlock_irq(¤t->sigmask_lock);
546 printk(KERN_WARNING "bt3c_cs: Waiting for pid %d failed (errno=%d).\n", pid, -result);
555 /* ======================== Card services HCI interaction ======================== */
558 int bt3c_open(bt3c_info_t *info)
560 struct hci_dev *hdev;
563 spin_lock_init(&(info->lock));
565 skb_queue_head_init(&(info->txq));
567 info->rx_state = RECV_WAIT_PACKET_TYPE;
573 if ((err = bt3c_firmware_load(info)) < 0)
576 /* Timeout before it is safe to send the first HCI packet */
578 set_current_state(TASK_INTERRUPTIBLE);
579 schedule_timeout(HZ);
582 /* Initialize and register HCI device */
584 hdev = &(info->hdev);
586 hdev->type = HCI_PCCARD;
587 hdev->driver_data = info;
589 hdev->open = bt3c_hci_open;
590 hdev->close = bt3c_hci_close;
591 hdev->flush = bt3c_hci_flush;
592 hdev->send = bt3c_hci_send_frame;
593 hdev->destruct = bt3c_hci_destruct;
594 hdev->ioctl = bt3c_hci_ioctl;
596 if (hci_register_dev(hdev) < 0) {
597 printk(KERN_WARNING "bt3c_cs: Can't register HCI device %s.\n", hdev->name);
605 int bt3c_close(bt3c_info_t *info)
607 struct hci_dev *hdev = &(info->hdev);
609 bt3c_hci_close(hdev);
611 if (hci_unregister_dev(hdev) < 0)
612 printk(KERN_WARNING "bt3c_cs: Can't unregister HCI device %s.\n", hdev->name);
619 /* ======================== Card services ======================== */
622 static void cs_error(client_handle_t handle, int func, int ret)
624 error_info_t err = { func, ret };
626 CardServices(ReportError, handle, &err);
630 dev_link_t *bt3c_attach(void)
633 client_reg_t client_reg;
637 /* Create new info device */
638 info = kmalloc(sizeof(*info), GFP_KERNEL);
641 memset(info, 0, sizeof(*info));
646 link->release.function = &bt3c_release;
647 link->release.data = (u_long)link;
648 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
649 link->io.NumPorts1 = 8;
650 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
651 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
653 if (irq_list[0] == -1)
654 link->irq.IRQInfo2 = irq_mask;
656 for (i = 0; i < 4; i++)
657 link->irq.IRQInfo2 |= 1 << irq_list[i];
659 link->irq.Handler = bt3c_interrupt;
660 link->irq.Instance = info;
662 link->conf.Attributes = CONF_ENABLE_IRQ;
664 link->conf.IntType = INT_MEMORY_AND_IO;
666 /* Register with Card Services */
667 link->next = dev_list;
669 client_reg.dev_info = &dev_info;
670 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
671 client_reg.EventMask =
672 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
673 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
674 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
675 client_reg.event_handler = &bt3c_event;
676 client_reg.Version = 0x0210;
677 client_reg.event_callback_args.client_data = link;
679 ret = CardServices(RegisterClient, &link->handle, &client_reg);
680 if (ret != CS_SUCCESS) {
681 cs_error(link->handle, RegisterClient, ret);
690 void bt3c_detach(dev_link_t *link)
692 bt3c_info_t *info = link->priv;
696 /* Locate device structure */
697 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
704 del_timer(&link->release);
706 if (link->state & DEV_CONFIG)
707 bt3c_release((u_long)link);
710 ret = CardServices(DeregisterClient, link->handle);
711 if (ret != CS_SUCCESS)
712 cs_error(link->handle, DeregisterClient, ret);
715 /* Unlink device structure, free bits */
722 static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
726 i = CardServices(fn, handle, tuple);
728 return CS_NO_MORE_ITEMS;
730 i = CardServices(GetTupleData, handle, tuple);
734 return CardServices(ParseTuple, handle, tuple, parse);
738 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
739 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
741 void bt3c_config(dev_link_t *link)
743 static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
744 client_handle_t handle = link->handle;
745 bt3c_info_t *info = link->priv;
749 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
750 config_info_t config;
751 int i, j, try, last_ret, last_fn;
753 tuple.TupleData = (cisdata_t *)buf;
754 tuple.TupleOffset = 0;
755 tuple.TupleDataMax = 255;
756 tuple.Attributes = 0;
758 /* Get configuration register information */
759 tuple.DesiredTuple = CISTPL_CONFIG;
760 last_ret = first_tuple(handle, &tuple, &parse);
761 if (last_ret != CS_SUCCESS) {
762 last_fn = ParseTuple;
765 link->conf.ConfigBase = parse.config.base;
766 link->conf.Present = parse.config.rmask[0];
769 link->state |= DEV_CONFIG;
770 i = CardServices(GetConfigurationInfo, handle, &config);
771 link->conf.Vcc = config.Vcc;
773 /* First pass: look for a config entry that looks normal. */
774 tuple.TupleData = (cisdata_t *)buf;
775 tuple.TupleOffset = 0;
776 tuple.TupleDataMax = 255;
777 tuple.Attributes = 0;
778 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
779 /* Two tries: without IO aliases, then with aliases */
780 for (try = 0; try < 2; try++) {
781 i = first_tuple(handle, &tuple, &parse);
782 while (i != CS_NO_MORE_ITEMS) {
785 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
786 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
787 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
788 link->conf.ConfigIndex = cf->index;
789 link->io.BasePort1 = cf->io.win[0].base;
790 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
791 i = CardServices(RequestIO, link->handle, &link->io);
796 i = next_tuple(handle, &tuple, &parse);
800 /* Second pass: try to find an entry that isn't picky about
801 its base address, then try to grab any standard serial port
802 address, and finally try to get any free port. */
803 i = first_tuple(handle, &tuple, &parse);
804 while (i != CS_NO_MORE_ITEMS) {
805 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
806 link->conf.ConfigIndex = cf->index;
807 for (j = 0; j < 5; j++) {
808 link->io.BasePort1 = base[j];
809 link->io.IOAddrLines = base[j] ? 16 : 3;
810 i = CardServices(RequestIO, link->handle, &link->io);
815 i = next_tuple(handle, &tuple, &parse);
819 if (i != CS_SUCCESS) {
820 printk(KERN_NOTICE "bt3c_cs: No usable port range found. Giving up.\n");
821 cs_error(link->handle, RequestIO, i);
825 i = CardServices(RequestIRQ, link->handle, &link->irq);
826 if (i != CS_SUCCESS) {
827 cs_error(link->handle, RequestIRQ, i);
828 link->irq.AssignedIRQ = 0;
831 i = CardServices(RequestConfiguration, link->handle, &link->conf);
832 if (i != CS_SUCCESS) {
833 cs_error(link->handle, RequestConfiguration, i);
839 if (bt3c_open(info) != 0)
842 strcpy(info->node.dev_name, info->hdev.name);
843 link->dev = &info->node;
844 link->state &= ~DEV_CONFIG_PENDING;
849 cs_error(link->handle, last_fn, last_ret);
852 bt3c_release((u_long)link);
856 void bt3c_release(u_long arg)
858 dev_link_t *link = (dev_link_t *)arg;
859 bt3c_info_t *info = link->priv;
861 if (link->state & DEV_PRESENT)
868 CardServices(ReleaseConfiguration, link->handle);
869 CardServices(ReleaseIO, link->handle, &link->io);
870 CardServices(ReleaseIRQ, link->handle, &link->irq);
872 link->state &= ~DEV_CONFIG;
876 int bt3c_event(event_t event, int priority, event_callback_args_t *args)
878 dev_link_t *link = args->client_data;
879 bt3c_info_t *info = link->priv;
882 case CS_EVENT_CARD_REMOVAL:
883 link->state &= ~DEV_PRESENT;
884 if (link->state & DEV_CONFIG) {
886 mod_timer(&link->release, jiffies + HZ / 20);
889 case CS_EVENT_CARD_INSERTION:
890 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
893 case CS_EVENT_PM_SUSPEND:
894 link->state |= DEV_SUSPEND;
895 /* Fall through... */
896 case CS_EVENT_RESET_PHYSICAL:
897 if (link->state & DEV_CONFIG)
898 CardServices(ReleaseConfiguration, link->handle);
900 case CS_EVENT_PM_RESUME:
901 link->state &= ~DEV_SUSPEND;
902 /* Fall through... */
903 case CS_EVENT_CARD_RESET:
905 CardServices(RequestConfiguration, link->handle, &link->conf);
914 /* ======================== Module initialization ======================== */
917 int __init init_bt3c_cs(void)
922 CardServices(GetCardServicesInfo, &serv);
923 if (serv.Revision != CS_RELEASE_CODE) {
924 printk(KERN_NOTICE "bt3c_cs: Card Services release does not match!\n");
928 err = register_pccard_driver(&dev_info, &bt3c_attach, &bt3c_detach);
934 void __exit exit_bt3c_cs(void)
936 unregister_pccard_driver(&dev_info);
938 while (dev_list != NULL)
939 bt3c_detach(dev_list);
943 module_init(init_bt3c_cs);
944 module_exit(exit_bt3c_cs);