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