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 <linux/interrupt.h>
44 #include <linux/slab.h>
45 #include "../comedidev.h"
47 #include <linux/ioport.h>
51 #include <pcmcia/cs.h>
52 #include <pcmcia/cistpl.h>
53 #include <pcmcia/cisreg.h>
54 #include <pcmcia/ds.h>
56 static struct pcmcia_device *pcmcia_cur_dev = NULL;
58 #define DIO24_SIZE 4 /* size of io region used by board */
60 static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it);
61 static int dio24_detach(struct comedi_device *dev);
63 enum dio24_bustype { pcmcia_bustype };
65 struct dio24_board_struct {
67 int device_id; /* device id for pcmcia board */
68 enum dio24_bustype bustype; /* PCMCIA */
69 int have_dio; /* have 8255 chip */
70 /* function pointers so we can use inb/outb or readb/writeb as appropriate */
71 unsigned int (*read_byte) (unsigned int address);
72 void (*write_byte) (unsigned int byte, unsigned int address);
75 static const struct dio24_board_struct dio24_boards[] = {
77 .name = "daqcard-dio24",
78 .device_id = 0x475c, /* 0x10b is manufacturer id, 0x475c is device id */
79 .bustype = pcmcia_bustype,
83 .name = "ni_daq_dio24",
84 .device_id = 0x475c, /* 0x10b is manufacturer id, 0x475c is device id */
85 .bustype = pcmcia_bustype,
91 * Useful for shorthand access to the particular board structure
93 #define thisboard ((const struct dio24_board_struct *)dev->board_ptr)
95 struct dio24_private {
97 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",
104 .module = THIS_MODULE,
105 .attach = dio24_attach,
106 .detach = dio24_detach,
107 .num_names = ARRAY_SIZE(dio24_boards),
108 .board_name = &dio24_boards[0].name,
109 .offset = sizeof(struct dio24_board_struct),
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->resource[0]->start;
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 free_irq(dev->irq, dev);
190 /* PCMCIA crap -- watch your words! */
192 static void dio24_config(struct pcmcia_device *link);
193 static void dio24_release(struct pcmcia_device *link);
194 static int dio24_cs_suspend(struct pcmcia_device *p_dev);
195 static int dio24_cs_resume(struct pcmcia_device *p_dev);
198 The attach() and detach() entry points are used to create and destroy
199 "instances" of the driver, where each instance represents everything
200 needed to manage one actual PCMCIA card.
203 static int dio24_cs_attach(struct pcmcia_device *);
204 static void dio24_cs_detach(struct pcmcia_device *);
207 You'll also need to prototype all the functions that will actually
208 be used to talk to your device. See 'memory_cs' for a good example
209 of a fully self-sufficient driver; the other drivers rely more or
210 less on other parts of the kernel.
214 The dev_info variable is the "key" that is used to match up this
215 device driver with appropriate cards, through the card configuration
219 static const dev_info_t dev_info = "ni_daq_dio24";
221 struct local_info_t {
222 struct pcmcia_device *link;
224 struct bus_operations *bus;
227 /*======================================================================
229 dio24_cs_attach() creates an "instance" of the driver, allocating
230 local data structures for one device. The device is registered
233 The dev_link structure is initialized, but we don't actually
234 configure the card at this point -- we wait until we receive a
235 card insertion event.
237 ======================================================================*/
239 static int dio24_cs_attach(struct pcmcia_device *link)
241 struct local_info_t *local;
243 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n");
245 dev_dbg(&link->dev, "dio24_cs_attach()\n");
247 /* Allocate space for private device-specific data */
248 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
255 General socket configuration defaults can go here. In this
256 client, we assume very little, and rely on the CIS for almost
257 everything. In most clients, many details (i.e., number, sizes,
258 and attributes of IO windows) are fixed by the nature of the
259 device, and can be hard-wired here.
261 link->conf.Attributes = 0;
262 link->conf.IntType = INT_MEMORY_AND_IO;
264 pcmcia_cur_dev = link;
269 } /* dio24_cs_attach */
271 /*======================================================================
273 This deletes a driver "instance". The device is de-registered
274 with Card Services. If it has been released, all local data
275 structures are freed. Otherwise, the structures will be freed
276 when the device is released.
278 ======================================================================*/
280 static void dio24_cs_detach(struct pcmcia_device *link)
283 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n");
285 dev_dbg(&link->dev, "dio24_cs_detach\n");
287 ((struct local_info_t *)link->priv)->stop = 1;
290 /* This points to the parent local_info_t struct */
294 } /* dio24_cs_detach */
296 /*======================================================================
298 dio24_config() is scheduled to run after a CARD_INSERTION event
299 is received, to configure the PCMCIA socket, and to make the
300 device available to the system.
302 ======================================================================*/
304 static int dio24_pcmcia_config_loop(struct pcmcia_device *p_dev,
305 cistpl_cftable_entry_t *cfg,
306 cistpl_cftable_entry_t *dflt,
313 /* Does this card need audio output? */
314 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
315 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
316 p_dev->conf.Status = CCSR_AUDIO_ENA;
319 /* Do we need to allocate an interrupt? */
320 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
322 /* IO window settings */
323 p_dev->resource[0]->end = p_dev->resource[1]->end = 0;
324 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
325 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
326 p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
327 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
328 p_dev->resource[0]->flags |=
329 pcmcia_io_cfg_data_width(io->flags);
330 p_dev->resource[0]->start = io->win[0].base;
331 p_dev->resource[0]->end = io->win[0].len;
333 p_dev->resource[1]->flags = p_dev->resource[0]->flags;
334 p_dev->resource[1]->start = io->win[1].base;
335 p_dev->resource[1]->end = io->win[1].len;
337 /* This reserves IO space but doesn't actually enable it */
338 if (pcmcia_request_io(p_dev) != 0)
342 /* If we got this far, we're cool! */
346 static void dio24_config(struct pcmcia_device *link)
350 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n");
352 dev_dbg(&link->dev, "dio24_config\n");
354 ret = pcmcia_loop_config(link, dio24_pcmcia_config_loop, NULL);
356 dev_warn(&link->dev, "no configuration found\n");
364 This actually configures the PCMCIA socket -- setting up
365 the I/O windows and the interrupt mapping, and putting the
366 card and host interface into "Memory and IO" mode.
368 ret = pcmcia_request_configuration(link, &link->conf);
372 /* Finally, report what we've done */
373 dev_info(&link->dev, "index 0x%02x", link->conf.ConfigIndex);
374 if (link->conf.Attributes & CONF_ENABLE_IRQ)
375 printk(", irq %d", link->irq);
376 if (link->resource[0])
377 printk(" & %pR", link->resource[0]);
378 if (link->resource[1])
379 printk(" & %pR", link->resource[1]);
385 printk(KERN_INFO "Fallo");
390 static void dio24_release(struct pcmcia_device *link)
392 dev_dbg(&link->dev, "dio24_release\n");
394 pcmcia_disable_device(link);
395 } /* dio24_release */
397 /*======================================================================
399 The card status event handler. Mostly, this schedules other
400 stuff to run after an event is received.
402 When a CARD_REMOVAL event is received, we immediately set a
403 private flag to block future accesses to this device. All the
404 functions that actually access the device should check this flag
405 to make sure the card is still present.
407 ======================================================================*/
409 static int dio24_cs_suspend(struct pcmcia_device *link)
411 struct local_info_t *local = link->priv;
413 /* Mark the device as stopped, to block IO until later */
416 } /* dio24_cs_suspend */
418 static int dio24_cs_resume(struct pcmcia_device *link)
420 struct local_info_t *local = link->priv;
424 } /* dio24_cs_resume */
426 /*====================================================================*/
428 static struct pcmcia_device_id dio24_cs_ids[] = {
429 /* N.B. These IDs should match those in dio24_boards */
430 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */
434 MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids);
435 MODULE_AUTHOR("Daniel Vecino Castel <dvecino@able.es>");
436 MODULE_DESCRIPTION("Comedi driver for National Instruments "
437 "PCMCIA DAQ-Card DIO-24");
438 MODULE_LICENSE("GPL");
440 struct pcmcia_driver dio24_cs_driver = {
441 .probe = dio24_cs_attach,
442 .remove = dio24_cs_detach,
443 .suspend = dio24_cs_suspend,
444 .resume = dio24_cs_resume,
445 .id_table = dio24_cs_ids,
446 .owner = THIS_MODULE,
452 static int __init init_dio24_cs(void)
454 printk("ni_daq_dio24: HOLA SOY YO!\n");
455 pcmcia_register_driver(&dio24_cs_driver);
459 static void __exit exit_dio24_cs(void)
461 pcmcia_unregister_driver(&dio24_cs_driver);
464 int __init init_module(void)
468 ret = init_dio24_cs();
472 return comedi_driver_register(&driver_dio24);
475 void __exit cleanup_module(void)
478 comedi_driver_unregister(&driver_dio24);