74644df24477028e018710265234d32cdecdf11b
[linux-2.6.git] / drivers / bluetooth / btuart_cs.c
1 /*
2  *
3  *  Driver for Bluetooth PCMCIA cards with HCI UART interface
4  *
5  *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation;
11  *
12  *  Software distributed under the License is distributed on an "AS
13  *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  *  implied. See the License for the specific language governing
15  *  rights and limitations under the License.
16  *
17  *  The initial developer of the original code is David A. Hinds
18  *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
19  *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
20  *
21  */
22
23 #include <linux/config.h>
24 #include <linux/module.h>
25
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/errno.h>
33 #include <linux/ptrace.h>
34 #include <linux/ioport.h>
35 #include <linux/spinlock.h>
36 #include <linux/moduleparam.h>
37
38 #include <linux/skbuff.h>
39 #include <linux/string.h>
40 #include <linux/serial.h>
41 #include <linux/serial_reg.h>
42 #include <linux/bitops.h>
43 #include <asm/system.h>
44 #include <asm/io.h>
45
46 #include <pcmcia/version.h>
47 #include <pcmcia/cs_types.h>
48 #include <pcmcia/cs.h>
49 #include <pcmcia/cistpl.h>
50 #include <pcmcia/ciscode.h>
51 #include <pcmcia/ds.h>
52 #include <pcmcia/cisreg.h>
53
54 #include <net/bluetooth/bluetooth.h>
55 #include <net/bluetooth/hci_core.h>
56
57
58
59 /* ======================== Module parameters ======================== */
60
61
62 /* Bit map of interrupts to choose from */
63 static unsigned int irq_mask = 0xffff;
64 static int irq_list[4] = { -1 };
65
66 module_param(irq_mask, uint, 0);
67 module_param_array(irq_list, int, NULL, 0);
68
69 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
70 MODULE_DESCRIPTION("Bluetooth driver for Bluetooth PCMCIA cards with HCI UART interface");
71 MODULE_LICENSE("GPL");
72
73
74
75 /* ======================== Local structures ======================== */
76
77
78 typedef struct btuart_info_t {
79         dev_link_t link;
80         dev_node_t node;
81
82         struct hci_dev *hdev;
83
84         spinlock_t lock;        /* For serializing operations */
85
86         struct sk_buff_head txq;
87         unsigned long tx_state;
88
89         unsigned long rx_state;
90         unsigned long rx_count;
91         struct sk_buff *rx_skb;
92 } btuart_info_t;
93
94
95 static void btuart_config(dev_link_t *link);
96 static void btuart_release(dev_link_t *link);
97 static int btuart_event(event_t event, int priority, event_callback_args_t *args);
98
99 static dev_info_t dev_info = "btuart_cs";
100
101 static dev_link_t *btuart_attach(void);
102 static void btuart_detach(dev_link_t *);
103
104 static dev_link_t *dev_list = NULL;
105
106
107 /* Maximum baud rate */
108 #define SPEED_MAX  115200
109
110 /* Default baud rate: 57600, 115200, 230400 or 460800 */
111 #define DEFAULT_BAUD_RATE  115200
112
113
114 /* Transmit states  */
115 #define XMIT_SENDING    1
116 #define XMIT_WAKEUP     2
117 #define XMIT_WAITING    8
118
119 /* Receiver states */
120 #define RECV_WAIT_PACKET_TYPE   0
121 #define RECV_WAIT_EVENT_HEADER  1
122 #define RECV_WAIT_ACL_HEADER    2
123 #define RECV_WAIT_SCO_HEADER    3
124 #define RECV_WAIT_DATA          4
125
126
127
128 /* ======================== Interrupt handling ======================== */
129
130
131 static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
132 {
133         int actual = 0;
134
135         /* Tx FIFO should be empty */
136         if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
137                 return 0;
138
139         /* Fill FIFO with current frame */
140         while ((fifo_size-- > 0) && (actual < len)) {
141                 /* Transmit next byte */
142                 outb(buf[actual], iobase + UART_TX);
143                 actual++;
144         }
145
146         return actual;
147 }
148
149
150 static void btuart_write_wakeup(btuart_info_t *info)
151 {
152         if (!info) {
153                 BT_ERR("Unknown device");
154                 return;
155         }
156
157         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
158                 set_bit(XMIT_WAKEUP, &(info->tx_state));
159                 return;
160         }
161
162         do {
163                 register unsigned int iobase = info->link.io.BasePort1;
164                 register struct sk_buff *skb;
165                 register int len;
166
167                 clear_bit(XMIT_WAKEUP, &(info->tx_state));
168
169                 if (!(info->link.state & DEV_PRESENT))
170                         return;
171
172                 if (!(skb = skb_dequeue(&(info->txq))))
173                         break;
174
175                 /* Send frame */
176                 len = btuart_write(iobase, 16, skb->data, skb->len);
177                 set_bit(XMIT_WAKEUP, &(info->tx_state));
178
179                 if (len == skb->len) {
180                         kfree_skb(skb);
181                 } else {
182                         skb_pull(skb, len);
183                         skb_queue_head(&(info->txq), skb);
184                 }
185
186                 info->hdev->stat.byte_tx += len;
187
188         } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
189
190         clear_bit(XMIT_SENDING, &(info->tx_state));
191 }
192
193
194 static void btuart_receive(btuart_info_t *info)
195 {
196         unsigned int iobase;
197         int boguscount = 0;
198
199         if (!info) {
200                 BT_ERR("Unknown device");
201                 return;
202         }
203
204         iobase = info->link.io.BasePort1;
205
206         do {
207                 info->hdev->stat.byte_rx++;
208
209                 /* Allocate packet */
210                 if (info->rx_skb == NULL) {
211                         info->rx_state = RECV_WAIT_PACKET_TYPE;
212                         info->rx_count = 0;
213                         if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
214                                 BT_ERR("Can't allocate mem for new packet");
215                                 return;
216                         }
217                 }
218
219                 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
220
221                         info->rx_skb->dev = (void *) info->hdev;
222                         info->rx_skb->pkt_type = inb(iobase + UART_RX);
223
224                         switch (info->rx_skb->pkt_type) {
225
226                         case HCI_EVENT_PKT:
227                                 info->rx_state = RECV_WAIT_EVENT_HEADER;
228                                 info->rx_count = HCI_EVENT_HDR_SIZE;
229                                 break;
230
231                         case HCI_ACLDATA_PKT:
232                                 info->rx_state = RECV_WAIT_ACL_HEADER;
233                                 info->rx_count = HCI_ACL_HDR_SIZE;
234                                 break;
235
236                         case HCI_SCODATA_PKT:
237                                 info->rx_state = RECV_WAIT_SCO_HEADER;
238                                 info->rx_count = HCI_SCO_HDR_SIZE;
239                                 break;
240
241                         default:
242                                 /* Unknown packet */
243                                 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
244                                 info->hdev->stat.err_rx++;
245                                 clear_bit(HCI_RUNNING, &(info->hdev->flags));
246
247                                 kfree_skb(info->rx_skb);
248                                 info->rx_skb = NULL;
249                                 break;
250
251                         }
252
253                 } else {
254
255                         *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
256                         info->rx_count--;
257
258                         if (info->rx_count == 0) {
259
260                                 int dlen;
261                                 struct hci_event_hdr *eh;
262                                 struct hci_acl_hdr *ah;
263                                 struct hci_sco_hdr *sh;
264
265
266                                 switch (info->rx_state) {
267
268                                 case RECV_WAIT_EVENT_HEADER:
269                                         eh = (struct hci_event_hdr *)(info->rx_skb->data);
270                                         info->rx_state = RECV_WAIT_DATA;
271                                         info->rx_count = eh->plen;
272                                         break;
273
274                                 case RECV_WAIT_ACL_HEADER:
275                                         ah = (struct hci_acl_hdr *)(info->rx_skb->data);
276                                         dlen = __le16_to_cpu(ah->dlen);
277                                         info->rx_state = RECV_WAIT_DATA;
278                                         info->rx_count = dlen;
279                                         break;
280
281                                 case RECV_WAIT_SCO_HEADER:
282                                         sh = (struct hci_sco_hdr *)(info->rx_skb->data);
283                                         info->rx_state = RECV_WAIT_DATA;
284                                         info->rx_count = sh->dlen;
285                                         break;
286
287                                 case RECV_WAIT_DATA:
288                                         hci_recv_frame(info->rx_skb);
289                                         info->rx_skb = NULL;
290                                         break;
291
292                                 }
293
294                         }
295
296                 }
297
298                 /* Make sure we don't stay here too long */
299                 if (boguscount++ > 16)
300                         break;
301
302         } while (inb(iobase + UART_LSR) & UART_LSR_DR);
303 }
304
305
306 static irqreturn_t btuart_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
307 {
308         btuart_info_t *info = dev_inst;
309         unsigned int iobase;
310         int boguscount = 0;
311         int iir, lsr;
312
313         if (!info || !info->hdev) {
314                 BT_ERR("Call of irq %d for unknown device", irq);
315                 return IRQ_NONE;
316         }
317
318         iobase = info->link.io.BasePort1;
319
320         spin_lock(&(info->lock));
321
322         iir = inb(iobase + UART_IIR) & UART_IIR_ID;
323         while (iir) {
324
325                 /* Clear interrupt */
326                 lsr = inb(iobase + UART_LSR);
327
328                 switch (iir) {
329                 case UART_IIR_RLSI:
330                         BT_ERR("RLSI");
331                         break;
332                 case UART_IIR_RDI:
333                         /* Receive interrupt */
334                         btuart_receive(info);
335                         break;
336                 case UART_IIR_THRI:
337                         if (lsr & UART_LSR_THRE) {
338                                 /* Transmitter ready for data */
339                                 btuart_write_wakeup(info);
340                         }
341                         break;
342                 default:
343                         BT_ERR("Unhandled IIR=%#x", iir);
344                         break;
345                 }
346
347                 /* Make sure we don't stay here too long */
348                 if (boguscount++ > 100)
349                         break;
350
351                 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
352
353         }
354
355         spin_unlock(&(info->lock));
356
357         return IRQ_HANDLED;
358 }
359
360
361 static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
362 {
363         unsigned long flags;
364         unsigned int iobase;
365         int fcr;                /* FIFO control reg */
366         int lcr;                /* Line control reg */
367         int divisor;
368
369         if (!info) {
370                 BT_ERR("Unknown device");
371                 return;
372         }
373
374         iobase = info->link.io.BasePort1;
375
376         spin_lock_irqsave(&(info->lock), flags);
377
378         /* Turn off interrupts */
379         outb(0, iobase + UART_IER);
380
381         divisor = SPEED_MAX / speed;
382
383         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
384
385         /* 
386          * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
387          * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
388          * about this timeout since it will always be fast enough. 
389          */
390
391         if (speed < 38400)
392                 fcr |= UART_FCR_TRIGGER_1;
393         else
394                 fcr |= UART_FCR_TRIGGER_14;
395
396         /* Bluetooth cards use 8N1 */
397         lcr = UART_LCR_WLEN8;
398
399         outb(UART_LCR_DLAB | lcr, iobase + UART_LCR);   /* Set DLAB */
400         outb(divisor & 0xff, iobase + UART_DLL);        /* Set speed */
401         outb(divisor >> 8, iobase + UART_DLM);
402         outb(lcr, iobase + UART_LCR);   /* Set 8N1  */
403         outb(fcr, iobase + UART_FCR);   /* Enable FIFO's */
404
405         /* Turn on interrups */
406         outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
407
408         spin_unlock_irqrestore(&(info->lock), flags);
409 }
410
411
412
413 /* ======================== HCI interface ======================== */
414
415
416 static int btuart_hci_flush(struct hci_dev *hdev)
417 {
418         btuart_info_t *info = (btuart_info_t *)(hdev->driver_data);
419
420         /* Drop TX queue */
421         skb_queue_purge(&(info->txq));
422
423         return 0;
424 }
425
426
427 static int btuart_hci_open(struct hci_dev *hdev)
428 {
429         set_bit(HCI_RUNNING, &(hdev->flags));
430
431         return 0;
432 }
433
434
435 static int btuart_hci_close(struct hci_dev *hdev)
436 {
437         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
438                 return 0;
439
440         btuart_hci_flush(hdev);
441
442         return 0;
443 }
444
445
446 static int btuart_hci_send_frame(struct sk_buff *skb)
447 {
448         btuart_info_t *info;
449         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
450
451         if (!hdev) {
452                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
453                 return -ENODEV;
454         }
455
456         info = (btuart_info_t *)(hdev->driver_data);
457
458         switch (skb->pkt_type) {
459         case HCI_COMMAND_PKT:
460                 hdev->stat.cmd_tx++;
461                 break;
462         case HCI_ACLDATA_PKT:
463                 hdev->stat.acl_tx++;
464                 break;
465         case HCI_SCODATA_PKT:
466                 hdev->stat.sco_tx++;
467                 break;
468         };
469
470         /* Prepend skb with frame type */
471         memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
472         skb_queue_tail(&(info->txq), skb);
473
474         btuart_write_wakeup(info);
475
476         return 0;
477 }
478
479
480 static void btuart_hci_destruct(struct hci_dev *hdev)
481 {
482 }
483
484
485 static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
486 {
487         return -ENOIOCTLCMD;
488 }
489
490
491
492 /* ======================== Card services HCI interaction ======================== */
493
494
495 static int btuart_open(btuart_info_t *info)
496 {
497         unsigned long flags;
498         unsigned int iobase = info->link.io.BasePort1;
499         struct hci_dev *hdev;
500
501         spin_lock_init(&(info->lock));
502
503         skb_queue_head_init(&(info->txq));
504
505         info->rx_state = RECV_WAIT_PACKET_TYPE;
506         info->rx_count = 0;
507         info->rx_skb = NULL;
508
509         /* Initialize HCI device */
510         hdev = hci_alloc_dev();
511         if (!hdev) {
512                 BT_ERR("Can't allocate HCI device");
513                 return -ENOMEM;
514         }
515
516         info->hdev = hdev;
517
518         hdev->type = HCI_PCCARD;
519         hdev->driver_data = info;
520
521         hdev->open     = btuart_hci_open;
522         hdev->close    = btuart_hci_close;
523         hdev->flush    = btuart_hci_flush;
524         hdev->send     = btuart_hci_send_frame;
525         hdev->destruct = btuart_hci_destruct;
526         hdev->ioctl    = btuart_hci_ioctl;
527
528         hdev->owner = THIS_MODULE;
529
530         spin_lock_irqsave(&(info->lock), flags);
531
532         /* Reset UART */
533         outb(0, iobase + UART_MCR);
534
535         /* Turn off interrupts */
536         outb(0, iobase + UART_IER);
537
538         /* Initialize UART */
539         outb(UART_LCR_WLEN8, iobase + UART_LCR);        /* Reset DLAB */
540         outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
541
542         /* Turn on interrupts */
543         // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
544
545         spin_unlock_irqrestore(&(info->lock), flags);
546
547         btuart_change_speed(info, DEFAULT_BAUD_RATE);
548
549         /* Timeout before it is safe to send the first HCI packet */
550         msleep(1000);
551
552         /* Register HCI device */
553         if (hci_register_dev(hdev) < 0) {
554                 BT_ERR("Can't register HCI device");
555                 info->hdev = NULL;
556                 hci_free_dev(hdev);
557                 return -ENODEV;
558         }
559
560         return 0;
561 }
562
563
564 static int btuart_close(btuart_info_t *info)
565 {
566         unsigned long flags;
567         unsigned int iobase = info->link.io.BasePort1;
568         struct hci_dev *hdev = info->hdev;
569
570         if (!hdev)
571                 return -ENODEV;
572
573         btuart_hci_close(hdev);
574
575         spin_lock_irqsave(&(info->lock), flags);
576
577         /* Reset UART */
578         outb(0, iobase + UART_MCR);
579
580         /* Turn off interrupts */
581         outb(0, iobase + UART_IER);
582
583         spin_unlock_irqrestore(&(info->lock), flags);
584
585         if (hci_unregister_dev(hdev) < 0)
586                 BT_ERR("Can't unregister HCI device %s", hdev->name);
587
588         hci_free_dev(hdev);
589
590         return 0;
591 }
592
593 static dev_link_t *btuart_attach(void)
594 {
595         btuart_info_t *info;
596         client_reg_t client_reg;
597         dev_link_t *link;
598         int i, ret;
599
600         /* Create new info device */
601         info = kmalloc(sizeof(*info), GFP_KERNEL);
602         if (!info)
603                 return NULL;
604         memset(info, 0, sizeof(*info));
605
606         link = &info->link;
607         link->priv = info;
608
609         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
610         link->io.NumPorts1 = 8;
611         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
612         link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
613
614         if (irq_list[0] == -1)
615                 link->irq.IRQInfo2 = irq_mask;
616         else
617                 for (i = 0; i < 4; i++)
618                         link->irq.IRQInfo2 |= 1 << irq_list[i];
619
620         link->irq.Handler = btuart_interrupt;
621         link->irq.Instance = info;
622
623         link->conf.Attributes = CONF_ENABLE_IRQ;
624         link->conf.Vcc = 50;
625         link->conf.IntType = INT_MEMORY_AND_IO;
626
627         /* Register with Card Services */
628         link->next = dev_list;
629         dev_list = link;
630         client_reg.dev_info = &dev_info;
631         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
632         client_reg.EventMask =
633                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
634                 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
635                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
636         client_reg.event_handler = &btuart_event;
637         client_reg.Version = 0x0210;
638         client_reg.event_callback_args.client_data = link;
639
640         ret = pcmcia_register_client(&link->handle, &client_reg);
641         if (ret != CS_SUCCESS) {
642                 cs_error(link->handle, RegisterClient, ret);
643                 btuart_detach(link);
644                 return NULL;
645         }
646
647         return link;
648 }
649
650
651 static void btuart_detach(dev_link_t *link)
652 {
653         btuart_info_t *info = link->priv;
654         dev_link_t **linkp;
655         int ret;
656
657         /* Locate device structure */
658         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
659                 if (*linkp == link)
660                         break;
661
662         if (*linkp == NULL)
663                 return;
664
665         if (link->state & DEV_CONFIG)
666                 btuart_release(link);
667
668         if (link->handle) {
669                 ret = pcmcia_deregister_client(link->handle);
670                 if (ret != CS_SUCCESS)
671                         cs_error(link->handle, DeregisterClient, ret);
672         }
673
674         /* Unlink device structure, free bits */
675         *linkp = link->next;
676
677         kfree(info);
678 }
679
680 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
681 {
682         int i;
683
684         i = pcmcia_get_tuple_data(handle, tuple);
685         if (i != CS_SUCCESS)
686                 return i;
687
688         return pcmcia_parse_tuple(handle, tuple, parse);
689 }
690
691 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
692 {
693         if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
694                 return CS_NO_MORE_ITEMS;
695         return get_tuple(handle, tuple, parse);
696 }
697
698 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
699 {
700         if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
701                 return CS_NO_MORE_ITEMS;
702         return get_tuple(handle, tuple, parse);
703 }
704
705 static void btuart_config(dev_link_t *link)
706 {
707         static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
708         client_handle_t handle = link->handle;
709         btuart_info_t *info = link->priv;
710         tuple_t tuple;
711         u_short buf[256];
712         cisparse_t parse;
713         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
714         config_info_t config;
715         int i, j, try, last_ret, last_fn;
716
717         tuple.TupleData = (cisdata_t *)buf;
718         tuple.TupleOffset = 0;
719         tuple.TupleDataMax = 255;
720         tuple.Attributes = 0;
721
722         /* Get configuration register information */
723         tuple.DesiredTuple = CISTPL_CONFIG;
724         last_ret = first_tuple(handle, &tuple, &parse);
725         if (last_ret != CS_SUCCESS) {
726                 last_fn = ParseTuple;
727                 goto cs_failed;
728         }
729         link->conf.ConfigBase = parse.config.base;
730         link->conf.Present = parse.config.rmask[0];
731
732         /* Configure card */
733         link->state |= DEV_CONFIG;
734         i = pcmcia_get_configuration_info(handle, &config);
735         link->conf.Vcc = config.Vcc;
736
737         /* First pass: look for a config entry that looks normal. */
738         tuple.TupleData = (cisdata_t *) buf;
739         tuple.TupleOffset = 0;
740         tuple.TupleDataMax = 255;
741         tuple.Attributes = 0;
742         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
743         /* Two tries: without IO aliases, then with aliases */
744         for (try = 0; try < 2; try++) {
745                 i = first_tuple(handle, &tuple, &parse);
746                 while (i != CS_NO_MORE_ITEMS) {
747                         if (i != CS_SUCCESS)
748                                 goto next_entry;
749                         if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
750                                 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
751                         if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
752                                 link->conf.ConfigIndex = cf->index;
753                                 link->io.BasePort1 = cf->io.win[0].base;
754                                 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
755                                 i = pcmcia_request_io(link->handle, &link->io);
756                                 if (i == CS_SUCCESS)
757                                         goto found_port;
758                         }
759 next_entry:
760                         i = next_tuple(handle, &tuple, &parse);
761                 }
762         }
763
764         /* Second pass: try to find an entry that isn't picky about
765            its base address, then try to grab any standard serial port
766            address, and finally try to get any free port. */
767         i = first_tuple(handle, &tuple, &parse);
768         while (i != CS_NO_MORE_ITEMS) {
769                 if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
770                     && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
771                         link->conf.ConfigIndex = cf->index;
772                         for (j = 0; j < 5; j++) {
773                                 link->io.BasePort1 = base[j];
774                                 link->io.IOAddrLines = base[j] ? 16 : 3;
775                                 i = pcmcia_request_io(link->handle, &link->io);
776                                 if (i == CS_SUCCESS)
777                                         goto found_port;
778                         }
779                 }
780                 i = next_tuple(handle, &tuple, &parse);
781         }
782
783 found_port:
784         if (i != CS_SUCCESS) {
785                 BT_ERR("No usable port range found");
786                 cs_error(link->handle, RequestIO, i);
787                 goto failed;
788         }
789
790         i = pcmcia_request_irq(link->handle, &link->irq);
791         if (i != CS_SUCCESS) {
792                 cs_error(link->handle, RequestIRQ, i);
793                 link->irq.AssignedIRQ = 0;
794         }
795
796         i = pcmcia_request_configuration(link->handle, &link->conf);
797         if (i != CS_SUCCESS) {
798                 cs_error(link->handle, RequestConfiguration, i);
799                 goto failed;
800         }
801
802         if (btuart_open(info) != 0)
803                 goto failed;
804
805         strcpy(info->node.dev_name, info->hdev->name);
806         link->dev = &info->node;
807         link->state &= ~DEV_CONFIG_PENDING;
808
809         return;
810
811 cs_failed:
812         cs_error(link->handle, last_fn, last_ret);
813
814 failed:
815         btuart_release(link);
816 }
817
818
819 static void btuart_release(dev_link_t *link)
820 {
821         btuart_info_t *info = link->priv;
822
823         if (link->state & DEV_PRESENT)
824                 btuart_close(info);
825
826         link->dev = NULL;
827
828         pcmcia_release_configuration(link->handle);
829         pcmcia_release_io(link->handle, &link->io);
830         pcmcia_release_irq(link->handle, &link->irq);
831
832         link->state &= ~DEV_CONFIG;
833 }
834
835
836 static int btuart_event(event_t event, int priority, event_callback_args_t *args)
837 {
838         dev_link_t *link = args->client_data;
839         btuart_info_t *info = link->priv;
840
841         switch (event) {
842         case CS_EVENT_CARD_REMOVAL:
843                 link->state &= ~DEV_PRESENT;
844                 if (link->state & DEV_CONFIG) {
845                         btuart_close(info);
846                         btuart_release(link);
847                 }
848                 break;
849         case CS_EVENT_CARD_INSERTION:
850                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
851                 btuart_config(link);
852                 break;
853         case CS_EVENT_PM_SUSPEND:
854                 link->state |= DEV_SUSPEND;
855                 /* Fall through... */
856         case CS_EVENT_RESET_PHYSICAL:
857                 if (link->state & DEV_CONFIG)
858                         pcmcia_release_configuration(link->handle);
859                 break;
860         case CS_EVENT_PM_RESUME:
861                 link->state &= ~DEV_SUSPEND;
862                 /* Fall through... */
863         case CS_EVENT_CARD_RESET:
864                 if (DEV_OK(link))
865                         pcmcia_request_configuration(link->handle, &link->conf);
866                 break;
867         }
868
869         return 0;
870 }
871
872 static struct pcmcia_driver btuart_driver = {
873         .owner          = THIS_MODULE,
874         .drv            = {
875                 .name   = "btuart_cs",
876         },
877         .attach         = btuart_attach,
878         .detach         = btuart_detach,
879 };
880
881 static int __init init_btuart_cs(void)
882 {
883         return pcmcia_register_driver(&btuart_driver);
884 }
885
886
887 static void __exit exit_btuart_cs(void)
888 {
889         pcmcia_unregister_driver(&btuart_driver);
890
891         /* XXX: this really needs to move into generic code.. */
892         while (dev_list != NULL)
893                 btuart_detach(dev_list);
894 }
895
896 module_init(init_btuart_cs);
897 module_exit(exit_btuart_cs);