• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  module/drivers.c
4  *  functions for manipulating drivers
5  *
6  *  COMEDI - Linux Control and Measurement Device Interface
7  *  Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8  *  Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
9  */
10 
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/ioport.h>
16 #include <linux/slab.h>
17 #include <linux/dma-direction.h>
18 #include <linux/interrupt.h>
19 #include <linux/firmware.h>
20 #include <linux/comedi/comedidev.h>
21 #include "comedi_internal.h"
22 
23 struct comedi_driver *comedi_drivers;
24 /* protects access to comedi_drivers */
25 DEFINE_MUTEX(comedi_drivers_list_lock);
26 
27 /**
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  */
comedi_set_hw_dev(struct comedi_device * dev,struct device * hw_dev)49 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
50 {
51 	if (hw_dev == dev->hw_dev)
52 		return 0;
53 	if (dev->hw_dev)
54 		return -EEXIST;
55 	dev->hw_dev = get_device(hw_dev);
56 	return 0;
57 }
58 EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
59 
comedi_clear_hw_dev(struct comedi_device * dev)60 static void comedi_clear_hw_dev(struct comedi_device *dev)
61 {
62 	put_device(dev->hw_dev);
63 	dev->hw_dev = NULL;
64 }
65 
66 /**
67  * 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.
76  */
comedi_alloc_devpriv(struct comedi_device * dev,size_t size)77 void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
78 {
79 	dev->private = kzalloc(size, GFP_KERNEL);
80 	return dev->private;
81 }
82 EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
83 
84 /**
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  */
comedi_alloc_subdevices(struct comedi_device * dev,int num_subdevices)96 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
97 {
98 	struct comedi_subdevice *s;
99 	int i;
100 
101 	if (num_subdevices < 1)
102 		return -EINVAL;
103 
104 	s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
105 	if (!s)
106 		return -ENOMEM;
107 	dev->subdevices = s;
108 	dev->n_subdevices = num_subdevices;
109 
110 	for (i = 0; i < num_subdevices; ++i) {
111 		s = &dev->subdevices[i];
112 		s->device = dev;
113 		s->index = i;
114 		s->async_dma_dir = DMA_NONE;
115 		spin_lock_init(&s->spin_lock);
116 		s->minor = -1;
117 	}
118 	return 0;
119 }
120 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
121 
122 /**
123  * 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.
139  */
comedi_alloc_subdev_readback(struct comedi_subdevice * s)140 int 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;
148 
149 	if (!s->insn_read)
150 		s->insn_read = comedi_readback_insn_read;
151 
152 	return 0;
153 }
154 EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
155 
comedi_device_detach_cleanup(struct comedi_device * dev)156 static void comedi_device_detach_cleanup(struct comedi_device *dev)
157 {
158 	int i;
159 	struct comedi_subdevice *s;
160 
161 	lockdep_assert_held(&dev->attach_lock);
162 	lockdep_assert_held(&dev->mutex);
163 	if (dev->subdevices) {
164 		for (i = 0; i < dev->n_subdevices; i++) {
165 			s = &dev->subdevices[i];
166 			if (comedi_can_auto_free_spriv(s))
167 				kfree(s->private);
168 			comedi_free_subdevice_minor(s);
169 			if (s->async) {
170 				comedi_buf_alloc(dev, s, 0);
171 				kfree(s->async);
172 			}
173 			kfree(s->readback);
174 		}
175 		kfree(dev->subdevices);
176 		dev->subdevices = NULL;
177 		dev->n_subdevices = 0;
178 	}
179 	kfree(dev->private);
180 	kfree(dev->pacer);
181 	dev->private = NULL;
182 	dev->pacer = NULL;
183 	dev->driver = NULL;
184 	dev->board_name = NULL;
185 	dev->board_ptr = NULL;
186 	dev->mmio = NULL;
187 	dev->iobase = 0;
188 	dev->iolen = 0;
189 	dev->ioenabled = false;
190 	dev->irq = 0;
191 	dev->read_subdev = NULL;
192 	dev->write_subdev = NULL;
193 	dev->open = NULL;
194 	dev->close = NULL;
195 	comedi_clear_hw_dev(dev);
196 }
197 
comedi_device_detach(struct comedi_device * dev)198 void comedi_device_detach(struct comedi_device *dev)
199 {
200 	lockdep_assert_held(&dev->mutex);
201 	comedi_device_cancel_all(dev);
202 	down_write(&dev->attach_lock);
203 	dev->attached = false;
204 	dev->detach_count++;
205 	if (dev->driver)
206 		dev->driver->detach(dev);
207 	comedi_device_detach_cleanup(dev);
208 	up_write(&dev->attach_lock);
209 }
210 
poll_invalid(struct comedi_device * dev,struct comedi_subdevice * s)211 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
212 {
213 	return -EINVAL;
214 }
215 
insn_device_inval(struct comedi_device * dev,struct comedi_insn * insn,unsigned int * data)216 static int insn_device_inval(struct comedi_device *dev,
217 			     struct comedi_insn *insn, unsigned int *data)
218 {
219 	return -EINVAL;
220 }
221 
get_zero_valid_routes(struct comedi_device * dev,unsigned int n_pairs,unsigned int * pair_data)222 static 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 
insn_inval(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)229 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
230 	       struct comedi_insn *insn, unsigned int *data)
231 {
232 	return -EINVAL;
233 }
234 
235 /**
236  * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback.
237  * @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.
251  */
comedi_readback_insn_read(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)252 int 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 }
268 EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
269 
270 /**
271  * 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.
286  */
comedi_timeout(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,int (* cb)(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned long context),unsigned long context)287 int 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 }
307 EXPORT_SYMBOL_GPL(comedi_timeout);
308 
309 /**
310  * 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.
334  */
comedi_dio_insn_config(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data,unsigned int mask)335 int 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 = CR_CHAN(insn->chanspec);
342 
343 	if (!mask && chan < 32)
344 		mask = 1U << chan;
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 }
365 EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
366 
367 /**
368  * 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.
380  */
comedi_dio_update_state(struct comedi_subdevice * s,unsigned int * data)381 unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
382 				     unsigned int *data)
383 {
384 	unsigned int chanmask = (s->n_chan < 32) ? ((1U << 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 }
396 EXPORT_SYMBOL_GPL(comedi_dio_update_state);
397 
398 /**
399  * comedi_bytes_per_scan_cmd() - Get length of asynchronous command "scan" in
400  * bytes
401  * @s: COMEDI subdevice.
402  * @cmd: COMEDI command.
403  *
404  * Determines the overall scan length according to the subdevice type and the
405  * number of channels in the scan for the specified command.
406  *
407  * 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.
412  *
413  * Returns the overall scan length in bytes.
414  */
comedi_bytes_per_scan_cmd(struct comedi_subdevice * s,struct comedi_cmd * cmd)415 unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s,
416 				       struct comedi_cmd *cmd)
417 {
418 	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:
425 		bits_per_sample = 8 * comedi_bytes_per_sample(s);
426 		num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample);
427 		break;
428 	default:
429 		num_samples = cmd->scan_end_arg;
430 		break;
431 	}
432 	return comedi_samples_to_bytes(s, num_samples);
433 }
434 EXPORT_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  */
comedi_bytes_per_scan(struct comedi_subdevice * s)451 unsigned 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 }
457 EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
458 
__comedi_nscans_left(struct comedi_subdevice * s,unsigned int nscans)459 static 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 
477 /**
478  * 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.
481  *
482  * If @nscans is 0, it is set to the number of scans available in the
483  * async buffer.
484  *
485  * 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.
487  *
488  * The return value will then be either the expected number of scans or the
489  * number of scans remaining to complete the command, whichever is fewer.
490  */
comedi_nscans_left(struct comedi_subdevice * s,unsigned int nscans)491 unsigned int comedi_nscans_left(struct comedi_subdevice *s,
492 				unsigned int nscans)
493 {
494 	if (nscans == 0) {
495 		unsigned int nbytes = comedi_buf_read_n_available(s);
496 
497 		nscans = nbytes / comedi_bytes_per_scan(s);
498 	}
499 	return __comedi_nscans_left(s, nscans);
500 }
501 EXPORT_SYMBOL_GPL(comedi_nscans_left);
502 
503 /**
504  * comedi_nsamples_left() - Return the number of samples left in the command
505  * @s: COMEDI subdevice.
506  * @nsamples: The expected number of samples.
507  *
508  * Returns the number of samples remaining to complete the command, or the
509  * specified expected number of samples (@nsamples), whichever is fewer.
510  */
comedi_nsamples_left(struct comedi_subdevice * s,unsigned int nsamples)511 unsigned 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;
516 	unsigned long long scans_left;
517 	unsigned long long samples_left;
518 
519 	if (cmd->stop_src != TRIG_COUNT)
520 		return nsamples;
521 
522 	scans_left = __comedi_nscans_left(s, cmd->stop_arg);
523 	if (!scans_left)
524 		return 0;
525 
526 	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;
531 	return nsamples;
532 }
533 EXPORT_SYMBOL_GPL(comedi_nsamples_left);
534 
535 /**
536  * 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.
539  *
540  * Increments the scan progress by the number of bytes specified by @num_bytes.
541  * 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
543  * event flag (%COMEDI_CB_EOS) to be processed later.
544  */
comedi_inc_scan_progress(struct comedi_subdevice * s,unsigned int num_bytes)545 void comedi_inc_scan_progress(struct comedi_subdevice *s,
546 			      unsigned int num_bytes)
547 {
548 	struct comedi_async *async = s->async;
549 	struct comedi_cmd *cmd = &async->cmd;
550 	unsigned int scan_length = comedi_bytes_per_scan(s);
551 
552 	/* 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 
558 	async->scan_progress += num_bytes;
559 	if (async->scan_progress >= scan_length) {
560 		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 
567 		async->scan_progress %= scan_length;
568 		async->events |= COMEDI_CB_EOS;
569 	}
570 }
571 EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
572 
573 /**
574  * comedi_handle_events() - Handle events and possibly stop acquisition
575  * @dev: COMEDI device.
576  * @s: COMEDI subdevice.
577  *
578  * Handles outstanding asynchronous acquisition event flags associated
579  * with the subdevice.  Call the subdevice's @s->cancel() handler if the
580  * "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  */
comedi_handle_events(struct comedi_device * dev,struct comedi_subdevice * s)589 unsigned 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 
597 	if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel)
598 		s->cancel(dev, s);
599 
600 	comedi_event(dev, s);
601 
602 	return events;
603 }
604 EXPORT_SYMBOL_GPL(comedi_handle_events);
605 
insn_rw_emulate_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)606 static int insn_rw_emulate_bits(struct comedi_device *dev,
607 				struct comedi_subdevice *s,
608 				struct comedi_insn *insn,
609 				unsigned int *data)
610 {
611 	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];
615 	int ret;
616 
617 	if (insn->n == 0)
618 		return 0;
619 
620 	memset(_data, 0, sizeof(_data));
621 	memset(&_insn, 0, sizeof(_insn));
622 	_insn.insn = INSN_BITS;
623 	_insn.chanspec = base_chan;
624 	_insn.n = 2;
625 	_insn.subdev = insn->subdev;
626 
627 	if (insn->insn == INSN_WRITE) {
628 		if (!(s->subdev_flags & SDF_WRITABLE))
629 			return -EINVAL;
630 		_data[0] = 1U << (chan - base_chan);		     /* mask */
631 		_data[1] = data[0] ? (1U << (chan - base_chan)) : 0; /* bits */
632 	}
633 
634 	ret = s->insn_bits(dev, s, &_insn, _data);
635 	if (ret < 0)
636 		return ret;
637 
638 	if (insn->insn == INSN_READ)
639 		data[0] = (_data[1] >> (chan - base_chan)) & 1;
640 
641 	return 1;
642 }
643 
__comedi_device_postconfig_async(struct comedi_device * dev,struct comedi_subdevice * s)644 static int __comedi_device_postconfig_async(struct comedi_device *dev,
645 					    struct comedi_subdevice *s)
646 {
647 	struct comedi_async *async;
648 	unsigned int buf_size;
649 	int ret;
650 
651 	lockdep_assert_held(&dev->mutex);
652 	if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
653 		dev_warn(dev->class_dev,
654 			 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
655 		return -EINVAL;
656 	}
657 	if (!s->do_cmdtest) {
658 		dev_warn(dev->class_dev,
659 			 "async subdevices must have a do_cmdtest() function\n");
660 		return -EINVAL;
661 	}
662 	if (!s->cancel)
663 		dev_warn(dev->class_dev,
664 			 "async subdevices should have a cancel() function\n");
665 
666 	async = kzalloc(sizeof(*async), GFP_KERNEL);
667 	if (!async)
668 		return -ENOMEM;
669 
670 	init_waitqueue_head(&async->wait_head);
671 	s->async = async;
672 
673 	async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
674 	buf_size = comedi_default_buf_size_kb * 1024;
675 	if (buf_size > async->max_bufsize)
676 		buf_size = async->max_bufsize;
677 
678 	if (comedi_buf_alloc(dev, s, buf_size) < 0) {
679 		dev_warn(dev->class_dev, "Buffer allocation failed\n");
680 		return -ENOMEM;
681 	}
682 	if (s->buf_change) {
683 		ret = s->buf_change(dev, s);
684 		if (ret < 0)
685 			return ret;
686 	}
687 
688 	comedi_alloc_subdevice_minor(s);
689 
690 	return 0;
691 }
692 
__comedi_device_postconfig(struct comedi_device * dev)693 static int __comedi_device_postconfig(struct comedi_device *dev)
694 {
695 	struct comedi_subdevice *s;
696 	int ret;
697 	int i;
698 
699 	lockdep_assert_held(&dev->mutex);
700 	if (!dev->insn_device_config)
701 		dev->insn_device_config = insn_device_inval;
702 
703 	if (!dev->get_valid_routes)
704 		dev->get_valid_routes = get_zero_valid_routes;
705 
706 	for (i = 0; i < dev->n_subdevices; i++) {
707 		s = &dev->subdevices[i];
708 
709 		if (s->type == COMEDI_SUBD_UNUSED)
710 			continue;
711 
712 		if (s->type == COMEDI_SUBD_DO) {
713 			if (s->n_chan < 32)
714 				s->io_bits = (1U << s->n_chan) - 1;
715 			else
716 				s->io_bits = 0xffffffff;
717 		}
718 
719 		if (s->len_chanlist == 0)
720 			s->len_chanlist = 1;
721 
722 		if (s->do_cmd) {
723 			ret = __comedi_device_postconfig_async(dev, s);
724 			if (ret)
725 				return ret;
726 		}
727 
728 		if (!s->range_table && !s->range_table_list)
729 			s->range_table = &range_unknown;
730 
731 		if (!s->insn_read && s->insn_bits)
732 			s->insn_read = insn_rw_emulate_bits;
733 		if (!s->insn_write && s->insn_bits)
734 			s->insn_write = insn_rw_emulate_bits;
735 
736 		if (!s->insn_read)
737 			s->insn_read = insn_inval;
738 		if (!s->insn_write)
739 			s->insn_write = insn_inval;
740 		if (!s->insn_bits)
741 			s->insn_bits = insn_inval;
742 		if (!s->insn_config)
743 			s->insn_config = insn_inval;
744 
745 		if (!s->poll)
746 			s->poll = poll_invalid;
747 	}
748 
749 	return 0;
750 }
751 
752 /* do a little post-config cleanup */
comedi_device_postconfig(struct comedi_device * dev)753 static int comedi_device_postconfig(struct comedi_device *dev)
754 {
755 	int ret;
756 
757 	lockdep_assert_held(&dev->mutex);
758 	ret = __comedi_device_postconfig(dev);
759 	if (ret < 0)
760 		return ret;
761 	down_write(&dev->attach_lock);
762 	dev->attached = true;
763 	up_write(&dev->attach_lock);
764 	return 0;
765 }
766 
767 /*
768  * Generic recognize function for drivers that register their supported
769  * board names.
770  *
771  * 'driv->board_name' points to a 'const char *' member within the
772  * zeroth element of an array of some private board information
773  * structure, say 'struct foo_board' containing a member 'const char
774  * *board_name' that is initialized to point to a board name string that
775  * is one of the candidates matched against this function's 'name'
776  * parameter.
777  *
778  * 'driv->offset' is the size of the private board information
779  * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
780  * the length of the array of private board information structures.
781  *
782  * If one of the board names in the array of private board information
783  * structures matches the name supplied to this function, the function
784  * returns a pointer to the pointer to the board name, otherwise it
785  * returns NULL.  The return value ends up in the 'board_ptr' member of
786  * a 'struct comedi_device' that the low-level comedi driver's
787  * 'attach()' hook can convert to a point to a particular element of its
788  * array of private board information structures by subtracting the
789  * offset of the member that points to the board name.  (No subtraction
790  * is required if the board name pointer is the first member of the
791  * private board information structure, which is generally the case.)
792  */
comedi_recognize(struct comedi_driver * driv,const char * name)793 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
794 {
795 	char **name_ptr = (char **)driv->board_name;
796 	int i;
797 
798 	for (i = 0; i < driv->num_names; i++) {
799 		if (strcmp(*name_ptr, name) == 0)
800 			return name_ptr;
801 		name_ptr = (void *)name_ptr + driv->offset;
802 	}
803 
804 	return NULL;
805 }
806 
comedi_report_boards(struct comedi_driver * driv)807 static void comedi_report_boards(struct comedi_driver *driv)
808 {
809 	unsigned int i;
810 	const char *const *name_ptr;
811 
812 	pr_info("comedi: valid board names for %s driver are:\n",
813 		driv->driver_name);
814 
815 	name_ptr = driv->board_name;
816 	for (i = 0; i < driv->num_names; i++) {
817 		pr_info(" %s\n", *name_ptr);
818 		name_ptr = (const char **)((char *)name_ptr + driv->offset);
819 	}
820 
821 	if (driv->num_names == 0)
822 		pr_info(" %s\n", driv->driver_name);
823 }
824 
825 /**
826  * comedi_load_firmware() - Request and load firmware for a device
827  * @dev: COMEDI device.
828  * @device: Hardware device.
829  * @name: The name of the firmware image.
830  * @cb: Callback to the upload the firmware image.
831  * @context: Private context from the driver.
832  *
833  * Sends a firmware request for the hardware device and waits for it.  Calls
834  * the callback function to upload the firmware to the device, them releases
835  * the firmware.
836  *
837  * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number
838  * from the firmware request or the callback function.
839  */
comedi_load_firmware(struct comedi_device * dev,struct device * device,const char * name,int (* cb)(struct comedi_device * dev,const u8 * data,size_t size,unsigned long context),unsigned long context)840 int comedi_load_firmware(struct comedi_device *dev,
841 			 struct device *device,
842 			 const char *name,
843 			 int (*cb)(struct comedi_device *dev,
844 				   const u8 *data, size_t size,
845 				   unsigned long context),
846 			 unsigned long context)
847 {
848 	const struct firmware *fw;
849 	int ret;
850 
851 	if (!cb)
852 		return -EINVAL;
853 
854 	ret = request_firmware(&fw, name, device);
855 	if (ret == 0) {
856 		ret = cb(dev, fw->data, fw->size, context);
857 		release_firmware(fw);
858 	}
859 
860 	return min(ret, 0);
861 }
862 EXPORT_SYMBOL_GPL(comedi_load_firmware);
863 
864 /**
865  * __comedi_request_region() - Request an I/O region for a legacy driver
866  * @dev: COMEDI device.
867  * @start: Base address of the I/O region.
868  * @len: Length of the I/O region.
869  *
870  * Requests the specified I/O port region which must start at a non-zero
871  * address.
872  *
873  * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
874  * fails.
875  */
__comedi_request_region(struct comedi_device * dev,unsigned long start,unsigned long len)876 int __comedi_request_region(struct comedi_device *dev,
877 			    unsigned long start, unsigned long len)
878 {
879 	if (!start) {
880 		dev_warn(dev->class_dev,
881 			 "%s: a I/O base address must be specified\n",
882 			 dev->board_name);
883 		return -EINVAL;
884 	}
885 
886 	if (!request_region(start, len, dev->board_name)) {
887 		dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
888 			 dev->board_name, start, len);
889 		return -EIO;
890 	}
891 
892 	return 0;
893 }
894 EXPORT_SYMBOL_GPL(__comedi_request_region);
895 
896 /**
897  * comedi_request_region() - Request an I/O region for a legacy driver
898  * @dev: COMEDI device.
899  * @start: Base address of the I/O region.
900  * @len: Length of the I/O region.
901  *
902  * Requests the specified I/O port region which must start at a non-zero
903  * address.
904  *
905  * On success, @dev->iobase is set to the base address of the region and
906  * @dev->iolen is set to its length.
907  *
908  * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
909  * fails.
910  */
comedi_request_region(struct comedi_device * dev,unsigned long start,unsigned long len)911 int comedi_request_region(struct comedi_device *dev,
912 			  unsigned long start, unsigned long len)
913 {
914 	int ret;
915 
916 	ret = __comedi_request_region(dev, start, len);
917 	if (ret == 0) {
918 		dev->iobase = start;
919 		dev->iolen = len;
920 	}
921 
922 	return ret;
923 }
924 EXPORT_SYMBOL_GPL(comedi_request_region);
925 
926 /**
927  * comedi_legacy_detach() - A generic (*detach) function for legacy drivers
928  * @dev: COMEDI device.
929  *
930  * This is a simple, generic 'detach' handler for legacy COMEDI devices that
931  * just use a single I/O port region and possibly an IRQ and that don't need
932  * any special clean-up for their private device or subdevice storage.  It
933  * can also be called by a driver-specific 'detach' handler.
934  *
935  * If @dev->irq is non-zero, the IRQ will be freed.  If @dev->iobase and
936  * @dev->iolen are both non-zero, the I/O port region will be released.
937  */
comedi_legacy_detach(struct comedi_device * dev)938 void comedi_legacy_detach(struct comedi_device *dev)
939 {
940 	if (dev->irq) {
941 		free_irq(dev->irq, dev);
942 		dev->irq = 0;
943 	}
944 	if (dev->iobase && dev->iolen) {
945 		release_region(dev->iobase, dev->iolen);
946 		dev->iobase = 0;
947 		dev->iolen = 0;
948 	}
949 }
950 EXPORT_SYMBOL_GPL(comedi_legacy_detach);
951 
comedi_device_attach(struct comedi_device * dev,struct comedi_devconfig * it)952 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
953 {
954 	struct comedi_driver *driv;
955 	int ret;
956 
957 	lockdep_assert_held(&dev->mutex);
958 	if (dev->attached)
959 		return -EBUSY;
960 
961 	mutex_lock(&comedi_drivers_list_lock);
962 	for (driv = comedi_drivers; driv; driv = driv->next) {
963 		if (!try_module_get(driv->module))
964 			continue;
965 		if (driv->num_names) {
966 			dev->board_ptr = comedi_recognize(driv, it->board_name);
967 			if (dev->board_ptr)
968 				break;
969 		} else if (strcmp(driv->driver_name, it->board_name) == 0) {
970 			break;
971 		}
972 		module_put(driv->module);
973 	}
974 	if (!driv) {
975 		/*  recognize has failed if we get here */
976 		/*  report valid board names before returning error */
977 		for (driv = comedi_drivers; driv; driv = driv->next) {
978 			if (!try_module_get(driv->module))
979 				continue;
980 			comedi_report_boards(driv);
981 			module_put(driv->module);
982 		}
983 		ret = -EIO;
984 		goto out;
985 	}
986 	if (!driv->attach) {
987 		/* driver does not support manual configuration */
988 		dev_warn(dev->class_dev,
989 			 "driver '%s' does not support attach using comedi_config\n",
990 			 driv->driver_name);
991 		module_put(driv->module);
992 		ret = -EIO;
993 		goto out;
994 	}
995 	dev->driver = driv;
996 	dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
997 					 : dev->driver->driver_name;
998 	ret = driv->attach(dev, it);
999 	if (ret >= 0)
1000 		ret = comedi_device_postconfig(dev);
1001 	if (ret < 0) {
1002 		comedi_device_detach(dev);
1003 		module_put(driv->module);
1004 	}
1005 	/* On success, the driver module count has been incremented. */
1006 out:
1007 	mutex_unlock(&comedi_drivers_list_lock);
1008 	return ret;
1009 }
1010 
1011 /**
1012  * comedi_auto_config() - Create a COMEDI device for a hardware device
1013  * @hardware_device: Hardware device.
1014  * @driver: COMEDI low-level driver for the hardware device.
1015  * @context: Driver context for the auto_attach handler.
1016  *
1017  * Allocates a new COMEDI device for the hardware device and calls the
1018  * low-level driver's 'auto_attach' handler to set-up the hardware and
1019  * allocate the COMEDI subdevices.  Additional "post-configuration" setting
1020  * up is performed on successful return from the 'auto_attach' handler.
1021  * If the 'auto_attach' handler fails, the low-level driver's 'detach'
1022  * handler will be called as part of the clean-up.
1023  *
1024  * This is usually called from a wrapper function in a bus-specific COMEDI
1025  * module, which in turn is usually called from a bus device 'probe'
1026  * function in the low-level driver.
1027  *
1028  * Returns 0 on success, -EINVAL if the parameters are invalid or the
1029  * post-configuration determines the driver has set the COMEDI device up
1030  * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of
1031  * COMEDI minor device numbers, or some negative error number returned by
1032  * the driver's 'auto_attach' handler.
1033  */
comedi_auto_config(struct device * hardware_device,struct comedi_driver * driver,unsigned long context)1034 int comedi_auto_config(struct device *hardware_device,
1035 		       struct comedi_driver *driver, unsigned long context)
1036 {
1037 	struct comedi_device *dev;
1038 	int ret;
1039 
1040 	if (!hardware_device) {
1041 		pr_warn("BUG! %s called with NULL hardware_device\n", __func__);
1042 		return -EINVAL;
1043 	}
1044 	if (!driver) {
1045 		dev_warn(hardware_device,
1046 			 "BUG! %s called with NULL comedi driver\n", __func__);
1047 		return -EINVAL;
1048 	}
1049 
1050 	if (!driver->auto_attach) {
1051 		dev_warn(hardware_device,
1052 			 "BUG! comedi driver '%s' has no auto_attach handler\n",
1053 			 driver->driver_name);
1054 		return -EINVAL;
1055 	}
1056 
1057 	dev = comedi_alloc_board_minor(hardware_device);
1058 	if (IS_ERR(dev)) {
1059 		dev_warn(hardware_device,
1060 			 "driver '%s' could not create device.\n",
1061 			 driver->driver_name);
1062 		return PTR_ERR(dev);
1063 	}
1064 	/* Note: comedi_alloc_board_minor() locked dev->mutex. */
1065 	lockdep_assert_held(&dev->mutex);
1066 
1067 	dev->driver = driver;
1068 	dev->board_name = dev->driver->driver_name;
1069 	ret = driver->auto_attach(dev, context);
1070 	if (ret >= 0)
1071 		ret = comedi_device_postconfig(dev);
1072 
1073 	if (ret < 0) {
1074 		dev_warn(hardware_device,
1075 			 "driver '%s' failed to auto-configure device.\n",
1076 			 driver->driver_name);
1077 		mutex_unlock(&dev->mutex);
1078 		comedi_release_hardware_device(hardware_device);
1079 	} else {
1080 		/*
1081 		 * class_dev should be set properly here
1082 		 *  after a successful auto config
1083 		 */
1084 		dev_info(dev->class_dev,
1085 			 "driver '%s' has successfully auto-configured '%s'.\n",
1086 			 driver->driver_name, dev->board_name);
1087 		mutex_unlock(&dev->mutex);
1088 	}
1089 	return ret;
1090 }
1091 EXPORT_SYMBOL_GPL(comedi_auto_config);
1092 
1093 /**
1094  * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device
1095  * @hardware_device: Hardware device previously passed to
1096  *                   comedi_auto_config().
1097  *
1098  * Cleans up and eventually destroys the COMEDI device allocated by
1099  * comedi_auto_config() for the same hardware device.  As part of this
1100  * clean-up, the low-level COMEDI driver's 'detach' handler will be called.
1101  * (The COMEDI device itself will persist in an unattached state if it is
1102  * still open, until it is released, and any mmapped buffers will persist
1103  * until they are munmapped.)
1104  *
1105  * This is usually called from a wrapper module in a bus-specific COMEDI
1106  * module, which in turn is usually set as the bus device 'remove' function
1107  * in the low-level COMEDI driver.
1108  */
comedi_auto_unconfig(struct device * hardware_device)1109 void comedi_auto_unconfig(struct device *hardware_device)
1110 {
1111 	if (!hardware_device)
1112 		return;
1113 	comedi_release_hardware_device(hardware_device);
1114 }
1115 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
1116 
1117 /**
1118  * comedi_driver_register() - Register a low-level COMEDI driver
1119  * @driver: Low-level COMEDI driver.
1120  *
1121  * The low-level COMEDI driver is added to the list of registered COMEDI
1122  * drivers.  This is used by the handler for the "/proc/comedi" file and is
1123  * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure
1124  * "legacy" COMEDI devices (for those low-level drivers that support it).
1125  *
1126  * Returns 0.
1127  */
comedi_driver_register(struct comedi_driver * driver)1128 int comedi_driver_register(struct comedi_driver *driver)
1129 {
1130 	mutex_lock(&comedi_drivers_list_lock);
1131 	driver->next = comedi_drivers;
1132 	comedi_drivers = driver;
1133 	mutex_unlock(&comedi_drivers_list_lock);
1134 
1135 	return 0;
1136 }
1137 EXPORT_SYMBOL_GPL(comedi_driver_register);
1138 
1139 /**
1140  * comedi_driver_unregister() - Unregister a low-level COMEDI driver
1141  * @driver: Low-level COMEDI driver.
1142  *
1143  * The low-level COMEDI driver is removed from the list of registered COMEDI
1144  * drivers.  Detaches any COMEDI devices attached to the driver, which will
1145  * result in the low-level driver's 'detach' handler being called for those
1146  * devices before this function returns.
1147  */
comedi_driver_unregister(struct comedi_driver * driver)1148 void comedi_driver_unregister(struct comedi_driver *driver)
1149 {
1150 	struct comedi_driver *prev;
1151 	int i;
1152 
1153 	/* unlink the driver */
1154 	mutex_lock(&comedi_drivers_list_lock);
1155 	if (comedi_drivers == driver) {
1156 		comedi_drivers = driver->next;
1157 	} else {
1158 		for (prev = comedi_drivers; prev->next; prev = prev->next) {
1159 			if (prev->next == driver) {
1160 				prev->next = driver->next;
1161 				break;
1162 			}
1163 		}
1164 	}
1165 	mutex_unlock(&comedi_drivers_list_lock);
1166 
1167 	/* check for devices using this driver */
1168 	for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
1169 		struct comedi_device *dev = comedi_dev_get_from_minor(i);
1170 
1171 		if (!dev)
1172 			continue;
1173 
1174 		mutex_lock(&dev->mutex);
1175 		if (dev->attached && dev->driver == driver) {
1176 			if (dev->use_count)
1177 				dev_warn(dev->class_dev,
1178 					 "BUG! detaching device with use_count=%d\n",
1179 					 dev->use_count);
1180 			comedi_device_detach(dev);
1181 		}
1182 		mutex_unlock(&dev->mutex);
1183 		comedi_dev_put(dev);
1184 	}
1185 }
1186 EXPORT_SYMBOL_GPL(comedi_driver_unregister);
1187