3 * A Comedi driver to 'bond' or merge multiple drivers and devices as one.
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 * Copyright (C) 2005 Calin A. Culianu <calin@ajvar.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.
22 * Description: A driver to 'bond' (merge) multiple subdevices from multiple
23 * devices together as one.
26 * Updated: Mon, 10 Oct 00:18:25 -0500
29 * This driver allows you to 'bond' (merge) multiple comedi subdevices
30 * (coming from possibly difference boards and/or drivers) together. For
31 * example, if you had a board with 2 different DIO subdevices, and
32 * another with 1 DIO subdevice, you could 'bond' them with this driver
33 * so that they look like one big fat DIO subdevice. This makes writing
34 * applications slightly easier as you don't have to worry about managing
35 * different subdevices in the application -- you just worry about
36 * indexing one linear array of channel id's.
38 * Right now only DIO subdevices are supported as that's the personal itch
39 * I am scratching with this driver. If you want to add support for AI and AO
40 * subdevs, go right on ahead and do so!
42 * Commands aren't supported -- although it would be cool if they were.
44 * Configuration Options:
45 * List of comedi-minors to bond. All subdevices of the same type
46 * within each minor will be concatenated together in the order given here.
49 #include <linux/module.h>
50 #include <linux/string.h>
51 #include <linux/slab.h>
52 #include "../comedi.h"
53 #include "../comedilib.h"
54 #include "../comedidev.h"
56 struct bonded_device {
57 struct comedi_device *dev;
63 struct comedi_bond_private {
65 struct bonded_device **devs;
70 static int bonding_dio_insn_bits(struct comedi_device *dev,
71 struct comedi_subdevice *s,
72 struct comedi_insn *insn, unsigned int *data)
74 struct comedi_bond_private *devpriv = dev->private;
75 unsigned int n_left, n_done, base_chan;
76 unsigned int write_mask, data_bits;
77 struct bonded_device **devs;
81 base_chan = CR_CHAN(insn->chanspec);
82 /* do a maximum of 32 channels, starting from base_chan. */
83 n_left = devpriv->nchans - base_chan;
90 struct bonded_device *bdev = *devs++;
92 if (base_chan < bdev->nchans) {
93 /* base channel falls within bonded device */
94 unsigned int b_chans, b_mask, b_write_mask, b_data_bits;
98 * Get num channels to do for bonded device and set
99 * up mask and data bits for bonded device.
101 b_chans = bdev->nchans - base_chan;
102 if (b_chans > n_left)
104 b_mask = (1U << b_chans) - 1;
105 b_write_mask = (write_mask >> n_done) & b_mask;
106 b_data_bits = (data_bits >> n_done) & b_mask;
107 /* Read/Write the new digital lines. */
108 ret = comedi_dio_bitfield2(bdev->dev, bdev->subdev,
109 b_write_mask, &b_data_bits,
113 /* Place read bits into data[1]. */
114 data[1] &= ~(b_mask << n_done);
115 data[1] |= (b_data_bits & b_mask) << n_done;
117 * Set up for following bonded device (if still have
118 * channels to read/write).
124 /* Skip bonded devices before base channel. */
125 base_chan -= bdev->nchans;
132 static int bonding_dio_insn_config(struct comedi_device *dev,
133 struct comedi_subdevice *s,
134 struct comedi_insn *insn, unsigned int *data)
136 struct comedi_bond_private *devpriv = dev->private;
137 unsigned int chan = CR_CHAN(insn->chanspec);
139 struct bonded_device *bdev;
140 struct bonded_device **devs;
143 * Locate bonded subdevice and adjust channel.
145 devs = devpriv->devs;
146 for (bdev = *devs++; chan >= bdev->nchans; bdev = *devs++)
147 chan -= bdev->nchans;
150 * The input or output configuration of each digital line is
151 * configured by a special insn_config instruction. chanspec
152 * contains the channel to be changed, and data[0] contains the
153 * configuration instruction INSN_CONFIG_DIO_OUTPUT,
154 * INSN_CONFIG_DIO_INPUT or INSN_CONFIG_DIO_QUERY.
156 * Note that INSN_CONFIG_DIO_OUTPUT == COMEDI_OUTPUT,
157 * and INSN_CONFIG_DIO_INPUT == COMEDI_INPUT. This is deliberate ;)
160 case INSN_CONFIG_DIO_OUTPUT:
161 case INSN_CONFIG_DIO_INPUT:
162 ret = comedi_dio_config(bdev->dev, bdev->subdev, chan, data[0]);
164 case INSN_CONFIG_DIO_QUERY:
165 ret = comedi_dio_get_config(bdev->dev, bdev->subdev, chan,
177 static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
179 struct comedi_bond_private *devpriv = dev->private;
180 DECLARE_BITMAP(devs_opened, COMEDI_NUM_BOARD_MINORS);
183 memset(&devs_opened, 0, sizeof(devs_opened));
184 devpriv->name[0] = 0;
186 * Loop through all comedi devices specified on the command-line,
187 * building our device list.
189 for (i = 0; i < COMEDI_NDEVCONFOPTS && (!i || it->options[i]); ++i) {
190 char file[sizeof("/dev/comediXXXXXX")];
191 int minor = it->options[i];
192 struct comedi_device *d;
193 int sdev = -1, nchans;
194 struct bonded_device *bdev;
195 struct bonded_device **devs;
197 if (minor < 0 || minor >= COMEDI_NUM_BOARD_MINORS) {
198 dev_err(dev->class_dev,
199 "Minor %d is invalid!\n", minor);
202 if (minor == dev->minor) {
203 dev_err(dev->class_dev,
204 "Cannot bond this driver to itself!\n");
207 if (test_and_set_bit(minor, devs_opened)) {
208 dev_err(dev->class_dev,
209 "Minor %d specified more than once!\n", minor);
213 snprintf(file, sizeof(file), "/dev/comedi%d", minor);
214 file[sizeof(file) - 1] = 0;
216 d = comedi_open(file);
219 dev_err(dev->class_dev,
220 "Minor %u could not be opened\n", minor);
224 /* Do DIO, as that's all we support now.. */
225 while ((sdev = comedi_find_subdevice_by_type(d, COMEDI_SUBD_DIO,
227 nchans = comedi_get_n_channels(d, sdev);
229 dev_err(dev->class_dev,
230 "comedi_get_n_channels() returned %d on minor %u subdev %d!\n",
231 nchans, minor, sdev);
234 bdev = kmalloc(sizeof(*bdev), GFP_KERNEL);
241 bdev->nchans = nchans;
242 devpriv->nchans += nchans;
245 * Now put bdev pointer at end of devpriv->devs array
249 /* ergh.. ugly.. we need to realloc :( */
250 devs = krealloc(devpriv->devs,
251 (devpriv->ndevs + 1) * sizeof(*devs),
254 dev_err(dev->class_dev,
255 "Could not allocate memory. Out of memory?\n");
259 devpriv->devs = devs;
260 devpriv->devs[devpriv->ndevs++] = bdev;
262 /* Append dev:subdev to devpriv->name */
265 snprintf(buf, sizeof(buf), "%u:%u ",
266 bdev->minor, bdev->subdev);
267 strlcat(devpriv->name, buf,
268 sizeof(devpriv->name));
273 if (!devpriv->nchans) {
274 dev_err(dev->class_dev, "No channels found!\n");
281 static int bonding_attach(struct comedi_device *dev,
282 struct comedi_devconfig *it)
284 struct comedi_bond_private *devpriv;
285 struct comedi_subdevice *s;
288 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
293 * Setup our bonding from config params.. sets up our private struct..
295 ret = do_dev_config(dev, it);
299 dev->board_name = devpriv->name;
301 ret = comedi_alloc_subdevices(dev, 1);
305 s = &dev->subdevices[0];
306 s->type = COMEDI_SUBD_DIO;
307 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
308 s->n_chan = devpriv->nchans;
310 s->range_table = &range_digital;
311 s->insn_bits = bonding_dio_insn_bits;
312 s->insn_config = bonding_dio_insn_config;
314 dev_info(dev->class_dev,
315 "%s: %s attached, %u channels from %u devices\n",
316 dev->driver->driver_name, dev->board_name,
317 devpriv->nchans, devpriv->ndevs);
322 static void bonding_detach(struct comedi_device *dev)
324 struct comedi_bond_private *devpriv = dev->private;
326 if (devpriv && devpriv->devs) {
327 DECLARE_BITMAP(devs_closed, COMEDI_NUM_BOARD_MINORS);
329 memset(&devs_closed, 0, sizeof(devs_closed));
330 while (devpriv->ndevs--) {
331 struct bonded_device *bdev;
333 bdev = devpriv->devs[devpriv->ndevs];
336 if (!test_and_set_bit(bdev->minor, devs_closed))
337 comedi_close(bdev->dev);
340 kfree(devpriv->devs);
341 devpriv->devs = NULL;
345 static struct comedi_driver bonding_driver = {
346 .driver_name = "comedi_bond",
347 .module = THIS_MODULE,
348 .attach = bonding_attach,
349 .detach = bonding_detach,
351 module_comedi_driver(bonding_driver);
353 MODULE_AUTHOR("Calin A. Culianu");
354 MODULE_DESCRIPTION("comedi_bond: A driver for COMEDI to bond multiple COMEDI devices together as one.");
355 MODULE_LICENSE("GPL");