5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1998 David A. Schleef <ds@schleef.org>
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.
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.
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.
25 Description: generic 8255 support
26 Devices: [standard] 8255 (8255)
29 Updated: Fri, 7 Jun 2002 12:56:45 -0700
31 The classic in digital I/O. The 8255 appears in Comedi as a single
32 digital I/O subdevice with 24 channels. The channel 0 corresponds
33 to the 8255's port A, bit 0; channel 23 corresponds to port C, bit
34 7. Direction configuration is done in blocks, with channels 0-7,
35 8-15, 16-19, and 20-23 making up the 4 blocks. The only 8255 mode
38 You should enable compilation this driver if you plan to use a board
39 that has an 8255 chip. For multifunction boards, the main driver will
40 configure the 8255 subdevice automatically.
42 This driver also works independently with ISA and PCI cards that
43 directly map the 8255 registers to I/O ports, including cards with
44 multiple 8255 chips. To configure the driver for such a card, the
45 option list should be a list of the I/O port bases for each of the
46 8255 chips. For example,
48 comedi_config /dev/comedi0 8255 0x200,0x204,0x208,0x20c
50 Note that most PCI 8255 boards do NOT work with this driver, and
51 need a separate driver as a wrapper. For those that do work, the
52 I/O port base address can be found in the output of 'lspci -v'.
57 This file contains an exported subdevice for driving an 8255.
59 To use this subdevice as part of another driver, you need to
60 set up the subdevice in the attach function of the driver by
63 subdev_8255_init(device, subdevice, io_function, iobase)
65 device and subdevice are pointers to the device and subdevice
66 structures. io_function will be called to provide the
67 low-level input/output to the device, i.e., actual register
68 access. io_function will be called with the value of iobase
69 as the last parameter. If the 8255 device is mapped as 4
70 consecutive I/O ports, you can use NULL for io_function
71 and the I/O port base for iobase, and an internal function will
72 handle the register access.
74 In addition, if the main driver handles interrupts, you can
75 enable commands on the subdevice by calling subdev_8255_init_irq()
76 instead. Then, when you get an interrupt that is likely to be
77 from the 8255, you should call subdev_8255_interrupt(), which
78 will copy the latched value to a Comedi buffer.
81 #include "../comedidev.h"
83 #include <linux/ioport.h>
84 #include <linux/slab.h>
86 #include "comedi_fc.h"
94 #define CR_C_LO_IO 0x01
96 #define CR_B_MODE 0x04
97 #define CR_C_HI_IO 0x08
99 #define CR_A_MODE(a) ((a)<<5)
102 struct subdev_8255_private {
103 unsigned long iobase;
104 int (*io) (int, int, int, unsigned long);
107 static int subdev_8255_io(int dir, int port, int data, unsigned long iobase)
110 outb(data, iobase + port);
113 return inb(iobase + port);
117 void subdev_8255_interrupt(struct comedi_device *dev,
118 struct comedi_subdevice *s)
120 struct subdev_8255_private *spriv = s->private;
121 unsigned long iobase = spriv->iobase;
124 d = spriv->io(0, _8255_DATA, 0, iobase);
125 d |= (spriv->io(0, _8255_DATA + 1, 0, iobase) << 8);
127 comedi_buf_put(s->async, d);
128 s->async->events |= COMEDI_CB_EOS;
130 comedi_event(dev, s);
132 EXPORT_SYMBOL_GPL(subdev_8255_interrupt);
134 static int subdev_8255_insn(struct comedi_device *dev,
135 struct comedi_subdevice *s,
136 struct comedi_insn *insn, unsigned int *data)
138 struct subdev_8255_private *spriv = s->private;
139 unsigned long iobase = spriv->iobase;
153 spriv->io(1, _8255_DATA, v & 0xff, iobase);
155 spriv->io(1, _8255_DATA + 1, (v >> 8) & 0xff, iobase);
157 spriv->io(1, _8255_DATA + 2, (v >> 16) & 0xff, iobase);
162 v = spriv->io(0, _8255_DATA, 0, iobase);
163 v |= (spriv->io(0, _8255_DATA + 1, 0, iobase) << 8);
164 v |= (spriv->io(0, _8255_DATA + 2, 0, iobase) << 16);
171 static void subdev_8255_do_config(struct comedi_device *dev,
172 struct comedi_subdevice *s)
174 struct subdev_8255_private *spriv = s->private;
175 unsigned long iobase = spriv->iobase;
179 /* 1 in io_bits indicates output, 1 in config indicates input */
180 if (!(s->io_bits & 0x0000ff))
182 if (!(s->io_bits & 0x00ff00))
184 if (!(s->io_bits & 0x0f0000))
185 config |= CR_C_LO_IO;
186 if (!(s->io_bits & 0xf00000))
187 config |= CR_C_HI_IO;
189 spriv->io(1, _8255_CR, config, iobase);
192 static int subdev_8255_insn_config(struct comedi_device *dev,
193 struct comedi_subdevice *s,
194 struct comedi_insn *insn, unsigned int *data)
199 mask = 1 << CR_CHAN(insn->chanspec);
202 else if (mask & 0x00ff00)
204 else if (mask & 0x0f0000)
210 case INSN_CONFIG_DIO_INPUT:
213 case INSN_CONFIG_DIO_OUTPUT:
216 case INSN_CONFIG_DIO_QUERY:
217 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
224 subdev_8255_do_config(dev, s);
229 static int subdev_8255_cmdtest(struct comedi_device *dev,
230 struct comedi_subdevice *s,
231 struct comedi_cmd *cmd)
235 /* Step 1 : check if triggers are trivially valid */
237 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
238 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
239 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
240 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
241 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_NONE);
246 /* Step 2a : make sure trigger sources are unique */
247 /* Step 2b : and mutually compatible */
252 /* Step 3: check if arguments are trivially valid */
254 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
255 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
256 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
257 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, 1);
258 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
271 static int subdev_8255_cmd(struct comedi_device *dev,
272 struct comedi_subdevice *s)
279 static int subdev_8255_cancel(struct comedi_device *dev,
280 struct comedi_subdevice *s)
287 int subdev_8255_init(struct comedi_device *dev, struct comedi_subdevice *s,
288 int (*io) (int, int, int, unsigned long),
289 unsigned long iobase)
291 struct subdev_8255_private *spriv;
293 spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
297 spriv->iobase = iobase;
298 spriv->io = io ? io : subdev_8255_io;
302 s->type = COMEDI_SUBD_DIO;
303 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
305 s->range_table = &range_digital;
307 s->insn_bits = subdev_8255_insn;
308 s->insn_config = subdev_8255_insn_config;
313 subdev_8255_do_config(dev, s);
317 EXPORT_SYMBOL_GPL(subdev_8255_init);
319 int subdev_8255_init_irq(struct comedi_device *dev, struct comedi_subdevice *s,
320 int (*io) (int, int, int, unsigned long),
321 unsigned long iobase)
325 ret = subdev_8255_init(dev, s, io, iobase);
329 s->do_cmdtest = subdev_8255_cmdtest;
330 s->do_cmd = subdev_8255_cmd;
331 s->cancel = subdev_8255_cancel;
335 EXPORT_SYMBOL_GPL(subdev_8255_init_irq);
339 Start of the 8255 standalone device
343 static int dev_8255_attach(struct comedi_device *dev,
344 struct comedi_devconfig *it)
346 struct comedi_subdevice *s;
348 unsigned long iobase;
351 for (i = 0; i < COMEDI_NDEVCONFOPTS; i++) {
352 iobase = it->options[i];
357 dev_warn(dev->class_dev, "no devices specified\n");
361 ret = comedi_alloc_subdevices(dev, i);
365 for (i = 0; i < dev->n_subdevices; i++) {
366 s = &dev->subdevices[i];
367 iobase = it->options[i];
369 ret = __comedi_request_region(dev, iobase, _8255_SIZE);
371 s->type = COMEDI_SUBD_UNUSED;
373 ret = subdev_8255_init(dev, s, NULL, iobase);
382 static void dev_8255_detach(struct comedi_device *dev)
384 struct comedi_subdevice *s;
385 struct subdev_8255_private *spriv;
388 for (i = 0; i < dev->n_subdevices; i++) {
389 s = &dev->subdevices[i];
390 if (s->type != COMEDI_SUBD_UNUSED) {
392 release_region(spriv->iobase, _8255_SIZE);
394 comedi_spriv_free(dev, i);
398 static struct comedi_driver dev_8255_driver = {
399 .driver_name = "8255",
400 .module = THIS_MODULE,
401 .attach = dev_8255_attach,
402 .detach = dev_8255_detach,
404 module_comedi_driver(dev_8255_driver);
406 MODULE_AUTHOR("Comedi http://www.comedi.org");
407 MODULE_DESCRIPTION("Comedi low-level driver");
408 MODULE_LICENSE("GPL");