2 comedi/drivers/ni_daq_dio24.c
3 Driver for National Instruments PCMCIA DAQ-Card DIO-24
4 Copyright (C) 2002 Daniel Vecino Castel <dvecino@able.es>
6 PCMCIA crap at end of file is adapted from dummy_cs.c 1.31 2001/08/24 12:13:13
7 from the pcmcia package.
8 The initial developer of the pcmcia dummy_cs.c code is David A. Hinds
9 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
10 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ************************************************************************
30 Description: National Instruments PCMCIA DAQ-Card DIO-24
31 Author: Daniel Vecino Castel <dvecino@able.es>
32 Devices: [National Instruments] PCMCIA DAQ-Card DIO-24 (ni_daq_dio24)
34 Updated: Thu, 07 Nov 2002 21:53:06 -0800
36 This is just a wrapper around the 8255.o driver to properly handle
40 //#define LABPC_DEBUG // enable debugging messages
43 #include "../comedidev.h"
45 #include <linux/ioport.h>
49 #include <pcmcia/cs_types.h>
50 #include <pcmcia/cs.h>
51 #include <pcmcia/cistpl.h>
52 #include <pcmcia/cisreg.h>
53 #include <pcmcia/ds.h>
55 static struct pcmcia_device *pcmcia_cur_dev = NULL;
57 #define DIO24_SIZE 4 // size of io region used by board
59 static int dio24_attach(struct comedi_device * dev, struct comedi_devconfig * it);
60 static int dio24_detach(struct comedi_device * dev);
62 enum dio24_bustype { pcmcia_bustype };
64 typedef struct dio24_board_struct {
66 int device_id; // device id for pcmcia board
67 enum dio24_bustype bustype; // PCMCIA
68 int have_dio; // have 8255 chip
69 // function pointers so we can use inb/outb or readb/writeb as appropriate
70 unsigned int (*read_byte) (unsigned int address);
71 void (*write_byte) (unsigned int byte, unsigned int address);
74 static const dio24_board dio24_boards[] = {
76 name: "daqcard-dio24",
77 device_id:0x475c,// 0x10b is manufacturer id, 0x475c is device id
78 bustype: pcmcia_bustype,
83 device_id:0x475c,// 0x10b is manufacturer id, 0x475c is device id
84 bustype: pcmcia_bustype,
90 * Useful for shorthand access to the particular board structure
92 #define thisboard ((const dio24_board *)dev->board_ptr)
94 struct dio24_private {
96 int data; /* number of data points left to be taken */
100 #define devpriv ((struct dio24_private *)dev->private)
102 static struct comedi_driver driver_dio24 = {
103 driver_name:"ni_daq_dio24",
107 num_names:sizeof(dio24_boards) / sizeof(dio24_board),
108 board_name:&dio24_boards[0].name,
109 offset:sizeof(dio24_board),
112 static int dio24_attach(struct comedi_device * dev, struct comedi_devconfig * it)
114 struct comedi_subdevice *s;
115 unsigned long iobase = 0;
117 unsigned int irq = 0;
119 struct pcmcia_device *link;
121 /* allocate and initialize dev->private */
122 if (alloc_private(dev, sizeof(struct dio24_private)) < 0)
125 // get base address, irq etc. based on bustype
126 switch (thisboard->bustype) {
128 link = pcmcia_cur_dev; /* XXX hack */
131 iobase = link->io.BasePort1;
133 irq = link->irq.AssignedIRQ;
137 printk("bug! couldn't determine board type\n");
141 printk("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor,
142 thisboard->name, iobase);
145 printk(", irq %u", irq);
152 printk("io base address is zero!\n");
156 dev->iobase = iobase;
163 dev->board_name = thisboard->name;
165 if (alloc_subdevices(dev, 1) < 0)
169 s = dev->subdevices + 0;
170 subdev_8255_init(dev, s, NULL, dev->iobase);
175 static int dio24_detach(struct comedi_device * dev)
177 printk("comedi%d: ni_daq_dio24: remove\n", dev->minor);
180 subdev_8255_cleanup(dev, dev->subdevices + 0);
182 if (thisboard->bustype != pcmcia_bustype && dev->iobase)
183 release_region(dev->iobase, DIO24_SIZE);
185 comedi_free_irq(dev->irq, dev);
193 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
194 you do not define PCMCIA_DEBUG at all, all the debug code will be
195 left out. If you compile with PCMCIA_DEBUG=0, the debug code will
196 be present but disabled -- but it can then be enabled for specific
197 modules at load time with a 'pc_debug=#' option to insmod.
200 static int pc_debug = PCMCIA_DEBUG;
201 module_param(pc_debug, int, 0644);
202 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
203 static char *version = "ni_daq_dio24.c, based on dummy_cs.c";
205 #define DEBUG(n, args...)
208 /*====================================================================*/
210 static void dio24_config(struct pcmcia_device *link);
211 static void dio24_release(struct pcmcia_device *link);
212 static int dio24_cs_suspend(struct pcmcia_device *p_dev);
213 static int dio24_cs_resume(struct pcmcia_device *p_dev);
216 The attach() and detach() entry points are used to create and destroy
217 "instances" of the driver, where each instance represents everything
218 needed to manage one actual PCMCIA card.
221 static int dio24_cs_attach(struct pcmcia_device *);
222 static void dio24_cs_detach(struct pcmcia_device *);
225 You'll also need to prototype all the functions that will actually
226 be used to talk to your device. See 'memory_cs' for a good example
227 of a fully self-sufficient driver; the other drivers rely more or
228 less on other parts of the kernel.
232 The dev_info variable is the "key" that is used to match up this
233 device driver with appropriate cards, through the card configuration
237 static const dev_info_t dev_info = "ni_daq_dio24";
239 typedef struct local_info_t {
240 struct pcmcia_device *link;
243 struct bus_operations *bus;
246 /*======================================================================
248 dio24_cs_attach() creates an "instance" of the driver, allocating
249 local data structures for one device. The device is registered
252 The dev_link structure is initialized, but we don't actually
253 configure the card at this point -- we wait until we receive a
254 card insertion event.
256 ======================================================================*/
258 static int dio24_cs_attach(struct pcmcia_device *link)
262 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n");
264 DEBUG(0, "dio24_cs_attach()\n");
266 /* Allocate space for private device-specific data */
267 local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
273 /* Interrupt setup */
274 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
275 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
276 link->irq.Handler = NULL;
279 General socket configuration defaults can go here. In this
280 client, we assume very little, and rely on the CIS for almost
281 everything. In most clients, many details (i.e., number, sizes,
282 and attributes of IO windows) are fixed by the nature of the
283 device, and can be hard-wired here.
285 link->conf.Attributes = 0;
286 link->conf.IntType = INT_MEMORY_AND_IO;
288 pcmcia_cur_dev = link;
293 } /* dio24_cs_attach */
295 /*======================================================================
297 This deletes a driver "instance". The device is de-registered
298 with Card Services. If it has been released, all local data
299 structures are freed. Otherwise, the structures will be freed
300 when the device is released.
302 ======================================================================*/
304 static void dio24_cs_detach(struct pcmcia_device *link)
307 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n");
309 DEBUG(0, "dio24_cs_detach(0x%p)\n", link);
311 if (link->dev_node) {
312 ((local_info_t *) link->priv)->stop = 1;
316 /* This points to the parent local_info_t struct */
320 } /* dio24_cs_detach */
322 /*======================================================================
324 dio24_config() is scheduled to run after a CARD_INSERTION event
325 is received, to configure the PCMCIA socket, and to make the
326 device available to the system.
328 ======================================================================*/
330 static void dio24_config(struct pcmcia_device *link)
332 local_info_t *dev = link->priv;
339 cistpl_cftable_entry_t dflt = { 0 };
341 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n");
343 DEBUG(0, "dio24_config(0x%p)\n", link);
346 This reads the card's CONFIG tuple to find its configuration
349 tuple.DesiredTuple = CISTPL_CONFIG;
350 tuple.Attributes = 0;
351 tuple.TupleData = buf;
352 tuple.TupleDataMax = sizeof(buf);
353 tuple.TupleOffset = 0;
354 if ((last_ret = pcmcia_get_first_tuple(link, &tuple)) != 0) {
355 cs_error(link, GetFirstTuple, last_ret);
358 if ((last_ret = pcmcia_get_tuple_data(link, &tuple)) != 0) {
359 cs_error(link, GetTupleData, last_ret);
362 if ((last_ret = pcmcia_parse_tuple(&tuple, &parse)) != 0) {
363 cs_error(link, ParseTuple, last_ret);
366 link->conf.ConfigBase = parse.config.base;
367 link->conf.Present = parse.config.rmask[0];
370 In this loop, we scan the CIS for configuration table entries,
371 each of which describes a valid card configuration, including
372 voltage, IO window, memory window, and interrupt settings.
374 We make no assumptions about the card to be configured: we use
375 just the information available in the CIS. In an ideal world,
376 this would work for any PCMCIA card, but it requires a complete
377 and accurate CIS. In practice, a driver usually "knows" most of
378 these things without consulting the CIS, and most client drivers
379 will only use the CIS to fill in implementation-defined details.
381 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
382 if ((last_ret = pcmcia_get_first_tuple(link, &tuple)) != 0) {
383 cs_error(link, GetFirstTuple, last_ret);
387 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
388 if (pcmcia_get_tuple_data(link, &tuple) != 0)
390 if (pcmcia_parse_tuple(&tuple, &parse) != 0)
393 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
397 link->conf.ConfigIndex = cfg->index;
399 /* Does this card need audio output? */
400 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
401 link->conf.Attributes |= CONF_ENABLE_SPKR;
402 link->conf.Status = CCSR_AUDIO_ENA;
405 /* Do we need to allocate an interrupt? */
406 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
407 link->conf.Attributes |= CONF_ENABLE_IRQ;
409 /* IO window settings */
410 link->io.NumPorts1 = link->io.NumPorts2 = 0;
411 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
412 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
413 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
414 if (!(io->flags & CISTPL_IO_8BIT))
415 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
416 if (!(io->flags & CISTPL_IO_16BIT))
417 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
418 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
419 link->io.BasePort1 = io->win[0].base;
420 link->io.NumPorts1 = io->win[0].len;
422 link->io.Attributes2 = link->io.Attributes1;
423 link->io.BasePort2 = io->win[1].base;
424 link->io.NumPorts2 = io->win[1].len;
426 /* This reserves IO space but doesn't actually enable it */
427 if (pcmcia_request_io(link, &link->io) != 0)
431 if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) {
433 (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
434 req.Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
435 req.Attributes |= WIN_ENABLE;
436 req.Base = mem->win[0].host_addr;
437 req.Size = mem->win[0].len;
438 if (req.Size < 0x1000)
441 if (pcmcia_request_window(&link, &req, &link->win))
444 map.CardOffset = mem->win[0].card_addr;
445 if (pcmcia_map_mem_page(link->win, &map))
448 /* If we got this far, we're cool! */
452 if ((last_ret = pcmcia_get_next_tuple(link, &tuple)) != 0) {
453 cs_error(link, GetNextTuple, last_ret);
459 Allocate an interrupt line. Note that this does not assign a
460 handler to the interrupt, unless the 'Handler' member of the
461 irq structure is initialized.
463 if (link->conf.Attributes & CONF_ENABLE_IRQ)
464 if ((last_ret = pcmcia_request_irq(link, &link->irq)) != 0) {
465 cs_error(link, RequestIRQ, last_ret);
470 This actually configures the PCMCIA socket -- setting up
471 the I/O windows and the interrupt mapping, and putting the
472 card and host interface into "Memory and IO" mode.
474 if ((last_ret = pcmcia_request_configuration(link, &link->conf)) != 0) {
475 cs_error(link, RequestConfiguration, last_ret);
480 At this point, the dev_node_t structure(s) need to be
481 initialized and arranged in a linked list at link->dev.
483 sprintf(dev->node.dev_name, "ni_daq_dio24");
484 dev->node.major = dev->node.minor = 0;
485 link->dev_node = &dev->node;
487 /* Finally, report what we've done */
488 printk(KERN_INFO "%s: index 0x%02x",
489 dev->node.dev_name, link->conf.ConfigIndex);
490 if (link->conf.Attributes & CONF_ENABLE_IRQ)
491 printk(", irq %d", link->irq.AssignedIRQ);
492 if (link->io.NumPorts1)
493 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
494 link->io.BasePort1 + link->io.NumPorts1 - 1);
495 if (link->io.NumPorts2)
496 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
497 link->io.BasePort2 + link->io.NumPorts2 - 1);
499 printk(", mem 0x%06lx-0x%06lx", req.Base,
500 req.Base + req.Size - 1);
506 printk(KERN_INFO "Fallo");
511 static void dio24_release(struct pcmcia_device *link)
513 DEBUG(0, "dio24_release(0x%p)\n", link);
515 pcmcia_disable_device(link);
516 } /* dio24_release */
518 /*======================================================================
520 The card status event handler. Mostly, this schedules other
521 stuff to run after an event is received.
523 When a CARD_REMOVAL event is received, we immediately set a
524 private flag to block future accesses to this device. All the
525 functions that actually access the device should check this flag
526 to make sure the card is still present.
528 ======================================================================*/
530 static int dio24_cs_suspend(struct pcmcia_device *link)
532 local_info_t *local = link->priv;
534 /* Mark the device as stopped, to block IO until later */
537 } /* dio24_cs_suspend */
539 static int dio24_cs_resume(struct pcmcia_device *link)
541 local_info_t *local = link->priv;
545 } /* dio24_cs_resume */
547 /*====================================================================*/
549 static struct pcmcia_device_id dio24_cs_ids[] = {
550 /* N.B. These IDs should match those in dio24_boards */
551 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */
555 MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids);
557 struct pcmcia_driver dio24_cs_driver = {
558 .probe = dio24_cs_attach,
559 .remove = dio24_cs_detach,
560 .suspend = dio24_cs_suspend,
561 .resume = dio24_cs_resume,
562 .id_table = dio24_cs_ids,
563 .owner = THIS_MODULE,
569 static int __init init_dio24_cs(void)
571 printk("ni_daq_dio24: HOLA SOY YO!\n");
572 DEBUG(0, "%s\n", version);
573 pcmcia_register_driver(&dio24_cs_driver);
577 static void __exit exit_dio24_cs(void)
579 DEBUG(0, "ni_dio24: unloading\n");
580 pcmcia_unregister_driver(&dio24_cs_driver);
583 int __init init_module(void)
587 ret = init_dio24_cs();
591 return comedi_driver_register(&driver_dio24);
594 void __exit cleanup_module(void)
597 comedi_driver_unregister(&driver_dio24);