PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/staging/comedi/drivers/comedi_bond.c

https://bitbucket.org/wisechild/galaxy-nexus
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
  1. /*
  2. comedi/drivers/comedi_bond.c
  3. A Comedi driver to 'bond' or merge multiple drivers and devices as one.
  4. COMEDI - Linux Control and Measurement Device Interface
  5. Copyright (C) 2000 David A. Schleef <ds@schleef.org>
  6. Copyright (C) 2005 Calin A. Culianu <calin@ajvar.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /*
  20. Driver: comedi_bond
  21. Description: A driver to 'bond' (merge) multiple subdevices from multiple
  22. devices together as one.
  23. Devices:
  24. Author: ds
  25. Updated: Mon, 10 Oct 00:18:25 -0500
  26. Status: works
  27. This driver allows you to 'bond' (merge) multiple comedi subdevices
  28. (coming from possibly difference boards and/or drivers) together. For
  29. example, if you had a board with 2 different DIO subdevices, and
  30. another with 1 DIO subdevice, you could 'bond' them with this driver
  31. so that they look like one big fat DIO subdevice. This makes writing
  32. applications slightly easier as you don't have to worry about managing
  33. different subdevices in the application -- you just worry about
  34. indexing one linear array of channel id's.
  35. Right now only DIO subdevices are supported as that's the personal itch
  36. I am scratching with this driver. If you want to add support for AI and AO
  37. subdevs, go right on ahead and do so!
  38. Commands aren't supported -- although it would be cool if they were.
  39. Configuration Options:
  40. List of comedi-minors to bond. All subdevices of the same type
  41. within each minor will be concatenated together in the order given here.
  42. */
  43. #include <linux/string.h>
  44. #include <linux/slab.h>
  45. #include "../comedi.h"
  46. #include "../comedilib.h"
  47. #include "../comedidev.h"
  48. /* The maxiumum number of channels per subdevice. */
  49. #define MAX_CHANS 256
  50. #define MODULE_NAME "comedi_bond"
  51. MODULE_LICENSE("GPL");
  52. #ifndef STR
  53. # define STR1(x) #x
  54. # define STR(x) STR1(x)
  55. #endif
  56. static int debug;
  57. module_param(debug, int, 0644);
  58. MODULE_PARM_DESC(debug, "If true, print extra cryptic debugging output useful"
  59. "only to developers.");
  60. #define LOG_MSG(x...) printk(KERN_INFO MODULE_NAME": "x)
  61. #define DEBUG(x...) \
  62. do { \
  63. if (debug) \
  64. printk(KERN_DEBUG MODULE_NAME": DEBUG: "x); \
  65. } while (0)
  66. #define WARNING(x...) printk(KERN_WARNING MODULE_NAME ": WARNING: "x)
  67. #define ERROR(x...) printk(KERN_ERR MODULE_NAME ": INTERNAL ERROR: "x)
  68. MODULE_AUTHOR("Calin A. Culianu");
  69. MODULE_DESCRIPTION(MODULE_NAME "A driver for COMEDI to bond multiple COMEDI "
  70. "devices together as one. In the words of John Lennon: "
  71. "'And the world will live as one...'");
  72. /*
  73. * Board descriptions for two imaginary boards. Describing the
  74. * boards in this way is optional, and completely driver-dependent.
  75. * Some drivers use arrays such as this, other do not.
  76. */
  77. struct BondingBoard {
  78. const char *name;
  79. };
  80. static const struct BondingBoard bondingBoards[] = {
  81. {
  82. .name = MODULE_NAME,
  83. },
  84. };
  85. /*
  86. * Useful for shorthand access to the particular board structure
  87. */
  88. #define thisboard ((const struct BondingBoard *)dev->board_ptr)
  89. struct BondedDevice {
  90. struct comedi_device *dev;
  91. unsigned minor;
  92. unsigned subdev;
  93. unsigned subdev_type;
  94. unsigned nchans;
  95. unsigned chanid_offset; /* The offset into our unified linear
  96. channel-id's of chanid 0 on this
  97. subdevice. */
  98. };
  99. /* this structure is for data unique to this hardware driver. If
  100. several hardware drivers keep similar information in this structure,
  101. feel free to suggest moving the variable to the struct comedi_device struct. */
  102. struct Private {
  103. # define MAX_BOARD_NAME 256
  104. char name[MAX_BOARD_NAME];
  105. struct BondedDevice **devs;
  106. unsigned ndevs;
  107. struct BondedDevice *chanIdDevMap[MAX_CHANS];
  108. unsigned nchans;
  109. };
  110. /*
  111. * most drivers define the following macro to make it easy to
  112. * access the private structure.
  113. */
  114. #define devpriv ((struct Private *)dev->private)
  115. /*
  116. * The struct comedi_driver structure tells the Comedi core module
  117. * which functions to call to configure/deconfigure (attach/detach)
  118. * the board, and also about the kernel module that contains
  119. * the device code.
  120. */
  121. static int bonding_attach(struct comedi_device *dev,
  122. struct comedi_devconfig *it);
  123. static int bonding_detach(struct comedi_device *dev);
  124. /** Build Private array of all devices.. */
  125. static int doDevConfig(struct comedi_device *dev, struct comedi_devconfig *it);
  126. static void doDevUnconfig(struct comedi_device *dev);
  127. /* Ugly implementation of realloc that always copies memory around -- I'm lazy,
  128. * what can I say? I like to do wasteful memcopies.. :) */
  129. static void *Realloc(const void *ptr, size_t len, size_t old_len);
  130. static struct comedi_driver driver_bonding = {
  131. .driver_name = MODULE_NAME,
  132. .module = THIS_MODULE,
  133. .attach = bonding_attach,
  134. .detach = bonding_detach,
  135. /* It is not necessary to implement the following members if you are
  136. * writing a driver for a ISA PnP or PCI card */
  137. /* Most drivers will support multiple types of boards by
  138. * having an array of board structures. These were defined
  139. * in skel_boards[] above. Note that the element 'name'
  140. * was first in the structure -- Comedi uses this fact to
  141. * extract the name of the board without knowing any details
  142. * about the structure except for its length.
  143. * When a device is attached (by comedi_config), the name
  144. * of the device is given to Comedi, and Comedi tries to
  145. * match it by going through the list of board names. If
  146. * there is a match, the address of the pointer is put
  147. * into dev->board_ptr and driver->attach() is called.
  148. *
  149. * Note that these are not necessary if you can determine
  150. * the type of board in software. ISA PnP, PCI, and PCMCIA
  151. * devices are such boards.
  152. */
  153. .board_name = &bondingBoards[0].name,
  154. .offset = sizeof(struct BondingBoard),
  155. .num_names = ARRAY_SIZE(bondingBoards),
  156. };
  157. static int bonding_dio_insn_bits(struct comedi_device *dev,
  158. struct comedi_subdevice *s,
  159. struct comedi_insn *insn, unsigned int *data);
  160. static int bonding_dio_insn_config(struct comedi_device *dev,
  161. struct comedi_subdevice *s,
  162. struct comedi_insn *insn,
  163. unsigned int *data);
  164. /*
  165. * Attach is called by the Comedi core to configure the driver
  166. * for a particular board. If you specified a board_name array
  167. * in the driver structure, dev->board_ptr contains that
  168. * address.
  169. */
  170. static int bonding_attach(struct comedi_device *dev,
  171. struct comedi_devconfig *it)
  172. {
  173. struct comedi_subdevice *s;
  174. LOG_MSG("comedi%d\n", dev->minor);
  175. /*
  176. * Allocate the private structure area. alloc_private() is a
  177. * convenient macro defined in comedidev.h.
  178. */
  179. if (alloc_private(dev, sizeof(struct Private)) < 0)
  180. return -ENOMEM;
  181. /*
  182. * Setup our bonding from config params.. sets up our Private struct..
  183. */
  184. if (!doDevConfig(dev, it))
  185. return -EINVAL;
  186. /*
  187. * Initialize dev->board_name. Note that we can use the "thisboard"
  188. * macro now, since we just initialized it in the last line.
  189. */
  190. dev->board_name = devpriv->name;
  191. /*
  192. * Allocate the subdevice structures. alloc_subdevice() is a
  193. * convenient macro defined in comedidev.h.
  194. */
  195. if (alloc_subdevices(dev, 1) < 0)
  196. return -ENOMEM;
  197. s = dev->subdevices + 0;
  198. s->type = COMEDI_SUBD_DIO;
  199. s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
  200. s->n_chan = devpriv->nchans;
  201. s->maxdata = 1;
  202. s->range_table = &range_digital;
  203. s->insn_bits = bonding_dio_insn_bits;
  204. s->insn_config = bonding_dio_insn_config;
  205. LOG_MSG("attached with %u DIO channels coming from %u different "
  206. "subdevices all bonded together. "
  207. "John Lennon would be proud!\n",
  208. devpriv->nchans, devpriv->ndevs);
  209. return 1;
  210. }
  211. /*
  212. * _detach is called to deconfigure a device. It should deallocate
  213. * resources.
  214. * This function is also called when _attach() fails, so it should be
  215. * careful not to release resources that were not necessarily
  216. * allocated by _attach(). dev->private and dev->subdevices are
  217. * deallocated automatically by the core.
  218. */
  219. static int bonding_detach(struct comedi_device *dev)
  220. {
  221. LOG_MSG("comedi%d: remove\n", dev->minor);
  222. doDevUnconfig(dev);
  223. return 0;
  224. }
  225. /* DIO devices are slightly special. Although it is possible to
  226. * implement the insn_read/insn_write interface, it is much more
  227. * useful to applications if you implement the insn_bits interface.
  228. * This allows packed reading/writing of the DIO channels. The
  229. * comedi core can convert between insn_bits and insn_read/write */
  230. static int bonding_dio_insn_bits(struct comedi_device *dev,
  231. struct comedi_subdevice *s,
  232. struct comedi_insn *insn, unsigned int *data)
  233. {
  234. #define LSAMPL_BITS (sizeof(unsigned int)*8)
  235. unsigned nchans = LSAMPL_BITS, num_done = 0, i;
  236. if (insn->n != 2)
  237. return -EINVAL;
  238. if (devpriv->nchans < nchans)
  239. nchans = devpriv->nchans;
  240. /* The insn data is a mask in data[0] and the new data
  241. * in data[1], each channel cooresponding to a bit. */
  242. for (i = 0; num_done < nchans && i < devpriv->ndevs; ++i) {
  243. struct BondedDevice *bdev = devpriv->devs[i];
  244. /* Grab the channel mask and data of only the bits corresponding
  245. to this subdevice.. need to shift them to zero position of
  246. course. */
  247. /* Bits corresponding to this subdev. */
  248. unsigned int subdevMask = ((1 << bdev->nchans) - 1);
  249. unsigned int writeMask, dataBits;
  250. /* Argh, we have >= LSAMPL_BITS chans.. take all bits */
  251. if (bdev->nchans >= LSAMPL_BITS)
  252. subdevMask = (unsigned int)(-1);
  253. writeMask = (data[0] >> num_done) & subdevMask;
  254. dataBits = (data[1] >> num_done) & subdevMask;
  255. /* Read/Write the new digital lines */
  256. if (comedi_dio_bitfield(bdev->dev, bdev->subdev, writeMask,
  257. &dataBits) != 2)
  258. return -EINVAL;
  259. /* Make room for the new bits in data[1], the return value */
  260. data[1] &= ~(subdevMask << num_done);
  261. /* Put the bits in the return value */
  262. data[1] |= (dataBits & subdevMask) << num_done;
  263. /* Save the new bits to the saved state.. */
  264. s->state = data[1];
  265. num_done += bdev->nchans;
  266. }
  267. return insn->n;
  268. }
  269. static int bonding_dio_insn_config(struct comedi_device *dev,
  270. struct comedi_subdevice *s,
  271. struct comedi_insn *insn, unsigned int *data)
  272. {
  273. int chan = CR_CHAN(insn->chanspec), ret, io_bits = s->io_bits;
  274. unsigned int io;
  275. struct BondedDevice *bdev;
  276. if (chan < 0 || chan >= devpriv->nchans)
  277. return -EINVAL;
  278. bdev = devpriv->chanIdDevMap[chan];
  279. /* The input or output configuration of each digital line is
  280. * configured by a special insn_config instruction. chanspec
  281. * contains the channel to be changed, and data[0] contains the
  282. * value COMEDI_INPUT or COMEDI_OUTPUT. */
  283. switch (data[0]) {
  284. case INSN_CONFIG_DIO_OUTPUT:
  285. io = COMEDI_OUTPUT; /* is this really necessary? */
  286. io_bits |= 1 << chan;
  287. break;
  288. case INSN_CONFIG_DIO_INPUT:
  289. io = COMEDI_INPUT; /* is this really necessary? */
  290. io_bits &= ~(1 << chan);
  291. break;
  292. case INSN_CONFIG_DIO_QUERY:
  293. data[1] =
  294. (io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
  295. return insn->n;
  296. break;
  297. default:
  298. return -EINVAL;
  299. break;
  300. }
  301. /* 'real' channel id for this subdev.. */
  302. chan -= bdev->chanid_offset;
  303. ret = comedi_dio_config(bdev->dev, bdev->subdev, chan, io);
  304. if (ret != 1)
  305. return -EINVAL;
  306. /* Finally, save the new io_bits values since we didn't get
  307. an error above. */
  308. s->io_bits = io_bits;
  309. return insn->n;
  310. }
  311. static void *Realloc(const void *oldmem, size_t newlen, size_t oldlen)
  312. {
  313. void *newmem = kmalloc(newlen, GFP_KERNEL);
  314. if (newmem && oldmem)
  315. memcpy(newmem, oldmem, min(oldlen, newlen));
  316. kfree(oldmem);
  317. return newmem;
  318. }
  319. static int doDevConfig(struct comedi_device *dev, struct comedi_devconfig *it)
  320. {
  321. int i;
  322. struct comedi_device *devs_opened[COMEDI_NUM_BOARD_MINORS];
  323. memset(devs_opened, 0, sizeof(devs_opened));
  324. devpriv->name[0] = 0;
  325. /* Loop through all comedi devices specified on the command-line,
  326. building our device list */
  327. for (i = 0; i < COMEDI_NDEVCONFOPTS && (!i || it->options[i]); ++i) {
  328. char file[] = "/dev/comediXXXXXX";
  329. int minor = it->options[i];
  330. struct comedi_device *d;
  331. int sdev = -1, nchans, tmp;
  332. struct BondedDevice *bdev = NULL;
  333. if (minor < 0 || minor >= COMEDI_NUM_BOARD_MINORS) {
  334. ERROR("Minor %d is invalid!\n", minor);
  335. return 0;
  336. }
  337. if (minor == dev->minor) {
  338. ERROR("Cannot bond this driver to itself!\n");
  339. return 0;
  340. }
  341. if (devs_opened[minor]) {
  342. ERROR("Minor %d specified more than once!\n", minor);
  343. return 0;
  344. }
  345. snprintf(file, sizeof(file), "/dev/comedi%u", minor);
  346. file[sizeof(file) - 1] = 0;
  347. d = devs_opened[minor] = comedi_open(file);
  348. if (!d) {
  349. ERROR("Minor %u could not be opened\n", minor);
  350. return 0;
  351. }
  352. /* Do DIO, as that's all we support now.. */
  353. while ((sdev = comedi_find_subdevice_by_type(d, COMEDI_SUBD_DIO,
  354. sdev + 1)) > -1) {
  355. nchans = comedi_get_n_channels(d, sdev);
  356. if (nchans <= 0) {
  357. ERROR("comedi_get_n_channels() returned %d "
  358. "on minor %u subdev %d!\n",
  359. nchans, minor, sdev);
  360. return 0;
  361. }
  362. bdev = kmalloc(sizeof(*bdev), GFP_KERNEL);
  363. if (!bdev) {
  364. ERROR("Out of memory.\n");
  365. return 0;
  366. }
  367. bdev->dev = d;
  368. bdev->minor = minor;
  369. bdev->subdev = sdev;
  370. bdev->subdev_type = COMEDI_SUBD_DIO;
  371. bdev->nchans = nchans;
  372. bdev->chanid_offset = devpriv->nchans;
  373. /* map channel id's to BondedDevice * pointer.. */
  374. while (nchans--)
  375. devpriv->chanIdDevMap[devpriv->nchans++] = bdev;
  376. /* Now put bdev pointer at end of devpriv->devs array
  377. * list.. */
  378. /* ergh.. ugly.. we need to realloc :( */
  379. tmp = devpriv->ndevs * sizeof(bdev);
  380. devpriv->devs =
  381. Realloc(devpriv->devs,
  382. ++devpriv->ndevs * sizeof(bdev), tmp);
  383. if (!devpriv->devs) {
  384. ERROR("Could not allocate memory. "
  385. "Out of memory?");
  386. return 0;
  387. }
  388. devpriv->devs[devpriv->ndevs - 1] = bdev;
  389. {
  390. /** Append dev:subdev to devpriv->name */
  391. char buf[20];
  392. int left =
  393. MAX_BOARD_NAME - strlen(devpriv->name) - 1;
  394. snprintf(buf, sizeof(buf), "%d:%d ", dev->minor,
  395. bdev->subdev);
  396. buf[sizeof(buf) - 1] = 0;
  397. strncat(devpriv->name, buf, left);
  398. }
  399. }
  400. }
  401. if (!devpriv->nchans) {
  402. ERROR("No channels found!\n");
  403. return 0;
  404. }
  405. return 1;
  406. }
  407. static void doDevUnconfig(struct comedi_device *dev)
  408. {
  409. unsigned long devs_closed = 0;
  410. if (devpriv) {
  411. while (devpriv->ndevs-- && devpriv->devs) {
  412. struct BondedDevice *bdev;
  413. bdev = devpriv->devs[devpriv->ndevs];
  414. if (!bdev)
  415. continue;
  416. if (!(devs_closed & (0x1 << bdev->minor))) {
  417. comedi_close(bdev->dev);
  418. devs_closed |= (0x1 << bdev->minor);
  419. }
  420. kfree(bdev);
  421. }
  422. kfree(devpriv->devs);
  423. devpriv->devs = NULL;
  424. kfree(devpriv);
  425. dev->private = NULL;
  426. }
  427. }
  428. static int __init init(void)
  429. {
  430. return comedi_driver_register(&driver_bonding);
  431. }
  432. static void __exit cleanup(void)
  433. {
  434. comedi_driver_unregister(&driver_bonding);
  435. }
  436. module_init(init);
  437. module_exit(cleanup);