initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / bluetooth / bt3c_cs.c
blobd67d0385f20bdf2caddc7025be270ad6af618c86
1 /*
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>
38 #include <linux/skbuff.h>
39 #include <linux/string.h>
40 #include <linux/serial.h>
41 #include <linux/serial_reg.h>
42 #include <asm/system.h>
43 #include <asm/bitops.h>
44 #include <asm/io.h>
46 #include <linux/device.h>
47 #include <linux/firmware.h>
49 #include <pcmcia/version.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 /* Bit map of interrupts to choose from */
66 static u_int irq_mask = 0xffff;
67 static int irq_list[4] = { -1 };
69 MODULE_PARM(irq_mask, "i");
70 MODULE_PARM(irq_list, "1-4i");
72 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
73 MODULE_DESCRIPTION("Bluetooth driver for the 3Com Bluetooth PCMCIA card");
74 MODULE_LICENSE("GPL");
78 /* ======================== Local structures ======================== */
81 typedef struct bt3c_info_t {
82 dev_link_t link;
83 dev_node_t node;
85 struct hci_dev *hdev;
87 spinlock_t lock; /* For serializing operations */
89 struct sk_buff_head txq;
90 unsigned long tx_state;
92 unsigned long rx_state;
93 unsigned long rx_count;
94 struct sk_buff *rx_skb;
95 } bt3c_info_t;
98 void bt3c_config(dev_link_t *link);
99 void bt3c_release(dev_link_t *link);
100 int bt3c_event(event_t event, int priority, event_callback_args_t *args);
102 static dev_info_t dev_info = "bt3c_cs";
104 dev_link_t *bt3c_attach(void);
105 void bt3c_detach(dev_link_t *);
107 static dev_link_t *dev_list = NULL;
110 /* Transmit states */
111 #define XMIT_SENDING 1
112 #define XMIT_WAKEUP 2
113 #define XMIT_WAITING 8
115 /* Receiver states */
116 #define RECV_WAIT_PACKET_TYPE 0
117 #define RECV_WAIT_EVENT_HEADER 1
118 #define RECV_WAIT_ACL_HEADER 2
119 #define RECV_WAIT_SCO_HEADER 3
120 #define RECV_WAIT_DATA 4
124 /* ======================== Special I/O functions ======================== */
127 #define DATA_L 0
128 #define DATA_H 1
129 #define ADDR_L 2
130 #define ADDR_H 3
131 #define CONTROL 4
134 inline void bt3c_address(unsigned int iobase, unsigned short addr)
136 outb(addr & 0xff, iobase + ADDR_L);
137 outb((addr >> 8) & 0xff, iobase + ADDR_H);
141 inline void bt3c_put(unsigned int iobase, unsigned short value)
143 outb(value & 0xff, iobase + DATA_L);
144 outb((value >> 8) & 0xff, iobase + DATA_H);
148 inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
150 bt3c_address(iobase, addr);
151 bt3c_put(iobase, value);
155 inline unsigned short bt3c_get(unsigned int iobase)
157 unsigned short value = inb(iobase + DATA_L);
159 value |= inb(iobase + DATA_H) << 8;
161 return value;
165 inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
167 bt3c_address(iobase, addr);
169 return bt3c_get(iobase);
174 /* ======================== Interrupt handling ======================== */
177 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
179 int actual = 0;
181 bt3c_address(iobase, 0x7080);
183 /* Fill FIFO with current frame */
184 while (actual < len) {
185 /* Transmit next byte */
186 bt3c_put(iobase, buf[actual]);
187 actual++;
190 bt3c_io_write(iobase, 0x7005, actual);
192 return actual;
196 static void bt3c_write_wakeup(bt3c_info_t *info)
198 if (!info) {
199 BT_ERR("Unknown device");
200 return;
203 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
204 return;
206 do {
207 register unsigned int iobase = info->link.io.BasePort1;
208 register struct sk_buff *skb;
209 register int len;
211 if (!(info->link.state & DEV_PRESENT))
212 break;
215 if (!(skb = skb_dequeue(&(info->txq)))) {
216 clear_bit(XMIT_SENDING, &(info->tx_state));
217 break;
220 /* Send frame */
221 len = bt3c_write(iobase, 256, skb->data, skb->len);
223 if (len != skb->len) {
224 BT_ERR("Very strange");
227 kfree_skb(skb);
229 info->hdev->stat.byte_tx += len;
231 } while (0);
235 static void bt3c_receive(bt3c_info_t *info)
237 unsigned int iobase;
238 int size = 0, avail;
240 if (!info) {
241 BT_ERR("Unknown device");
242 return;
245 iobase = info->link.io.BasePort1;
247 avail = bt3c_read(iobase, 0x7006);
248 //printk("bt3c_cs: receiving %d bytes\n", avail);
250 bt3c_address(iobase, 0x7480);
251 while (size < avail) {
252 size++;
253 info->hdev->stat.byte_rx++;
255 /* Allocate packet */
256 if (info->rx_skb == NULL) {
257 info->rx_state = RECV_WAIT_PACKET_TYPE;
258 info->rx_count = 0;
259 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
260 BT_ERR("Can't allocate mem for new packet");
261 return;
266 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
268 info->rx_skb->dev = (void *) info->hdev;
269 info->rx_skb->pkt_type = inb(iobase + DATA_L);
270 inb(iobase + DATA_H);
271 //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
273 switch (info->rx_skb->pkt_type) {
275 case HCI_EVENT_PKT:
276 info->rx_state = RECV_WAIT_EVENT_HEADER;
277 info->rx_count = HCI_EVENT_HDR_SIZE;
278 break;
280 case HCI_ACLDATA_PKT:
281 info->rx_state = RECV_WAIT_ACL_HEADER;
282 info->rx_count = HCI_ACL_HDR_SIZE;
283 break;
285 case HCI_SCODATA_PKT:
286 info->rx_state = RECV_WAIT_SCO_HEADER;
287 info->rx_count = HCI_SCO_HDR_SIZE;
288 break;
290 default:
291 /* Unknown packet */
292 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
293 info->hdev->stat.err_rx++;
294 clear_bit(HCI_RUNNING, &(info->hdev->flags));
296 kfree_skb(info->rx_skb);
297 info->rx_skb = NULL;
298 break;
302 } else {
304 __u8 x = inb(iobase + DATA_L);
306 *skb_put(info->rx_skb, 1) = x;
307 inb(iobase + DATA_H);
308 info->rx_count--;
310 if (info->rx_count == 0) {
312 int dlen;
313 struct hci_event_hdr *eh;
314 struct hci_acl_hdr *ah;
315 struct hci_sco_hdr *sh;
317 switch (info->rx_state) {
319 case RECV_WAIT_EVENT_HEADER:
320 eh = (struct hci_event_hdr *)(info->rx_skb->data);
321 info->rx_state = RECV_WAIT_DATA;
322 info->rx_count = eh->plen;
323 break;
325 case RECV_WAIT_ACL_HEADER:
326 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
327 dlen = __le16_to_cpu(ah->dlen);
328 info->rx_state = RECV_WAIT_DATA;
329 info->rx_count = dlen;
330 break;
332 case RECV_WAIT_SCO_HEADER:
333 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
334 info->rx_state = RECV_WAIT_DATA;
335 info->rx_count = sh->dlen;
336 break;
338 case RECV_WAIT_DATA:
339 hci_recv_frame(info->rx_skb);
340 info->rx_skb = NULL;
341 break;
351 bt3c_io_write(iobase, 0x7006, 0x0000);
355 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
357 bt3c_info_t *info = dev_inst;
358 unsigned int iobase;
359 int iir;
361 if (!info || !info->hdev) {
362 BT_ERR("Call of irq %d for unknown device", irq);
363 return IRQ_NONE;
366 iobase = info->link.io.BasePort1;
368 spin_lock(&(info->lock));
370 iir = inb(iobase + CONTROL);
371 if (iir & 0x80) {
372 int stat = bt3c_read(iobase, 0x7001);
374 if ((stat & 0xff) == 0x7f) {
375 BT_ERR("Very strange (stat=0x%04x)", stat);
376 } else if ((stat & 0xff) != 0xff) {
377 if (stat & 0x0020) {
378 int stat = bt3c_read(iobase, 0x7002) & 0x10;
379 BT_INFO("%s: Antenna %s", info->hdev->name,
380 stat ? "out" : "in");
382 if (stat & 0x0001)
383 bt3c_receive(info);
384 if (stat & 0x0002) {
385 //BT_ERR("Ack (stat=0x%04x)", stat);
386 clear_bit(XMIT_SENDING, &(info->tx_state));
387 bt3c_write_wakeup(info);
390 bt3c_io_write(iobase, 0x7001, 0x0000);
392 outb(iir, iobase + CONTROL);
396 spin_unlock(&(info->lock));
398 return IRQ_HANDLED;
403 /* ======================== HCI interface ======================== */
406 static int bt3c_hci_flush(struct hci_dev *hdev)
408 bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
410 /* Drop TX queue */
411 skb_queue_purge(&(info->txq));
413 return 0;
417 static int bt3c_hci_open(struct hci_dev *hdev)
419 set_bit(HCI_RUNNING, &(hdev->flags));
421 return 0;
425 static int bt3c_hci_close(struct hci_dev *hdev)
427 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
428 return 0;
430 bt3c_hci_flush(hdev);
432 return 0;
436 static int bt3c_hci_send_frame(struct sk_buff *skb)
438 bt3c_info_t *info;
439 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
440 unsigned long flags;
442 if (!hdev) {
443 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
444 return -ENODEV;
447 info = (bt3c_info_t *) (hdev->driver_data);
449 switch (skb->pkt_type) {
450 case HCI_COMMAND_PKT:
451 hdev->stat.cmd_tx++;
452 break;
453 case HCI_ACLDATA_PKT:
454 hdev->stat.acl_tx++;
455 break;
456 case HCI_SCODATA_PKT:
457 hdev->stat.sco_tx++;
458 break;
461 /* Prepend skb with frame type */
462 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
463 skb_queue_tail(&(info->txq), skb);
465 spin_lock_irqsave(&(info->lock), flags);
467 bt3c_write_wakeup(info);
469 spin_unlock_irqrestore(&(info->lock), flags);
471 return 0;
475 static void bt3c_hci_destruct(struct hci_dev *hdev)
480 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
482 return -ENOIOCTLCMD;
487 /* ======================== Card services HCI interaction ======================== */
490 static struct device *bt3c_device(void)
492 static char *kobj_name = "bt3c";
494 static struct device dev = {
495 .bus_id = "pcmcia",
497 dev.kobj.k_name = kmalloc(strlen(kobj_name) + 1, GFP_KERNEL);
498 strcpy(dev.kobj.k_name, kobj_name);
499 kobject_init(&dev.kobj);
501 return &dev;
505 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
507 char *ptr = (char *) firmware;
508 char b[9];
509 unsigned int iobase, size, addr, fcs, tmp;
510 int i, err = 0;
512 iobase = info->link.io.BasePort1;
514 /* Reset */
515 bt3c_io_write(iobase, 0x8040, 0x0404);
516 bt3c_io_write(iobase, 0x8040, 0x0400);
518 udelay(1);
520 bt3c_io_write(iobase, 0x8040, 0x0404);
522 udelay(17);
524 /* Load */
525 while (count) {
526 if (ptr[0] != 'S') {
527 BT_ERR("Bad address in firmware");
528 err = -EFAULT;
529 goto error;
532 memset(b, 0, sizeof(b));
533 memcpy(b, ptr + 2, 2);
534 size = simple_strtol(b, NULL, 16);
536 memset(b, 0, sizeof(b));
537 memcpy(b, ptr + 4, 8);
538 addr = simple_strtol(b, NULL, 16);
540 memset(b, 0, sizeof(b));
541 memcpy(b, ptr + (size * 2) + 2, 2);
542 fcs = simple_strtol(b, NULL, 16);
544 memset(b, 0, sizeof(b));
545 for (tmp = 0, i = 0; i < size; i++) {
546 memcpy(b, ptr + (i * 2) + 2, 2);
547 tmp += simple_strtol(b, NULL, 16);
550 if (((tmp + fcs) & 0xff) != 0xff) {
551 BT_ERR("Checksum error in firmware");
552 err = -EILSEQ;
553 goto error;
556 if (ptr[1] == '3') {
557 bt3c_address(iobase, addr);
559 memset(b, 0, sizeof(b));
560 for (i = 0; i < (size - 4) / 2; i++) {
561 memcpy(b, ptr + (i * 4) + 12, 4);
562 tmp = simple_strtol(b, NULL, 16);
563 bt3c_put(iobase, tmp);
567 ptr += (size * 2) + 6;
568 count -= (size * 2) + 6;
571 udelay(17);
573 /* Boot */
574 bt3c_address(iobase, 0x3000);
575 outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
577 error:
578 udelay(17);
580 /* Clear */
581 bt3c_io_write(iobase, 0x7006, 0x0000);
582 bt3c_io_write(iobase, 0x7005, 0x0000);
583 bt3c_io_write(iobase, 0x7001, 0x0000);
585 return err;
589 int bt3c_open(bt3c_info_t *info)
591 const struct firmware *firmware;
592 struct hci_dev *hdev;
593 int err;
595 spin_lock_init(&(info->lock));
597 skb_queue_head_init(&(info->txq));
599 info->rx_state = RECV_WAIT_PACKET_TYPE;
600 info->rx_count = 0;
601 info->rx_skb = NULL;
603 /* Initialize HCI device */
604 hdev = hci_alloc_dev();
605 if (!hdev) {
606 BT_ERR("Can't allocate HCI device");
607 return -ENOMEM;
610 info->hdev = hdev;
612 hdev->type = HCI_PCCARD;
613 hdev->driver_data = info;
615 hdev->open = bt3c_hci_open;
616 hdev->close = bt3c_hci_close;
617 hdev->flush = bt3c_hci_flush;
618 hdev->send = bt3c_hci_send_frame;
619 hdev->destruct = bt3c_hci_destruct;
620 hdev->ioctl = bt3c_hci_ioctl;
622 hdev->owner = THIS_MODULE;
624 /* Load firmware */
625 err = request_firmware(&firmware, "BT3CPCC.bin", bt3c_device());
626 if (err < 0) {
627 BT_ERR("Firmware request failed");
628 goto error;
631 err = bt3c_load_firmware(info, firmware->data, firmware->size);
633 release_firmware(firmware);
635 if (err < 0) {
636 BT_ERR("Firmware loading failed");
637 goto error;
640 /* Timeout before it is safe to send the first HCI packet */
641 msleep(1000);
643 /* Register HCI device */
644 err = hci_register_dev(hdev);
645 if (err < 0) {
646 BT_ERR("Can't register HCI device");
647 goto error;
650 return 0;
652 error:
653 info->hdev = NULL;
654 hci_free_dev(hdev);
655 return err;
659 int bt3c_close(bt3c_info_t *info)
661 struct hci_dev *hdev = info->hdev;
663 if (!hdev)
664 return -ENODEV;
666 bt3c_hci_close(hdev);
668 if (hci_unregister_dev(hdev) < 0)
669 BT_ERR("Can't unregister HCI device %s", hdev->name);
671 hci_free_dev(hdev);
673 return 0;
676 dev_link_t *bt3c_attach(void)
678 bt3c_info_t *info;
679 client_reg_t client_reg;
680 dev_link_t *link;
681 int i, ret;
683 /* Create new info device */
684 info = kmalloc(sizeof(*info), GFP_KERNEL);
685 if (!info)
686 return NULL;
687 memset(info, 0, sizeof(*info));
689 link = &info->link;
690 link->priv = info;
692 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
693 link->io.NumPorts1 = 8;
694 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
695 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
697 if (irq_list[0] == -1)
698 link->irq.IRQInfo2 = irq_mask;
699 else
700 for (i = 0; i < 4; i++)
701 link->irq.IRQInfo2 |= 1 << irq_list[i];
703 link->irq.Handler = bt3c_interrupt;
704 link->irq.Instance = info;
706 link->conf.Attributes = CONF_ENABLE_IRQ;
707 link->conf.Vcc = 50;
708 link->conf.IntType = INT_MEMORY_AND_IO;
710 /* Register with Card Services */
711 link->next = dev_list;
712 dev_list = link;
713 client_reg.dev_info = &dev_info;
714 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
715 client_reg.EventMask =
716 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
717 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
718 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
719 client_reg.event_handler = &bt3c_event;
720 client_reg.Version = 0x0210;
721 client_reg.event_callback_args.client_data = link;
723 ret = pcmcia_register_client(&link->handle, &client_reg);
724 if (ret != CS_SUCCESS) {
725 cs_error(link->handle, RegisterClient, ret);
726 bt3c_detach(link);
727 return NULL;
730 return link;
734 void bt3c_detach(dev_link_t *link)
736 bt3c_info_t *info = link->priv;
737 dev_link_t **linkp;
738 int ret;
740 /* Locate device structure */
741 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
742 if (*linkp == link)
743 break;
745 if (*linkp == NULL)
746 return;
748 if (link->state & DEV_CONFIG)
749 bt3c_release(link);
751 if (link->handle) {
752 ret = pcmcia_deregister_client(link->handle);
753 if (ret != CS_SUCCESS)
754 cs_error(link->handle, DeregisterClient, ret);
757 /* Unlink device structure, free bits */
758 *linkp = link->next;
760 kfree(info);
763 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
765 int i;
767 i = pcmcia_get_tuple_data(handle, tuple);
768 if (i != CS_SUCCESS)
769 return i;
771 return pcmcia_parse_tuple(handle, tuple, parse);
774 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
776 if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
777 return CS_NO_MORE_ITEMS;
778 return get_tuple(handle, tuple, parse);
781 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
783 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
784 return CS_NO_MORE_ITEMS;
785 return get_tuple(handle, tuple, parse);
788 void bt3c_config(dev_link_t *link)
790 static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
791 client_handle_t handle = link->handle;
792 bt3c_info_t *info = link->priv;
793 tuple_t tuple;
794 u_short buf[256];
795 cisparse_t parse;
796 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
797 config_info_t config;
798 int i, j, try, last_ret, last_fn;
800 tuple.TupleData = (cisdata_t *)buf;
801 tuple.TupleOffset = 0;
802 tuple.TupleDataMax = 255;
803 tuple.Attributes = 0;
805 /* Get configuration register information */
806 tuple.DesiredTuple = CISTPL_CONFIG;
807 last_ret = first_tuple(handle, &tuple, &parse);
808 if (last_ret != CS_SUCCESS) {
809 last_fn = ParseTuple;
810 goto cs_failed;
812 link->conf.ConfigBase = parse.config.base;
813 link->conf.Present = parse.config.rmask[0];
815 /* Configure card */
816 link->state |= DEV_CONFIG;
817 i = pcmcia_get_configuration_info(handle, &config);
818 link->conf.Vcc = config.Vcc;
820 /* First pass: look for a config entry that looks normal. */
821 tuple.TupleData = (cisdata_t *)buf;
822 tuple.TupleOffset = 0;
823 tuple.TupleDataMax = 255;
824 tuple.Attributes = 0;
825 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
826 /* Two tries: without IO aliases, then with aliases */
827 for (try = 0; try < 2; try++) {
828 i = first_tuple(handle, &tuple, &parse);
829 while (i != CS_NO_MORE_ITEMS) {
830 if (i != CS_SUCCESS)
831 goto next_entry;
832 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
833 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
834 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
835 link->conf.ConfigIndex = cf->index;
836 link->io.BasePort1 = cf->io.win[0].base;
837 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
838 i = pcmcia_request_io(link->handle, &link->io);
839 if (i == CS_SUCCESS)
840 goto found_port;
842 next_entry:
843 i = next_tuple(handle, &tuple, &parse);
847 /* Second pass: try to find an entry that isn't picky about
848 its base address, then try to grab any standard serial port
849 address, and finally try to get any free port. */
850 i = first_tuple(handle, &tuple, &parse);
851 while (i != CS_NO_MORE_ITEMS) {
852 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
853 link->conf.ConfigIndex = cf->index;
854 for (j = 0; j < 5; j++) {
855 link->io.BasePort1 = base[j];
856 link->io.IOAddrLines = base[j] ? 16 : 3;
857 i = pcmcia_request_io(link->handle, &link->io);
858 if (i == CS_SUCCESS)
859 goto found_port;
862 i = next_tuple(handle, &tuple, &parse);
865 found_port:
866 if (i != CS_SUCCESS) {
867 BT_ERR("No usable port range found");
868 cs_error(link->handle, RequestIO, i);
869 goto failed;
872 i = pcmcia_request_irq(link->handle, &link->irq);
873 if (i != CS_SUCCESS) {
874 cs_error(link->handle, RequestIRQ, i);
875 link->irq.AssignedIRQ = 0;
878 i = pcmcia_request_configuration(link->handle, &link->conf);
879 if (i != CS_SUCCESS) {
880 cs_error(link->handle, RequestConfiguration, i);
881 goto failed;
884 if (bt3c_open(info) != 0)
885 goto failed;
887 strcpy(info->node.dev_name, info->hdev->name);
888 link->dev = &info->node;
889 link->state &= ~DEV_CONFIG_PENDING;
891 return;
893 cs_failed:
894 cs_error(link->handle, last_fn, last_ret);
896 failed:
897 bt3c_release(link);
901 void bt3c_release(dev_link_t *link)
903 bt3c_info_t *info = link->priv;
905 if (link->state & DEV_PRESENT)
906 bt3c_close(info);
908 link->dev = NULL;
910 pcmcia_release_configuration(link->handle);
911 pcmcia_release_io(link->handle, &link->io);
912 pcmcia_release_irq(link->handle, &link->irq);
914 link->state &= ~DEV_CONFIG;
918 int bt3c_event(event_t event, int priority, event_callback_args_t *args)
920 dev_link_t *link = args->client_data;
921 bt3c_info_t *info = link->priv;
923 switch (event) {
924 case CS_EVENT_CARD_REMOVAL:
925 link->state &= ~DEV_PRESENT;
926 if (link->state & DEV_CONFIG) {
927 bt3c_close(info);
928 bt3c_release(link);
930 break;
931 case CS_EVENT_CARD_INSERTION:
932 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
933 bt3c_config(link);
934 break;
935 case CS_EVENT_PM_SUSPEND:
936 link->state |= DEV_SUSPEND;
937 /* Fall through... */
938 case CS_EVENT_RESET_PHYSICAL:
939 if (link->state & DEV_CONFIG)
940 pcmcia_release_configuration(link->handle);
941 break;
942 case CS_EVENT_PM_RESUME:
943 link->state &= ~DEV_SUSPEND;
944 /* Fall through... */
945 case CS_EVENT_CARD_RESET:
946 if (DEV_OK(link))
947 pcmcia_request_configuration(link->handle, &link->conf);
948 break;
951 return 0;
954 static struct pcmcia_driver bt3c_driver = {
955 .owner = THIS_MODULE,
956 .drv = {
957 .name = "bt3c_cs",
959 .attach = bt3c_attach,
960 .detach = bt3c_detach,
963 static int __init init_bt3c_cs(void)
965 return pcmcia_register_driver(&bt3c_driver);
969 static void __exit exit_bt3c_cs(void)
971 pcmcia_unregister_driver(&bt3c_driver);
973 /* XXX: this really needs to move into generic code.. */
974 while (dev_list != NULL)
975 bt3c_detach(dev_list);
978 module_init(init_bt3c_cs);
979 module_exit(exit_bt3c_cs);