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