2 * comedi/drivers/ni_daq_700.c
3 * Driver for DAQCard-700 DIO only
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.
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)
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_types.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 DIO700_SIZE 8 /* size of io region used by board */
60 static int dio700_attach(struct comedi_device *dev,
61 struct comedi_devconfig *it);
62 static int dio700_detach(struct comedi_device *dev);
64 enum dio700_bustype { pcmcia_bustype };
68 int device_id; /* device id for pcmcia board */
69 enum dio700_bustype bustype; /* PCMCIA */
70 int have_dio; /* have daqcard-700 dio */
71 /* function pointers so we can use inb/outb or readb/writeb */
73 unsigned int (*read_byte) (unsigned int address);
74 void (*write_byte) (unsigned int byte, unsigned int address);
77 static const struct dio700_board dio700_boards[] = {
79 .name = "daqcard-700",
80 /* 0x10b is manufacturer id, 0x4743 is device id */
82 .bustype = pcmcia_bustype,
87 /* 0x10b is manufacturer id, 0x4743 is device id */
89 .bustype = pcmcia_bustype,
95 * Useful for shorthand access to the particular board structure
97 #define thisboard ((const struct dio700_board *)dev->board_ptr)
99 struct dio700_private {
101 int data; /* number of data points left to be taken */
104 #define devpriv ((struct dio700_private *)dev->private)
106 static struct comedi_driver driver_dio700 = {
107 .driver_name = "ni_daq_700",
108 .module = THIS_MODULE,
109 .attach = dio700_attach,
110 .detach = dio700_detach,
111 .num_names = ARRAY_SIZE(dio700_boards),
112 .board_name = &dio700_boards[0].name,
113 .offset = sizeof(struct dio700_board),
116 /* the real driver routines */
125 struct subdev_700_struct {
126 unsigned long cb_arg;
127 int (*cb_func) (int, int, int, unsigned long);
131 #define CALLBACK_ARG (((struct subdev_700_struct *)s->private)->cb_arg)
132 #define CALLBACK_FUNC (((struct subdev_700_struct *)s->private)->cb_func)
133 #define subdevpriv ((struct subdev_700_struct *)s->private)
135 static void do_config(struct comedi_device *dev, struct comedi_subdevice *s);
137 void subdev_700_interrupt(struct comedi_device *dev, struct comedi_subdevice *s)
141 d = CALLBACK_FUNC(0, _700_DATA, 0, CALLBACK_ARG);
143 comedi_buf_put(s->async, d);
144 s->async->events |= COMEDI_CB_EOS;
146 comedi_event(dev, s);
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;
155 outb(data, iobase + DIO_W);
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,
167 s->state &= ~data[0];
168 s->state |= (data[0] & data[1]);
171 CALLBACK_FUNC(1, _700_DATA, s->state & 0xff,
175 data[1] = s->state & 0xff;
176 data[1] |= CALLBACK_FUNC(0, _700_DATA, 0, CALLBACK_ARG) << 8;
181 static int subdev_700_insn_config(struct comedi_device *dev,
182 struct comedi_subdevice *s,
183 struct comedi_insn *insn, unsigned int *data)
187 case INSN_CONFIG_DIO_INPUT:
189 case INSN_CONFIG_DIO_OUTPUT:
191 case INSN_CONFIG_DIO_QUERY:
194 io_bits & (1 << CR_CHAN(insn->chanspec))) ? COMEDI_OUTPUT :
205 static void do_config(struct comedi_device *dev, struct comedi_subdevice *s)
206 { /* use powerup defaults */
210 static int subdev_700_cmdtest(struct comedi_device *dev,
211 struct comedi_subdevice *s,
212 struct comedi_cmd *cmd)
219 tmp = cmd->start_src;
220 cmd->start_src &= TRIG_NOW;
221 if (!cmd->start_src || tmp != cmd->start_src)
224 tmp = cmd->scan_begin_src;
225 cmd->scan_begin_src &= TRIG_EXT;
226 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
229 tmp = cmd->convert_src;
230 cmd->convert_src &= TRIG_FOLLOW;
231 if (!cmd->convert_src || tmp != cmd->convert_src)
234 tmp = cmd->scan_end_src;
235 cmd->scan_end_src &= TRIG_COUNT;
236 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
240 cmd->stop_src &= TRIG_NONE;
241 if (!cmd->stop_src || tmp != cmd->stop_src)
254 if (cmd->start_arg != 0) {
258 if (cmd->scan_begin_arg != 0) {
259 cmd->scan_begin_arg = 0;
262 if (cmd->convert_arg != 0) {
263 cmd->convert_arg = 0;
266 if (cmd->scan_end_arg != 1) {
267 cmd->scan_end_arg = 1;
270 if (cmd->stop_arg != 0) {
286 static int subdev_700_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
293 static int subdev_700_cancel(struct comedi_device *dev,
294 struct comedi_subdevice *s)
301 int subdev_700_init(struct comedi_device *dev, struct comedi_subdevice *s,
302 int (*cb) (int, int, int, unsigned long), unsigned long arg)
304 s->type = COMEDI_SUBD_DIO;
305 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
307 s->range_table = &range_digital;
310 s->private = kmalloc(sizeof(struct subdev_700_struct), GFP_KERNEL);
316 CALLBACK_FUNC = subdev_700_cb;
320 s->insn_bits = subdev_700_insn;
321 s->insn_config = subdev_700_insn_config;
330 int subdev_700_init_irq(struct comedi_device *dev, struct comedi_subdevice *s,
331 int (*cb) (int, int, int, unsigned long),
336 ret = subdev_700_init(dev, s, cb, arg);
340 s->do_cmdtest = subdev_700_cmdtest;
341 s->do_cmd = subdev_700_cmd;
342 s->cancel = subdev_700_cancel;
344 subdevpriv->have_irq = 1;
349 void subdev_700_cleanup(struct comedi_device *dev, struct comedi_subdevice *s)
352 if (subdevpriv->have_irq)
357 EXPORT_SYMBOL(subdev_700_init);
358 EXPORT_SYMBOL(subdev_700_init_irq);
359 EXPORT_SYMBOL(subdev_700_cleanup);
360 EXPORT_SYMBOL(subdev_700_interrupt);
362 static int dio700_attach(struct comedi_device *dev, struct comedi_devconfig *it)
364 struct comedi_subdevice *s;
365 unsigned long iobase = 0;
367 unsigned int irq = 0;
369 struct pcmcia_device *link;
371 /* allocate and initialize dev->private */
372 if (alloc_private(dev, sizeof(struct dio700_private)) < 0)
375 /* get base address, irq etc. based on bustype */
376 switch (thisboard->bustype) {
378 link = pcmcia_cur_dev; /* XXX hack */
381 iobase = link->io.BasePort1;
387 printk("bug! couldn't determine board type\n");
391 printk("comedi%d: ni_daq_700: %s, io 0x%lx", dev->minor,
392 thisboard->name, iobase);
395 printk(", irq %u", irq);
402 printk("io base address is zero!\n");
406 dev->iobase = iobase;
413 dev->board_name = thisboard->name;
415 if (alloc_subdevices(dev, 1) < 0)
418 /* DAQCard-700 dio */
419 s = dev->subdevices + 0;
420 subdev_700_init(dev, s, NULL, dev->iobase);
425 static int dio700_detach(struct comedi_device *dev)
427 printk("comedi%d: ni_daq_700: cs-remove\n", dev->minor);
430 subdev_700_cleanup(dev, dev->subdevices + 0);
432 if (thisboard->bustype != pcmcia_bustype && dev->iobase)
433 release_region(dev->iobase, DIO700_SIZE);
435 free_irq(dev->irq, dev);
440 /* PCMCIA crap -- watch your words, please! */
442 static void dio700_config(struct pcmcia_device *link);
443 static void dio700_release(struct pcmcia_device *link);
444 static int dio700_cs_suspend(struct pcmcia_device *p_dev);
445 static int dio700_cs_resume(struct pcmcia_device *p_dev);
448 The attach() and detach() entry points are used to create and destroy
449 "instances" of the driver, where each instance represents everything
450 needed to manage one actual PCMCIA card.
453 static int dio700_cs_attach(struct pcmcia_device *);
454 static void dio700_cs_detach(struct pcmcia_device *);
457 You'll also need to prototype all the functions that will actually
458 be used to talk to your device. See 'memory_cs' for a good example
459 of a fully self-sufficient driver; the other drivers rely more or
460 less on other parts of the kernel.
464 The dev_info variable is the "key" that is used to match up this
465 device driver with appropriate cards, through the card configuration
469 static const dev_info_t dev_info = "ni_daq_700";
471 struct local_info_t {
472 struct pcmcia_device *link;
474 struct bus_operations *bus;
477 /*======================================================================
479 dio700_cs_attach() creates an "instance" of the driver, allocating
480 local data structures for one device. The device is registered
483 The dev_link structure is initialized, but we don't actually
484 configure the card at this point -- we wait until we receive a
485 card insertion event.
487 ======================================================================*/
489 static int dio700_cs_attach(struct pcmcia_device *link)
491 struct local_info_t *local;
493 printk(KERN_INFO "ni_daq_700: cs-attach\n");
495 dev_dbg(&link->dev, "dio700_cs_attach()\n");
497 /* Allocate space for private device-specific data */
498 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
505 General socket configuration defaults can go here. In this
506 client, we assume very little, and rely on the CIS for almost
507 everything. In most clients, many details (i.e., number, sizes,
508 and attributes of IO windows) are fixed by the nature of the
509 device, and can be hard-wired here.
511 link->conf.Attributes = 0;
512 link->conf.IntType = INT_MEMORY_AND_IO;
514 pcmcia_cur_dev = link;
519 } /* dio700_cs_attach */
521 /*======================================================================
523 This deletes a driver "instance". The device is de-registered
524 with Card Services. If it has been released, all local data
525 structures are freed. Otherwise, the structures will be freed
526 when the device is released.
528 ======================================================================*/
530 static void dio700_cs_detach(struct pcmcia_device *link)
533 printk(KERN_INFO "ni_daq_700: cs-detach!\n");
535 dev_dbg(&link->dev, "dio700_cs_detach\n");
537 ((struct local_info_t *)link->priv)->stop = 1;
538 dio700_release(link);
540 /* This points to the parent struct local_info_t struct */
544 } /* dio700_cs_detach */
546 /*======================================================================
548 dio700_config() is scheduled to run after a CARD_INSERTION event
549 is received, to configure the PCMCIA socket, and to make the
550 device available to the system.
552 ======================================================================*/
554 static int dio700_pcmcia_config_loop(struct pcmcia_device *p_dev,
555 cistpl_cftable_entry_t *cfg,
556 cistpl_cftable_entry_t *dflt,
560 win_req_t *req = priv_data;
566 /* Does this card need audio output? */
567 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
568 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
569 p_dev->conf.Status = CCSR_AUDIO_ENA;
572 /* Do we need to allocate an interrupt? */
573 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
575 /* IO window settings */
576 p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
577 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
578 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
579 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
580 if (!(io->flags & CISTPL_IO_8BIT))
581 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
582 if (!(io->flags & CISTPL_IO_16BIT))
583 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
584 p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
585 p_dev->io.BasePort1 = io->win[0].base;
586 p_dev->io.NumPorts1 = io->win[0].len;
588 p_dev->io.Attributes2 = p_dev->io.Attributes1;
589 p_dev->io.BasePort2 = io->win[1].base;
590 p_dev->io.NumPorts2 = io->win[1].len;
592 /* This reserves IO space but doesn't actually enable it */
593 if (pcmcia_request_io(p_dev, &p_dev->io) != 0)
597 if ((cfg->mem.nwin > 0) || (dflt->mem.nwin > 0)) {
599 (cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
600 req->Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
601 req->Attributes |= WIN_ENABLE;
602 req->Base = mem->win[0].host_addr;
603 req->Size = mem->win[0].len;
604 if (req->Size < 0x1000)
606 req->AccessSpeed = 0;
607 if (pcmcia_request_window(p_dev, req, &p_dev->win))
610 map.CardOffset = mem->win[0].card_addr;
611 if (pcmcia_map_mem_page(p_dev, p_dev->win, &map))
614 /* If we got this far, we're cool! */
618 static void dio700_config(struct pcmcia_device *link)
623 printk(KERN_INFO "ni_daq_700: cs-config\n");
625 dev_dbg(&link->dev, "dio700_config\n");
627 ret = pcmcia_loop_config(link, dio700_pcmcia_config_loop, &req);
629 dev_warn(&link->dev, "no configuration found\n");
637 This actually configures the PCMCIA socket -- setting up
638 the I/O windows and the interrupt mapping, and putting the
639 card and host interface into "Memory and IO" mode.
641 ret = pcmcia_request_configuration(link, &link->conf);
645 /* Finally, report what we've done */
646 dev_info(&link->dev, "index 0x%02x", link->conf.ConfigIndex);
647 if (link->conf.Attributes & CONF_ENABLE_IRQ)
648 printk(", irq %d", link->irq);
649 if (link->io.NumPorts1)
650 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
651 link->io.BasePort1 + link->io.NumPorts1 - 1);
652 if (link->io.NumPorts2)
653 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
654 link->io.BasePort2 + link->io.NumPorts2 - 1);
656 printk(", mem 0x%06lx-0x%06lx", req.Base,
657 req.Base + req.Size - 1);
663 printk(KERN_INFO "ni_daq_700 cs failed");
664 dio700_release(link);
666 } /* dio700_config */
668 static void dio700_release(struct pcmcia_device *link)
670 dev_dbg(&link->dev, "dio700_release\n");
672 pcmcia_disable_device(link);
673 } /* dio700_release */
675 /*======================================================================
677 The card status event handler. Mostly, this schedules other
678 stuff to run after an event is received.
680 When a CARD_REMOVAL event is received, we immediately set a
681 private flag to block future accesses to this device. All the
682 functions that actually access the device should check this flag
683 to make sure the card is still present.
685 ======================================================================*/
687 static int dio700_cs_suspend(struct pcmcia_device *link)
689 struct local_info_t *local = link->priv;
691 /* Mark the device as stopped, to block IO until later */
694 } /* dio700_cs_suspend */
696 static int dio700_cs_resume(struct pcmcia_device *link)
698 struct local_info_t *local = link->priv;
702 } /* dio700_cs_resume */
704 /*====================================================================*/
706 static struct pcmcia_device_id dio700_cs_ids[] = {
707 /* N.B. These IDs should match those in dio700_boards */
708 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x4743), /* daqcard-700 */
712 MODULE_LICENSE("GPL");
713 MODULE_DEVICE_TABLE(pcmcia, dio700_cs_ids);
715 struct pcmcia_driver dio700_cs_driver = {
716 .probe = dio700_cs_attach,
717 .remove = dio700_cs_detach,
718 .suspend = dio700_cs_suspend,
719 .resume = dio700_cs_resume,
720 .id_table = dio700_cs_ids,
721 .owner = THIS_MODULE,
727 static int __init init_dio700_cs(void)
729 pcmcia_register_driver(&dio700_cs_driver);
733 static void __exit exit_dio700_cs(void)
735 pr_debug("ni_daq_700: unloading\n");
736 pcmcia_unregister_driver(&dio700_cs_driver);
739 int __init init_module(void)
743 ret = init_dio700_cs();
747 return comedi_driver_register(&driver_dio700);
750 void __exit cleanup_module(void)
753 comedi_driver_unregister(&driver_dio700);