drivers.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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 = kzalloc_objs(*s, num_subdevices);
  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. static unsigned int _comedi_bytes_per_scan(struct comedi_subdevice *s)
  405. {
  406. struct comedi_cmd *cmd = &s->async->cmd;
  407. return comedi_bytes_per_scan_cmd(s, cmd);
  408. }
  409. /**
  410. * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes
  411. * @s: COMEDI subdevice.
  412. *
  413. * Determines the overall scan length according to the subdevice type and the
  414. * number of channels in the scan for the current command.
  415. *
  416. * For digital input, output or input/output subdevices, samples for
  417. * multiple channels are assumed to be packed into one or more unsigned
  418. * short or unsigned int values according to the subdevice's %SDF_LSAMPL
  419. * flag. For other types of subdevice, samples are assumed to occupy a
  420. * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag.
  421. *
  422. * Returns the overall scan length in bytes.
  423. */
  424. unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
  425. {
  426. unsigned int num_bytes;
  427. if (comedi_get_is_subdevice_running(s)) {
  428. num_bytes = _comedi_bytes_per_scan(s);
  429. comedi_put_is_subdevice_running(s);
  430. } else {
  431. /* Use nomimal, single sample scan length. */
  432. num_bytes = comedi_samples_to_bytes(s, 1);
  433. }
  434. return num_bytes;
  435. }
  436. EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
  437. static unsigned int __comedi_nscans_left(struct comedi_subdevice *s,
  438. unsigned int nscans)
  439. {
  440. struct comedi_async *async = s->async;
  441. struct comedi_cmd *cmd = &async->cmd;
  442. if (cmd->stop_src == TRIG_COUNT) {
  443. unsigned int scans_left = 0;
  444. if (async->scans_done < cmd->stop_arg)
  445. scans_left = cmd->stop_arg - async->scans_done;
  446. if (nscans > scans_left)
  447. nscans = scans_left;
  448. }
  449. return nscans;
  450. }
  451. static unsigned int _comedi_nscans_left(struct comedi_subdevice *s,
  452. unsigned int nscans)
  453. {
  454. if (nscans == 0) {
  455. unsigned int nbytes = _comedi_buf_read_n_available(s);
  456. nscans = nbytes / _comedi_bytes_per_scan(s);
  457. }
  458. return __comedi_nscans_left(s, nscans);
  459. }
  460. /**
  461. * comedi_nscans_left() - Return the number of scans left in the command
  462. * @s: COMEDI subdevice.
  463. * @nscans: The expected number of scans or 0 for all available scans.
  464. *
  465. * If @nscans is 0, it is set to the number of scans available in the
  466. * async buffer.
  467. *
  468. * If the async command has a stop_src of %TRIG_COUNT, the @nscans will be
  469. * checked against the number of scans remaining to complete the command.
  470. *
  471. * The return value will then be either the expected number of scans or the
  472. * number of scans remaining to complete the command, whichever is fewer.
  473. */
  474. unsigned int comedi_nscans_left(struct comedi_subdevice *s,
  475. unsigned int nscans)
  476. {
  477. if (comedi_get_is_subdevice_running(s)) {
  478. nscans = _comedi_nscans_left(s, nscans);
  479. comedi_put_is_subdevice_running(s);
  480. } else {
  481. nscans = 0;
  482. }
  483. return nscans;
  484. }
  485. EXPORT_SYMBOL_GPL(comedi_nscans_left);
  486. static unsigned int _comedi_nsamples_left(struct comedi_subdevice *s,
  487. unsigned int nsamples)
  488. {
  489. struct comedi_async *async = s->async;
  490. struct comedi_cmd *cmd = &async->cmd;
  491. unsigned long long scans_left;
  492. unsigned long long samples_left;
  493. if (cmd->stop_src != TRIG_COUNT)
  494. return nsamples;
  495. scans_left = __comedi_nscans_left(s, cmd->stop_arg);
  496. if (!scans_left)
  497. return 0;
  498. samples_left = scans_left * cmd->scan_end_arg -
  499. comedi_bytes_to_samples(s, async->scan_progress);
  500. if (samples_left < nsamples)
  501. return samples_left;
  502. return nsamples;
  503. }
  504. /**
  505. * comedi_nsamples_left() - Return the number of samples left in the command
  506. * @s: COMEDI subdevice.
  507. * @nsamples: The expected number of samples.
  508. *
  509. * Returns the number of samples remaining to complete the command, or the
  510. * specified expected number of samples (@nsamples), whichever is fewer.
  511. */
  512. unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
  513. unsigned int nsamples)
  514. {
  515. if (comedi_get_is_subdevice_running(s)) {
  516. nsamples = _comedi_nsamples_left(s, nsamples);
  517. comedi_put_is_subdevice_running(s);
  518. } else {
  519. nsamples = 0;
  520. }
  521. return nsamples;
  522. }
  523. EXPORT_SYMBOL_GPL(comedi_nsamples_left);
  524. void _comedi_inc_scan_progress(struct comedi_subdevice *s,
  525. unsigned int num_bytes)
  526. {
  527. struct comedi_async *async = s->async;
  528. struct comedi_cmd *cmd = &async->cmd;
  529. unsigned int scan_length = _comedi_bytes_per_scan(s);
  530. /* track the 'cur_chan' for non-SDF_PACKED subdevices */
  531. if (!(s->subdev_flags & SDF_PACKED)) {
  532. async->cur_chan += comedi_bytes_to_samples(s, num_bytes);
  533. async->cur_chan %= cmd->chanlist_len;
  534. }
  535. async->scan_progress += num_bytes;
  536. if (async->scan_progress >= scan_length) {
  537. unsigned int nscans = async->scan_progress / scan_length;
  538. if (async->scans_done < (UINT_MAX - nscans))
  539. async->scans_done += nscans;
  540. else
  541. async->scans_done = UINT_MAX;
  542. async->scan_progress %= scan_length;
  543. async->events |= COMEDI_CB_EOS;
  544. }
  545. }
  546. /**
  547. * comedi_inc_scan_progress() - Update scan progress in asynchronous command
  548. * @s: COMEDI subdevice.
  549. * @num_bytes: Amount of data in bytes to increment scan progress.
  550. *
  551. * Increments the scan progress by the number of bytes specified by @num_bytes.
  552. * If the scan progress reaches or exceeds the scan length in bytes, reduce
  553. * it modulo the scan length in bytes and set the "end of scan" asynchronous
  554. * event flag (%COMEDI_CB_EOS) to be processed later.
  555. */
  556. void comedi_inc_scan_progress(struct comedi_subdevice *s,
  557. unsigned int num_bytes)
  558. {
  559. if (comedi_get_is_subdevice_running(s)) {
  560. _comedi_inc_scan_progress(s, num_bytes);
  561. comedi_put_is_subdevice_running(s);
  562. }
  563. }
  564. EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
  565. static unsigned int _comedi_handle_events(struct comedi_device *dev,
  566. struct comedi_subdevice *s)
  567. {
  568. unsigned int events = s->async->events;
  569. if (events == 0)
  570. return events;
  571. if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel)
  572. s->cancel(dev, s);
  573. _comedi_event(dev, s);
  574. return events;
  575. }
  576. /**
  577. * comedi_handle_events() - Handle events and possibly stop acquisition
  578. * @dev: COMEDI device.
  579. * @s: COMEDI subdevice.
  580. *
  581. * Handles outstanding asynchronous acquisition event flags associated
  582. * with the subdevice. Call the subdevice's @s->cancel() handler if the
  583. * "end of acquisition", "error" or "overflow" event flags are set in order
  584. * to stop the acquisition at the driver level.
  585. *
  586. * Calls comedi_event() to further process the event flags, which may mark
  587. * the asynchronous command as no longer running, possibly terminated with
  588. * an error, and may wake up tasks.
  589. *
  590. * Return a bit-mask of the handled events.
  591. */
  592. unsigned int comedi_handle_events(struct comedi_device *dev,
  593. struct comedi_subdevice *s)
  594. {
  595. unsigned int events;
  596. if (comedi_get_is_subdevice_running(s)) {
  597. events = _comedi_handle_events(dev, s);
  598. comedi_put_is_subdevice_running(s);
  599. } else {
  600. events = 0;
  601. }
  602. return events;
  603. }
  604. EXPORT_SYMBOL_GPL(comedi_handle_events);
  605. static int insn_rw_emulate_bits(struct comedi_device *dev,
  606. struct comedi_subdevice *s,
  607. struct comedi_insn *insn,
  608. unsigned int *data)
  609. {
  610. struct comedi_insn _insn;
  611. unsigned int chan = CR_CHAN(insn->chanspec);
  612. unsigned int base_chan = (chan < 32) ? 0 : chan;
  613. unsigned int _data[2];
  614. unsigned int i;
  615. int ret;
  616. memset(_data, 0, sizeof(_data));
  617. memset(&_insn, 0, sizeof(_insn));
  618. _insn.insn = INSN_BITS;
  619. _insn.chanspec = base_chan;
  620. _insn.n = 2;
  621. _insn.subdev = insn->subdev;
  622. if (insn->insn == INSN_WRITE) {
  623. if (!(s->subdev_flags & SDF_WRITABLE))
  624. return -EINVAL;
  625. _data[0] = 1U << (chan - base_chan); /* mask */
  626. }
  627. for (i = 0; i < insn->n; i++) {
  628. if (insn->insn == INSN_WRITE)
  629. _data[1] = data[i] ? _data[0] : 0; /* bits */
  630. ret = s->insn_bits(dev, s, &_insn, _data);
  631. if (ret < 0)
  632. return ret;
  633. if (insn->insn == INSN_READ)
  634. data[i] = (_data[1] >> (chan - base_chan)) & 1;
  635. }
  636. return insn->n;
  637. }
  638. static int __comedi_device_postconfig_async(struct comedi_device *dev,
  639. struct comedi_subdevice *s)
  640. {
  641. struct comedi_async *async;
  642. unsigned int buf_size;
  643. int ret;
  644. lockdep_assert_held(&dev->mutex);
  645. if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
  646. dev_warn(dev->class_dev,
  647. "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
  648. return -EINVAL;
  649. }
  650. if (!s->do_cmdtest) {
  651. dev_warn(dev->class_dev,
  652. "async subdevices must have a do_cmdtest() function\n");
  653. return -EINVAL;
  654. }
  655. if (!s->cancel)
  656. dev_warn(dev->class_dev,
  657. "async subdevices should have a cancel() function\n");
  658. async = kzalloc_obj(*async);
  659. if (!async)
  660. return -ENOMEM;
  661. init_waitqueue_head(&async->wait_head);
  662. init_completion(&async->run_complete);
  663. s->async = async;
  664. async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
  665. buf_size = comedi_default_buf_size_kb * 1024;
  666. if (buf_size > async->max_bufsize)
  667. buf_size = async->max_bufsize;
  668. if (comedi_buf_alloc(dev, s, buf_size) < 0) {
  669. dev_warn(dev->class_dev, "Buffer allocation failed\n");
  670. return -ENOMEM;
  671. }
  672. if (s->buf_change) {
  673. ret = s->buf_change(dev, s);
  674. if (ret < 0)
  675. return ret;
  676. }
  677. comedi_alloc_subdevice_minor(s);
  678. return 0;
  679. }
  680. static int __comedi_device_postconfig(struct comedi_device *dev)
  681. {
  682. struct comedi_subdevice *s;
  683. int ret;
  684. int i;
  685. lockdep_assert_held(&dev->mutex);
  686. if (!dev->insn_device_config)
  687. dev->insn_device_config = insn_device_inval;
  688. if (!dev->get_valid_routes)
  689. dev->get_valid_routes = get_zero_valid_routes;
  690. for (i = 0; i < dev->n_subdevices; i++) {
  691. s = &dev->subdevices[i];
  692. if (s->type == COMEDI_SUBD_UNUSED)
  693. continue;
  694. if (s->type == COMEDI_SUBD_DO) {
  695. if (s->n_chan < 32)
  696. s->io_bits = (1U << s->n_chan) - 1;
  697. else
  698. s->io_bits = 0xffffffff;
  699. }
  700. if (s->len_chanlist == 0)
  701. s->len_chanlist = 1;
  702. if (s->do_cmd) {
  703. ret = __comedi_device_postconfig_async(dev, s);
  704. if (ret)
  705. return ret;
  706. }
  707. if (!s->range_table && !s->range_table_list)
  708. s->range_table = &range_unknown;
  709. if (!s->insn_read && s->insn_bits)
  710. s->insn_read = insn_rw_emulate_bits;
  711. if (!s->insn_write && s->insn_bits)
  712. s->insn_write = insn_rw_emulate_bits;
  713. if (!s->insn_read)
  714. s->insn_read = insn_inval;
  715. if (!s->insn_write)
  716. s->insn_write = insn_inval;
  717. if (!s->insn_bits)
  718. s->insn_bits = insn_inval;
  719. if (!s->insn_config)
  720. s->insn_config = insn_inval;
  721. if (!s->poll)
  722. s->poll = poll_invalid;
  723. }
  724. return 0;
  725. }
  726. /* do a little post-config cleanup */
  727. static int comedi_device_postconfig(struct comedi_device *dev)
  728. {
  729. int ret;
  730. lockdep_assert_held(&dev->mutex);
  731. ret = __comedi_device_postconfig(dev);
  732. if (ret < 0)
  733. return ret;
  734. down_write(&dev->attach_lock);
  735. dev->attached = true;
  736. up_write(&dev->attach_lock);
  737. return 0;
  738. }
  739. /*
  740. * Generic recognize function for drivers that register their supported
  741. * board names.
  742. *
  743. * 'driv->board_name' points to a 'const char *' member within the
  744. * zeroth element of an array of some private board information
  745. * structure, say 'struct foo_board' containing a member 'const char
  746. * *board_name' that is initialized to point to a board name string that
  747. * is one of the candidates matched against this function's 'name'
  748. * parameter.
  749. *
  750. * 'driv->offset' is the size of the private board information
  751. * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
  752. * the length of the array of private board information structures.
  753. *
  754. * If one of the board names in the array of private board information
  755. * structures matches the name supplied to this function, the function
  756. * returns a pointer to the pointer to the board name, otherwise it
  757. * returns NULL. The return value ends up in the 'board_ptr' member of
  758. * a 'struct comedi_device' that the low-level comedi driver's
  759. * 'attach()' hook can convert to a point to a particular element of its
  760. * array of private board information structures by subtracting the
  761. * offset of the member that points to the board name. (No subtraction
  762. * is required if the board name pointer is the first member of the
  763. * private board information structure, which is generally the case.)
  764. */
  765. static void *comedi_recognize(struct comedi_driver *driv, const char *name)
  766. {
  767. char **name_ptr = (char **)driv->board_name;
  768. int i;
  769. for (i = 0; i < driv->num_names; i++) {
  770. if (strcmp(*name_ptr, name) == 0)
  771. return name_ptr;
  772. name_ptr = (void *)name_ptr + driv->offset;
  773. }
  774. return NULL;
  775. }
  776. static void comedi_report_boards(struct comedi_driver *driv)
  777. {
  778. unsigned int i;
  779. const char *const *name_ptr;
  780. pr_info("comedi: valid board names for %s driver are:\n",
  781. driv->driver_name);
  782. name_ptr = driv->board_name;
  783. for (i = 0; i < driv->num_names; i++) {
  784. pr_info(" %s\n", *name_ptr);
  785. name_ptr = (const char **)((char *)name_ptr + driv->offset);
  786. }
  787. if (driv->num_names == 0)
  788. pr_info(" %s\n", driv->driver_name);
  789. }
  790. /**
  791. * comedi_load_firmware() - Request and load firmware for a device
  792. * @dev: COMEDI device.
  793. * @device: Hardware device.
  794. * @name: The name of the firmware image.
  795. * @cb: Callback to the upload the firmware image.
  796. * @context: Private context from the driver.
  797. *
  798. * Sends a firmware request for the hardware device and waits for it. Calls
  799. * the callback function to upload the firmware to the device, them releases
  800. * the firmware.
  801. *
  802. * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number
  803. * from the firmware request or the callback function.
  804. */
  805. int comedi_load_firmware(struct comedi_device *dev,
  806. struct device *device,
  807. const char *name,
  808. int (*cb)(struct comedi_device *dev,
  809. const u8 *data, size_t size,
  810. unsigned long context),
  811. unsigned long context)
  812. {
  813. const struct firmware *fw;
  814. int ret;
  815. if (!cb)
  816. return -EINVAL;
  817. ret = request_firmware(&fw, name, device);
  818. if (ret == 0) {
  819. ret = cb(dev, fw->data, fw->size, context);
  820. release_firmware(fw);
  821. }
  822. return min(ret, 0);
  823. }
  824. EXPORT_SYMBOL_GPL(comedi_load_firmware);
  825. /**
  826. * __comedi_request_region() - Request an I/O region for a legacy driver
  827. * @dev: COMEDI device.
  828. * @start: Base address of the I/O region.
  829. * @len: Length of the I/O region.
  830. *
  831. * Requests the specified I/O port region which must start at a non-zero
  832. * address.
  833. *
  834. * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
  835. * fails.
  836. */
  837. int __comedi_request_region(struct comedi_device *dev,
  838. unsigned long start, unsigned long len)
  839. {
  840. if (!start) {
  841. dev_warn(dev->class_dev,
  842. "%s: a I/O base address must be specified\n",
  843. dev->board_name);
  844. return -EINVAL;
  845. }
  846. if (!request_region(start, len, dev->board_name)) {
  847. dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
  848. dev->board_name, start, len);
  849. return -EIO;
  850. }
  851. return 0;
  852. }
  853. EXPORT_SYMBOL_GPL(__comedi_request_region);
  854. /**
  855. * comedi_request_region() - Request an I/O region for a legacy driver
  856. * @dev: COMEDI device.
  857. * @start: Base address of the I/O region.
  858. * @len: Length of the I/O region.
  859. *
  860. * Requests the specified I/O port region which must start at a non-zero
  861. * address.
  862. *
  863. * On success, @dev->iobase is set to the base address of the region and
  864. * @dev->iolen is set to its length.
  865. *
  866. * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
  867. * fails.
  868. */
  869. int comedi_request_region(struct comedi_device *dev,
  870. unsigned long start, unsigned long len)
  871. {
  872. int ret;
  873. ret = __comedi_request_region(dev, start, len);
  874. if (ret == 0) {
  875. dev->iobase = start;
  876. dev->iolen = len;
  877. }
  878. return ret;
  879. }
  880. EXPORT_SYMBOL_GPL(comedi_request_region);
  881. /**
  882. * comedi_legacy_detach() - A generic (*detach) function for legacy drivers
  883. * @dev: COMEDI device.
  884. *
  885. * This is a simple, generic 'detach' handler for legacy COMEDI devices that
  886. * just use a single I/O port region and possibly an IRQ and that don't need
  887. * any special clean-up for their private device or subdevice storage. It
  888. * can also be called by a driver-specific 'detach' handler.
  889. *
  890. * If @dev->irq is non-zero, the IRQ will be freed. If @dev->iobase and
  891. * @dev->iolen are both non-zero, the I/O port region will be released.
  892. */
  893. void comedi_legacy_detach(struct comedi_device *dev)
  894. {
  895. if (dev->irq) {
  896. free_irq(dev->irq, dev);
  897. dev->irq = 0;
  898. }
  899. if (dev->iobase && dev->iolen) {
  900. release_region(dev->iobase, dev->iolen);
  901. dev->iobase = 0;
  902. dev->iolen = 0;
  903. }
  904. }
  905. EXPORT_SYMBOL_GPL(comedi_legacy_detach);
  906. int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
  907. {
  908. struct comedi_driver *driv;
  909. int ret;
  910. lockdep_assert_held(&dev->mutex);
  911. if (dev->attached)
  912. return -EBUSY;
  913. mutex_lock(&comedi_drivers_list_lock);
  914. for (driv = comedi_drivers; driv; driv = driv->next) {
  915. if (!try_module_get(driv->module))
  916. continue;
  917. if (driv->num_names) {
  918. dev->board_ptr = comedi_recognize(driv, it->board_name);
  919. if (dev->board_ptr)
  920. break;
  921. } else if (strcmp(driv->driver_name, it->board_name) == 0) {
  922. break;
  923. }
  924. module_put(driv->module);
  925. }
  926. if (!driv) {
  927. /* recognize has failed if we get here */
  928. /* report valid board names before returning error */
  929. for (driv = comedi_drivers; driv; driv = driv->next) {
  930. if (!try_module_get(driv->module))
  931. continue;
  932. comedi_report_boards(driv);
  933. module_put(driv->module);
  934. }
  935. ret = -EIO;
  936. goto out;
  937. }
  938. if (!driv->attach) {
  939. /* driver does not support manual configuration */
  940. dev_warn(dev->class_dev,
  941. "driver '%s' does not support attach using comedi_config\n",
  942. driv->driver_name);
  943. module_put(driv->module);
  944. ret = -EIO;
  945. goto out;
  946. }
  947. if (IS_ENABLED(CONFIG_LOCKDEP)) {
  948. /*
  949. * dev->spinlock is for private use by the attached low-level
  950. * driver. Reinitialize it to stop lock-dependency tracking
  951. * between attachments to different low-level drivers.
  952. */
  953. spin_lock_init(&dev->spinlock);
  954. }
  955. dev->driver = driv;
  956. dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
  957. : dev->driver->driver_name;
  958. ret = driv->attach(dev, it);
  959. if (ret >= 0)
  960. ret = comedi_device_postconfig(dev);
  961. if (ret < 0) {
  962. comedi_device_detach(dev);
  963. module_put(driv->module);
  964. }
  965. /* On success, the driver module count has been incremented. */
  966. out:
  967. mutex_unlock(&comedi_drivers_list_lock);
  968. return ret;
  969. }
  970. /**
  971. * comedi_auto_config() - Create a COMEDI device for a hardware device
  972. * @hardware_device: Hardware device.
  973. * @driver: COMEDI low-level driver for the hardware device.
  974. * @context: Driver context for the auto_attach handler.
  975. *
  976. * Allocates a new COMEDI device for the hardware device and calls the
  977. * low-level driver's 'auto_attach' handler to set-up the hardware and
  978. * allocate the COMEDI subdevices. Additional "post-configuration" setting
  979. * up is performed on successful return from the 'auto_attach' handler.
  980. * If the 'auto_attach' handler fails, the low-level driver's 'detach'
  981. * handler will be called as part of the clean-up.
  982. *
  983. * This is usually called from a wrapper function in a bus-specific COMEDI
  984. * module, which in turn is usually called from a bus device 'probe'
  985. * function in the low-level driver.
  986. *
  987. * Returns 0 on success, -EINVAL if the parameters are invalid or the
  988. * post-configuration determines the driver has set the COMEDI device up
  989. * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of
  990. * COMEDI minor device numbers, or some negative error number returned by
  991. * the driver's 'auto_attach' handler.
  992. */
  993. int comedi_auto_config(struct device *hardware_device,
  994. struct comedi_driver *driver, unsigned long context)
  995. {
  996. struct comedi_device *dev;
  997. int ret;
  998. if (!hardware_device) {
  999. pr_warn("BUG! %s called with NULL hardware_device\n", __func__);
  1000. return -EINVAL;
  1001. }
  1002. if (!driver) {
  1003. dev_warn(hardware_device,
  1004. "BUG! %s called with NULL comedi driver\n", __func__);
  1005. return -EINVAL;
  1006. }
  1007. if (!driver->auto_attach) {
  1008. dev_warn(hardware_device,
  1009. "BUG! comedi driver '%s' has no auto_attach handler\n",
  1010. driver->driver_name);
  1011. return -EINVAL;
  1012. }
  1013. dev = comedi_alloc_board_minor(hardware_device);
  1014. if (IS_ERR(dev)) {
  1015. dev_warn(hardware_device,
  1016. "driver '%s' could not create device.\n",
  1017. driver->driver_name);
  1018. return PTR_ERR(dev);
  1019. }
  1020. /* Note: comedi_alloc_board_minor() locked dev->mutex. */
  1021. lockdep_assert_held(&dev->mutex);
  1022. dev->driver = driver;
  1023. dev->board_name = dev->driver->driver_name;
  1024. ret = driver->auto_attach(dev, context);
  1025. if (ret >= 0)
  1026. ret = comedi_device_postconfig(dev);
  1027. if (ret < 0) {
  1028. dev_warn(hardware_device,
  1029. "driver '%s' failed to auto-configure device.\n",
  1030. driver->driver_name);
  1031. mutex_unlock(&dev->mutex);
  1032. comedi_release_hardware_device(hardware_device);
  1033. } else {
  1034. /*
  1035. * class_dev should be set properly here
  1036. * after a successful auto config
  1037. */
  1038. dev_info(dev->class_dev,
  1039. "driver '%s' has successfully auto-configured '%s'.\n",
  1040. driver->driver_name, dev->board_name);
  1041. mutex_unlock(&dev->mutex);
  1042. }
  1043. return ret;
  1044. }
  1045. EXPORT_SYMBOL_GPL(comedi_auto_config);
  1046. /**
  1047. * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device
  1048. * @hardware_device: Hardware device previously passed to
  1049. * comedi_auto_config().
  1050. *
  1051. * Cleans up and eventually destroys the COMEDI device allocated by
  1052. * comedi_auto_config() for the same hardware device. As part of this
  1053. * clean-up, the low-level COMEDI driver's 'detach' handler will be called.
  1054. * (The COMEDI device itself will persist in an unattached state if it is
  1055. * still open, until it is released, and any mmapped buffers will persist
  1056. * until they are munmapped.)
  1057. *
  1058. * This is usually called from a wrapper module in a bus-specific COMEDI
  1059. * module, which in turn is usually set as the bus device 'remove' function
  1060. * in the low-level COMEDI driver.
  1061. */
  1062. void comedi_auto_unconfig(struct device *hardware_device)
  1063. {
  1064. if (!hardware_device)
  1065. return;
  1066. comedi_release_hardware_device(hardware_device);
  1067. }
  1068. EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
  1069. /**
  1070. * comedi_driver_register() - Register a low-level COMEDI driver
  1071. * @driver: Low-level COMEDI driver.
  1072. *
  1073. * The low-level COMEDI driver is added to the list of registered COMEDI
  1074. * drivers. This is used by the handler for the "/proc/comedi" file and is
  1075. * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure
  1076. * "legacy" COMEDI devices (for those low-level drivers that support it).
  1077. *
  1078. * Returns 0.
  1079. */
  1080. int comedi_driver_register(struct comedi_driver *driver)
  1081. {
  1082. mutex_lock(&comedi_drivers_list_lock);
  1083. driver->next = comedi_drivers;
  1084. comedi_drivers = driver;
  1085. mutex_unlock(&comedi_drivers_list_lock);
  1086. return 0;
  1087. }
  1088. EXPORT_SYMBOL_GPL(comedi_driver_register);
  1089. /**
  1090. * comedi_driver_unregister() - Unregister a low-level COMEDI driver
  1091. * @driver: Low-level COMEDI driver.
  1092. *
  1093. * The low-level COMEDI driver is removed from the list of registered COMEDI
  1094. * drivers. Detaches any COMEDI devices attached to the driver, which will
  1095. * result in the low-level driver's 'detach' handler being called for those
  1096. * devices before this function returns.
  1097. */
  1098. void comedi_driver_unregister(struct comedi_driver *driver)
  1099. {
  1100. struct comedi_driver *prev;
  1101. int i;
  1102. /* unlink the driver */
  1103. mutex_lock(&comedi_drivers_list_lock);
  1104. if (comedi_drivers == driver) {
  1105. comedi_drivers = driver->next;
  1106. } else {
  1107. for (prev = comedi_drivers; prev->next; prev = prev->next) {
  1108. if (prev->next == driver) {
  1109. prev->next = driver->next;
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. mutex_unlock(&comedi_drivers_list_lock);
  1115. /* check for devices using this driver */
  1116. for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
  1117. struct comedi_device *dev = comedi_dev_get_from_minor(i);
  1118. if (!dev)
  1119. continue;
  1120. mutex_lock(&dev->mutex);
  1121. if (dev->attached && dev->driver == driver) {
  1122. if (dev->use_count)
  1123. dev_warn(dev->class_dev,
  1124. "BUG! detaching device with use_count=%d\n",
  1125. dev->use_count);
  1126. comedi_device_detach(dev);
  1127. }
  1128. mutex_unlock(&dev->mutex);
  1129. comedi_dev_put(dev);
  1130. }
  1131. }
  1132. EXPORT_SYMBOL_GPL(comedi_driver_unregister);