Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | /*
* comedi_bond.c
* A Comedi driver to 'bond' or merge multiple drivers and devices as one.
*
* COMEDI - Linux Control and Measurement Device Interface
* Copyright (C) 2000 David A. Schleef <ds@schleef.org>
* Copyright (C) 2005 Calin A. Culianu <calin@ajvar.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* Driver: comedi_bond
* Description: A driver to 'bond' (merge) multiple subdevices from multiple
* devices together as one.
* Devices:
* Author: ds
* Updated: Mon, 10 Oct 00:18:25 -0500
* Status: works
*
* This driver allows you to 'bond' (merge) multiple comedi subdevices
* (coming from possibly difference boards and/or drivers) together. For
* example, if you had a board with 2 different DIO subdevices, and
* another with 1 DIO subdevice, you could 'bond' them with this driver
* so that they look like one big fat DIO subdevice. This makes writing
* applications slightly easier as you don't have to worry about managing
* different subdevices in the application -- you just worry about
* indexing one linear array of channel id's.
*
* Right now only DIO subdevices are supported as that's the personal itch
* I am scratching with this driver. If you want to add support for AI and AO
* subdevs, go right on ahead and do so!
*
* Commands aren't supported -- although it would be cool if they were.
*
* Configuration Options:
* List of comedi-minors to bond. All subdevices of the same type
* within each minor will be concatenated together in the order given here.
*/
#include <linux/module.h>
#include <linux/string.h>
#include <linux/slab.h>
#include "../comedi.h"
#include "../comedilib.h"
#include "../comedidev.h"
struct bonded_device {
struct comedi_device *dev;
unsigned minor;
unsigned subdev;
unsigned nchans;
};
struct comedi_bond_private {
# define MAX_BOARD_NAME 256
char name[MAX_BOARD_NAME];
struct bonded_device **devs;
unsigned ndevs;
unsigned nchans;
};
static int bonding_dio_insn_bits(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn, unsigned int *data)
{
struct comedi_bond_private *devpriv = dev->private;
unsigned int n_left, n_done, base_chan;
unsigned int write_mask, data_bits;
struct bonded_device **devs;
write_mask = data[0];
data_bits = data[1];
base_chan = CR_CHAN(insn->chanspec);
/* do a maximum of 32 channels, starting from base_chan. */
n_left = devpriv->nchans - base_chan;
if (n_left > 32)
n_left = 32;
n_done = 0;
devs = devpriv->devs;
do {
struct bonded_device *bdev = *devs++;
if (base_chan < bdev->nchans) {
/* base channel falls within bonded device */
unsigned int b_chans, b_mask, b_write_mask, b_data_bits;
int ret;
/*
* Get num channels to do for bonded device and set
* up mask and data bits for bonded device.
*/
b_chans = bdev->nchans - base_chan;
if (b_chans > n_left)
b_chans = n_left;
b_mask = (1U << b_chans) - 1;
b_write_mask = (write_mask >> n_done) & b_mask;
b_data_bits = (data_bits >> n_done) & b_mask;
/* Read/Write the new digital lines. */
ret = comedi_dio_bitfield2(bdev->dev, bdev->subdev,
b_write_mask, &b_data_bits,
base_chan);
if (ret < 0)
return ret;
/* Place read bits into data[1]. */
data[1] &= ~(b_mask << n_done);
data[1] |= (b_data_bits & b_mask) << n_done;
/*
* Set up for following bonded device (if still have
* channels to read/write).
*/
base_chan = 0;
n_done += b_chans;
n_left -= b_chans;
} else {
/* Skip bonded devices before base channel. */
base_chan -= bdev->nchans;
}
} while (n_left);
return insn->n;
}
static int bonding_dio_insn_config(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn, unsigned int *data)
{
struct comedi_bond_private *devpriv = dev->private;
unsigned int chan = CR_CHAN(insn->chanspec);
int ret;
struct bonded_device *bdev;
struct bonded_device **devs;
/*
* Locate bonded subdevice and adjust channel.
*/
devs = devpriv->devs;
for (bdev = *devs++; chan >= bdev->nchans; bdev = *devs++)
chan -= bdev->nchans;
/*
* The input or output configuration of each digital line is
* configured by a special insn_config instruction. chanspec
* contains the channel to be changed, and data[0] contains the
* configuration instruction INSN_CONFIG_DIO_OUTPUT,
* INSN_CONFIG_DIO_INPUT or INSN_CONFIG_DIO_QUERY.
*
* Note that INSN_CONFIG_DIO_OUTPUT == COMEDI_OUTPUT,
* and INSN_CONFIG_DIO_INPUT == COMEDI_INPUT. This is deliberate ;)
*/
switch (data[0]) {
case INSN_CONFIG_DIO_OUTPUT:
case INSN_CONFIG_DIO_INPUT:
ret = comedi_dio_config(bdev->dev, bdev->subdev, chan, data[0]);
break;
case INSN_CONFIG_DIO_QUERY:
ret = comedi_dio_get_config(bdev->dev, bdev->subdev, chan,
&data[1]);
break;
default:
ret = -EINVAL;
break;
}
if (ret >= 0)
ret = insn->n;
return ret;
}
static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
{
struct comedi_bond_private *devpriv = dev->private;
DECLARE_BITMAP(devs_opened, COMEDI_NUM_BOARD_MINORS);
int i;
memset(&devs_opened, 0, sizeof(devs_opened));
devpriv->name[0] = 0;
/*
* Loop through all comedi devices specified on the command-line,
* building our device list.
*/
for (i = 0; i < COMEDI_NDEVCONFOPTS && (!i || it->options[i]); ++i) {
char file[sizeof("/dev/comediXXXXXX")];
int minor = it->options[i];
struct comedi_device *d;
int sdev = -1, nchans;
struct bonded_device *bdev;
struct bonded_device **devs;
if (minor < 0 || minor >= COMEDI_NUM_BOARD_MINORS) {
dev_err(dev->class_dev,
"Minor %d is invalid!\n", minor);
return -EINVAL;
}
if (minor == dev->minor) {
dev_err(dev->class_dev,
"Cannot bond this driver to itself!\n");
return -EINVAL;
}
if (test_and_set_bit(minor, devs_opened)) {
dev_err(dev->class_dev,
"Minor %d specified more than once!\n", minor);
return -EINVAL;
}
snprintf(file, sizeof(file), "/dev/comedi%d", minor);
file[sizeof(file) - 1] = 0;
d = comedi_open(file);
if (!d) {
dev_err(dev->class_dev,
"Minor %u could not be opened\n", minor);
return -ENODEV;
}
/* Do DIO, as that's all we support now.. */
while ((sdev = comedi_find_subdevice_by_type(d, COMEDI_SUBD_DIO,
sdev + 1)) > -1) {
nchans = comedi_get_n_channels(d, sdev);
if (nchans <= 0) {
dev_err(dev->class_dev,
"comedi_get_n_channels() returned %d on minor %u subdev %d!\n",
nchans, minor, sdev);
return -EINVAL;
}
bdev = kmalloc(sizeof(*bdev), GFP_KERNEL);
if (!bdev)
return -ENOMEM;
bdev->dev = d;
bdev->minor = minor;
bdev->subdev = sdev;
bdev->nchans = nchans;
devpriv->nchans += nchans;
/*
* Now put bdev pointer at end of devpriv->devs array
* list..
*/
/* ergh.. ugly.. we need to realloc :( */
devs = krealloc(devpriv->devs,
(devpriv->ndevs + 1) * sizeof(*devs),
GFP_KERNEL);
if (!devs) {
dev_err(dev->class_dev,
"Could not allocate memory. Out of memory?\n");
kfree(bdev);
return -ENOMEM;
}
devpriv->devs = devs;
devpriv->devs[devpriv->ndevs++] = bdev;
{
/* Append dev:subdev to devpriv->name */
char buf[20];
int left =
MAX_BOARD_NAME - strlen(devpriv->name) - 1;
snprintf(buf, sizeof(buf), "%u:%u ",
bdev->minor, bdev->subdev);
buf[sizeof(buf) - 1] = 0;
strncat(devpriv->name, buf, left);
}
}
}
if (!devpriv->nchans) {
dev_err(dev->class_dev, "No channels found!\n");
return -EINVAL;
}
return 0;
}
static int bonding_attach(struct comedi_device *dev,
struct comedi_devconfig *it)
{
struct comedi_bond_private *devpriv;
struct comedi_subdevice *s;
int ret;
devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
if (!devpriv)
return -ENOMEM;
/*
* Setup our bonding from config params.. sets up our private struct..
*/
ret = do_dev_config(dev, it);
if (ret)
return ret;
dev->board_name = devpriv->name;
ret = comedi_alloc_subdevices(dev, 1);
if (ret)
return ret;
s = &dev->subdevices[0];
s->type = COMEDI_SUBD_DIO;
s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
s->n_chan = devpriv->nchans;
s->maxdata = 1;
s->range_table = &range_digital;
s->insn_bits = bonding_dio_insn_bits;
s->insn_config = bonding_dio_insn_config;
dev_info(dev->class_dev,
"%s: %s attached, %u channels from %u devices\n",
dev->driver->driver_name, dev->board_name,
devpriv->nchans, devpriv->ndevs);
return 0;
}
static void bonding_detach(struct comedi_device *dev)
{
struct comedi_bond_private *devpriv = dev->private;
if (devpriv && devpriv->devs) {
DECLARE_BITMAP(devs_closed, COMEDI_NUM_BOARD_MINORS);
memset(&devs_closed, 0, sizeof(devs_closed));
while (devpriv->ndevs--) {
struct bonded_device *bdev;
bdev = devpriv->devs[devpriv->ndevs];
if (!bdev)
continue;
if (!test_and_set_bit(bdev->minor, devs_closed))
comedi_close(bdev->dev);
kfree(bdev);
}
kfree(devpriv->devs);
devpriv->devs = NULL;
}
}
static struct comedi_driver bonding_driver = {
.driver_name = "comedi_bond",
.module = THIS_MODULE,
.attach = bonding_attach,
.detach = bonding_detach,
};
module_comedi_driver(bonding_driver);
MODULE_AUTHOR("Calin A. Culianu");
MODULE_DESCRIPTION("comedi_bond: A driver for COMEDI to bond multiple COMEDI devices together as one.");
MODULE_LICENSE("GPL");
|