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 {
64 # define MAX_BOARD_NAME 256
65 char name[MAX_BOARD_NAME];
66 struct bonded_device **devs;
71 static int bonding_dio_insn_bits(struct comedi_device *dev,
72 struct comedi_subdevice *s,
73 struct comedi_insn *insn, unsigned int *data)
75 struct comedi_bond_private *devpriv = dev->private;
76 unsigned int n_left, n_done, base_chan;
77 unsigned int write_mask, data_bits;
78 struct bonded_device **devs;
82 base_chan = CR_CHAN(insn->chanspec);
83 /* do a maximum of 32 channels, starting from base_chan. */
84 n_left = devpriv->nchans - base_chan;
91 struct bonded_device *bdev = *devs++;
93 if (base_chan < bdev->nchans) {
94 /* base channel falls within bonded device */
95 unsigned int b_chans, b_mask, b_write_mask, b_data_bits;
99 * Get num channels to do for bonded device and set
100 * up mask and data bits for bonded device.
102 b_chans = bdev->nchans - base_chan;
103 if (b_chans > n_left)
105 b_mask = (1U << b_chans) - 1;
106 b_write_mask = (write_mask >> n_done) & b_mask;
107 b_data_bits = (data_bits >> n_done) & b_mask;
108 /* Read/Write the new digital lines. */
109 ret = comedi_dio_bitfield2(bdev->dev, bdev->subdev,
110 b_write_mask, &b_data_bits,
114 /* Place read bits into data[1]. */
115 data[1] &= ~(b_mask << n_done);
116 data[1] |= (b_data_bits & b_mask) << n_done;
118 * Set up for following bonded device (if still have
119 * channels to read/write).
125 /* Skip bonded devices before base channel. */
126 base_chan -= bdev->nchans;
133 static int bonding_dio_insn_config(struct comedi_device *dev,
134 struct comedi_subdevice *s,
135 struct comedi_insn *insn, unsigned int *data)
137 struct comedi_bond_private *devpriv = dev->private;
138 unsigned int chan = CR_CHAN(insn->chanspec);
140 struct bonded_device *bdev;
141 struct bonded_device **devs;
144 * Locate bonded subdevice and adjust channel.
146 devs = devpriv->devs;
147 for (bdev = *devs++; chan >= bdev->nchans; bdev = *devs++)
148 chan -= bdev->nchans;
151 * The input or output configuration of each digital line is
152 * configured by a special insn_config instruction. chanspec
153 * contains the channel to be changed, and data[0] contains the
154 * configuration instruction INSN_CONFIG_DIO_OUTPUT,
155 * INSN_CONFIG_DIO_INPUT or INSN_CONFIG_DIO_QUERY.
157 * Note that INSN_CONFIG_DIO_OUTPUT == COMEDI_OUTPUT,
158 * and INSN_CONFIG_DIO_INPUT == COMEDI_INPUT. This is deliberate ;)
161 case INSN_CONFIG_DIO_OUTPUT:
162 case INSN_CONFIG_DIO_INPUT:
163 ret = comedi_dio_config(bdev->dev, bdev->subdev, chan, data[0]);
165 case INSN_CONFIG_DIO_QUERY:
166 ret = comedi_dio_get_config(bdev->dev, bdev->subdev, chan,
178 static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
180 struct comedi_bond_private *devpriv = dev->private;
181 DECLARE_BITMAP(devs_opened, COMEDI_NUM_BOARD_MINORS);
184 memset(&devs_opened, 0, sizeof(devs_opened));
185 devpriv->name[0] = 0;
187 * Loop through all comedi devices specified on the command-line,
188 * building our device list.
190 for (i = 0; i < COMEDI_NDEVCONFOPTS && (!i || it->options[i]); ++i) {
191 char file[sizeof("/dev/comediXXXXXX")];
192 int minor = it->options[i];
193 struct comedi_device *d;
194 int sdev = -1, nchans;
195 struct bonded_device *bdev;
196 struct bonded_device **devs;
198 if (minor < 0 || minor >= COMEDI_NUM_BOARD_MINORS) {
199 dev_err(dev->class_dev,
200 "Minor %d is invalid!\n", minor);
203 if (minor == dev->minor) {
204 dev_err(dev->class_dev,
205 "Cannot bond this driver to itself!\n");
208 if (test_and_set_bit(minor, devs_opened)) {
209 dev_err(dev->class_dev,
210 "Minor %d specified more than once!\n", minor);
214 snprintf(file, sizeof(file), "/dev/comedi%u", minor);
215 file[sizeof(file) - 1] = 0;
217 d = comedi_open(file);
220 dev_err(dev->class_dev,
221 "Minor %u could not be opened\n", minor);
225 /* Do DIO, as that's all we support now.. */
226 while ((sdev = comedi_find_subdevice_by_type(d, COMEDI_SUBD_DIO,
228 nchans = comedi_get_n_channels(d, sdev);
230 dev_err(dev->class_dev,
231 "comedi_get_n_channels() returned %d on minor %u subdev %d!\n",
232 nchans, minor, sdev);
235 bdev = kmalloc(sizeof(*bdev), GFP_KERNEL);
242 bdev->nchans = nchans;
243 devpriv->nchans += nchans;
246 * Now put bdev pointer at end of devpriv->devs array
250 /* ergh.. ugly.. we need to realloc :( */
251 devs = krealloc(devpriv->devs,
252 (devpriv->ndevs + 1) * sizeof(*devs),
255 dev_err(dev->class_dev,
256 "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 MAX_BOARD_NAME - strlen(devpriv->name) - 1;
266 snprintf(buf, sizeof(buf), "%d:%d ",
267 bdev->minor, bdev->subdev);
268 buf[sizeof(buf) - 1] = 0;
269 strncat(devpriv->name, buf, left);
275 if (!devpriv->nchans) {
276 dev_err(dev->class_dev, "No channels found!\n");
283 static int bonding_attach(struct comedi_device *dev,
284 struct comedi_devconfig *it)
286 struct comedi_bond_private *devpriv;
287 struct comedi_subdevice *s;
290 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
295 * Setup our bonding from config params.. sets up our private struct..
297 ret = do_dev_config(dev, it);
301 dev->board_name = devpriv->name;
303 ret = comedi_alloc_subdevices(dev, 1);
307 s = &dev->subdevices[0];
308 s->type = COMEDI_SUBD_DIO;
309 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
310 s->n_chan = devpriv->nchans;
312 s->range_table = &range_digital;
313 s->insn_bits = bonding_dio_insn_bits;
314 s->insn_config = bonding_dio_insn_config;
316 dev_info(dev->class_dev,
317 "%s: %s attached, %u channels from %u devices\n",
318 dev->driver->driver_name, dev->board_name,
319 devpriv->nchans, devpriv->ndevs);
324 static void bonding_detach(struct comedi_device *dev)
326 struct comedi_bond_private *devpriv = dev->private;
328 if (devpriv && devpriv->devs) {
329 DECLARE_BITMAP(devs_closed, COMEDI_NUM_BOARD_MINORS);
331 memset(&devs_closed, 0, sizeof(devs_closed));
332 while (devpriv->ndevs--) {
333 struct bonded_device *bdev;
335 bdev = devpriv->devs[devpriv->ndevs];
338 if (!test_and_set_bit(bdev->minor, devs_closed))
339 comedi_close(bdev->dev);
342 kfree(devpriv->devs);
343 devpriv->devs = NULL;
347 static struct comedi_driver bonding_driver = {
348 .driver_name = "comedi_bond",
349 .module = THIS_MODULE,
350 .attach = bonding_attach,
351 .detach = bonding_detach,
353 module_comedi_driver(bonding_driver);
355 MODULE_AUTHOR("Calin A. Culianu");
356 MODULE_DESCRIPTION("comedi_bond: A driver for COMEDI to bond multiple COMEDI devices together as one.");
357 MODULE_LICENSE("GPL");