GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / comedi / drivers / ni_daq_700.c
blob04706a433c04214ef81e5bea5b6b739c735b5942
1 /*
2 * comedi/drivers/ni_daq_700.c
3 * Driver for DAQCard-700 DIO only
4 * copied from 8255
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
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.
26 Driver: ni_daq_700
27 Description: National Instruments PCMCIA DAQCard-700 DIO only
28 Author: Fred Brooks <nsaspook@nsaspook.com>,
29 based on ni_daq_dio24 by Daniel Vecino Castel <dvecino@able.es>
30 Devices: [National Instruments] PCMCIA DAQ-Card-700 (ni_daq_700)
31 Status: works
32 Updated: Thu, 21 Feb 2008 12:07:20 +0000
34 The daqcard-700 appears in Comedi as a single digital I/O subdevice with
35 16 channels. The channel 0 corresponds to the daqcard-700's output
36 port, bit 0; channel 8 corresponds to the input port, bit 0.
38 Direction configuration: channels 0-7 output, 8-15 input (8225 device
39 emu as port A output, port B input, port C N/A).
41 IRQ is assigned but not used.
44 #include <linux/interrupt.h>
45 #include <linux/slab.h>
46 #include "../comedidev.h"
48 #include <linux/ioport.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 DIO700_SIZE 8 /* size of io region used by board */
59 static int dio700_attach(struct comedi_device *dev,
60 struct comedi_devconfig *it);
61 static int dio700_detach(struct comedi_device *dev);
63 enum dio700_bustype { pcmcia_bustype };
65 struct dio700_board {
66 const char *name;
67 int device_id; /* device id for pcmcia board */
68 enum dio700_bustype bustype; /* PCMCIA */
69 int have_dio; /* have daqcard-700 dio */
70 /* function pointers so we can use inb/outb or readb/writeb */
71 /* as appropriate */
72 unsigned int (*read_byte) (unsigned int address);
73 void (*write_byte) (unsigned int byte, unsigned int address);
76 static const struct dio700_board dio700_boards[] = {
78 .name = "daqcard-700",
79 /* 0x10b is manufacturer id, 0x4743 is device id */
80 .device_id = 0x4743,
81 .bustype = pcmcia_bustype,
82 .have_dio = 1,
85 .name = "ni_daq_700",
86 /* 0x10b is manufacturer id, 0x4743 is device id */
87 .device_id = 0x4743,
88 .bustype = pcmcia_bustype,
89 .have_dio = 1,
94 * Useful for shorthand access to the particular board structure
96 #define thisboard ((const struct dio700_board *)dev->board_ptr)
98 struct dio700_private {
100 int data; /* number of data points left to be taken */
103 #define devpriv ((struct dio700_private *)dev->private)
105 static struct comedi_driver driver_dio700 = {
106 .driver_name = "ni_daq_700",
107 .module = THIS_MODULE,
108 .attach = dio700_attach,
109 .detach = dio700_detach,
110 .num_names = ARRAY_SIZE(dio700_boards),
111 .board_name = &dio700_boards[0].name,
112 .offset = sizeof(struct dio700_board),
115 /* the real driver routines */
117 #define _700_SIZE 8
119 #define _700_DATA 0
121 #define DIO_W 0x04
122 #define DIO_R 0x05
124 struct subdev_700_struct {
125 unsigned long cb_arg;
126 int (*cb_func) (int, int, int, unsigned long);
127 int have_irq;
130 #define CALLBACK_ARG (((struct subdev_700_struct *)s->private)->cb_arg)
131 #define CALLBACK_FUNC (((struct subdev_700_struct *)s->private)->cb_func)
132 #define subdevpriv ((struct subdev_700_struct *)s->private)
134 static void do_config(struct comedi_device *dev, struct comedi_subdevice *s);
136 void subdev_700_interrupt(struct comedi_device *dev, struct comedi_subdevice *s)
138 short d;
140 d = CALLBACK_FUNC(0, _700_DATA, 0, CALLBACK_ARG);
142 comedi_buf_put(s->async, d);
143 s->async->events |= COMEDI_CB_EOS;
145 comedi_event(dev, s);
147 EXPORT_SYMBOL(subdev_700_interrupt);
149 static int subdev_700_cb(int dir, int port, int data, unsigned long arg)
151 /* port is always A for output and B for input (8255 emu) */
152 unsigned long iobase = arg;
154 if (dir) {
155 outb(data, iobase + DIO_W);
156 return 0;
157 } else {
158 return inb(iobase + DIO_R);
162 static int subdev_700_insn(struct comedi_device *dev,
163 struct comedi_subdevice *s, struct comedi_insn *insn,
164 unsigned int *data)
166 if (data[0]) {
167 s->state &= ~data[0];
168 s->state |= (data[0] & data[1]);
170 if (data[0] & 0xff)
171 CALLBACK_FUNC(1, _700_DATA, s->state & 0xff,
172 CALLBACK_ARG);
175 data[1] = s->state & 0xff;
176 data[1] |= CALLBACK_FUNC(0, _700_DATA, 0, CALLBACK_ARG) << 8;
178 return 2;
181 static int subdev_700_insn_config(struct comedi_device *dev,
182 struct comedi_subdevice *s,
183 struct comedi_insn *insn, unsigned int *data)
186 switch (data[0]) {
187 case INSN_CONFIG_DIO_INPUT:
188 break;
189 case INSN_CONFIG_DIO_OUTPUT:
190 break;
191 case INSN_CONFIG_DIO_QUERY:
192 data[1] =
193 (s->
194 io_bits & (1 << CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT :
195 COMEDI_INPUT;
196 return insn->n;
197 break;
198 default:
199 return -EINVAL;
202 return 1;
205 static void do_config(struct comedi_device *dev, struct comedi_subdevice *s)
206 { /* use powerup defaults */
207 return;
210 static int subdev_700_cmdtest(struct comedi_device *dev,
211 struct comedi_subdevice *s,
212 struct comedi_cmd *cmd)
214 int err = 0;
215 unsigned int tmp;
217 /* step 1 */
219 tmp = cmd->start_src;
220 cmd->start_src &= TRIG_NOW;
221 if (!cmd->start_src || tmp != cmd->start_src)
222 err++;
224 tmp = cmd->scan_begin_src;
225 cmd->scan_begin_src &= TRIG_EXT;
226 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
227 err++;
229 tmp = cmd->convert_src;
230 cmd->convert_src &= TRIG_FOLLOW;
231 if (!cmd->convert_src || tmp != cmd->convert_src)
232 err++;
234 tmp = cmd->scan_end_src;
235 cmd->scan_end_src &= TRIG_COUNT;
236 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
237 err++;
239 tmp = cmd->stop_src;
240 cmd->stop_src &= TRIG_NONE;
241 if (!cmd->stop_src || tmp != cmd->stop_src)
242 err++;
244 if (err)
245 return 1;
247 /* step 2 */
249 if (err)
250 return 2;
252 /* step 3 */
254 if (cmd->start_arg != 0) {
255 cmd->start_arg = 0;
256 err++;
258 if (cmd->scan_begin_arg != 0) {
259 cmd->scan_begin_arg = 0;
260 err++;
262 if (cmd->convert_arg != 0) {
263 cmd->convert_arg = 0;
264 err++;
266 if (cmd->scan_end_arg != 1) {
267 cmd->scan_end_arg = 1;
268 err++;
270 if (cmd->stop_arg != 0) {
271 cmd->stop_arg = 0;
272 err++;
275 if (err)
276 return 3;
278 /* step 4 */
280 if (err)
281 return 4;
283 return 0;
286 static int subdev_700_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
289 return 0;
292 static int subdev_700_cancel(struct comedi_device *dev,
293 struct comedi_subdevice *s)
296 return 0;
299 int subdev_700_init(struct comedi_device *dev, struct comedi_subdevice *s,
300 int (*cb) (int, int, int, unsigned long), unsigned long arg)
302 s->type = COMEDI_SUBD_DIO;
303 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
304 s->n_chan = 16;
305 s->range_table = &range_digital;
306 s->maxdata = 1;
308 s->private = kmalloc(sizeof(struct subdev_700_struct), GFP_KERNEL);
309 if (!s->private)
310 return -ENOMEM;
312 CALLBACK_ARG = arg;
313 if (cb == NULL)
314 CALLBACK_FUNC = subdev_700_cb;
315 else
316 CALLBACK_FUNC = cb;
318 s->insn_bits = subdev_700_insn;
319 s->insn_config = subdev_700_insn_config;
321 s->state = 0;
322 s->io_bits = 0x00ff;
323 do_config(dev, s);
325 return 0;
327 EXPORT_SYMBOL(subdev_700_init);
329 int subdev_700_init_irq(struct comedi_device *dev, struct comedi_subdevice *s,
330 int (*cb) (int, int, int, unsigned long),
331 unsigned long arg)
333 int ret;
335 ret = subdev_700_init(dev, s, cb, arg);
336 if (ret < 0)
337 return ret;
339 s->do_cmdtest = subdev_700_cmdtest;
340 s->do_cmd = subdev_700_cmd;
341 s->cancel = subdev_700_cancel;
343 subdevpriv->have_irq = 1;
345 return 0;
347 EXPORT_SYMBOL(subdev_700_init_irq);
349 void subdev_700_cleanup(struct comedi_device *dev, struct comedi_subdevice *s)
351 if (s->private)
352 if (subdevpriv->have_irq)
354 kfree(s->private);
356 EXPORT_SYMBOL(subdev_700_cleanup);
358 static int dio700_attach(struct comedi_device *dev, struct comedi_devconfig *it)
360 struct comedi_subdevice *s;
361 unsigned long iobase = 0;
362 #ifdef incomplete
363 unsigned int irq = 0;
364 #endif
365 struct pcmcia_device *link;
367 /* allocate and initialize dev->private */
368 if (alloc_private(dev, sizeof(struct dio700_private)) < 0)
369 return -ENOMEM;
371 /* get base address, irq etc. based on bustype */
372 switch (thisboard->bustype) {
373 case pcmcia_bustype:
374 link = pcmcia_cur_dev;
375 if (!link)
376 return -EIO;
377 iobase = link->resource[0]->start;
378 #ifdef incomplete
379 irq = link->irq;
380 #endif
381 break;
382 default:
383 printk("bug! couldn't determine board type\n");
384 return -EINVAL;
385 break;
387 printk("comedi%d: ni_daq_700: %s, io 0x%lx", dev->minor,
388 thisboard->name, iobase);
389 #ifdef incomplete
390 if (irq)
391 printk(", irq %u", irq);
393 #endif
395 printk("\n");
397 if (iobase == 0) {
398 printk("io base address is zero!\n");
399 return -EINVAL;
402 dev->iobase = iobase;
404 #ifdef incomplete
405 /* grab our IRQ */
406 dev->irq = irq;
407 #endif
409 dev->board_name = thisboard->name;
411 if (alloc_subdevices(dev, 1) < 0)
412 return -ENOMEM;
414 /* DAQCard-700 dio */
415 s = dev->subdevices + 0;
416 subdev_700_init(dev, s, NULL, dev->iobase);
418 return 0;
421 static int dio700_detach(struct comedi_device *dev)
423 printk("comedi%d: ni_daq_700: cs-remove\n", dev->minor);
425 if (dev->subdevices)
426 subdev_700_cleanup(dev, dev->subdevices + 0);
428 if (thisboard->bustype != pcmcia_bustype && dev->iobase)
429 release_region(dev->iobase, DIO700_SIZE);
430 if (dev->irq)
431 free_irq(dev->irq, dev);
433 return 0;
436 /* PCMCIA crap -- watch your words, please! */
438 static void dio700_config(struct pcmcia_device *link);
439 static void dio700_release(struct pcmcia_device *link);
440 static int dio700_cs_suspend(struct pcmcia_device *p_dev);
441 static int dio700_cs_resume(struct pcmcia_device *p_dev);
444 The attach() and detach() entry points are used to create and destroy
445 "instances" of the driver, where each instance represents everything
446 needed to manage one actual PCMCIA card.
449 static int dio700_cs_attach(struct pcmcia_device *);
450 static void dio700_cs_detach(struct pcmcia_device *);
453 You'll also need to prototype all the functions that will actually
454 be used to talk to your device. See 'memory_cs' for a good example
455 of a fully self-sufficient driver; the other drivers rely more or
456 less on other parts of the kernel.
459 struct local_info_t {
460 struct pcmcia_device *link;
461 int stop;
462 struct bus_operations *bus;
465 /*======================================================================
467 dio700_cs_attach() creates an "instance" of the driver, allocating
468 local data structures for one device. The device is registered
469 with Card Services.
471 The dev_link structure is initialized, but we don't actually
472 configure the card at this point -- we wait until we receive a
473 card insertion event.
475 ======================================================================*/
477 static int dio700_cs_attach(struct pcmcia_device *link)
479 struct local_info_t *local;
481 printk(KERN_INFO "ni_daq_700: cs-attach\n");
483 dev_dbg(&link->dev, "dio700_cs_attach()\n");
485 /* Allocate space for private device-specific data */
486 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
487 if (!local)
488 return -ENOMEM;
489 local->link = link;
490 link->priv = local;
493 General socket configuration defaults can go here. In this
494 client, we assume very little, and rely on the CIS for almost
495 everything. In most clients, many details (i.e., number, sizes,
496 and attributes of IO windows) are fixed by the nature of the
497 device, and can be hard-wired here.
499 link->conf.Attributes = 0;
500 link->conf.IntType = INT_MEMORY_AND_IO;
502 pcmcia_cur_dev = link;
504 dio700_config(link);
506 return 0;
507 } /* dio700_cs_attach */
509 /*======================================================================
511 This deletes a driver "instance". The device is de-registered
512 with Card Services. If it has been released, all local data
513 structures are freed. Otherwise, the structures will be freed
514 when the device is released.
516 ======================================================================*/
518 static void dio700_cs_detach(struct pcmcia_device *link)
521 printk(KERN_INFO "ni_daq_700: cs-detach!\n");
523 dev_dbg(&link->dev, "dio700_cs_detach\n");
525 ((struct local_info_t *)link->priv)->stop = 1;
526 dio700_release(link);
528 /* This points to the parent struct local_info_t struct */
529 kfree(link->priv);
531 } /* dio700_cs_detach */
533 /*======================================================================
535 dio700_config() is scheduled to run after a CARD_INSERTION event
536 is received, to configure the PCMCIA socket, and to make the
537 device available to the system.
539 ======================================================================*/
541 static int dio700_pcmcia_config_loop(struct pcmcia_device *p_dev,
542 cistpl_cftable_entry_t *cfg,
543 cistpl_cftable_entry_t *dflt,
544 unsigned int vcc,
545 void *priv_data)
547 if (cfg->index == 0)
548 return -ENODEV;
550 /* Does this card need audio output? */
551 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
552 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
553 p_dev->conf.Status = CCSR_AUDIO_ENA;
556 /* Do we need to allocate an interrupt? */
557 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
559 /* IO window settings */
560 p_dev->resource[0]->end = p_dev->resource[1]->end = 0;
561 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
562 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
563 p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
564 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
565 p_dev->resource[0]->flags |=
566 pcmcia_io_cfg_data_width(io->flags);
567 p_dev->resource[0]->start = io->win[0].base;
568 p_dev->resource[0]->end = io->win[0].len;
569 if (io->nwin > 1) {
570 p_dev->resource[1]->flags = p_dev->resource[0]->flags;
571 p_dev->resource[1]->start = io->win[1].base;
572 p_dev->resource[1]->end = io->win[1].len;
574 /* This reserves IO space but doesn't actually enable it */
575 if (pcmcia_request_io(p_dev) != 0)
576 return -ENODEV;
579 /* If we got this far, we're cool! */
580 return 0;
583 static void dio700_config(struct pcmcia_device *link)
585 win_req_t req;
586 int ret;
588 printk(KERN_INFO "ni_daq_700: cs-config\n");
590 dev_dbg(&link->dev, "dio700_config\n");
592 ret = pcmcia_loop_config(link, dio700_pcmcia_config_loop, NULL);
593 if (ret) {
594 dev_warn(&link->dev, "no configuration found\n");
595 goto failed;
598 if (!link->irq)
599 goto failed;
602 This actually configures the PCMCIA socket -- setting up
603 the I/O windows and the interrupt mapping, and putting the
604 card and host interface into "Memory and IO" mode.
606 ret = pcmcia_request_configuration(link, &link->conf);
607 if (ret != 0)
608 goto failed;
610 /* Finally, report what we've done */
611 dev_info(&link->dev, "index 0x%02x", link->conf.ConfigIndex);
612 if (link->conf.Attributes & CONF_ENABLE_IRQ)
613 printk(", irq %d", link->irq);
614 if (link->resource[0])
615 printk(", io %pR", link->resource[0]);
616 if (link->resource[1])
617 printk(" & %pR", link->resource[1]);
618 printk("\n");
620 return;
622 failed:
623 printk(KERN_INFO "ni_daq_700 cs failed");
624 dio700_release(link);
626 } /* dio700_config */
628 static void dio700_release(struct pcmcia_device *link)
630 dev_dbg(&link->dev, "dio700_release\n");
632 pcmcia_disable_device(link);
633 } /* dio700_release */
635 /*======================================================================
637 The card status event handler. Mostly, this schedules other
638 stuff to run after an event is received.
640 When a CARD_REMOVAL event is received, we immediately set a
641 private flag to block future accesses to this device. All the
642 functions that actually access the device should check this flag
643 to make sure the card is still present.
645 ======================================================================*/
647 static int dio700_cs_suspend(struct pcmcia_device *link)
649 struct local_info_t *local = link->priv;
651 /* Mark the device as stopped, to block IO until later */
652 local->stop = 1;
653 return 0;
654 } /* dio700_cs_suspend */
656 static int dio700_cs_resume(struct pcmcia_device *link)
658 struct local_info_t *local = link->priv;
660 local->stop = 0;
661 return 0;
662 } /* dio700_cs_resume */
664 /*====================================================================*/
666 static struct pcmcia_device_id dio700_cs_ids[] = {
667 /* N.B. These IDs should match those in dio700_boards */
668 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x4743), /* daqcard-700 */
669 PCMCIA_DEVICE_NULL
673 MODULE_DEVICE_TABLE(pcmcia, dio700_cs_ids);
674 MODULE_AUTHOR("Fred Brooks <nsaspook@nsaspook.com>");
675 MODULE_DESCRIPTION("Comedi driver for National Instruments "
676 "PCMCIA DAQCard-700 DIO");
677 MODULE_LICENSE("GPL");
679 struct pcmcia_driver dio700_cs_driver = {
680 .probe = dio700_cs_attach,
681 .remove = dio700_cs_detach,
682 .suspend = dio700_cs_suspend,
683 .resume = dio700_cs_resume,
684 .id_table = dio700_cs_ids,
685 .owner = THIS_MODULE,
686 .drv = {
687 .name = "ni_daq_700",
691 static int __init init_dio700_cs(void)
693 pcmcia_register_driver(&dio700_cs_driver);
694 return 0;
697 static void __exit exit_dio700_cs(void)
699 pr_debug("ni_daq_700: unloading\n");
700 pcmcia_unregister_driver(&dio700_cs_driver);
703 int __init init_module(void)
705 int ret;
707 ret = init_dio700_cs();
708 if (ret < 0)
709 return ret;
711 return comedi_driver_register(&driver_dio700);
714 void __exit cleanup_module(void)
716 exit_dio700_cs();
717 comedi_driver_unregister(&driver_dio700);