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_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/cisreg.h>
55 #include <pcmcia/ds.h>
57 static struct pcmcia_device *pcmcia_cur_dev = NULL;
59 #define DIO24_SIZE 4 /* size of io region used by board */
61 static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it);
62 static int dio24_detach(struct comedi_device *dev);
64 enum dio24_bustype { pcmcia_bustype };
66 struct dio24_board_struct {
68 int device_id; /* device id for pcmcia board */
69 enum dio24_bustype bustype; /* PCMCIA */
70 int have_dio; /* have 8255 chip */
71 /* function pointers so we can use inb/outb or readb/writeb as appropriate */
72 unsigned int (*read_byte) (unsigned int address);
73 void (*write_byte) (unsigned int byte, unsigned int address);
76 static const struct dio24_board_struct dio24_boards[] = {
78 .name = "daqcard-dio24",
79 .device_id = 0x475c, /* 0x10b is manufacturer id, 0x475c is device id */
80 .bustype = pcmcia_bustype,
84 .name = "ni_daq_dio24",
85 .device_id = 0x475c, /* 0x10b is manufacturer id, 0x475c is device id */
86 .bustype = pcmcia_bustype,
92 * Useful for shorthand access to the particular board structure
94 #define thisboard ((const struct dio24_board_struct *)dev->board_ptr)
96 struct dio24_private {
98 int data; /* number of data points left to be taken */
101 #define devpriv ((struct dio24_private *)dev->private)
103 static struct comedi_driver driver_dio24 = {
104 .driver_name = "ni_daq_dio24",
105 .module = THIS_MODULE,
106 .attach = dio24_attach,
107 .detach = dio24_detach,
108 .num_names = ARRAY_SIZE(dio24_boards),
109 .board_name = &dio24_boards[0].name,
110 .offset = sizeof(struct dio24_board_struct),
113 static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it)
115 struct comedi_subdevice *s;
116 unsigned long iobase = 0;
118 unsigned int irq = 0;
120 struct pcmcia_device *link;
122 /* allocate and initialize dev->private */
123 if (alloc_private(dev, sizeof(struct dio24_private)) < 0)
126 /* get base address, irq etc. based on bustype */
127 switch (thisboard->bustype) {
129 link = pcmcia_cur_dev; /* XXX hack */
132 iobase = link->io.BasePort1;
138 printk("bug! couldn't determine board type\n");
142 printk("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor,
143 thisboard->name, iobase);
146 printk(", irq %u", irq);
153 printk("io base address is zero!\n");
157 dev->iobase = iobase;
164 dev->board_name = thisboard->name;
166 if (alloc_subdevices(dev, 1) < 0)
170 s = dev->subdevices + 0;
171 subdev_8255_init(dev, s, NULL, dev->iobase);
176 static int dio24_detach(struct comedi_device *dev)
178 printk("comedi%d: ni_daq_dio24: remove\n", dev->minor);
181 subdev_8255_cleanup(dev, dev->subdevices + 0);
183 if (thisboard->bustype != pcmcia_bustype && dev->iobase)
184 release_region(dev->iobase, DIO24_SIZE);
186 free_irq(dev->irq, dev);
191 /* PCMCIA crap -- watch your words! */
193 static void dio24_config(struct pcmcia_device *link);
194 static void dio24_release(struct pcmcia_device *link);
195 static int dio24_cs_suspend(struct pcmcia_device *p_dev);
196 static int dio24_cs_resume(struct pcmcia_device *p_dev);
199 The attach() and detach() entry points are used to create and destroy
200 "instances" of the driver, where each instance represents everything
201 needed to manage one actual PCMCIA card.
204 static int dio24_cs_attach(struct pcmcia_device *);
205 static void dio24_cs_detach(struct pcmcia_device *);
208 You'll also need to prototype all the functions that will actually
209 be used to talk to your device. See 'memory_cs' for a good example
210 of a fully self-sufficient driver; the other drivers rely more or
211 less on other parts of the kernel.
215 The dev_info variable is the "key" that is used to match up this
216 device driver with appropriate cards, through the card configuration
220 static const dev_info_t dev_info = "ni_daq_dio24";
222 struct local_info_t {
223 struct pcmcia_device *link;
226 struct bus_operations *bus;
229 /*======================================================================
231 dio24_cs_attach() creates an "instance" of the driver, allocating
232 local data structures for one device. The device is registered
235 The dev_link structure is initialized, but we don't actually
236 configure the card at this point -- we wait until we receive a
237 card insertion event.
239 ======================================================================*/
241 static int dio24_cs_attach(struct pcmcia_device *link)
243 struct local_info_t *local;
245 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n");
247 dev_dbg(&link->dev, "dio24_cs_attach()\n");
249 /* Allocate space for private device-specific data */
250 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
257 General socket configuration defaults can go here. In this
258 client, we assume very little, and rely on the CIS for almost
259 everything. In most clients, many details (i.e., number, sizes,
260 and attributes of IO windows) are fixed by the nature of the
261 device, and can be hard-wired here.
263 link->conf.Attributes = 0;
264 link->conf.IntType = INT_MEMORY_AND_IO;
266 pcmcia_cur_dev = link;
271 } /* dio24_cs_attach */
273 /*======================================================================
275 This deletes a driver "instance". The device is de-registered
276 with Card Services. If it has been released, all local data
277 structures are freed. Otherwise, the structures will be freed
278 when the device is released.
280 ======================================================================*/
282 static void dio24_cs_detach(struct pcmcia_device *link)
285 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n");
287 dev_dbg(&link->dev, "dio24_cs_detach\n");
289 if (link->dev_node) {
290 ((struct local_info_t *)link->priv)->stop = 1;
294 /* This points to the parent local_info_t struct */
298 } /* dio24_cs_detach */
300 /*======================================================================
302 dio24_config() is scheduled to run after a CARD_INSERTION event
303 is received, to configure the PCMCIA socket, and to make the
304 device available to the system.
306 ======================================================================*/
308 static int dio24_pcmcia_config_loop(struct pcmcia_device *p_dev,
309 cistpl_cftable_entry_t *cfg,
310 cistpl_cftable_entry_t *dflt,
314 win_req_t *req = priv_data;
320 /* Does this card need audio output? */
321 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
322 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
323 p_dev->conf.Status = CCSR_AUDIO_ENA;
326 /* Do we need to allocate an interrupt? */
327 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
329 /* IO window settings */
330 p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
331 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
332 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
333 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
334 if (!(io->flags & CISTPL_IO_8BIT))
335 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
336 if (!(io->flags & CISTPL_IO_16BIT))
337 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
338 p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
339 p_dev->io.BasePort1 = io->win[0].base;
340 p_dev->io.NumPorts1 = io->win[0].len;
342 p_dev->io.Attributes2 = p_dev->io.Attributes1;
343 p_dev->io.BasePort2 = io->win[1].base;
344 p_dev->io.NumPorts2 = io->win[1].len;
346 /* This reserves IO space but doesn't actually enable it */
347 if (pcmcia_request_io(p_dev, &p_dev->io) != 0)
351 if ((cfg->mem.nwin > 0) || (dflt->mem.nwin > 0)) {
353 (cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
354 req->Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
355 req->Attributes |= WIN_ENABLE;
356 req->Base = mem->win[0].host_addr;
357 req->Size = mem->win[0].len;
358 if (req->Size < 0x1000)
360 req->AccessSpeed = 0;
361 if (pcmcia_request_window(p_dev, req, &p_dev->win))
364 map.CardOffset = mem->win[0].card_addr;
365 if (pcmcia_map_mem_page(p_dev, p_dev->win, &map))
368 /* If we got this far, we're cool! */
372 static void dio24_config(struct pcmcia_device *link)
374 struct local_info_t *dev = link->priv;
378 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n");
380 dev_dbg(&link->dev, "dio24_config\n");
382 ret = pcmcia_loop_config(link, dio24_pcmcia_config_loop, &req);
384 dev_warn(&link->dev, "no configuration found\n");
392 This actually configures the PCMCIA socket -- setting up
393 the I/O windows and the interrupt mapping, and putting the
394 card and host interface into "Memory and IO" mode.
396 ret = pcmcia_request_configuration(link, &link->conf);
401 At this point, the dev_node_t structure(s) need to be
402 initialized and arranged in a linked list at link->dev.
404 sprintf(dev->node.dev_name, "ni_daq_dio24");
405 dev->node.major = dev->node.minor = 0;
406 link->dev_node = &dev->node;
408 /* Finally, report what we've done */
409 printk(KERN_INFO "%s: index 0x%02x",
410 dev->node.dev_name, link->conf.ConfigIndex);
411 if (link->conf.Attributes & CONF_ENABLE_IRQ)
412 printk(", irq %d", link->irq);
413 if (link->io.NumPorts1)
414 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
415 link->io.BasePort1 + link->io.NumPorts1 - 1);
416 if (link->io.NumPorts2)
417 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
418 link->io.BasePort2 + link->io.NumPorts2 - 1);
420 printk(", mem 0x%06lx-0x%06lx", req.Base,
421 req.Base + req.Size - 1);
427 printk(KERN_INFO "Fallo");
432 static void dio24_release(struct pcmcia_device *link)
434 dev_dbg(&link->dev, "dio24_release\n");
436 pcmcia_disable_device(link);
437 } /* dio24_release */
439 /*======================================================================
441 The card status event handler. Mostly, this schedules other
442 stuff to run after an event is received.
444 When a CARD_REMOVAL event is received, we immediately set a
445 private flag to block future accesses to this device. All the
446 functions that actually access the device should check this flag
447 to make sure the card is still present.
449 ======================================================================*/
451 static int dio24_cs_suspend(struct pcmcia_device *link)
453 struct local_info_t *local = link->priv;
455 /* Mark the device as stopped, to block IO until later */
458 } /* dio24_cs_suspend */
460 static int dio24_cs_resume(struct pcmcia_device *link)
462 struct local_info_t *local = link->priv;
466 } /* dio24_cs_resume */
468 /*====================================================================*/
470 static struct pcmcia_device_id dio24_cs_ids[] = {
471 /* N.B. These IDs should match those in dio24_boards */
472 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */
476 MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids);
478 struct pcmcia_driver dio24_cs_driver = {
479 .probe = dio24_cs_attach,
480 .remove = dio24_cs_detach,
481 .suspend = dio24_cs_suspend,
482 .resume = dio24_cs_resume,
483 .id_table = dio24_cs_ids,
484 .owner = THIS_MODULE,
490 static int __init init_dio24_cs(void)
492 printk("ni_daq_dio24: HOLA SOY YO!\n");
493 pcmcia_register_driver(&dio24_cs_driver);
497 static void __exit exit_dio24_cs(void)
499 pcmcia_unregister_driver(&dio24_cs_driver);
502 int __init init_module(void)
506 ret = init_dio24_cs();
510 return comedi_driver_register(&driver_dio24);
513 void __exit cleanup_module(void)
516 comedi_driver_unregister(&driver_dio24);