patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / bluetooth / bt3c_cs.c
1 /*
2  *
3  *  Driver for the 3Com Bluetooth PCMCIA card
4  *
5  *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
6  *                           Jose Orlando Pereira <jop@di.uminho.pt>
7  *
8  *
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;
12  *
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.
17  *
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.
21  *
22  */
23
24 #include <linux/config.h>
25 #include <linux/module.h>
26
27 #include <linux/kernel.h>
28 #include <linux/kmod.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/types.h>
33 #include <linux/sched.h>
34 #include <linux/errno.h>
35 #include <linux/ptrace.h>
36 #include <linux/ioport.h>
37 #include <linux/spinlock.h>
38
39 #include <linux/skbuff.h>
40 #include <linux/string.h>
41 #include <linux/serial.h>
42 #include <linux/serial_reg.h>
43 #include <asm/system.h>
44 #include <asm/bitops.h>
45 #include <asm/io.h>
46
47 #include <linux/device.h>
48 #include <linux/firmware.h>
49
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>
57
58 #include <net/bluetooth/bluetooth.h>
59 #include <net/bluetooth/hci_core.h>
60
61
62
63 /* ======================== Module parameters ======================== */
64
65
66 /* Bit map of interrupts to choose from */
67 static u_int irq_mask = 0xffff;
68 static int irq_list[4] = { -1 };
69
70 MODULE_PARM(irq_mask, "i");
71 MODULE_PARM(irq_list, "1-4i");
72
73 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
74 MODULE_DESCRIPTION("Bluetooth driver for the 3Com Bluetooth PCMCIA card");
75 MODULE_LICENSE("GPL");
76
77
78
79 /* ======================== Local structures ======================== */
80
81
82 typedef struct bt3c_info_t {
83         dev_link_t link;
84         dev_node_t node;
85
86         struct hci_dev *hdev;
87
88         spinlock_t lock;                /* For serializing operations */
89
90         struct sk_buff_head txq;
91         unsigned long tx_state;
92
93         unsigned long rx_state;
94         unsigned long rx_count;
95         struct sk_buff *rx_skb;
96 } bt3c_info_t;
97
98
99 void bt3c_config(dev_link_t *link);
100 void bt3c_release(dev_link_t *link);
101 int bt3c_event(event_t event, int priority, event_callback_args_t *args);
102
103 static dev_info_t dev_info = "bt3c_cs";
104
105 dev_link_t *bt3c_attach(void);
106 void bt3c_detach(dev_link_t *);
107
108 static dev_link_t *dev_list = NULL;
109
110
111 /* Transmit states  */
112 #define XMIT_SENDING  1
113 #define XMIT_WAKEUP   2
114 #define XMIT_WAITING  8
115
116 /* Receiver states */
117 #define RECV_WAIT_PACKET_TYPE   0
118 #define RECV_WAIT_EVENT_HEADER  1
119 #define RECV_WAIT_ACL_HEADER    2
120 #define RECV_WAIT_SCO_HEADER    3
121 #define RECV_WAIT_DATA          4
122
123
124
125 /* ======================== Special I/O functions ======================== */
126
127
128 #define DATA_L   0
129 #define DATA_H   1
130 #define ADDR_L   2
131 #define ADDR_H   3
132 #define CONTROL  4
133
134
135 inline void bt3c_address(unsigned int iobase, unsigned short addr)
136 {
137         outb(addr & 0xff, iobase + ADDR_L);
138         outb((addr >> 8) & 0xff, iobase + ADDR_H);
139 }
140
141
142 inline void bt3c_put(unsigned int iobase, unsigned short value)
143 {
144         outb(value & 0xff, iobase + DATA_L);
145         outb((value >> 8) & 0xff, iobase + DATA_H);
146 }
147
148
149 inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
150 {
151         bt3c_address(iobase, addr);
152         bt3c_put(iobase, value);
153 }
154
155
156 inline unsigned short bt3c_get(unsigned int iobase)
157 {
158         unsigned short value = inb(iobase + DATA_L);
159
160         value |= inb(iobase + DATA_H) << 8;
161
162         return value;
163 }
164
165
166 inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
167 {
168         bt3c_address(iobase, addr);
169
170         return bt3c_get(iobase);
171 }
172
173
174
175 /* ======================== Interrupt handling ======================== */
176
177
178 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
179 {
180         int actual = 0;
181
182         bt3c_address(iobase, 0x7080);
183
184         /* Fill FIFO with current frame */
185         while (actual < len) {
186                 /* Transmit next byte */
187                 bt3c_put(iobase, buf[actual]);
188                 actual++;
189         }
190
191         bt3c_io_write(iobase, 0x7005, actual);
192
193         return actual;
194 }
195
196
197 static void bt3c_write_wakeup(bt3c_info_t *info, int from)
198 {
199         unsigned long flags;
200
201         if (!info) {
202                 BT_ERR("Unknown device");
203                 return;
204         }
205
206         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
207                 return;
208
209         spin_lock_irqsave(&(info->lock), flags);
210
211         do {
212                 register unsigned int iobase = info->link.io.BasePort1;
213                 register struct sk_buff *skb;
214                 register int len;
215
216                 if (!(info->link.state & DEV_PRESENT))
217                         break;
218
219
220                 if (!(skb = skb_dequeue(&(info->txq)))) {
221                         clear_bit(XMIT_SENDING, &(info->tx_state));
222                         break;
223                 }
224
225                 /* Send frame */
226                 len = bt3c_write(iobase, 256, skb->data, skb->len);
227
228                 if (len != skb->len) {
229                         BT_ERR("Very strange");
230                 }
231
232                 kfree_skb(skb);
233
234                 info->hdev->stat.byte_tx += len;
235
236         } while (0);
237
238         spin_unlock_irqrestore(&(info->lock), flags);
239 }
240
241
242 static void bt3c_receive(bt3c_info_t *info)
243 {
244         unsigned int iobase;
245         int size = 0, avail;
246
247         if (!info) {
248                 BT_ERR("Unknown device");
249                 return;
250         }
251
252         iobase = info->link.io.BasePort1;
253
254         avail = bt3c_read(iobase, 0x7006);
255         //printk("bt3c_cs: receiving %d bytes\n", avail);
256
257         bt3c_address(iobase, 0x7480);
258         while (size < avail) {
259                 size++;
260                 info->hdev->stat.byte_rx++;
261
262                 /* Allocate packet */
263                 if (info->rx_skb == NULL) {
264                         info->rx_state = RECV_WAIT_PACKET_TYPE;
265                         info->rx_count = 0;
266                         if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
267                                 BT_ERR("Can't allocate mem for new packet");
268                                 return;
269                         }
270                 }
271
272
273                 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
274
275                         info->rx_skb->dev = (void *) info->hdev;
276                         info->rx_skb->pkt_type = inb(iobase + DATA_L);
277                         inb(iobase + DATA_H);
278                         //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
279
280                         switch (info->rx_skb->pkt_type) {
281
282                         case HCI_EVENT_PKT:
283                                 info->rx_state = RECV_WAIT_EVENT_HEADER;
284                                 info->rx_count = HCI_EVENT_HDR_SIZE;
285                                 break;
286
287                         case HCI_ACLDATA_PKT:
288                                 info->rx_state = RECV_WAIT_ACL_HEADER;
289                                 info->rx_count = HCI_ACL_HDR_SIZE;
290                                 break;
291
292                         case HCI_SCODATA_PKT:
293                                 info->rx_state = RECV_WAIT_SCO_HEADER;
294                                 info->rx_count = HCI_SCO_HDR_SIZE;
295                                 break;
296
297                         default:
298                                 /* Unknown packet */
299                                 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
300                                 info->hdev->stat.err_rx++;
301                                 clear_bit(HCI_RUNNING, &(info->hdev->flags));
302
303                                 kfree_skb(info->rx_skb);
304                                 info->rx_skb = NULL;
305                                 break;
306
307                         }
308
309                 } else {
310
311                         __u8 x = inb(iobase + DATA_L);
312
313                         *skb_put(info->rx_skb, 1) = x;
314                         inb(iobase + DATA_H);
315                         info->rx_count--;
316
317                         if (info->rx_count == 0) {
318
319                                 int dlen;
320                                 struct hci_event_hdr *eh;
321                                 struct hci_acl_hdr *ah;
322                                 struct hci_sco_hdr *sh;
323
324                                 switch (info->rx_state) {
325
326                                 case RECV_WAIT_EVENT_HEADER:
327                                         eh = (struct hci_event_hdr *)(info->rx_skb->data);
328                                         info->rx_state = RECV_WAIT_DATA;
329                                         info->rx_count = eh->plen;
330                                         break;
331
332                                 case RECV_WAIT_ACL_HEADER:
333                                         ah = (struct hci_acl_hdr *)(info->rx_skb->data);
334                                         dlen = __le16_to_cpu(ah->dlen);
335                                         info->rx_state = RECV_WAIT_DATA;
336                                         info->rx_count = dlen;
337                                         break;
338
339                                 case RECV_WAIT_SCO_HEADER:
340                                         sh = (struct hci_sco_hdr *)(info->rx_skb->data);
341                                         info->rx_state = RECV_WAIT_DATA;
342                                         info->rx_count = sh->dlen;
343                                         break;
344
345                                 case RECV_WAIT_DATA:
346                                         hci_recv_frame(info->rx_skb);
347                                         info->rx_skb = NULL;
348                                         break;
349
350                                 }
351
352                         }
353
354                 }
355
356         }
357
358         bt3c_io_write(iobase, 0x7006, 0x0000);
359 }
360
361
362 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
363 {
364         bt3c_info_t *info = dev_inst;
365         unsigned int iobase;
366         int iir;
367
368         if (!info || !info->hdev) {
369                 BT_ERR("Call of irq %d for unknown device", irq);
370                 return IRQ_NONE;
371         }
372
373         iobase = info->link.io.BasePort1;
374
375         spin_lock(&(info->lock));
376
377         iir = inb(iobase + CONTROL);
378         if (iir & 0x80) {
379                 int stat = bt3c_read(iobase, 0x7001);
380
381                 if ((stat & 0xff) == 0x7f) {
382                         BT_ERR("Very strange (stat=0x%04x)", stat);
383                 } else if ((stat & 0xff) != 0xff) {
384                         if (stat & 0x0020) {
385                                 int stat = bt3c_read(iobase, 0x7002) & 0x10;
386                                 BT_INFO("%s: Antenna %s", info->hdev->name,
387                                                         stat ? "out" : "in");
388                         }
389                         if (stat & 0x0001)
390                                 bt3c_receive(info);
391                         if (stat & 0x0002) {
392                                 //BT_ERR("Ack (stat=0x%04x)", stat);
393                                 clear_bit(XMIT_SENDING, &(info->tx_state));
394                                 bt3c_write_wakeup(info, 1);
395                         }
396
397                         bt3c_io_write(iobase, 0x7001, 0x0000);
398
399                         outb(iir, iobase + CONTROL);
400                 }
401         }
402
403         spin_unlock(&(info->lock));
404
405         return IRQ_HANDLED;
406 }
407
408
409
410 /* ======================== HCI interface ======================== */
411
412
413 static int bt3c_hci_flush(struct hci_dev *hdev)
414 {
415         bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
416
417         /* Drop TX queue */
418         skb_queue_purge(&(info->txq));
419
420         return 0;
421 }
422
423
424 static int bt3c_hci_open(struct hci_dev *hdev)
425 {
426         set_bit(HCI_RUNNING, &(hdev->flags));
427
428         return 0;
429 }
430
431
432 static int bt3c_hci_close(struct hci_dev *hdev)
433 {
434         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
435                 return 0;
436
437         bt3c_hci_flush(hdev);
438
439         return 0;
440 }
441
442
443 static int bt3c_hci_send_frame(struct sk_buff *skb)
444 {
445         bt3c_info_t *info;
446         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
447
448         if (!hdev) {
449                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
450                 return -ENODEV;
451         }
452
453         info = (bt3c_info_t *) (hdev->driver_data);
454
455         switch (skb->pkt_type) {
456         case HCI_COMMAND_PKT:
457                 hdev->stat.cmd_tx++;
458                 break;
459         case HCI_ACLDATA_PKT:
460                 hdev->stat.acl_tx++;
461                 break;
462         case HCI_SCODATA_PKT:
463                 hdev->stat.sco_tx++;
464                 break;
465         };
466
467         /* Prepend skb with frame type */
468         memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
469         skb_queue_tail(&(info->txq), skb);
470
471         bt3c_write_wakeup(info, 0);
472
473         return 0;
474 }
475
476
477 static void bt3c_hci_destruct(struct hci_dev *hdev)
478 {
479 }
480
481
482 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
483 {
484         return -ENOIOCTLCMD;
485 }
486
487
488
489 /* ======================== Card services HCI interaction ======================== */
490
491
492 static struct device bt3c_device = {
493         .bus_id = "pcmcia",
494         .kobj = {
495                 .k_name = "bt3c"
496         }
497 };
498
499
500 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
501 {
502         char *ptr = (char *) firmware;
503         char b[9];
504         unsigned int iobase, size, addr, fcs, tmp;
505         int i, err = 0;
506
507         iobase = info->link.io.BasePort1;
508
509         /* Reset */
510         bt3c_io_write(iobase, 0x8040, 0x0404);
511         bt3c_io_write(iobase, 0x8040, 0x0400);
512
513         udelay(1);
514
515         bt3c_io_write(iobase, 0x8040, 0x0404);
516
517         udelay(17);
518
519         /* Load */
520         while (count) {
521                 if (ptr[0] != 'S') {
522                         BT_ERR("Bad address in firmware");
523                         err = -EFAULT;
524                         goto error;
525                 }
526
527                 memset(b, 0, sizeof(b));
528                 memcpy(b, ptr + 2, 2);
529                 size = simple_strtol(b, NULL, 16);
530
531                 memset(b, 0, sizeof(b));
532                 memcpy(b, ptr + 4, 8);
533                 addr = simple_strtol(b, NULL, 16);
534
535                 memset(b, 0, sizeof(b));
536                 memcpy(b, ptr + (size * 2) + 2, 2);
537                 fcs = simple_strtol(b, NULL, 16);
538
539                 memset(b, 0, sizeof(b));
540                 for (tmp = 0, i = 0; i < size; i++) {
541                         memcpy(b, ptr + (i * 2) + 2, 2);
542                         tmp += simple_strtol(b, NULL, 16);
543                 }
544
545                 if (((tmp + fcs) & 0xff) != 0xff) {
546                         BT_ERR("Checksum error in firmware");
547                         err = -EILSEQ;
548                         goto error;
549                 }
550
551                 if (ptr[1] == '3') {
552                         bt3c_address(iobase, addr);
553
554                         memset(b, 0, sizeof(b));
555                         for (i = 0; i < (size - 4) / 2; i++) {
556                                 memcpy(b, ptr + (i * 4) + 12, 4);
557                                 tmp = simple_strtol(b, NULL, 16);
558                                 bt3c_put(iobase, tmp);
559                         }
560                 }
561
562                 ptr   += (size * 2) + 6;
563                 count -= (size * 2) + 6;
564         }
565
566         udelay(17);
567
568         /* Boot */
569         bt3c_address(iobase, 0x3000);
570         outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
571
572 error:
573         udelay(17);
574
575         /* Clear */
576         bt3c_io_write(iobase, 0x7006, 0x0000);
577         bt3c_io_write(iobase, 0x7005, 0x0000);
578         bt3c_io_write(iobase, 0x7001, 0x0000);
579
580         return err;
581 }
582
583
584 int bt3c_open(bt3c_info_t *info)
585 {
586         const struct firmware *firmware;
587         struct hci_dev *hdev;
588         int err;
589
590         spin_lock_init(&(info->lock));
591
592         skb_queue_head_init(&(info->txq));
593
594         info->rx_state = RECV_WAIT_PACKET_TYPE;
595         info->rx_count = 0;
596         info->rx_skb = NULL;
597
598         /* Initialize HCI device */
599         hdev = hci_alloc_dev();
600         if (!hdev) {
601                 BT_ERR("Can't allocate HCI device");
602                 return -ENOMEM;
603         }
604
605         info->hdev = hdev;
606
607         hdev->type = HCI_PCCARD;
608         hdev->driver_data = info;
609
610         hdev->open     = bt3c_hci_open;
611         hdev->close    = bt3c_hci_close;
612         hdev->flush    = bt3c_hci_flush;
613         hdev->send     = bt3c_hci_send_frame;
614         hdev->destruct = bt3c_hci_destruct;
615         hdev->ioctl    = bt3c_hci_ioctl;
616
617         hdev->owner = THIS_MODULE;
618
619         /* Load firmware */
620         err = request_firmware(&firmware, "BT3CPCC.bin", &bt3c_device);
621         if (err < 0) {
622                 BT_ERR("Firmware request failed");
623                 goto error;
624         }
625
626         err = bt3c_load_firmware(info, firmware->data, firmware->size);
627
628         release_firmware(firmware);
629
630         if (err < 0) {
631                 BT_ERR("Firmware loading failed");
632                 goto error;
633         }
634
635         /* Timeout before it is safe to send the first HCI packet */
636         set_current_state(TASK_INTERRUPTIBLE);
637         schedule_timeout(HZ);
638
639         /* Register HCI device */
640         err = hci_register_dev(hdev);
641         if (err < 0) {
642                 BT_ERR("Can't register HCI device");
643                 goto error;
644         }
645
646         return 0;
647
648 error:
649         info->hdev = NULL;
650         hci_free_dev(hdev);
651         return err;
652 }
653
654
655 int bt3c_close(bt3c_info_t *info)
656 {
657         struct hci_dev *hdev = info->hdev;
658
659         if (!hdev)
660                 return -ENODEV;
661
662         bt3c_hci_close(hdev);
663
664         if (hci_unregister_dev(hdev) < 0)
665                 BT_ERR("Can't unregister HCI device %s", hdev->name);
666
667         hci_free_dev(hdev);
668
669         return 0;
670 }
671
672 dev_link_t *bt3c_attach(void)
673 {
674         bt3c_info_t *info;
675         client_reg_t client_reg;
676         dev_link_t *link;
677         int i, ret;
678
679         /* Create new info device */
680         info = kmalloc(sizeof(*info), GFP_KERNEL);
681         if (!info)
682                 return NULL;
683         memset(info, 0, sizeof(*info));
684
685         link = &info->link;
686         link->priv = info;
687
688         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
689         link->io.NumPorts1 = 8;
690         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
691         link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
692
693         if (irq_list[0] == -1)
694                 link->irq.IRQInfo2 = irq_mask;
695         else
696                 for (i = 0; i < 4; i++)
697                         link->irq.IRQInfo2 |= 1 << irq_list[i];
698
699         link->irq.Handler = bt3c_interrupt;
700         link->irq.Instance = info;
701
702         link->conf.Attributes = CONF_ENABLE_IRQ;
703         link->conf.Vcc = 50;
704         link->conf.IntType = INT_MEMORY_AND_IO;
705
706         /* Register with Card Services */
707         link->next = dev_list;
708         dev_list = link;
709         client_reg.dev_info = &dev_info;
710         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
711         client_reg.EventMask =
712             CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
713             CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
714             CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
715         client_reg.event_handler = &bt3c_event;
716         client_reg.Version = 0x0210;
717         client_reg.event_callback_args.client_data = link;
718
719         ret = pcmcia_register_client(&link->handle, &client_reg);
720         if (ret != CS_SUCCESS) {
721                 cs_error(link->handle, RegisterClient, ret);
722                 bt3c_detach(link);
723                 return NULL;
724         }
725
726         return link;
727 }
728
729
730 void bt3c_detach(dev_link_t *link)
731 {
732         bt3c_info_t *info = link->priv;
733         dev_link_t **linkp;
734         int ret;
735
736         /* Locate device structure */
737         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
738                 if (*linkp == link)
739                         break;
740
741         if (*linkp == NULL)
742                 return;
743
744         if (link->state & DEV_CONFIG)
745                 bt3c_release(link);
746
747         if (link->handle) {
748                 ret = pcmcia_deregister_client(link->handle);
749                 if (ret != CS_SUCCESS)
750                         cs_error(link->handle, DeregisterClient, ret);
751         }
752
753         /* Unlink device structure, free bits */
754         *linkp = link->next;
755
756         kfree(info);
757 }
758
759 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
760 {
761         int i;
762
763         i = pcmcia_get_tuple_data(handle, tuple);
764         if (i != CS_SUCCESS)
765                 return i;
766
767         return pcmcia_parse_tuple(handle, tuple, parse);
768 }
769
770 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
771 {
772         if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
773                 return CS_NO_MORE_ITEMS;
774         return get_tuple(handle, tuple, parse);
775 }
776
777 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
778 {
779         if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
780                 return CS_NO_MORE_ITEMS;
781         return get_tuple(handle, tuple, parse);
782 }
783
784 void bt3c_config(dev_link_t *link)
785 {
786         static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
787         client_handle_t handle = link->handle;
788         bt3c_info_t *info = link->priv;
789         tuple_t tuple;
790         u_short buf[256];
791         cisparse_t parse;
792         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
793         config_info_t config;
794         int i, j, try, last_ret, last_fn;
795
796         tuple.TupleData = (cisdata_t *)buf;
797         tuple.TupleOffset = 0;
798         tuple.TupleDataMax = 255;
799         tuple.Attributes = 0;
800
801         /* Get configuration register information */
802         tuple.DesiredTuple = CISTPL_CONFIG;
803         last_ret = first_tuple(handle, &tuple, &parse);
804         if (last_ret != CS_SUCCESS) {
805                 last_fn = ParseTuple;
806                 goto cs_failed;
807         }
808         link->conf.ConfigBase = parse.config.base;
809         link->conf.Present = parse.config.rmask[0];
810
811         /* Configure card */
812         link->state |= DEV_CONFIG;
813         i = pcmcia_get_configuration_info(handle, &config);
814         link->conf.Vcc = config.Vcc;
815
816         /* First pass: look for a config entry that looks normal. */
817         tuple.TupleData = (cisdata_t *)buf;
818         tuple.TupleOffset = 0;
819         tuple.TupleDataMax = 255;
820         tuple.Attributes = 0;
821         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
822         /* Two tries: without IO aliases, then with aliases */
823         for (try = 0; try < 2; try++) {
824                 i = first_tuple(handle, &tuple, &parse);
825                 while (i != CS_NO_MORE_ITEMS) {
826                         if (i != CS_SUCCESS)
827                                 goto next_entry;
828                         if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
829                                 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
830                         if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
831                                 link->conf.ConfigIndex = cf->index;
832                                 link->io.BasePort1 = cf->io.win[0].base;
833                                 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
834                                 i = pcmcia_request_io(link->handle, &link->io);
835                                 if (i == CS_SUCCESS)
836                                         goto found_port;
837                         }
838 next_entry:
839                         i = next_tuple(handle, &tuple, &parse);
840                 }
841         }
842
843         /* Second pass: try to find an entry that isn't picky about
844            its base address, then try to grab any standard serial port
845            address, and finally try to get any free port. */
846         i = first_tuple(handle, &tuple, &parse);
847         while (i != CS_NO_MORE_ITEMS) {
848                 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
849                         link->conf.ConfigIndex = cf->index;
850                         for (j = 0; j < 5; j++) {
851                                 link->io.BasePort1 = base[j];
852                                 link->io.IOAddrLines = base[j] ? 16 : 3;
853                                 i = pcmcia_request_io(link->handle, &link->io);
854                                 if (i == CS_SUCCESS)
855                                         goto found_port;
856                         }
857                 }
858                 i = next_tuple(handle, &tuple, &parse);
859         }
860
861 found_port:
862         if (i != CS_SUCCESS) {
863                 BT_ERR("No usable port range found");
864                 cs_error(link->handle, RequestIO, i);
865                 goto failed;
866         }
867
868         i = pcmcia_request_irq(link->handle, &link->irq);
869         if (i != CS_SUCCESS) {
870                 cs_error(link->handle, RequestIRQ, i);
871                 link->irq.AssignedIRQ = 0;
872         }
873
874         i = pcmcia_request_configuration(link->handle, &link->conf);
875         if (i != CS_SUCCESS) {
876                 cs_error(link->handle, RequestConfiguration, i);
877                 goto failed;
878         }
879
880         if (bt3c_open(info) != 0)
881                 goto failed;
882
883         strcpy(info->node.dev_name, info->hdev->name);
884         link->dev = &info->node;
885         link->state &= ~DEV_CONFIG_PENDING;
886
887         return;
888
889 cs_failed:
890         cs_error(link->handle, last_fn, last_ret);
891
892 failed:
893         bt3c_release(link);
894 }
895
896
897 void bt3c_release(dev_link_t *link)
898 {
899         bt3c_info_t *info = link->priv;
900
901         if (link->state & DEV_PRESENT)
902                 bt3c_close(info);
903
904         link->dev = NULL;
905
906         pcmcia_release_configuration(link->handle);
907         pcmcia_release_io(link->handle, &link->io);
908         pcmcia_release_irq(link->handle, &link->irq);
909
910         link->state &= ~DEV_CONFIG;
911 }
912
913
914 int bt3c_event(event_t event, int priority, event_callback_args_t *args)
915 {
916         dev_link_t *link = args->client_data;
917         bt3c_info_t *info = link->priv;
918
919         switch (event) {
920         case CS_EVENT_CARD_REMOVAL:
921                 link->state &= ~DEV_PRESENT;
922                 if (link->state & DEV_CONFIG) {
923                         bt3c_close(info);
924                         bt3c_release(link);
925                 }
926                 break;
927         case CS_EVENT_CARD_INSERTION:
928                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
929                 bt3c_config(link);
930                 break;
931         case CS_EVENT_PM_SUSPEND:
932                 link->state |= DEV_SUSPEND;
933                 /* Fall through... */
934         case CS_EVENT_RESET_PHYSICAL:
935                 if (link->state & DEV_CONFIG)
936                         pcmcia_release_configuration(link->handle);
937                 break;
938         case CS_EVENT_PM_RESUME:
939                 link->state &= ~DEV_SUSPEND;
940                 /* Fall through... */
941         case CS_EVENT_CARD_RESET:
942                 if (DEV_OK(link))
943                         pcmcia_request_configuration(link->handle, &link->conf);
944                 break;
945         }
946
947         return 0;
948 }
949
950 static struct pcmcia_driver bt3c_driver = {
951         .owner          = THIS_MODULE,
952         .drv            = {
953                 .name   = "bt3c_cs",
954         },
955         .attach         = bt3c_attach,
956         .detach         = bt3c_detach,
957 };
958
959 static int __init init_bt3c_cs(void)
960 {
961         return pcmcia_register_driver(&bt3c_driver);
962 }
963
964
965 static void __exit exit_bt3c_cs(void)
966 {
967         pcmcia_unregister_driver(&bt3c_driver);
968
969         /* XXX: this really needs to move into generic code.. */
970         while (dev_list != NULL)
971                 bt3c_detach(dev_list);
972 }
973
974 module_init(init_bt3c_cs);
975 module_exit(exit_bt3c_cs);