pcmuio.c revision 5f74ea14c07fee91d3bdbaad88bff6264c6200e6
1/*
2    comedi/drivers/pcmuio.c
3    Driver for Winsystems PC-104 based 48-channel and 96-channel DIO boards.
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 2006 Calin A. Culianu <calin@ajvar.org>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22/*
23Driver: pcmuio
24Description: A driver for the PCM-UIO48A and PCM-UIO96A boards from Winsystems.
25Devices: [Winsystems] PCM-UIO48A (pcmuio48), PCM-UIO96A (pcmuio96)
26Author: Calin Culianu <calin@ajvar.org>
27Updated: Fri, 13 Jan 2006 12:01:01 -0500
28Status: works
29
30A driver for the relatively straightforward-to-program PCM-UIO48A and
31PCM-UIO96A boards from Winsystems.  These boards use either one or two
32(in the 96-DIO version) WS16C48 ASIC HighDensity I/O Chips (HDIO).
33This chip is interesting in that each I/O line is individually
34programmable for INPUT or OUTPUT (thus comedi_dio_config can be done
35on a per-channel basis).  Also, each chip supports edge-triggered
36interrupts for the first 24 I/O lines.  Of course, since the
3796-channel version of the board has two ASICs, it can detect polarity
38changes on up to 48 I/O lines.  Since this is essentially an (non-PnP)
39ISA board, I/O Address and IRQ selection are done through jumpers on
40the board.  You need to pass that information to this driver as the
41first and second comedi_config option, respectively.  Note that the
4248-channel version uses 16 bytes of IO memory and the 96-channel
43version uses 32-bytes (in case you are worried about conflicts).  The
4448-channel board is split into two 24-channel comedi subdevices.
45The 96-channel board is split into 4 24-channel DIO subdevices.
46
47Note that IRQ support has been added, but it is untested.
48
49To use edge-detection IRQ support, pass the IRQs of both ASICS
50(for the 96 channel version) or just 1 ASIC (for 48-channel version).
51Then, use use comedi_commands with TRIG_NOW.
52Your callback will be called each time an edge is triggered, and the data
53values will be two sample_t's, which should be concatenated to form one
5432-bit unsigned int.  This value is the mask of channels that had
55edges detected from your channel list.  Note that the bits positions
56in the mask correspond to positions in your chanlist when you specified
57the command and *not* channel id's!
58
59To set the polarity of the edge-detection interrupts pass a nonzero value for
60either CR_RANGE or CR_AREF for edge-up polarity, or a zero value for both
61CR_RANGE and CR_AREF if you want edge-down polarity.
62
63In the 48-channel version:
64
65On subdev 0, the first 24 channels channels are edge-detect channels.
66
67In the 96-channel board you have the collowing channels that can do edge detection:
68
69subdev 0, channels 0-24  (first 24 channels of 1st ASIC)
70subdev 2, channels 0-24  (first 24 channels of 2nd ASIC)
71
72Configuration Options:
73  [0] - I/O port base address
74  [1] - IRQ (for first ASIC, or first 24 channels)
75  [2] - IRQ for second ASIC (pcmuio96 only - IRQ for chans 48-72 .. can be the same as first irq!)
76*/
77
78#include "../comedidev.h"
79
80#include <linux/pci.h>		/* for PCI devices */
81
82#define MIN(a, b) (((a) < (b)) ? (a) : (b))
83#define CHANS_PER_PORT   8
84#define PORTS_PER_ASIC   6
85#define INTR_PORTS_PER_ASIC   3
86#define MAX_CHANS_PER_SUBDEV 24	/* number of channels per comedi subdevice */
87#define PORTS_PER_SUBDEV (MAX_CHANS_PER_SUBDEV/CHANS_PER_PORT)
88#define CHANS_PER_ASIC (CHANS_PER_PORT*PORTS_PER_ASIC)
89#define INTR_CHANS_PER_ASIC 24
90#define INTR_PORTS_PER_SUBDEV (INTR_CHANS_PER_ASIC/CHANS_PER_PORT)
91#define MAX_DIO_CHANS   (PORTS_PER_ASIC*2*CHANS_PER_PORT)
92#define MAX_ASICS       (MAX_DIO_CHANS/CHANS_PER_ASIC)
93#define SDEV_NO ((int)(s - dev->subdevices))
94#define CALC_N_SUBDEVS(nchans) ((nchans)/MAX_CHANS_PER_SUBDEV + (!!((nchans)%MAX_CHANS_PER_SUBDEV)) /*+ (nchans > INTR_CHANS_PER_ASIC ? 2 : 1)*/)
95/* IO Memory sizes */
96#define ASIC_IOSIZE (0x10)
97#define PCMUIO48_IOSIZE ASIC_IOSIZE
98#define PCMUIO96_IOSIZE (ASIC_IOSIZE*2)
99
100/* Some offsets - these are all in the 16byte IO memory offset from
101   the base address.  Note that there is a paging scheme to swap out
102   offsets 0x8-0xA using the PAGELOCK register.  See the table below.
103
104  Register(s)       Pages        R/W?        Description
105  --------------------------------------------------------------
106  REG_PORTx         All          R/W         Read/Write/Configure IO
107  REG_INT_PENDING   All          ReadOnly    Quickly see which INT_IDx has int.
108  REG_PAGELOCK      All          WriteOnly   Select a page
109  REG_POLx          Pg. 1 only   WriteOnly   Select edge-detection polarity
110  REG_ENABx         Pg. 2 only   WriteOnly   Enable/Disable edge-detect. int.
111  REG_INT_IDx       Pg. 3 only   R/W         See which ports/bits have ints.
112 */
113#define REG_PORT0 0x0
114#define REG_PORT1 0x1
115#define REG_PORT2 0x2
116#define REG_PORT3 0x3
117#define REG_PORT4 0x4
118#define REG_PORT5 0x5
119#define REG_INT_PENDING 0x6
120#define REG_PAGELOCK 0x7	/* page selector register, upper 2 bits select a page
121				   and bits 0-5 are used to 'lock down' a particular
122				   port above to make it readonly.  */
123#define REG_POL0 0x8
124#define REG_POL1 0x9
125#define REG_POL2 0xA
126#define REG_ENAB0 0x8
127#define REG_ENAB1 0x9
128#define REG_ENAB2 0xA
129#define REG_INT_ID0 0x8
130#define REG_INT_ID1 0x9
131#define REG_INT_ID2 0xA
132
133#define NUM_PAGED_REGS 3
134#define NUM_PAGES 4
135#define FIRST_PAGED_REG 0x8
136#define REG_PAGE_BITOFFSET 6
137#define REG_LOCK_BITOFFSET 0
138#define REG_PAGE_MASK (~((0x1<<REG_PAGE_BITOFFSET)-1))
139#define REG_LOCK_MASK ~(REG_PAGE_MASK)
140#define PAGE_POL 1
141#define PAGE_ENAB 2
142#define PAGE_INT_ID 3
143
144/*
145 * Board descriptions for two imaginary boards.  Describing the
146 * boards in this way is optional, and completely driver-dependent.
147 * Some drivers use arrays such as this, other do not.
148 */
149struct pcmuio_board {
150	const char *name;
151	const int num_asics;
152	const int num_channels_per_port;
153	const int num_ports;
154};
155
156static const struct pcmuio_board pcmuio_boards[] = {
157	{
158	.name = "pcmuio48",
159	.num_asics = 1,
160	.num_ports = 6,
161		},
162	{
163	.name = "pcmuio96",
164	.num_asics = 2,
165	.num_ports = 12,
166		},
167};
168
169/*
170 * Useful for shorthand access to the particular board structure
171 */
172#define thisboard ((const struct pcmuio_board *)dev->board_ptr)
173
174/* this structure is for data unique to this subdevice.  */
175struct pcmuio_subdev_private {
176	/* mapping of halfwords (bytes) in port/chanarray to iobase */
177	unsigned long iobases[PORTS_PER_SUBDEV];
178
179	/* The below is only used for intr subdevices */
180	struct {
181		int asic;	/* if non-negative, this subdev has an interrupt asic */
182		int first_chan;	/* if nonnegative, the first channel id for
183				   interrupts. */
184		int num_asic_chans;	/* the number of asic channels in this subdev
185					   that have interrutps */
186		int asic_chan;	/* if nonnegative, the first channel id with
187				   respect to the asic that has interrupts */
188		int enabled_mask;	/* subdev-relative channel mask for channels
189					   we are interested in */
190		int active;
191		int stop_count;
192		int continuous;
193		spinlock_t spinlock;
194	} intr;
195};
196
197/* this structure is for data unique to this hardware driver.  If
198   several hardware drivers keep similar information in this structure,
199   feel free to suggest moving the variable to the struct comedi_device struct.  */
200struct pcmuio_private {
201	struct {
202		unsigned char pagelock;	/* current page and lock */
203		unsigned char pol[NUM_PAGED_REGS];	/* shadow of POLx registers */
204		unsigned char enab[NUM_PAGED_REGS];	/* shadow of ENABx registers */
205		int num;
206		unsigned long iobase;
207		unsigned int irq;
208		spinlock_t spinlock;
209	} asics[MAX_ASICS];
210	struct pcmuio_subdev_private *sprivs;
211};
212
213/*
214 * most drivers define the following macro to make it easy to
215 * access the private structure.
216 */
217#define devpriv ((struct pcmuio_private *)dev->private)
218#define subpriv ((struct pcmuio_subdev_private *)s->private)
219/*
220 * The struct comedi_driver structure tells the Comedi core module
221 * which functions to call to configure/deconfigure (attach/detach)
222 * the board, and also about the kernel module that contains
223 * the device code.
224 */
225static int pcmuio_attach(struct comedi_device *dev, struct comedi_devconfig *it);
226static int pcmuio_detach(struct comedi_device *dev);
227
228static struct comedi_driver driver = {
229	.driver_name = "pcmuio",
230	.module = THIS_MODULE,
231	.attach = pcmuio_attach,
232	.detach = pcmuio_detach,
233/* It is not necessary to implement the following members if you are
234 * writing a driver for a ISA PnP or PCI card */
235	/* Most drivers will support multiple types of boards by
236	 * having an array of board structures.  These were defined
237	 * in pcmuio_boards[] above.  Note that the element 'name'
238	 * was first in the structure -- Comedi uses this fact to
239	 * extract the name of the board without knowing any details
240	 * about the structure except for its length.
241	 * When a device is attached (by comedi_config), the name
242	 * of the device is given to Comedi, and Comedi tries to
243	 * match it by going through the list of board names.  If
244	 * there is a match, the address of the pointer is put
245	 * into dev->board_ptr and driver->attach() is called.
246	 *
247	 * Note that these are not necessary if you can determine
248	 * the type of board in software.  ISA PnP, PCI, and PCMCIA
249	 * devices are such boards.
250	 */
251	.board_name = &pcmuio_boards[0].name,
252	.offset = sizeof(struct pcmuio_board),
253	.num_names = ARRAY_SIZE(pcmuio_boards),
254};
255
256static int pcmuio_dio_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
257	struct comedi_insn *insn, unsigned int *data);
258static int pcmuio_dio_insn_config(struct comedi_device *dev, struct comedi_subdevice *s,
259	struct comedi_insn *insn, unsigned int *data);
260
261static irqreturn_t interrupt_pcmuio(int irq, void *d);
262static void pcmuio_stop_intr(struct comedi_device *, struct comedi_subdevice *);
263static int pcmuio_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
264static int pcmuio_cmd(struct comedi_device *dev, struct comedi_subdevice *s);
265static int pcmuio_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
266	struct comedi_cmd *cmd);
267
268/* some helper functions to deal with specifics of this device's registers */
269static void init_asics(struct comedi_device *dev);	/* sets up/clears ASIC chips to defaults */
270static void switch_page(struct comedi_device *dev, int asic, int page);
271#ifdef notused
272static void lock_port(struct comedi_device *dev, int asic, int port);
273static void unlock_port(struct comedi_device *dev, int asic, int port);
274#endif
275
276/*
277 * Attach is called by the Comedi core to configure the driver
278 * for a particular board.  If you specified a board_name array
279 * in the driver structure, dev->board_ptr contains that
280 * address.
281 */
282static int pcmuio_attach(struct comedi_device *dev, struct comedi_devconfig *it)
283{
284	struct comedi_subdevice *s;
285	int sdev_no, chans_left, n_subdevs, port, asic, thisasic_chanct = 0;
286	unsigned long iobase;
287	unsigned int irq[MAX_ASICS];
288
289	iobase = it->options[0];
290	irq[0] = it->options[1];
291	irq[1] = it->options[2];
292
293	printk("comedi%d: %s: io: %lx ", dev->minor, driver.driver_name,
294		iobase);
295
296	dev->iobase = iobase;
297
298	if (!iobase || !request_region(iobase,
299			thisboard->num_asics * ASIC_IOSIZE,
300			driver.driver_name)) {
301		printk("I/O port conflict\n");
302		return -EIO;
303	}
304
305/*
306 * Initialize dev->board_name.  Note that we can use the "thisboard"
307 * macro now, since we just initialized it in the last line.
308 */
309	dev->board_name = thisboard->name;
310
311/*
312 * Allocate the private structure area.  alloc_private() is a
313 * convenient macro defined in comedidev.h.
314 */
315	if (alloc_private(dev, sizeof(struct pcmuio_private)) < 0) {
316		printk("cannot allocate private data structure\n");
317		return -ENOMEM;
318	}
319
320	for (asic = 0; asic < MAX_ASICS; ++asic) {
321		devpriv->asics[asic].num = asic;
322		devpriv->asics[asic].iobase = dev->iobase + asic * ASIC_IOSIZE;
323		devpriv->asics[asic].irq = 0;	/* this gets actually set at the end of
324						   this function when we
325						   request_irqs */
326		spin_lock_init(&devpriv->asics[asic].spinlock);
327	}
328
329	chans_left = CHANS_PER_ASIC * thisboard->num_asics;
330	n_subdevs = CALC_N_SUBDEVS(chans_left);
331	devpriv->sprivs =
332		kcalloc(n_subdevs, sizeof(struct pcmuio_subdev_private), GFP_KERNEL);
333	if (!devpriv->sprivs) {
334		printk("cannot allocate subdevice private data structures\n");
335		return -ENOMEM;
336	}
337	/*
338	 * Allocate the subdevice structures.  alloc_subdevice() is a
339	 * convenient macro defined in comedidev.h.
340	 *
341	 * Allocate 2 subdevs (32 + 16 DIO lines) or 3 32 DIO subdevs for the
342	 * 96-channel version of the board.
343	 */
344	if (alloc_subdevices(dev, n_subdevs) < 0) {
345		printk("cannot allocate subdevice data structures\n");
346		return -ENOMEM;
347	}
348
349	port = 0;
350	asic = 0;
351	for (sdev_no = 0; sdev_no < (int)dev->n_subdevices; ++sdev_no) {
352		int byte_no;
353
354		s = dev->subdevices + sdev_no;
355		s->private = devpriv->sprivs + sdev_no;
356		s->maxdata = 1;
357		s->range_table = &range_digital;
358		s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
359		s->type = COMEDI_SUBD_DIO;
360		s->insn_bits = pcmuio_dio_insn_bits;
361		s->insn_config = pcmuio_dio_insn_config;
362		s->n_chan = MIN(chans_left, MAX_CHANS_PER_SUBDEV);
363		subpriv->intr.asic = -1;
364		subpriv->intr.first_chan = -1;
365		subpriv->intr.asic_chan = -1;
366		subpriv->intr.num_asic_chans = -1;
367		subpriv->intr.active = 0;
368		s->len_chanlist = 1;
369
370		/* save the ioport address for each 'port' of 8 channels in the
371		   subdevice */
372		for (byte_no = 0; byte_no < PORTS_PER_SUBDEV; ++byte_no, ++port) {
373			if (port >= PORTS_PER_ASIC) {
374				port = 0;
375				++asic;
376				thisasic_chanct = 0;
377			}
378			subpriv->iobases[byte_no] =
379				devpriv->asics[asic].iobase + port;
380
381			if (thisasic_chanct <
382				CHANS_PER_PORT * INTR_PORTS_PER_ASIC
383				&& subpriv->intr.asic < 0) {
384				/* this is an interrupt subdevice, so setup the struct */
385				subpriv->intr.asic = asic;
386				subpriv->intr.active = 0;
387				subpriv->intr.stop_count = 0;
388				subpriv->intr.first_chan = byte_no * 8;
389				subpriv->intr.asic_chan = thisasic_chanct;
390				subpriv->intr.num_asic_chans =
391					s->n_chan - subpriv->intr.first_chan;
392				dev->read_subdev = s;
393				s->subdev_flags |= SDF_CMD_READ;
394				s->cancel = pcmuio_cancel;
395				s->do_cmd = pcmuio_cmd;
396				s->do_cmdtest = pcmuio_cmdtest;
397				s->len_chanlist = subpriv->intr.num_asic_chans;
398			}
399			thisasic_chanct += CHANS_PER_PORT;
400		}
401		spin_lock_init(&subpriv->intr.spinlock);
402
403		chans_left -= s->n_chan;
404
405		if (!chans_left) {
406			asic = 0;	/* reset the asic to our first asic, to do intr subdevs */
407			port = 0;
408		}
409
410	}
411
412	init_asics(dev);	/* clear out all the registers, basically */
413
414	for (asic = 0; irq[0] && asic < MAX_ASICS; ++asic) {
415		if (irq[asic]
416			&& request_irq(irq[asic], interrupt_pcmuio,
417				IRQF_SHARED, thisboard->name, dev)) {
418			int i;
419			/* unroll the allocated irqs.. */
420			for (i = asic - 1; i >= 0; --i) {
421				free_irq(irq[i], dev);
422				devpriv->asics[i].irq = irq[i] = 0;
423			}
424			irq[asic] = 0;
425		}
426		devpriv->asics[asic].irq = irq[asic];
427	}
428
429	dev->irq = irq[0];	/* grr.. wish comedi dev struct supported multiple
430				   irqs.. */
431
432	if (irq[0]) {
433		printk("irq: %u ", irq[0]);
434		if (irq[1] && thisboard->num_asics == 2)
435			printk("second ASIC irq: %u ", irq[1]);
436	} else {
437		printk("(IRQ mode disabled) ");
438	}
439
440	printk("attached\n");
441
442	return 1;
443}
444
445/*
446 * _detach is called to deconfigure a device.  It should deallocate
447 * resources.
448 * This function is also called when _attach() fails, so it should be
449 * careful not to release resources that were not necessarily
450 * allocated by _attach().  dev->private and dev->subdevices are
451 * deallocated automatically by the core.
452 */
453static int pcmuio_detach(struct comedi_device *dev)
454{
455	int i;
456
457	printk("comedi%d: %s: remove\n", dev->minor, driver.driver_name);
458	if (dev->iobase)
459		release_region(dev->iobase, ASIC_IOSIZE * thisboard->num_asics);
460
461	for (i = 0; i < MAX_ASICS; ++i) {
462		if (devpriv->asics[i].irq)
463			free_irq(devpriv->asics[i].irq, dev);
464	}
465
466	if (devpriv && devpriv->sprivs)
467		kfree(devpriv->sprivs);
468
469	return 0;
470}
471
472/* DIO devices are slightly special.  Although it is possible to
473 * implement the insn_read/insn_write interface, it is much more
474 * useful to applications if you implement the insn_bits interface.
475 * This allows packed reading/writing of the DIO channels.  The
476 * comedi core can convert between insn_bits and insn_read/write */
477static int pcmuio_dio_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
478	struct comedi_insn *insn, unsigned int *data)
479{
480	int byte_no;
481	if (insn->n != 2)
482		return -EINVAL;
483
484	/* NOTE:
485	   reading a 0 means this channel was high
486	   writine a 0 sets the channel high
487	   reading a 1 means this channel was low
488	   writing a 1 means set this channel low
489
490	   Therefore everything is always inverted. */
491
492	/* The insn data is a mask in data[0] and the new data
493	 * in data[1], each channel cooresponding to a bit. */
494
495#ifdef DAMMIT_ITS_BROKEN
496	/* DEBUG */
497	printk("write mask: %08x  data: %08x\n", data[0], data[1]);
498#endif
499
500	s->state = 0;
501
502	for (byte_no = 0; byte_no < s->n_chan / CHANS_PER_PORT; ++byte_no) {
503		/* address of 8-bit port */
504		unsigned long ioaddr = subpriv->iobases[byte_no],
505			/* bit offset of port in 32-bit doubleword */
506			offset = byte_no * 8;
507		/* this 8-bit port's data */
508		unsigned char byte = 0,
509			/* The write mask for this port (if any) */
510			write_mask_byte = (data[0] >> offset) & 0xff,
511			/* The data byte for this port */
512			data_byte = (data[1] >> offset) & 0xff;
513
514		byte = inb(ioaddr);	/* read all 8-bits for this port */
515
516#ifdef DAMMIT_ITS_BROKEN
517		/* DEBUG */
518		printk("byte %d wmb %02x db %02x offset %02d io %04x, data_in %02x ", byte_no, (unsigned)write_mask_byte, (unsigned)data_byte, offset, ioaddr, (unsigned)byte);
519#endif
520
521		if (write_mask_byte) {
522			/* this byte has some write_bits -- so set the output lines */
523			byte &= ~write_mask_byte;	/* clear bits for write mask */
524			byte |= ~data_byte & write_mask_byte;	/* set to inverted data_byte */
525			/* Write out the new digital output state */
526			outb(byte, ioaddr);
527		}
528#ifdef DAMMIT_ITS_BROKEN
529		/* DEBUG */
530		printk("data_out_byte %02x\n", (unsigned)byte);
531#endif
532		/* save the digital input lines for this byte.. */
533		s->state |= ((unsigned int)byte) << offset;
534	}
535
536	/* now return the DIO lines to data[1] - note they came inverted! */
537	data[1] = ~s->state;
538
539#ifdef DAMMIT_ITS_BROKEN
540	/* DEBUG */
541	printk("s->state %08x data_out %08x\n", s->state, data[1]);
542#endif
543
544	return 2;
545}
546
547/* The input or output configuration of each digital line is
548 * configured by a special insn_config instruction.  chanspec
549 * contains the channel to be changed, and data[0] contains the
550 * value COMEDI_INPUT or COMEDI_OUTPUT. */
551static int pcmuio_dio_insn_config(struct comedi_device *dev, struct comedi_subdevice *s,
552	struct comedi_insn *insn, unsigned int *data)
553{
554	int chan = CR_CHAN(insn->chanspec), byte_no = chan / 8, bit_no =
555		chan % 8;
556	unsigned long ioaddr;
557	unsigned char byte;
558
559	/* Compute ioaddr for this channel */
560	ioaddr = subpriv->iobases[byte_no];
561
562	/* NOTE:
563	   writing a 0 an IO channel's bit sets the channel to INPUT
564	   and pulls the line high as well
565
566	   writing a 1 to an IO channel's  bit pulls the line low
567
568	   All channels are implicitly always in OUTPUT mode -- but when
569	   they are high they can be considered to be in INPUT mode..
570
571	   Thus, we only force channels low if the config request was INPUT,
572	   otherwise we do nothing to the hardware.    */
573
574	switch (data[0]) {
575	case INSN_CONFIG_DIO_OUTPUT:
576		/* save to io_bits -- don't actually do anything since
577		   all input channels are also output channels... */
578		s->io_bits |= 1 << chan;
579		break;
580	case INSN_CONFIG_DIO_INPUT:
581		/* write a 0 to the actual register representing the channel
582		   to set it to 'input'.  0 means "float high". */
583		byte = inb(ioaddr);
584		byte &= ~(1 << bit_no);
585				/**< set input channel to '0' */
586
587		/* write out byte -- this is the only time we actually affect the
588		   hardware as all channels are implicitly output -- but input
589		   channels are set to float-high */
590		outb(byte, ioaddr);
591
592		/* save to io_bits */
593		s->io_bits &= ~(1 << chan);
594		break;
595
596	case INSN_CONFIG_DIO_QUERY:
597		/* retreive from shadow register */
598		data[1] =
599			(s->
600			io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
601		return insn->n;
602		break;
603
604	default:
605		return -EINVAL;
606		break;
607	}
608
609	return insn->n;
610}
611
612static void init_asics(struct comedi_device *dev)
613{				/* sets up an
614				   ASIC chip to defaults */
615	int asic;
616
617	for (asic = 0; asic < thisboard->num_asics; ++asic) {
618		int port, page;
619		unsigned long baseaddr = dev->iobase + asic * ASIC_IOSIZE;
620
621		switch_page(dev, asic, 0);	/* switch back to page 0 */
622
623		/* first, clear all the DIO port bits */
624		for (port = 0; port < PORTS_PER_ASIC; ++port)
625			outb(0, baseaddr + REG_PORT0 + port);
626
627		/* Next, clear all the paged registers for each page */
628		for (page = 1; page < NUM_PAGES; ++page) {
629			int reg;
630			/* now clear all the paged registers */
631			switch_page(dev, asic, page);
632			for (reg = FIRST_PAGED_REG;
633				reg < FIRST_PAGED_REG + NUM_PAGED_REGS; ++reg)
634				outb(0, baseaddr + reg);
635		}
636
637		/* DEBUG  set rising edge interrupts on port0 of both asics */
638		/*switch_page(dev, asic, PAGE_POL);
639		   outb(0xff, baseaddr + REG_POL0);
640		   switch_page(dev, asic, PAGE_ENAB);
641		   outb(0xff, baseaddr + REG_ENAB0); */
642		/* END DEBUG */
643
644		switch_page(dev, asic, 0);	/* switch back to default page 0 */
645
646	}
647}
648
649static void switch_page(struct comedi_device *dev, int asic, int page)
650{
651	if (asic < 0 || asic >= thisboard->num_asics)
652		return;		/* paranoia */
653	if (page < 0 || page >= NUM_PAGES)
654		return;		/* more paranoia */
655
656	devpriv->asics[asic].pagelock &= ~REG_PAGE_MASK;
657	devpriv->asics[asic].pagelock |= page << REG_PAGE_BITOFFSET;
658
659	/* now write out the shadow register */
660	outb(devpriv->asics[asic].pagelock,
661		dev->iobase + ASIC_IOSIZE * asic + REG_PAGELOCK);
662}
663
664#ifdef notused
665static void lock_port(struct comedi_device *dev, int asic, int port)
666{
667	if (asic < 0 || asic >= thisboard->num_asics)
668		return;		/* paranoia */
669	if (port < 0 || port >= PORTS_PER_ASIC)
670		return;		/* more paranoia */
671
672	devpriv->asics[asic].pagelock |= 0x1 << port;
673	/* now write out the shadow register */
674	outb(devpriv->asics[asic].pagelock,
675		dev->iobase + ASIC_IOSIZE * asic + REG_PAGELOCK);
676}
677
678static void unlock_port(struct comedi_device *dev, int asic, int port)
679{
680	if (asic < 0 || asic >= thisboard->num_asics)
681		return;		/* paranoia */
682	if (port < 0 || port >= PORTS_PER_ASIC)
683		return;		/* more paranoia */
684	devpriv->asics[asic].pagelock &= ~(0x1 << port) | REG_LOCK_MASK;
685	/* now write out the shadow register */
686	outb(devpriv->asics[asic].pagelock,
687		dev->iobase + ASIC_IOSIZE * asic + REG_PAGELOCK);
688}
689#endif /* notused */
690
691static irqreturn_t interrupt_pcmuio(int irq, void *d)
692{
693	int asic, got1 = 0;
694	struct comedi_device *dev = (struct comedi_device *) d;
695
696	for (asic = 0; asic < MAX_ASICS; ++asic) {
697		if (irq == devpriv->asics[asic].irq) {
698			unsigned long flags;
699			unsigned triggered = 0;
700			unsigned long iobase = devpriv->asics[asic].iobase;
701			/* it is an interrupt for ASIC #asic */
702			unsigned char int_pend;
703
704			spin_lock_irqsave(&devpriv->asics[asic].spinlock, flags);
705
706			int_pend = inb(iobase + REG_INT_PENDING) & 0x07;
707
708			if (int_pend) {
709				int port;
710				for (port = 0; port < INTR_PORTS_PER_ASIC;
711					++port) {
712					if (int_pend & (0x1 << port)) {
713						unsigned char
714							io_lines_with_edges = 0;
715						switch_page(dev, asic,
716							PAGE_INT_ID);
717						io_lines_with_edges =
718							inb(iobase +
719							REG_INT_ID0 + port);
720
721						if (io_lines_with_edges)
722							/* clear pending interrupt */
723							outb(0, iobase +
724								REG_INT_ID0 +
725								port);
726
727						triggered |=
728							io_lines_with_edges <<
729							port * 8;
730					}
731				}
732
733				++got1;
734			}
735
736			spin_unlock_irqrestore(&devpriv->asics[asic].spinlock, flags);
737
738			if (triggered) {
739				struct comedi_subdevice *s;
740				/* TODO here: dispatch io lines to subdevs with commands.. */
741				printk("PCMUIO DEBUG: got edge detect interrupt %d asic %d which_chans: %06x\n", irq, asic, triggered);
742				for (s = dev->subdevices;
743					s < dev->subdevices + dev->n_subdevices;
744					++s) {
745					if (subpriv->intr.asic == asic) {	/* this is an interrupt subdev, and it matches this asic! */
746						unsigned long flags;
747						unsigned oldevents;
748
749						spin_lock_irqsave (&subpriv->intr.spinlock, flags);
750
751						oldevents = s->async->events;
752
753						if (subpriv->intr.active) {
754							unsigned mytrig =
755								((triggered >>
756									subpriv->
757									intr.
758									asic_chan)
759								& ((0x1 << subpriv->intr.num_asic_chans) - 1)) << subpriv->intr.first_chan;
760							if (mytrig & subpriv->
761								intr.
762								enabled_mask) {
763								unsigned int val =
764									0;
765								unsigned int n,
766									ch, len;
767
768								len = s->async->
769									cmd.
770									chanlist_len;
771								for (n = 0;
772									n < len;
773									n++) {
774									ch = CR_CHAN(s->async->cmd.chanlist[n]);
775									if (mytrig & (1U << ch)) {
776										val |= (1U << n);
777									}
778								}
779								/* Write the scan to the buffer. */
780								if (comedi_buf_put(s->async, ((short *) &val)[0])
781									&&
782									comedi_buf_put
783									(s->async, ((short *) &val)[1])) {
784									s->async->events |= (COMEDI_CB_BLOCK | COMEDI_CB_EOS);
785								} else {
786									/* Overflow! Stop acquisition!! */
787									/* TODO: STOP_ACQUISITION_CALL_HERE!! */
788									pcmuio_stop_intr
789										(dev,
790										s);
791								}
792
793								/* Check for end of acquisition. */
794								if (!subpriv->
795									intr.
796									continuous)
797								{
798									/* stop_src == TRIG_COUNT */
799									if (subpriv->intr.stop_count > 0) {
800										subpriv->
801											intr.
802											stop_count--;
803										if (subpriv->intr.stop_count == 0) {
804											s->async->events |= COMEDI_CB_EOA;
805											/* TODO: STOP_ACQUISITION_CALL_HERE!! */
806											pcmuio_stop_intr
807												(dev,
808												s);
809										}
810									}
811								}
812							}
813						}
814
815						spin_unlock_irqrestore(&subpriv->intr.spinlock, flags);
816
817						if (oldevents !=
818							s->async->events) {
819							comedi_event(dev, s);
820						}
821
822					}
823
824				}
825			}
826
827		}
828	}
829	if (!got1)
830		return IRQ_NONE;	/* interrupt from other source */
831	return IRQ_HANDLED;
832}
833
834static void pcmuio_stop_intr(struct comedi_device *dev, struct comedi_subdevice *s)
835{
836	int nports, firstport, asic, port;
837
838	asic = subpriv->intr.asic;
839	if (asic < 0)
840		return;		/* not an interrupt subdev */
841
842	subpriv->intr.enabled_mask = 0;
843	subpriv->intr.active = 0;
844	s->async->inttrig = 0;
845	nports = subpriv->intr.num_asic_chans / CHANS_PER_PORT;
846	firstport = subpriv->intr.asic_chan / CHANS_PER_PORT;
847	switch_page(dev, asic, PAGE_ENAB);
848	for (port = firstport; port < firstport + nports; ++port) {
849		/* disable all intrs for this subdev.. */
850		outb(0, devpriv->asics[asic].iobase + REG_ENAB0 + port);
851	}
852}
853
854static int pcmuio_start_intr(struct comedi_device *dev, struct comedi_subdevice *s)
855{
856	if (!subpriv->intr.continuous && subpriv->intr.stop_count == 0) {
857		/* An empty acquisition! */
858		s->async->events |= COMEDI_CB_EOA;
859		subpriv->intr.active = 0;
860		return 1;
861	} else {
862		unsigned bits = 0, pol_bits = 0, n;
863		int nports, firstport, asic, port;
864		struct comedi_cmd *cmd = &s->async->cmd;
865
866		asic = subpriv->intr.asic;
867		if (asic < 0)
868			return 1;	/* not an interrupt
869					   subdev */
870		subpriv->intr.enabled_mask = 0;
871		subpriv->intr.active = 1;
872		nports = subpriv->intr.num_asic_chans / CHANS_PER_PORT;
873		firstport = subpriv->intr.asic_chan / CHANS_PER_PORT;
874		if (cmd->chanlist) {
875			for (n = 0; n < cmd->chanlist_len; n++) {
876				bits |= (1U << CR_CHAN(cmd->chanlist[n]));
877				pol_bits |= (CR_AREF(cmd->chanlist[n])
878					|| CR_RANGE(cmd->chanlist[n]) ? 1U : 0U)
879					<< CR_CHAN(cmd->chanlist[n]);
880			}
881		}
882		bits &= ((0x1 << subpriv->intr.num_asic_chans) -
883			1) << subpriv->intr.first_chan;
884		subpriv->intr.enabled_mask = bits;
885
886		switch_page(dev, asic, PAGE_ENAB);
887		for (port = firstport; port < firstport + nports; ++port) {
888			unsigned enab =
889				bits >> (subpriv->intr.first_chan + (port -
890					firstport) * 8) & 0xff, pol =
891				pol_bits >> (subpriv->intr.first_chan + (port -
892					firstport) * 8) & 0xff;
893			/* set enab intrs for this subdev.. */
894			outb(enab,
895				devpriv->asics[asic].iobase + REG_ENAB0 + port);
896			switch_page(dev, asic, PAGE_POL);
897			outb(pol,
898				devpriv->asics[asic].iobase + REG_ENAB0 + port);
899		}
900	}
901	return 0;
902}
903
904static int pcmuio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
905{
906	unsigned long flags;
907
908	spin_lock_irqsave(&subpriv->intr.spinlock, flags);
909	if (subpriv->intr.active)
910		pcmuio_stop_intr(dev, s);
911	spin_unlock_irqrestore(&subpriv->intr.spinlock, flags);
912
913	return 0;
914}
915
916/*
917 * Internal trigger function to start acquisition for an 'INTERRUPT' subdevice.
918 */
919static int
920pcmuio_inttrig_start_intr(struct comedi_device *dev, struct comedi_subdevice *s,
921	unsigned int trignum)
922{
923	unsigned long flags;
924	int event = 0;
925
926	if (trignum != 0)
927		return -EINVAL;
928
929	spin_lock_irqsave(&subpriv->intr.spinlock, flags);
930	s->async->inttrig = 0;
931	if (subpriv->intr.active) {
932		event = pcmuio_start_intr(dev, s);
933	}
934	spin_unlock_irqrestore(&subpriv->intr.spinlock, flags);
935
936	if (event) {
937		comedi_event(dev, s);
938	}
939
940	return 1;
941}
942
943/*
944 * 'do_cmd' function for an 'INTERRUPT' subdevice.
945 */
946static int pcmuio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
947{
948	struct comedi_cmd *cmd = &s->async->cmd;
949	unsigned long flags;
950	int event = 0;
951
952	spin_lock_irqsave(&subpriv->intr.spinlock, flags);
953	subpriv->intr.active = 1;
954
955	/* Set up end of acquisition. */
956	switch (cmd->stop_src) {
957	case TRIG_COUNT:
958		subpriv->intr.continuous = 0;
959		subpriv->intr.stop_count = cmd->stop_arg;
960		break;
961	default:
962		/* TRIG_NONE */
963		subpriv->intr.continuous = 1;
964		subpriv->intr.stop_count = 0;
965		break;
966	}
967
968	/* Set up start of acquisition. */
969	switch (cmd->start_src) {
970	case TRIG_INT:
971		s->async->inttrig = pcmuio_inttrig_start_intr;
972		break;
973	default:
974		/* TRIG_NOW */
975		event = pcmuio_start_intr(dev, s);
976		break;
977	}
978	spin_unlock_irqrestore(&subpriv->intr.spinlock, flags);
979
980	if (event) {
981		comedi_event(dev, s);
982	}
983
984	return 0;
985}
986
987/*
988 * 'do_cmdtest' function for an 'INTERRUPT' subdevice.
989 */
990static int
991pcmuio_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s, struct comedi_cmd *cmd)
992{
993	int err = 0;
994	unsigned int tmp;
995
996	/* step 1: make sure trigger sources are trivially valid */
997
998	tmp = cmd->start_src;
999	cmd->start_src &= (TRIG_NOW | TRIG_INT);
1000	if (!cmd->start_src || tmp != cmd->start_src)
1001		err++;
1002
1003	tmp = cmd->scan_begin_src;
1004	cmd->scan_begin_src &= TRIG_EXT;
1005	if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1006		err++;
1007
1008	tmp = cmd->convert_src;
1009	cmd->convert_src &= TRIG_NOW;
1010	if (!cmd->convert_src || tmp != cmd->convert_src)
1011		err++;
1012
1013	tmp = cmd->scan_end_src;
1014	cmd->scan_end_src &= TRIG_COUNT;
1015	if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1016		err++;
1017
1018	tmp = cmd->stop_src;
1019	cmd->stop_src &= (TRIG_COUNT | TRIG_NONE);
1020	if (!cmd->stop_src || tmp != cmd->stop_src)
1021		err++;
1022
1023	if (err)
1024		return 1;
1025
1026	/* step 2: make sure trigger sources are unique and mutually compatible */
1027
1028	/* these tests are true if more than one _src bit is set */
1029	if ((cmd->start_src & (cmd->start_src - 1)) != 0)
1030		err++;
1031	if ((cmd->scan_begin_src & (cmd->scan_begin_src - 1)) != 0)
1032		err++;
1033	if ((cmd->convert_src & (cmd->convert_src - 1)) != 0)
1034		err++;
1035	if ((cmd->scan_end_src & (cmd->scan_end_src - 1)) != 0)
1036		err++;
1037	if ((cmd->stop_src & (cmd->stop_src - 1)) != 0)
1038		err++;
1039
1040	if (err)
1041		return 2;
1042
1043	/* step 3: make sure arguments are trivially compatible */
1044
1045	/* cmd->start_src == TRIG_NOW || cmd->start_src == TRIG_INT */
1046	if (cmd->start_arg != 0) {
1047		cmd->start_arg = 0;
1048		err++;
1049	}
1050
1051	/* cmd->scan_begin_src == TRIG_EXT */
1052	if (cmd->scan_begin_arg != 0) {
1053		cmd->scan_begin_arg = 0;
1054		err++;
1055	}
1056
1057	/* cmd->convert_src == TRIG_NOW */
1058	if (cmd->convert_arg != 0) {
1059		cmd->convert_arg = 0;
1060		err++;
1061	}
1062
1063	/* cmd->scan_end_src == TRIG_COUNT */
1064	if (cmd->scan_end_arg != cmd->chanlist_len) {
1065		cmd->scan_end_arg = cmd->chanlist_len;
1066		err++;
1067	}
1068
1069	switch (cmd->stop_src) {
1070	case TRIG_COUNT:
1071		/* any count allowed */
1072		break;
1073	case TRIG_NONE:
1074		if (cmd->stop_arg != 0) {
1075			cmd->stop_arg = 0;
1076			err++;
1077		}
1078		break;
1079	default:
1080		break;
1081	}
1082
1083	if (err)
1084		return 3;
1085
1086	/* step 4: fix up any arguments */
1087
1088	/* if (err) return 4; */
1089
1090	return 0;
1091}
1092
1093/*
1094 * A convenient macro that defines init_module() and cleanup_module(),
1095 * as necessary.
1096 */
1097COMEDI_INITCLEANUP(driver);
1098