[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / drivers / bluetooth / bt3c_cs.c
blobf71e5c76963d27abf5a4d14a0b0767225e513df7
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>
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>
45 #include <asm/io.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 {
76 dev_link_t link;
77 dev_node_t node;
79 struct hci_dev *hdev;
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;
89 } bt3c_info_t;
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 ======================== */
121 #define DATA_L 0
122 #define DATA_H 1
123 #define ADDR_L 2
124 #define ADDR_H 3
125 #define CONTROL 4
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;
155 return value;
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)
173 int actual = 0;
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]);
181 actual++;
184 bt3c_io_write(iobase, 0x7005, actual);
186 return actual;
190 static void bt3c_write_wakeup(bt3c_info_t *info)
192 if (!info) {
193 BT_ERR("Unknown device");
194 return;
197 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
198 return;
200 do {
201 register unsigned int iobase = info->link.io.BasePort1;
202 register struct sk_buff *skb;
203 register int len;
205 if (!(info->link.state & DEV_PRESENT))
206 break;
209 if (!(skb = skb_dequeue(&(info->txq)))) {
210 clear_bit(XMIT_SENDING, &(info->tx_state));
211 break;
214 /* Send frame */
215 len = bt3c_write(iobase, 256, skb->data, skb->len);
217 if (len != skb->len) {
218 BT_ERR("Very strange");
221 kfree_skb(skb);
223 info->hdev->stat.byte_tx += len;
225 } while (0);
229 static void bt3c_receive(bt3c_info_t *info)
231 unsigned int iobase;
232 int size = 0, avail;
234 if (!info) {
235 BT_ERR("Unknown device");
236 return;
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) {
246 size++;
247 info->hdev->stat.byte_rx++;
249 /* Allocate packet */
250 if (info->rx_skb == NULL) {
251 info->rx_state = RECV_WAIT_PACKET_TYPE;
252 info->rx_count = 0;
253 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
254 BT_ERR("Can't allocate mem for new packet");
255 return;
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) {
269 case HCI_EVENT_PKT:
270 info->rx_state = RECV_WAIT_EVENT_HEADER;
271 info->rx_count = HCI_EVENT_HDR_SIZE;
272 break;
274 case HCI_ACLDATA_PKT:
275 info->rx_state = RECV_WAIT_ACL_HEADER;
276 info->rx_count = HCI_ACL_HDR_SIZE;
277 break;
279 case HCI_SCODATA_PKT:
280 info->rx_state = RECV_WAIT_SCO_HEADER;
281 info->rx_count = HCI_SCO_HDR_SIZE;
282 break;
284 default:
285 /* Unknown packet */
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);
291 info->rx_skb = NULL;
292 break;
296 } else {
298 __u8 x = inb(iobase + DATA_L);
300 *skb_put(info->rx_skb, 1) = x;
301 inb(iobase + DATA_H);
302 info->rx_count--;
304 if (info->rx_count == 0) {
306 int dlen;
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;
317 break;
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;
324 break;
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;
330 break;
332 case RECV_WAIT_DATA:
333 hci_recv_frame(info->rx_skb);
334 info->rx_skb = NULL;
335 break;
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;
352 unsigned int iobase;
353 int iir;
355 if (!info || !info->hdev) {
356 BT_ERR("Call of irq %d for unknown device", irq);
357 return IRQ_NONE;
360 iobase = info->link.io.BasePort1;
362 spin_lock(&(info->lock));
364 iir = inb(iobase + CONTROL);
365 if (iir & 0x80) {
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) {
371 if (stat & 0x0020) {
372 int stat = bt3c_read(iobase, 0x7002) & 0x10;
373 BT_INFO("%s: Antenna %s", info->hdev->name,
374 stat ? "out" : "in");
376 if (stat & 0x0001)
377 bt3c_receive(info);
378 if (stat & 0x0002) {
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));
392 return IRQ_HANDLED;
397 /* ======================== HCI interface ======================== */
400 static int bt3c_hci_flush(struct hci_dev *hdev)
402 bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
404 /* Drop TX queue */
405 skb_queue_purge(&(info->txq));
407 return 0;
411 static int bt3c_hci_open(struct hci_dev *hdev)
413 set_bit(HCI_RUNNING, &(hdev->flags));
415 return 0;
419 static int bt3c_hci_close(struct hci_dev *hdev)
421 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
422 return 0;
424 bt3c_hci_flush(hdev);
426 return 0;
430 static int bt3c_hci_send_frame(struct sk_buff *skb)
432 bt3c_info_t *info;
433 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
434 unsigned long flags;
436 if (!hdev) {
437 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
438 return -ENODEV;
441 info = (bt3c_info_t *) (hdev->driver_data);
443 switch (skb->pkt_type) {
444 case HCI_COMMAND_PKT:
445 hdev->stat.cmd_tx++;
446 break;
447 case HCI_ACLDATA_PKT:
448 hdev->stat.acl_tx++;
449 break;
450 case HCI_SCODATA_PKT:
451 hdev->stat.sco_tx++;
452 break;
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);
465 return 0;
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)
476 return -ENOIOCTLCMD;
481 /* ======================== Card services HCI interaction ======================== */
484 static struct device *bt3c_device(void)
486 static struct device dev = {
487 .bus_id = "pcmcia",
489 kobject_set_name(&dev.kobj, "bt3c");
490 kobject_init(&dev.kobj);
492 return &dev;
496 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
498 char *ptr = (char *) firmware;
499 char b[9];
500 unsigned int iobase, size, addr, fcs, tmp;
501 int i, err = 0;
503 iobase = info->link.io.BasePort1;
505 /* Reset */
506 bt3c_io_write(iobase, 0x8040, 0x0404);
507 bt3c_io_write(iobase, 0x8040, 0x0400);
509 udelay(1);
511 bt3c_io_write(iobase, 0x8040, 0x0404);
513 udelay(17);
515 /* Load */
516 while (count) {
517 if (ptr[0] != 'S') {
518 BT_ERR("Bad address in firmware");
519 err = -EFAULT;
520 goto error;
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");
543 err = -EILSEQ;
544 goto error;
547 if (ptr[1] == '3') {
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;
562 udelay(17);
564 /* Boot */
565 bt3c_address(iobase, 0x3000);
566 outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
568 error:
569 udelay(17);
571 /* Clear */
572 bt3c_io_write(iobase, 0x7006, 0x0000);
573 bt3c_io_write(iobase, 0x7005, 0x0000);
574 bt3c_io_write(iobase, 0x7001, 0x0000);
576 return err;
580 static int bt3c_open(bt3c_info_t *info)
582 const struct firmware *firmware;
583 struct hci_dev *hdev;
584 int err;
586 spin_lock_init(&(info->lock));
588 skb_queue_head_init(&(info->txq));
590 info->rx_state = RECV_WAIT_PACKET_TYPE;
591 info->rx_count = 0;
592 info->rx_skb = NULL;
594 /* Initialize HCI device */
595 hdev = hci_alloc_dev();
596 if (!hdev) {
597 BT_ERR("Can't allocate HCI device");
598 return -ENOMEM;
601 info->hdev = hdev;
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;
615 /* Load firmware */
616 err = request_firmware(&firmware, "BT3CPCC.bin", bt3c_device());
617 if (err < 0) {
618 BT_ERR("Firmware request failed");
619 goto error;
622 err = bt3c_load_firmware(info, firmware->data, firmware->size);
624 release_firmware(firmware);
626 if (err < 0) {
627 BT_ERR("Firmware loading failed");
628 goto error;
631 /* Timeout before it is safe to send the first HCI packet */
632 msleep(1000);
634 /* Register HCI device */
635 err = hci_register_dev(hdev);
636 if (err < 0) {
637 BT_ERR("Can't register HCI device");
638 goto error;
641 return 0;
643 error:
644 info->hdev = NULL;
645 hci_free_dev(hdev);
646 return err;
650 static int bt3c_close(bt3c_info_t *info)
652 struct hci_dev *hdev = info->hdev;
654 if (!hdev)
655 return -ENODEV;
657 bt3c_hci_close(hdev);
659 if (hci_unregister_dev(hdev) < 0)
660 BT_ERR("Can't unregister HCI device %s", hdev->name);
662 hci_free_dev(hdev);
664 return 0;
667 static dev_link_t *bt3c_attach(void)
669 bt3c_info_t *info;
670 client_reg_t client_reg;
671 dev_link_t *link;
672 int ret;
674 /* Create new info device */
675 info = kmalloc(sizeof(*info), GFP_KERNEL);
676 if (!info)
677 return NULL;
678 memset(info, 0, sizeof(*info));
680 link = &info->link;
681 link->priv = 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;
692 link->conf.Vcc = 50;
693 link->conf.IntType = INT_MEMORY_AND_IO;
695 /* Register with Card Services */
696 link->next = dev_list;
697 dev_list = link;
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);
710 bt3c_detach(link);
711 return NULL;
714 return link;
718 static void bt3c_detach(dev_link_t *link)
720 bt3c_info_t *info = link->priv;
721 dev_link_t **linkp;
722 int ret;
724 /* Locate device structure */
725 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
726 if (*linkp == link)
727 break;
729 if (*linkp == NULL)
730 return;
732 if (link->state & DEV_CONFIG)
733 bt3c_release(link);
735 if (link->handle) {
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 */
742 *linkp = link->next;
744 kfree(info);
747 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
749 int i;
751 i = pcmcia_get_tuple_data(handle, tuple);
752 if (i != CS_SUCCESS)
753 return i;
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;
777 tuple_t tuple;
778 u_short buf[256];
779 cisparse_t parse;
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;
794 goto cs_failed;
796 link->conf.ConfigBase = parse.config.base;
797 link->conf.Present = parse.config.rmask[0];
799 /* Configure card */
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) {
814 if (i != CS_SUCCESS)
815 goto next_entry;
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);
823 if (i == CS_SUCCESS)
824 goto found_port;
826 next_entry:
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);
842 if (i == CS_SUCCESS)
843 goto found_port;
846 i = next_tuple(handle, &tuple, &parse);
849 found_port:
850 if (i != CS_SUCCESS) {
851 BT_ERR("No usable port range found");
852 cs_error(link->handle, RequestIO, i);
853 goto failed;
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);
865 goto failed;
868 if (bt3c_open(info) != 0)
869 goto failed;
871 strcpy(info->node.dev_name, info->hdev->name);
872 link->dev = &info->node;
873 link->state &= ~DEV_CONFIG_PENDING;
875 return;
877 cs_failed:
878 cs_error(link->handle, last_fn, last_ret);
880 failed:
881 bt3c_release(link);
885 static void bt3c_release(dev_link_t *link)
887 bt3c_info_t *info = link->priv;
889 if (link->state & DEV_PRESENT)
890 bt3c_close(info);
892 link->dev = NULL;
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;
907 switch (event) {
908 case CS_EVENT_CARD_REMOVAL:
909 link->state &= ~DEV_PRESENT;
910 if (link->state & DEV_CONFIG) {
911 bt3c_close(info);
912 bt3c_release(link);
914 break;
915 case CS_EVENT_CARD_INSERTION:
916 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
917 bt3c_config(link);
918 break;
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);
925 break;
926 case CS_EVENT_PM_RESUME:
927 link->state &= ~DEV_SUSPEND;
928 /* Fall through... */
929 case CS_EVENT_CARD_RESET:
930 if (DEV_OK(link))
931 pcmcia_request_configuration(link->handle, &link->conf);
932 break;
935 return 0;
938 static struct pcmcia_driver bt3c_driver = {
939 .owner = THIS_MODULE,
940 .drv = {
941 .name = "bt3c_cs",
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);