1 /*======================================================================
3 A driver for PCMCIA serial devices
5 serial_cs.c 1.123 2000/08/24 18:46:38
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/kernel.h>
36 #include <linux/init.h>
37 #include <linux/sched.h>
38 #include <linux/ptrace.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/timer.h>
42 #include <linux/tty.h>
43 #include <linux/serial.h>
44 #include <linux/serial_core.h>
45 #include <linux/major.h>
46 #include <linux/workqueue.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>
59 static int pc_debug
= PCMCIA_DEBUG
;
60 MODULE_PARM(pc_debug
, "i");
61 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
62 static char *version
= "serial_cs.c 1.123 2000/08/24 18:46:38 (David Hinds)";
64 #define DEBUG(n, args...)
67 /*====================================================================*/
69 /* Parameters that can be set with 'insmod' */
71 /* Bit map of interrupts to choose from */
72 static u_int irq_mask
= 0xdeb8;
73 static int irq_list
[4] = { -1 };
75 /* Enable the speaker? */
76 static int do_sound
= 1;
78 MODULE_PARM(irq_mask
, "i");
79 MODULE_PARM(irq_list
, "1-4i");
80 MODULE_PARM(do_sound
, "i");
82 /*====================================================================*/
84 /* Table of multi-port card ID's */
89 int multi
; /* 1 = multifunction, > 1 = # ports */
92 static struct multi_id multi_id
[] = {
93 { MANFID_OMEGA
, PRODID_OMEGA_QSP_100
, 4 },
94 { MANFID_QUATECH
, PRODID_QUATECH_DUAL_RS232
, 2 },
95 { MANFID_QUATECH
, PRODID_QUATECH_DUAL_RS232_D1
, 2 },
96 { MANFID_QUATECH
, PRODID_QUATECH_QUAD_RS232
, 4 },
97 { MANFID_SOCKET
, PRODID_SOCKET_DUAL_RS232
, 2 },
98 { MANFID_INTEL
, PRODID_INTEL_DUAL_RS232
, 2 },
99 { MANFID_NATINST
, PRODID_NATINST_QUAD_RS232
, 4 }
101 #define MULTI_COUNT (sizeof(multi_id)/sizeof(struct multi_id))
103 typedef struct serial_info
{
111 struct work_struct remove
;
114 static void serial_config(dev_link_t
* link
);
115 static int serial_event(event_t event
, int priority
,
116 event_callback_args_t
* args
);
118 static dev_info_t dev_info
= "serial_cs";
120 static dev_link_t
*serial_attach(void);
121 static void serial_detach(dev_link_t
*);
123 static dev_link_t
*dev_list
= NULL
;
125 /*======================================================================
127 After a card is removed, do_serial_release() will unregister
128 the serial device(s), and release the PCMCIA configuration.
130 ======================================================================*/
133 * This always runs in process context.
135 static void do_serial_release(void *arg
)
137 struct serial_info
*info
= arg
;
140 DEBUG(0, "serial_release(0x%p)\n", &info
->link
);
143 * Recheck to see if the device is still configured.
145 if (info
->link
.state
& DEV_CONFIG
) {
146 for (i
= 0; i
< info
->ndev
; i
++)
147 unregister_serial(info
->line
[i
]);
149 info
->link
.dev
= NULL
;
152 CardServices(ReleaseConfiguration
, info
->link
.handle
);
153 CardServices(ReleaseIO
, info
->link
.handle
, &info
->link
.io
);
154 CardServices(ReleaseIRQ
, info
->link
.handle
, &info
->link
.irq
);
157 info
->link
.state
&= ~DEV_CONFIG
;
162 * This may be called from IRQ context.
164 static void serial_remove(dev_link_t
*link
)
166 struct serial_info
*info
= link
->priv
;
168 link
->state
&= ~DEV_PRESENT
;
171 * FIXME: Since the card has probably been removed,
172 * we should call into the serial layer and hang up
173 * the ports on the card immediately.
176 if (link
->state
& DEV_CONFIG
)
177 schedule_work(&info
->remove
);
180 /*======================================================================
182 serial_attach() creates an "instance" of the driver, allocating
183 local data structures for one device. The device is registered
186 ======================================================================*/
188 static dev_link_t
*serial_attach(void)
191 client_reg_t client_reg
;
195 DEBUG(0, "serial_attach()\n");
197 /* Create new serial device */
198 info
= kmalloc(sizeof (*info
), GFP_KERNEL
);
201 memset(info
, 0, sizeof (*info
));
205 INIT_WORK(&info
->remove
, do_serial_release
, info
);
207 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_8
;
208 link
->io
.NumPorts1
= 8;
209 link
->irq
.Attributes
= IRQ_TYPE_EXCLUSIVE
;
210 link
->irq
.IRQInfo1
= IRQ_INFO2_VALID
| IRQ_LEVEL_ID
;
211 if (irq_list
[0] == -1)
212 link
->irq
.IRQInfo2
= irq_mask
;
214 for (i
= 0; i
< 4; i
++)
215 link
->irq
.IRQInfo2
|= 1 << irq_list
[i
];
216 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
.Attributes
= INFO_IO_CLIENT
| INFO_CARD_SHARE
;
229 client_reg
.EventMask
=
230 CS_EVENT_CARD_INSERTION
| CS_EVENT_CARD_REMOVAL
|
231 CS_EVENT_RESET_PHYSICAL
| CS_EVENT_CARD_RESET
|
232 CS_EVENT_PM_SUSPEND
| CS_EVENT_PM_RESUME
;
233 client_reg
.event_handler
= &serial_event
;
234 client_reg
.Version
= 0x0210;
235 client_reg
.event_callback_args
.client_data
= link
;
236 ret
= CardServices(RegisterClient
, &link
->handle
, &client_reg
);
237 if (ret
!= CS_SUCCESS
) {
238 cs_error(link
->handle
, RegisterClient
, ret
);
246 /*======================================================================
248 This deletes a driver "instance". The device is de-registered
249 with Card Services. If it has been released, all local data
250 structures are freed. Otherwise, the structures will be freed
251 when the device is released.
253 ======================================================================*/
255 static void serial_detach(dev_link_t
* link
)
257 serial_info_t
*info
= link
->priv
;
261 DEBUG(0, "serial_detach(0x%p)\n", link
);
263 /* Locate device structure */
264 for (linkp
= &dev_list
; *linkp
; linkp
= &(*linkp
)->next
)
271 * Ensure any outstanding scheduled tasks are completed.
273 flush_scheduled_work();
276 * Ensure that the ports have been released.
278 do_serial_release(info
);
281 ret
= CardServices(DeregisterClient
, link
->handle
);
282 if (ret
!= CS_SUCCESS
)
283 cs_error(link
->handle
, DeregisterClient
, ret
);
286 /* Unlink device structure, free bits */
291 /*====================================================================*/
293 static int setup_serial(serial_info_t
* info
, ioaddr_t port
, int irq
)
295 struct serial_struct serial
;
298 memset(&serial
, 0, sizeof (serial
));
301 serial
.flags
= UPF_SKIP_TEST
| UPF_SHARE_IRQ
;
302 line
= register_serial(&serial
);
304 printk(KERN_NOTICE
"serial_cs: register_serial() at 0x%04lx,"
305 " irq %d failed\n", (u_long
) serial
.port
, serial
.irq
);
309 info
->line
[info
->ndev
] = line
;
310 sprintf(info
->node
[info
->ndev
].dev_name
, "ttyS%d", line
);
311 info
->node
[info
->ndev
].major
= TTY_MAJOR
;
312 info
->node
[info
->ndev
].minor
= 0x40 + line
;
314 info
->node
[info
->ndev
- 1].next
= &info
->node
[info
->ndev
];
320 /*====================================================================*/
323 get_tuple(int fn
, client_handle_t handle
, tuple_t
* tuple
, cisparse_t
* parse
)
326 i
= CardServices(fn
, handle
, tuple
);
328 return CS_NO_MORE_ITEMS
;
329 i
= CardServices(GetTupleData
, handle
, tuple
);
332 return CardServices(ParseTuple
, handle
, tuple
, parse
);
335 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
336 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
338 /*====================================================================*/
340 static int simple_config(dev_link_t
* link
)
342 static ioaddr_t base
[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
343 client_handle_t handle
= link
->handle
;
344 serial_info_t
*info
= link
->priv
;
348 cistpl_cftable_entry_t
*cf
= &parse
.cftable_entry
;
349 config_info_t config
;
352 /* If the card is already configured, look up the port and irq */
353 i
= CardServices(GetConfigurationInfo
, handle
, &config
);
354 if ((i
== CS_SUCCESS
) && (config
.Attributes
& CONF_VALID_CLIENT
)) {
356 if ((config
.BasePort2
!= 0) && (config
.NumPorts2
== 8)) {
357 port
= config
.BasePort2
;
359 } else if ((info
->manfid
== MANFID_OSITECH
) &&
360 (config
.NumPorts1
== 0x40)) {
361 port
= config
.BasePort1
+ 0x28;
365 return setup_serial(info
, port
, config
.AssignedIRQ
);
367 link
->conf
.Vcc
= config
.Vcc
;
369 /* First pass: look for a config entry that looks normal. */
370 tuple
.TupleData
= (cisdata_t
*) buf
;
371 tuple
.TupleOffset
= 0;
372 tuple
.TupleDataMax
= 255;
373 tuple
.Attributes
= 0;
374 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
375 /* Two tries: without IO aliases, then with aliases */
376 for (try = 0; try < 2; try++) {
377 i
= first_tuple(handle
, &tuple
, &parse
);
378 while (i
!= CS_NO_MORE_ITEMS
) {
381 if (cf
->vpp1
.present
& (1 << CISTPL_POWER_VNOM
))
382 link
->conf
.Vpp1
= link
->conf
.Vpp2
=
383 cf
->vpp1
.param
[CISTPL_POWER_VNOM
] / 10000;
384 if ((cf
->io
.nwin
> 0) && (cf
->io
.win
[0].len
== 8) &&
385 (cf
->io
.win
[0].base
!= 0)) {
386 link
->conf
.ConfigIndex
= cf
->index
;
387 link
->io
.BasePort1
= cf
->io
.win
[0].base
;
388 link
->io
.IOAddrLines
= (try == 0) ?
389 16 : cf
->io
.flags
& CISTPL_IO_LINES_MASK
;
391 CardServices(RequestIO
, link
->handle
,
397 i
= next_tuple(handle
, &tuple
, &parse
);
401 /* Second pass: try to find an entry that isn't picky about
402 its base address, then try to grab any standard serial port
403 address, and finally try to get any free port. */
404 i
= first_tuple(handle
, &tuple
, &parse
);
405 while (i
!= CS_NO_MORE_ITEMS
) {
406 if ((i
== CS_SUCCESS
) && (cf
->io
.nwin
> 0) &&
407 ((cf
->io
.flags
& CISTPL_IO_LINES_MASK
) <= 3)) {
408 link
->conf
.ConfigIndex
= cf
->index
;
409 for (j
= 0; j
< 5; j
++) {
410 link
->io
.BasePort1
= base
[j
];
411 link
->io
.IOAddrLines
= base
[j
] ? 16 : 3;
412 i
= CardServices(RequestIO
, link
->handle
,
418 i
= next_tuple(handle
, &tuple
, &parse
);
422 if (i
!= CS_SUCCESS
) {
424 "serial_cs: no usable port range found, giving up\n");
425 cs_error(link
->handle
, RequestIO
, i
);
429 i
= CardServices(RequestIRQ
, link
->handle
, &link
->irq
);
430 if (i
!= CS_SUCCESS
) {
431 cs_error(link
->handle
, RequestIRQ
, i
);
432 link
->irq
.AssignedIRQ
= 0;
434 if (info
->multi
&& (info
->manfid
== MANFID_3COM
))
435 link
->conf
.ConfigIndex
&= ~(0x08);
436 i
= CardServices(RequestConfiguration
, link
->handle
, &link
->conf
);
437 if (i
!= CS_SUCCESS
) {
438 cs_error(link
->handle
, RequestConfiguration
, i
);
442 return setup_serial(info
, link
->io
.BasePort1
, link
->irq
.AssignedIRQ
);
445 static int multi_config(dev_link_t
* link
)
447 client_handle_t handle
= link
->handle
;
448 serial_info_t
*info
= link
->priv
;
452 cistpl_cftable_entry_t
*cf
= &parse
.cftable_entry
;
455 tuple
.TupleData
= (cisdata_t
*) buf
;
456 tuple
.TupleOffset
= 0;
457 tuple
.TupleDataMax
= 255;
458 tuple
.Attributes
= 0;
459 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
461 /* First, look for a generic full-sized window */
462 link
->io
.NumPorts1
= info
->multi
* 8;
463 i
= first_tuple(handle
, &tuple
, &parse
);
464 while (i
!= CS_NO_MORE_ITEMS
) {
465 /* The quad port cards have bad CIS's, so just look for a
466 window larger than 8 ports and assume it will be right */
467 if ((i
== CS_SUCCESS
) && (cf
->io
.nwin
== 1) &&
468 (cf
->io
.win
[0].len
> 8)) {
469 link
->conf
.ConfigIndex
= cf
->index
;
470 link
->io
.BasePort1
= cf
->io
.win
[0].base
;
471 link
->io
.IOAddrLines
=
472 cf
->io
.flags
& CISTPL_IO_LINES_MASK
;
473 i
= CardServices(RequestIO
, link
->handle
, &link
->io
);
474 base2
= link
->io
.BasePort1
+ 8;
478 i
= next_tuple(handle
, &tuple
, &parse
);
481 /* If that didn't work, look for two windows */
482 if (i
!= CS_SUCCESS
) {
483 link
->io
.NumPorts1
= link
->io
.NumPorts2
= 8;
485 i
= first_tuple(handle
, &tuple
, &parse
);
486 while (i
!= CS_NO_MORE_ITEMS
) {
487 if ((i
== CS_SUCCESS
) && (cf
->io
.nwin
== 2)) {
488 link
->conf
.ConfigIndex
= cf
->index
;
489 link
->io
.BasePort1
= cf
->io
.win
[0].base
;
490 link
->io
.BasePort2
= cf
->io
.win
[1].base
;
491 link
->io
.IOAddrLines
=
492 cf
->io
.flags
& CISTPL_IO_LINES_MASK
;
494 CardServices(RequestIO
, link
->handle
,
496 base2
= link
->io
.BasePort2
;
500 i
= next_tuple(handle
, &tuple
, &parse
);
504 if (i
!= CS_SUCCESS
) {
505 cs_error(link
->handle
, RequestIO
, i
);
509 i
= CardServices(RequestIRQ
, link
->handle
, &link
->irq
);
510 if (i
!= CS_SUCCESS
) {
512 "serial_cs: no usable port range found, giving up\n");
513 cs_error(link
->handle
, RequestIRQ
, i
);
514 link
->irq
.AssignedIRQ
= 0;
516 /* Socket Dual IO: this enables irq's for second port */
517 if (info
->multi
&& (info
->manfid
== MANFID_SOCKET
)) {
518 link
->conf
.Present
|= PRESENT_EXT_STATUS
;
519 link
->conf
.ExtStatus
= ESR_REQ_ATTN_ENA
;
521 i
= CardServices(RequestConfiguration
, link
->handle
, &link
->conf
);
522 if (i
!= CS_SUCCESS
) {
523 cs_error(link
->handle
, RequestConfiguration
, i
);
527 setup_serial(info
, link
->io
.BasePort1
, link
->irq
.AssignedIRQ
);
528 /* The Nokia cards are not really multiport cards */
529 if (info
->manfid
== MANFID_NOKIA
)
531 for (i
= 0; i
< info
->multi
- 1; i
++)
532 setup_serial(info
, base2
+ (8 * i
), link
->irq
.AssignedIRQ
);
537 /*======================================================================
539 serial_config() is scheduled to run after a CARD_INSERTION event
540 is received, to configure the PCMCIA socket, and to make the
541 serial device available to the system.
543 ======================================================================*/
545 #define CS_CHECK(fn, args...) \
546 while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
548 void serial_config(dev_link_t
* link
)
550 client_handle_t handle
= link
->handle
;
551 serial_info_t
*info
= link
->priv
;
555 cistpl_cftable_entry_t
*cf
= &parse
.cftable_entry
;
556 int i
, last_ret
, last_fn
;
558 DEBUG(0, "serial_config(0x%p)\n", link
);
560 tuple
.TupleData
= (cisdata_t
*) buf
;
561 tuple
.TupleOffset
= 0;
562 tuple
.TupleDataMax
= 255;
563 tuple
.Attributes
= 0;
564 /* Get configuration register information */
565 tuple
.DesiredTuple
= CISTPL_CONFIG
;
566 last_ret
= first_tuple(handle
, &tuple
, &parse
);
567 if (last_ret
!= CS_SUCCESS
) {
568 last_fn
= ParseTuple
;
571 link
->conf
.ConfigBase
= parse
.config
.base
;
572 link
->conf
.Present
= parse
.config
.rmask
[0];
575 link
->state
|= DEV_CONFIG
;
577 /* Is this a compliant multifunction card? */
578 tuple
.DesiredTuple
= CISTPL_LONGLINK_MFC
;
579 tuple
.Attributes
= TUPLE_RETURN_COMMON
| TUPLE_RETURN_LINK
;
580 info
->multi
= (first_tuple(handle
, &tuple
, &parse
) == CS_SUCCESS
);
582 /* Is this a multiport card? */
583 tuple
.DesiredTuple
= CISTPL_MANFID
;
584 if (first_tuple(handle
, &tuple
, &parse
) == CS_SUCCESS
) {
585 info
->manfid
= le16_to_cpu(buf
[0]);
586 for (i
= 0; i
< MULTI_COUNT
; i
++)
587 if ((info
->manfid
== multi_id
[i
].manfid
) &&
588 (le16_to_cpu(buf
[1]) == multi_id
[i
].prodid
))
591 info
->multi
= multi_id
[i
].multi
;
594 /* Another check for dual-serial cards: look for either serial or
595 multifunction cards that ask for appropriate IO port ranges */
596 tuple
.DesiredTuple
= CISTPL_FUNCID
;
597 if ((info
->multi
== 0) &&
598 ((first_tuple(handle
, &tuple
, &parse
) != CS_SUCCESS
) ||
599 (parse
.funcid
.func
== CISTPL_FUNCID_MULTI
) ||
600 (parse
.funcid
.func
== CISTPL_FUNCID_SERIAL
))) {
601 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
602 if (first_tuple(handle
, &tuple
, &parse
) == CS_SUCCESS
) {
603 if ((cf
->io
.nwin
== 1) && (cf
->io
.win
[0].len
% 8 == 0))
604 info
->multi
= cf
->io
.win
[0].len
>> 3;
605 if ((cf
->io
.nwin
== 2) && (cf
->io
.win
[0].len
== 8) &&
606 (cf
->io
.win
[1].len
== 8))
619 if (info
->manfid
== MANFID_IBM
) {
620 conf_reg_t reg
= { 0, CS_READ
, 0x800, 0 };
621 CS_CHECK(AccessConfigurationRegister
, link
->handle
, ®
);
622 reg
.Action
= CS_WRITE
;
623 reg
.Value
= reg
.Value
| 1;
624 CS_CHECK(AccessConfigurationRegister
, link
->handle
, ®
);
627 link
->dev
= &info
->node
[0];
628 link
->state
&= ~DEV_CONFIG_PENDING
;
632 cs_error(link
->handle
, last_fn
, last_ret
);
634 do_serial_release(info
);
637 /*======================================================================
639 The card status event handler. Mostly, this schedules other
640 stuff to run after an event is received. A CARD_REMOVAL event
641 also sets some flags to discourage the serial drivers from
642 talking to the ports.
644 ======================================================================*/
647 serial_event(event_t event
, int priority
, event_callback_args_t
* args
)
649 dev_link_t
*link
= args
->client_data
;
650 serial_info_t
*info
= link
->priv
;
652 DEBUG(1, "serial_event(0x%06x)\n", event
);
655 case CS_EVENT_CARD_REMOVAL
:
659 case CS_EVENT_CARD_INSERTION
:
660 link
->state
|= DEV_PRESENT
| DEV_CONFIG_PENDING
;
664 case CS_EVENT_PM_SUSPEND
:
665 link
->state
|= DEV_SUSPEND
;
666 /* Fall through... */
667 case CS_EVENT_RESET_PHYSICAL
:
668 if ((link
->state
& DEV_CONFIG
) && !info
->slave
)
669 CardServices(ReleaseConfiguration
, link
->handle
);
672 case CS_EVENT_PM_RESUME
:
673 link
->state
&= ~DEV_SUSPEND
;
674 /* Fall through... */
675 case CS_EVENT_CARD_RESET
:
676 if (DEV_OK(link
) && !info
->slave
)
677 CardServices(RequestConfiguration
, link
->handle
,
684 static struct pcmcia_driver serial_cs_driver
= {
685 .owner
= THIS_MODULE
,
689 .attach
= serial_attach
,
690 .detach
= serial_detach
,
693 static int __init
init_serial_cs(void)
695 return pcmcia_register_driver(&serial_cs_driver
);
698 static void __exit
exit_serial_cs(void)
700 pcmcia_unregister_driver(&serial_cs_driver
);
702 /* XXX: this really needs to move into generic code.. */
703 while (dev_list
!= NULL
)
704 serial_detach(dev_list
);
707 module_init(init_serial_cs
);
708 module_exit(exit_serial_cs
);
710 MODULE_LICENSE("GPL");