blob: 8eb1f699a857baeaba8a37570327c33deeb185e5 [file] [log] [blame]
Greg Kroah-Hartmane184e2b2017-11-07 14:58:43 +01001// SPDX-License-Identifier: GPL-2.0+
David Schleefed9eccb2008-11-04 20:29:31 -08002/*
Marcos CanĂ¡n50fbb882015-09-16 17:48:57 -03003 * 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>
Ramiro Oliveira37a70292016-09-30 11:32:05 +01009 */
David Schleefed9eccb2008-11-04 20:29:31 -080010
David Schleefed9eccb2008-11-04 20:29:31 -080011#include <linux/device.h>
12#include <linux/module.h>
David Schleefed9eccb2008-11-04 20:29:31 -080013#include <linux/errno.h>
14#include <linux/kernel.h>
David Schleefed9eccb2008-11-04 20:29:31 -080015#include <linux/ioport.h>
David Schleefed9eccb2008-11-04 20:29:31 -080016#include <linux/slab.h>
Ian Abbottd35d8932015-09-17 17:19:16 +010017#include <linux/dma-direction.h>
H Hartley Sweeten3d1fe3f2013-04-18 14:34:37 -070018#include <linux/interrupt.h>
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -070019#include <linux/firmware.h>
Ian Abbottdf0e68c12021-11-17 12:05:59 +000020#include <linux/comedi/comedidev.h>
Ian Abbott3a5fa272012-06-19 10:17:44 +010021#include "comedi_internal.h"
Greg Kroah-Hartman242e7ad2010-05-03 15:20:29 -070022
Bill Pemberton139dfbd2009-03-16 22:05:25 -040023struct comedi_driver *comedi_drivers;
H Hartley Sweeten3df9f212014-07-18 14:28:11 -070024/* protects access to comedi_drivers */
Ian Abbottc383e2d2013-06-27 14:50:58 +010025DEFINE_MUTEX(comedi_drivers_list_lock);
David Schleefed9eccb2008-11-04 20:29:31 -080026
Ian Abbott9b348452015-09-17 17:19:17 +010027/**
28 * comedi_set_hw_dev() - Set hardware device associated with COMEDI device
29 * @dev: COMEDI device.
30 * @hw_dev: Hardware device.
31 *
32 * For automatically configured COMEDI devices (resulting from a call to
33 * comedi_auto_config() or one of its wrappers from the low-level COMEDI
34 * driver), comedi_set_hw_dev() is called automatically by the COMEDI core
35 * to associate the COMEDI device with the hardware device. It can also be
36 * called directly by "legacy" low-level COMEDI drivers that rely on the
37 * %COMEDI_DEVCONFIG ioctl to configure the hardware as long as the hardware
38 * has a &struct device.
39 *
40 * If @dev->hw_dev is NULL, it gets a reference to @hw_dev and sets
41 * @dev->hw_dev, otherwise, it does nothing. Calling it multiple times
42 * with the same hardware device is not considered an error. If it gets
43 * a reference to the hardware device, it will be automatically 'put' when
44 * the device is detached from COMEDI.
45 *
46 * Returns 0 if @dev->hw_dev was NULL or the same as @hw_dev, otherwise
47 * returns -EEXIST.
48 */
Ian Abbottda717512013-02-01 13:23:19 +000049int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
50{
Ian Abbottde06d7c2013-02-01 13:23:20 +000051 if (hw_dev == dev->hw_dev)
52 return 0;
H Hartley Sweetene2850162015-03-04 12:15:29 -070053 if (dev->hw_dev)
Ian Abbottde06d7c2013-02-01 13:23:20 +000054 return -EEXIST;
Ian Abbottda717512013-02-01 13:23:19 +000055 dev->hw_dev = get_device(hw_dev);
Ian Abbottda717512013-02-01 13:23:19 +000056 return 0;
57}
58EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
59
Ian Abbottde06d7c2013-02-01 13:23:20 +000060static void comedi_clear_hw_dev(struct comedi_device *dev)
61{
62 put_device(dev->hw_dev);
63 dev->hw_dev = NULL;
64}
65
H Hartley Sweeten54db9962013-06-24 16:55:14 -070066/**
Ian Abbott9b348452015-09-17 17:19:17 +010067 * comedi_alloc_devpriv() - Allocate memory for the device private data
68 * @dev: COMEDI device.
69 * @size: Size of the memory to allocate.
70 *
71 * The allocated memory is zero-filled. @dev->private points to it on
72 * return. The memory will be automatically freed when the COMEDI device is
73 * "detached".
74 *
75 * Returns a pointer to the allocated memory, or NULL on failure.
H Hartley Sweeten54db9962013-06-24 16:55:14 -070076 */
77void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
78{
79 dev->private = kzalloc(size, GFP_KERNEL);
80 return dev->private;
81}
82EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
83
Ian Abbott9b348452015-09-17 17:19:17 +010084/**
85 * comedi_alloc_subdevices() - Allocate subdevices for COMEDI device
86 * @dev: COMEDI device.
87 * @num_subdevices: Number of subdevices to allocate.
88 *
89 * Allocates and initializes an array of &struct comedi_subdevice for the
90 * COMEDI device. If successful, sets @dev->subdevices to point to the
91 * first one and @dev->n_subdevices to the number.
92 *
93 * Returns 0 on success, -EINVAL if @num_subdevices is < 1, or -ENOMEM if
94 * failed to allocate the memory.
95 */
H Hartley Sweeten8b9ba6e2012-06-12 11:57:27 -070096int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -070097{
H Hartley Sweeten03afcf42012-06-12 11:59:55 -070098 struct comedi_subdevice *s;
H Hartley Sweeten8b9ba6e2012-06-12 11:57:27 -070099 int i;
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700100
H Hartley Sweeten7f801c42012-06-12 11:57:45 -0700101 if (num_subdevices < 1)
102 return -EINVAL;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700103
104 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
105 if (!s)
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700106 return -ENOMEM;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700107 dev->subdevices = s;
H Hartley Sweetenfba1d0f2012-06-12 11:58:27 -0700108 dev->n_subdevices = num_subdevices;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700109
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700110 for (i = 0; i < num_subdevices; ++i) {
H Hartley Sweeten5e4c58c2012-09-05 18:21:25 -0700111 s = &dev->subdevices[i];
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700112 s->device = dev;
H Hartley Sweeten90a35c12012-12-19 17:27:02 -0700113 s->index = i;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700114 s->async_dma_dir = DMA_NONE;
115 spin_lock_init(&s->spin_lock);
116 s->minor = -1;
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700117 }
118 return 0;
119}
120EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
121
H Hartley Sweetend2762062014-08-25 16:03:54 -0700122/**
Ian Abbott9b348452015-09-17 17:19:17 +0100123 * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback
124 * @s: COMEDI subdevice.
125 *
126 * This is called by low-level COMEDI drivers to allocate an array to record
127 * the last values written to a subdevice's analog output channels (at least
128 * by the %INSN_WRITE instruction), to allow them to be read back by an
129 * %INSN_READ instruction. It also provides a default handler for the
130 * %INSN_READ instruction unless one has already been set.
131 *
132 * On success, @s->readback points to the first element of the array, which
133 * is zero-filled. The low-level driver is responsible for updating its
134 * contents. @s->insn_read will be set to comedi_readback_insn_read()
135 * unless it is already non-NULL.
136 *
137 * Returns 0 on success, -EINVAL if the subdevice has no channels, or
138 * -ENOMEM on allocation failure.
H Hartley Sweetend2762062014-08-25 16:03:54 -0700139 */
140int comedi_alloc_subdev_readback(struct comedi_subdevice *s)
141{
142 if (!s->n_chan)
143 return -EINVAL;
144
145 s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL);
146 if (!s->readback)
147 return -ENOMEM;
H Hartley Sweetenaa116722014-11-21 10:19:10 -0700148
149 if (!s->insn_read)
150 s->insn_read = comedi_readback_insn_read;
151
H Hartley Sweetend2762062014-08-25 16:03:54 -0700152 return 0;
153}
154EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
155
Ian Abbott3867e202013-11-08 15:03:26 +0000156static void comedi_device_detach_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800157{
158 int i;
Bill Pemberton34c43922009-03-16 22:05:14 -0400159 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800160
Ian Abbottf44303e2019-04-17 15:39:30 +0100161 lockdep_assert_held(&dev->attach_lock);
Ian Abbott77c21b62019-04-17 15:39:29 +0100162 lockdep_assert_held(&dev->mutex);
David Schleefed9eccb2008-11-04 20:29:31 -0800163 if (dev->subdevices) {
164 for (i = 0; i < dev->n_subdevices; i++) {
H Hartley Sweeten5e4c58c2012-09-05 18:21:25 -0700165 s = &dev->subdevices[i];
Ian Abbott8fc369a2015-04-21 13:18:10 +0100166 if (comedi_can_auto_free_spriv(s))
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700167 kfree(s->private);
David Schleefed9eccb2008-11-04 20:29:31 -0800168 comedi_free_subdevice_minor(s);
169 if (s->async) {
170 comedi_buf_alloc(dev, s, 0);
171 kfree(s->async);
172 }
H Hartley Sweetend2762062014-08-25 16:03:54 -0700173 kfree(s->readback);
David Schleefed9eccb2008-11-04 20:29:31 -0800174 }
175 kfree(dev->subdevices);
176 dev->subdevices = NULL;
177 dev->n_subdevices = 0;
178 }
Bill Pembertondedd1322009-03-16 22:04:18 -0400179 kfree(dev->private);
H Hartley Sweeten43db0622015-02-23 14:57:29 -0700180 kfree(dev->pacer);
Bill Pembertondedd1322009-03-16 22:04:18 -0400181 dev->private = NULL;
H Hartley Sweeten43db0622015-02-23 14:57:29 -0700182 dev->pacer = NULL;
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700183 dev->driver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800184 dev->board_name = NULL;
185 dev->board_ptr = NULL;
H Hartley Sweetend7e6dc12014-07-29 15:01:20 -0700186 dev->mmio = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800187 dev->iobase = 0;
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700188 dev->iolen = 0;
Ian Abbott00ca68842013-03-15 13:15:35 +0000189 dev->ioenabled = false;
David Schleefed9eccb2008-11-04 20:29:31 -0800190 dev->irq = 0;
191 dev->read_subdev = NULL;
192 dev->write_subdev = NULL;
193 dev->open = NULL;
194 dev->close = NULL;
Ian Abbottde06d7c2013-02-01 13:23:20 +0000195 comedi_clear_hw_dev(dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800196}
197
Ian Abbott016599f2013-04-04 14:58:58 +0100198void comedi_device_detach(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800199{
Ian Abbott77c21b62019-04-17 15:39:29 +0100200 lockdep_assert_held(&dev->mutex);
Ian Abbottd19db512013-11-08 15:03:28 +0000201 comedi_device_cancel_all(dev);
Ian Abbottbf11c132013-11-08 15:03:25 +0000202 down_write(&dev->attach_lock);
Ian Abbotta7401cd2013-03-15 13:15:33 +0000203 dev->attached = false;
Ian Abbottef77c0b2013-11-08 15:03:29 +0000204 dev->detach_count++;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100205 if (dev->driver)
David Schleefed9eccb2008-11-04 20:29:31 -0800206 dev->driver->detach(dev);
Ian Abbott3867e202013-11-08 15:03:26 +0000207 comedi_device_detach_cleanup(dev);
Ian Abbottbf11c132013-11-08 15:03:25 +0000208 up_write(&dev->attach_lock);
David Schleefed9eccb2008-11-04 20:29:31 -0800209}
210
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700211static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
212{
213 return -EINVAL;
214}
215
Spencer E. Olsond7569ad2018-10-03 14:56:02 -0600216static int insn_device_inval(struct comedi_device *dev,
217 struct comedi_insn *insn, unsigned int *data)
218{
219 return -EINVAL;
220}
221
222static unsigned int get_zero_valid_routes(struct comedi_device *dev,
223 unsigned int n_pairs,
224 unsigned int *pair_data)
225{
226 return 0;
227}
228
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700229int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
230 struct comedi_insn *insn, unsigned int *data)
231{
232 return -EINVAL;
233}
234
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700235/**
H Hartley Sweetend2762062014-08-25 16:03:54 -0700236 * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback.
Ian Abbott9b348452015-09-17 17:19:17 +0100237 * @dev: COMEDI device.
238 * @s: COMEDI subdevice.
239 * @insn: COMEDI instruction.
240 * @data: Pointer to return the readback data.
241 *
242 * Handles the %INSN_READ instruction for subdevices that use the readback
243 * array allocated by comedi_alloc_subdev_readback(). It may be used
244 * directly as the subdevice's handler (@s->insn_read) or called via a
245 * wrapper.
246 *
247 * @insn->n is normally 1, which will read a single value. If higher, the
248 * same element of the readback array will be read multiple times.
249 *
250 * Returns @insn->n on success, or -EINVAL if @s->readback is NULL.
H Hartley Sweetend2762062014-08-25 16:03:54 -0700251 */
252int comedi_readback_insn_read(struct comedi_device *dev,
253 struct comedi_subdevice *s,
254 struct comedi_insn *insn,
255 unsigned int *data)
256{
257 unsigned int chan = CR_CHAN(insn->chanspec);
258 int i;
259
260 if (!s->readback)
261 return -EINVAL;
262
263 for (i = 0; i < insn->n; i++)
264 data[i] = s->readback[chan];
265
266 return insn->n;
267}
268EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
269
270/**
Ian Abbott9b348452015-09-17 17:19:17 +0100271 * comedi_timeout() - Busy-wait for a driver condition to occur
272 * @dev: COMEDI device.
273 * @s: COMEDI subdevice.
274 * @insn: COMEDI instruction.
275 * @cb: Callback to check for the condition.
276 * @context: Private context from the driver.
277 *
278 * Busy-waits for up to a second (%COMEDI_TIMEOUT_MS) for the condition or
279 * some error (other than -EBUSY) to occur. The parameters @dev, @s, @insn,
280 * and @context are passed to the callback function, which returns -EBUSY to
281 * continue waiting or some other value to stop waiting (generally 0 if the
282 * condition occurred, or some error value).
283 *
284 * Returns -ETIMEDOUT if timed out, otherwise the return value from the
285 * callback function.
H Hartley Sweeten91506402014-02-10 11:49:00 -0700286 */
287int comedi_timeout(struct comedi_device *dev,
288 struct comedi_subdevice *s,
289 struct comedi_insn *insn,
290 int (*cb)(struct comedi_device *dev,
291 struct comedi_subdevice *s,
292 struct comedi_insn *insn,
293 unsigned long context),
294 unsigned long context)
295{
296 unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS);
297 int ret;
298
299 while (time_before(jiffies, timeout)) {
300 ret = cb(dev, s, insn, context);
301 if (ret != -EBUSY)
302 return ret; /* success (0) or non EBUSY errno */
303 cpu_relax();
304 }
305 return -ETIMEDOUT;
306}
307EXPORT_SYMBOL_GPL(comedi_timeout);
308
309/**
Ian Abbott9b348452015-09-17 17:19:17 +0100310 * comedi_dio_insn_config() - Boilerplate (*insn_config) for DIO subdevices
311 * @dev: COMEDI device.
312 * @s: COMEDI subdevice.
313 * @insn: COMEDI instruction.
314 * @data: Instruction parameters and return data.
315 * @mask: io_bits mask for grouped channels, or 0 for single channel.
316 *
317 * If @mask is 0, it is replaced with a single-bit mask corresponding to the
318 * channel number specified by @insn->chanspec. Otherwise, @mask
319 * corresponds to a group of channels (which should include the specified
320 * channel) that are always configured together as inputs or outputs.
321 *
322 * Partially handles the %INSN_CONFIG_DIO_INPUT, %INSN_CONFIG_DIO_OUTPUTS,
323 * and %INSN_CONFIG_DIO_QUERY instructions. The first two update
324 * @s->io_bits to record the directions of the masked channels. The last
325 * one sets @data[1] to the current direction of the group of channels
326 * (%COMEDI_INPUT) or %COMEDI_OUTPUT) as recorded in @s->io_bits.
327 *
328 * The caller is responsible for updating the DIO direction in the hardware
329 * registers if this function returns 0.
330 *
331 * Returns 0 for a %INSN_CONFIG_DIO_INPUT or %INSN_CONFIG_DIO_OUTPUT
332 * instruction, @insn->n (> 0) for a %INSN_CONFIG_DIO_QUERY instruction, or
333 * -EINVAL for some other instruction.
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700334 */
335int comedi_dio_insn_config(struct comedi_device *dev,
336 struct comedi_subdevice *s,
337 struct comedi_insn *insn,
338 unsigned int *data,
339 unsigned int mask)
340{
341 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
342
343 if (!mask)
344 mask = chan_mask;
345
346 switch (data[0]) {
347 case INSN_CONFIG_DIO_INPUT:
348 s->io_bits &= ~mask;
349 break;
350
351 case INSN_CONFIG_DIO_OUTPUT:
352 s->io_bits |= mask;
353 break;
354
355 case INSN_CONFIG_DIO_QUERY:
356 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
357 return insn->n;
358
359 default:
360 return -EINVAL;
361 }
362
363 return 0;
364}
365EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
366
H Hartley Sweeten05e60b12013-08-30 11:04:56 -0700367/**
Ian Abbott9b348452015-09-17 17:19:17 +0100368 * comedi_dio_update_state() - Update the internal state of DIO subdevices
369 * @s: COMEDI subdevice.
370 * @data: The channel mask and bits to update.
371 *
372 * Updates @s->state which holds the internal state of the outputs for DIO
373 * or DO subdevices (up to 32 channels). @data[0] contains a bit-mask of
374 * the channels to be updated. @data[1] contains a bit-mask of those
375 * channels to be set to '1'. The caller is responsible for updating the
376 * outputs in hardware according to @s->state. As a minimum, the channels
377 * in the returned bit-mask need to be updated.
378 *
379 * Returns @mask with non-existent channels removed.
H Hartley Sweeten05e60b12013-08-30 11:04:56 -0700380 */
381unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
382 unsigned int *data)
383{
384 unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
385 : 0xffffffff;
386 unsigned int mask = data[0] & chanmask;
387 unsigned int bits = data[1];
388
389 if (mask) {
390 s->state &= ~mask;
391 s->state |= (bits & mask);
392 }
393
394 return mask;
395}
396EXPORT_SYMBOL_GPL(comedi_dio_update_state);
397
Ian Abbottf146fe62014-09-15 13:45:57 +0100398/**
Ian Abbottbafd9c62019-03-04 14:33:54 +0000399 * comedi_bytes_per_scan_cmd() - Get length of asynchronous command "scan" in
400 * bytes
Ian Abbott9b348452015-09-17 17:19:17 +0100401 * @s: COMEDI subdevice.
Ian Abbottbafd9c62019-03-04 14:33:54 +0000402 * @cmd: COMEDI command.
Ian Abbottf146fe62014-09-15 13:45:57 +0100403 *
404 * Determines the overall scan length according to the subdevice type and the
Ian Abbottbafd9c62019-03-04 14:33:54 +0000405 * number of channels in the scan for the specified command.
Ian Abbottf146fe62014-09-15 13:45:57 +0100406 *
Ian Abbott9b348452015-09-17 17:19:17 +0100407 * For digital input, output or input/output subdevices, samples for
408 * multiple channels are assumed to be packed into one or more unsigned
409 * short or unsigned int values according to the subdevice's %SDF_LSAMPL
410 * flag. For other types of subdevice, samples are assumed to occupy a
411 * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag.
Ian Abbottf146fe62014-09-15 13:45:57 +0100412 *
413 * Returns the overall scan length in bytes.
414 */
Ian Abbottbafd9c62019-03-04 14:33:54 +0000415unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s,
416 struct comedi_cmd *cmd)
Ian Abbottf146fe62014-09-15 13:45:57 +0100417{
Ian Abbottf146fe62014-09-15 13:45:57 +0100418 unsigned int num_samples;
419 unsigned int bits_per_sample;
420
421 switch (s->type) {
422 case COMEDI_SUBD_DI:
423 case COMEDI_SUBD_DO:
424 case COMEDI_SUBD_DIO:
H Hartley Sweetenc39e0502014-10-31 12:04:28 -0700425 bits_per_sample = 8 * comedi_bytes_per_sample(s);
Ian Abbottaf57d892014-11-12 16:00:48 +0000426 num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample);
Ian Abbottf146fe62014-09-15 13:45:57 +0100427 break;
428 default:
Ian Abbottaf57d892014-11-12 16:00:48 +0000429 num_samples = cmd->scan_end_arg;
Ian Abbottf146fe62014-09-15 13:45:57 +0100430 break;
431 }
H Hartley Sweetenc39e0502014-10-31 12:04:28 -0700432 return comedi_samples_to_bytes(s, num_samples);
Ian Abbottf146fe62014-09-15 13:45:57 +0100433}
Ian Abbottbafd9c62019-03-04 14:33:54 +0000434EXPORT_SYMBOL_GPL(comedi_bytes_per_scan_cmd);
435
436/**
437 * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes
438 * @s: COMEDI subdevice.
439 *
440 * Determines the overall scan length according to the subdevice type and the
441 * number of channels in the scan for the current command.
442 *
443 * For digital input, output or input/output subdevices, samples for
444 * multiple channels are assumed to be packed into one or more unsigned
445 * short or unsigned int values according to the subdevice's %SDF_LSAMPL
446 * flag. For other types of subdevice, samples are assumed to occupy a
447 * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag.
448 *
449 * Returns the overall scan length in bytes.
450 */
451unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
452{
453 struct comedi_cmd *cmd = &s->async->cmd;
454
455 return comedi_bytes_per_scan_cmd(s, cmd);
456}
Ian Abbottf146fe62014-09-15 13:45:57 +0100457EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
458
Ian Abbott92d354c2015-10-23 10:56:09 +0100459static unsigned int __comedi_nscans_left(struct comedi_subdevice *s,
460 unsigned int nscans)
461{
462 struct comedi_async *async = s->async;
463 struct comedi_cmd *cmd = &async->cmd;
464
465 if (cmd->stop_src == TRIG_COUNT) {
466 unsigned int scans_left = 0;
467
468 if (async->scans_done < cmd->stop_arg)
469 scans_left = cmd->stop_arg - async->scans_done;
470
471 if (nscans > scans_left)
472 nscans = scans_left;
473 }
474 return nscans;
475}
476
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100477/**
Ian Abbott9b348452015-09-17 17:19:17 +0100478 * comedi_nscans_left() - Return the number of scans left in the command
479 * @s: COMEDI subdevice.
480 * @nscans: The expected number of scans or 0 for all available scans.
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700481 *
Ian Abbott9b348452015-09-17 17:19:17 +0100482 * If @nscans is 0, it is set to the number of scans available in the
483 * async buffer.
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700484 *
Ian Abbott9b348452015-09-17 17:19:17 +0100485 * If the async command has a stop_src of %TRIG_COUNT, the @nscans will be
486 * checked against the number of scans remaining to complete the command.
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700487 *
488 * The return value will then be either the expected number of scans or the
Ian Abbott9b348452015-09-17 17:19:17 +0100489 * number of scans remaining to complete the command, whichever is fewer.
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700490 */
491unsigned int comedi_nscans_left(struct comedi_subdevice *s,
492 unsigned int nscans)
493{
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700494 if (nscans == 0) {
495 unsigned int nbytes = comedi_buf_read_n_available(s);
496
497 nscans = nbytes / comedi_bytes_per_scan(s);
498 }
Ian Abbott92d354c2015-10-23 10:56:09 +0100499 return __comedi_nscans_left(s, nscans);
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700500}
501EXPORT_SYMBOL_GPL(comedi_nscans_left);
502
503/**
Ian Abbott9b348452015-09-17 17:19:17 +0100504 * comedi_nsamples_left() - Return the number of samples left in the command
505 * @s: COMEDI subdevice.
506 * @nsamples: The expected number of samples.
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700507 *
Ian Abbott9b348452015-09-17 17:19:17 +0100508 * Returns the number of samples remaining to complete the command, or the
509 * specified expected number of samples (@nsamples), whichever is fewer.
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700510 */
511unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
512 unsigned int nsamples)
513{
514 struct comedi_async *async = s->async;
515 struct comedi_cmd *cmd = &async->cmd;
Chris Opperman2665df52018-06-13 19:14:35 +0200516 unsigned long long scans_left;
517 unsigned long long samples_left;
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700518
Chris Opperman2665df52018-06-13 19:14:35 +0200519 if (cmd->stop_src != TRIG_COUNT)
520 return nsamples;
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700521
Chris Opperman2665df52018-06-13 19:14:35 +0200522 scans_left = __comedi_nscans_left(s, cmd->stop_arg);
523 if (!scans_left)
524 return 0;
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700525
Chris Opperman2665df52018-06-13 19:14:35 +0200526 samples_left = scans_left * cmd->scan_end_arg -
527 comedi_bytes_to_samples(s, async->scan_progress);
528
529 if (samples_left < nsamples)
530 return samples_left;
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700531 return nsamples;
532}
533EXPORT_SYMBOL_GPL(comedi_nsamples_left);
534
535/**
Ian Abbott9b348452015-09-17 17:19:17 +0100536 * comedi_inc_scan_progress() - Update scan progress in asynchronous command
537 * @s: COMEDI subdevice.
538 * @num_bytes: Amount of data in bytes to increment scan progress.
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100539 *
Ian Abbott9b348452015-09-17 17:19:17 +0100540 * Increments the scan progress by the number of bytes specified by @num_bytes.
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100541 * If the scan progress reaches or exceeds the scan length in bytes, reduce
542 * it modulo the scan length in bytes and set the "end of scan" asynchronous
Ian Abbott9b348452015-09-17 17:19:17 +0100543 * event flag (%COMEDI_CB_EOS) to be processed later.
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100544 */
545void comedi_inc_scan_progress(struct comedi_subdevice *s,
546 unsigned int num_bytes)
547{
548 struct comedi_async *async = s->async;
H Hartley Sweetenf8736ca2014-10-31 09:49:31 -0700549 struct comedi_cmd *cmd = &async->cmd;
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100550 unsigned int scan_length = comedi_bytes_per_scan(s);
551
H Hartley Sweetenf8736ca2014-10-31 09:49:31 -0700552 /* track the 'cur_chan' for non-SDF_PACKED subdevices */
553 if (!(s->subdev_flags & SDF_PACKED)) {
554 async->cur_chan += comedi_bytes_to_samples(s, num_bytes);
555 async->cur_chan %= cmd->chanlist_len;
556 }
557
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100558 async->scan_progress += num_bytes;
559 if (async->scan_progress >= scan_length) {
H Hartley Sweeten1dacbe52014-11-05 10:20:52 -0700560 unsigned int nscans = async->scan_progress / scan_length;
561
562 if (async->scans_done < (UINT_MAX - nscans))
563 async->scans_done += nscans;
564 else
565 async->scans_done = UINT_MAX;
566
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100567 async->scan_progress %= scan_length;
568 async->events |= COMEDI_CB_EOS;
569 }
570}
571EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
572
Ian Abbott5a780352014-09-15 13:46:01 +0100573/**
Ian Abbott9b348452015-09-17 17:19:17 +0100574 * comedi_handle_events() - Handle events and possibly stop acquisition
575 * @dev: COMEDI device.
576 * @s: COMEDI subdevice.
Ian Abbott5a780352014-09-15 13:46:01 +0100577 *
578 * Handles outstanding asynchronous acquisition event flags associated
Ian Abbott9b348452015-09-17 17:19:17 +0100579 * with the subdevice. Call the subdevice's @s->cancel() handler if the
Ian Abbott5a780352014-09-15 13:46:01 +0100580 * "end of acquisition", "error" or "overflow" event flags are set in order
581 * to stop the acquisition at the driver level.
582 *
583 * Calls comedi_event() to further process the event flags, which may mark
584 * the asynchronous command as no longer running, possibly terminated with
585 * an error, and may wake up tasks.
586 *
587 * Return a bit-mask of the handled events.
588 */
589unsigned int comedi_handle_events(struct comedi_device *dev,
590 struct comedi_subdevice *s)
591{
592 unsigned int events = s->async->events;
593
594 if (events == 0)
595 return events;
596
H Hartley Sweeten7be7cd12016-03-30 10:47:25 -0700597 if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel)
Ian Abbott5a780352014-09-15 13:46:01 +0100598 s->cancel(dev, s);
599
600 comedi_event(dev, s);
601
602 return events;
603}
604EXPORT_SYMBOL_GPL(comedi_handle_events);
605
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700606static int insn_rw_emulate_bits(struct comedi_device *dev,
607 struct comedi_subdevice *s,
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700608 struct comedi_insn *insn,
609 unsigned int *data)
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700610{
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700611 struct comedi_insn _insn;
612 unsigned int chan = CR_CHAN(insn->chanspec);
613 unsigned int base_chan = (chan < 32) ? 0 : chan;
614 unsigned int _data[2];
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700615 int ret;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700616
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700617 memset(_data, 0, sizeof(_data));
618 memset(&_insn, 0, sizeof(_insn));
619 _insn.insn = INSN_BITS;
620 _insn.chanspec = base_chan;
621 _insn.n = 2;
622 _insn.subdev = insn->subdev;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700623
624 if (insn->insn == INSN_WRITE) {
625 if (!(s->subdev_flags & SDF_WRITABLE))
626 return -EINVAL;
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700627 _data[0] = 1 << (chan - base_chan); /* mask */
628 _data[1] = data[0] ? (1 << (chan - base_chan)) : 0; /* bits */
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700629 }
630
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700631 ret = s->insn_bits(dev, s, &_insn, _data);
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700632 if (ret < 0)
633 return ret;
634
635 if (insn->insn == INSN_READ)
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700636 data[0] = (_data[1] >> (chan - base_chan)) & 1;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700637
638 return 1;
639}
640
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700641static int __comedi_device_postconfig_async(struct comedi_device *dev,
642 struct comedi_subdevice *s)
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700643{
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700644 struct comedi_async *async;
645 unsigned int buf_size;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700646 int ret;
647
Ian Abbott77c21b62019-04-17 15:39:29 +0100648 lockdep_assert_held(&dev->mutex);
H Hartley Sweeten57b71c32013-01-21 14:37:15 -0700649 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
650 dev_warn(dev->class_dev,
651 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
652 return -EINVAL;
653 }
654 if (!s->do_cmdtest) {
655 dev_warn(dev->class_dev,
656 "async subdevices must have a do_cmdtest() function\n");
657 return -EINVAL;
658 }
H Hartley Sweeten7be7cd12016-03-30 10:47:25 -0700659 if (!s->cancel)
660 dev_warn(dev->class_dev,
661 "async subdevices should have a cancel() function\n");
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700662
663 async = kzalloc(sizeof(*async), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800664 if (!async)
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700665 return -ENOMEM;
Joe Perches78110bb2013-02-11 09:41:29 -0800666
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700667 init_waitqueue_head(&async->wait_head);
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700668 s->async = async;
669
670 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
671 buf_size = comedi_default_buf_size_kb * 1024;
672 if (buf_size > async->max_bufsize)
673 buf_size = async->max_bufsize;
674
675 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
676 dev_warn(dev->class_dev, "Buffer allocation failed\n");
677 return -ENOMEM;
678 }
679 if (s->buf_change) {
H Hartley Sweetend546b892014-07-21 11:48:32 -0700680 ret = s->buf_change(dev, s);
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700681 if (ret < 0)
682 return ret;
683 }
684
Ian Abbottf65cc542013-02-01 10:20:30 +0000685 comedi_alloc_subdevice_minor(s);
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700686
687 return 0;
688}
689
690static int __comedi_device_postconfig(struct comedi_device *dev)
691{
692 struct comedi_subdevice *s;
693 int ret;
694 int i;
695
Ian Abbott77c21b62019-04-17 15:39:29 +0100696 lockdep_assert_held(&dev->mutex);
Spencer E. Olsond7569ad2018-10-03 14:56:02 -0600697 if (!dev->insn_device_config)
698 dev->insn_device_config = insn_device_inval;
699
700 if (!dev->get_valid_routes)
701 dev->get_valid_routes = get_zero_valid_routes;
702
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700703 for (i = 0; i < dev->n_subdevices; i++) {
704 s = &dev->subdevices[i];
705
706 if (s->type == COMEDI_SUBD_UNUSED)
707 continue;
708
H Hartley Sweeten09567cb2013-08-30 10:47:03 -0700709 if (s->type == COMEDI_SUBD_DO) {
710 if (s->n_chan < 32)
711 s->io_bits = (1 << s->n_chan) - 1;
712 else
713 s->io_bits = 0xffffffff;
714 }
715
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700716 if (s->len_chanlist == 0)
717 s->len_chanlist = 1;
718
719 if (s->do_cmd) {
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700720 ret = __comedi_device_postconfig_async(dev, s);
721 if (ret)
722 return ret;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700723 }
724
725 if (!s->range_table && !s->range_table_list)
726 s->range_table = &range_unknown;
727
728 if (!s->insn_read && s->insn_bits)
729 s->insn_read = insn_rw_emulate_bits;
730 if (!s->insn_write && s->insn_bits)
731 s->insn_write = insn_rw_emulate_bits;
732
733 if (!s->insn_read)
734 s->insn_read = insn_inval;
735 if (!s->insn_write)
736 s->insn_write = insn_inval;
737 if (!s->insn_bits)
738 s->insn_bits = insn_inval;
739 if (!s->insn_config)
740 s->insn_config = insn_inval;
741
742 if (!s->poll)
743 s->poll = poll_invalid;
744 }
745
746 return 0;
747}
748
Ian Abbott3902a372012-03-30 17:15:00 +0100749/* do a little post-config cleanup */
Ian Abbott3902a372012-03-30 17:15:00 +0100750static int comedi_device_postconfig(struct comedi_device *dev)
751{
Ian Abbottb2a644b2013-04-04 14:58:56 +0100752 int ret;
753
Ian Abbott77c21b62019-04-17 15:39:29 +0100754 lockdep_assert_held(&dev->mutex);
Ian Abbottb2a644b2013-04-04 14:58:56 +0100755 ret = __comedi_device_postconfig(dev);
H Hartley Sweetenae5dd5f2013-04-08 10:56:02 -0700756 if (ret < 0)
Ian Abbott3902a372012-03-30 17:15:00 +0100757 return ret;
Ian Abbottbf11c132013-11-08 15:03:25 +0000758 down_write(&dev->attach_lock);
Ian Abbotta7401cd2013-03-15 13:15:33 +0000759 dev->attached = true;
Ian Abbottbf11c132013-11-08 15:03:25 +0000760 up_write(&dev->attach_lock);
Ian Abbott3902a372012-03-30 17:15:00 +0100761 return 0;
762}
763
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700764/*
765 * Generic recognize function for drivers that register their supported
766 * board names.
767 *
768 * 'driv->board_name' points to a 'const char *' member within the
769 * zeroth element of an array of some private board information
770 * structure, say 'struct foo_board' containing a member 'const char
771 * *board_name' that is initialized to point to a board name string that
772 * is one of the candidates matched against this function's 'name'
773 * parameter.
774 *
775 * 'driv->offset' is the size of the private board information
776 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
777 * the length of the array of private board information structures.
778 *
779 * If one of the board names in the array of private board information
780 * structures matches the name supplied to this function, the function
781 * returns a pointer to the pointer to the board name, otherwise it
782 * returns NULL. The return value ends up in the 'board_ptr' member of
783 * a 'struct comedi_device' that the low-level comedi driver's
784 * 'attach()' hook can convert to a point to a particular element of its
785 * array of private board information structures by subtracting the
786 * offset of the member that points to the board name. (No subtraction
787 * is required if the board name pointer is the first member of the
788 * private board information structure, which is generally the case.)
789 */
790static void *comedi_recognize(struct comedi_driver *driv, const char *name)
791{
792 char **name_ptr = (char **)driv->board_name;
793 int i;
794
795 for (i = 0; i < driv->num_names; i++) {
796 if (strcmp(*name_ptr, name) == 0)
797 return name_ptr;
798 name_ptr = (void *)name_ptr + driv->offset;
799 }
800
801 return NULL;
802}
803
804static void comedi_report_boards(struct comedi_driver *driv)
805{
806 unsigned int i;
807 const char *const *name_ptr;
808
809 pr_info("comedi: valid board names for %s driver are:\n",
810 driv->driver_name);
811
812 name_ptr = driv->board_name;
813 for (i = 0; i < driv->num_names; i++) {
814 pr_info(" %s\n", *name_ptr);
815 name_ptr = (const char **)((char *)name_ptr + driv->offset);
816 }
817
818 if (driv->num_names == 0)
819 pr_info(" %s\n", driv->driver_name);
820}
821
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700822/**
Ian Abbott9b348452015-09-17 17:19:17 +0100823 * comedi_load_firmware() - Request and load firmware for a device
824 * @dev: COMEDI device.
825 * @device: Hardware device.
826 * @name: The name of the firmware image.
827 * @cb: Callback to the upload the firmware image.
828 * @context: Private context from the driver.
829 *
830 * Sends a firmware request for the hardware device and waits for it. Calls
831 * the callback function to upload the firmware to the device, them releases
832 * the firmware.
833 *
834 * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number
835 * from the firmware request or the callback function.
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700836 */
837int comedi_load_firmware(struct comedi_device *dev,
838 struct device *device,
839 const char *name,
840 int (*cb)(struct comedi_device *dev,
H Hartley Sweetend5695412013-05-17 11:18:01 -0700841 const u8 *data, size_t size,
842 unsigned long context),
843 unsigned long context)
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700844{
845 const struct firmware *fw;
846 int ret;
847
848 if (!cb)
849 return -EINVAL;
850
851 ret = request_firmware(&fw, name, device);
852 if (ret == 0) {
H Hartley Sweetend5695412013-05-17 11:18:01 -0700853 ret = cb(dev, fw->data, fw->size, context);
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700854 release_firmware(fw);
855 }
856
H Hartley Sweetenc6236c02013-12-10 16:31:25 -0700857 return ret < 0 ? ret : 0;
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700858}
859EXPORT_SYMBOL_GPL(comedi_load_firmware);
860
861/**
Ian Abbott9b348452015-09-17 17:19:17 +0100862 * __comedi_request_region() - Request an I/O region for a legacy driver
863 * @dev: COMEDI device.
864 * @start: Base address of the I/O region.
865 * @len: Length of the I/O region.
866 *
867 * Requests the specified I/O port region which must start at a non-zero
868 * address.
869 *
870 * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
871 * fails.
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700872 */
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700873int __comedi_request_region(struct comedi_device *dev,
874 unsigned long start, unsigned long len)
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700875{
876 if (!start) {
877 dev_warn(dev->class_dev,
878 "%s: a I/O base address must be specified\n",
879 dev->board_name);
880 return -EINVAL;
881 }
882
883 if (!request_region(start, len, dev->board_name)) {
884 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
885 dev->board_name, start, len);
886 return -EIO;
887 }
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700888
889 return 0;
890}
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700891EXPORT_SYMBOL_GPL(__comedi_request_region);
892
893/**
Ian Abbott9b348452015-09-17 17:19:17 +0100894 * comedi_request_region() - Request an I/O region for a legacy driver
895 * @dev: COMEDI device.
896 * @start: Base address of the I/O region.
897 * @len: Length of the I/O region.
898 *
899 * Requests the specified I/O port region which must start at a non-zero
900 * address.
901 *
902 * On success, @dev->iobase is set to the base address of the region and
903 * @dev->iolen is set to its length.
904 *
905 * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
906 * fails.
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700907 */
908int comedi_request_region(struct comedi_device *dev,
909 unsigned long start, unsigned long len)
910{
911 int ret;
912
913 ret = __comedi_request_region(dev, start, len);
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700914 if (ret == 0) {
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700915 dev->iobase = start;
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700916 dev->iolen = len;
917 }
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700918
919 return ret;
920}
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700921EXPORT_SYMBOL_GPL(comedi_request_region);
922
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700923/**
Ian Abbott9b348452015-09-17 17:19:17 +0100924 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers
925 * @dev: COMEDI device.
926 *
927 * This is a simple, generic 'detach' handler for legacy COMEDI devices that
928 * just use a single I/O port region and possibly an IRQ and that don't need
929 * any special clean-up for their private device or subdevice storage. It
930 * can also be called by a driver-specific 'detach' handler.
931 *
932 * If @dev->irq is non-zero, the IRQ will be freed. If @dev->iobase and
933 * @dev->iolen are both non-zero, the I/O port region will be released.
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700934 */
935void comedi_legacy_detach(struct comedi_device *dev)
936{
H Hartley Sweeten3d1fe3f2013-04-18 14:34:37 -0700937 if (dev->irq) {
938 free_irq(dev->irq, dev);
939 dev->irq = 0;
940 }
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700941 if (dev->iobase && dev->iolen) {
942 release_region(dev->iobase, dev->iolen);
943 dev->iobase = 0;
944 dev->iolen = 0;
945 }
946}
947EXPORT_SYMBOL_GPL(comedi_legacy_detach);
948
Bill Pemberton0707bb02009-03-16 22:06:20 -0400949int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
David Schleefed9eccb2008-11-04 20:29:31 -0800950{
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400951 struct comedi_driver *driv;
David Schleefed9eccb2008-11-04 20:29:31 -0800952 int ret;
953
Ian Abbott77c21b62019-04-17 15:39:29 +0100954 lockdep_assert_held(&dev->mutex);
David Schleefed9eccb2008-11-04 20:29:31 -0800955 if (dev->attached)
956 return -EBUSY;
957
Ian Abbottc383e2d2013-06-27 14:50:58 +0100958 mutex_lock(&comedi_drivers_list_lock);
David Schleefed9eccb2008-11-04 20:29:31 -0800959 for (driv = comedi_drivers; driv; driv = driv->next) {
GĂ¼ngör Erseymen559e9a62012-09-11 17:56:42 +0300960 if (!try_module_get(driv->module))
David Schleefed9eccb2008-11-04 20:29:31 -0800961 continue;
David Schleefed9eccb2008-11-04 20:29:31 -0800962 if (driv->num_names) {
963 dev->board_ptr = comedi_recognize(driv, it->board_name);
Ian Abbott3902a372012-03-30 17:15:00 +0100964 if (dev->board_ptr)
965 break;
H Hartley Sweeten3df9f212014-07-18 14:28:11 -0700966 } else if (strcmp(driv->driver_name, it->board_name) == 0) {
Ian Abbott3902a372012-03-30 17:15:00 +0100967 break;
H Hartley Sweeten3df9f212014-07-18 14:28:11 -0700968 }
David Schleefed9eccb2008-11-04 20:29:31 -0800969 module_put(driv->module);
970 }
H Hartley Sweetene2850162015-03-04 12:15:29 -0700971 if (!driv) {
Ian Abbott3902a372012-03-30 17:15:00 +0100972 /* recognize has failed if we get here */
973 /* report valid board names before returning error */
974 for (driv = comedi_drivers; driv; driv = driv->next) {
GĂ¼ngör Erseymen559e9a62012-09-11 17:56:42 +0300975 if (!try_module_get(driv->module))
Ian Abbott3902a372012-03-30 17:15:00 +0100976 continue;
Ian Abbott3902a372012-03-30 17:15:00 +0100977 comedi_report_boards(driv);
978 module_put(driv->module);
979 }
Ian Abbottc383e2d2013-06-27 14:50:58 +0100980 ret = -EIO;
981 goto out;
Ian Abbott3902a372012-03-30 17:15:00 +0100982 }
H Hartley Sweetene2850162015-03-04 12:15:29 -0700983 if (!driv->attach) {
Ian Abbott8c3714d2012-08-15 15:02:45 +0100984 /* driver does not support manual configuration */
985 dev_warn(dev->class_dev,
986 "driver '%s' does not support attach using comedi_config\n",
987 driv->driver_name);
988 module_put(driv->module);
Ted Chen1a59adb2015-08-05 01:18:46 +0800989 ret = -EIO;
Ian Abbottc383e2d2013-06-27 14:50:58 +0100990 goto out;
Ian Abbott8c3714d2012-08-15 15:02:45 +0100991 }
Ian Abbott3902a372012-03-30 17:15:00 +0100992 dev->driver = driv;
H Hartley Sweeten34b68402013-04-08 10:55:29 -0700993 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
994 : dev->driver->driver_name;
Ian Abbott3902a372012-03-30 17:15:00 +0100995 ret = driv->attach(dev, it);
Ian Abbott74ece1082013-04-04 14:58:59 +0100996 if (ret >= 0)
997 ret = comedi_device_postconfig(dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800998 if (ret < 0) {
Ian Abbott016599f2013-04-04 14:58:58 +0100999 comedi_device_detach(dev);
Ian Abbott3955dfa2013-08-23 12:37:17 +01001000 module_put(driv->module);
David Schleefed9eccb2008-11-04 20:29:31 -08001001 }
Ian Abbottb2a644b2013-04-04 14:58:56 +01001002 /* On success, the driver module count has been incremented. */
Ian Abbottc383e2d2013-06-27 14:50:58 +01001003out:
1004 mutex_unlock(&comedi_drivers_list_lock);
Ian Abbottb2a644b2013-04-04 14:58:56 +01001005 return ret;
David Schleefed9eccb2008-11-04 20:29:31 -08001006}
1007
Ian Abbott9b348452015-09-17 17:19:17 +01001008/**
1009 * comedi_auto_config() - Create a COMEDI device for a hardware device
1010 * @hardware_device: Hardware device.
1011 * @driver: COMEDI low-level driver for the hardware device.
1012 * @context: Driver context for the auto_attach handler.
1013 *
1014 * Allocates a new COMEDI device for the hardware device and calls the
1015 * low-level driver's 'auto_attach' handler to set-up the hardware and
1016 * allocate the COMEDI subdevices. Additional "post-configuration" setting
1017 * up is performed on successful return from the 'auto_attach' handler.
1018 * If the 'auto_attach' handler fails, the low-level driver's 'detach'
1019 * handler will be called as part of the clean-up.
1020 *
1021 * This is usually called from a wrapper function in a bus-specific COMEDI
1022 * module, which in turn is usually called from a bus device 'probe'
1023 * function in the low-level driver.
1024 *
1025 * Returns 0 on success, -EINVAL if the parameters are invalid or the
1026 * post-configuration determines the driver has set the COMEDI device up
1027 * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of
1028 * COMEDI minor device numbers, or some negative error number returned by
1029 * the driver's 'auto_attach' handler.
1030 */
Ian Abbotta588da12012-11-14 13:10:38 +00001031int comedi_auto_config(struct device *hardware_device,
1032 struct comedi_driver *driver, unsigned long context)
Ian Abbottf40116702012-03-30 17:15:01 +01001033{
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001034 struct comedi_device *dev;
Ian Abbottf40116702012-03-30 17:15:01 +01001035 int ret;
1036
Ian Abbottf08e0ac2013-04-04 14:58:42 +01001037 if (!hardware_device) {
Simo Koskinen423a8a62017-08-28 15:01:32 +02001038 pr_warn("BUG! %s called with NULL hardware_device\n", __func__);
Ian Abbottf08e0ac2013-04-04 14:58:42 +01001039 return -EINVAL;
1040 }
1041 if (!driver) {
1042 dev_warn(hardware_device,
Simo Koskinen423a8a62017-08-28 15:01:32 +02001043 "BUG! %s called with NULL comedi driver\n", __func__);
Ian Abbottf08e0ac2013-04-04 14:58:42 +01001044 return -EINVAL;
1045 }
1046
Ian Abbotta588da12012-11-14 13:10:38 +00001047 if (!driver->auto_attach) {
1048 dev_warn(hardware_device,
1049 "BUG! comedi driver '%s' has no auto_attach handler\n",
1050 driver->driver_name);
1051 return -EINVAL;
1052 }
1053
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001054 dev = comedi_alloc_board_minor(hardware_device);
Bernd Porrbcb62322014-01-07 21:42:25 +00001055 if (IS_ERR(dev)) {
1056 dev_warn(hardware_device,
1057 "driver '%s' could not create device.\n",
1058 driver->driver_name);
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001059 return PTR_ERR(dev);
Bernd Porrbcb62322014-01-07 21:42:25 +00001060 }
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001061 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
Ian Abbott77c21b62019-04-17 15:39:29 +01001062 lockdep_assert_held(&dev->mutex);
Ian Abbottf40116702012-03-30 17:15:01 +01001063
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001064 dev->driver = driver;
H Hartley Sweeten34b68402013-04-08 10:55:29 -07001065 dev->board_name = dev->driver->driver_name;
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001066 ret = driver->auto_attach(dev, context);
Ian Abbott74ece1082013-04-04 14:58:59 +01001067 if (ret >= 0)
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001068 ret = comedi_device_postconfig(dev);
Ian Abbottf40116702012-03-30 17:15:01 +01001069
Bernd Porrbcb62322014-01-07 21:42:25 +00001070 if (ret < 0) {
1071 dev_warn(hardware_device,
1072 "driver '%s' failed to auto-configure device.\n",
1073 driver->driver_name);
Ian Abbottf4396962019-04-17 15:35:31 +01001074 mutex_unlock(&dev->mutex);
Ian Abbottf5b31e12013-04-04 14:58:48 +01001075 comedi_release_hardware_device(hardware_device);
Bernd Porrbcb62322014-01-07 21:42:25 +00001076 } else {
1077 /*
1078 * class_dev should be set properly here
1079 * after a successful auto config
1080 */
1081 dev_info(dev->class_dev,
1082 "driver '%s' has successfully auto-configured '%s'.\n",
1083 driver->driver_name, dev->board_name);
Ian Abbottf4396962019-04-17 15:35:31 +01001084 mutex_unlock(&dev->mutex);
Bernd Porrbcb62322014-01-07 21:42:25 +00001085 }
Ian Abbottf40116702012-03-30 17:15:01 +01001086 return ret;
1087}
Ian Abbott8ed705a2012-10-27 21:44:14 +01001088EXPORT_SYMBOL_GPL(comedi_auto_config);
1089
Ian Abbott9b348452015-09-17 17:19:17 +01001090/**
1091 * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device
1092 * @hardware_device: Hardware device previously passed to
1093 * comedi_auto_config().
1094 *
1095 * Cleans up and eventually destroys the COMEDI device allocated by
1096 * comedi_auto_config() for the same hardware device. As part of this
1097 * clean-up, the low-level COMEDI driver's 'detach' handler will be called.
1098 * (The COMEDI device itself will persist in an unattached state if it is
1099 * still open, until it is released, and any mmapped buffers will persist
1100 * until they are munmapped.)
1101 *
1102 * This is usually called from a wrapper module in a bus-specific COMEDI
1103 * module, which in turn is usually set as the bus device 'remove' function
1104 * in the low-level COMEDI driver.
1105 */
Ian Abbott8ed705a2012-10-27 21:44:14 +01001106void comedi_auto_unconfig(struct device *hardware_device)
David Schleefed9eccb2008-11-04 20:29:31 -08001107{
H Hartley Sweetene2850162015-03-04 12:15:29 -07001108 if (!hardware_device)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301109 return;
Ian Abbott3346b792013-04-04 14:58:47 +01001110 comedi_release_hardware_device(hardware_device);
David Schleefed9eccb2008-11-04 20:29:31 -08001111}
Ian Abbott8ed705a2012-10-27 21:44:14 +01001112EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001113
Ian Abbott9b348452015-09-17 17:19:17 +01001114/**
1115 * comedi_driver_register() - Register a low-level COMEDI driver
1116 * @driver: Low-level COMEDI driver.
1117 *
1118 * The low-level COMEDI driver is added to the list of registered COMEDI
1119 * drivers. This is used by the handler for the "/proc/comedi" file and is
1120 * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure
1121 * "legacy" COMEDI devices (for those low-level drivers that support it).
1122 *
1123 * Returns 0.
1124 */
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001125int comedi_driver_register(struct comedi_driver *driver)
1126{
Ian Abbottc383e2d2013-06-27 14:50:58 +01001127 mutex_lock(&comedi_drivers_list_lock);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001128 driver->next = comedi_drivers;
1129 comedi_drivers = driver;
Ian Abbottc383e2d2013-06-27 14:50:58 +01001130 mutex_unlock(&comedi_drivers_list_lock);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001131
1132 return 0;
1133}
H Hartley Sweeten5660e742013-04-12 10:11:54 -07001134EXPORT_SYMBOL_GPL(comedi_driver_register);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001135
Ian Abbott9b348452015-09-17 17:19:17 +01001136/**
1137 * comedi_driver_unregister() - Unregister a low-level COMEDI driver
1138 * @driver: Low-level COMEDI driver.
1139 *
1140 * The low-level COMEDI driver is removed from the list of registered COMEDI
1141 * drivers. Detaches any COMEDI devices attached to the driver, which will
1142 * result in the low-level driver's 'detach' handler being called for those
1143 * devices before this function returns.
1144 */
Ian Abbott99c0e262013-06-27 14:50:57 +01001145void comedi_driver_unregister(struct comedi_driver *driver)
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001146{
1147 struct comedi_driver *prev;
1148 int i;
1149
Ian Abbottc383e2d2013-06-27 14:50:58 +01001150 /* unlink the driver */
1151 mutex_lock(&comedi_drivers_list_lock);
1152 if (comedi_drivers == driver) {
1153 comedi_drivers = driver->next;
1154 } else {
1155 for (prev = comedi_drivers; prev->next; prev = prev->next) {
1156 if (prev->next == driver) {
1157 prev->next = driver->next;
1158 break;
1159 }
1160 }
1161 }
1162 mutex_unlock(&comedi_drivers_list_lock);
1163
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001164 /* check for devices using this driver */
1165 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
Ian Abbotta200fad2013-11-08 15:03:35 +00001166 struct comedi_device *dev = comedi_dev_get_from_minor(i);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001167
1168 if (!dev)
1169 continue;
1170
1171 mutex_lock(&dev->mutex);
1172 if (dev->attached && dev->driver == driver) {
1173 if (dev->use_count)
1174 dev_warn(dev->class_dev,
1175 "BUG! detaching device with use_count=%d\n",
1176 dev->use_count);
1177 comedi_device_detach(dev);
1178 }
1179 mutex_unlock(&dev->mutex);
Ian Abbotta200fad2013-11-08 15:03:35 +00001180 comedi_dev_put(dev);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001181 }
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001182}
H Hartley Sweeten5660e742013-04-12 10:11:54 -07001183EXPORT_SYMBOL_GPL(comedi_driver_unregister);