1 /*======================================================================
3 A driver for PCMCIA serial devices
5 serial_cs.c 1.134 2002/05/04 05:48:53
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/
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.
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.
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.
32 ======================================================================*/
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/serial_core.h>
44 #include <linux/major.h>
46 #include <asm/system.h>
48 #include <pcmcia/version.h>
49 #include <pcmcia/cs_types.h>
50 #include <pcmcia/cs.h>
51 #include <pcmcia/cistpl.h>
52 #include <pcmcia/ciscode.h>
53 #include <pcmcia/ds.h>
54 #include <pcmcia/cisreg.h>
59 static int pc_debug = PCMCIA_DEBUG;
60 module_param(pc_debug, int, 0644);
61 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
62 static char *version = "serial_cs.c 1.134 2002/05/04 05:48:53 (David Hinds)";
64 #define DEBUG(n, args...)
67 /*====================================================================*/
69 /* Parameters that can be set with 'insmod' */
71 /* Enable the speaker? */
72 static int do_sound = 1;
73 /* Skip strict UART tests? */
74 static int buggy_uart;
76 module_param(do_sound, int, 0444);
77 module_param(buggy_uart, int, 0444);
79 /*====================================================================*/
81 /* Table of multi-port card ID's */
86 int multi; /* 1 = multifunction, > 1 = # ports */
89 static struct multi_id multi_id[] = {
90 { MANFID_OMEGA, PRODID_OMEGA_QSP_100, 4 },
91 { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232, 2 },
92 { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232_D1, 2 },
93 { MANFID_QUATECH, PRODID_QUATECH_QUAD_RS232, 4 },
94 { MANFID_SOCKET, PRODID_SOCKET_DUAL_RS232, 2 },
95 { MANFID_INTEL, PRODID_INTEL_DUAL_RS232, 2 },
96 { MANFID_NATINST, PRODID_NATINST_QUAD_RS232, 4 }
98 #define MULTI_COUNT (sizeof(multi_id)/sizeof(struct multi_id))
110 static void serial_config(dev_link_t * link);
111 static int serial_event(event_t event, int priority,
112 event_callback_args_t * args);
114 static dev_info_t dev_info = "serial_cs";
116 static dev_link_t *serial_attach(void);
117 static void serial_detach(dev_link_t *);
119 static dev_link_t *dev_list = NULL;
121 /*======================================================================
123 After a card is removed, serial_remove() will unregister
124 the serial device(s), and release the PCMCIA configuration.
126 ======================================================================*/
128 static void serial_remove(dev_link_t *link)
130 struct serial_info *info = link->priv;
133 link->state &= ~DEV_PRESENT;
135 DEBUG(0, "serial_release(0x%p)\n", link);
138 * Recheck to see if the device is still configured.
140 if (info->link.state & DEV_CONFIG) {
141 for (i = 0; i < info->ndev; i++)
142 serial8250_unregister_port(info->line[i]);
144 info->link.dev = NULL;
147 pcmcia_release_configuration(info->link.handle);
148 pcmcia_release_io(info->link.handle, &info->link.io);
149 pcmcia_release_irq(info->link.handle, &info->link.irq);
152 info->link.state &= ~DEV_CONFIG;
156 static void serial_suspend(dev_link_t *link)
158 link->state |= DEV_SUSPEND;
160 if (link->state & DEV_CONFIG) {
161 struct serial_info *info = link->priv;
164 for (i = 0; i < info->ndev; i++)
165 serial8250_suspend_port(info->line[i]);
168 pcmcia_release_configuration(link->handle);
172 static void serial_resume(dev_link_t *link)
174 link->state &= ~DEV_SUSPEND;
177 struct serial_info *info = link->priv;
181 pcmcia_request_configuration(link->handle, &link->conf);
183 for (i = 0; i < info->ndev; i++)
184 serial8250_resume_port(info->line[i]);
188 /*======================================================================
190 serial_attach() creates an "instance" of the driver, allocating
191 local data structures for one device. The device is registered
194 ======================================================================*/
196 static dev_link_t *serial_attach(void)
198 struct serial_info *info;
199 client_reg_t client_reg;
203 DEBUG(0, "serial_attach()\n");
205 /* Create new serial device */
206 info = kmalloc(sizeof (*info), GFP_KERNEL);
209 memset(info, 0, sizeof (*info));
213 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
214 link->io.NumPorts1 = 8;
215 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
216 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
217 link->conf.Attributes = CONF_ENABLE_IRQ;
219 link->conf.Attributes |= CONF_ENABLE_SPKR;
220 link->conf.Status = CCSR_AUDIO_ENA;
222 link->conf.IntType = INT_MEMORY_AND_IO;
224 /* Register with Card Services */
225 link->next = dev_list;
227 client_reg.dev_info = &dev_info;
228 client_reg.EventMask =
229 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
230 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
231 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
232 client_reg.event_handler = &serial_event;
233 client_reg.Version = 0x0210;
234 client_reg.event_callback_args.client_data = link;
235 ret = pcmcia_register_client(&link->handle, &client_reg);
236 if (ret != CS_SUCCESS) {
237 cs_error(link->handle, RegisterClient, ret);
245 /*======================================================================
247 This deletes a driver "instance". The device is de-registered
248 with Card Services. If it has been released, all local data
249 structures are freed. Otherwise, the structures will be freed
250 when the device is released.
252 ======================================================================*/
254 static void serial_detach(dev_link_t * link)
256 struct serial_info *info = link->priv;
260 DEBUG(0, "serial_detach(0x%p)\n", link);
262 /* Locate device structure */
263 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
270 * Ensure any outstanding scheduled tasks are completed.
272 flush_scheduled_work();
275 * Ensure that the ports have been released.
280 ret = pcmcia_deregister_client(link->handle);
281 if (ret != CS_SUCCESS)
282 cs_error(link->handle, DeregisterClient, ret);
285 /* Unlink device structure, free bits */
290 /*====================================================================*/
292 static int setup_serial(struct serial_info * info, kio_addr_t iobase, int irq)
294 struct uart_port port;
297 memset(&port, 0, sizeof (struct uart_port));
298 port.iobase = iobase;
300 port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
301 port.uartclk = 1843200;
303 port.flags |= UPF_BUGGY_UART;
304 line = serial8250_register_port(&port);
306 printk(KERN_NOTICE "serial_cs: serial8250_register_port() at "
307 "0x%04lx, irq %d failed\n", (u_long)iobase, irq);
311 info->line[info->ndev] = line;
312 sprintf(info->node[info->ndev].dev_name, "ttyS%d", line);
313 info->node[info->ndev].major = TTY_MAJOR;
314 info->node[info->ndev].minor = 0x40 + line;
316 info->node[info->ndev - 1].next = &info->node[info->ndev];
322 /*====================================================================*/
325 first_tuple(client_handle_t handle, tuple_t * tuple, cisparse_t * parse)
328 i = pcmcia_get_first_tuple(handle, tuple);
330 return CS_NO_MORE_ITEMS;
331 i = pcmcia_get_tuple_data(handle, tuple);
334 return pcmcia_parse_tuple(handle, tuple, parse);
338 next_tuple(client_handle_t handle, tuple_t * tuple, cisparse_t * parse)
341 i = pcmcia_get_next_tuple(handle, tuple);
343 return CS_NO_MORE_ITEMS;
344 i = pcmcia_get_tuple_data(handle, tuple);
347 return pcmcia_parse_tuple(handle, tuple, parse);
350 /*====================================================================*/
352 static int simple_config(dev_link_t *link)
354 static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
355 static int size_table[2] = { 8, 16 };
356 client_handle_t handle = link->handle;
357 struct serial_info *info = link->priv;
361 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
362 config_info_t config;
366 /* If the card is already configured, look up the port and irq */
367 i = pcmcia_get_configuration_info(handle, &config);
368 if ((i == CS_SUCCESS) && (config.Attributes & CONF_VALID_CLIENT)) {
370 if ((config.BasePort2 != 0) && (config.NumPorts2 == 8)) {
371 port = config.BasePort2;
373 } else if ((info->manfid == MANFID_OSITECH) &&
374 (config.NumPorts1 == 0x40)) {
375 port = config.BasePort1 + 0x28;
379 return setup_serial(info, port, config.AssignedIRQ);
381 link->conf.Vcc = config.Vcc;
383 /* First pass: look for a config entry that looks normal. */
384 tuple.TupleData = (cisdata_t *) buf;
385 tuple.TupleOffset = 0;
386 tuple.TupleDataMax = 255;
387 tuple.Attributes = 0;
388 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
389 /* Two tries: without IO aliases, then with aliases */
390 for (s = 0; s < 2; s++) {
391 for (try = 0; try < 2; try++) {
392 i = first_tuple(handle, &tuple, &parse);
393 while (i != CS_NO_MORE_ITEMS) {
396 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
397 link->conf.Vpp1 = link->conf.Vpp2 =
398 cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
399 if ((cf->io.nwin > 0) && (cf->io.win[0].len == size_table[s]) &&
400 (cf->io.win[0].base != 0)) {
401 link->conf.ConfigIndex = cf->index;
402 link->io.BasePort1 = cf->io.win[0].base;
403 link->io.IOAddrLines = (try == 0) ?
404 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
405 i = pcmcia_request_io(link->handle, &link->io);
410 i = next_tuple(handle, &tuple, &parse);
414 /* Second pass: try to find an entry that isn't picky about
415 its base address, then try to grab any standard serial port
416 address, and finally try to get any free port. */
417 i = first_tuple(handle, &tuple, &parse);
418 while (i != CS_NO_MORE_ITEMS) {
419 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) &&
420 ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
421 link->conf.ConfigIndex = cf->index;
422 for (j = 0; j < 5; j++) {
423 link->io.BasePort1 = base[j];
424 link->io.IOAddrLines = base[j] ? 16 : 3;
425 i = pcmcia_request_io(link->handle, &link->io);
430 i = next_tuple(handle, &tuple, &parse);
434 if (i != CS_SUCCESS) {
436 "serial_cs: no usable port range found, giving up\n");
437 cs_error(link->handle, RequestIO, i);
441 i = pcmcia_request_irq(link->handle, &link->irq);
442 if (i != CS_SUCCESS) {
443 cs_error(link->handle, RequestIRQ, i);
444 link->irq.AssignedIRQ = 0;
446 if (info->multi && (info->manfid == MANFID_3COM))
447 link->conf.ConfigIndex &= ~(0x08);
448 i = pcmcia_request_configuration(link->handle, &link->conf);
449 if (i != CS_SUCCESS) {
450 cs_error(link->handle, RequestConfiguration, i);
454 return setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
457 static int multi_config(dev_link_t * link)
459 client_handle_t handle = link->handle;
460 struct serial_info *info = link->priv;
464 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
465 config_info_t config;
468 i = pcmcia_get_configuration_info(handle, &config);
469 if (i != CS_SUCCESS) {
470 cs_error(handle, GetConfigurationInfo, i);
473 link->conf.Vcc = config.Vcc;
475 tuple.TupleData = (cisdata_t *) buf;
476 tuple.TupleOffset = 0;
477 tuple.TupleDataMax = 255;
478 tuple.Attributes = 0;
479 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
481 /* First, look for a generic full-sized window */
482 link->io.NumPorts1 = info->multi * 8;
483 i = first_tuple(handle, &tuple, &parse);
484 while (i != CS_NO_MORE_ITEMS) {
485 /* The quad port cards have bad CIS's, so just look for a
486 window larger than 8 ports and assume it will be right */
487 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) &&
488 (cf->io.win[0].len > 8)) {
489 link->conf.ConfigIndex = cf->index;
490 link->io.BasePort1 = cf->io.win[0].base;
491 link->io.IOAddrLines =
492 cf->io.flags & CISTPL_IO_LINES_MASK;
493 i = pcmcia_request_io(link->handle, &link->io);
494 base2 = link->io.BasePort1 + 8;
498 i = next_tuple(handle, &tuple, &parse);
501 /* If that didn't work, look for two windows */
502 if (i != CS_SUCCESS) {
503 link->io.NumPorts1 = link->io.NumPorts2 = 8;
505 i = first_tuple(handle, &tuple, &parse);
506 while (i != CS_NO_MORE_ITEMS) {
507 if ((i == CS_SUCCESS) && (cf->io.nwin == 2)) {
508 link->conf.ConfigIndex = cf->index;
509 link->io.BasePort1 = cf->io.win[0].base;
510 link->io.BasePort2 = cf->io.win[1].base;
511 link->io.IOAddrLines =
512 cf->io.flags & CISTPL_IO_LINES_MASK;
513 i = pcmcia_request_io(link->handle, &link->io);
514 base2 = link->io.BasePort2;
518 i = next_tuple(handle, &tuple, &parse);
522 if (i != CS_SUCCESS) {
523 cs_error(link->handle, RequestIO, i);
527 i = pcmcia_request_irq(link->handle, &link->irq);
528 if (i != CS_SUCCESS) {
530 "serial_cs: no usable port range found, giving up\n");
531 cs_error(link->handle, RequestIRQ, i);
532 link->irq.AssignedIRQ = 0;
534 /* Socket Dual IO: this enables irq's for second port */
535 if (info->multi && (info->manfid == MANFID_SOCKET)) {
536 link->conf.Present |= PRESENT_EXT_STATUS;
537 link->conf.ExtStatus = ESR_REQ_ATTN_ENA;
539 i = pcmcia_request_configuration(link->handle, &link->conf);
540 if (i != CS_SUCCESS) {
541 cs_error(link->handle, RequestConfiguration, i);
545 /* The Oxford Semiconductor OXCF950 cards are in fact single-port:
546 8 registers are for the UART, the others are extra registers */
547 if (info->manfid == MANFID_OXSEMI) {
548 if (cf->index == 1 || cf->index == 3) {
549 setup_serial(info, base2, link->irq.AssignedIRQ);
550 outb(12, link->io.BasePort1 + 1);
552 setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
558 setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
559 /* The Nokia cards are not really multiport cards */
560 if (info->manfid == MANFID_NOKIA)
562 for (i = 0; i < info->multi - 1; i++)
563 setup_serial(info, base2 + (8 * i), link->irq.AssignedIRQ);
568 /*======================================================================
570 serial_config() is scheduled to run after a CARD_INSERTION event
571 is received, to configure the PCMCIA socket, and to make the
572 serial device available to the system.
574 ======================================================================*/
576 void serial_config(dev_link_t * link)
578 client_handle_t handle = link->handle;
579 struct serial_info *info = link->priv;
583 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
584 int i, last_ret, last_fn;
586 DEBUG(0, "serial_config(0x%p)\n", link);
588 tuple.TupleData = (cisdata_t *) buf;
589 tuple.TupleOffset = 0;
590 tuple.TupleDataMax = 255;
591 tuple.Attributes = 0;
592 /* Get configuration register information */
593 tuple.DesiredTuple = CISTPL_CONFIG;
594 last_ret = first_tuple(handle, &tuple, &parse);
595 if (last_ret != CS_SUCCESS) {
596 last_fn = ParseTuple;
599 link->conf.ConfigBase = parse.config.base;
600 link->conf.Present = parse.config.rmask[0];
603 link->state |= DEV_CONFIG;
605 /* Is this a compliant multifunction card? */
606 tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
607 tuple.Attributes = TUPLE_RETURN_COMMON | TUPLE_RETURN_LINK;
608 info->multi = (first_tuple(handle, &tuple, &parse) == CS_SUCCESS);
610 /* Is this a multiport card? */
611 tuple.DesiredTuple = CISTPL_MANFID;
612 if (first_tuple(handle, &tuple, &parse) == CS_SUCCESS) {
613 info->manfid = le16_to_cpu(buf[0]);
614 for (i = 0; i < MULTI_COUNT; i++)
615 if ((info->manfid == multi_id[i].manfid) &&
616 (le16_to_cpu(buf[1]) == multi_id[i].prodid))
619 info->multi = multi_id[i].multi;
622 /* Another check for dual-serial cards: look for either serial or
623 multifunction cards that ask for appropriate IO port ranges */
624 tuple.DesiredTuple = CISTPL_FUNCID;
625 if ((info->multi == 0) &&
626 ((first_tuple(handle, &tuple, &parse) != CS_SUCCESS) ||
627 (parse.funcid.func == CISTPL_FUNCID_MULTI) ||
628 (parse.funcid.func == CISTPL_FUNCID_SERIAL))) {
629 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
630 if (first_tuple(handle, &tuple, &parse) == CS_SUCCESS) {
631 if ((cf->io.nwin == 1) && (cf->io.win[0].len % 8 == 0))
632 info->multi = cf->io.win[0].len >> 3;
633 if ((cf->io.nwin == 2) && (cf->io.win[0].len == 8) &&
634 (cf->io.win[1].len == 8))
647 if (info->manfid == MANFID_IBM) {
648 conf_reg_t reg = { 0, CS_READ, 0x800, 0 };
649 last_ret = pcmcia_access_configuration_register(link->handle, ®);
651 last_fn = AccessConfigurationRegister;
654 reg.Action = CS_WRITE;
655 reg.Value = reg.Value | 1;
656 last_ret = pcmcia_access_configuration_register(link->handle, ®);
658 last_fn = AccessConfigurationRegister;
663 link->dev = &info->node[0];
664 link->state &= ~DEV_CONFIG_PENDING;
668 cs_error(link->handle, last_fn, last_ret);
671 link->state &= ~DEV_CONFIG_PENDING;
674 /*======================================================================
676 The card status event handler. Mostly, this schedules other
677 stuff to run after an event is received. A CARD_REMOVAL event
678 also sets some flags to discourage the serial drivers from
679 talking to the ports.
681 ======================================================================*/
684 serial_event(event_t event, int priority, event_callback_args_t * args)
686 dev_link_t *link = args->client_data;
687 struct serial_info *info = link->priv;
689 DEBUG(1, "serial_event(0x%06x)\n", event);
692 case CS_EVENT_CARD_REMOVAL:
696 case CS_EVENT_CARD_INSERTION:
697 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
701 case CS_EVENT_PM_SUSPEND:
702 serial_suspend(link);
705 case CS_EVENT_RESET_PHYSICAL:
706 if ((link->state & DEV_CONFIG) && !info->slave)
707 pcmcia_release_configuration(link->handle);
710 case CS_EVENT_PM_RESUME:
714 case CS_EVENT_CARD_RESET:
715 if (DEV_OK(link) && !info->slave)
716 pcmcia_request_configuration(link->handle, &link->conf);
722 static struct pcmcia_driver serial_cs_driver = {
723 .owner = THIS_MODULE,
727 .attach = serial_attach,
728 .detach = serial_detach,
731 static int __init init_serial_cs(void)
733 return pcmcia_register_driver(&serial_cs_driver);
736 static void __exit exit_serial_cs(void)
738 pcmcia_unregister_driver(&serial_cs_driver);
739 BUG_ON(dev_list != NULL);
742 module_init(init_serial_cs);
743 module_exit(exit_serial_cs);
745 MODULE_LICENSE("GPL");