drivers.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * module/drivers.c
  3. * functions for manipulating drivers
  4. *
  5. * COMEDI - Linux Control and Measurement Device Interface
  6. * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
  7. * Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
  8. *
  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.
  13. *
  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.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/ioport.h>
  24. #include <linux/slab.h>
  25. #include <linux/dma-direction.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/firmware.h>
  28. #include "comedidev.h"
  29. #include "comedi_internal.h"
  30. struct comedi_driver *comedi_drivers;
  31. /* protects access to comedi_drivers */
  32. DEFINE_MUTEX(comedi_drivers_list_lock);
  33. /**
  34. * comedi_set_hw_dev() - Set hardware device associated with COMEDI device
  35. * @dev: COMEDI device.
  36. * @hw_dev: Hardware device.
  37. *
  38. * For automatically configured COMEDI devices (resulting from a call to
  39. * comedi_auto_config() or one of its wrappers from the low-level COMEDI
  40. * driver), comedi_set_hw_dev() is called automatically by the COMEDI core
  41. * to associate the COMEDI device with the hardware device. It can also be
  42. * called directly by "legacy" low-level COMEDI drivers that rely on the
  43. * %COMEDI_DEVCONFIG ioctl to configure the hardware as long as the hardware
  44. * has a &struct device.
  45. *
  46. * If @dev->hw_dev is NULL, it gets a reference to @hw_dev and sets
  47. * @dev->hw_dev, otherwise, it does nothing. Calling it multiple times
  48. * with the same hardware device is not considered an error. If it gets
  49. * a reference to the hardware device, it will be automatically 'put' when
  50. * the device is detached from COMEDI.
  51. *
  52. * Returns 0 if @dev->hw_dev was NULL or the same as @hw_dev, otherwise
  53. * returns -EEXIST.
  54. */
  55. int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
  56. {
  57. if (hw_dev == dev->hw_dev)
  58. return 0;
  59. if (dev->hw_dev)
  60. return -EEXIST;
  61. dev->hw_dev = get_device(hw_dev);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
  65. static void comedi_clear_hw_dev(struct comedi_device *dev)
  66. {
  67. put_device(dev->hw_dev);
  68. dev->hw_dev = NULL;
  69. }
  70. /**
  71. * comedi_alloc_devpriv() - Allocate memory for the device private data
  72. * @dev: COMEDI device.
  73. * @size: Size of the memory to allocate.
  74. *
  75. * The allocated memory is zero-filled. @dev->private points to it on
  76. * return. The memory will be automatically freed when the COMEDI device is
  77. * "detached".
  78. *
  79. * Returns a pointer to the allocated memory, or NULL on failure.
  80. */
  81. void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
  82. {
  83. dev->private = kzalloc(size, GFP_KERNEL);
  84. return dev->private;
  85. }
  86. EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
  87. /**
  88. * comedi_alloc_subdevices() - Allocate subdevices for COMEDI device
  89. * @dev: COMEDI device.
  90. * @num_subdevices: Number of subdevices to allocate.
  91. *
  92. * Allocates and initializes an array of &struct comedi_subdevice for the
  93. * COMEDI device. If successful, sets @dev->subdevices to point to the
  94. * first one and @dev->n_subdevices to the number.
  95. *
  96. * Returns 0 on success, -EINVAL if @num_subdevices is < 1, or -ENOMEM if
  97. * failed to allocate the memory.
  98. */
  99. int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
  100. {
  101. struct comedi_subdevice *s;
  102. int i;
  103. if (num_subdevices < 1)
  104. return -EINVAL;
  105. s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
  106. if (!s)
  107. return -ENOMEM;
  108. dev->subdevices = s;
  109. dev->n_subdevices = num_subdevices;
  110. for (i = 0; i < num_subdevices; ++i) {
  111. s = &dev->subdevices[i];
  112. s->device = dev;
  113. s->index = i;
  114. s->async_dma_dir = DMA_NONE;
  115. spin_lock_init(&s->spin_lock);
  116. s->minor = -1;
  117. }
  118. return 0;
  119. }
  120. EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
  121. /**
  122. * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback
  123. * @s: COMEDI subdevice.
  124. *
  125. * This is called by low-level COMEDI drivers to allocate an array to record
  126. * the last values written to a subdevice's analog output channels (at least
  127. * by the %INSN_WRITE instruction), to allow them to be read back by an
  128. * %INSN_READ instruction. It also provides a default handler for the
  129. * %INSN_READ instruction unless one has already been set.
  130. *
  131. * On success, @s->readback points to the first element of the array, which
  132. * is zero-filled. The low-level driver is responsible for updating its
  133. * contents. @s->insn_read will be set to comedi_readback_insn_read()
  134. * unless it is already non-NULL.
  135. *
  136. * Returns 0 on success, -EINVAL if the subdevice has no channels, or
  137. * -ENOMEM on allocation failure.
  138. */
  139. int comedi_alloc_subdev_readback(struct comedi_subdevice *s)
  140. {
  141. if (!s->n_chan)
  142. return -EINVAL;
  143. s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL);
  144. if (!s->readback)
  145. return -ENOMEM;
  146. if (!s->insn_read)
  147. s->insn_read = comedi_readback_insn_read;
  148. return 0;
  149. }
  150. EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
  151. static void comedi_device_detach_cleanup(struct comedi_device *dev)
  152. {
  153. int i;
  154. struct comedi_subdevice *s;
  155. if (dev->subdevices) {
  156. for (i = 0; i < dev->n_subdevices; i++) {
  157. s = &dev->subdevices[i];
  158. if (comedi_can_auto_free_spriv(s))
  159. kfree(s->private);
  160. comedi_free_subdevice_minor(s);
  161. if (s->async) {
  162. comedi_buf_alloc(dev, s, 0);
  163. kfree(s->async);
  164. }
  165. kfree(s->readback);
  166. }
  167. kfree(dev->subdevices);
  168. dev->subdevices = NULL;
  169. dev->n_subdevices = 0;
  170. }
  171. kfree(dev->private);
  172. kfree(dev->pacer);
  173. dev->private = NULL;
  174. dev->pacer = NULL;
  175. dev->driver = NULL;
  176. dev->board_name = NULL;
  177. dev->board_ptr = NULL;
  178. dev->mmio = NULL;
  179. dev->iobase = 0;
  180. dev->iolen = 0;
  181. dev->ioenabled = false;
  182. dev->irq = 0;
  183. dev->read_subdev = NULL;
  184. dev->write_subdev = NULL;
  185. dev->open = NULL;
  186. dev->close = NULL;
  187. comedi_clear_hw_dev(dev);
  188. }
  189. void comedi_device_detach(struct comedi_device *dev)
  190. {
  191. comedi_device_cancel_all(dev);
  192. down_write(&dev->attach_lock);
  193. dev->attached = false;
  194. dev->detach_count++;
  195. if (dev->driver)
  196. dev->driver->detach(dev);
  197. comedi_device_detach_cleanup(dev);
  198. up_write(&dev->attach_lock);
  199. }
  200. static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
  201. {
  202. return -EINVAL;
  203. }
  204. int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
  205. struct comedi_insn *insn, unsigned int *data)
  206. {
  207. return -EINVAL;
  208. }
  209. /**
  210. * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback.
  211. * @dev: COMEDI device.
  212. * @s: COMEDI subdevice.
  213. * @insn: COMEDI instruction.
  214. * @data: Pointer to return the readback data.
  215. *
  216. * Handles the %INSN_READ instruction for subdevices that use the readback
  217. * array allocated by comedi_alloc_subdev_readback(). It may be used
  218. * directly as the subdevice's handler (@s->insn_read) or called via a
  219. * wrapper.
  220. *
  221. * @insn->n is normally 1, which will read a single value. If higher, the
  222. * same element of the readback array will be read multiple times.
  223. *
  224. * Returns @insn->n on success, or -EINVAL if @s->readback is NULL.
  225. */
  226. int comedi_readback_insn_read(struct comedi_device *dev,
  227. struct comedi_subdevice *s,
  228. struct comedi_insn *insn,
  229. unsigned int *data)
  230. {
  231. unsigned int chan = CR_CHAN(insn->chanspec);
  232. int i;
  233. if (!s->readback)
  234. return -EINVAL;
  235. for (i = 0; i < insn->n; i++)
  236. data[i] = s->readback[chan];
  237. return insn->n;
  238. }
  239. EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
  240. /**
  241. * comedi_timeout() - Busy-wait for a driver condition to occur
  242. * @dev: COMEDI device.
  243. * @s: COMEDI subdevice.
  244. * @insn: COMEDI instruction.
  245. * @cb: Callback to check for the condition.
  246. * @context: Private context from the driver.
  247. *
  248. * Busy-waits for up to a second (%COMEDI_TIMEOUT_MS) for the condition or
  249. * some error (other than -EBUSY) to occur. The parameters @dev, @s, @insn,
  250. * and @context are passed to the callback function, which returns -EBUSY to
  251. * continue waiting or some other value to stop waiting (generally 0 if the
  252. * condition occurred, or some error value).
  253. *
  254. * Returns -ETIMEDOUT if timed out, otherwise the return value from the
  255. * callback function.
  256. */
  257. int comedi_timeout(struct comedi_device *dev,
  258. struct comedi_subdevice *s,
  259. struct comedi_insn *insn,
  260. int (*cb)(struct comedi_device *dev,
  261. struct comedi_subdevice *s,
  262. struct comedi_insn *insn,
  263. unsigned long context),
  264. unsigned long context)
  265. {
  266. unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS);
  267. int ret;
  268. while (time_before(jiffies, timeout)) {
  269. ret = cb(dev, s, insn, context);
  270. if (ret != -EBUSY)
  271. return ret; /* success (0) or non EBUSY errno */
  272. cpu_relax();
  273. }
  274. return -ETIMEDOUT;
  275. }
  276. EXPORT_SYMBOL_GPL(comedi_timeout);
  277. /**
  278. * comedi_dio_insn_config() - Boilerplate (*insn_config) for DIO subdevices
  279. * @dev: COMEDI device.
  280. * @s: COMEDI subdevice.
  281. * @insn: COMEDI instruction.
  282. * @data: Instruction parameters and return data.
  283. * @mask: io_bits mask for grouped channels, or 0 for single channel.
  284. *
  285. * If @mask is 0, it is replaced with a single-bit mask corresponding to the
  286. * channel number specified by @insn->chanspec. Otherwise, @mask
  287. * corresponds to a group of channels (which should include the specified
  288. * channel) that are always configured together as inputs or outputs.
  289. *
  290. * Partially handles the %INSN_CONFIG_DIO_INPUT, %INSN_CONFIG_DIO_OUTPUTS,
  291. * and %INSN_CONFIG_DIO_QUERY instructions. The first two update
  292. * @s->io_bits to record the directions of the masked channels. The last
  293. * one sets @data[1] to the current direction of the group of channels
  294. * (%COMEDI_INPUT) or %COMEDI_OUTPUT) as recorded in @s->io_bits.
  295. *
  296. * The caller is responsible for updating the DIO direction in the hardware
  297. * registers if this function returns 0.
  298. *
  299. * Returns 0 for a %INSN_CONFIG_DIO_INPUT or %INSN_CONFIG_DIO_OUTPUT
  300. * instruction, @insn->n (> 0) for a %INSN_CONFIG_DIO_QUERY instruction, or
  301. * -EINVAL for some other instruction.
  302. */
  303. int comedi_dio_insn_config(struct comedi_device *dev,
  304. struct comedi_subdevice *s,
  305. struct comedi_insn *insn,
  306. unsigned int *data,
  307. unsigned int mask)
  308. {
  309. unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
  310. if (!mask)
  311. mask = chan_mask;
  312. switch (data[0]) {
  313. case INSN_CONFIG_DIO_INPUT:
  314. s->io_bits &= ~mask;
  315. break;
  316. case INSN_CONFIG_DIO_OUTPUT:
  317. s->io_bits |= mask;
  318. break;
  319. case INSN_CONFIG_DIO_QUERY:
  320. data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
  321. return insn->n;
  322. default:
  323. return -EINVAL;
  324. }
  325. return 0;
  326. }
  327. EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
  328. /**
  329. * comedi_dio_update_state() - Update the internal state of DIO subdevices
  330. * @s: COMEDI subdevice.
  331. * @data: The channel mask and bits to update.
  332. *
  333. * Updates @s->state which holds the internal state of the outputs for DIO
  334. * or DO subdevices (up to 32 channels). @data[0] contains a bit-mask of
  335. * the channels to be updated. @data[1] contains a bit-mask of those
  336. * channels to be set to '1'. The caller is responsible for updating the
  337. * outputs in hardware according to @s->state. As a minimum, the channels
  338. * in the returned bit-mask need to be updated.
  339. *
  340. * Returns @mask with non-existent channels removed.
  341. */
  342. unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
  343. unsigned int *data)
  344. {
  345. unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
  346. : 0xffffffff;
  347. unsigned int mask = data[0] & chanmask;
  348. unsigned int bits = data[1];
  349. if (mask) {
  350. s->state &= ~mask;
  351. s->state |= (bits & mask);
  352. }
  353. return mask;
  354. }
  355. EXPORT_SYMBOL_GPL(comedi_dio_update_state);
  356. /**
  357. * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes
  358. * @s: COMEDI subdevice.
  359. *
  360. * Determines the overall scan length according to the subdevice type and the
  361. * number of channels in the scan.
  362. *
  363. * For digital input, output or input/output subdevices, samples for
  364. * multiple channels are assumed to be packed into one or more unsigned
  365. * short or unsigned int values according to the subdevice's %SDF_LSAMPL
  366. * flag. For other types of subdevice, samples are assumed to occupy a
  367. * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag.
  368. *
  369. * Returns the overall scan length in bytes.
  370. */
  371. unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
  372. {
  373. struct comedi_cmd *cmd = &s->async->cmd;
  374. unsigned int num_samples;
  375. unsigned int bits_per_sample;
  376. switch (s->type) {
  377. case COMEDI_SUBD_DI:
  378. case COMEDI_SUBD_DO:
  379. case COMEDI_SUBD_DIO:
  380. bits_per_sample = 8 * comedi_bytes_per_sample(s);
  381. num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample);
  382. break;
  383. default:
  384. num_samples = cmd->scan_end_arg;
  385. break;
  386. }
  387. return comedi_samples_to_bytes(s, num_samples);
  388. }
  389. EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
  390. static unsigned int __comedi_nscans_left(struct comedi_subdevice *s,
  391. unsigned int nscans)
  392. {
  393. struct comedi_async *async = s->async;
  394. struct comedi_cmd *cmd = &async->cmd;
  395. if (cmd->stop_src == TRIG_COUNT) {
  396. unsigned int scans_left = 0;
  397. if (async->scans_done < cmd->stop_arg)
  398. scans_left = cmd->stop_arg - async->scans_done;
  399. if (nscans > scans_left)
  400. nscans = scans_left;
  401. }
  402. return nscans;
  403. }
  404. /**
  405. * comedi_nscans_left() - Return the number of scans left in the command
  406. * @s: COMEDI subdevice.
  407. * @nscans: The expected number of scans or 0 for all available scans.
  408. *
  409. * If @nscans is 0, it is set to the number of scans available in the
  410. * async buffer.
  411. *
  412. * If the async command has a stop_src of %TRIG_COUNT, the @nscans will be
  413. * checked against the number of scans remaining to complete the command.
  414. *
  415. * The return value will then be either the expected number of scans or the
  416. * number of scans remaining to complete the command, whichever is fewer.
  417. */
  418. unsigned int comedi_nscans_left(struct comedi_subdevice *s,
  419. unsigned int nscans)
  420. {
  421. if (nscans == 0) {
  422. unsigned int nbytes = comedi_buf_read_n_available(s);
  423. nscans = nbytes / comedi_bytes_per_scan(s);
  424. }
  425. return __comedi_nscans_left(s, nscans);
  426. }
  427. EXPORT_SYMBOL_GPL(comedi_nscans_left);
  428. /**
  429. * comedi_nsamples_left() - Return the number of samples left in the command
  430. * @s: COMEDI subdevice.
  431. * @nsamples: The expected number of samples.
  432. *
  433. * Returns the number of samples remaining to complete the command, or the
  434. * specified expected number of samples (@nsamples), whichever is fewer.
  435. */
  436. unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
  437. unsigned int nsamples)
  438. {
  439. struct comedi_async *async = s->async;
  440. struct comedi_cmd *cmd = &async->cmd;
  441. if (cmd->stop_src == TRIG_COUNT) {
  442. unsigned int scans_left = __comedi_nscans_left(s, cmd->stop_arg);
  443. unsigned int scan_pos =
  444. comedi_bytes_to_samples(s, async->scan_progress);
  445. unsigned long long samples_left = 0;
  446. if (scans_left) {
  447. samples_left = ((unsigned long long)scans_left *
  448. cmd->scan_end_arg) - scan_pos;
  449. }
  450. if (samples_left < nsamples)
  451. nsamples = samples_left;
  452. }
  453. return nsamples;
  454. }
  455. EXPORT_SYMBOL_GPL(comedi_nsamples_left);
  456. /**
  457. * comedi_inc_scan_progress() - Update scan progress in asynchronous command
  458. * @s: COMEDI subdevice.
  459. * @num_bytes: Amount of data in bytes to increment scan progress.
  460. *
  461. * Increments the scan progress by the number of bytes specified by @num_bytes.
  462. * If the scan progress reaches or exceeds the scan length in bytes, reduce
  463. * it modulo the scan length in bytes and set the "end of scan" asynchronous
  464. * event flag (%COMEDI_CB_EOS) to be processed later.
  465. */
  466. void comedi_inc_scan_progress(struct comedi_subdevice *s,
  467. unsigned int num_bytes)
  468. {
  469. struct comedi_async *async = s->async;
  470. struct comedi_cmd *cmd = &async->cmd;
  471. unsigned int scan_length = comedi_bytes_per_scan(s);
  472. /* track the 'cur_chan' for non-SDF_PACKED subdevices */
  473. if (!(s->subdev_flags & SDF_PACKED)) {
  474. async->cur_chan += comedi_bytes_to_samples(s, num_bytes);
  475. async->cur_chan %= cmd->chanlist_len;
  476. }
  477. async->scan_progress += num_bytes;
  478. if (async->scan_progress >= scan_length) {
  479. unsigned int nscans = async->scan_progress / scan_length;
  480. if (async->scans_done < (UINT_MAX - nscans))
  481. async->scans_done += nscans;
  482. else
  483. async->scans_done = UINT_MAX;
  484. async->scan_progress %= scan_length;
  485. async->events |= COMEDI_CB_EOS;
  486. }
  487. }
  488. EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
  489. /**
  490. * comedi_handle_events() - Handle events and possibly stop acquisition
  491. * @dev: COMEDI device.
  492. * @s: COMEDI subdevice.
  493. *
  494. * Handles outstanding asynchronous acquisition event flags associated
  495. * with the subdevice. Call the subdevice's @s->cancel() handler if the
  496. * "end of acquisition", "error" or "overflow" event flags are set in order
  497. * to stop the acquisition at the driver level.
  498. *
  499. * Calls comedi_event() to further process the event flags, which may mark
  500. * the asynchronous command as no longer running, possibly terminated with
  501. * an error, and may wake up tasks.
  502. *
  503. * Return a bit-mask of the handled events.
  504. */
  505. unsigned int comedi_handle_events(struct comedi_device *dev,
  506. struct comedi_subdevice *s)
  507. {
  508. unsigned int events = s->async->events;
  509. if (events == 0)
  510. return events;
  511. if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel)
  512. s->cancel(dev, s);
  513. comedi_event(dev, s);
  514. return events;
  515. }
  516. EXPORT_SYMBOL_GPL(comedi_handle_events);
  517. static int insn_rw_emulate_bits(struct comedi_device *dev,
  518. struct comedi_subdevice *s,
  519. struct comedi_insn *insn,
  520. unsigned int *data)
  521. {
  522. struct comedi_insn _insn;
  523. unsigned int chan = CR_CHAN(insn->chanspec);
  524. unsigned int base_chan = (chan < 32) ? 0 : chan;
  525. unsigned int _data[2];
  526. int ret;
  527. memset(_data, 0, sizeof(_data));
  528. memset(&_insn, 0, sizeof(_insn));
  529. _insn.insn = INSN_BITS;
  530. _insn.chanspec = base_chan;
  531. _insn.n = 2;
  532. _insn.subdev = insn->subdev;
  533. if (insn->insn == INSN_WRITE) {
  534. if (!(s->subdev_flags & SDF_WRITABLE))
  535. return -EINVAL;
  536. _data[0] = 1 << (chan - base_chan); /* mask */
  537. _data[1] = data[0] ? (1 << (chan - base_chan)) : 0; /* bits */
  538. }
  539. ret = s->insn_bits(dev, s, &_insn, _data);
  540. if (ret < 0)
  541. return ret;
  542. if (insn->insn == INSN_READ)
  543. data[0] = (_data[1] >> (chan - base_chan)) & 1;
  544. return 1;
  545. }
  546. static int __comedi_device_postconfig_async(struct comedi_device *dev,
  547. struct comedi_subdevice *s)
  548. {
  549. struct comedi_async *async;
  550. unsigned int buf_size;
  551. int ret;
  552. if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
  553. dev_warn(dev->class_dev,
  554. "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
  555. return -EINVAL;
  556. }
  557. if (!s->do_cmdtest) {
  558. dev_warn(dev->class_dev,
  559. "async subdevices must have a do_cmdtest() function\n");
  560. return -EINVAL;
  561. }
  562. if (!s->cancel)
  563. dev_warn(dev->class_dev,
  564. "async subdevices should have a cancel() function\n");
  565. async = kzalloc(sizeof(*async), GFP_KERNEL);
  566. if (!async)
  567. return -ENOMEM;
  568. init_waitqueue_head(&async->wait_head);
  569. s->async = async;
  570. async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
  571. buf_size = comedi_default_buf_size_kb * 1024;
  572. if (buf_size > async->max_bufsize)
  573. buf_size = async->max_bufsize;
  574. if (comedi_buf_alloc(dev, s, buf_size) < 0) {
  575. dev_warn(dev->class_dev, "Buffer allocation failed\n");
  576. return -ENOMEM;
  577. }
  578. if (s->buf_change) {
  579. ret = s->buf_change(dev, s);
  580. if (ret < 0)
  581. return ret;
  582. }
  583. comedi_alloc_subdevice_minor(s);
  584. return 0;
  585. }
  586. static int __comedi_device_postconfig(struct comedi_device *dev)
  587. {
  588. struct comedi_subdevice *s;
  589. int ret;
  590. int i;
  591. for (i = 0; i < dev->n_subdevices; i++) {
  592. s = &dev->subdevices[i];
  593. if (s->type == COMEDI_SUBD_UNUSED)
  594. continue;
  595. if (s->type == COMEDI_SUBD_DO) {
  596. if (s->n_chan < 32)
  597. s->io_bits = (1 << s->n_chan) - 1;
  598. else
  599. s->io_bits = 0xffffffff;
  600. }
  601. if (s->len_chanlist == 0)
  602. s->len_chanlist = 1;
  603. if (s->do_cmd) {
  604. ret = __comedi_device_postconfig_async(dev, s);
  605. if (ret)
  606. return ret;
  607. }
  608. if (!s->range_table && !s->range_table_list)
  609. s->range_table = &range_unknown;
  610. if (!s->insn_read && s->insn_bits)
  611. s->insn_read = insn_rw_emulate_bits;
  612. if (!s->insn_write && s->insn_bits)
  613. s->insn_write = insn_rw_emulate_bits;
  614. if (!s->insn_read)
  615. s->insn_read = insn_inval;
  616. if (!s->insn_write)
  617. s->insn_write = insn_inval;
  618. if (!s->insn_bits)
  619. s->insn_bits = insn_inval;
  620. if (!s->insn_config)
  621. s->insn_config = insn_inval;
  622. if (!s->poll)
  623. s->poll = poll_invalid;
  624. }
  625. return 0;
  626. }
  627. /* do a little post-config cleanup */
  628. static int comedi_device_postconfig(struct comedi_device *dev)
  629. {
  630. int ret;
  631. ret = __comedi_device_postconfig(dev);
  632. if (ret < 0)
  633. return ret;
  634. down_write(&dev->attach_lock);
  635. dev->attached = true;
  636. up_write(&dev->attach_lock);
  637. return 0;
  638. }
  639. /*
  640. * Generic recognize function for drivers that register their supported
  641. * board names.
  642. *
  643. * 'driv->board_name' points to a 'const char *' member within the
  644. * zeroth element of an array of some private board information
  645. * structure, say 'struct foo_board' containing a member 'const char
  646. * *board_name' that is initialized to point to a board name string that
  647. * is one of the candidates matched against this function's 'name'
  648. * parameter.
  649. *
  650. * 'driv->offset' is the size of the private board information
  651. * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
  652. * the length of the array of private board information structures.
  653. *
  654. * If one of the board names in the array of private board information
  655. * structures matches the name supplied to this function, the function
  656. * returns a pointer to the pointer to the board name, otherwise it
  657. * returns NULL. The return value ends up in the 'board_ptr' member of
  658. * a 'struct comedi_device' that the low-level comedi driver's
  659. * 'attach()' hook can convert to a point to a particular element of its
  660. * array of private board information structures by subtracting the
  661. * offset of the member that points to the board name. (No subtraction
  662. * is required if the board name pointer is the first member of the
  663. * private board information structure, which is generally the case.)
  664. */
  665. static void *comedi_recognize(struct comedi_driver *driv, const char *name)
  666. {
  667. char **name_ptr = (char **)driv->board_name;
  668. int i;
  669. for (i = 0; i < driv->num_names; i++) {
  670. if (strcmp(*name_ptr, name) == 0)
  671. return name_ptr;
  672. name_ptr = (void *)name_ptr + driv->offset;
  673. }
  674. return NULL;
  675. }
  676. static void comedi_report_boards(struct comedi_driver *driv)
  677. {
  678. unsigned int i;
  679. const char *const *name_ptr;
  680. pr_info("comedi: valid board names for %s driver are:\n",
  681. driv->driver_name);
  682. name_ptr = driv->board_name;
  683. for (i = 0; i < driv->num_names; i++) {
  684. pr_info(" %s\n", *name_ptr);
  685. name_ptr = (const char **)((char *)name_ptr + driv->offset);
  686. }
  687. if (driv->num_names == 0)
  688. pr_info(" %s\n", driv->driver_name);
  689. }
  690. /**
  691. * comedi_load_firmware() - Request and load firmware for a device
  692. * @dev: COMEDI device.
  693. * @device: Hardware device.
  694. * @name: The name of the firmware image.
  695. * @cb: Callback to the upload the firmware image.
  696. * @context: Private context from the driver.
  697. *
  698. * Sends a firmware request for the hardware device and waits for it. Calls
  699. * the callback function to upload the firmware to the device, them releases
  700. * the firmware.
  701. *
  702. * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number
  703. * from the firmware request or the callback function.
  704. */
  705. int comedi_load_firmware(struct comedi_device *dev,
  706. struct device *device,
  707. const char *name,
  708. int (*cb)(struct comedi_device *dev,
  709. const u8 *data, size_t size,
  710. unsigned long context),
  711. unsigned long context)
  712. {
  713. const struct firmware *fw;
  714. int ret;
  715. if (!cb)
  716. return -EINVAL;
  717. ret = maybe_reject_firmware(&fw, name, device);
  718. if (ret == 0) {
  719. ret = cb(dev, fw->data, fw->size, context);
  720. release_firmware(fw);
  721. }
  722. return ret < 0 ? ret : 0;
  723. }
  724. EXPORT_SYMBOL_GPL(comedi_load_firmware);
  725. /**
  726. * __comedi_request_region() - Request an I/O region for a legacy driver
  727. * @dev: COMEDI device.
  728. * @start: Base address of the I/O region.
  729. * @len: Length of the I/O region.
  730. *
  731. * Requests the specified I/O port region which must start at a non-zero
  732. * address.
  733. *
  734. * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
  735. * fails.
  736. */
  737. int __comedi_request_region(struct comedi_device *dev,
  738. unsigned long start, unsigned long len)
  739. {
  740. if (!start) {
  741. dev_warn(dev->class_dev,
  742. "%s: a I/O base address must be specified\n",
  743. dev->board_name);
  744. return -EINVAL;
  745. }
  746. if (!request_region(start, len, dev->board_name)) {
  747. dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
  748. dev->board_name, start, len);
  749. return -EIO;
  750. }
  751. return 0;
  752. }
  753. EXPORT_SYMBOL_GPL(__comedi_request_region);
  754. /**
  755. * comedi_request_region() - Request an I/O region for a legacy driver
  756. * @dev: COMEDI device.
  757. * @start: Base address of the I/O region.
  758. * @len: Length of the I/O region.
  759. *
  760. * Requests the specified I/O port region which must start at a non-zero
  761. * address.
  762. *
  763. * On success, @dev->iobase is set to the base address of the region and
  764. * @dev->iolen is set to its length.
  765. *
  766. * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
  767. * fails.
  768. */
  769. int comedi_request_region(struct comedi_device *dev,
  770. unsigned long start, unsigned long len)
  771. {
  772. int ret;
  773. ret = __comedi_request_region(dev, start, len);
  774. if (ret == 0) {
  775. dev->iobase = start;
  776. dev->iolen = len;
  777. }
  778. return ret;
  779. }
  780. EXPORT_SYMBOL_GPL(comedi_request_region);
  781. /**
  782. * comedi_legacy_detach() - A generic (*detach) function for legacy drivers
  783. * @dev: COMEDI device.
  784. *
  785. * This is a simple, generic 'detach' handler for legacy COMEDI devices that
  786. * just use a single I/O port region and possibly an IRQ and that don't need
  787. * any special clean-up for their private device or subdevice storage. It
  788. * can also be called by a driver-specific 'detach' handler.
  789. *
  790. * If @dev->irq is non-zero, the IRQ will be freed. If @dev->iobase and
  791. * @dev->iolen are both non-zero, the I/O port region will be released.
  792. */
  793. void comedi_legacy_detach(struct comedi_device *dev)
  794. {
  795. if (dev->irq) {
  796. free_irq(dev->irq, dev);
  797. dev->irq = 0;
  798. }
  799. if (dev->iobase && dev->iolen) {
  800. release_region(dev->iobase, dev->iolen);
  801. dev->iobase = 0;
  802. dev->iolen = 0;
  803. }
  804. }
  805. EXPORT_SYMBOL_GPL(comedi_legacy_detach);
  806. int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
  807. {
  808. struct comedi_driver *driv;
  809. int ret;
  810. if (dev->attached)
  811. return -EBUSY;
  812. mutex_lock(&comedi_drivers_list_lock);
  813. for (driv = comedi_drivers; driv; driv = driv->next) {
  814. if (!try_module_get(driv->module))
  815. continue;
  816. if (driv->num_names) {
  817. dev->board_ptr = comedi_recognize(driv, it->board_name);
  818. if (dev->board_ptr)
  819. break;
  820. } else if (strcmp(driv->driver_name, it->board_name) == 0) {
  821. break;
  822. }
  823. module_put(driv->module);
  824. }
  825. if (!driv) {
  826. /* recognize has failed if we get here */
  827. /* report valid board names before returning error */
  828. for (driv = comedi_drivers; driv; driv = driv->next) {
  829. if (!try_module_get(driv->module))
  830. continue;
  831. comedi_report_boards(driv);
  832. module_put(driv->module);
  833. }
  834. ret = -EIO;
  835. goto out;
  836. }
  837. if (!driv->attach) {
  838. /* driver does not support manual configuration */
  839. dev_warn(dev->class_dev,
  840. "driver '%s' does not support attach using comedi_config\n",
  841. driv->driver_name);
  842. module_put(driv->module);
  843. ret = -EIO;
  844. goto out;
  845. }
  846. dev->driver = driv;
  847. dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
  848. : dev->driver->driver_name;
  849. ret = driv->attach(dev, it);
  850. if (ret >= 0)
  851. ret = comedi_device_postconfig(dev);
  852. if (ret < 0) {
  853. comedi_device_detach(dev);
  854. module_put(driv->module);
  855. }
  856. /* On success, the driver module count has been incremented. */
  857. out:
  858. mutex_unlock(&comedi_drivers_list_lock);
  859. return ret;
  860. }
  861. /**
  862. * comedi_auto_config() - Create a COMEDI device for a hardware device
  863. * @hardware_device: Hardware device.
  864. * @driver: COMEDI low-level driver for the hardware device.
  865. * @context: Driver context for the auto_attach handler.
  866. *
  867. * Allocates a new COMEDI device for the hardware device and calls the
  868. * low-level driver's 'auto_attach' handler to set-up the hardware and
  869. * allocate the COMEDI subdevices. Additional "post-configuration" setting
  870. * up is performed on successful return from the 'auto_attach' handler.
  871. * If the 'auto_attach' handler fails, the low-level driver's 'detach'
  872. * handler will be called as part of the clean-up.
  873. *
  874. * This is usually called from a wrapper function in a bus-specific COMEDI
  875. * module, which in turn is usually called from a bus device 'probe'
  876. * function in the low-level driver.
  877. *
  878. * Returns 0 on success, -EINVAL if the parameters are invalid or the
  879. * post-configuration determines the driver has set the COMEDI device up
  880. * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of
  881. * COMEDI minor device numbers, or some negative error number returned by
  882. * the driver's 'auto_attach' handler.
  883. */
  884. int comedi_auto_config(struct device *hardware_device,
  885. struct comedi_driver *driver, unsigned long context)
  886. {
  887. struct comedi_device *dev;
  888. int ret;
  889. if (!hardware_device) {
  890. pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
  891. return -EINVAL;
  892. }
  893. if (!driver) {
  894. dev_warn(hardware_device,
  895. "BUG! comedi_auto_config called with NULL comedi driver\n");
  896. return -EINVAL;
  897. }
  898. if (!driver->auto_attach) {
  899. dev_warn(hardware_device,
  900. "BUG! comedi driver '%s' has no auto_attach handler\n",
  901. driver->driver_name);
  902. return -EINVAL;
  903. }
  904. dev = comedi_alloc_board_minor(hardware_device);
  905. if (IS_ERR(dev)) {
  906. dev_warn(hardware_device,
  907. "driver '%s' could not create device.\n",
  908. driver->driver_name);
  909. return PTR_ERR(dev);
  910. }
  911. /* Note: comedi_alloc_board_minor() locked dev->mutex. */
  912. dev->driver = driver;
  913. dev->board_name = dev->driver->driver_name;
  914. ret = driver->auto_attach(dev, context);
  915. if (ret >= 0)
  916. ret = comedi_device_postconfig(dev);
  917. mutex_unlock(&dev->mutex);
  918. if (ret < 0) {
  919. dev_warn(hardware_device,
  920. "driver '%s' failed to auto-configure device.\n",
  921. driver->driver_name);
  922. comedi_release_hardware_device(hardware_device);
  923. } else {
  924. /*
  925. * class_dev should be set properly here
  926. * after a successful auto config
  927. */
  928. dev_info(dev->class_dev,
  929. "driver '%s' has successfully auto-configured '%s'.\n",
  930. driver->driver_name, dev->board_name);
  931. }
  932. return ret;
  933. }
  934. EXPORT_SYMBOL_GPL(comedi_auto_config);
  935. /**
  936. * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device
  937. * @hardware_device: Hardware device previously passed to
  938. * comedi_auto_config().
  939. *
  940. * Cleans up and eventually destroys the COMEDI device allocated by
  941. * comedi_auto_config() for the same hardware device. As part of this
  942. * clean-up, the low-level COMEDI driver's 'detach' handler will be called.
  943. * (The COMEDI device itself will persist in an unattached state if it is
  944. * still open, until it is released, and any mmapped buffers will persist
  945. * until they are munmapped.)
  946. *
  947. * This is usually called from a wrapper module in a bus-specific COMEDI
  948. * module, which in turn is usually set as the bus device 'remove' function
  949. * in the low-level COMEDI driver.
  950. */
  951. void comedi_auto_unconfig(struct device *hardware_device)
  952. {
  953. if (!hardware_device)
  954. return;
  955. comedi_release_hardware_device(hardware_device);
  956. }
  957. EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
  958. /**
  959. * comedi_driver_register() - Register a low-level COMEDI driver
  960. * @driver: Low-level COMEDI driver.
  961. *
  962. * The low-level COMEDI driver is added to the list of registered COMEDI
  963. * drivers. This is used by the handler for the "/proc/comedi" file and is
  964. * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure
  965. * "legacy" COMEDI devices (for those low-level drivers that support it).
  966. *
  967. * Returns 0.
  968. */
  969. int comedi_driver_register(struct comedi_driver *driver)
  970. {
  971. mutex_lock(&comedi_drivers_list_lock);
  972. driver->next = comedi_drivers;
  973. comedi_drivers = driver;
  974. mutex_unlock(&comedi_drivers_list_lock);
  975. return 0;
  976. }
  977. EXPORT_SYMBOL_GPL(comedi_driver_register);
  978. /**
  979. * comedi_driver_unregister() - Unregister a low-level COMEDI driver
  980. * @driver: Low-level COMEDI driver.
  981. *
  982. * The low-level COMEDI driver is removed from the list of registered COMEDI
  983. * drivers. Detaches any COMEDI devices attached to the driver, which will
  984. * result in the low-level driver's 'detach' handler being called for those
  985. * devices before this function returns.
  986. */
  987. void comedi_driver_unregister(struct comedi_driver *driver)
  988. {
  989. struct comedi_driver *prev;
  990. int i;
  991. /* unlink the driver */
  992. mutex_lock(&comedi_drivers_list_lock);
  993. if (comedi_drivers == driver) {
  994. comedi_drivers = driver->next;
  995. } else {
  996. for (prev = comedi_drivers; prev->next; prev = prev->next) {
  997. if (prev->next == driver) {
  998. prev->next = driver->next;
  999. break;
  1000. }
  1001. }
  1002. }
  1003. mutex_unlock(&comedi_drivers_list_lock);
  1004. /* check for devices using this driver */
  1005. for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
  1006. struct comedi_device *dev = comedi_dev_get_from_minor(i);
  1007. if (!dev)
  1008. continue;
  1009. mutex_lock(&dev->mutex);
  1010. if (dev->attached && dev->driver == driver) {
  1011. if (dev->use_count)
  1012. dev_warn(dev->class_dev,
  1013. "BUG! detaching device with use_count=%d\n",
  1014. dev->use_count);
  1015. comedi_device_detach(dev);
  1016. }
  1017. mutex_unlock(&dev->mutex);
  1018. comedi_dev_put(dev);
  1019. }
  1020. }
  1021. EXPORT_SYMBOL_GPL(comedi_driver_unregister);