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