VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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/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
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>
45
46 #include <linux/device.h>
47 #include <linux/firmware.h>
48
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>
56
57 #include <net/bluetooth/bluetooth.h>
58 #include <net/bluetooth/hci_core.h>
59
60
61
62 /* ======================== Module parameters ======================== */
63
64
65 /* Bit map of interrupts to choose from */
66 static u_int irq_mask = 0xffff;
67 static int irq_list[4] = { -1 };
68
69 MODULE_PARM(irq_mask, "i");
70 MODULE_PARM(irq_list, "1-4i");
71
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");
75
76
77
78 /* ======================== Local structures ======================== */
79
80
81 typedef struct bt3c_info_t {
82         dev_link_t link;
83         dev_node_t node;
84
85         struct hci_dev *hdev;
86
87         spinlock_t lock;                /* For serializing operations */
88
89         struct sk_buff_head txq;
90         unsigned long tx_state;
91
92         unsigned long rx_state;
93         unsigned long rx_count;
94         struct sk_buff *rx_skb;
95 } bt3c_info_t;
96
97
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);
101
102 static dev_info_t dev_info = "bt3c_cs";
103
104 dev_link_t *bt3c_attach(void);
105 void bt3c_detach(dev_link_t *);
106
107 static dev_link_t *dev_list = NULL;
108
109
110 /* Transmit states  */
111 #define XMIT_SENDING  1
112 #define XMIT_WAKEUP   2
113 #define XMIT_WAITING  8
114
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
121
122
123
124 /* ======================== Special I/O functions ======================== */
125
126
127 #define DATA_L   0
128 #define DATA_H   1
129 #define ADDR_L   2
130 #define ADDR_H   3
131 #define CONTROL  4
132
133
134 inline void bt3c_address(unsigned int iobase, unsigned short addr)
135 {
136         outb(addr & 0xff, iobase + ADDR_L);
137         outb((addr >> 8) & 0xff, iobase + ADDR_H);
138 }
139
140
141 inline void bt3c_put(unsigned int iobase, unsigned short value)
142 {
143         outb(value & 0xff, iobase + DATA_L);
144         outb((value >> 8) & 0xff, iobase + DATA_H);
145 }
146
147
148 inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
149 {
150         bt3c_address(iobase, addr);
151         bt3c_put(iobase, value);
152 }
153
154
155 inline unsigned short bt3c_get(unsigned int iobase)
156 {
157         unsigned short value = inb(iobase + DATA_L);
158
159         value |= inb(iobase + DATA_H) << 8;
160
161         return value;
162 }
163
164
165 inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
166 {
167         bt3c_address(iobase, addr);
168
169         return bt3c_get(iobase);
170 }
171
172
173
174 /* ======================== Interrupt handling ======================== */
175
176
177 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
178 {
179         int actual = 0;
180
181         bt3c_address(iobase, 0x7080);
182
183         /* Fill FIFO with current frame */
184         while (actual < len) {
185                 /* Transmit next byte */
186                 bt3c_put(iobase, buf[actual]);
187                 actual++;
188         }
189
190         bt3c_io_write(iobase, 0x7005, actual);
191
192         return actual;
193 }
194
195
196 static void bt3c_write_wakeup(bt3c_info_t *info)
197 {
198         if (!info) {
199                 BT_ERR("Unknown device");
200                 return;
201         }
202
203         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
204                 return;
205
206         do {
207                 register unsigned int iobase = info->link.io.BasePort1;
208                 register struct sk_buff *skb;
209                 register int len;
210
211                 if (!(info->link.state & DEV_PRESENT))
212                         break;
213
214
215                 if (!(skb = skb_dequeue(&(info->txq)))) {
216                         clear_bit(XMIT_SENDING, &(info->tx_state));
217                         break;
218                 }
219
220                 /* Send frame */
221                 len = bt3c_write(iobase, 256, skb->data, skb->len);
222
223                 if (len != skb->len) {
224                         BT_ERR("Very strange");
225                 }
226
227                 kfree_skb(skb);
228
229                 info->hdev->stat.byte_tx += len;
230
231         } while (0);
232 }
233
234
235 static void bt3c_receive(bt3c_info_t *info)
236 {
237         unsigned int iobase;
238         int size = 0, avail;
239
240         if (!info) {
241                 BT_ERR("Unknown device");
242                 return;
243         }
244
245         iobase = info->link.io.BasePort1;
246
247         avail = bt3c_read(iobase, 0x7006);
248         //printk("bt3c_cs: receiving %d bytes\n", avail);
249
250         bt3c_address(iobase, 0x7480);
251         while (size < avail) {
252                 size++;
253                 info->hdev->stat.byte_rx++;
254
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;
262                         }
263                 }
264
265
266                 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
267
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);
272
273                         switch (info->rx_skb->pkt_type) {
274
275                         case HCI_EVENT_PKT:
276                                 info->rx_state = RECV_WAIT_EVENT_HEADER;
277                                 info->rx_count = HCI_EVENT_HDR_SIZE;
278                                 break;
279
280                         case HCI_ACLDATA_PKT:
281                                 info->rx_state = RECV_WAIT_ACL_HEADER;
282                                 info->rx_count = HCI_ACL_HDR_SIZE;
283                                 break;
284
285                         case HCI_SCODATA_PKT:
286                                 info->rx_state = RECV_WAIT_SCO_HEADER;
287                                 info->rx_count = HCI_SCO_HDR_SIZE;
288                                 break;
289
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));
295
296                                 kfree_skb(info->rx_skb);
297                                 info->rx_skb = NULL;
298                                 break;
299
300                         }
301
302                 } else {
303
304                         __u8 x = inb(iobase + DATA_L);
305
306                         *skb_put(info->rx_skb, 1) = x;
307                         inb(iobase + DATA_H);
308                         info->rx_count--;
309
310                         if (info->rx_count == 0) {
311
312                                 int dlen;
313                                 struct hci_event_hdr *eh;
314                                 struct hci_acl_hdr *ah;
315                                 struct hci_sco_hdr *sh;
316
317                                 switch (info->rx_state) {
318
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;
324
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;
331
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;
337
338                                 case RECV_WAIT_DATA:
339                                         hci_recv_frame(info->rx_skb);
340                                         info->rx_skb = NULL;
341                                         break;
342
343                                 }
344
345                         }
346
347                 }
348
349         }
350
351         bt3c_io_write(iobase, 0x7006, 0x0000);
352 }
353
354
355 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
356 {
357         bt3c_info_t *info = dev_inst;
358         unsigned int iobase;
359         int iir;
360
361         if (!info || !info->hdev) {
362                 BT_ERR("Call of irq %d for unknown device", irq);
363                 return IRQ_NONE;
364         }
365
366         iobase = info->link.io.BasePort1;
367
368         spin_lock(&(info->lock));
369
370         iir = inb(iobase + CONTROL);
371         if (iir & 0x80) {
372                 int stat = bt3c_read(iobase, 0x7001);
373
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");
381                         }
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);
388                         }
389
390                         bt3c_io_write(iobase, 0x7001, 0x0000);
391
392                         outb(iir, iobase + CONTROL);
393                 }
394         }
395
396         spin_unlock(&(info->lock));
397
398         return IRQ_HANDLED;
399 }
400
401
402
403 /* ======================== HCI interface ======================== */
404
405
406 static int bt3c_hci_flush(struct hci_dev *hdev)
407 {
408         bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
409
410         /* Drop TX queue */
411         skb_queue_purge(&(info->txq));
412
413         return 0;
414 }
415
416
417 static int bt3c_hci_open(struct hci_dev *hdev)
418 {
419         set_bit(HCI_RUNNING, &(hdev->flags));
420
421         return 0;
422 }
423
424
425 static int bt3c_hci_close(struct hci_dev *hdev)
426 {
427         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
428                 return 0;
429
430         bt3c_hci_flush(hdev);
431
432         return 0;
433 }
434
435
436 static int bt3c_hci_send_frame(struct sk_buff *skb)
437 {
438         bt3c_info_t *info;
439         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
440         unsigned long flags;
441
442         if (!hdev) {
443                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
444                 return -ENODEV;
445         }
446
447         info = (bt3c_info_t *) (hdev->driver_data);
448
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;
459         };
460
461         /* Prepend skb with frame type */
462         memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
463         skb_queue_tail(&(info->txq), skb);
464
465         spin_lock_irqsave(&(info->lock), flags);
466
467         bt3c_write_wakeup(info);
468
469         spin_unlock_irqrestore(&(info->lock), flags);
470
471         return 0;
472 }
473
474
475 static void bt3c_hci_destruct(struct hci_dev *hdev)
476 {
477 }
478
479
480 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
481 {
482         return -ENOIOCTLCMD;
483 }
484
485
486
487 /* ======================== Card services HCI interaction ======================== */
488
489
490 static struct device *bt3c_device(void)
491 {
492         static char *kobj_name = "bt3c";
493
494         static struct device dev = {
495                 .bus_id = "pcmcia",
496         };
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);
500
501         return &dev;
502 }
503
504
505 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
506 {
507         char *ptr = (char *) firmware;
508         char b[9];
509         unsigned int iobase, size, addr, fcs, tmp;
510         int i, err = 0;
511
512         iobase = info->link.io.BasePort1;
513
514         /* Reset */
515         bt3c_io_write(iobase, 0x8040, 0x0404);
516         bt3c_io_write(iobase, 0x8040, 0x0400);
517
518         udelay(1);
519
520         bt3c_io_write(iobase, 0x8040, 0x0404);
521
522         udelay(17);
523
524         /* Load */
525         while (count) {
526                 if (ptr[0] != 'S') {
527                         BT_ERR("Bad address in firmware");
528                         err = -EFAULT;
529                         goto error;
530                 }
531
532                 memset(b, 0, sizeof(b));
533                 memcpy(b, ptr + 2, 2);
534                 size = simple_strtol(b, NULL, 16);
535
536                 memset(b, 0, sizeof(b));
537                 memcpy(b, ptr + 4, 8);
538                 addr = simple_strtol(b, NULL, 16);
539
540                 memset(b, 0, sizeof(b));
541                 memcpy(b, ptr + (size * 2) + 2, 2);
542                 fcs = simple_strtol(b, NULL, 16);
543
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);
548                 }
549
550                 if (((tmp + fcs) & 0xff) != 0xff) {
551                         BT_ERR("Checksum error in firmware");
552                         err = -EILSEQ;
553                         goto error;
554                 }
555
556                 if (ptr[1] == '3') {
557                         bt3c_address(iobase, addr);
558
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);
564                         }
565                 }
566
567                 ptr   += (size * 2) + 6;
568                 count -= (size * 2) + 6;
569         }
570
571         udelay(17);
572
573         /* Boot */
574         bt3c_address(iobase, 0x3000);
575         outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
576
577 error:
578         udelay(17);
579
580         /* Clear */
581         bt3c_io_write(iobase, 0x7006, 0x0000);
582         bt3c_io_write(iobase, 0x7005, 0x0000);
583         bt3c_io_write(iobase, 0x7001, 0x0000);
584
585         return err;
586 }
587
588
589 int bt3c_open(bt3c_info_t *info)
590 {
591         const struct firmware *firmware;
592         struct hci_dev *hdev;
593         int err;
594
595         spin_lock_init(&(info->lock));
596
597         skb_queue_head_init(&(info->txq));
598
599         info->rx_state = RECV_WAIT_PACKET_TYPE;
600         info->rx_count = 0;
601         info->rx_skb = NULL;
602
603         /* Initialize HCI device */
604         hdev = hci_alloc_dev();
605         if (!hdev) {
606                 BT_ERR("Can't allocate HCI device");
607                 return -ENOMEM;
608         }
609
610         info->hdev = hdev;
611
612         hdev->type = HCI_PCCARD;
613         hdev->driver_data = info;
614
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;
621
622         hdev->owner = THIS_MODULE;
623
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;
629         }
630
631         err = bt3c_load_firmware(info, firmware->data, firmware->size);
632
633         release_firmware(firmware);
634
635         if (err < 0) {
636                 BT_ERR("Firmware loading failed");
637                 goto error;
638         }
639
640         /* Timeout before it is safe to send the first HCI packet */
641         msleep(1000);
642
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;
648         }
649
650         return 0;
651
652 error:
653         info->hdev = NULL;
654         hci_free_dev(hdev);
655         return err;
656 }
657
658
659 int bt3c_close(bt3c_info_t *info)
660 {
661         struct hci_dev *hdev = info->hdev;
662
663         if (!hdev)
664                 return -ENODEV;
665
666         bt3c_hci_close(hdev);
667
668         if (hci_unregister_dev(hdev) < 0)
669                 BT_ERR("Can't unregister HCI device %s", hdev->name);
670
671         hci_free_dev(hdev);
672
673         return 0;
674 }
675
676 dev_link_t *bt3c_attach(void)
677 {
678         bt3c_info_t *info;
679         client_reg_t client_reg;
680         dev_link_t *link;
681         int i, ret;
682
683         /* Create new info device */
684         info = kmalloc(sizeof(*info), GFP_KERNEL);
685         if (!info)
686                 return NULL;
687         memset(info, 0, sizeof(*info));
688
689         link = &info->link;
690         link->priv = info;
691
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;
696
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];
702
703         link->irq.Handler = bt3c_interrupt;
704         link->irq.Instance = info;
705
706         link->conf.Attributes = CONF_ENABLE_IRQ;
707         link->conf.Vcc = 50;
708         link->conf.IntType = INT_MEMORY_AND_IO;
709
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;
722
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;
728         }
729
730         return link;
731 }
732
733
734 void bt3c_detach(dev_link_t *link)
735 {
736         bt3c_info_t *info = link->priv;
737         dev_link_t **linkp;
738         int ret;
739
740         /* Locate device structure */
741         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
742                 if (*linkp == link)
743                         break;
744
745         if (*linkp == NULL)
746                 return;
747
748         if (link->state & DEV_CONFIG)
749                 bt3c_release(link);
750
751         if (link->handle) {
752                 ret = pcmcia_deregister_client(link->handle);
753                 if (ret != CS_SUCCESS)
754                         cs_error(link->handle, DeregisterClient, ret);
755         }
756
757         /* Unlink device structure, free bits */
758         *linkp = link->next;
759
760         kfree(info);
761 }
762
763 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
764 {
765         int i;
766
767         i = pcmcia_get_tuple_data(handle, tuple);
768         if (i != CS_SUCCESS)
769                 return i;
770
771         return pcmcia_parse_tuple(handle, tuple, parse);
772 }
773
774 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
775 {
776         if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
777                 return CS_NO_MORE_ITEMS;
778         return get_tuple(handle, tuple, parse);
779 }
780
781 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
782 {
783         if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
784                 return CS_NO_MORE_ITEMS;
785         return get_tuple(handle, tuple, parse);
786 }
787
788 void bt3c_config(dev_link_t *link)
789 {
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;
799
800         tuple.TupleData = (cisdata_t *)buf;
801         tuple.TupleOffset = 0;
802         tuple.TupleDataMax = 255;
803         tuple.Attributes = 0;
804
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;
811         }
812         link->conf.ConfigBase = parse.config.base;
813         link->conf.Present = parse.config.rmask[0];
814
815         /* Configure card */
816         link->state |= DEV_CONFIG;
817         i = pcmcia_get_configuration_info(handle, &config);
818         link->conf.Vcc = config.Vcc;
819
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;
841                         }
842 next_entry:
843                         i = next_tuple(handle, &tuple, &parse);
844                 }
845         }
846
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;
860                         }
861                 }
862                 i = next_tuple(handle, &tuple, &parse);
863         }
864
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;
870         }
871
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;
876         }
877
878         i = pcmcia_request_configuration(link->handle, &link->conf);
879         if (i != CS_SUCCESS) {
880                 cs_error(link->handle, RequestConfiguration, i);
881                 goto failed;
882         }
883
884         if (bt3c_open(info) != 0)
885                 goto failed;
886
887         strcpy(info->node.dev_name, info->hdev->name);
888         link->dev = &info->node;
889         link->state &= ~DEV_CONFIG_PENDING;
890
891         return;
892
893 cs_failed:
894         cs_error(link->handle, last_fn, last_ret);
895
896 failed:
897         bt3c_release(link);
898 }
899
900
901 void bt3c_release(dev_link_t *link)
902 {
903         bt3c_info_t *info = link->priv;
904
905         if (link->state & DEV_PRESENT)
906                 bt3c_close(info);
907
908         link->dev = NULL;
909
910         pcmcia_release_configuration(link->handle);
911         pcmcia_release_io(link->handle, &link->io);
912         pcmcia_release_irq(link->handle, &link->irq);
913
914         link->state &= ~DEV_CONFIG;
915 }
916
917
918 int bt3c_event(event_t event, int priority, event_callback_args_t *args)
919 {
920         dev_link_t *link = args->client_data;
921         bt3c_info_t *info = link->priv;
922
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);
929                 }
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;
949         }
950
951         return 0;
952 }
953
954 static struct pcmcia_driver bt3c_driver = {
955         .owner          = THIS_MODULE,
956         .drv            = {
957                 .name   = "bt3c_cs",
958         },
959         .attach         = bt3c_attach,
960         .detach         = bt3c_detach,
961 };
962
963 static int __init init_bt3c_cs(void)
964 {
965         return pcmcia_register_driver(&bt3c_driver);
966 }
967
968
969 static void __exit exit_bt3c_cs(void)
970 {
971         pcmcia_unregister_driver(&bt3c_driver);
972
973         /* XXX: this really needs to move into generic code.. */
974         while (dev_list != NULL)
975                 bt3c_detach(dev_list);
976 }
977
978 module_init(init_bt3c_cs);
979 module_exit(exit_bt3c_cs);