2 comedi/drivers/das08_cs.c
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 Copyright (C) 2001,2002,2003 Frank Mori Hess <fmhess@users.sourceforge.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *****************************************************************
28 Description: DAS-08 PCMCIA boards
29 Author: Warren Jasper, ds, Frank Hess
30 Devices: [ComputerBoards] PCM-DAS08 (pcm-das08)
33 This is the PCMCIA-specific support split off from the
36 Options (for pcm-das08):
39 Command support does not exist, but could be added for this board.
42 #include <linux/comedidev.h>
44 #include <linux/delay.h>
45 #include <linux/pci.h>
46 #include <linux/version.h>
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/ds.h>
56 A linked list of "instances" of the das08_pcmcia device. Each actual
57 PCMCIA card corresponds to one device instance, and is described
58 by one dev_link_t structure (defined in ds.h).
60 You may not want to use a linked list for this -- for example, the
61 memory card driver uses an array of dev_link_t pointers, where minor
62 device numbers are used to derive the corresponding array index.
64 static dev_link_t *dev_list = NULL;
66 #define thisboard ((struct das08_board_struct *)dev->board_ptr)
68 static int das08_cs_attach(comedi_device *dev,comedi_devconfig *it);
70 static comedi_driver driver_das08_cs =
72 driver_name: "das08_cs",
74 attach: das08_cs_attach,
75 detach: das08_common_detach,
76 board_name: &das08_cs_boards[0].name,
77 num_names: sizeof(das08_cs_boards) / sizeof(struct das08_board_struct),
78 offset: sizeof(struct das08_board_struct),
81 static int das08_cs_attach(comedi_device *dev,comedi_devconfig *it)
85 dev_link_t *link = dev_list; // XXX hack
87 if((ret=alloc_private(dev,sizeof(struct das08_private_struct)))<0)
90 printk("comedi%d: das08_cs: ", dev->minor);
91 // deal with a pci board
93 if(thisboard->bustype == pcmcia)
97 printk(" no pcmcia cards found\n");
100 iobase = link->io.BasePort1;
102 printk(" bug! board does not have PCMCIA bustype\n");
108 return das08_common_attach( dev, iobase );
111 /*======================================================================
113 The following pcmcia code for the pcm-das08 is adapted from the
114 dummy_cs.c driver of the Linux PCMCIA Card Services package.
116 The initial developer of the original code is David A. Hinds
117 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
118 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
120 ======================================================================*/
123 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
124 you do not define PCMCIA_DEBUG at all, all the debug code will be
125 left out. If you compile with PCMCIA_DEBUG=0, the debug code will
126 be present but disabled -- but it can then be enabled for specific
127 modules at load time with a 'pc_debug=#' option to insmod.
131 static int pc_debug = PCMCIA_DEBUG;
132 module_param(pc_debug, int, 0644);
133 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
134 static char *version =
135 "das08.c pcmcia code (Frank Hess), modified from dummy_cs.c 1.31 2001/08/24 12:13:13 (David Hinds)";
137 #define DEBUG(n, args...)
140 /*====================================================================*/
141 static void das08_pcmcia_config(dev_link_t *link);
142 static void das08_pcmcia_release(u_long arg);
143 static int das08_pcmcia_suspend(struct pcmcia_device *p_dev);
144 static int das08_pcmcia_resume(struct pcmcia_device *p_dev);
147 The attach() and detach() entry points are used to create and destroy
148 "instances" of the driver, where each instance represents everything
149 needed to manage one actual PCMCIA card.
152 static int das08_pcmcia_attach(struct pcmcia_device *);
153 static void das08_pcmcia_detach(struct pcmcia_device *);
156 You'll also need to prototype all the functions that will actually
157 be used to talk to your device. See 'memory_cs' for a good example
158 of a fully self-sufficient driver; the other drivers rely more or
159 less on other parts of the kernel.
163 The dev_info variable is the "key" that is used to match up this
164 device driver with appropriate cards, through the card configuration
168 static dev_info_t dev_info = "pcm-das08";
171 A dev_link_t structure has fields for most things that are needed
172 to keep track of a socket, but there will usually be some device
173 specific information that also needs to be kept track of. The
174 'priv' pointer in a dev_link_t structure can be used to point to
175 a device-specific private data structure, like this.
177 To simplify the data structure handling, we actually include the
178 dev_link_t structure in the device's private data structure.
180 A driver needs to provide a dev_node_t structure for each device
181 on a card. In some cases, there is only one device per card (for
182 example, ethernet cards, modems). In other cases, there may be
183 many actual or logical devices (SCSI adapters, memory cards with
184 multiple partitions). The dev_node_t structures need to be kept
185 in a linked list starting at the 'dev' field of a dev_link_t
186 structure. We allocate them in the card's private data structure,
187 because they generally shouldn't be allocated dynamically.
189 In this case, we also provide a flag to indicate if a device is
190 "stopped" due to a power management event, or card ejection. The
191 device IO routines can use a flag like this to throttle IO to a
192 card that is not ready to accept it.
194 The bus_operations pointer is used on platforms for which we need
195 to use special socket-specific versions of normal IO primitives
196 (inb, outb, readb, writeb, etc) for card IO.
199 typedef struct local_info_t {
203 struct bus_operations *bus;
206 /*======================================================================
208 das08_pcmcia_attach() creates an "instance" of the driver, allocating
209 local data structures for one device. The device is registered
212 The dev_link structure is initialized, but we don't actually
213 configure the card at this point -- we wait until we receive a
214 card insertion event.
216 ======================================================================*/
218 static int das08_pcmcia_attach(struct pcmcia_device *p_dev)
223 DEBUG(0, "das08_pcmcia_attach()\n");
225 /* Allocate space for private device-specific data */
226 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
227 if (!local) return -ENOMEM;
228 memset(local, 0, sizeof(local_info_t));
229 link = &local->link; link->priv = local;
231 /* Interrupt setup */
232 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
233 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
234 link->irq.Handler = NULL;
237 General socket configuration defaults can go here. In this
238 client, we assume very little, and rely on the CIS for almost
239 everything. In most clients, many details (i.e., number, sizes,
240 and attributes of IO windows) are fixed by the nature of the
241 device, and can be hard-wired here.
243 link->conf.Attributes = 0;
245 link->conf.IntType = INT_MEMORY_AND_IO;
247 link->next = dev_list;
250 link->handle = p_dev;
251 p_dev->instance = link;
252 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
253 das08_pcmcia_config(link);
256 } /* das08_pcmcia_attach */
258 /*======================================================================
260 This deletes a driver "instance". The device is de-registered
261 with Card Services. If it has been released, all local data
262 structures are freed. Otherwise, the structures will be freed
263 when the device is released.
265 ======================================================================*/
267 static void das08_pcmcia_detach(struct pcmcia_device *p_dev)
269 dev_link_t *link = dev_to_instance(p_dev);
272 DEBUG(0, "das08_pcmcia_detach(0x%p)\n", link);
274 /* Locate device structure */
275 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
276 if (*linkp == link) break;
281 If the device is currently configured and active, we won't
282 actually delete it yet. Instead, it is marked so that when
283 the release() function is called, that will trigger a proper
286 if (link->state & DEV_CONFIG)
288 ((local_info_t *)link->priv)->stop = 1;
289 das08_pcmcia_release((u_long)link);
292 /* Unlink device structure, and free it */
294 /* This points to the parent local_info_t struct */
297 } /* das08_pcmcia_detach */
299 /*======================================================================
301 das08_pcmcia_config() is scheduled to run after a CARD_INSERTION event
302 is received, to configure the PCMCIA socket, and to make the
303 device available to the system.
305 ======================================================================*/
307 static void das08_pcmcia_config(dev_link_t *link)
309 client_handle_t handle = link->handle;
310 local_info_t *dev = link->priv;
313 int last_fn, last_ret;
316 cistpl_cftable_entry_t dflt = { 0 };
318 DEBUG(0, "das08_pcmcia_config(0x%p)\n", link);
321 This reads the card's CONFIG tuple to find its configuration
324 tuple.DesiredTuple = CISTPL_CONFIG;
325 tuple.Attributes = 0;
326 tuple.TupleData = buf;
327 tuple.TupleDataMax = sizeof(buf);
328 tuple.TupleOffset = 0;
329 last_fn = GetFirstTuple;
330 if((last_ret = pcmcia_get_first_tuple(handle, &tuple)) != 0) goto cs_failed;
331 last_fn = GetTupleData;
332 if((last_ret = pcmcia_get_tuple_data(handle, &tuple)) != 0) goto cs_failed;
333 last_fn = ParseTuple;
334 if((last_ret = pcmcia_parse_tuple(handle, &tuple, &parse)) != 0) goto cs_failed;
335 link->conf.ConfigBase = parse.config.base;
336 link->conf.Present = parse.config.rmask[0];
339 link->state |= DEV_CONFIG;
341 /* Look up the current Vcc */
342 last_fn = GetConfigurationInfo;
343 if((last_ret = pcmcia_get_configuration_info(handle, &conf)) != 0) goto cs_failed;
344 link->conf.Vcc = conf.Vcc;
347 In this loop, we scan the CIS for configuration table entries,
348 each of which describes a valid card configuration, including
349 voltage, IO window, memory window, and interrupt settings.
351 We make no assumptions about the card to be configured: we use
352 just the information available in the CIS. In an ideal world,
353 this would work for any PCMCIA card, but it requires a complete
354 and accurate CIS. In practice, a driver usually "knows" most of
355 these things without consulting the CIS, and most client drivers
356 will only use the CIS to fill in implementation-defined details.
358 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
359 last_fn = GetFirstTuple;
360 if((last_ret = pcmcia_get_first_tuple(handle, &tuple)) != 0) goto cs_failed;
362 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
363 if((last_ret = pcmcia_get_tuple_data(handle, &tuple)) != 0) goto next_entry;
364 if((last_ret = pcmcia_parse_tuple(handle, &tuple, &parse)) != 0) goto next_entry;
366 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
367 if (cfg->index == 0) goto next_entry;
368 link->conf.ConfigIndex = cfg->index;
370 /* Does this card need audio output? */
371 /* if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
372 link->conf.Attributes |= CONF_ENABLE_SPKR;
373 link->conf.Status = CCSR_AUDIO_ENA;
376 /* Use power settings for Vcc and Vpp if present */
377 /* Note that the CIS values need to be rescaled */
378 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
379 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM]/10000)
381 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
382 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM]/10000)
386 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
387 link->conf.Vpp1 = link->conf.Vpp2 =
388 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
389 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
390 link->conf.Vpp1 = link->conf.Vpp2 =
391 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
393 /* Do we need to allocate an interrupt? */
394 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
395 link->conf.Attributes |= CONF_ENABLE_IRQ;
397 /* IO window settings */
398 link->io.NumPorts1 = link->io.NumPorts2 = 0;
399 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
400 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
401 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
402 if (!(io->flags & CISTPL_IO_8BIT))
403 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
404 if (!(io->flags & CISTPL_IO_16BIT))
405 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
406 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
407 link->io.BasePort1 = io->win[0].base;
408 link->io.NumPorts1 = io->win[0].len;
410 link->io.Attributes2 = link->io.Attributes1;
411 link->io.BasePort2 = io->win[1].base;
412 link->io.NumPorts2 = io->win[1].len;
414 /* This reserves IO space but doesn't actually enable it */
415 if(pcmcia_request_io(link->handle, &link->io) != 0) goto next_entry;
418 /* If we got this far, we're cool! */
422 if (link->io.NumPorts1)
423 pcmcia_release_io(link->handle, &link->io);
424 last_fn = GetNextTuple;
425 if((last_ret = pcmcia_get_next_tuple(handle, &tuple)) != 0) goto cs_failed;
429 Allocate an interrupt line. Note that this does not assign a
430 handler to the interrupt, unless the 'Handler' member of the
431 irq structure is initialized.
433 if (link->conf.Attributes & CONF_ENABLE_IRQ)
435 last_fn = RequestIRQ;
436 if((last_ret = pcmcia_request_irq(handle, &link->irq)) != 0) goto cs_failed;
440 This actually configures the PCMCIA socket -- setting up
441 the I/O windows and the interrupt mapping, and putting the
442 card and host interface into "Memory and IO" mode.
444 last_fn = RequestConfiguration;
445 if((last_ret = pcmcia_request_configuration(link->handle, &link->conf)) != 0) goto cs_failed;
448 At this point, the dev_node_t structure(s) need to be
449 initialized and arranged in a linked list at link->dev.
451 sprintf(dev->node.dev_name, "pcm-das08");
452 dev->node.major = dev->node.minor = 0;
453 link->dev = &dev->node;
455 /* Finally, report what we've done */
456 printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
457 dev->node.dev_name, link->conf.ConfigIndex,
458 link->conf.Vcc/10, link->conf.Vcc%10);
460 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
461 if (link->conf.Attributes & CONF_ENABLE_IRQ)
462 printk(", irq %u", link->irq.AssignedIRQ);
463 if (link->io.NumPorts1)
464 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
465 link->io.BasePort1+link->io.NumPorts1-1);
466 if (link->io.NumPorts2)
467 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
468 link->io.BasePort2+link->io.NumPorts2-1);
471 link->state &= ~DEV_CONFIG_PENDING;
475 cs_error(link->handle, last_fn, last_ret);
476 das08_pcmcia_release((u_long)link);
478 } /* das08_pcmcia_config */
480 /*======================================================================
482 After a card is removed, das08_pcmcia_release() will unregister the
483 device, and release the PCMCIA configuration. If the device is
484 still open, this will be postponed until it is closed.
486 ======================================================================*/
488 static void das08_pcmcia_release(u_long arg)
490 dev_link_t *link = (dev_link_t *)arg;
492 DEBUG(0, "das08_pcmcia_release(0x%p)\n", link);
494 /* Unlink the device chain */
498 In a normal driver, additional code may be needed to release
499 other kernel data structures associated with this device.
502 /* Don't bother checking to see if these succeed or not */
503 pcmcia_release_configuration(link->handle);
504 if (link->io.NumPorts1)
505 pcmcia_release_io(link->handle, &link->io);
506 if (link->irq.AssignedIRQ)
507 pcmcia_release_irq(link->handle, &link->irq);
508 link->state &= ~DEV_CONFIG;
510 } /* das08_pcmcia_release */
512 /*======================================================================
514 The card status event handler. Mostly, this schedules other
515 stuff to run after an event is received.
517 When a CARD_REMOVAL event is received, we immediately set a
518 private flag to block future accesses to this device. All the
519 functions that actually access the device should check this flag
520 to make sure the card is still present.
522 ======================================================================*/
524 static int das08_pcmcia_suspend(struct pcmcia_device *p_dev)
526 dev_link_t *link = dev_to_instance(p_dev);
527 local_info_t *local = link->priv;
529 link->state |= DEV_SUSPEND;
530 /* Mark the device as stopped, to block IO until later */
532 if (link->state & DEV_CONFIG)
533 pcmcia_release_configuration(link->handle);
536 } /* das08_pcmcia_suspend */
538 static int das08_pcmcia_resume(struct pcmcia_device *p_dev)
540 dev_link_t *link = dev_to_instance(p_dev);
541 local_info_t *local = link->priv;
543 link->state &= ~DEV_SUSPEND;
544 if (link->state & DEV_CONFIG)
545 pcmcia_request_configuration(link->handle, &link->conf);
548 } /* das08_pcmcia_resume */
550 /*====================================================================*/
552 static struct pcmcia_device_id das08_cs_id_table[] =
554 PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x4001),
557 MODULE_DEVICE_TABLE(pcmcia, das08_cs_id_table);
559 struct pcmcia_driver das08_cs_driver =
561 .probe = das08_pcmcia_attach,
562 .remove = das08_pcmcia_detach,
563 .suspend = das08_pcmcia_suspend,
564 .resume = das08_pcmcia_resume,
565 .id_table = das08_cs_id_table,
566 .owner = THIS_MODULE,
572 static int __init init_das08_pcmcia_cs(void)
574 DEBUG(0, "%s\n", version);
575 pcmcia_register_driver(&das08_cs_driver);
579 static void __exit exit_das08_pcmcia_cs(void)
581 DEBUG(0, "das08_pcmcia_cs: unloading\n");
582 pcmcia_unregister_driver(&das08_cs_driver);
583 while (dev_list != NULL)
585 if (dev_list->state & DEV_CONFIG)
586 das08_pcmcia_release((u_long)dev_list);
587 das08_pcmcia_detach(dev_list->handle);
591 static int __init das08_cs_init_module(void)
595 ret = init_das08_pcmcia_cs();
599 return comedi_driver_register(&driver_das08_cs);
602 static void __exit das08_cs_exit_module(void)
604 exit_das08_pcmcia_cs();
605 comedi_driver_unregister(&driver_das08_cs);
608 MODULE_LICENSE("GPL");
609 module_init( das08_cs_init_module );
610 module_exit( das08_cs_exit_module );