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