/drivers/staging/comedi/drivers/comedi_bond.c
C | 504 lines | 290 code | 58 blank | 156 comment | 40 complexity | cf3633e3971acbb4b665d259fc0cde61 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
- /*
- comedi/drivers/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.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- /*
- 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/string.h>
- #include <linux/slab.h>
- #include "../comedi.h"
- #include "../comedilib.h"
- #include "../comedidev.h"
- /* The maxiumum number of channels per subdevice. */
- #define MAX_CHANS 256
- #define MODULE_NAME "comedi_bond"
- MODULE_LICENSE("GPL");
- #ifndef STR
- # define STR1(x) #x
- # define STR(x) STR1(x)
- #endif
- static int debug;
- module_param(debug, int, 0644);
- MODULE_PARM_DESC(debug, "If true, print extra cryptic debugging output useful"
- "only to developers.");
- #define LOG_MSG(x...) printk(KERN_INFO MODULE_NAME": "x)
- #define DEBUG(x...) \
- do { \
- if (debug) \
- printk(KERN_DEBUG MODULE_NAME": DEBUG: "x); \
- } while (0)
- #define WARNING(x...) printk(KERN_WARNING MODULE_NAME ": WARNING: "x)
- #define ERROR(x...) printk(KERN_ERR MODULE_NAME ": INTERNAL ERROR: "x)
- MODULE_AUTHOR("Calin A. Culianu");
- MODULE_DESCRIPTION(MODULE_NAME "A driver for COMEDI to bond multiple COMEDI "
- "devices together as one. In the words of John Lennon: "
- "'And the world will live as one...'");
- /*
- * Board descriptions for two imaginary boards. Describing the
- * boards in this way is optional, and completely driver-dependent.
- * Some drivers use arrays such as this, other do not.
- */
- struct BondingBoard {
- const char *name;
- };
- static const struct BondingBoard bondingBoards[] = {
- {
- .name = MODULE_NAME,
- },
- };
- /*
- * Useful for shorthand access to the particular board structure
- */
- #define thisboard ((const struct BondingBoard *)dev->board_ptr)
- struct BondedDevice {
- struct comedi_device *dev;
- unsigned minor;
- unsigned subdev;
- unsigned subdev_type;
- unsigned nchans;
- unsigned chanid_offset; /* The offset into our unified linear
- channel-id's of chanid 0 on this
- subdevice. */
- };
- /* this structure is for data unique to this hardware driver. If
- several hardware drivers keep similar information in this structure,
- feel free to suggest moving the variable to the struct comedi_device struct. */
- struct Private {
- # define MAX_BOARD_NAME 256
- char name[MAX_BOARD_NAME];
- struct BondedDevice **devs;
- unsigned ndevs;
- struct BondedDevice *chanIdDevMap[MAX_CHANS];
- unsigned nchans;
- };
- /*
- * most drivers define the following macro to make it easy to
- * access the private structure.
- */
- #define devpriv ((struct Private *)dev->private)
- /*
- * The struct comedi_driver structure tells the Comedi core module
- * which functions to call to configure/deconfigure (attach/detach)
- * the board, and also about the kernel module that contains
- * the device code.
- */
- static int bonding_attach(struct comedi_device *dev,
- struct comedi_devconfig *it);
- static int bonding_detach(struct comedi_device *dev);
- /** Build Private array of all devices.. */
- static int doDevConfig(struct comedi_device *dev, struct comedi_devconfig *it);
- static void doDevUnconfig(struct comedi_device *dev);
- /* Ugly implementation of realloc that always copies memory around -- I'm lazy,
- * what can I say? I like to do wasteful memcopies.. :) */
- static void *Realloc(const void *ptr, size_t len, size_t old_len);
- static struct comedi_driver driver_bonding = {
- .driver_name = MODULE_NAME,
- .module = THIS_MODULE,
- .attach = bonding_attach,
- .detach = bonding_detach,
- /* It is not necessary to implement the following members if you are
- * writing a driver for a ISA PnP or PCI card */
- /* Most drivers will support multiple types of boards by
- * having an array of board structures. These were defined
- * in skel_boards[] above. Note that the element 'name'
- * was first in the structure -- Comedi uses this fact to
- * extract the name of the board without knowing any details
- * about the structure except for its length.
- * When a device is attached (by comedi_config), the name
- * of the device is given to Comedi, and Comedi tries to
- * match it by going through the list of board names. If
- * there is a match, the address of the pointer is put
- * into dev->board_ptr and driver->attach() is called.
- *
- * Note that these are not necessary if you can determine
- * the type of board in software. ISA PnP, PCI, and PCMCIA
- * devices are such boards.
- */
- .board_name = &bondingBoards[0].name,
- .offset = sizeof(struct BondingBoard),
- .num_names = ARRAY_SIZE(bondingBoards),
- };
- static int bonding_dio_insn_bits(struct comedi_device *dev,
- struct comedi_subdevice *s,
- struct comedi_insn *insn, unsigned int *data);
- static int bonding_dio_insn_config(struct comedi_device *dev,
- struct comedi_subdevice *s,
- struct comedi_insn *insn,
- unsigned int *data);
- /*
- * Attach is called by the Comedi core to configure the driver
- * for a particular board. If you specified a board_name array
- * in the driver structure, dev->board_ptr contains that
- * address.
- */
- static int bonding_attach(struct comedi_device *dev,
- struct comedi_devconfig *it)
- {
- struct comedi_subdevice *s;
- LOG_MSG("comedi%d\n", dev->minor);
- /*
- * Allocate the private structure area. alloc_private() is a
- * convenient macro defined in comedidev.h.
- */
- if (alloc_private(dev, sizeof(struct Private)) < 0)
- return -ENOMEM;
- /*
- * Setup our bonding from config params.. sets up our Private struct..
- */
- if (!doDevConfig(dev, it))
- return -EINVAL;
- /*
- * Initialize dev->board_name. Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
- dev->board_name = devpriv->name;
- /*
- * Allocate the subdevice structures. alloc_subdevice() is a
- * convenient macro defined in comedidev.h.
- */
- if (alloc_subdevices(dev, 1) < 0)
- return -ENOMEM;
- 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;
- LOG_MSG("attached with %u DIO channels coming from %u different "
- "subdevices all bonded together. "
- "John Lennon would be proud!\n",
- devpriv->nchans, devpriv->ndevs);
- return 1;
- }
- /*
- * _detach is called to deconfigure a device. It should deallocate
- * resources.
- * This function is also called when _attach() fails, so it should be