ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / bluetooth / dtl1_cs.c
1 /*
2  *
3  *  A driver for Nokia Connectivity Card DTL-1 devices
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/errno.h>
32 #include <linux/ptrace.h>
33 #include <linux/ioport.h>
34 #include <linux/spinlock.h>
35
36 #include <linux/skbuff.h>
37 #include <linux/string.h>
38 #include <linux/serial.h>
39 #include <linux/serial_reg.h>
40 #include <asm/system.h>
41 #include <asm/bitops.h>
42 #include <asm/io.h>
43
44 #include <pcmcia/version.h>
45 #include <pcmcia/cs_types.h>
46 #include <pcmcia/cs.h>
47 #include <pcmcia/cistpl.h>
48 #include <pcmcia/ciscode.h>
49 #include <pcmcia/ds.h>
50 #include <pcmcia/cisreg.h>
51
52 #include <net/bluetooth/bluetooth.h>
53 #include <net/bluetooth/hci_core.h>
54
55
56
57 /* ======================== Module parameters ======================== */
58
59
60 /* Bit map of interrupts to choose from */
61 static u_int irq_mask = 0xffff;
62 static int irq_list[4] = { -1 };
63
64 MODULE_PARM(irq_mask, "i");
65 MODULE_PARM(irq_list, "1-4i");
66
67 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
68 MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
69 MODULE_LICENSE("GPL");
70
71
72
73 /* ======================== Local structures ======================== */
74
75
76 typedef struct dtl1_info_t {
77         dev_link_t link;
78         dev_node_t node;
79
80         struct hci_dev *hdev;
81
82         spinlock_t lock;                /* For serializing operations */
83
84         unsigned long flowmask;         /* HCI flow mask */
85         int ri_latch;
86
87         struct sk_buff_head txq;
88         unsigned long tx_state;
89
90         unsigned long rx_state;
91         unsigned long rx_count;
92         struct sk_buff *rx_skb;
93 } dtl1_info_t;
94
95
96 void dtl1_config(dev_link_t *link);
97 void dtl1_release(dev_link_t *link);
98 int dtl1_event(event_t event, int priority, event_callback_args_t *args);
99
100 static dev_info_t dev_info = "dtl1_cs";
101
102 dev_link_t *dtl1_attach(void);
103 void dtl1_detach(dev_link_t *);
104
105 static dev_link_t *dev_list = NULL;
106
107
108 /* Transmit states  */
109 #define XMIT_SENDING  1
110 #define XMIT_WAKEUP   2
111 #define XMIT_WAITING  8
112
113 /* Receiver States */
114 #define RECV_WAIT_NSH   0
115 #define RECV_WAIT_DATA  1
116
117
118 typedef struct {
119         u8 type;
120         u8 zero;
121         u16 len;
122 } __attribute__ ((packed)) nsh_t;       /* Nokia Specific Header */
123
124 #define NSHL  4                         /* Nokia Specific Header Length */
125
126
127
128 /* ======================== Interrupt handling ======================== */
129
130
131 static int dtl1_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 dtl1_write_wakeup(dtl1_info_t *info)
151 {
152         if (!info) {
153                 BT_ERR("Unknown device");
154                 return;
155         }
156
157         if (test_bit(XMIT_WAITING, &(info->tx_state))) {
158                 set_bit(XMIT_WAKEUP, &(info->tx_state));
159                 return;
160         }
161
162         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
163                 set_bit(XMIT_WAKEUP, &(info->tx_state));
164                 return;
165         }
166
167         do {
168                 register unsigned int iobase = info->link.io.BasePort1;
169                 register struct sk_buff *skb;
170                 register int len;
171
172                 clear_bit(XMIT_WAKEUP, &(info->tx_state));
173
174                 if (!(info->link.state & DEV_PRESENT))
175                         return;
176
177                 if (!(skb = skb_dequeue(&(info->txq))))
178                         break;
179
180                 /* Send frame */
181                 len = dtl1_write(iobase, 32, skb->data, skb->len);
182
183                 if (len == skb->len) {
184                         set_bit(XMIT_WAITING, &(info->tx_state));
185                         kfree_skb(skb);
186                 } else {
187                         skb_pull(skb, len);
188                         skb_queue_head(&(info->txq), skb);
189                 }
190
191                 info->hdev->stat.byte_tx += len;
192
193         } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
194
195         clear_bit(XMIT_SENDING, &(info->tx_state));
196 }
197
198
199 static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
200 {
201         u8 flowmask = *(u8 *)skb->data;
202         int i;
203
204         printk(KERN_INFO "Bluetooth: Nokia control data =");
205         for (i = 0; i < skb->len; i++) {
206                 printk(" %02x", skb->data[i]);
207         }
208         printk("\n");
209
210         /* transition to active state */
211         if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
212                 clear_bit(XMIT_WAITING, &(info->tx_state));
213                 dtl1_write_wakeup(info);
214         }
215
216         info->flowmask = flowmask;
217
218         kfree_skb(skb);
219 }
220
221
222 static void dtl1_receive(dtl1_info_t *info)
223 {
224         unsigned int iobase;
225         nsh_t *nsh;
226         int boguscount = 0;
227
228         if (!info) {
229                 BT_ERR("Unknown device");
230                 return;
231         }
232
233         iobase = info->link.io.BasePort1;
234
235         do {
236                 info->hdev->stat.byte_rx++;
237
238                 /* Allocate packet */
239                 if (info->rx_skb == NULL)
240                         if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
241                                 BT_ERR("Can't allocate mem for new packet");
242                                 info->rx_state = RECV_WAIT_NSH;
243                                 info->rx_count = NSHL;
244                                 return;
245                         }
246
247                 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
248                 nsh = (nsh_t *)info->rx_skb->data;
249
250                 info->rx_count--;
251
252                 if (info->rx_count == 0) {
253
254                         switch (info->rx_state) {
255                         case RECV_WAIT_NSH:
256                                 info->rx_state = RECV_WAIT_DATA;
257                                 info->rx_count = nsh->len + (nsh->len & 0x0001);
258                                 break;
259                         case RECV_WAIT_DATA:
260                                 info->rx_skb->pkt_type = nsh->type;
261
262                                 /* remove PAD byte if it exists */
263                                 if (nsh->len & 0x0001) {
264                                         info->rx_skb->tail--;
265                                         info->rx_skb->len--;
266                                 }
267
268                                 /* remove NSH */
269                                 skb_pull(info->rx_skb, NSHL);
270
271                                 switch (info->rx_skb->pkt_type) {
272                                 case 0x80:
273                                         /* control data for the Nokia Card */
274                                         dtl1_control(info, info->rx_skb);
275                                         break;
276                                 case 0x82:
277                                 case 0x83:
278                                 case 0x84:
279                                         /* send frame to the HCI layer */
280                                         info->rx_skb->dev = (void *) info->hdev;
281                                         info->rx_skb->pkt_type &= 0x0f;
282                                         hci_recv_frame(info->rx_skb);
283                                         break;
284                                 default:
285                                         /* unknown packet */
286                                         BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
287                                         kfree_skb(info->rx_skb);
288                                         break;
289                                 }
290
291                                 info->rx_state = RECV_WAIT_NSH;
292                                 info->rx_count = NSHL;
293                                 info->rx_skb = NULL;
294                                 break;
295                         }
296
297                 }
298
299                 /* Make sure we don't stay here too long */
300                 if (boguscount++ > 32)
301                         break;
302
303         } while (inb(iobase + UART_LSR) & UART_LSR_DR);
304 }
305
306
307 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
308 {
309         dtl1_info_t *info = dev_inst;
310         unsigned int iobase;
311         unsigned char msr;
312         int boguscount = 0;
313         int iir, lsr;
314
315         if (!info || !info->hdev) {
316                 BT_ERR("Call of irq %d for unknown device", irq);
317                 return IRQ_NONE;
318         }
319
320         iobase = info->link.io.BasePort1;
321
322         spin_lock(&(info->lock));
323
324         iir = inb(iobase + UART_IIR) & UART_IIR_ID;
325         while (iir) {
326
327                 /* Clear interrupt */
328                 lsr = inb(iobase + UART_LSR);
329
330                 switch (iir) {
331                 case UART_IIR_RLSI:
332                         BT_ERR("RLSI");
333                         break;
334                 case UART_IIR_RDI:
335                         /* Receive interrupt */
336                         dtl1_receive(info);
337                         break;
338                 case UART_IIR_THRI:
339                         if (lsr & UART_LSR_THRE) {
340                                 /* Transmitter ready for data */
341                                 dtl1_write_wakeup(info);
342                         }
343                         break;
344                 default:
345                         BT_ERR("Unhandled IIR=%#x", iir);
346                         break;
347                 }
348
349                 /* Make sure we don't stay here too long */
350                 if (boguscount++ > 100)
351                         break;
352
353                 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
354
355         }
356
357         msr = inb(iobase + UART_MSR);
358
359         if (info->ri_latch ^ (msr & UART_MSR_RI)) {
360                 info->ri_latch = msr & UART_MSR_RI;
361                 clear_bit(XMIT_WAITING, &(info->tx_state));
362                 dtl1_write_wakeup(info);
363         }
364
365         spin_unlock(&(info->lock));
366
367         return IRQ_HANDLED;
368 }
369
370
371
372 /* ======================== HCI interface ======================== */
373
374
375 static int dtl1_hci_open(struct hci_dev *hdev)
376 {
377         set_bit(HCI_RUNNING, &(hdev->flags));
378
379         return 0;
380 }
381
382
383 static int dtl1_hci_flush(struct hci_dev *hdev)
384 {
385         dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
386
387         /* Drop TX queue */
388         skb_queue_purge(&(info->txq));
389
390         return 0;
391 }
392
393
394 static int dtl1_hci_close(struct hci_dev *hdev)
395 {
396         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
397                 return 0;
398
399         dtl1_hci_flush(hdev);
400
401         return 0;
402 }
403
404
405 static int dtl1_hci_send_frame(struct sk_buff *skb)
406 {
407         dtl1_info_t *info;
408         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
409         struct sk_buff *s;
410         nsh_t nsh;
411
412         if (!hdev) {
413                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
414                 return -ENODEV;
415         }
416
417         info = (dtl1_info_t *)(hdev->driver_data);
418
419         switch (skb->pkt_type) {
420         case HCI_COMMAND_PKT:
421                 hdev->stat.cmd_tx++;
422                 nsh.type = 0x81;
423                 break;
424         case HCI_ACLDATA_PKT:
425                 hdev->stat.acl_tx++;
426                 nsh.type = 0x82;
427                 break;
428         case HCI_SCODATA_PKT:
429                 hdev->stat.sco_tx++;
430                 nsh.type = 0x83;
431                 break;
432         };
433
434         nsh.zero = 0;
435         nsh.len = skb->len;
436
437         s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
438         skb_reserve(s, NSHL);
439         memcpy(skb_put(s, skb->len), skb->data, skb->len);
440         if (skb->len & 0x0001)
441                 *skb_put(s, 1) = 0;     /* PAD */
442
443         /* Prepend skb with Nokia frame header and queue */
444         memcpy(skb_push(s, NSHL), &nsh, NSHL);
445         skb_queue_tail(&(info->txq), s);
446
447         dtl1_write_wakeup(info);
448
449         kfree_skb(skb);
450
451         return 0;
452 }
453
454
455 static void dtl1_hci_destruct(struct hci_dev *hdev)
456 {
457 }
458
459
460 static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd,  unsigned long arg)
461 {
462         return -ENOIOCTLCMD;
463 }
464
465
466
467 /* ======================== Card services HCI interaction ======================== */
468
469
470 int dtl1_open(dtl1_info_t *info)
471 {
472         unsigned long flags;
473         unsigned int iobase = info->link.io.BasePort1;
474         struct hci_dev *hdev;
475
476         spin_lock_init(&(info->lock));
477
478         skb_queue_head_init(&(info->txq));
479
480         info->rx_state = RECV_WAIT_NSH;
481         info->rx_count = NSHL;
482         info->rx_skb = NULL;
483
484         set_bit(XMIT_WAITING, &(info->tx_state));
485
486         /* Initialize HCI device */
487         hdev = hci_alloc_dev();
488         if (!hdev) {
489                 BT_ERR("Can't allocate HCI device");
490                 return -ENOMEM;
491         }
492
493         info->hdev = hdev;
494
495         hdev->type = HCI_PCCARD;
496         hdev->driver_data = info;
497
498         hdev->open     = dtl1_hci_open;
499         hdev->close    = dtl1_hci_close;
500         hdev->flush    = dtl1_hci_flush;
501         hdev->send     = dtl1_hci_send_frame;
502         hdev->destruct = dtl1_hci_destruct;
503         hdev->ioctl    = dtl1_hci_ioctl;
504
505         hdev->owner = THIS_MODULE;
506
507         spin_lock_irqsave(&(info->lock), flags);
508
509         /* Reset UART */
510         outb(0, iobase + UART_MCR);
511
512         /* Turn off interrupts */
513         outb(0, iobase + UART_IER);
514
515         /* Initialize UART */
516         outb(UART_LCR_WLEN8, iobase + UART_LCR);        /* Reset DLAB */
517         outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
518
519         info->ri_latch = inb(info->link.io.BasePort1 + UART_MSR) & UART_MSR_RI;
520
521         /* Turn on interrupts */
522         outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
523
524         spin_unlock_irqrestore(&(info->lock), flags);
525
526         /* Timeout before it is safe to send the first HCI packet */
527         set_current_state(TASK_INTERRUPTIBLE);
528         schedule_timeout(HZ * 2);
529
530         /* Register HCI device */
531         if (hci_register_dev(hdev) < 0) {
532                 BT_ERR("Can't register HCI device");
533                 info->hdev = NULL;
534                 hci_free_dev(hdev);
535                 return -ENODEV;
536         }
537
538         return 0;
539 }
540
541
542 int dtl1_close(dtl1_info_t *info)
543 {
544         unsigned long flags;
545         unsigned int iobase = info->link.io.BasePort1;
546         struct hci_dev *hdev = info->hdev;
547
548         if (!hdev)
549                 return -ENODEV;
550
551         dtl1_hci_close(hdev);
552
553         spin_lock_irqsave(&(info->lock), flags);
554
555         /* Reset UART */
556         outb(0, iobase + UART_MCR);
557
558         /* Turn off interrupts */
559         outb(0, iobase + UART_IER);
560
561         spin_unlock_irqrestore(&(info->lock), flags);
562
563         if (hci_unregister_dev(hdev) < 0)
564                 BT_ERR("Can't unregister HCI device %s", hdev->name);
565
566         hci_free_dev(hdev);
567
568         return 0;
569 }
570
571 dev_link_t *dtl1_attach(void)
572 {
573         dtl1_info_t *info;
574         client_reg_t client_reg;
575         dev_link_t *link;
576         int i, ret;
577
578         /* Create new info device */
579         info = kmalloc(sizeof(*info), GFP_KERNEL);
580         if (!info)
581                 return NULL;
582         memset(info, 0, sizeof(*info));
583
584         link = &info->link;
585         link->priv = info;
586
587         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
588         link->io.NumPorts1 = 8;
589         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
590         link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
591
592         if (irq_list[0] == -1)
593                 link->irq.IRQInfo2 = irq_mask;
594         else
595                 for (i = 0; i < 4; i++)
596                         link->irq.IRQInfo2 |= 1 << irq_list[i];
597
598         link->irq.Handler = dtl1_interrupt;
599         link->irq.Instance = info;
600
601         link->conf.Attributes = CONF_ENABLE_IRQ;
602         link->conf.Vcc = 50;
603         link->conf.IntType = INT_MEMORY_AND_IO;
604
605         /* Register with Card Services */
606         link->next = dev_list;
607         dev_list = link;
608         client_reg.dev_info = &dev_info;
609         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
610         client_reg.EventMask =
611                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
612                 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
613                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
614         client_reg.event_handler = &dtl1_event;
615         client_reg.Version = 0x0210;
616         client_reg.event_callback_args.client_data = link;
617
618         ret = pcmcia_register_client(&link->handle, &client_reg);
619         if (ret != CS_SUCCESS) {
620                 cs_error(link->handle, RegisterClient, ret);
621                 dtl1_detach(link);
622                 return NULL;
623         }
624
625         return link;
626 }
627
628
629 void dtl1_detach(dev_link_t *link)
630 {
631         dtl1_info_t *info = link->priv;
632         dev_link_t **linkp;
633         int ret;
634
635         /* Locate device structure */
636         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
637                 if (*linkp == link)
638                         break;
639
640         if (*linkp == NULL)
641                 return;
642
643         if (link->state & DEV_CONFIG)
644                 dtl1_release(link);
645
646         if (link->handle) {
647                 ret = pcmcia_deregister_client(link->handle);
648                 if (ret != CS_SUCCESS)
649                         cs_error(link->handle, DeregisterClient, ret);
650         }
651
652         /* Unlink device structure, free bits */
653         *linkp = link->next;
654
655         kfree(info);
656 }
657
658 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
659 {
660         int i;
661
662         i = pcmcia_get_tuple_data(handle, tuple);
663         if (i != CS_SUCCESS)
664                 return i;
665
666         return pcmcia_parse_tuple(handle, tuple, parse);
667 }
668
669 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
670 {
671         if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
672                 return CS_NO_MORE_ITEMS;
673         return get_tuple(handle, tuple, parse);
674 }
675
676 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
677 {
678         if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
679                 return CS_NO_MORE_ITEMS;
680         return get_tuple(handle, tuple, parse);
681 }
682
683 void dtl1_config(dev_link_t *link)
684 {
685         client_handle_t handle = link->handle;
686         dtl1_info_t *info = link->priv;
687         tuple_t tuple;
688         u_short buf[256];
689         cisparse_t parse;
690         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
691         config_info_t config;
692         int i, last_ret, last_fn;
693
694         tuple.TupleData = (cisdata_t *)buf;
695         tuple.TupleOffset = 0;
696         tuple.TupleDataMax = 255;
697         tuple.Attributes = 0;
698
699         /* Get configuration register information */
700         tuple.DesiredTuple = CISTPL_CONFIG;
701         last_ret = first_tuple(handle, &tuple, &parse);
702         if (last_ret != CS_SUCCESS) {
703                 last_fn = ParseTuple;
704                 goto cs_failed;
705         }
706         link->conf.ConfigBase = parse.config.base;
707         link->conf.Present = parse.config.rmask[0];
708
709         /* Configure card */
710         link->state |= DEV_CONFIG;
711         i = pcmcia_get_configuration_info(handle, &config);
712         link->conf.Vcc = config.Vcc;
713
714         tuple.TupleData = (cisdata_t *)buf;
715         tuple.TupleOffset = 0;
716         tuple.TupleDataMax = 255;
717         tuple.Attributes = 0;
718         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
719
720         /* Look for a generic full-sized window */
721         link->io.NumPorts1 = 8;
722         i = first_tuple(handle, &tuple, &parse);
723         while (i != CS_NO_MORE_ITEMS) {
724                 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
725                         link->conf.ConfigIndex = cf->index;
726                         link->io.BasePort1 = cf->io.win[0].base;
727                         link->io.NumPorts1 = cf->io.win[0].len; /*yo */
728                         link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
729                         i = pcmcia_request_io(link->handle, &link->io);
730                         if (i == CS_SUCCESS)
731                                 break;
732                 }
733                 i = next_tuple(handle, &tuple, &parse);
734         }
735
736         if (i != CS_SUCCESS) {
737                 cs_error(link->handle, RequestIO, i);
738                 goto failed;
739         }
740
741         i = pcmcia_request_irq(link->handle, &link->irq);
742         if (i != CS_SUCCESS) {
743                 cs_error(link->handle, RequestIRQ, i);
744                 link->irq.AssignedIRQ = 0;
745         }
746
747         i = pcmcia_request_configuration(link->handle, &link->conf);
748         if (i != CS_SUCCESS) {
749                 cs_error(link->handle, RequestConfiguration, i);
750                 goto failed;
751         }
752
753         if (dtl1_open(info) != 0)
754                 goto failed;
755
756         strcpy(info->node.dev_name, info->hdev->name);
757         link->dev = &info->node;
758         link->state &= ~DEV_CONFIG_PENDING;
759
760         return;
761
762 cs_failed:
763         cs_error(link->handle, last_fn, last_ret);
764
765 failed:
766         dtl1_release(link);
767 }
768
769
770 void dtl1_release(dev_link_t *link)
771 {
772         dtl1_info_t *info = link->priv;
773
774         if (link->state & DEV_PRESENT)
775                 dtl1_close(info);
776
777         link->dev = NULL;
778
779         pcmcia_release_configuration(link->handle);
780         pcmcia_release_io(link->handle, &link->io);
781         pcmcia_release_irq(link->handle, &link->irq);
782
783         link->state &= ~DEV_CONFIG;
784 }
785
786
787 int dtl1_event(event_t event, int priority, event_callback_args_t *args)
788 {
789         dev_link_t *link = args->client_data;
790         dtl1_info_t *info = link->priv;
791
792         switch (event) {
793         case CS_EVENT_CARD_REMOVAL:
794                 link->state &= ~DEV_PRESENT;
795                 if (link->state & DEV_CONFIG) {
796                         dtl1_close(info);
797                         dtl1_release(link);
798                 }
799                 break;
800         case CS_EVENT_CARD_INSERTION:
801                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
802                 dtl1_config(link);
803                 break;
804         case CS_EVENT_PM_SUSPEND:
805                 link->state |= DEV_SUSPEND;
806                 /* Fall through... */
807         case CS_EVENT_RESET_PHYSICAL:
808                 if (link->state & DEV_CONFIG)
809                         pcmcia_release_configuration(link->handle);
810                 break;
811         case CS_EVENT_PM_RESUME:
812                 link->state &= ~DEV_SUSPEND;
813                 /* Fall through... */
814         case CS_EVENT_CARD_RESET:
815                 if (DEV_OK(link))
816                         pcmcia_request_configuration(link->handle, &link->conf);
817                 break;
818         }
819
820         return 0;
821 }
822
823 static struct pcmcia_driver dtl1_driver = {
824         .owner          = THIS_MODULE,
825         .drv            = {
826                 .name   = "dtl1_cs",
827         },
828         .attach         = dtl1_attach,
829         .detach         = dtl1_detach,
830 };
831
832 static int __init init_dtl1_cs(void)
833 {
834         return pcmcia_register_driver(&dtl1_driver);
835 }
836
837
838 static void __exit exit_dtl1_cs(void)
839 {
840         pcmcia_unregister_driver(&dtl1_driver);
841
842         /* XXX: this really needs to move into generic code.. */
843         while (dev_list != NULL)
844                 dtl1_detach(dev_list);
845 }
846
847 module_init(init_dtl1_cs);
848 module_exit(exit_dtl1_cs);