ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / serial / serial_cs.c
1 /*======================================================================
2
3     A driver for PCMCIA serial devices
4
5     serial_cs.c 1.134 2002/05/04 05:48:53
6
7     The contents of this file are subject to the Mozilla Public
8     License Version 1.1 (the "License"); you may not use this file
9     except in compliance with the License. You may obtain a copy of
10     the License at http://www.mozilla.org/MPL/
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     Alternatively, the contents of this file may be used under the
22     terms of the GNU General Public License version 2 (the "GPL"), in which
23     case the provisions of the GPL are applicable instead of the
24     above.  If you wish to allow the use of your version of this file
25     only under the terms of the GPL and not to allow others to use
26     your version of this file under the MPL, indicate your decision
27     by deleting the provisions above and replace them with the notice
28     and other provisions required by the GPL.  If you do not delete
29     the provisions above, a recipient may use your version of this
30     file under either the MPL or the GPL.
31     
32 ======================================================================*/
33
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/sched.h>
39 #include <linux/ptrace.h>
40 #include <linux/slab.h>
41 #include <linux/string.h>
42 #include <linux/timer.h>
43 #include <linux/tty.h>
44 #include <linux/serial.h>
45 #include <linux/serial_core.h>
46 #include <linux/major.h>
47 #include <asm/io.h>
48 #include <asm/system.h>
49
50 #include <pcmcia/version.h>
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/ciscode.h>
55 #include <pcmcia/ds.h>
56 #include <pcmcia/cisreg.h>
57
58 #include "8250.h"
59
60 #ifdef PCMCIA_DEBUG
61 static int pc_debug = PCMCIA_DEBUG;
62 MODULE_PARM(pc_debug, "i");
63 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
64 static char *version = "serial_cs.c 1.134 2002/05/04 05:48:53 (David Hinds)";
65 #else
66 #define DEBUG(n, args...)
67 #endif
68
69 /*====================================================================*/
70
71 /* Parameters that can be set with 'insmod' */
72
73 /* Bit map of interrupts to choose from */
74 static u_int irq_mask = 0xdeb8;
75 static int irq_list[4];
76 static unsigned int irq_list_count;
77
78 /* Enable the speaker? */
79 static int do_sound = 1;
80 /* Skip strict UART tests? */
81 static int buggy_uart;
82
83 module_param(irq_mask, uint, 0444);
84 module_param_array(irq_list, int, irq_list_count, 0444);
85 module_param(do_sound, int, 0444);
86 module_param(buggy_uart, int, 0444);
87
88 /*====================================================================*/
89
90 /* Table of multi-port card ID's */
91
92 struct multi_id {
93         u_short manfid;
94         u_short prodid;
95         int multi;              /* 1 = multifunction, > 1 = # ports */
96 };
97
98 static struct multi_id multi_id[] = {
99         { MANFID_OMEGA,   PRODID_OMEGA_QSP_100,         4 },
100         { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232,    2 },
101         { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232_D1, 2 },
102         { MANFID_QUATECH, PRODID_QUATECH_QUAD_RS232,    4 },
103         { MANFID_SOCKET,  PRODID_SOCKET_DUAL_RS232,     2 },
104         { MANFID_INTEL,   PRODID_INTEL_DUAL_RS232,      2 },
105         { MANFID_NATINST, PRODID_NATINST_QUAD_RS232,    4 }
106 };
107 #define MULTI_COUNT (sizeof(multi_id)/sizeof(struct multi_id))
108
109 struct serial_info {
110         dev_link_t              link;
111         int                     ndev;
112         int                     multi;
113         int                     slave;
114         int                     manfid;
115         dev_node_t              node[4];
116         int                     line[4];
117 };
118
119 static void serial_config(dev_link_t * link);
120 static int serial_event(event_t event, int priority,
121                         event_callback_args_t * args);
122
123 static dev_info_t dev_info = "serial_cs";
124
125 static dev_link_t *serial_attach(void);
126 static void serial_detach(dev_link_t *);
127
128 static dev_link_t *dev_list = NULL;
129
130 /*======================================================================
131
132     After a card is removed, serial_remove() will unregister
133     the serial device(s), and release the PCMCIA configuration.
134     
135 ======================================================================*/
136
137 static void serial_remove(dev_link_t *link)
138 {
139         struct serial_info *info = link->priv;
140         int i;
141
142         link->state &= ~DEV_PRESENT;
143
144         DEBUG(0, "serial_release(0x%p)\n", link);
145
146         /*
147          * Recheck to see if the device is still configured.
148          */
149         if (info->link.state & DEV_CONFIG) {
150                 for (i = 0; i < info->ndev; i++)
151                         unregister_serial(info->line[i]);
152
153                 info->link.dev = NULL;
154
155                 if (!info->slave) {
156                         pcmcia_release_configuration(info->link.handle);
157                         pcmcia_release_io(info->link.handle, &info->link.io);
158                         pcmcia_release_irq(info->link.handle, &info->link.irq);
159                 }
160
161                 info->link.state &= ~DEV_CONFIG;
162         }
163 }
164
165 static void serial_suspend(dev_link_t *link)
166 {
167         link->state |= DEV_SUSPEND;
168
169         if (link->state & DEV_CONFIG) {
170                 struct serial_info *info = link->priv;
171                 int i;
172
173                 for (i = 0; i < info->ndev; i++)
174                         serial8250_suspend_port(info->line[i]);
175
176                 if (!info->slave)
177                         pcmcia_release_configuration(link->handle);
178         }
179 }
180
181 static void serial_resume(dev_link_t *link)
182 {
183         link->state &= ~DEV_SUSPEND;
184
185         if (DEV_OK(link)) {
186                 struct serial_info *info = link->priv;
187                 int i;
188
189                 if (!info->slave)
190                         pcmcia_request_configuration(link->handle, &link->conf);
191
192                 for (i = 0; i < info->ndev; i++)
193                         serial8250_resume_port(info->line[i]);
194         }
195 }
196
197 /*======================================================================
198
199     serial_attach() creates an "instance" of the driver, allocating
200     local data structures for one device.  The device is registered
201     with Card Services.
202
203 ======================================================================*/
204
205 static dev_link_t *serial_attach(void)
206 {
207         struct serial_info *info;
208         client_reg_t client_reg;
209         dev_link_t *link;
210         int i, ret;
211
212         DEBUG(0, "serial_attach()\n");
213
214         /* Create new serial device */
215         info = kmalloc(sizeof (*info), GFP_KERNEL);
216         if (!info)
217                 return NULL;
218         memset(info, 0, sizeof (*info));
219         link = &info->link;
220         link->priv = info;
221
222         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
223         link->io.NumPorts1 = 8;
224         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
225         link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
226         if (irq_list_count == 0)
227                 link->irq.IRQInfo2 = irq_mask;
228         else
229                 for (i = 0; i < irq_list_count; i++)
230                         link->irq.IRQInfo2 |= 1 << irq_list[i];
231         link->conf.Attributes = CONF_ENABLE_IRQ;
232         if (do_sound) {
233                 link->conf.Attributes |= CONF_ENABLE_SPKR;
234                 link->conf.Status = CCSR_AUDIO_ENA;
235         }
236         link->conf.IntType = INT_MEMORY_AND_IO;
237
238         /* Register with Card Services */
239         link->next = dev_list;
240         dev_list = link;
241         client_reg.dev_info = &dev_info;
242         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
243         client_reg.EventMask =
244             CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
245             CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
246             CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
247         client_reg.event_handler = &serial_event;
248         client_reg.Version = 0x0210;
249         client_reg.event_callback_args.client_data = link;
250         ret = pcmcia_register_client(&link->handle, &client_reg);
251         if (ret != CS_SUCCESS) {
252                 cs_error(link->handle, RegisterClient, ret);
253                 serial_detach(link);
254                 return NULL;
255         }
256
257         return link;
258 }
259
260 /*======================================================================
261
262     This deletes a driver "instance".  The device is de-registered
263     with Card Services.  If it has been released, all local data
264     structures are freed.  Otherwise, the structures will be freed
265     when the device is released.
266
267 ======================================================================*/
268
269 static void serial_detach(dev_link_t * link)
270 {
271         struct serial_info *info = link->priv;
272         dev_link_t **linkp;
273         int ret;
274
275         DEBUG(0, "serial_detach(0x%p)\n", link);
276
277         /* Locate device structure */
278         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
279                 if (*linkp == link)
280                         break;
281         if (*linkp == NULL)
282                 return;
283
284         /*
285          * Ensure any outstanding scheduled tasks are completed.
286          */
287         flush_scheduled_work();
288
289         /*
290          * Ensure that the ports have been released.
291          */
292         serial_remove(link);
293
294         if (link->handle) {
295                 ret = pcmcia_deregister_client(link->handle);
296                 if (ret != CS_SUCCESS)
297                         cs_error(link->handle, DeregisterClient, ret);
298         }
299
300         /* Unlink device structure, free bits */
301         *linkp = link->next;
302         kfree(info);
303 }
304
305 /*====================================================================*/
306
307 static int setup_serial(struct serial_info * info, ioaddr_t port, int irq)
308 {
309         struct serial_struct serial;
310         int line;
311
312         memset(&serial, 0, sizeof (serial));
313         serial.port = port;
314         serial.irq = irq;
315         serial.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ;
316         if (buggy_uart)
317                 serial.flags |= UPF_BUGGY_UART;
318         line = register_serial(&serial);
319         if (line < 0) {
320                 printk(KERN_NOTICE "serial_cs: register_serial() at 0x%04lx,"
321                        " irq %d failed\n", (u_long) serial.port, serial.irq);
322                 return -EINVAL;
323         }
324
325         info->line[info->ndev] = line;
326         sprintf(info->node[info->ndev].dev_name, "ttyS%d", line);
327         info->node[info->ndev].major = TTY_MAJOR;
328         info->node[info->ndev].minor = 0x40 + line;
329         if (info->ndev > 0)
330                 info->node[info->ndev - 1].next = &info->node[info->ndev];
331         info->ndev++;
332
333         return 0;
334 }
335
336 /*====================================================================*/
337
338 static int
339 first_tuple(client_handle_t handle, tuple_t * tuple, cisparse_t * parse)
340 {
341         int i;
342         i = pcmcia_get_first_tuple(handle, tuple);
343         if (i != CS_SUCCESS)
344                 return CS_NO_MORE_ITEMS;
345         i = pcmcia_get_tuple_data(handle, tuple);
346         if (i != CS_SUCCESS)
347                 return i;
348         return pcmcia_parse_tuple(handle, tuple, parse);
349 }
350
351 static int
352 next_tuple(client_handle_t handle, tuple_t * tuple, cisparse_t * parse)
353 {
354         int i;
355         i = pcmcia_get_next_tuple(handle, tuple);
356         if (i != CS_SUCCESS)
357                 return CS_NO_MORE_ITEMS;
358         i = pcmcia_get_tuple_data(handle, tuple);
359         if (i != CS_SUCCESS)
360                 return i;
361         return pcmcia_parse_tuple(handle, tuple, parse);
362 }
363
364 /*====================================================================*/
365
366 static int simple_config(dev_link_t * link)
367 {
368         static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
369         client_handle_t handle = link->handle;
370         struct serial_info *info = link->priv;
371         tuple_t tuple;
372         u_char buf[256];
373         cisparse_t parse;
374         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
375         config_info_t config;
376         int i, j, try;
377
378         /* If the card is already configured, look up the port and irq */
379         i = pcmcia_get_configuration_info(handle, &config);
380         if ((i == CS_SUCCESS) && (config.Attributes & CONF_VALID_CLIENT)) {
381                 ioaddr_t port = 0;
382                 if ((config.BasePort2 != 0) && (config.NumPorts2 == 8)) {
383                         port = config.BasePort2;
384                         info->slave = 1;
385                 } else if ((info->manfid == MANFID_OSITECH) &&
386                            (config.NumPorts1 == 0x40)) {
387                         port = config.BasePort1 + 0x28;
388                         info->slave = 1;
389                 }
390                 if (info->slave)
391                         return setup_serial(info, port, config.AssignedIRQ);
392         }
393         link->conf.Vcc = config.Vcc;
394
395         /* First pass: look for a config entry that looks normal. */
396         tuple.TupleData = (cisdata_t *) buf;
397         tuple.TupleOffset = 0;
398         tuple.TupleDataMax = 255;
399         tuple.Attributes = 0;
400         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
401         /* Two tries: without IO aliases, then with aliases */
402         for (try = 0; try < 2; try++) {
403                 i = first_tuple(handle, &tuple, &parse);
404                 while (i != CS_NO_MORE_ITEMS) {
405                         if (i != CS_SUCCESS)
406                                 goto next_entry;
407                         if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
408                                 link->conf.Vpp1 = link->conf.Vpp2 =
409                                     cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
410                         if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) &&
411                             (cf->io.win[0].base != 0)) {
412                                 link->conf.ConfigIndex = cf->index;
413                                 link->io.BasePort1 = cf->io.win[0].base;
414                                 link->io.IOAddrLines = (try == 0) ?
415                                     16 : cf->io.flags & CISTPL_IO_LINES_MASK;
416                                 i = pcmcia_request_io(link->handle, &link->io);
417                                 if (i == CS_SUCCESS)
418                                         goto found_port;
419                         }
420                       next_entry:
421                         i = next_tuple(handle, &tuple, &parse);
422                 }
423         }
424
425         /* Second pass: try to find an entry that isn't picky about
426            its base address, then try to grab any standard serial port
427            address, and finally try to get any free port. */
428         i = first_tuple(handle, &tuple, &parse);
429         while (i != CS_NO_MORE_ITEMS) {
430                 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) &&
431                     ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
432                         link->conf.ConfigIndex = cf->index;
433                         for (j = 0; j < 5; j++) {
434                                 link->io.BasePort1 = base[j];
435                                 link->io.IOAddrLines = base[j] ? 16 : 3;
436                                 i = pcmcia_request_io(link->handle, &link->io);
437                                 if (i == CS_SUCCESS)
438                                         goto found_port;
439                         }
440                 }
441                 i = next_tuple(handle, &tuple, &parse);
442         }
443
444       found_port:
445         if (i != CS_SUCCESS) {
446                 printk(KERN_NOTICE
447                        "serial_cs: no usable port range found, giving up\n");
448                 cs_error(link->handle, RequestIO, i);
449                 return -1;
450         }
451
452         i = pcmcia_request_irq(link->handle, &link->irq);
453         if (i != CS_SUCCESS) {
454                 cs_error(link->handle, RequestIRQ, i);
455                 link->irq.AssignedIRQ = 0;
456         }
457         if (info->multi && (info->manfid == MANFID_3COM))
458                 link->conf.ConfigIndex &= ~(0x08);
459         i = pcmcia_request_configuration(link->handle, &link->conf);
460         if (i != CS_SUCCESS) {
461                 cs_error(link->handle, RequestConfiguration, i);
462                 return -1;
463         }
464
465         return setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
466 }
467
468 static int multi_config(dev_link_t * link)
469 {
470         client_handle_t handle = link->handle;
471         struct serial_info *info = link->priv;
472         tuple_t tuple;
473         u_char buf[256];
474         cisparse_t parse;
475         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
476         config_info_t config;
477         int i, base2 = 0;
478
479         i = pcmcia_get_configuration_info(handle, &config);
480         if (i != CS_SUCCESS) {
481                 cs_error(handle, GetConfigurationInfo, i);
482                 return -1;
483         }
484         link->conf.Vcc = config.Vcc;
485
486         tuple.TupleData = (cisdata_t *) buf;
487         tuple.TupleOffset = 0;
488         tuple.TupleDataMax = 255;
489         tuple.Attributes = 0;
490         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
491
492         /* First, look for a generic full-sized window */
493         link->io.NumPorts1 = info->multi * 8;
494         i = first_tuple(handle, &tuple, &parse);
495         while (i != CS_NO_MORE_ITEMS) {
496                 /* The quad port cards have bad CIS's, so just look for a
497                    window larger than 8 ports and assume it will be right */
498                 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) &&
499                     (cf->io.win[0].len > 8)) {
500                         link->conf.ConfigIndex = cf->index;
501                         link->io.BasePort1 = cf->io.win[0].base;
502                         link->io.IOAddrLines =
503                             cf->io.flags & CISTPL_IO_LINES_MASK;
504                         i = pcmcia_request_io(link->handle, &link->io);
505                         base2 = link->io.BasePort1 + 8;
506                         if (i == CS_SUCCESS)
507                                 break;
508                 }
509                 i = next_tuple(handle, &tuple, &parse);
510         }
511
512         /* If that didn't work, look for two windows */
513         if (i != CS_SUCCESS) {
514                 link->io.NumPorts1 = link->io.NumPorts2 = 8;
515                 info->multi = 2;
516                 i = first_tuple(handle, &tuple, &parse);
517                 while (i != CS_NO_MORE_ITEMS) {
518                         if ((i == CS_SUCCESS) && (cf->io.nwin == 2)) {
519                                 link->conf.ConfigIndex = cf->index;
520                                 link->io.BasePort1 = cf->io.win[0].base;
521                                 link->io.BasePort2 = cf->io.win[1].base;
522                                 link->io.IOAddrLines =
523                                     cf->io.flags & CISTPL_IO_LINES_MASK;
524                                 i = pcmcia_request_io(link->handle, &link->io);
525                                 base2 = link->io.BasePort2;
526                                 if (i == CS_SUCCESS)
527                                         break;
528                         }
529                         i = next_tuple(handle, &tuple, &parse);
530                 }
531         }
532
533         if (i != CS_SUCCESS) {
534                 cs_error(link->handle, RequestIO, i);
535                 return -1;
536         }
537
538         i = pcmcia_request_irq(link->handle, &link->irq);
539         if (i != CS_SUCCESS) {
540                 printk(KERN_NOTICE
541                        "serial_cs: no usable port range found, giving up\n");
542                 cs_error(link->handle, RequestIRQ, i);
543                 link->irq.AssignedIRQ = 0;
544         }
545         /* Socket Dual IO: this enables irq's for second port */
546         if (info->multi && (info->manfid == MANFID_SOCKET)) {
547                 link->conf.Present |= PRESENT_EXT_STATUS;
548                 link->conf.ExtStatus = ESR_REQ_ATTN_ENA;
549         }
550         i = pcmcia_request_configuration(link->handle, &link->conf);
551         if (i != CS_SUCCESS) {
552                 cs_error(link->handle, RequestConfiguration, i);
553                 return -1;
554         }
555
556         /* The Oxford Semiconductor OXCF950 cards are in fact single-port:
557            8 registers are for the UART, the others are extra registers */
558         if (info->manfid == MANFID_OXSEMI) {
559                 if (cf->index == 1 || cf->index == 3) {
560                         setup_serial(info, base2, link->irq.AssignedIRQ);
561                         outb(12, link->io.BasePort1 + 1);
562                 } else {
563                         setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
564                         outb(12, base2 + 1);
565                 }
566                 return 0;
567         }
568
569         setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
570         /* The Nokia cards are not really multiport cards */
571         if (info->manfid == MANFID_NOKIA)
572                 return 0;
573         for (i = 0; i < info->multi - 1; i++)
574                 setup_serial(info, base2 + (8 * i), link->irq.AssignedIRQ);
575
576         return 0;
577 }
578
579 /*======================================================================
580
581     serial_config() is scheduled to run after a CARD_INSERTION event
582     is received, to configure the PCMCIA socket, and to make the
583     serial device available to the system.
584
585 ======================================================================*/
586
587 void serial_config(dev_link_t * link)
588 {
589         client_handle_t handle = link->handle;
590         struct serial_info *info = link->priv;
591         tuple_t tuple;
592         u_short buf[128];
593         cisparse_t parse;
594         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
595         int i, last_ret, last_fn;
596
597         DEBUG(0, "serial_config(0x%p)\n", link);
598
599         tuple.TupleData = (cisdata_t *) buf;
600         tuple.TupleOffset = 0;
601         tuple.TupleDataMax = 255;
602         tuple.Attributes = 0;
603         /* Get configuration register information */
604         tuple.DesiredTuple = CISTPL_CONFIG;
605         last_ret = first_tuple(handle, &tuple, &parse);
606         if (last_ret != CS_SUCCESS) {
607                 last_fn = ParseTuple;
608                 goto cs_failed;
609         }
610         link->conf.ConfigBase = parse.config.base;
611         link->conf.Present = parse.config.rmask[0];
612
613         /* Configure card */
614         link->state |= DEV_CONFIG;
615
616         /* Is this a compliant multifunction card? */
617         tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
618         tuple.Attributes = TUPLE_RETURN_COMMON | TUPLE_RETURN_LINK;
619         info->multi = (first_tuple(handle, &tuple, &parse) == CS_SUCCESS);
620
621         /* Is this a multiport card? */
622         tuple.DesiredTuple = CISTPL_MANFID;
623         if (first_tuple(handle, &tuple, &parse) == CS_SUCCESS) {
624                 info->manfid = le16_to_cpu(buf[0]);
625                 for (i = 0; i < MULTI_COUNT; i++)
626                         if ((info->manfid == multi_id[i].manfid) &&
627                             (le16_to_cpu(buf[1]) == multi_id[i].prodid))
628                                 break;
629                 if (i < MULTI_COUNT)
630                         info->multi = multi_id[i].multi;
631         }
632
633         /* Another check for dual-serial cards: look for either serial or
634            multifunction cards that ask for appropriate IO port ranges */
635         tuple.DesiredTuple = CISTPL_FUNCID;
636         if ((info->multi == 0) &&
637             ((first_tuple(handle, &tuple, &parse) != CS_SUCCESS) ||
638              (parse.funcid.func == CISTPL_FUNCID_MULTI) ||
639              (parse.funcid.func == CISTPL_FUNCID_SERIAL))) {
640                 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
641                 if (first_tuple(handle, &tuple, &parse) == CS_SUCCESS) {
642                         if ((cf->io.nwin == 1) && (cf->io.win[0].len % 8 == 0))
643                                 info->multi = cf->io.win[0].len >> 3;
644                         if ((cf->io.nwin == 2) && (cf->io.win[0].len == 8) &&
645                             (cf->io.win[1].len == 8))
646                                 info->multi = 2;
647                 }
648         }
649
650         if (info->multi > 1)
651                 multi_config(link);
652         else
653                 simple_config(link);
654
655         if (info->ndev == 0)
656                 goto failed;
657
658         if (info->manfid == MANFID_IBM) {
659                 conf_reg_t reg = { 0, CS_READ, 0x800, 0 };
660                 last_ret = pcmcia_access_configuration_register(link->handle, &reg);
661                 if (last_ret) {
662                         last_fn = AccessConfigurationRegister;
663                         goto cs_failed;
664                 }
665                 reg.Action = CS_WRITE;
666                 reg.Value = reg.Value | 1;
667                 last_ret = pcmcia_access_configuration_register(link->handle, &reg);
668                 if (last_ret) {
669                         last_fn = AccessConfigurationRegister;
670                         goto cs_failed;
671                 }
672         }
673
674         link->dev = &info->node[0];
675         link->state &= ~DEV_CONFIG_PENDING;
676         return;
677
678  cs_failed:
679         cs_error(link->handle, last_fn, last_ret);
680  failed:
681         serial_remove(link);
682         link->state &= ~DEV_CONFIG_PENDING;
683 }
684
685 /*======================================================================
686
687     The card status event handler.  Mostly, this schedules other
688     stuff to run after an event is received.  A CARD_REMOVAL event
689     also sets some flags to discourage the serial drivers from
690     talking to the ports.
691     
692 ======================================================================*/
693
694 static int
695 serial_event(event_t event, int priority, event_callback_args_t * args)
696 {
697         dev_link_t *link = args->client_data;
698         struct serial_info *info = link->priv;
699
700         DEBUG(1, "serial_event(0x%06x)\n", event);
701
702         switch (event) {
703         case CS_EVENT_CARD_REMOVAL:
704                 serial_remove(link);
705                 break;
706
707         case CS_EVENT_CARD_INSERTION:
708                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
709                 serial_config(link);
710                 break;
711
712         case CS_EVENT_PM_SUSPEND:
713                 serial_suspend(link);
714                 break;
715
716         case CS_EVENT_RESET_PHYSICAL:
717                 if ((link->state & DEV_CONFIG) && !info->slave)
718                         pcmcia_release_configuration(link->handle);
719                 break;
720
721         case CS_EVENT_PM_RESUME:
722                 serial_resume(link);
723                 break;
724
725         case CS_EVENT_CARD_RESET:
726                 if (DEV_OK(link) && !info->slave)
727                         pcmcia_request_configuration(link->handle, &link->conf);
728                 break;
729         }
730         return 0;
731 }
732
733 static struct pcmcia_driver serial_cs_driver = {
734         .owner          = THIS_MODULE,
735         .drv            = {
736                 .name   = "serial_cs",
737         },
738         .attach         = serial_attach,
739         .detach         = serial_detach,
740 };
741
742 static int __init init_serial_cs(void)
743 {
744         return pcmcia_register_driver(&serial_cs_driver);
745 }
746
747 static void __exit exit_serial_cs(void)
748 {
749         pcmcia_unregister_driver(&serial_cs_driver);
750
751         /* XXX: this really needs to move into generic code.. */
752         while (dev_list != NULL)
753                 serial_detach(dev_list);
754 }
755
756 module_init(init_serial_cs);
757 module_exit(exit_serial_cs);
758
759 MODULE_LICENSE("GPL");