blob: 44511d7294505f241b7dc2c368efe1965f159a19 [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
Marcos CanĂ¡n50fbb882015-09-16 17:48:57 -03002 * module/drivers.c
3 * functions for manipulating drivers
4 *
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7 * Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
David Schleefed9eccb2008-11-04 20:29:31 -080018*/
19
David Schleefed9eccb2008-11-04 20:29:31 -080020#include <linux/device.h>
21#include <linux/module.h>
David Schleefed9eccb2008-11-04 20:29:31 -080022#include <linux/errno.h>
23#include <linux/kernel.h>
David Schleefed9eccb2008-11-04 20:29:31 -080024#include <linux/ioport.h>
David Schleefed9eccb2008-11-04 20:29:31 -080025#include <linux/slab.h>
Ian Abbottd35d8932015-09-17 17:19:16 +010026#include <linux/dma-direction.h>
H Hartley Sweeten3d1fe3f2013-04-18 14:34:37 -070027#include <linux/interrupt.h>
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -070028#include <linux/firmware.h>
David Schleefed9eccb2008-11-04 20:29:31 -080029
Greg Kroah-Hartman242e7ad2010-05-03 15:20:29 -070030#include "comedidev.h"
Ian Abbott3a5fa272012-06-19 10:17:44 +010031#include "comedi_internal.h"
Greg Kroah-Hartman242e7ad2010-05-03 15:20:29 -070032
Bill Pemberton139dfbd2009-03-16 22:05:25 -040033struct comedi_driver *comedi_drivers;
H Hartley Sweeten3df9f212014-07-18 14:28:11 -070034/* protects access to comedi_drivers */
Ian Abbottc383e2d2013-06-27 14:50:58 +010035DEFINE_MUTEX(comedi_drivers_list_lock);
David Schleefed9eccb2008-11-04 20:29:31 -080036
Ian Abbott9b348452015-09-17 17:19:17 +010037/**
38 * comedi_set_hw_dev() - Set hardware device associated with COMEDI device
39 * @dev: COMEDI device.
40 * @hw_dev: Hardware device.
41 *
42 * For automatically configured COMEDI devices (resulting from a call to
43 * comedi_auto_config() or one of its wrappers from the low-level COMEDI
44 * driver), comedi_set_hw_dev() is called automatically by the COMEDI core
45 * to associate the COMEDI device with the hardware device. It can also be
46 * called directly by "legacy" low-level COMEDI drivers that rely on the
47 * %COMEDI_DEVCONFIG ioctl to configure the hardware as long as the hardware
48 * has a &struct device.
49 *
50 * If @dev->hw_dev is NULL, it gets a reference to @hw_dev and sets
51 * @dev->hw_dev, otherwise, it does nothing. Calling it multiple times
52 * with the same hardware device is not considered an error. If it gets
53 * a reference to the hardware device, it will be automatically 'put' when
54 * the device is detached from COMEDI.
55 *
56 * Returns 0 if @dev->hw_dev was NULL or the same as @hw_dev, otherwise
57 * returns -EEXIST.
58 */
Ian Abbottda717512013-02-01 13:23:19 +000059int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
60{
Ian Abbottde06d7c2013-02-01 13:23:20 +000061 if (hw_dev == dev->hw_dev)
62 return 0;
H Hartley Sweetene2850162015-03-04 12:15:29 -070063 if (dev->hw_dev)
Ian Abbottde06d7c2013-02-01 13:23:20 +000064 return -EEXIST;
Ian Abbottda717512013-02-01 13:23:19 +000065 dev->hw_dev = get_device(hw_dev);
Ian Abbottda717512013-02-01 13:23:19 +000066 return 0;
67}
68EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
69
Ian Abbottde06d7c2013-02-01 13:23:20 +000070static void comedi_clear_hw_dev(struct comedi_device *dev)
71{
72 put_device(dev->hw_dev);
73 dev->hw_dev = NULL;
74}
75
H Hartley Sweeten54db9962013-06-24 16:55:14 -070076/**
Ian Abbott9b348452015-09-17 17:19:17 +010077 * comedi_alloc_devpriv() - Allocate memory for the device private data
78 * @dev: COMEDI device.
79 * @size: Size of the memory to allocate.
80 *
81 * The allocated memory is zero-filled. @dev->private points to it on
82 * return. The memory will be automatically freed when the COMEDI device is
83 * "detached".
84 *
85 * Returns a pointer to the allocated memory, or NULL on failure.
H Hartley Sweeten54db9962013-06-24 16:55:14 -070086 */
87void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
88{
89 dev->private = kzalloc(size, GFP_KERNEL);
90 return dev->private;
91}
92EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
93
Ian Abbott9b348452015-09-17 17:19:17 +010094/**
95 * comedi_alloc_subdevices() - Allocate subdevices for COMEDI device
96 * @dev: COMEDI device.
97 * @num_subdevices: Number of subdevices to allocate.
98 *
99 * Allocates and initializes an array of &struct comedi_subdevice for the
100 * COMEDI device. If successful, sets @dev->subdevices to point to the
101 * first one and @dev->n_subdevices to the number.
102 *
103 * Returns 0 on success, -EINVAL if @num_subdevices is < 1, or -ENOMEM if
104 * failed to allocate the memory.
105 */
H Hartley Sweeten8b9ba6e2012-06-12 11:57:27 -0700106int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700107{
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700108 struct comedi_subdevice *s;
H Hartley Sweeten8b9ba6e2012-06-12 11:57:27 -0700109 int i;
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700110
H Hartley Sweeten7f801c42012-06-12 11:57:45 -0700111 if (num_subdevices < 1)
112 return -EINVAL;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700113
114 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
115 if (!s)
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700116 return -ENOMEM;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700117 dev->subdevices = s;
H Hartley Sweetenfba1d0f2012-06-12 11:58:27 -0700118 dev->n_subdevices = num_subdevices;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700119
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700120 for (i = 0; i < num_subdevices; ++i) {
H Hartley Sweeten5e4c58c2012-09-05 18:21:25 -0700121 s = &dev->subdevices[i];
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700122 s->device = dev;
H Hartley Sweeten90a35c12012-12-19 17:27:02 -0700123 s->index = i;
H Hartley Sweeten03afcf42012-06-12 11:59:55 -0700124 s->async_dma_dir = DMA_NONE;
125 spin_lock_init(&s->spin_lock);
126 s->minor = -1;
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700127 }
128 return 0;
129}
130EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
131
H Hartley Sweetend2762062014-08-25 16:03:54 -0700132/**
Ian Abbott9b348452015-09-17 17:19:17 +0100133 * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback
134 * @s: COMEDI subdevice.
135 *
136 * This is called by low-level COMEDI drivers to allocate an array to record
137 * the last values written to a subdevice's analog output channels (at least
138 * by the %INSN_WRITE instruction), to allow them to be read back by an
139 * %INSN_READ instruction. It also provides a default handler for the
140 * %INSN_READ instruction unless one has already been set.
141 *
142 * On success, @s->readback points to the first element of the array, which
143 * is zero-filled. The low-level driver is responsible for updating its
144 * contents. @s->insn_read will be set to comedi_readback_insn_read()
145 * unless it is already non-NULL.
146 *
147 * Returns 0 on success, -EINVAL if the subdevice has no channels, or
148 * -ENOMEM on allocation failure.
H Hartley Sweetend2762062014-08-25 16:03:54 -0700149 */
150int comedi_alloc_subdev_readback(struct comedi_subdevice *s)
151{
152 if (!s->n_chan)
153 return -EINVAL;
154
155 s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL);
156 if (!s->readback)
157 return -ENOMEM;
H Hartley Sweetenaa116722014-11-21 10:19:10 -0700158
159 if (!s->insn_read)
160 s->insn_read = comedi_readback_insn_read;
161
H Hartley Sweetend2762062014-08-25 16:03:54 -0700162 return 0;
163}
164EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
165
Ian Abbott3867e202013-11-08 15:03:26 +0000166static void comedi_device_detach_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800167{
168 int i;
Bill Pemberton34c43922009-03-16 22:05:14 -0400169 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800170
171 if (dev->subdevices) {
172 for (i = 0; i < dev->n_subdevices; i++) {
H Hartley Sweeten5e4c58c2012-09-05 18:21:25 -0700173 s = &dev->subdevices[i];
Ian Abbott8fc369a2015-04-21 13:18:10 +0100174 if (comedi_can_auto_free_spriv(s))
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700175 kfree(s->private);
David Schleefed9eccb2008-11-04 20:29:31 -0800176 comedi_free_subdevice_minor(s);
177 if (s->async) {
178 comedi_buf_alloc(dev, s, 0);
179 kfree(s->async);
180 }
H Hartley Sweetend2762062014-08-25 16:03:54 -0700181 kfree(s->readback);
David Schleefed9eccb2008-11-04 20:29:31 -0800182 }
183 kfree(dev->subdevices);
184 dev->subdevices = NULL;
185 dev->n_subdevices = 0;
186 }
Bill Pembertondedd1322009-03-16 22:04:18 -0400187 kfree(dev->private);
H Hartley Sweeten43db0622015-02-23 14:57:29 -0700188 kfree(dev->pacer);
Bill Pembertondedd1322009-03-16 22:04:18 -0400189 dev->private = NULL;
H Hartley Sweeten43db0622015-02-23 14:57:29 -0700190 dev->pacer = NULL;
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700191 dev->driver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800192 dev->board_name = NULL;
193 dev->board_ptr = NULL;
H Hartley Sweetend7e6dc12014-07-29 15:01:20 -0700194 dev->mmio = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800195 dev->iobase = 0;
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700196 dev->iolen = 0;
Ian Abbott00ca68842013-03-15 13:15:35 +0000197 dev->ioenabled = false;
David Schleefed9eccb2008-11-04 20:29:31 -0800198 dev->irq = 0;
199 dev->read_subdev = NULL;
200 dev->write_subdev = NULL;
201 dev->open = NULL;
202 dev->close = NULL;
Ian Abbottde06d7c2013-02-01 13:23:20 +0000203 comedi_clear_hw_dev(dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800204}
205
Ian Abbott016599f2013-04-04 14:58:58 +0100206void comedi_device_detach(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800207{
Ian Abbottd19db512013-11-08 15:03:28 +0000208 comedi_device_cancel_all(dev);
Ian Abbottbf11c132013-11-08 15:03:25 +0000209 down_write(&dev->attach_lock);
Ian Abbotta7401cd2013-03-15 13:15:33 +0000210 dev->attached = false;
Ian Abbottef77c0b2013-11-08 15:03:29 +0000211 dev->detach_count++;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100212 if (dev->driver)
David Schleefed9eccb2008-11-04 20:29:31 -0800213 dev->driver->detach(dev);
Ian Abbott3867e202013-11-08 15:03:26 +0000214 comedi_device_detach_cleanup(dev);
Ian Abbottbf11c132013-11-08 15:03:25 +0000215 up_write(&dev->attach_lock);
David Schleefed9eccb2008-11-04 20:29:31 -0800216}
217
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700218static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
219{
220 return -EINVAL;
221}
222
223int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
224 struct comedi_insn *insn, unsigned int *data)
225{
226 return -EINVAL;
227}
228
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700229/**
H Hartley Sweetend2762062014-08-25 16:03:54 -0700230 * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback.
Ian Abbott9b348452015-09-17 17:19:17 +0100231 * @dev: COMEDI device.
232 * @s: COMEDI subdevice.
233 * @insn: COMEDI instruction.
234 * @data: Pointer to return the readback data.
235 *
236 * Handles the %INSN_READ instruction for subdevices that use the readback
237 * array allocated by comedi_alloc_subdev_readback(). It may be used
238 * directly as the subdevice's handler (@s->insn_read) or called via a
239 * wrapper.
240 *
241 * @insn->n is normally 1, which will read a single value. If higher, the
242 * same element of the readback array will be read multiple times.
243 *
244 * Returns @insn->n on success, or -EINVAL if @s->readback is NULL.
H Hartley Sweetend2762062014-08-25 16:03:54 -0700245 */
246int comedi_readback_insn_read(struct comedi_device *dev,
247 struct comedi_subdevice *s,
248 struct comedi_insn *insn,
249 unsigned int *data)
250{
251 unsigned int chan = CR_CHAN(insn->chanspec);
252 int i;
253
254 if (!s->readback)
255 return -EINVAL;
256
257 for (i = 0; i < insn->n; i++)
258 data[i] = s->readback[chan];
259
260 return insn->n;
261}
262EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
263
264/**
Ian Abbott9b348452015-09-17 17:19:17 +0100265 * comedi_timeout() - Busy-wait for a driver condition to occur
266 * @dev: COMEDI device.
267 * @s: COMEDI subdevice.
268 * @insn: COMEDI instruction.
269 * @cb: Callback to check for the condition.
270 * @context: Private context from the driver.
271 *
272 * Busy-waits for up to a second (%COMEDI_TIMEOUT_MS) for the condition or
273 * some error (other than -EBUSY) to occur. The parameters @dev, @s, @insn,
274 * and @context are passed to the callback function, which returns -EBUSY to
275 * continue waiting or some other value to stop waiting (generally 0 if the
276 * condition occurred, or some error value).
277 *
278 * Returns -ETIMEDOUT if timed out, otherwise the return value from the
279 * callback function.
H Hartley Sweeten91506402014-02-10 11:49:00 -0700280 */
281int comedi_timeout(struct comedi_device *dev,
282 struct comedi_subdevice *s,
283 struct comedi_insn *insn,
284 int (*cb)(struct comedi_device *dev,
285 struct comedi_subdevice *s,
286 struct comedi_insn *insn,
287 unsigned long context),
288 unsigned long context)
289{
290 unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS);
291 int ret;
292
293 while (time_before(jiffies, timeout)) {
294 ret = cb(dev, s, insn, context);
295 if (ret != -EBUSY)
296 return ret; /* success (0) or non EBUSY errno */
297 cpu_relax();
298 }
299 return -ETIMEDOUT;
300}
301EXPORT_SYMBOL_GPL(comedi_timeout);
302
303/**
Ian Abbott9b348452015-09-17 17:19:17 +0100304 * comedi_dio_insn_config() - Boilerplate (*insn_config) for DIO subdevices
305 * @dev: COMEDI device.
306 * @s: COMEDI subdevice.
307 * @insn: COMEDI instruction.
308 * @data: Instruction parameters and return data.
309 * @mask: io_bits mask for grouped channels, or 0 for single channel.
310 *
311 * If @mask is 0, it is replaced with a single-bit mask corresponding to the
312 * channel number specified by @insn->chanspec. Otherwise, @mask
313 * corresponds to a group of channels (which should include the specified
314 * channel) that are always configured together as inputs or outputs.
315 *
316 * Partially handles the %INSN_CONFIG_DIO_INPUT, %INSN_CONFIG_DIO_OUTPUTS,
317 * and %INSN_CONFIG_DIO_QUERY instructions. The first two update
318 * @s->io_bits to record the directions of the masked channels. The last
319 * one sets @data[1] to the current direction of the group of channels
320 * (%COMEDI_INPUT) or %COMEDI_OUTPUT) as recorded in @s->io_bits.
321 *
322 * The caller is responsible for updating the DIO direction in the hardware
323 * registers if this function returns 0.
324 *
325 * Returns 0 for a %INSN_CONFIG_DIO_INPUT or %INSN_CONFIG_DIO_OUTPUT
326 * instruction, @insn->n (> 0) for a %INSN_CONFIG_DIO_QUERY instruction, or
327 * -EINVAL for some other instruction.
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700328 */
329int comedi_dio_insn_config(struct comedi_device *dev,
330 struct comedi_subdevice *s,
331 struct comedi_insn *insn,
332 unsigned int *data,
333 unsigned int mask)
334{
335 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
336
337 if (!mask)
338 mask = chan_mask;
339
340 switch (data[0]) {
341 case INSN_CONFIG_DIO_INPUT:
342 s->io_bits &= ~mask;
343 break;
344
345 case INSN_CONFIG_DIO_OUTPUT:
346 s->io_bits |= mask;
347 break;
348
349 case INSN_CONFIG_DIO_QUERY:
350 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
351 return insn->n;
352
353 default:
354 return -EINVAL;
355 }
356
357 return 0;
358}
359EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
360
H Hartley Sweeten05e60b12013-08-30 11:04:56 -0700361/**
Ian Abbott9b348452015-09-17 17:19:17 +0100362 * comedi_dio_update_state() - Update the internal state of DIO subdevices
363 * @s: COMEDI subdevice.
364 * @data: The channel mask and bits to update.
365 *
366 * Updates @s->state which holds the internal state of the outputs for DIO
367 * or DO subdevices (up to 32 channels). @data[0] contains a bit-mask of
368 * the channels to be updated. @data[1] contains a bit-mask of those
369 * channels to be set to '1'. The caller is responsible for updating the
370 * outputs in hardware according to @s->state. As a minimum, the channels
371 * in the returned bit-mask need to be updated.
372 *
373 * Returns @mask with non-existent channels removed.
H Hartley Sweeten05e60b12013-08-30 11:04:56 -0700374 */
375unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
376 unsigned int *data)
377{
378 unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
379 : 0xffffffff;
380 unsigned int mask = data[0] & chanmask;
381 unsigned int bits = data[1];
382
383 if (mask) {
384 s->state &= ~mask;
385 s->state |= (bits & mask);
386 }
387
388 return mask;
389}
390EXPORT_SYMBOL_GPL(comedi_dio_update_state);
391
Ian Abbottf146fe62014-09-15 13:45:57 +0100392/**
Ian Abbott9b348452015-09-17 17:19:17 +0100393 * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes
394 * @s: COMEDI subdevice.
Ian Abbottf146fe62014-09-15 13:45:57 +0100395 *
396 * Determines the overall scan length according to the subdevice type and the
397 * number of channels in the scan.
398 *
Ian Abbott9b348452015-09-17 17:19:17 +0100399 * For digital input, output or input/output subdevices, samples for
400 * multiple channels are assumed to be packed into one or more unsigned
401 * short or unsigned int values according to the subdevice's %SDF_LSAMPL
402 * flag. For other types of subdevice, samples are assumed to occupy a
403 * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag.
Ian Abbottf146fe62014-09-15 13:45:57 +0100404 *
405 * Returns the overall scan length in bytes.
406 */
407unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
408{
409 struct comedi_cmd *cmd = &s->async->cmd;
410 unsigned int num_samples;
411 unsigned int bits_per_sample;
412
413 switch (s->type) {
414 case COMEDI_SUBD_DI:
415 case COMEDI_SUBD_DO:
416 case COMEDI_SUBD_DIO:
H Hartley Sweetenc39e0502014-10-31 12:04:28 -0700417 bits_per_sample = 8 * comedi_bytes_per_sample(s);
Ian Abbottaf57d892014-11-12 16:00:48 +0000418 num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample);
Ian Abbottf146fe62014-09-15 13:45:57 +0100419 break;
420 default:
Ian Abbottaf57d892014-11-12 16:00:48 +0000421 num_samples = cmd->scan_end_arg;
Ian Abbottf146fe62014-09-15 13:45:57 +0100422 break;
423 }
H Hartley Sweetenc39e0502014-10-31 12:04:28 -0700424 return comedi_samples_to_bytes(s, num_samples);
Ian Abbottf146fe62014-09-15 13:45:57 +0100425}
426EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
427
Ian Abbott92d354c2015-10-23 10:56:09 +0100428static unsigned int __comedi_nscans_left(struct comedi_subdevice *s,
429 unsigned int nscans)
430{
431 struct comedi_async *async = s->async;
432 struct comedi_cmd *cmd = &async->cmd;
433
434 if (cmd->stop_src == TRIG_COUNT) {
435 unsigned int scans_left = 0;
436
437 if (async->scans_done < cmd->stop_arg)
438 scans_left = cmd->stop_arg - async->scans_done;
439
440 if (nscans > scans_left)
441 nscans = scans_left;
442 }
443 return nscans;
444}
445
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100446/**
Ian Abbott9b348452015-09-17 17:19:17 +0100447 * comedi_nscans_left() - Return the number of scans left in the command
448 * @s: COMEDI subdevice.
449 * @nscans: The expected number of scans or 0 for all available scans.
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700450 *
Ian Abbott9b348452015-09-17 17:19:17 +0100451 * If @nscans is 0, it is set to the number of scans available in the
452 * async buffer.
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700453 *
Ian Abbott9b348452015-09-17 17:19:17 +0100454 * If the async command has a stop_src of %TRIG_COUNT, the @nscans will be
455 * checked against the number of scans remaining to complete the command.
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700456 *
457 * The return value will then be either the expected number of scans or the
Ian Abbott9b348452015-09-17 17:19:17 +0100458 * number of scans remaining to complete the command, whichever is fewer.
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700459 */
460unsigned int comedi_nscans_left(struct comedi_subdevice *s,
461 unsigned int nscans)
462{
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700463 if (nscans == 0) {
464 unsigned int nbytes = comedi_buf_read_n_available(s);
465
466 nscans = nbytes / comedi_bytes_per_scan(s);
467 }
Ian Abbott92d354c2015-10-23 10:56:09 +0100468 return __comedi_nscans_left(s, nscans);
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700469}
470EXPORT_SYMBOL_GPL(comedi_nscans_left);
471
472/**
Ian Abbott9b348452015-09-17 17:19:17 +0100473 * comedi_nsamples_left() - Return the number of samples left in the command
474 * @s: COMEDI subdevice.
475 * @nsamples: The expected number of samples.
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700476 *
Ian Abbott9b348452015-09-17 17:19:17 +0100477 * Returns the number of samples remaining to complete the command, or the
478 * specified expected number of samples (@nsamples), whichever is fewer.
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700479 */
480unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
481 unsigned int nsamples)
482{
483 struct comedi_async *async = s->async;
484 struct comedi_cmd *cmd = &async->cmd;
485
486 if (cmd->stop_src == TRIG_COUNT) {
Ian Abbott92d354c2015-10-23 10:56:09 +0100487 unsigned int nscans = nsamples / cmd->scan_end_arg;
488 unsigned int scans_left = __comedi_nscans_left(s, nscans);
Ian Abbottaf57d892014-11-12 16:00:48 +0000489 unsigned int scan_pos =
490 comedi_bytes_to_samples(s, async->scan_progress);
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700491 unsigned long long samples_left = 0;
492
493 if (scans_left) {
494 samples_left = ((unsigned long long)scans_left *
Ian Abbottaf57d892014-11-12 16:00:48 +0000495 cmd->scan_end_arg) - scan_pos;
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700496 }
497
498 if (samples_left < nsamples)
499 nsamples = samples_left;
500 }
501 return nsamples;
502}
503EXPORT_SYMBOL_GPL(comedi_nsamples_left);
504
505/**
Ian Abbott9b348452015-09-17 17:19:17 +0100506 * comedi_inc_scan_progress() - Update scan progress in asynchronous command
507 * @s: COMEDI subdevice.
508 * @num_bytes: Amount of data in bytes to increment scan progress.
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100509 *
Ian Abbott9b348452015-09-17 17:19:17 +0100510 * Increments the scan progress by the number of bytes specified by @num_bytes.
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100511 * If the scan progress reaches or exceeds the scan length in bytes, reduce
512 * it modulo the scan length in bytes and set the "end of scan" asynchronous
Ian Abbott9b348452015-09-17 17:19:17 +0100513 * event flag (%COMEDI_CB_EOS) to be processed later.
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100514 */
515void comedi_inc_scan_progress(struct comedi_subdevice *s,
516 unsigned int num_bytes)
517{
518 struct comedi_async *async = s->async;
H Hartley Sweetenf8736ca2014-10-31 09:49:31 -0700519 struct comedi_cmd *cmd = &async->cmd;
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100520 unsigned int scan_length = comedi_bytes_per_scan(s);
521
H Hartley Sweetenf8736ca2014-10-31 09:49:31 -0700522 /* track the 'cur_chan' for non-SDF_PACKED subdevices */
523 if (!(s->subdev_flags & SDF_PACKED)) {
524 async->cur_chan += comedi_bytes_to_samples(s, num_bytes);
525 async->cur_chan %= cmd->chanlist_len;
526 }
527
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100528 async->scan_progress += num_bytes;
529 if (async->scan_progress >= scan_length) {
H Hartley Sweeten1dacbe52014-11-05 10:20:52 -0700530 unsigned int nscans = async->scan_progress / scan_length;
531
532 if (async->scans_done < (UINT_MAX - nscans))
533 async->scans_done += nscans;
534 else
535 async->scans_done = UINT_MAX;
536
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100537 async->scan_progress %= scan_length;
538 async->events |= COMEDI_CB_EOS;
539 }
540}
541EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
542
Ian Abbott5a780352014-09-15 13:46:01 +0100543/**
Ian Abbott9b348452015-09-17 17:19:17 +0100544 * comedi_handle_events() - Handle events and possibly stop acquisition
545 * @dev: COMEDI device.
546 * @s: COMEDI subdevice.
Ian Abbott5a780352014-09-15 13:46:01 +0100547 *
548 * Handles outstanding asynchronous acquisition event flags associated
Ian Abbott9b348452015-09-17 17:19:17 +0100549 * with the subdevice. Call the subdevice's @s->cancel() handler if the
Ian Abbott5a780352014-09-15 13:46:01 +0100550 * "end of acquisition", "error" or "overflow" event flags are set in order
551 * to stop the acquisition at the driver level.
552 *
553 * Calls comedi_event() to further process the event flags, which may mark
554 * the asynchronous command as no longer running, possibly terminated with
555 * an error, and may wake up tasks.
556 *
557 * Return a bit-mask of the handled events.
558 */
559unsigned int comedi_handle_events(struct comedi_device *dev,
560 struct comedi_subdevice *s)
561{
562 unsigned int events = s->async->events;
563
564 if (events == 0)
565 return events;
566
H Hartley Sweeten7be7cd12016-03-30 10:47:25 -0700567 if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel)
Ian Abbott5a780352014-09-15 13:46:01 +0100568 s->cancel(dev, s);
569
570 comedi_event(dev, s);
571
572 return events;
573}
574EXPORT_SYMBOL_GPL(comedi_handle_events);
575
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700576static int insn_rw_emulate_bits(struct comedi_device *dev,
577 struct comedi_subdevice *s,
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700578 struct comedi_insn *insn,
579 unsigned int *data)
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700580{
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700581 struct comedi_insn _insn;
582 unsigned int chan = CR_CHAN(insn->chanspec);
583 unsigned int base_chan = (chan < 32) ? 0 : chan;
584 unsigned int _data[2];
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700585 int ret;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700586
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700587 memset(_data, 0, sizeof(_data));
588 memset(&_insn, 0, sizeof(_insn));
589 _insn.insn = INSN_BITS;
590 _insn.chanspec = base_chan;
591 _insn.n = 2;
592 _insn.subdev = insn->subdev;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700593
594 if (insn->insn == INSN_WRITE) {
595 if (!(s->subdev_flags & SDF_WRITABLE))
596 return -EINVAL;
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700597 _data[0] = 1 << (chan - base_chan); /* mask */
598 _data[1] = data[0] ? (1 << (chan - base_chan)) : 0; /* bits */
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700599 }
600
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700601 ret = s->insn_bits(dev, s, &_insn, _data);
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700602 if (ret < 0)
603 return ret;
604
605 if (insn->insn == INSN_READ)
H Hartley Sweetenf91852c2016-03-30 10:47:24 -0700606 data[0] = (_data[1] >> (chan - base_chan)) & 1;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700607
608 return 1;
609}
610
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700611static int __comedi_device_postconfig_async(struct comedi_device *dev,
612 struct comedi_subdevice *s)
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700613{
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700614 struct comedi_async *async;
615 unsigned int buf_size;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700616 int ret;
617
H Hartley Sweeten57b71c32013-01-21 14:37:15 -0700618 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
619 dev_warn(dev->class_dev,
620 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
621 return -EINVAL;
622 }
623 if (!s->do_cmdtest) {
624 dev_warn(dev->class_dev,
625 "async subdevices must have a do_cmdtest() function\n");
626 return -EINVAL;
627 }
H Hartley Sweeten7be7cd12016-03-30 10:47:25 -0700628 if (!s->cancel)
629 dev_warn(dev->class_dev,
630 "async subdevices should have a cancel() function\n");
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700631
632 async = kzalloc(sizeof(*async), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800633 if (!async)
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700634 return -ENOMEM;
Joe Perches78110bb2013-02-11 09:41:29 -0800635
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700636 init_waitqueue_head(&async->wait_head);
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700637 s->async = async;
638
639 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
640 buf_size = comedi_default_buf_size_kb * 1024;
641 if (buf_size > async->max_bufsize)
642 buf_size = async->max_bufsize;
643
644 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
645 dev_warn(dev->class_dev, "Buffer allocation failed\n");
646 return -ENOMEM;
647 }
648 if (s->buf_change) {
H Hartley Sweetend546b892014-07-21 11:48:32 -0700649 ret = s->buf_change(dev, s);
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700650 if (ret < 0)
651 return ret;
652 }
653
Ian Abbottf65cc542013-02-01 10:20:30 +0000654 comedi_alloc_subdevice_minor(s);
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700655
656 return 0;
657}
658
659static int __comedi_device_postconfig(struct comedi_device *dev)
660{
661 struct comedi_subdevice *s;
662 int ret;
663 int i;
664
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700665 for (i = 0; i < dev->n_subdevices; i++) {
666 s = &dev->subdevices[i];
667
668 if (s->type == COMEDI_SUBD_UNUSED)
669 continue;
670
H Hartley Sweeten09567cb2013-08-30 10:47:03 -0700671 if (s->type == COMEDI_SUBD_DO) {
672 if (s->n_chan < 32)
673 s->io_bits = (1 << s->n_chan) - 1;
674 else
675 s->io_bits = 0xffffffff;
676 }
677
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700678 if (s->len_chanlist == 0)
679 s->len_chanlist = 1;
680
681 if (s->do_cmd) {
H Hartley Sweeten40f58a62013-01-21 14:36:40 -0700682 ret = __comedi_device_postconfig_async(dev, s);
683 if (ret)
684 return ret;
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700685 }
686
687 if (!s->range_table && !s->range_table_list)
688 s->range_table = &range_unknown;
689
690 if (!s->insn_read && s->insn_bits)
691 s->insn_read = insn_rw_emulate_bits;
692 if (!s->insn_write && s->insn_bits)
693 s->insn_write = insn_rw_emulate_bits;
694
695 if (!s->insn_read)
696 s->insn_read = insn_inval;
697 if (!s->insn_write)
698 s->insn_write = insn_inval;
699 if (!s->insn_bits)
700 s->insn_bits = insn_inval;
701 if (!s->insn_config)
702 s->insn_config = insn_inval;
703
704 if (!s->poll)
705 s->poll = poll_invalid;
706 }
707
708 return 0;
709}
710
Ian Abbott3902a372012-03-30 17:15:00 +0100711/* do a little post-config cleanup */
Ian Abbott3902a372012-03-30 17:15:00 +0100712static int comedi_device_postconfig(struct comedi_device *dev)
713{
Ian Abbottb2a644b2013-04-04 14:58:56 +0100714 int ret;
715
716 ret = __comedi_device_postconfig(dev);
H Hartley Sweetenae5dd5f2013-04-08 10:56:02 -0700717 if (ret < 0)
Ian Abbott3902a372012-03-30 17:15:00 +0100718 return ret;
Ian Abbottbf11c132013-11-08 15:03:25 +0000719 down_write(&dev->attach_lock);
Ian Abbotta7401cd2013-03-15 13:15:33 +0000720 dev->attached = true;
Ian Abbottbf11c132013-11-08 15:03:25 +0000721 up_write(&dev->attach_lock);
Ian Abbott3902a372012-03-30 17:15:00 +0100722 return 0;
723}
724
H Hartley Sweeten01fca372013-01-21 14:36:11 -0700725/*
726 * Generic recognize function for drivers that register their supported
727 * board names.
728 *
729 * 'driv->board_name' points to a 'const char *' member within the
730 * zeroth element of an array of some private board information
731 * structure, say 'struct foo_board' containing a member 'const char
732 * *board_name' that is initialized to point to a board name string that
733 * is one of the candidates matched against this function's 'name'
734 * parameter.
735 *
736 * 'driv->offset' is the size of the private board information
737 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
738 * the length of the array of private board information structures.
739 *
740 * If one of the board names in the array of private board information
741 * structures matches the name supplied to this function, the function
742 * returns a pointer to the pointer to the board name, otherwise it
743 * returns NULL. The return value ends up in the 'board_ptr' member of
744 * a 'struct comedi_device' that the low-level comedi driver's
745 * 'attach()' hook can convert to a point to a particular element of its
746 * array of private board information structures by subtracting the
747 * offset of the member that points to the board name. (No subtraction
748 * is required if the board name pointer is the first member of the
749 * private board information structure, which is generally the case.)
750 */
751static void *comedi_recognize(struct comedi_driver *driv, const char *name)
752{
753 char **name_ptr = (char **)driv->board_name;
754 int i;
755
756 for (i = 0; i < driv->num_names; i++) {
757 if (strcmp(*name_ptr, name) == 0)
758 return name_ptr;
759 name_ptr = (void *)name_ptr + driv->offset;
760 }
761
762 return NULL;
763}
764
765static void comedi_report_boards(struct comedi_driver *driv)
766{
767 unsigned int i;
768 const char *const *name_ptr;
769
770 pr_info("comedi: valid board names for %s driver are:\n",
771 driv->driver_name);
772
773 name_ptr = driv->board_name;
774 for (i = 0; i < driv->num_names; i++) {
775 pr_info(" %s\n", *name_ptr);
776 name_ptr = (const char **)((char *)name_ptr + driv->offset);
777 }
778
779 if (driv->num_names == 0)
780 pr_info(" %s\n", driv->driver_name);
781}
782
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700783/**
Ian Abbott9b348452015-09-17 17:19:17 +0100784 * comedi_load_firmware() - Request and load firmware for a device
785 * @dev: COMEDI device.
786 * @device: Hardware device.
787 * @name: The name of the firmware image.
788 * @cb: Callback to the upload the firmware image.
789 * @context: Private context from the driver.
790 *
791 * Sends a firmware request for the hardware device and waits for it. Calls
792 * the callback function to upload the firmware to the device, them releases
793 * the firmware.
794 *
795 * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number
796 * from the firmware request or the callback function.
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700797 */
798int comedi_load_firmware(struct comedi_device *dev,
799 struct device *device,
800 const char *name,
801 int (*cb)(struct comedi_device *dev,
H Hartley Sweetend5695412013-05-17 11:18:01 -0700802 const u8 *data, size_t size,
803 unsigned long context),
804 unsigned long context)
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700805{
806 const struct firmware *fw;
807 int ret;
808
809 if (!cb)
810 return -EINVAL;
811
812 ret = request_firmware(&fw, name, device);
813 if (ret == 0) {
H Hartley Sweetend5695412013-05-17 11:18:01 -0700814 ret = cb(dev, fw->data, fw->size, context);
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700815 release_firmware(fw);
816 }
817
H Hartley Sweetenc6236c02013-12-10 16:31:25 -0700818 return ret < 0 ? ret : 0;
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700819}
820EXPORT_SYMBOL_GPL(comedi_load_firmware);
821
822/**
Ian Abbott9b348452015-09-17 17:19:17 +0100823 * __comedi_request_region() - Request an I/O region for a legacy driver
824 * @dev: COMEDI device.
825 * @start: Base address of the I/O region.
826 * @len: Length of the I/O region.
827 *
828 * Requests the specified I/O port region which must start at a non-zero
829 * address.
830 *
831 * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
832 * fails.
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700833 */
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700834int __comedi_request_region(struct comedi_device *dev,
835 unsigned long start, unsigned long len)
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700836{
837 if (!start) {
838 dev_warn(dev->class_dev,
839 "%s: a I/O base address must be specified\n",
840 dev->board_name);
841 return -EINVAL;
842 }
843
844 if (!request_region(start, len, dev->board_name)) {
845 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
846 dev->board_name, start, len);
847 return -EIO;
848 }
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700849
850 return 0;
851}
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700852EXPORT_SYMBOL_GPL(__comedi_request_region);
853
854/**
Ian Abbott9b348452015-09-17 17:19:17 +0100855 * 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.
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700868 */
869int comedi_request_region(struct comedi_device *dev,
870 unsigned long start, unsigned long len)
871{
872 int ret;
873
874 ret = __comedi_request_region(dev, start, len);
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700875 if (ret == 0) {
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700876 dev->iobase = start;
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700877 dev->iolen = len;
878 }
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700879
880 return ret;
881}
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700882EXPORT_SYMBOL_GPL(comedi_request_region);
883
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700884/**
Ian Abbott9b348452015-09-17 17:19:17 +0100885 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers
886 * @dev: COMEDI device.
887 *
888 * This is a simple, generic 'detach' handler for legacy COMEDI devices that
889 * just use a single I/O port region and possibly an IRQ and that don't need
890 * any special clean-up for their private device or subdevice storage. It
891 * can also be called by a driver-specific 'detach' handler.
892 *
893 * If @dev->irq is non-zero, the IRQ will be freed. If @dev->iobase and
894 * @dev->iolen are both non-zero, the I/O port region will be released.
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700895 */
896void comedi_legacy_detach(struct comedi_device *dev)
897{
H Hartley Sweeten3d1fe3f2013-04-18 14:34:37 -0700898 if (dev->irq) {
899 free_irq(dev->irq, dev);
900 dev->irq = 0;
901 }
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700902 if (dev->iobase && dev->iolen) {
903 release_region(dev->iobase, dev->iolen);
904 dev->iobase = 0;
905 dev->iolen = 0;
906 }
907}
908EXPORT_SYMBOL_GPL(comedi_legacy_detach);
909
Bill Pemberton0707bb02009-03-16 22:06:20 -0400910int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
David Schleefed9eccb2008-11-04 20:29:31 -0800911{
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400912 struct comedi_driver *driv;
David Schleefed9eccb2008-11-04 20:29:31 -0800913 int ret;
914
915 if (dev->attached)
916 return -EBUSY;
917
Ian Abbottc383e2d2013-06-27 14:50:58 +0100918 mutex_lock(&comedi_drivers_list_lock);
David Schleefed9eccb2008-11-04 20:29:31 -0800919 for (driv = comedi_drivers; driv; driv = driv->next) {
GĂ¼ngör Erseymen559e9a62012-09-11 17:56:42 +0300920 if (!try_module_get(driv->module))
David Schleefed9eccb2008-11-04 20:29:31 -0800921 continue;
David Schleefed9eccb2008-11-04 20:29:31 -0800922 if (driv->num_names) {
923 dev->board_ptr = comedi_recognize(driv, it->board_name);
Ian Abbott3902a372012-03-30 17:15:00 +0100924 if (dev->board_ptr)
925 break;
H Hartley Sweeten3df9f212014-07-18 14:28:11 -0700926 } else if (strcmp(driv->driver_name, it->board_name) == 0) {
Ian Abbott3902a372012-03-30 17:15:00 +0100927 break;
H Hartley Sweeten3df9f212014-07-18 14:28:11 -0700928 }
David Schleefed9eccb2008-11-04 20:29:31 -0800929 module_put(driv->module);
930 }
H Hartley Sweetene2850162015-03-04 12:15:29 -0700931 if (!driv) {
Ian Abbott3902a372012-03-30 17:15:00 +0100932 /* recognize has failed if we get here */
933 /* report valid board names before returning error */
934 for (driv = comedi_drivers; driv; driv = driv->next) {
GĂ¼ngör Erseymen559e9a62012-09-11 17:56:42 +0300935 if (!try_module_get(driv->module))
Ian Abbott3902a372012-03-30 17:15:00 +0100936 continue;
Ian Abbott3902a372012-03-30 17:15:00 +0100937 comedi_report_boards(driv);
938 module_put(driv->module);
939 }
Ian Abbottc383e2d2013-06-27 14:50:58 +0100940 ret = -EIO;
941 goto out;
Ian Abbott3902a372012-03-30 17:15:00 +0100942 }
H Hartley Sweetene2850162015-03-04 12:15:29 -0700943 if (!driv->attach) {
Ian Abbott8c3714d2012-08-15 15:02:45 +0100944 /* driver does not support manual configuration */
945 dev_warn(dev->class_dev,
946 "driver '%s' does not support attach using comedi_config\n",
947 driv->driver_name);
948 module_put(driv->module);
Ted Chen1a59adb2015-08-05 01:18:46 +0800949 ret = -EIO;
Ian Abbottc383e2d2013-06-27 14:50:58 +0100950 goto out;
Ian Abbott8c3714d2012-08-15 15:02:45 +0100951 }
Ian Abbott3902a372012-03-30 17:15:00 +0100952 dev->driver = driv;
H Hartley Sweeten34b68402013-04-08 10:55:29 -0700953 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
954 : dev->driver->driver_name;
Ian Abbott3902a372012-03-30 17:15:00 +0100955 ret = driv->attach(dev, it);
Ian Abbott74ece1082013-04-04 14:58:59 +0100956 if (ret >= 0)
957 ret = comedi_device_postconfig(dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800958 if (ret < 0) {
Ian Abbott016599f2013-04-04 14:58:58 +0100959 comedi_device_detach(dev);
Ian Abbott3955dfa2013-08-23 12:37:17 +0100960 module_put(driv->module);
David Schleefed9eccb2008-11-04 20:29:31 -0800961 }
Ian Abbottb2a644b2013-04-04 14:58:56 +0100962 /* On success, the driver module count has been incremented. */
Ian Abbottc383e2d2013-06-27 14:50:58 +0100963out:
964 mutex_unlock(&comedi_drivers_list_lock);
Ian Abbottb2a644b2013-04-04 14:58:56 +0100965 return ret;
David Schleefed9eccb2008-11-04 20:29:31 -0800966}
967
Ian Abbott9b348452015-09-17 17:19:17 +0100968/**
969 * comedi_auto_config() - Create a COMEDI device for a hardware device
970 * @hardware_device: Hardware device.
971 * @driver: COMEDI low-level driver for the hardware device.
972 * @context: Driver context for the auto_attach handler.
973 *
974 * Allocates a new COMEDI device for the hardware device and calls the
975 * low-level driver's 'auto_attach' handler to set-up the hardware and
976 * allocate the COMEDI subdevices. Additional "post-configuration" setting
977 * up is performed on successful return from the 'auto_attach' handler.
978 * If the 'auto_attach' handler fails, the low-level driver's 'detach'
979 * handler will be called as part of the clean-up.
980 *
981 * This is usually called from a wrapper function in a bus-specific COMEDI
982 * module, which in turn is usually called from a bus device 'probe'
983 * function in the low-level driver.
984 *
985 * Returns 0 on success, -EINVAL if the parameters are invalid or the
986 * post-configuration determines the driver has set the COMEDI device up
987 * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of
988 * COMEDI minor device numbers, or some negative error number returned by
989 * the driver's 'auto_attach' handler.
990 */
Ian Abbotta588da12012-11-14 13:10:38 +0000991int comedi_auto_config(struct device *hardware_device,
992 struct comedi_driver *driver, unsigned long context)
Ian Abbottf40116702012-03-30 17:15:01 +0100993{
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -0700994 struct comedi_device *dev;
Ian Abbottf40116702012-03-30 17:15:01 +0100995 int ret;
996
Ian Abbottf08e0ac2013-04-04 14:58:42 +0100997 if (!hardware_device) {
998 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
999 return -EINVAL;
1000 }
1001 if (!driver) {
1002 dev_warn(hardware_device,
1003 "BUG! comedi_auto_config called with NULL comedi driver\n");
1004 return -EINVAL;
1005 }
1006
Ian Abbotta588da12012-11-14 13:10:38 +00001007 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
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001014 dev = comedi_alloc_board_minor(hardware_device);
Bernd Porrbcb62322014-01-07 21:42:25 +00001015 if (IS_ERR(dev)) {
1016 dev_warn(hardware_device,
1017 "driver '%s' could not create device.\n",
1018 driver->driver_name);
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001019 return PTR_ERR(dev);
Bernd Porrbcb62322014-01-07 21:42:25 +00001020 }
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001021 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
Ian Abbottf40116702012-03-30 17:15:01 +01001022
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001023 dev->driver = driver;
H Hartley Sweeten34b68402013-04-08 10:55:29 -07001024 dev->board_name = dev->driver->driver_name;
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001025 ret = driver->auto_attach(dev, context);
Ian Abbott74ece1082013-04-04 14:58:59 +01001026 if (ret >= 0)
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001027 ret = comedi_device_postconfig(dev);
H Hartley Sweeten6013a9a2013-04-08 10:55:05 -07001028 mutex_unlock(&dev->mutex);
Ian Abbottf40116702012-03-30 17:15:01 +01001029
Bernd Porrbcb62322014-01-07 21:42:25 +00001030 if (ret < 0) {
1031 dev_warn(hardware_device,
1032 "driver '%s' failed to auto-configure device.\n",
1033 driver->driver_name);
Ian Abbottf5b31e12013-04-04 14:58:48 +01001034 comedi_release_hardware_device(hardware_device);
Bernd Porrbcb62322014-01-07 21:42:25 +00001035 } else {
1036 /*
1037 * class_dev should be set properly here
1038 * after a successful auto config
1039 */
1040 dev_info(dev->class_dev,
1041 "driver '%s' has successfully auto-configured '%s'.\n",
1042 driver->driver_name, dev->board_name);
1043 }
Ian Abbottf40116702012-03-30 17:15:01 +01001044 return ret;
1045}
Ian Abbott8ed705a2012-10-27 21:44:14 +01001046EXPORT_SYMBOL_GPL(comedi_auto_config);
1047
Ian Abbott9b348452015-09-17 17:19:17 +01001048/**
1049 * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device
1050 * @hardware_device: Hardware device previously passed to
1051 * comedi_auto_config().
1052 *
1053 * Cleans up and eventually destroys the COMEDI device allocated by
1054 * comedi_auto_config() for the same hardware device. As part of this
1055 * clean-up, the low-level COMEDI driver's 'detach' handler will be called.
1056 * (The COMEDI device itself will persist in an unattached state if it is
1057 * still open, until it is released, and any mmapped buffers will persist
1058 * until they are munmapped.)
1059 *
1060 * This is usually called from a wrapper module in a bus-specific COMEDI
1061 * module, which in turn is usually set as the bus device 'remove' function
1062 * in the low-level COMEDI driver.
1063 */
Ian Abbott8ed705a2012-10-27 21:44:14 +01001064void comedi_auto_unconfig(struct device *hardware_device)
David Schleefed9eccb2008-11-04 20:29:31 -08001065{
H Hartley Sweetene2850162015-03-04 12:15:29 -07001066 if (!hardware_device)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301067 return;
Ian Abbott3346b792013-04-04 14:58:47 +01001068 comedi_release_hardware_device(hardware_device);
David Schleefed9eccb2008-11-04 20:29:31 -08001069}
Ian Abbott8ed705a2012-10-27 21:44:14 +01001070EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001071
Ian Abbott9b348452015-09-17 17:19:17 +01001072/**
1073 * comedi_driver_register() - Register a low-level COMEDI driver
1074 * @driver: Low-level COMEDI driver.
1075 *
1076 * The low-level COMEDI driver is added to the list of registered COMEDI
1077 * drivers. This is used by the handler for the "/proc/comedi" file and is
1078 * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure
1079 * "legacy" COMEDI devices (for those low-level drivers that support it).
1080 *
1081 * Returns 0.
1082 */
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001083int comedi_driver_register(struct comedi_driver *driver)
1084{
Ian Abbottc383e2d2013-06-27 14:50:58 +01001085 mutex_lock(&comedi_drivers_list_lock);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001086 driver->next = comedi_drivers;
1087 comedi_drivers = driver;
Ian Abbottc383e2d2013-06-27 14:50:58 +01001088 mutex_unlock(&comedi_drivers_list_lock);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001089
1090 return 0;
1091}
H Hartley Sweeten5660e742013-04-12 10:11:54 -07001092EXPORT_SYMBOL_GPL(comedi_driver_register);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001093
Ian Abbott9b348452015-09-17 17:19:17 +01001094/**
1095 * comedi_driver_unregister() - Unregister a low-level COMEDI driver
1096 * @driver: Low-level COMEDI driver.
1097 *
1098 * The low-level COMEDI driver is removed from the list of registered COMEDI
1099 * drivers. Detaches any COMEDI devices attached to the driver, which will
1100 * result in the low-level driver's 'detach' handler being called for those
1101 * devices before this function returns.
1102 */
Ian Abbott99c0e262013-06-27 14:50:57 +01001103void comedi_driver_unregister(struct comedi_driver *driver)
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001104{
1105 struct comedi_driver *prev;
1106 int i;
1107
Ian Abbottc383e2d2013-06-27 14:50:58 +01001108 /* unlink the driver */
1109 mutex_lock(&comedi_drivers_list_lock);
1110 if (comedi_drivers == driver) {
1111 comedi_drivers = driver->next;
1112 } else {
1113 for (prev = comedi_drivers; prev->next; prev = prev->next) {
1114 if (prev->next == driver) {
1115 prev->next = driver->next;
1116 break;
1117 }
1118 }
1119 }
1120 mutex_unlock(&comedi_drivers_list_lock);
1121
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001122 /* check for devices using this driver */
1123 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
Ian Abbotta200fad2013-11-08 15:03:35 +00001124 struct comedi_device *dev = comedi_dev_get_from_minor(i);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001125
1126 if (!dev)
1127 continue;
1128
1129 mutex_lock(&dev->mutex);
1130 if (dev->attached && dev->driver == driver) {
1131 if (dev->use_count)
1132 dev_warn(dev->class_dev,
1133 "BUG! detaching device with use_count=%d\n",
1134 dev->use_count);
1135 comedi_device_detach(dev);
1136 }
1137 mutex_unlock(&dev->mutex);
Ian Abbotta200fad2013-11-08 15:03:35 +00001138 comedi_dev_put(dev);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001139 }
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -07001140}
H Hartley Sweeten5660e742013-04-12 10:11:54 -07001141EXPORT_SYMBOL_GPL(comedi_driver_unregister);