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