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