drivers.c 34 KB

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