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, 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)
95 int data; /* number of data points left to be taken */
98 #define devpriv ((dio24_private *)dev->private)
100 static comedi_driver driver_dio24 = {
101 driver_name:"ni_daq_dio24",
105 num_names:sizeof(dio24_boards) / sizeof(dio24_board),
106 board_name:&dio24_boards[0].name,
107 offset:sizeof(dio24_board),
110 static int dio24_attach(struct comedi_device * dev, comedi_devconfig * it)
112 struct comedi_subdevice *s;
113 unsigned long iobase = 0;
115 unsigned int irq = 0;
117 struct pcmcia_device *link;
119 /* allocate and initialize dev->private */
120 if (alloc_private(dev, sizeof(dio24_private)) < 0)
123 // get base address, irq etc. based on bustype
124 switch (thisboard->bustype) {
126 link = pcmcia_cur_dev; /* XXX hack */
129 iobase = link->io.BasePort1;
131 irq = link->irq.AssignedIRQ;
135 printk("bug! couldn't determine board type\n");
139 printk("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor,
140 thisboard->name, iobase);
143 printk(", irq %u", irq);
150 printk("io base address is zero!\n");
154 dev->iobase = iobase;
161 dev->board_name = thisboard->name;
163 if (alloc_subdevices(dev, 1) < 0)
167 s = dev->subdevices + 0;
168 subdev_8255_init(dev, s, NULL, dev->iobase);
173 static int dio24_detach(struct comedi_device * dev)
175 printk("comedi%d: ni_daq_dio24: remove\n", dev->minor);
178 subdev_8255_cleanup(dev, dev->subdevices + 0);
180 if (thisboard->bustype != pcmcia_bustype && dev->iobase)
181 release_region(dev->iobase, DIO24_SIZE);
183 comedi_free_irq(dev->irq, dev);
191 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
192 you do not define PCMCIA_DEBUG at all, all the debug code will be
193 left out. If you compile with PCMCIA_DEBUG=0, the debug code will
194 be present but disabled -- but it can then be enabled for specific
195 modules at load time with a 'pc_debug=#' option to insmod.
198 static int pc_debug = PCMCIA_DEBUG;
199 module_param(pc_debug, int, 0644);
200 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
201 static char *version = "ni_daq_dio24.c, based on dummy_cs.c";
203 #define DEBUG(n, args...)
206 /*====================================================================*/
208 static void dio24_config(struct pcmcia_device *link);
209 static void dio24_release(struct pcmcia_device *link);
210 static int dio24_cs_suspend(struct pcmcia_device *p_dev);
211 static int dio24_cs_resume(struct pcmcia_device *p_dev);
214 The attach() and detach() entry points are used to create and destroy
215 "instances" of the driver, where each instance represents everything
216 needed to manage one actual PCMCIA card.
219 static int dio24_cs_attach(struct pcmcia_device *);
220 static void dio24_cs_detach(struct pcmcia_device *);
223 You'll also need to prototype all the functions that will actually
224 be used to talk to your device. See 'memory_cs' for a good example
225 of a fully self-sufficient driver; the other drivers rely more or
226 less on other parts of the kernel.
230 The dev_info variable is the "key" that is used to match up this
231 device driver with appropriate cards, through the card configuration
235 static const dev_info_t dev_info = "ni_daq_dio24";
237 typedef struct local_info_t {
238 struct pcmcia_device *link;
241 struct bus_operations *bus;
244 /*======================================================================
246 dio24_cs_attach() creates an "instance" of the driver, allocating
247 local data structures for one device. The device is registered
250 The dev_link structure is initialized, but we don't actually
251 configure the card at this point -- we wait until we receive a
252 card insertion event.
254 ======================================================================*/
256 static int dio24_cs_attach(struct pcmcia_device *link)
260 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n");
262 DEBUG(0, "dio24_cs_attach()\n");
264 /* Allocate space for private device-specific data */
265 local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
271 /* Interrupt setup */
272 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
273 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
274 link->irq.Handler = NULL;
277 General socket configuration defaults can go here. In this
278 client, we assume very little, and rely on the CIS for almost
279 everything. In most clients, many details (i.e., number, sizes,
280 and attributes of IO windows) are fixed by the nature of the
281 device, and can be hard-wired here.
283 link->conf.Attributes = 0;
284 link->conf.IntType = INT_MEMORY_AND_IO;
286 pcmcia_cur_dev = link;
291 } /* dio24_cs_attach */
293 /*======================================================================
295 This deletes a driver "instance". The device is de-registered
296 with Card Services. If it has been released, all local data
297 structures are freed. Otherwise, the structures will be freed
298 when the device is released.
300 ======================================================================*/
302 static void dio24_cs_detach(struct pcmcia_device *link)
305 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n");
307 DEBUG(0, "dio24_cs_detach(0x%p)\n", link);
309 if (link->dev_node) {
310 ((local_info_t *) link->priv)->stop = 1;
314 /* This points to the parent local_info_t struct */
318 } /* dio24_cs_detach */
320 /*======================================================================
322 dio24_config() is scheduled to run after a CARD_INSERTION event
323 is received, to configure the PCMCIA socket, and to make the
324 device available to the system.
326 ======================================================================*/
328 static void dio24_config(struct pcmcia_device *link)
330 local_info_t *dev = link->priv;
337 cistpl_cftable_entry_t dflt = { 0 };
339 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n");
341 DEBUG(0, "dio24_config(0x%p)\n", link);
344 This reads the card's CONFIG tuple to find its configuration
347 tuple.DesiredTuple = CISTPL_CONFIG;
348 tuple.Attributes = 0;
349 tuple.TupleData = buf;
350 tuple.TupleDataMax = sizeof(buf);
351 tuple.TupleOffset = 0;
352 if ((last_ret = pcmcia_get_first_tuple(link, &tuple)) != 0) {
353 cs_error(link, GetFirstTuple, last_ret);
356 if ((last_ret = pcmcia_get_tuple_data(link, &tuple)) != 0) {
357 cs_error(link, GetTupleData, last_ret);
360 if ((last_ret = pcmcia_parse_tuple(&tuple, &parse)) != 0) {
361 cs_error(link, ParseTuple, last_ret);
364 link->conf.ConfigBase = parse.config.base;
365 link->conf.Present = parse.config.rmask[0];
368 In this loop, we scan the CIS for configuration table entries,
369 each of which describes a valid card configuration, including
370 voltage, IO window, memory window, and interrupt settings.
372 We make no assumptions about the card to be configured: we use
373 just the information available in the CIS. In an ideal world,
374 this would work for any PCMCIA card, but it requires a complete
375 and accurate CIS. In practice, a driver usually "knows" most of
376 these things without consulting the CIS, and most client drivers
377 will only use the CIS to fill in implementation-defined details.
379 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
380 if ((last_ret = pcmcia_get_first_tuple(link, &tuple)) != 0) {
381 cs_error(link, GetFirstTuple, last_ret);
385 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
386 if (pcmcia_get_tuple_data(link, &tuple) != 0)
388 if (pcmcia_parse_tuple(&tuple, &parse) != 0)
391 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
395 link->conf.ConfigIndex = cfg->index;
397 /* Does this card need audio output? */
398 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
399 link->conf.Attributes |= CONF_ENABLE_SPKR;
400 link->conf.Status = CCSR_AUDIO_ENA;
403 /* Do we need to allocate an interrupt? */
404 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
405 link->conf.Attributes |= CONF_ENABLE_IRQ;
407 /* IO window settings */
408 link->io.NumPorts1 = link->io.NumPorts2 = 0;
409 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
410 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
411 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
412 if (!(io->flags & CISTPL_IO_8BIT))
413 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
414 if (!(io->flags & CISTPL_IO_16BIT))
415 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
416 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
417 link->io.BasePort1 = io->win[0].base;
418 link->io.NumPorts1 = io->win[0].len;
420 link->io.Attributes2 = link->io.Attributes1;
421 link->io.BasePort2 = io->win[1].base;
422 link->io.NumPorts2 = io->win[1].len;
424 /* This reserves IO space but doesn't actually enable it */
425 if (pcmcia_request_io(link, &link->io) != 0)
429 if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) {
431 (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
432 req.Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
433 req.Attributes |= WIN_ENABLE;
434 req.Base = mem->win[0].host_addr;
435 req.Size = mem->win[0].len;
436 if (req.Size < 0x1000)
439 if (pcmcia_request_window(&link, &req, &link->win))
442 map.CardOffset = mem->win[0].card_addr;
443 if (pcmcia_map_mem_page(link->win, &map))
446 /* If we got this far, we're cool! */
450 if ((last_ret = pcmcia_get_next_tuple(link, &tuple)) != 0) {
451 cs_error(link, GetNextTuple, last_ret);
457 Allocate an interrupt line. Note that this does not assign a
458 handler to the interrupt, unless the 'Handler' member of the
459 irq structure is initialized.
461 if (link->conf.Attributes & CONF_ENABLE_IRQ)
462 if ((last_ret = pcmcia_request_irq(link, &link->irq)) != 0) {
463 cs_error(link, RequestIRQ, last_ret);
468 This actually configures the PCMCIA socket -- setting up
469 the I/O windows and the interrupt mapping, and putting the
470 card and host interface into "Memory and IO" mode.
472 if ((last_ret = pcmcia_request_configuration(link, &link->conf)) != 0) {
473 cs_error(link, RequestConfiguration, last_ret);
478 At this point, the dev_node_t structure(s) need to be
479 initialized and arranged in a linked list at link->dev.
481 sprintf(dev->node.dev_name, "ni_daq_dio24");
482 dev->node.major = dev->node.minor = 0;
483 link->dev_node = &dev->node;
485 /* Finally, report what we've done */
486 printk(KERN_INFO "%s: index 0x%02x",
487 dev->node.dev_name, link->conf.ConfigIndex);
488 if (link->conf.Attributes & CONF_ENABLE_IRQ)
489 printk(", irq %d", link->irq.AssignedIRQ);
490 if (link->io.NumPorts1)
491 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
492 link->io.BasePort1 + link->io.NumPorts1 - 1);
493 if (link->io.NumPorts2)
494 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
495 link->io.BasePort2 + link->io.NumPorts2 - 1);
497 printk(", mem 0x%06lx-0x%06lx", req.Base,
498 req.Base + req.Size - 1);
504 printk(KERN_INFO "Fallo");
509 static void dio24_release(struct pcmcia_device *link)
511 DEBUG(0, "dio24_release(0x%p)\n", link);
513 pcmcia_disable_device(link);
514 } /* dio24_release */
516 /*======================================================================
518 The card status event handler. Mostly, this schedules other
519 stuff to run after an event is received.
521 When a CARD_REMOVAL event is received, we immediately set a
522 private flag to block future accesses to this device. All the
523 functions that actually access the device should check this flag
524 to make sure the card is still present.
526 ======================================================================*/
528 static int dio24_cs_suspend(struct pcmcia_device *link)
530 local_info_t *local = link->priv;
532 /* Mark the device as stopped, to block IO until later */
535 } /* dio24_cs_suspend */
537 static int dio24_cs_resume(struct pcmcia_device *link)
539 local_info_t *local = link->priv;
543 } /* dio24_cs_resume */
545 /*====================================================================*/
547 static struct pcmcia_device_id dio24_cs_ids[] = {
548 /* N.B. These IDs should match those in dio24_boards */
549 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */
553 MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids);
555 struct pcmcia_driver dio24_cs_driver = {
556 .probe = dio24_cs_attach,
557 .remove = dio24_cs_detach,
558 .suspend = dio24_cs_suspend,
559 .resume = dio24_cs_resume,
560 .id_table = dio24_cs_ids,
561 .owner = THIS_MODULE,
567 static int __init init_dio24_cs(void)
569 printk("ni_daq_dio24: HOLA SOY YO!\n");
570 DEBUG(0, "%s\n", version);
571 pcmcia_register_driver(&dio24_cs_driver);
575 static void __exit exit_dio24_cs(void)
577 DEBUG(0, "ni_dio24: unloading\n");
578 pcmcia_unregister_driver(&dio24_cs_driver);
581 int __init init_module(void)
585 ret = init_dio24_cs();
589 return comedi_driver_register(&driver_dio24);
592 void __exit cleanup_module(void)
595 comedi_driver_unregister(&driver_dio24);