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/tty.h>
44 #include <linux/serial.h>
45 #include <linux/serial_core.h>
46 #include <linux/major.h>
48 #include <asm/system.h>
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>
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)";
66 #define DEBUG(n, args...)
69 /*====================================================================*/
71 /* Parameters that can be set with 'insmod' */
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;
78 /* Enable the speaker? */
79 static int do_sound = 1;
80 /* Skip strict UART tests? */
81 static int buggy_uart;
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);
88 /*====================================================================*/
90 /* Table of multi-port card ID's */
95 int multi; /* 1 = multifunction, > 1 = # ports */
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 }
107 #define MULTI_COUNT (sizeof(multi_id)/sizeof(struct multi_id))
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);
123 static dev_info_t dev_info = "serial_cs";
125 static dev_link_t *serial_attach(void);
126 static void serial_detach(dev_link_t *);
128 static dev_link_t *dev_list = NULL;
130 /*======================================================================
132 After a card is removed, serial_remove() will unregister
133 the serial device(s), and release the PCMCIA configuration.
135 ======================================================================*/
137 static void serial_remove(dev_link_t *link)
139 struct serial_info *info = link->priv;
142 link->state &= ~DEV_PRESENT;
144 DEBUG(0, "serial_release(0x%p)\n", link);
147 * Recheck to see if the device is still configured.
149 if (info->link.state & DEV_CONFIG) {
150 for (i = 0; i < info->ndev; i++)
151 unregister_serial(info->line[i]);
153 info->link.dev = NULL;
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);
161 info->link.state &= ~DEV_CONFIG;
165 static void serial_suspend(dev_link_t *link)
167 link->state |= DEV_SUSPEND;
169 if (link->state & DEV_CONFIG) {
170 struct serial_info *info = link->priv;
173 for (i = 0; i < info->ndev; i++)
174 serial8250_suspend_port(info->line[i]);
177 pcmcia_release_configuration(link->handle);
181 static void serial_resume(dev_link_t *link)
183 link->state &= ~DEV_SUSPEND;
186 struct serial_info *info = link->priv;
190 pcmcia_request_configuration(link->handle, &link->conf);
192 for (i = 0; i < info->ndev; i++)
193 serial8250_resume_port(info->line[i]);
197 /*======================================================================
199 serial_attach() creates an "instance" of the driver, allocating
200 local data structures for one device. The device is registered
203 ======================================================================*/
205 static dev_link_t *serial_attach(void)
207 struct serial_info *info;
208 client_reg_t client_reg;
212 DEBUG(0, "serial_attach()\n");
214 /* Create new serial device */
215 info = kmalloc(sizeof (*info), GFP_KERNEL);
218 memset(info, 0, sizeof (*info));
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;
229 for (i = 0; i < irq_list_count; i++)
230 link->irq.IRQInfo2 |= 1 << irq_list[i];
231 link->conf.Attributes = CONF_ENABLE_IRQ;
233 link->conf.Attributes |= CONF_ENABLE_SPKR;
234 link->conf.Status = CCSR_AUDIO_ENA;
236 link->conf.IntType = INT_MEMORY_AND_IO;
238 /* Register with Card Services */
239 link->next = dev_list;
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);
260 /*======================================================================
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.
267 ======================================================================*/
269 static void serial_detach(dev_link_t * link)
271 struct serial_info *info = link->priv;
275 DEBUG(0, "serial_detach(0x%p)\n", link);
277 /* Locate device structure */
278 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
285 * Ensure any outstanding scheduled tasks are completed.
287 flush_scheduled_work();
290 * Ensure that the ports have been released.
295 ret = pcmcia_deregister_client(link->handle);
296 if (ret != CS_SUCCESS)
297 cs_error(link->handle, DeregisterClient, ret);
300 /* Unlink device structure, free bits */
305 /*====================================================================*/
307 static int setup_serial(struct serial_info * info, ioaddr_t port, int irq)
309 struct serial_struct serial;
312 memset(&serial, 0, sizeof (serial));
315 serial.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ;
317 serial.flags |= UPF_BUGGY_UART;
318 line = register_serial(&serial);
320 printk(KERN_NOTICE "serial_cs: register_serial() at 0x%04lx,"
321 " irq %d failed\n", (u_long) serial.port, serial.irq);
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;
330 info->node[info->ndev - 1].next = &info->node[info->ndev];
336 /*====================================================================*/
339 first_tuple(client_handle_t handle, tuple_t * tuple, cisparse_t * parse)
342 i = pcmcia_get_first_tuple(handle, tuple);
344 return CS_NO_MORE_ITEMS;
345 i = pcmcia_get_tuple_data(handle, tuple);
348 return pcmcia_parse_tuple(handle, tuple, parse);
352 next_tuple(client_handle_t handle, tuple_t * tuple, cisparse_t * parse)
355 i = pcmcia_get_next_tuple(handle, tuple);
357 return CS_NO_MORE_ITEMS;
358 i = pcmcia_get_tuple_data(handle, tuple);
361 return pcmcia_parse_tuple(handle, tuple, parse);
364 /*====================================================================*/
366 static int simple_config(dev_link_t * link)
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;
374 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
375 config_info_t config;
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)) {
382 if ((config.BasePort2 != 0) && (config.NumPorts2 == 8)) {
383 port = config.BasePort2;
385 } else if ((info->manfid == MANFID_OSITECH) &&
386 (config.NumPorts1 == 0x40)) {
387 port = config.BasePort1 + 0x28;
391 return setup_serial(info, port, config.AssignedIRQ);
393 link->conf.Vcc = config.Vcc;
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) {
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);
421 i = next_tuple(handle, &tuple, &parse);
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);
441 i = next_tuple(handle, &tuple, &parse);
445 if (i != CS_SUCCESS) {
447 "serial_cs: no usable port range found, giving up\n");
448 cs_error(link->handle, RequestIO, i);
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;
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);
465 return setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
468 static int multi_config(dev_link_t * link)
470 client_handle_t handle = link->handle;
471 struct serial_info *info = link->priv;
475 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
476 config_info_t config;
479 i = pcmcia_get_configuration_info(handle, &config);
480 if (i != CS_SUCCESS) {
481 cs_error(handle, GetConfigurationInfo, i);
484 link->conf.Vcc = config.Vcc;
486 tuple.TupleData = (cisdata_t *) buf;
487 tuple.TupleOffset = 0;
488 tuple.TupleDataMax = 255;
489 tuple.Attributes = 0;
490 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
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;
509 i = next_tuple(handle, &tuple, &parse);
512 /* If that didn't work, look for two windows */
513 if (i != CS_SUCCESS) {
514 link->io.NumPorts1 = link->io.NumPorts2 = 8;
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;
529 i = next_tuple(handle, &tuple, &parse);
533 if (i != CS_SUCCESS) {
534 cs_error(link->handle, RequestIO, i);
538 i = pcmcia_request_irq(link->handle, &link->irq);
539 if (i != CS_SUCCESS) {
541 "serial_cs: no usable port range found, giving up\n");
542 cs_error(link->handle, RequestIRQ, i);
543 link->irq.AssignedIRQ = 0;
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;
550 i = pcmcia_request_configuration(link->handle, &link->conf);
551 if (i != CS_SUCCESS) {
552 cs_error(link->handle, RequestConfiguration, i);
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);
563 setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
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)
573 for (i = 0; i < info->multi - 1; i++)
574 setup_serial(info, base2 + (8 * i), link->irq.AssignedIRQ);
579 /*======================================================================
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.
585 ======================================================================*/
587 void serial_config(dev_link_t * link)
589 client_handle_t handle = link->handle;
590 struct serial_info *info = link->priv;
594 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
595 int i, last_ret, last_fn;
597 DEBUG(0, "serial_config(0x%p)\n", link);
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;
610 link->conf.ConfigBase = parse.config.base;
611 link->conf.Present = parse.config.rmask[0];
614 link->state |= DEV_CONFIG;
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);
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))
630 info->multi = multi_id[i].multi;
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))
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, ®);
662 last_fn = AccessConfigurationRegister;
665 reg.Action = CS_WRITE;
666 reg.Value = reg.Value | 1;
667 last_ret = pcmcia_access_configuration_register(link->handle, ®);
669 last_fn = AccessConfigurationRegister;
674 link->dev = &info->node[0];
675 link->state &= ~DEV_CONFIG_PENDING;
679 cs_error(link->handle, last_fn, last_ret);
682 link->state &= ~DEV_CONFIG_PENDING;
685 /*======================================================================
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.
692 ======================================================================*/
695 serial_event(event_t event, int priority, event_callback_args_t * args)
697 dev_link_t *link = args->client_data;
698 struct serial_info *info = link->priv;
700 DEBUG(1, "serial_event(0x%06x)\n", event);
703 case CS_EVENT_CARD_REMOVAL:
707 case CS_EVENT_CARD_INSERTION:
708 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
712 case CS_EVENT_PM_SUSPEND:
713 serial_suspend(link);
716 case CS_EVENT_RESET_PHYSICAL:
717 if ((link->state & DEV_CONFIG) && !info->slave)
718 pcmcia_release_configuration(link->handle);
721 case CS_EVENT_PM_RESUME:
725 case CS_EVENT_CARD_RESET:
726 if (DEV_OK(link) && !info->slave)
727 pcmcia_request_configuration(link->handle, &link->conf);
733 static struct pcmcia_driver serial_cs_driver = {
734 .owner = THIS_MODULE,
738 .attach = serial_attach,
739 .detach = serial_detach,
742 static int __init init_serial_cs(void)
744 return pcmcia_register_driver(&serial_cs_driver);
747 static void __exit exit_serial_cs(void)
749 pcmcia_unregister_driver(&serial_cs_driver);
751 /* XXX: this really needs to move into generic code.. */
752 while (dev_list != NULL)
753 serial_detach(dev_list);
756 module_init(init_serial_cs);
757 module_exit(exit_serial_cs);
759 MODULE_LICENSE("GPL");