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