drivers.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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)
  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, unsigned int *data)
  520. {
  521. struct comedi_insn new_insn;
  522. int ret;
  523. static const unsigned channels_per_bitfield = 32;
  524. unsigned chan = CR_CHAN(insn->chanspec);
  525. const unsigned base_bitfield_channel =
  526. (chan < channels_per_bitfield) ? 0 : chan;
  527. unsigned int new_data[2];
  528. memset(new_data, 0, sizeof(new_data));
  529. memset(&new_insn, 0, sizeof(new_insn));
  530. new_insn.insn = INSN_BITS;
  531. new_insn.chanspec = base_bitfield_channel;
  532. new_insn.n = 2;
  533. new_insn.subdev = insn->subdev;
  534. if (insn->insn == INSN_WRITE) {
  535. if (!(s->subdev_flags & SDF_WRITABLE))
  536. return -EINVAL;
  537. new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
  538. new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
  539. : 0; /* bits */
  540. }
  541. ret = s->insn_bits(dev, s, &new_insn, new_data);
  542. if (ret < 0)
  543. return ret;
  544. if (insn->insn == INSN_READ)
  545. data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
  546. return 1;
  547. }
  548. static int __comedi_device_postconfig_async(struct comedi_device *dev,
  549. struct comedi_subdevice *s)
  550. {
  551. struct comedi_async *async;
  552. unsigned int buf_size;
  553. int ret;
  554. if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
  555. dev_warn(dev->class_dev,
  556. "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
  557. return -EINVAL;
  558. }
  559. if (!s->do_cmdtest) {
  560. dev_warn(dev->class_dev,
  561. "async subdevices must have a do_cmdtest() function\n");
  562. return -EINVAL;
  563. }
  564. async = kzalloc(sizeof(*async), GFP_KERNEL);
  565. if (!async)
  566. return -ENOMEM;
  567. init_waitqueue_head(&async->wait_head);
  568. s->async = async;
  569. async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
  570. buf_size = comedi_default_buf_size_kb * 1024;
  571. if (buf_size > async->max_bufsize)
  572. buf_size = async->max_bufsize;
  573. if (comedi_buf_alloc(dev, s, buf_size) < 0) {
  574. dev_warn(dev->class_dev, "Buffer allocation failed\n");
  575. return -ENOMEM;
  576. }
  577. if (s->buf_change) {
  578. ret = s->buf_change(dev, s);
  579. if (ret < 0)
  580. return ret;
  581. }
  582. comedi_alloc_subdevice_minor(s);
  583. return 0;
  584. }
  585. static int __comedi_device_postconfig(struct comedi_device *dev)
  586. {
  587. struct comedi_subdevice *s;
  588. int ret;
  589. int i;
  590. for (i = 0; i < dev->n_subdevices; i++) {
  591. s = &dev->subdevices[i];
  592. if (s->type == COMEDI_SUBD_UNUSED)
  593. continue;
  594. if (s->type == COMEDI_SUBD_DO) {
  595. if (s->n_chan < 32)
  596. s->io_bits = (1 << s->n_chan) - 1;
  597. else
  598. s->io_bits = 0xffffffff;
  599. }
  600. if (s->len_chanlist == 0)
  601. s->len_chanlist = 1;
  602. if (s->do_cmd) {
  603. ret = __comedi_device_postconfig_async(dev, s);
  604. if (ret)
  605. return ret;
  606. }
  607. if (!s->range_table && !s->range_table_list)
  608. s->range_table = &range_unknown;
  609. if (!s->insn_read && s->insn_bits)
  610. s->insn_read = insn_rw_emulate_bits;
  611. if (!s->insn_write && s->insn_bits)
  612. s->insn_write = insn_rw_emulate_bits;
  613. if (!s->insn_read)
  614. s->insn_read = insn_inval;
  615. if (!s->insn_write)
  616. s->insn_write = insn_inval;
  617. if (!s->insn_bits)
  618. s->insn_bits = insn_inval;
  619. if (!s->insn_config)
  620. s->insn_config = insn_inval;
  621. if (!s->poll)
  622. s->poll = poll_invalid;
  623. }
  624. return 0;
  625. }
  626. /* do a little post-config cleanup */
  627. static int comedi_device_postconfig(struct comedi_device *dev)
  628. {
  629. int ret;
  630. ret = __comedi_device_postconfig(dev);
  631. if (ret < 0)
  632. return ret;
  633. down_write(&dev->attach_lock);
  634. dev->attached = true;
  635. up_write(&dev->attach_lock);
  636. return 0;
  637. }
  638. /*
  639. * Generic recognize function for drivers that register their supported
  640. * board names.
  641. *
  642. * 'driv->board_name' points to a 'const char *' member within the
  643. * zeroth element of an array of some private board information
  644. * structure, say 'struct foo_board' containing a member 'const char
  645. * *board_name' that is initialized to point to a board name string that
  646. * is one of the candidates matched against this function's 'name'
  647. * parameter.
  648. *
  649. * 'driv->offset' is the size of the private board information
  650. * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
  651. * the length of the array of private board information structures.
  652. *
  653. * If one of the board names in the array of private board information
  654. * structures matches the name supplied to this function, the function
  655. * returns a pointer to the pointer to the board name, otherwise it
  656. * returns NULL. The return value ends up in the 'board_ptr' member of
  657. * a 'struct comedi_device' that the low-level comedi driver's
  658. * 'attach()' hook can convert to a point to a particular element of its
  659. * array of private board information structures by subtracting the
  660. * offset of the member that points to the board name. (No subtraction
  661. * is required if the board name pointer is the first member of the
  662. * private board information structure, which is generally the case.)
  663. */
  664. static void *comedi_recognize(struct comedi_driver *driv, const char *name)
  665. {
  666. char **name_ptr = (char **)driv->board_name;
  667. int i;
  668. for (i = 0; i < driv->num_names; i++) {
  669. if (strcmp(*name_ptr, name) == 0)
  670. return name_ptr;
  671. name_ptr = (void *)name_ptr + driv->offset;
  672. }
  673. return NULL;
  674. }
  675. static void comedi_report_boards(struct comedi_driver *driv)
  676. {
  677. unsigned int i;
  678. const char *const *name_ptr;
  679. pr_info("comedi: valid board names for %s driver are:\n",
  680. driv->driver_name);
  681. name_ptr = driv->board_name;
  682. for (i = 0; i < driv->num_names; i++) {
  683. pr_info(" %s\n", *name_ptr);
  684. name_ptr = (const char **)((char *)name_ptr + driv->offset);
  685. }
  686. if (driv->num_names == 0)
  687. pr_info(" %s\n", driv->driver_name);
  688. }
  689. /**
  690. * comedi_load_firmware() - Request and load firmware for a device
  691. * @dev: COMEDI device.
  692. * @device: Hardware device.
  693. * @name: The name of the firmware image.
  694. * @cb: Callback to the upload the firmware image.
  695. * @context: Private context from the driver.
  696. *
  697. * Sends a firmware request for the hardware device and waits for it. Calls
  698. * the callback function to upload the firmware to the device, them releases
  699. * the firmware.
  700. *
  701. * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number
  702. * from the firmware request or the callback function.
  703. */
  704. int comedi_load_firmware(struct comedi_device *dev,
  705. struct device *device,
  706. const char *name,
  707. int (*cb)(struct comedi_device *dev,
  708. const u8 *data, size_t size,
  709. unsigned long context),
  710. unsigned long context)
  711. {
  712. const struct firmware *fw;
  713. int ret;
  714. if (!cb)
  715. return -EINVAL;
  716. ret = request_firmware(&fw, name, device);
  717. if (ret == 0) {
  718. ret = cb(dev, fw->data, fw->size, context);
  719. release_firmware(fw);
  720. }
  721. return ret < 0 ? ret : 0;
  722. }
  723. EXPORT_SYMBOL_GPL(comedi_load_firmware);
  724. /**
  725. * __comedi_request_region() - Request an I/O region for a legacy driver
  726. * @dev: COMEDI device.
  727. * @start: Base address of the I/O region.
  728. * @len: Length of the I/O region.
  729. *
  730. * Requests the specified I/O port region which must start at a non-zero
  731. * address.
  732. *
  733. * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
  734. * fails.
  735. */
  736. int __comedi_request_region(struct comedi_device *dev,
  737. unsigned long start, unsigned long len)
  738. {
  739. if (!start) {
  740. dev_warn(dev->class_dev,
  741. "%s: a I/O base address must be specified\n",
  742. dev->board_name);
  743. return -EINVAL;
  744. }
  745. if (!request_region(start, len, dev->board_name)) {
  746. dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
  747. dev->board_name, start, len);
  748. return -EIO;
  749. }
  750. return 0;
  751. }
  752. EXPORT_SYMBOL_GPL(__comedi_request_region);
  753. /**
  754. * comedi_request_region() - Request an I/O region for a legacy driver
  755. * @dev: COMEDI device.
  756. * @start: Base address of the I/O region.
  757. * @len: Length of the I/O region.
  758. *
  759. * Requests the specified I/O port region which must start at a non-zero
  760. * address.
  761. *
  762. * On success, @dev->iobase is set to the base address of the region and
  763. * @dev->iolen is set to its length.
  764. *
  765. * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
  766. * fails.
  767. */
  768. int comedi_request_region(struct comedi_device *dev,
  769. unsigned long start, unsigned long len)
  770. {
  771. int ret;
  772. ret = __comedi_request_region(dev, start, len);
  773. if (ret == 0) {
  774. dev->iobase = start;
  775. dev->iolen = len;
  776. }
  777. return ret;
  778. }
  779. EXPORT_SYMBOL_GPL(comedi_request_region);
  780. /**
  781. * comedi_legacy_detach() - A generic (*detach) function for legacy drivers
  782. * @dev: COMEDI device.
  783. *
  784. * This is a simple, generic 'detach' handler for legacy COMEDI devices that
  785. * just use a single I/O port region and possibly an IRQ and that don't need
  786. * any special clean-up for their private device or subdevice storage. It
  787. * can also be called by a driver-specific 'detach' handler.
  788. *
  789. * If @dev->irq is non-zero, the IRQ will be freed. If @dev->iobase and
  790. * @dev->iolen are both non-zero, the I/O port region will be released.
  791. */
  792. void comedi_legacy_detach(struct comedi_device *dev)
  793. {
  794. if (dev->irq) {
  795. free_irq(dev->irq, dev);
  796. dev->irq = 0;
  797. }
  798. if (dev->iobase && dev->iolen) {
  799. release_region(dev->iobase, dev->iolen);
  800. dev->iobase = 0;
  801. dev->iolen = 0;
  802. }
  803. }
  804. EXPORT_SYMBOL_GPL(comedi_legacy_detach);
  805. int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
  806. {
  807. struct comedi_driver *driv;
  808. int ret;
  809. if (dev->attached)
  810. return -EBUSY;
  811. mutex_lock(&comedi_drivers_list_lock);
  812. for (driv = comedi_drivers; driv; driv = driv->next) {
  813. if (!try_module_get(driv->module))
  814. continue;
  815. if (driv->num_names) {
  816. dev->board_ptr = comedi_recognize(driv, it->board_name);
  817. if (dev->board_ptr)
  818. break;
  819. } else if (strcmp(driv->driver_name, it->board_name) == 0) {
  820. break;
  821. }
  822. module_put(driv->module);
  823. }
  824. if (!driv) {
  825. /* recognize has failed if we get here */
  826. /* report valid board names before returning error */
  827. for (driv = comedi_drivers; driv; driv = driv->next) {
  828. if (!try_module_get(driv->module))
  829. continue;
  830. comedi_report_boards(driv);
  831. module_put(driv->module);
  832. }
  833. ret = -EIO;
  834. goto out;
  835. }
  836. if (!driv->attach) {
  837. /* driver does not support manual configuration */
  838. dev_warn(dev->class_dev,
  839. "driver '%s' does not support attach using comedi_config\n",
  840. driv->driver_name);
  841. module_put(driv->module);
  842. ret = -EIO;
  843. goto out;
  844. }
  845. dev->driver = driv;
  846. dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
  847. : dev->driver->driver_name;
  848. ret = driv->attach(dev, it);
  849. if (ret >= 0)
  850. ret = comedi_device_postconfig(dev);
  851. if (ret < 0) {
  852. comedi_device_detach(dev);
  853. module_put(driv->module);
  854. }
  855. /* On success, the driver module count has been incremented. */
  856. out:
  857. mutex_unlock(&comedi_drivers_list_lock);
  858. return ret;
  859. }
  860. /**
  861. * comedi_auto_config() - Create a COMEDI device for a hardware device
  862. * @hardware_device: Hardware device.
  863. * @driver: COMEDI low-level driver for the hardware device.
  864. * @context: Driver context for the auto_attach handler.
  865. *
  866. * Allocates a new COMEDI device for the hardware device and calls the
  867. * low-level driver's 'auto_attach' handler to set-up the hardware and
  868. * allocate the COMEDI subdevices. Additional "post-configuration" setting
  869. * up is performed on successful return from the 'auto_attach' handler.
  870. * If the 'auto_attach' handler fails, the low-level driver's 'detach'
  871. * handler will be called as part of the clean-up.
  872. *
  873. * This is usually called from a wrapper function in a bus-specific COMEDI
  874. * module, which in turn is usually called from a bus device 'probe'
  875. * function in the low-level driver.
  876. *
  877. * Returns 0 on success, -EINVAL if the parameters are invalid or the
  878. * post-configuration determines the driver has set the COMEDI device up
  879. * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of
  880. * COMEDI minor device numbers, or some negative error number returned by
  881. * the driver's 'auto_attach' handler.
  882. */
  883. int comedi_auto_config(struct device *hardware_device,
  884. struct comedi_driver *driver, unsigned long context)
  885. {
  886. struct comedi_device *dev;
  887. int ret;
  888. if (!hardware_device) {
  889. pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
  890. return -EINVAL;
  891. }
  892. if (!driver) {
  893. dev_warn(hardware_device,
  894. "BUG! comedi_auto_config called with NULL comedi driver\n");
  895. return -EINVAL;
  896. }
  897. if (!driver->auto_attach) {
  898. dev_warn(hardware_device,
  899. "BUG! comedi driver '%s' has no auto_attach handler\n",
  900. driver->driver_name);
  901. return -EINVAL;
  902. }
  903. dev = comedi_alloc_board_minor(hardware_device);
  904. if (IS_ERR(dev)) {
  905. dev_warn(hardware_device,
  906. "driver '%s' could not create device.\n",
  907. driver->driver_name);
  908. return PTR_ERR(dev);
  909. }
  910. /* Note: comedi_alloc_board_minor() locked dev->mutex. */
  911. dev->driver = driver;
  912. dev->board_name = dev->driver->driver_name;
  913. ret = driver->auto_attach(dev, context);
  914. if (ret >= 0)
  915. ret = comedi_device_postconfig(dev);
  916. mutex_unlock(&dev->mutex);
  917. if (ret < 0) {
  918. dev_warn(hardware_device,
  919. "driver '%s' failed to auto-configure device.\n",
  920. driver->driver_name);
  921. comedi_release_hardware_device(hardware_device);
  922. } else {
  923. /*
  924. * class_dev should be set properly here
  925. * after a successful auto config
  926. */
  927. dev_info(dev->class_dev,
  928. "driver '%s' has successfully auto-configured '%s'.\n",
  929. driver->driver_name, dev->board_name);
  930. }
  931. return ret;
  932. }
  933. EXPORT_SYMBOL_GPL(comedi_auto_config);
  934. /**
  935. * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device
  936. * @hardware_device: Hardware device previously passed to
  937. * comedi_auto_config().
  938. *
  939. * Cleans up and eventually destroys the COMEDI device allocated by
  940. * comedi_auto_config() for the same hardware device. As part of this
  941. * clean-up, the low-level COMEDI driver's 'detach' handler will be called.
  942. * (The COMEDI device itself will persist in an unattached state if it is
  943. * still open, until it is released, and any mmapped buffers will persist
  944. * until they are munmapped.)
  945. *
  946. * This is usually called from a wrapper module in a bus-specific COMEDI
  947. * module, which in turn is usually set as the bus device 'remove' function
  948. * in the low-level COMEDI driver.
  949. */
  950. void comedi_auto_unconfig(struct device *hardware_device)
  951. {
  952. if (!hardware_device)
  953. return;
  954. comedi_release_hardware_device(hardware_device);
  955. }
  956. EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
  957. /**
  958. * comedi_driver_register() - Register a low-level COMEDI driver
  959. * @driver: Low-level COMEDI driver.
  960. *
  961. * The low-level COMEDI driver is added to the list of registered COMEDI
  962. * drivers. This is used by the handler for the "/proc/comedi" file and is
  963. * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure
  964. * "legacy" COMEDI devices (for those low-level drivers that support it).
  965. *
  966. * Returns 0.
  967. */
  968. int comedi_driver_register(struct comedi_driver *driver)
  969. {
  970. mutex_lock(&comedi_drivers_list_lock);
  971. driver->next = comedi_drivers;
  972. comedi_drivers = driver;
  973. mutex_unlock(&comedi_drivers_list_lock);
  974. return 0;
  975. }
  976. EXPORT_SYMBOL_GPL(comedi_driver_register);
  977. /**
  978. * comedi_driver_unregister() - Unregister a low-level COMEDI driver
  979. * @driver: Low-level COMEDI driver.
  980. *
  981. * The low-level COMEDI driver is removed from the list of registered COMEDI
  982. * drivers. Detaches any COMEDI devices attached to the driver, which will
  983. * result in the low-level driver's 'detach' handler being called for those
  984. * devices before this function returns.
  985. */
  986. void comedi_driver_unregister(struct comedi_driver *driver)
  987. {
  988. struct comedi_driver *prev;
  989. int i;
  990. /* unlink the driver */
  991. mutex_lock(&comedi_drivers_list_lock);
  992. if (comedi_drivers == driver) {
  993. comedi_drivers = driver->next;
  994. } else {
  995. for (prev = comedi_drivers; prev->next; prev = prev->next) {
  996. if (prev->next == driver) {
  997. prev->next = driver->next;
  998. break;
  999. }
  1000. }
  1001. }
  1002. mutex_unlock(&comedi_drivers_list_lock);
  1003. /* check for devices using this driver */
  1004. for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
  1005. struct comedi_device *dev = comedi_dev_get_from_minor(i);
  1006. if (!dev)
  1007. continue;
  1008. mutex_lock(&dev->mutex);
  1009. if (dev->attached && dev->driver == driver) {
  1010. if (dev->use_count)
  1011. dev_warn(dev->class_dev,
  1012. "BUG! detaching device with use_count=%d\n",
  1013. dev->use_count);
  1014. comedi_device_detach(dev);
  1015. }
  1016. mutex_unlock(&dev->mutex);
  1017. comedi_dev_put(dev);
  1018. }
  1019. }
  1020. EXPORT_SYMBOL_GPL(comedi_driver_unregister);