Greg Kroah-Hartman | e184e2b | 2017-11-07 14:58:43 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2 | /* |
Marcos CanĂ¡n | 50fbb88 | 2015-09-16 17:48:57 -0300 | [diff] [blame] | 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> |
Ramiro Oliveira | 37a7029 | 2016-09-30 11:32:05 +0100 | [diff] [blame] | 9 | */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 10 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 11 | #include <linux/device.h> |
| 12 | #include <linux/module.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/kernel.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 15 | #include <linux/ioport.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 16 | #include <linux/slab.h> |
Ian Abbott | d35d893 | 2015-09-17 17:19:16 +0100 | [diff] [blame] | 17 | #include <linux/dma-direction.h> |
H Hartley Sweeten | 3d1fe3f | 2013-04-18 14:34:37 -0700 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
H Hartley Sweeten | 9ff8b15 | 2013-05-17 11:17:00 -0700 | [diff] [blame] | 19 | #include <linux/firmware.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 20 | |
Greg Kroah-Hartman | 242e7ad | 2010-05-03 15:20:29 -0700 | [diff] [blame] | 21 | #include "comedidev.h" |
Ian Abbott | 3a5fa27 | 2012-06-19 10:17:44 +0100 | [diff] [blame] | 22 | #include "comedi_internal.h" |
Greg Kroah-Hartman | 242e7ad | 2010-05-03 15:20:29 -0700 | [diff] [blame] | 23 | |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 24 | struct comedi_driver *comedi_drivers; |
H Hartley Sweeten | 3df9f21 | 2014-07-18 14:28:11 -0700 | [diff] [blame] | 25 | /* protects access to comedi_drivers */ |
Ian Abbott | c383e2d | 2013-06-27 14:50:58 +0100 | [diff] [blame] | 26 | DEFINE_MUTEX(comedi_drivers_list_lock); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 27 | |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 28 | /** |
| 29 | * comedi_set_hw_dev() - Set hardware device associated with COMEDI device |
| 30 | * @dev: COMEDI device. |
| 31 | * @hw_dev: Hardware device. |
| 32 | * |
| 33 | * For automatically configured COMEDI devices (resulting from a call to |
| 34 | * comedi_auto_config() or one of its wrappers from the low-level COMEDI |
| 35 | * driver), comedi_set_hw_dev() is called automatically by the COMEDI core |
| 36 | * to associate the COMEDI device with the hardware device. It can also be |
| 37 | * called directly by "legacy" low-level COMEDI drivers that rely on the |
| 38 | * %COMEDI_DEVCONFIG ioctl to configure the hardware as long as the hardware |
| 39 | * has a &struct device. |
| 40 | * |
| 41 | * If @dev->hw_dev is NULL, it gets a reference to @hw_dev and sets |
| 42 | * @dev->hw_dev, otherwise, it does nothing. Calling it multiple times |
| 43 | * with the same hardware device is not considered an error. If it gets |
| 44 | * a reference to the hardware device, it will be automatically 'put' when |
| 45 | * the device is detached from COMEDI. |
| 46 | * |
| 47 | * Returns 0 if @dev->hw_dev was NULL or the same as @hw_dev, otherwise |
| 48 | * returns -EEXIST. |
| 49 | */ |
Ian Abbott | da71751 | 2013-02-01 13:23:19 +0000 | [diff] [blame] | 50 | int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev) |
| 51 | { |
Ian Abbott | de06d7c | 2013-02-01 13:23:20 +0000 | [diff] [blame] | 52 | if (hw_dev == dev->hw_dev) |
| 53 | return 0; |
H Hartley Sweeten | e285016 | 2015-03-04 12:15:29 -0700 | [diff] [blame] | 54 | if (dev->hw_dev) |
Ian Abbott | de06d7c | 2013-02-01 13:23:20 +0000 | [diff] [blame] | 55 | return -EEXIST; |
Ian Abbott | da71751 | 2013-02-01 13:23:19 +0000 | [diff] [blame] | 56 | dev->hw_dev = get_device(hw_dev); |
Ian Abbott | da71751 | 2013-02-01 13:23:19 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | EXPORT_SYMBOL_GPL(comedi_set_hw_dev); |
| 60 | |
Ian Abbott | de06d7c | 2013-02-01 13:23:20 +0000 | [diff] [blame] | 61 | static void comedi_clear_hw_dev(struct comedi_device *dev) |
| 62 | { |
| 63 | put_device(dev->hw_dev); |
| 64 | dev->hw_dev = NULL; |
| 65 | } |
| 66 | |
H Hartley Sweeten | 54db996 | 2013-06-24 16:55:14 -0700 | [diff] [blame] | 67 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 68 | * comedi_alloc_devpriv() - Allocate memory for the device private data |
| 69 | * @dev: COMEDI device. |
| 70 | * @size: Size of the memory to allocate. |
| 71 | * |
| 72 | * The allocated memory is zero-filled. @dev->private points to it on |
| 73 | * return. The memory will be automatically freed when the COMEDI device is |
| 74 | * "detached". |
| 75 | * |
| 76 | * Returns a pointer to the allocated memory, or NULL on failure. |
H Hartley Sweeten | 54db996 | 2013-06-24 16:55:14 -0700 | [diff] [blame] | 77 | */ |
| 78 | void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size) |
| 79 | { |
| 80 | dev->private = kzalloc(size, GFP_KERNEL); |
| 81 | return dev->private; |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(comedi_alloc_devpriv); |
| 84 | |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 85 | /** |
| 86 | * comedi_alloc_subdevices() - Allocate subdevices for COMEDI device |
| 87 | * @dev: COMEDI device. |
| 88 | * @num_subdevices: Number of subdevices to allocate. |
| 89 | * |
| 90 | * Allocates and initializes an array of &struct comedi_subdevice for the |
| 91 | * COMEDI device. If successful, sets @dev->subdevices to point to the |
| 92 | * first one and @dev->n_subdevices to the number. |
| 93 | * |
| 94 | * Returns 0 on success, -EINVAL if @num_subdevices is < 1, or -ENOMEM if |
| 95 | * failed to allocate the memory. |
| 96 | */ |
H Hartley Sweeten | 8b9ba6e | 2012-06-12 11:57:27 -0700 | [diff] [blame] | 97 | int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices) |
H Hartley Sweeten | 2f0b9d0 | 2012-06-11 17:45:15 -0700 | [diff] [blame] | 98 | { |
H Hartley Sweeten | 03afcf4 | 2012-06-12 11:59:55 -0700 | [diff] [blame] | 99 | struct comedi_subdevice *s; |
H Hartley Sweeten | 8b9ba6e | 2012-06-12 11:57:27 -0700 | [diff] [blame] | 100 | int i; |
H Hartley Sweeten | 2f0b9d0 | 2012-06-11 17:45:15 -0700 | [diff] [blame] | 101 | |
H Hartley Sweeten | 7f801c4 | 2012-06-12 11:57:45 -0700 | [diff] [blame] | 102 | if (num_subdevices < 1) |
| 103 | return -EINVAL; |
H Hartley Sweeten | 03afcf4 | 2012-06-12 11:59:55 -0700 | [diff] [blame] | 104 | |
| 105 | s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL); |
| 106 | if (!s) |
H Hartley Sweeten | 2f0b9d0 | 2012-06-11 17:45:15 -0700 | [diff] [blame] | 107 | return -ENOMEM; |
H Hartley Sweeten | 03afcf4 | 2012-06-12 11:59:55 -0700 | [diff] [blame] | 108 | dev->subdevices = s; |
H Hartley Sweeten | fba1d0f | 2012-06-12 11:58:27 -0700 | [diff] [blame] | 109 | dev->n_subdevices = num_subdevices; |
H Hartley Sweeten | 03afcf4 | 2012-06-12 11:59:55 -0700 | [diff] [blame] | 110 | |
H Hartley Sweeten | 2f0b9d0 | 2012-06-11 17:45:15 -0700 | [diff] [blame] | 111 | for (i = 0; i < num_subdevices; ++i) { |
H Hartley Sweeten | 5e4c58c | 2012-09-05 18:21:25 -0700 | [diff] [blame] | 112 | s = &dev->subdevices[i]; |
H Hartley Sweeten | 03afcf4 | 2012-06-12 11:59:55 -0700 | [diff] [blame] | 113 | s->device = dev; |
H Hartley Sweeten | 90a35c1 | 2012-12-19 17:27:02 -0700 | [diff] [blame] | 114 | s->index = i; |
H Hartley Sweeten | 03afcf4 | 2012-06-12 11:59:55 -0700 | [diff] [blame] | 115 | s->async_dma_dir = DMA_NONE; |
| 116 | spin_lock_init(&s->spin_lock); |
| 117 | s->minor = -1; |
H Hartley Sweeten | 2f0b9d0 | 2012-06-11 17:45:15 -0700 | [diff] [blame] | 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(comedi_alloc_subdevices); |
| 122 | |
H Hartley Sweeten | d276206 | 2014-08-25 16:03:54 -0700 | [diff] [blame] | 123 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 124 | * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback |
| 125 | * @s: COMEDI subdevice. |
| 126 | * |
| 127 | * This is called by low-level COMEDI drivers to allocate an array to record |
| 128 | * the last values written to a subdevice's analog output channels (at least |
| 129 | * by the %INSN_WRITE instruction), to allow them to be read back by an |
| 130 | * %INSN_READ instruction. It also provides a default handler for the |
| 131 | * %INSN_READ instruction unless one has already been set. |
| 132 | * |
| 133 | * On success, @s->readback points to the first element of the array, which |
| 134 | * is zero-filled. The low-level driver is responsible for updating its |
| 135 | * contents. @s->insn_read will be set to comedi_readback_insn_read() |
| 136 | * unless it is already non-NULL. |
| 137 | * |
| 138 | * Returns 0 on success, -EINVAL if the subdevice has no channels, or |
| 139 | * -ENOMEM on allocation failure. |
H Hartley Sweeten | d276206 | 2014-08-25 16:03:54 -0700 | [diff] [blame] | 140 | */ |
| 141 | int comedi_alloc_subdev_readback(struct comedi_subdevice *s) |
| 142 | { |
| 143 | if (!s->n_chan) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL); |
| 147 | if (!s->readback) |
| 148 | return -ENOMEM; |
H Hartley Sweeten | aa11672 | 2014-11-21 10:19:10 -0700 | [diff] [blame] | 149 | |
| 150 | if (!s->insn_read) |
| 151 | s->insn_read = comedi_readback_insn_read; |
| 152 | |
H Hartley Sweeten | d276206 | 2014-08-25 16:03:54 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback); |
| 156 | |
Ian Abbott | 3867e20 | 2013-11-08 15:03:26 +0000 | [diff] [blame] | 157 | static void comedi_device_detach_cleanup(struct comedi_device *dev) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 158 | { |
| 159 | int i; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 160 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 161 | |
Ian Abbott | f44303e | 2019-04-17 15:39:30 +0100 | [diff] [blame] | 162 | lockdep_assert_held(&dev->attach_lock); |
Ian Abbott | 77c21b6 | 2019-04-17 15:39:29 +0100 | [diff] [blame] | 163 | lockdep_assert_held(&dev->mutex); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 164 | if (dev->subdevices) { |
| 165 | for (i = 0; i < dev->n_subdevices; i++) { |
H Hartley Sweeten | 5e4c58c | 2012-09-05 18:21:25 -0700 | [diff] [blame] | 166 | s = &dev->subdevices[i]; |
Ian Abbott | 8fc369a | 2015-04-21 13:18:10 +0100 | [diff] [blame] | 167 | if (comedi_can_auto_free_spriv(s)) |
H Hartley Sweeten | 588ba6d | 2013-06-11 11:32:29 -0700 | [diff] [blame] | 168 | kfree(s->private); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 169 | comedi_free_subdevice_minor(s); |
| 170 | if (s->async) { |
| 171 | comedi_buf_alloc(dev, s, 0); |
| 172 | kfree(s->async); |
| 173 | } |
H Hartley Sweeten | d276206 | 2014-08-25 16:03:54 -0700 | [diff] [blame] | 174 | kfree(s->readback); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 175 | } |
| 176 | kfree(dev->subdevices); |
| 177 | dev->subdevices = NULL; |
| 178 | dev->n_subdevices = 0; |
| 179 | } |
Bill Pemberton | dedd132 | 2009-03-16 22:04:18 -0400 | [diff] [blame] | 180 | kfree(dev->private); |
H Hartley Sweeten | 43db062 | 2015-02-23 14:57:29 -0700 | [diff] [blame] | 181 | kfree(dev->pacer); |
Bill Pemberton | dedd132 | 2009-03-16 22:04:18 -0400 | [diff] [blame] | 182 | dev->private = NULL; |
H Hartley Sweeten | 43db062 | 2015-02-23 14:57:29 -0700 | [diff] [blame] | 183 | dev->pacer = NULL; |
Greg Kroah-Hartman | 7029a87 | 2010-05-03 15:55:45 -0700 | [diff] [blame] | 184 | dev->driver = NULL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 185 | dev->board_name = NULL; |
| 186 | dev->board_ptr = NULL; |
H Hartley Sweeten | d7e6dc1 | 2014-07-29 15:01:20 -0700 | [diff] [blame] | 187 | dev->mmio = NULL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 188 | dev->iobase = 0; |
H Hartley Sweeten | 316f97f | 2013-04-18 14:31:29 -0700 | [diff] [blame] | 189 | dev->iolen = 0; |
Ian Abbott | 00ca6884 | 2013-03-15 13:15:35 +0000 | [diff] [blame] | 190 | dev->ioenabled = false; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 191 | dev->irq = 0; |
| 192 | dev->read_subdev = NULL; |
| 193 | dev->write_subdev = NULL; |
| 194 | dev->open = NULL; |
| 195 | dev->close = NULL; |
Ian Abbott | de06d7c | 2013-02-01 13:23:20 +0000 | [diff] [blame] | 196 | comedi_clear_hw_dev(dev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Ian Abbott | 016599f | 2013-04-04 14:58:58 +0100 | [diff] [blame] | 199 | void comedi_device_detach(struct comedi_device *dev) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 200 | { |
Ian Abbott | 77c21b6 | 2019-04-17 15:39:29 +0100 | [diff] [blame] | 201 | lockdep_assert_held(&dev->mutex); |
Ian Abbott | d19db51 | 2013-11-08 15:03:28 +0000 | [diff] [blame] | 202 | comedi_device_cancel_all(dev); |
Ian Abbott | bf11c13 | 2013-11-08 15:03:25 +0000 | [diff] [blame] | 203 | down_write(&dev->attach_lock); |
Ian Abbott | a7401cd | 2013-03-15 13:15:33 +0000 | [diff] [blame] | 204 | dev->attached = false; |
Ian Abbott | ef77c0b | 2013-11-08 15:03:29 +0000 | [diff] [blame] | 205 | dev->detach_count++; |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 206 | if (dev->driver) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 207 | dev->driver->detach(dev); |
Ian Abbott | 3867e20 | 2013-11-08 15:03:26 +0000 | [diff] [blame] | 208 | comedi_device_detach_cleanup(dev); |
Ian Abbott | bf11c13 | 2013-11-08 15:03:25 +0000 | [diff] [blame] | 209 | up_write(&dev->attach_lock); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 210 | } |
| 211 | |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 212 | static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s) |
| 213 | { |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | |
Spencer E. Olson | d7569ad | 2018-10-03 14:56:02 -0600 | [diff] [blame] | 217 | static int insn_device_inval(struct comedi_device *dev, |
| 218 | struct comedi_insn *insn, unsigned int *data) |
| 219 | { |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
| 223 | static unsigned int get_zero_valid_routes(struct comedi_device *dev, |
| 224 | unsigned int n_pairs, |
| 225 | unsigned int *pair_data) |
| 226 | { |
| 227 | return 0; |
| 228 | } |
| 229 | |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 230 | int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s, |
| 231 | struct comedi_insn *insn, unsigned int *data) |
| 232 | { |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
H Hartley Sweeten | e523c6c | 2013-08-06 09:31:35 -0700 | [diff] [blame] | 236 | /** |
H Hartley Sweeten | d276206 | 2014-08-25 16:03:54 -0700 | [diff] [blame] | 237 | * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback. |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 238 | * @dev: COMEDI device. |
| 239 | * @s: COMEDI subdevice. |
| 240 | * @insn: COMEDI instruction. |
| 241 | * @data: Pointer to return the readback data. |
| 242 | * |
| 243 | * Handles the %INSN_READ instruction for subdevices that use the readback |
| 244 | * array allocated by comedi_alloc_subdev_readback(). It may be used |
| 245 | * directly as the subdevice's handler (@s->insn_read) or called via a |
| 246 | * wrapper. |
| 247 | * |
| 248 | * @insn->n is normally 1, which will read a single value. If higher, the |
| 249 | * same element of the readback array will be read multiple times. |
| 250 | * |
| 251 | * Returns @insn->n on success, or -EINVAL if @s->readback is NULL. |
H Hartley Sweeten | d276206 | 2014-08-25 16:03:54 -0700 | [diff] [blame] | 252 | */ |
| 253 | int comedi_readback_insn_read(struct comedi_device *dev, |
| 254 | struct comedi_subdevice *s, |
| 255 | struct comedi_insn *insn, |
| 256 | unsigned int *data) |
| 257 | { |
| 258 | unsigned int chan = CR_CHAN(insn->chanspec); |
| 259 | int i; |
| 260 | |
| 261 | if (!s->readback) |
| 262 | return -EINVAL; |
| 263 | |
| 264 | for (i = 0; i < insn->n; i++) |
| 265 | data[i] = s->readback[chan]; |
| 266 | |
| 267 | return insn->n; |
| 268 | } |
| 269 | EXPORT_SYMBOL_GPL(comedi_readback_insn_read); |
| 270 | |
| 271 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 272 | * comedi_timeout() - Busy-wait for a driver condition to occur |
| 273 | * @dev: COMEDI device. |
| 274 | * @s: COMEDI subdevice. |
| 275 | * @insn: COMEDI instruction. |
| 276 | * @cb: Callback to check for the condition. |
| 277 | * @context: Private context from the driver. |
| 278 | * |
| 279 | * Busy-waits for up to a second (%COMEDI_TIMEOUT_MS) for the condition or |
| 280 | * some error (other than -EBUSY) to occur. The parameters @dev, @s, @insn, |
| 281 | * and @context are passed to the callback function, which returns -EBUSY to |
| 282 | * continue waiting or some other value to stop waiting (generally 0 if the |
| 283 | * condition occurred, or some error value). |
| 284 | * |
| 285 | * Returns -ETIMEDOUT if timed out, otherwise the return value from the |
| 286 | * callback function. |
H Hartley Sweeten | 9150640 | 2014-02-10 11:49:00 -0700 | [diff] [blame] | 287 | */ |
| 288 | int comedi_timeout(struct comedi_device *dev, |
| 289 | struct comedi_subdevice *s, |
| 290 | struct comedi_insn *insn, |
| 291 | int (*cb)(struct comedi_device *dev, |
| 292 | struct comedi_subdevice *s, |
| 293 | struct comedi_insn *insn, |
| 294 | unsigned long context), |
| 295 | unsigned long context) |
| 296 | { |
| 297 | unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS); |
| 298 | int ret; |
| 299 | |
| 300 | while (time_before(jiffies, timeout)) { |
| 301 | ret = cb(dev, s, insn, context); |
| 302 | if (ret != -EBUSY) |
| 303 | return ret; /* success (0) or non EBUSY errno */ |
| 304 | cpu_relax(); |
| 305 | } |
| 306 | return -ETIMEDOUT; |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(comedi_timeout); |
| 309 | |
| 310 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 311 | * comedi_dio_insn_config() - Boilerplate (*insn_config) for DIO subdevices |
| 312 | * @dev: COMEDI device. |
| 313 | * @s: COMEDI subdevice. |
| 314 | * @insn: COMEDI instruction. |
| 315 | * @data: Instruction parameters and return data. |
| 316 | * @mask: io_bits mask for grouped channels, or 0 for single channel. |
| 317 | * |
| 318 | * If @mask is 0, it is replaced with a single-bit mask corresponding to the |
| 319 | * channel number specified by @insn->chanspec. Otherwise, @mask |
| 320 | * corresponds to a group of channels (which should include the specified |
| 321 | * channel) that are always configured together as inputs or outputs. |
| 322 | * |
| 323 | * Partially handles the %INSN_CONFIG_DIO_INPUT, %INSN_CONFIG_DIO_OUTPUTS, |
| 324 | * and %INSN_CONFIG_DIO_QUERY instructions. The first two update |
| 325 | * @s->io_bits to record the directions of the masked channels. The last |
| 326 | * one sets @data[1] to the current direction of the group of channels |
| 327 | * (%COMEDI_INPUT) or %COMEDI_OUTPUT) as recorded in @s->io_bits. |
| 328 | * |
| 329 | * The caller is responsible for updating the DIO direction in the hardware |
| 330 | * registers if this function returns 0. |
| 331 | * |
| 332 | * Returns 0 for a %INSN_CONFIG_DIO_INPUT or %INSN_CONFIG_DIO_OUTPUT |
| 333 | * instruction, @insn->n (> 0) for a %INSN_CONFIG_DIO_QUERY instruction, or |
| 334 | * -EINVAL for some other instruction. |
H Hartley Sweeten | e523c6c | 2013-08-06 09:31:35 -0700 | [diff] [blame] | 335 | */ |
| 336 | int comedi_dio_insn_config(struct comedi_device *dev, |
| 337 | struct comedi_subdevice *s, |
| 338 | struct comedi_insn *insn, |
| 339 | unsigned int *data, |
| 340 | unsigned int mask) |
| 341 | { |
| 342 | unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec); |
| 343 | |
| 344 | if (!mask) |
| 345 | mask = chan_mask; |
| 346 | |
| 347 | switch (data[0]) { |
| 348 | case INSN_CONFIG_DIO_INPUT: |
| 349 | s->io_bits &= ~mask; |
| 350 | break; |
| 351 | |
| 352 | case INSN_CONFIG_DIO_OUTPUT: |
| 353 | s->io_bits |= mask; |
| 354 | break; |
| 355 | |
| 356 | case INSN_CONFIG_DIO_QUERY: |
| 357 | data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT; |
| 358 | return insn->n; |
| 359 | |
| 360 | default: |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | EXPORT_SYMBOL_GPL(comedi_dio_insn_config); |
| 367 | |
H Hartley Sweeten | 05e60b1 | 2013-08-30 11:04:56 -0700 | [diff] [blame] | 368 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 369 | * comedi_dio_update_state() - Update the internal state of DIO subdevices |
| 370 | * @s: COMEDI subdevice. |
| 371 | * @data: The channel mask and bits to update. |
| 372 | * |
| 373 | * Updates @s->state which holds the internal state of the outputs for DIO |
| 374 | * or DO subdevices (up to 32 channels). @data[0] contains a bit-mask of |
| 375 | * the channels to be updated. @data[1] contains a bit-mask of those |
| 376 | * channels to be set to '1'. The caller is responsible for updating the |
| 377 | * outputs in hardware according to @s->state. As a minimum, the channels |
| 378 | * in the returned bit-mask need to be updated. |
| 379 | * |
| 380 | * Returns @mask with non-existent channels removed. |
H Hartley Sweeten | 05e60b1 | 2013-08-30 11:04:56 -0700 | [diff] [blame] | 381 | */ |
| 382 | unsigned int comedi_dio_update_state(struct comedi_subdevice *s, |
| 383 | unsigned int *data) |
| 384 | { |
| 385 | unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1) |
| 386 | : 0xffffffff; |
| 387 | unsigned int mask = data[0] & chanmask; |
| 388 | unsigned int bits = data[1]; |
| 389 | |
| 390 | if (mask) { |
| 391 | s->state &= ~mask; |
| 392 | s->state |= (bits & mask); |
| 393 | } |
| 394 | |
| 395 | return mask; |
| 396 | } |
| 397 | EXPORT_SYMBOL_GPL(comedi_dio_update_state); |
| 398 | |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 399 | /** |
Ian Abbott | bafd9c6 | 2019-03-04 14:33:54 +0000 | [diff] [blame] | 400 | * comedi_bytes_per_scan_cmd() - Get length of asynchronous command "scan" in |
| 401 | * bytes |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 402 | * @s: COMEDI subdevice. |
Ian Abbott | bafd9c6 | 2019-03-04 14:33:54 +0000 | [diff] [blame] | 403 | * @cmd: COMEDI command. |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 404 | * |
| 405 | * Determines the overall scan length according to the subdevice type and the |
Ian Abbott | bafd9c6 | 2019-03-04 14:33:54 +0000 | [diff] [blame] | 406 | * number of channels in the scan for the specified command. |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 407 | * |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 408 | * For digital input, output or input/output subdevices, samples for |
| 409 | * multiple channels are assumed to be packed into one or more unsigned |
| 410 | * short or unsigned int values according to the subdevice's %SDF_LSAMPL |
| 411 | * flag. For other types of subdevice, samples are assumed to occupy a |
| 412 | * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag. |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 413 | * |
| 414 | * Returns the overall scan length in bytes. |
| 415 | */ |
Ian Abbott | bafd9c6 | 2019-03-04 14:33:54 +0000 | [diff] [blame] | 416 | unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s, |
| 417 | struct comedi_cmd *cmd) |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 418 | { |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 419 | unsigned int num_samples; |
| 420 | unsigned int bits_per_sample; |
| 421 | |
| 422 | switch (s->type) { |
| 423 | case COMEDI_SUBD_DI: |
| 424 | case COMEDI_SUBD_DO: |
| 425 | case COMEDI_SUBD_DIO: |
H Hartley Sweeten | c39e050 | 2014-10-31 12:04:28 -0700 | [diff] [blame] | 426 | bits_per_sample = 8 * comedi_bytes_per_sample(s); |
Ian Abbott | af57d89 | 2014-11-12 16:00:48 +0000 | [diff] [blame] | 427 | num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample); |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 428 | break; |
| 429 | default: |
Ian Abbott | af57d89 | 2014-11-12 16:00:48 +0000 | [diff] [blame] | 430 | num_samples = cmd->scan_end_arg; |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 431 | break; |
| 432 | } |
H Hartley Sweeten | c39e050 | 2014-10-31 12:04:28 -0700 | [diff] [blame] | 433 | return comedi_samples_to_bytes(s, num_samples); |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 434 | } |
Ian Abbott | bafd9c6 | 2019-03-04 14:33:54 +0000 | [diff] [blame] | 435 | EXPORT_SYMBOL_GPL(comedi_bytes_per_scan_cmd); |
| 436 | |
| 437 | /** |
| 438 | * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes |
| 439 | * @s: COMEDI subdevice. |
| 440 | * |
| 441 | * Determines the overall scan length according to the subdevice type and the |
| 442 | * number of channels in the scan for the current command. |
| 443 | * |
| 444 | * For digital input, output or input/output subdevices, samples for |
| 445 | * multiple channels are assumed to be packed into one or more unsigned |
| 446 | * short or unsigned int values according to the subdevice's %SDF_LSAMPL |
| 447 | * flag. For other types of subdevice, samples are assumed to occupy a |
| 448 | * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag. |
| 449 | * |
| 450 | * Returns the overall scan length in bytes. |
| 451 | */ |
| 452 | unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s) |
| 453 | { |
| 454 | struct comedi_cmd *cmd = &s->async->cmd; |
| 455 | |
| 456 | return comedi_bytes_per_scan_cmd(s, cmd); |
| 457 | } |
Ian Abbott | f146fe63 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 458 | EXPORT_SYMBOL_GPL(comedi_bytes_per_scan); |
| 459 | |
Ian Abbott | 92d354c | 2015-10-23 10:56:09 +0100 | [diff] [blame] | 460 | static unsigned int __comedi_nscans_left(struct comedi_subdevice *s, |
| 461 | unsigned int nscans) |
| 462 | { |
| 463 | struct comedi_async *async = s->async; |
| 464 | struct comedi_cmd *cmd = &async->cmd; |
| 465 | |
| 466 | if (cmd->stop_src == TRIG_COUNT) { |
| 467 | unsigned int scans_left = 0; |
| 468 | |
| 469 | if (async->scans_done < cmd->stop_arg) |
| 470 | scans_left = cmd->stop_arg - async->scans_done; |
| 471 | |
| 472 | if (nscans > scans_left) |
| 473 | nscans = scans_left; |
| 474 | } |
| 475 | return nscans; |
| 476 | } |
| 477 | |
Ian Abbott | 2b4e1f6 | 2014-09-15 13:45:59 +0100 | [diff] [blame] | 478 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 479 | * comedi_nscans_left() - Return the number of scans left in the command |
| 480 | * @s: COMEDI subdevice. |
| 481 | * @nscans: The expected number of scans or 0 for all available scans. |
H Hartley Sweeten | 2ee3775 | 2014-11-05 10:31:29 -0700 | [diff] [blame] | 482 | * |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 483 | * If @nscans is 0, it is set to the number of scans available in the |
| 484 | * async buffer. |
H Hartley Sweeten | 2ee3775 | 2014-11-05 10:31:29 -0700 | [diff] [blame] | 485 | * |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 486 | * If the async command has a stop_src of %TRIG_COUNT, the @nscans will be |
| 487 | * checked against the number of scans remaining to complete the command. |
H Hartley Sweeten | 2ee3775 | 2014-11-05 10:31:29 -0700 | [diff] [blame] | 488 | * |
| 489 | * The return value will then be either the expected number of scans or the |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 490 | * number of scans remaining to complete the command, whichever is fewer. |
H Hartley Sweeten | 2ee3775 | 2014-11-05 10:31:29 -0700 | [diff] [blame] | 491 | */ |
| 492 | unsigned int comedi_nscans_left(struct comedi_subdevice *s, |
| 493 | unsigned int nscans) |
| 494 | { |
H Hartley Sweeten | 2ee3775 | 2014-11-05 10:31:29 -0700 | [diff] [blame] | 495 | if (nscans == 0) { |
| 496 | unsigned int nbytes = comedi_buf_read_n_available(s); |
| 497 | |
| 498 | nscans = nbytes / comedi_bytes_per_scan(s); |
| 499 | } |
Ian Abbott | 92d354c | 2015-10-23 10:56:09 +0100 | [diff] [blame] | 500 | return __comedi_nscans_left(s, nscans); |
H Hartley Sweeten | 2ee3775 | 2014-11-05 10:31:29 -0700 | [diff] [blame] | 501 | } |
| 502 | EXPORT_SYMBOL_GPL(comedi_nscans_left); |
| 503 | |
| 504 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 505 | * comedi_nsamples_left() - Return the number of samples left in the command |
| 506 | * @s: COMEDI subdevice. |
| 507 | * @nsamples: The expected number of samples. |
H Hartley Sweeten | f615915 | 2014-11-05 10:31:33 -0700 | [diff] [blame] | 508 | * |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 509 | * Returns the number of samples remaining to complete the command, or the |
| 510 | * specified expected number of samples (@nsamples), whichever is fewer. |
H Hartley Sweeten | f615915 | 2014-11-05 10:31:33 -0700 | [diff] [blame] | 511 | */ |
| 512 | unsigned int comedi_nsamples_left(struct comedi_subdevice *s, |
| 513 | unsigned int nsamples) |
| 514 | { |
| 515 | struct comedi_async *async = s->async; |
| 516 | struct comedi_cmd *cmd = &async->cmd; |
Chris Opperman | 2665df5 | 2018-06-13 19:14:35 +0200 | [diff] [blame] | 517 | unsigned long long scans_left; |
| 518 | unsigned long long samples_left; |
H Hartley Sweeten | f615915 | 2014-11-05 10:31:33 -0700 | [diff] [blame] | 519 | |
Chris Opperman | 2665df5 | 2018-06-13 19:14:35 +0200 | [diff] [blame] | 520 | if (cmd->stop_src != TRIG_COUNT) |
| 521 | return nsamples; |
H Hartley Sweeten | f615915 | 2014-11-05 10:31:33 -0700 | [diff] [blame] | 522 | |
Chris Opperman | 2665df5 | 2018-06-13 19:14:35 +0200 | [diff] [blame] | 523 | scans_left = __comedi_nscans_left(s, cmd->stop_arg); |
| 524 | if (!scans_left) |
| 525 | return 0; |
H Hartley Sweeten | f615915 | 2014-11-05 10:31:33 -0700 | [diff] [blame] | 526 | |
Chris Opperman | 2665df5 | 2018-06-13 19:14:35 +0200 | [diff] [blame] | 527 | samples_left = scans_left * cmd->scan_end_arg - |
| 528 | comedi_bytes_to_samples(s, async->scan_progress); |
| 529 | |
| 530 | if (samples_left < nsamples) |
| 531 | return samples_left; |
H Hartley Sweeten | f615915 | 2014-11-05 10:31:33 -0700 | [diff] [blame] | 532 | return nsamples; |
| 533 | } |
| 534 | EXPORT_SYMBOL_GPL(comedi_nsamples_left); |
| 535 | |
| 536 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 537 | * comedi_inc_scan_progress() - Update scan progress in asynchronous command |
| 538 | * @s: COMEDI subdevice. |
| 539 | * @num_bytes: Amount of data in bytes to increment scan progress. |
Ian Abbott | 2b4e1f6 | 2014-09-15 13:45:59 +0100 | [diff] [blame] | 540 | * |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 541 | * Increments the scan progress by the number of bytes specified by @num_bytes. |
Ian Abbott | 2b4e1f6 | 2014-09-15 13:45:59 +0100 | [diff] [blame] | 542 | * If the scan progress reaches or exceeds the scan length in bytes, reduce |
| 543 | * it modulo the scan length in bytes and set the "end of scan" asynchronous |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 544 | * event flag (%COMEDI_CB_EOS) to be processed later. |
Ian Abbott | 2b4e1f6 | 2014-09-15 13:45:59 +0100 | [diff] [blame] | 545 | */ |
| 546 | void comedi_inc_scan_progress(struct comedi_subdevice *s, |
| 547 | unsigned int num_bytes) |
| 548 | { |
| 549 | struct comedi_async *async = s->async; |
H Hartley Sweeten | f8736ca | 2014-10-31 09:49:31 -0700 | [diff] [blame] | 550 | struct comedi_cmd *cmd = &async->cmd; |
Ian Abbott | 2b4e1f6 | 2014-09-15 13:45:59 +0100 | [diff] [blame] | 551 | unsigned int scan_length = comedi_bytes_per_scan(s); |
| 552 | |
H Hartley Sweeten | f8736ca | 2014-10-31 09:49:31 -0700 | [diff] [blame] | 553 | /* track the 'cur_chan' for non-SDF_PACKED subdevices */ |
| 554 | if (!(s->subdev_flags & SDF_PACKED)) { |
| 555 | async->cur_chan += comedi_bytes_to_samples(s, num_bytes); |
| 556 | async->cur_chan %= cmd->chanlist_len; |
| 557 | } |
| 558 | |
Ian Abbott | 2b4e1f6 | 2014-09-15 13:45:59 +0100 | [diff] [blame] | 559 | async->scan_progress += num_bytes; |
| 560 | if (async->scan_progress >= scan_length) { |
H Hartley Sweeten | 1dacbe5 | 2014-11-05 10:20:52 -0700 | [diff] [blame] | 561 | unsigned int nscans = async->scan_progress / scan_length; |
| 562 | |
| 563 | if (async->scans_done < (UINT_MAX - nscans)) |
| 564 | async->scans_done += nscans; |
| 565 | else |
| 566 | async->scans_done = UINT_MAX; |
| 567 | |
Ian Abbott | 2b4e1f6 | 2014-09-15 13:45:59 +0100 | [diff] [blame] | 568 | async->scan_progress %= scan_length; |
| 569 | async->events |= COMEDI_CB_EOS; |
| 570 | } |
| 571 | } |
| 572 | EXPORT_SYMBOL_GPL(comedi_inc_scan_progress); |
| 573 | |
Ian Abbott | 5a78035 | 2014-09-15 13:46:01 +0100 | [diff] [blame] | 574 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 575 | * comedi_handle_events() - Handle events and possibly stop acquisition |
| 576 | * @dev: COMEDI device. |
| 577 | * @s: COMEDI subdevice. |
Ian Abbott | 5a78035 | 2014-09-15 13:46:01 +0100 | [diff] [blame] | 578 | * |
| 579 | * Handles outstanding asynchronous acquisition event flags associated |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 580 | * with the subdevice. Call the subdevice's @s->cancel() handler if the |
Ian Abbott | 5a78035 | 2014-09-15 13:46:01 +0100 | [diff] [blame] | 581 | * "end of acquisition", "error" or "overflow" event flags are set in order |
| 582 | * to stop the acquisition at the driver level. |
| 583 | * |
| 584 | * Calls comedi_event() to further process the event flags, which may mark |
| 585 | * the asynchronous command as no longer running, possibly terminated with |
| 586 | * an error, and may wake up tasks. |
| 587 | * |
| 588 | * Return a bit-mask of the handled events. |
| 589 | */ |
| 590 | unsigned int comedi_handle_events(struct comedi_device *dev, |
| 591 | struct comedi_subdevice *s) |
| 592 | { |
| 593 | unsigned int events = s->async->events; |
| 594 | |
| 595 | if (events == 0) |
| 596 | return events; |
| 597 | |
H Hartley Sweeten | 7be7cd1 | 2016-03-30 10:47:25 -0700 | [diff] [blame] | 598 | if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel) |
Ian Abbott | 5a78035 | 2014-09-15 13:46:01 +0100 | [diff] [blame] | 599 | s->cancel(dev, s); |
| 600 | |
| 601 | comedi_event(dev, s); |
| 602 | |
| 603 | return events; |
| 604 | } |
| 605 | EXPORT_SYMBOL_GPL(comedi_handle_events); |
| 606 | |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 607 | static int insn_rw_emulate_bits(struct comedi_device *dev, |
| 608 | struct comedi_subdevice *s, |
H Hartley Sweeten | f91852c | 2016-03-30 10:47:24 -0700 | [diff] [blame] | 609 | struct comedi_insn *insn, |
| 610 | unsigned int *data) |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 611 | { |
H Hartley Sweeten | f91852c | 2016-03-30 10:47:24 -0700 | [diff] [blame] | 612 | struct comedi_insn _insn; |
| 613 | unsigned int chan = CR_CHAN(insn->chanspec); |
| 614 | unsigned int base_chan = (chan < 32) ? 0 : chan; |
| 615 | unsigned int _data[2]; |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 616 | int ret; |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 617 | |
H Hartley Sweeten | f91852c | 2016-03-30 10:47:24 -0700 | [diff] [blame] | 618 | memset(_data, 0, sizeof(_data)); |
| 619 | memset(&_insn, 0, sizeof(_insn)); |
| 620 | _insn.insn = INSN_BITS; |
| 621 | _insn.chanspec = base_chan; |
| 622 | _insn.n = 2; |
| 623 | _insn.subdev = insn->subdev; |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 624 | |
| 625 | if (insn->insn == INSN_WRITE) { |
| 626 | if (!(s->subdev_flags & SDF_WRITABLE)) |
| 627 | return -EINVAL; |
H Hartley Sweeten | f91852c | 2016-03-30 10:47:24 -0700 | [diff] [blame] | 628 | _data[0] = 1 << (chan - base_chan); /* mask */ |
| 629 | _data[1] = data[0] ? (1 << (chan - base_chan)) : 0; /* bits */ |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 630 | } |
| 631 | |
H Hartley Sweeten | f91852c | 2016-03-30 10:47:24 -0700 | [diff] [blame] | 632 | ret = s->insn_bits(dev, s, &_insn, _data); |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 633 | if (ret < 0) |
| 634 | return ret; |
| 635 | |
| 636 | if (insn->insn == INSN_READ) |
H Hartley Sweeten | f91852c | 2016-03-30 10:47:24 -0700 | [diff] [blame] | 637 | data[0] = (_data[1] >> (chan - base_chan)) & 1; |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 638 | |
| 639 | return 1; |
| 640 | } |
| 641 | |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 642 | static int __comedi_device_postconfig_async(struct comedi_device *dev, |
| 643 | struct comedi_subdevice *s) |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 644 | { |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 645 | struct comedi_async *async; |
| 646 | unsigned int buf_size; |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 647 | int ret; |
| 648 | |
Ian Abbott | 77c21b6 | 2019-04-17 15:39:29 +0100 | [diff] [blame] | 649 | lockdep_assert_held(&dev->mutex); |
H Hartley Sweeten | 57b71c3 | 2013-01-21 14:37:15 -0700 | [diff] [blame] | 650 | if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) { |
| 651 | dev_warn(dev->class_dev, |
| 652 | "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n"); |
| 653 | return -EINVAL; |
| 654 | } |
| 655 | if (!s->do_cmdtest) { |
| 656 | dev_warn(dev->class_dev, |
| 657 | "async subdevices must have a do_cmdtest() function\n"); |
| 658 | return -EINVAL; |
| 659 | } |
H Hartley Sweeten | 7be7cd1 | 2016-03-30 10:47:25 -0700 | [diff] [blame] | 660 | if (!s->cancel) |
| 661 | dev_warn(dev->class_dev, |
| 662 | "async subdevices should have a cancel() function\n"); |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 663 | |
| 664 | async = kzalloc(sizeof(*async), GFP_KERNEL); |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 665 | if (!async) |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 666 | return -ENOMEM; |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 667 | |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 668 | init_waitqueue_head(&async->wait_head); |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 669 | s->async = async; |
| 670 | |
| 671 | async->max_bufsize = comedi_default_buf_maxsize_kb * 1024; |
| 672 | buf_size = comedi_default_buf_size_kb * 1024; |
| 673 | if (buf_size > async->max_bufsize) |
| 674 | buf_size = async->max_bufsize; |
| 675 | |
| 676 | if (comedi_buf_alloc(dev, s, buf_size) < 0) { |
| 677 | dev_warn(dev->class_dev, "Buffer allocation failed\n"); |
| 678 | return -ENOMEM; |
| 679 | } |
| 680 | if (s->buf_change) { |
H Hartley Sweeten | d546b89 | 2014-07-21 11:48:32 -0700 | [diff] [blame] | 681 | ret = s->buf_change(dev, s); |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 682 | if (ret < 0) |
| 683 | return ret; |
| 684 | } |
| 685 | |
Ian Abbott | f65cc54 | 2013-02-01 10:20:30 +0000 | [diff] [blame] | 686 | comedi_alloc_subdevice_minor(s); |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static int __comedi_device_postconfig(struct comedi_device *dev) |
| 692 | { |
| 693 | struct comedi_subdevice *s; |
| 694 | int ret; |
| 695 | int i; |
| 696 | |
Ian Abbott | 77c21b6 | 2019-04-17 15:39:29 +0100 | [diff] [blame] | 697 | lockdep_assert_held(&dev->mutex); |
Spencer E. Olson | d7569ad | 2018-10-03 14:56:02 -0600 | [diff] [blame] | 698 | if (!dev->insn_device_config) |
| 699 | dev->insn_device_config = insn_device_inval; |
| 700 | |
| 701 | if (!dev->get_valid_routes) |
| 702 | dev->get_valid_routes = get_zero_valid_routes; |
| 703 | |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 704 | for (i = 0; i < dev->n_subdevices; i++) { |
| 705 | s = &dev->subdevices[i]; |
| 706 | |
| 707 | if (s->type == COMEDI_SUBD_UNUSED) |
| 708 | continue; |
| 709 | |
H Hartley Sweeten | 09567cb | 2013-08-30 10:47:03 -0700 | [diff] [blame] | 710 | if (s->type == COMEDI_SUBD_DO) { |
| 711 | if (s->n_chan < 32) |
| 712 | s->io_bits = (1 << s->n_chan) - 1; |
| 713 | else |
| 714 | s->io_bits = 0xffffffff; |
| 715 | } |
| 716 | |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 717 | if (s->len_chanlist == 0) |
| 718 | s->len_chanlist = 1; |
| 719 | |
| 720 | if (s->do_cmd) { |
H Hartley Sweeten | 40f58a6 | 2013-01-21 14:36:40 -0700 | [diff] [blame] | 721 | ret = __comedi_device_postconfig_async(dev, s); |
| 722 | if (ret) |
| 723 | return ret; |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | if (!s->range_table && !s->range_table_list) |
| 727 | s->range_table = &range_unknown; |
| 728 | |
| 729 | if (!s->insn_read && s->insn_bits) |
| 730 | s->insn_read = insn_rw_emulate_bits; |
| 731 | if (!s->insn_write && s->insn_bits) |
| 732 | s->insn_write = insn_rw_emulate_bits; |
| 733 | |
| 734 | if (!s->insn_read) |
| 735 | s->insn_read = insn_inval; |
| 736 | if (!s->insn_write) |
| 737 | s->insn_write = insn_inval; |
| 738 | if (!s->insn_bits) |
| 739 | s->insn_bits = insn_inval; |
| 740 | if (!s->insn_config) |
| 741 | s->insn_config = insn_inval; |
| 742 | |
| 743 | if (!s->poll) |
| 744 | s->poll = poll_invalid; |
| 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 750 | /* do a little post-config cleanup */ |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 751 | static int comedi_device_postconfig(struct comedi_device *dev) |
| 752 | { |
Ian Abbott | b2a644b | 2013-04-04 14:58:56 +0100 | [diff] [blame] | 753 | int ret; |
| 754 | |
Ian Abbott | 77c21b6 | 2019-04-17 15:39:29 +0100 | [diff] [blame] | 755 | lockdep_assert_held(&dev->mutex); |
Ian Abbott | b2a644b | 2013-04-04 14:58:56 +0100 | [diff] [blame] | 756 | ret = __comedi_device_postconfig(dev); |
H Hartley Sweeten | ae5dd5f | 2013-04-08 10:56:02 -0700 | [diff] [blame] | 757 | if (ret < 0) |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 758 | return ret; |
Ian Abbott | bf11c13 | 2013-11-08 15:03:25 +0000 | [diff] [blame] | 759 | down_write(&dev->attach_lock); |
Ian Abbott | a7401cd | 2013-03-15 13:15:33 +0000 | [diff] [blame] | 760 | dev->attached = true; |
Ian Abbott | bf11c13 | 2013-11-08 15:03:25 +0000 | [diff] [blame] | 761 | up_write(&dev->attach_lock); |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 762 | return 0; |
| 763 | } |
| 764 | |
H Hartley Sweeten | 01fca37 | 2013-01-21 14:36:11 -0700 | [diff] [blame] | 765 | /* |
| 766 | * Generic recognize function for drivers that register their supported |
| 767 | * board names. |
| 768 | * |
| 769 | * 'driv->board_name' points to a 'const char *' member within the |
| 770 | * zeroth element of an array of some private board information |
| 771 | * structure, say 'struct foo_board' containing a member 'const char |
| 772 | * *board_name' that is initialized to point to a board name string that |
| 773 | * is one of the candidates matched against this function's 'name' |
| 774 | * parameter. |
| 775 | * |
| 776 | * 'driv->offset' is the size of the private board information |
| 777 | * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is |
| 778 | * the length of the array of private board information structures. |
| 779 | * |
| 780 | * If one of the board names in the array of private board information |
| 781 | * structures matches the name supplied to this function, the function |
| 782 | * returns a pointer to the pointer to the board name, otherwise it |
| 783 | * returns NULL. The return value ends up in the 'board_ptr' member of |
| 784 | * a 'struct comedi_device' that the low-level comedi driver's |
| 785 | * 'attach()' hook can convert to a point to a particular element of its |
| 786 | * array of private board information structures by subtracting the |
| 787 | * offset of the member that points to the board name. (No subtraction |
| 788 | * is required if the board name pointer is the first member of the |
| 789 | * private board information structure, which is generally the case.) |
| 790 | */ |
| 791 | static void *comedi_recognize(struct comedi_driver *driv, const char *name) |
| 792 | { |
| 793 | char **name_ptr = (char **)driv->board_name; |
| 794 | int i; |
| 795 | |
| 796 | for (i = 0; i < driv->num_names; i++) { |
| 797 | if (strcmp(*name_ptr, name) == 0) |
| 798 | return name_ptr; |
| 799 | name_ptr = (void *)name_ptr + driv->offset; |
| 800 | } |
| 801 | |
| 802 | return NULL; |
| 803 | } |
| 804 | |
| 805 | static void comedi_report_boards(struct comedi_driver *driv) |
| 806 | { |
| 807 | unsigned int i; |
| 808 | const char *const *name_ptr; |
| 809 | |
| 810 | pr_info("comedi: valid board names for %s driver are:\n", |
| 811 | driv->driver_name); |
| 812 | |
| 813 | name_ptr = driv->board_name; |
| 814 | for (i = 0; i < driv->num_names; i++) { |
| 815 | pr_info(" %s\n", *name_ptr); |
| 816 | name_ptr = (const char **)((char *)name_ptr + driv->offset); |
| 817 | } |
| 818 | |
| 819 | if (driv->num_names == 0) |
| 820 | pr_info(" %s\n", driv->driver_name); |
| 821 | } |
| 822 | |
H Hartley Sweeten | f375ac5 | 2013-04-09 16:05:54 -0700 | [diff] [blame] | 823 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 824 | * comedi_load_firmware() - Request and load firmware for a device |
| 825 | * @dev: COMEDI device. |
| 826 | * @device: Hardware device. |
| 827 | * @name: The name of the firmware image. |
| 828 | * @cb: Callback to the upload the firmware image. |
| 829 | * @context: Private context from the driver. |
| 830 | * |
| 831 | * Sends a firmware request for the hardware device and waits for it. Calls |
| 832 | * the callback function to upload the firmware to the device, them releases |
| 833 | * the firmware. |
| 834 | * |
| 835 | * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number |
| 836 | * from the firmware request or the callback function. |
H Hartley Sweeten | 9ff8b15 | 2013-05-17 11:17:00 -0700 | [diff] [blame] | 837 | */ |
| 838 | int comedi_load_firmware(struct comedi_device *dev, |
| 839 | struct device *device, |
| 840 | const char *name, |
| 841 | int (*cb)(struct comedi_device *dev, |
H Hartley Sweeten | d569541 | 2013-05-17 11:18:01 -0700 | [diff] [blame] | 842 | const u8 *data, size_t size, |
| 843 | unsigned long context), |
| 844 | unsigned long context) |
H Hartley Sweeten | 9ff8b15 | 2013-05-17 11:17:00 -0700 | [diff] [blame] | 845 | { |
| 846 | const struct firmware *fw; |
| 847 | int ret; |
| 848 | |
| 849 | if (!cb) |
| 850 | return -EINVAL; |
| 851 | |
| 852 | ret = request_firmware(&fw, name, device); |
| 853 | if (ret == 0) { |
H Hartley Sweeten | d569541 | 2013-05-17 11:18:01 -0700 | [diff] [blame] | 854 | ret = cb(dev, fw->data, fw->size, context); |
H Hartley Sweeten | 9ff8b15 | 2013-05-17 11:17:00 -0700 | [diff] [blame] | 855 | release_firmware(fw); |
| 856 | } |
| 857 | |
H Hartley Sweeten | c6236c0 | 2013-12-10 16:31:25 -0700 | [diff] [blame] | 858 | return ret < 0 ? ret : 0; |
H Hartley Sweeten | 9ff8b15 | 2013-05-17 11:17:00 -0700 | [diff] [blame] | 859 | } |
| 860 | EXPORT_SYMBOL_GPL(comedi_load_firmware); |
| 861 | |
| 862 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 863 | * __comedi_request_region() - Request an I/O region for a legacy driver |
| 864 | * @dev: COMEDI device. |
| 865 | * @start: Base address of the I/O region. |
| 866 | * @len: Length of the I/O region. |
| 867 | * |
| 868 | * Requests the specified I/O port region which must start at a non-zero |
| 869 | * address. |
| 870 | * |
| 871 | * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request |
| 872 | * fails. |
H Hartley Sweeten | f375ac5 | 2013-04-09 16:05:54 -0700 | [diff] [blame] | 873 | */ |
H Hartley Sweeten | ca8b296 | 2013-04-09 16:30:11 -0700 | [diff] [blame] | 874 | int __comedi_request_region(struct comedi_device *dev, |
| 875 | unsigned long start, unsigned long len) |
H Hartley Sweeten | f375ac5 | 2013-04-09 16:05:54 -0700 | [diff] [blame] | 876 | { |
| 877 | if (!start) { |
| 878 | dev_warn(dev->class_dev, |
| 879 | "%s: a I/O base address must be specified\n", |
| 880 | dev->board_name); |
| 881 | return -EINVAL; |
| 882 | } |
| 883 | |
| 884 | if (!request_region(start, len, dev->board_name)) { |
| 885 | dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n", |
| 886 | dev->board_name, start, len); |
| 887 | return -EIO; |
| 888 | } |
H Hartley Sweeten | f375ac5 | 2013-04-09 16:05:54 -0700 | [diff] [blame] | 889 | |
| 890 | return 0; |
| 891 | } |
H Hartley Sweeten | ca8b296 | 2013-04-09 16:30:11 -0700 | [diff] [blame] | 892 | EXPORT_SYMBOL_GPL(__comedi_request_region); |
| 893 | |
| 894 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 895 | * comedi_request_region() - Request an I/O region for a legacy driver |
| 896 | * @dev: COMEDI device. |
| 897 | * @start: Base address of the I/O region. |
| 898 | * @len: Length of the I/O region. |
| 899 | * |
| 900 | * Requests the specified I/O port region which must start at a non-zero |
| 901 | * address. |
| 902 | * |
| 903 | * On success, @dev->iobase is set to the base address of the region and |
| 904 | * @dev->iolen is set to its length. |
| 905 | * |
| 906 | * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request |
| 907 | * fails. |
H Hartley Sweeten | ca8b296 | 2013-04-09 16:30:11 -0700 | [diff] [blame] | 908 | */ |
| 909 | int comedi_request_region(struct comedi_device *dev, |
| 910 | unsigned long start, unsigned long len) |
| 911 | { |
| 912 | int ret; |
| 913 | |
| 914 | ret = __comedi_request_region(dev, start, len); |
H Hartley Sweeten | 316f97f | 2013-04-18 14:31:29 -0700 | [diff] [blame] | 915 | if (ret == 0) { |
H Hartley Sweeten | ca8b296 | 2013-04-09 16:30:11 -0700 | [diff] [blame] | 916 | dev->iobase = start; |
H Hartley Sweeten | 316f97f | 2013-04-18 14:31:29 -0700 | [diff] [blame] | 917 | dev->iolen = len; |
| 918 | } |
H Hartley Sweeten | ca8b296 | 2013-04-09 16:30:11 -0700 | [diff] [blame] | 919 | |
| 920 | return ret; |
| 921 | } |
H Hartley Sweeten | f375ac5 | 2013-04-09 16:05:54 -0700 | [diff] [blame] | 922 | EXPORT_SYMBOL_GPL(comedi_request_region); |
| 923 | |
H Hartley Sweeten | 316f97f | 2013-04-18 14:31:29 -0700 | [diff] [blame] | 924 | /** |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 925 | * comedi_legacy_detach() - A generic (*detach) function for legacy drivers |
| 926 | * @dev: COMEDI device. |
| 927 | * |
| 928 | * This is a simple, generic 'detach' handler for legacy COMEDI devices that |
| 929 | * just use a single I/O port region and possibly an IRQ and that don't need |
| 930 | * any special clean-up for their private device or subdevice storage. It |
| 931 | * can also be called by a driver-specific 'detach' handler. |
| 932 | * |
| 933 | * If @dev->irq is non-zero, the IRQ will be freed. If @dev->iobase and |
| 934 | * @dev->iolen are both non-zero, the I/O port region will be released. |
H Hartley Sweeten | 316f97f | 2013-04-18 14:31:29 -0700 | [diff] [blame] | 935 | */ |
| 936 | void comedi_legacy_detach(struct comedi_device *dev) |
| 937 | { |
H Hartley Sweeten | 3d1fe3f | 2013-04-18 14:34:37 -0700 | [diff] [blame] | 938 | if (dev->irq) { |
| 939 | free_irq(dev->irq, dev); |
| 940 | dev->irq = 0; |
| 941 | } |
H Hartley Sweeten | 316f97f | 2013-04-18 14:31:29 -0700 | [diff] [blame] | 942 | if (dev->iobase && dev->iolen) { |
| 943 | release_region(dev->iobase, dev->iolen); |
| 944 | dev->iobase = 0; |
| 945 | dev->iolen = 0; |
| 946 | } |
| 947 | } |
| 948 | EXPORT_SYMBOL_GPL(comedi_legacy_detach); |
| 949 | |
Bill Pemberton | 0707bb0 | 2009-03-16 22:06:20 -0400 | [diff] [blame] | 950 | int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 951 | { |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 952 | struct comedi_driver *driv; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 953 | int ret; |
| 954 | |
Ian Abbott | 77c21b6 | 2019-04-17 15:39:29 +0100 | [diff] [blame] | 955 | lockdep_assert_held(&dev->mutex); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 956 | if (dev->attached) |
| 957 | return -EBUSY; |
| 958 | |
Ian Abbott | c383e2d | 2013-06-27 14:50:58 +0100 | [diff] [blame] | 959 | mutex_lock(&comedi_drivers_list_lock); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 960 | for (driv = comedi_drivers; driv; driv = driv->next) { |
GĂ¼ngör Erseymen | 559e9a6 | 2012-09-11 17:56:42 +0300 | [diff] [blame] | 961 | if (!try_module_get(driv->module)) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 962 | continue; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 963 | if (driv->num_names) { |
| 964 | dev->board_ptr = comedi_recognize(driv, it->board_name); |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 965 | if (dev->board_ptr) |
| 966 | break; |
H Hartley Sweeten | 3df9f21 | 2014-07-18 14:28:11 -0700 | [diff] [blame] | 967 | } else if (strcmp(driv->driver_name, it->board_name) == 0) { |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 968 | break; |
H Hartley Sweeten | 3df9f21 | 2014-07-18 14:28:11 -0700 | [diff] [blame] | 969 | } |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 970 | module_put(driv->module); |
| 971 | } |
H Hartley Sweeten | e285016 | 2015-03-04 12:15:29 -0700 | [diff] [blame] | 972 | if (!driv) { |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 973 | /* recognize has failed if we get here */ |
| 974 | /* report valid board names before returning error */ |
| 975 | for (driv = comedi_drivers; driv; driv = driv->next) { |
GĂ¼ngör Erseymen | 559e9a6 | 2012-09-11 17:56:42 +0300 | [diff] [blame] | 976 | if (!try_module_get(driv->module)) |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 977 | continue; |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 978 | comedi_report_boards(driv); |
| 979 | module_put(driv->module); |
| 980 | } |
Ian Abbott | c383e2d | 2013-06-27 14:50:58 +0100 | [diff] [blame] | 981 | ret = -EIO; |
| 982 | goto out; |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 983 | } |
H Hartley Sweeten | e285016 | 2015-03-04 12:15:29 -0700 | [diff] [blame] | 984 | if (!driv->attach) { |
Ian Abbott | 8c3714d | 2012-08-15 15:02:45 +0100 | [diff] [blame] | 985 | /* driver does not support manual configuration */ |
| 986 | dev_warn(dev->class_dev, |
| 987 | "driver '%s' does not support attach using comedi_config\n", |
| 988 | driv->driver_name); |
| 989 | module_put(driv->module); |
Ted Chen | 1a59adb | 2015-08-05 01:18:46 +0800 | [diff] [blame] | 990 | ret = -EIO; |
Ian Abbott | c383e2d | 2013-06-27 14:50:58 +0100 | [diff] [blame] | 991 | goto out; |
Ian Abbott | 8c3714d | 2012-08-15 15:02:45 +0100 | [diff] [blame] | 992 | } |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 993 | dev->driver = driv; |
H Hartley Sweeten | 34b6840 | 2013-04-08 10:55:29 -0700 | [diff] [blame] | 994 | dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr |
| 995 | : dev->driver->driver_name; |
Ian Abbott | 3902a37 | 2012-03-30 17:15:00 +0100 | [diff] [blame] | 996 | ret = driv->attach(dev, it); |
Ian Abbott | 74ece108 | 2013-04-04 14:58:59 +0100 | [diff] [blame] | 997 | if (ret >= 0) |
| 998 | ret = comedi_device_postconfig(dev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 999 | if (ret < 0) { |
Ian Abbott | 016599f | 2013-04-04 14:58:58 +0100 | [diff] [blame] | 1000 | comedi_device_detach(dev); |
Ian Abbott | 3955dfa | 2013-08-23 12:37:17 +0100 | [diff] [blame] | 1001 | module_put(driv->module); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1002 | } |
Ian Abbott | b2a644b | 2013-04-04 14:58:56 +0100 | [diff] [blame] | 1003 | /* On success, the driver module count has been incremented. */ |
Ian Abbott | c383e2d | 2013-06-27 14:50:58 +0100 | [diff] [blame] | 1004 | out: |
| 1005 | mutex_unlock(&comedi_drivers_list_lock); |
Ian Abbott | b2a644b | 2013-04-04 14:58:56 +0100 | [diff] [blame] | 1006 | return ret; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 1009 | /** |
| 1010 | * comedi_auto_config() - Create a COMEDI device for a hardware device |
| 1011 | * @hardware_device: Hardware device. |
| 1012 | * @driver: COMEDI low-level driver for the hardware device. |
| 1013 | * @context: Driver context for the auto_attach handler. |
| 1014 | * |
| 1015 | * Allocates a new COMEDI device for the hardware device and calls the |
| 1016 | * low-level driver's 'auto_attach' handler to set-up the hardware and |
| 1017 | * allocate the COMEDI subdevices. Additional "post-configuration" setting |
| 1018 | * up is performed on successful return from the 'auto_attach' handler. |
| 1019 | * If the 'auto_attach' handler fails, the low-level driver's 'detach' |
| 1020 | * handler will be called as part of the clean-up. |
| 1021 | * |
| 1022 | * This is usually called from a wrapper function in a bus-specific COMEDI |
| 1023 | * module, which in turn is usually called from a bus device 'probe' |
| 1024 | * function in the low-level driver. |
| 1025 | * |
| 1026 | * Returns 0 on success, -EINVAL if the parameters are invalid or the |
| 1027 | * post-configuration determines the driver has set the COMEDI device up |
| 1028 | * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of |
| 1029 | * COMEDI minor device numbers, or some negative error number returned by |
| 1030 | * the driver's 'auto_attach' handler. |
| 1031 | */ |
Ian Abbott | a588da1 | 2012-11-14 13:10:38 +0000 | [diff] [blame] | 1032 | int comedi_auto_config(struct device *hardware_device, |
| 1033 | struct comedi_driver *driver, unsigned long context) |
Ian Abbott | f4011670 | 2012-03-30 17:15:01 +0100 | [diff] [blame] | 1034 | { |
H Hartley Sweeten | 6013a9a | 2013-04-08 10:55:05 -0700 | [diff] [blame] | 1035 | struct comedi_device *dev; |
Ian Abbott | f4011670 | 2012-03-30 17:15:01 +0100 | [diff] [blame] | 1036 | int ret; |
| 1037 | |
Ian Abbott | f08e0ac | 2013-04-04 14:58:42 +0100 | [diff] [blame] | 1038 | if (!hardware_device) { |
Simo Koskinen | 423a8a6 | 2017-08-28 15:01:32 +0200 | [diff] [blame] | 1039 | pr_warn("BUG! %s called with NULL hardware_device\n", __func__); |
Ian Abbott | f08e0ac | 2013-04-04 14:58:42 +0100 | [diff] [blame] | 1040 | return -EINVAL; |
| 1041 | } |
| 1042 | if (!driver) { |
| 1043 | dev_warn(hardware_device, |
Simo Koskinen | 423a8a6 | 2017-08-28 15:01:32 +0200 | [diff] [blame] | 1044 | "BUG! %s called with NULL comedi driver\n", __func__); |
Ian Abbott | f08e0ac | 2013-04-04 14:58:42 +0100 | [diff] [blame] | 1045 | return -EINVAL; |
| 1046 | } |
| 1047 | |
Ian Abbott | a588da1 | 2012-11-14 13:10:38 +0000 | [diff] [blame] | 1048 | if (!driver->auto_attach) { |
| 1049 | dev_warn(hardware_device, |
| 1050 | "BUG! comedi driver '%s' has no auto_attach handler\n", |
| 1051 | driver->driver_name); |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | |
H Hartley Sweeten | 6013a9a | 2013-04-08 10:55:05 -0700 | [diff] [blame] | 1055 | dev = comedi_alloc_board_minor(hardware_device); |
Bernd Porr | bcb6232 | 2014-01-07 21:42:25 +0000 | [diff] [blame] | 1056 | if (IS_ERR(dev)) { |
| 1057 | dev_warn(hardware_device, |
| 1058 | "driver '%s' could not create device.\n", |
| 1059 | driver->driver_name); |
H Hartley Sweeten | 6013a9a | 2013-04-08 10:55:05 -0700 | [diff] [blame] | 1060 | return PTR_ERR(dev); |
Bernd Porr | bcb6232 | 2014-01-07 21:42:25 +0000 | [diff] [blame] | 1061 | } |
H Hartley Sweeten | 6013a9a | 2013-04-08 10:55:05 -0700 | [diff] [blame] | 1062 | /* Note: comedi_alloc_board_minor() locked dev->mutex. */ |
Ian Abbott | 77c21b6 | 2019-04-17 15:39:29 +0100 | [diff] [blame] | 1063 | lockdep_assert_held(&dev->mutex); |
Ian Abbott | f4011670 | 2012-03-30 17:15:01 +0100 | [diff] [blame] | 1064 | |
H Hartley Sweeten | 6013a9a | 2013-04-08 10:55:05 -0700 | [diff] [blame] | 1065 | dev->driver = driver; |
H Hartley Sweeten | 34b6840 | 2013-04-08 10:55:29 -0700 | [diff] [blame] | 1066 | dev->board_name = dev->driver->driver_name; |
H Hartley Sweeten | 6013a9a | 2013-04-08 10:55:05 -0700 | [diff] [blame] | 1067 | ret = driver->auto_attach(dev, context); |
Ian Abbott | 74ece108 | 2013-04-04 14:58:59 +0100 | [diff] [blame] | 1068 | if (ret >= 0) |
H Hartley Sweeten | 6013a9a | 2013-04-08 10:55:05 -0700 | [diff] [blame] | 1069 | ret = comedi_device_postconfig(dev); |
Ian Abbott | f4011670 | 2012-03-30 17:15:01 +0100 | [diff] [blame] | 1070 | |
Bernd Porr | bcb6232 | 2014-01-07 21:42:25 +0000 | [diff] [blame] | 1071 | if (ret < 0) { |
| 1072 | dev_warn(hardware_device, |
| 1073 | "driver '%s' failed to auto-configure device.\n", |
| 1074 | driver->driver_name); |
Ian Abbott | f439696 | 2019-04-17 15:35:31 +0100 | [diff] [blame] | 1075 | mutex_unlock(&dev->mutex); |
Ian Abbott | f5b31e1 | 2013-04-04 14:58:48 +0100 | [diff] [blame] | 1076 | comedi_release_hardware_device(hardware_device); |
Bernd Porr | bcb6232 | 2014-01-07 21:42:25 +0000 | [diff] [blame] | 1077 | } else { |
| 1078 | /* |
| 1079 | * class_dev should be set properly here |
| 1080 | * after a successful auto config |
| 1081 | */ |
| 1082 | dev_info(dev->class_dev, |
| 1083 | "driver '%s' has successfully auto-configured '%s'.\n", |
| 1084 | driver->driver_name, dev->board_name); |
Ian Abbott | f439696 | 2019-04-17 15:35:31 +0100 | [diff] [blame] | 1085 | mutex_unlock(&dev->mutex); |
Bernd Porr | bcb6232 | 2014-01-07 21:42:25 +0000 | [diff] [blame] | 1086 | } |
Ian Abbott | f4011670 | 2012-03-30 17:15:01 +0100 | [diff] [blame] | 1087 | return ret; |
| 1088 | } |
Ian Abbott | 8ed705a | 2012-10-27 21:44:14 +0100 | [diff] [blame] | 1089 | EXPORT_SYMBOL_GPL(comedi_auto_config); |
| 1090 | |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 1091 | /** |
| 1092 | * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device |
| 1093 | * @hardware_device: Hardware device previously passed to |
| 1094 | * comedi_auto_config(). |
| 1095 | * |
| 1096 | * Cleans up and eventually destroys the COMEDI device allocated by |
| 1097 | * comedi_auto_config() for the same hardware device. As part of this |
| 1098 | * clean-up, the low-level COMEDI driver's 'detach' handler will be called. |
| 1099 | * (The COMEDI device itself will persist in an unattached state if it is |
| 1100 | * still open, until it is released, and any mmapped buffers will persist |
| 1101 | * until they are munmapped.) |
| 1102 | * |
| 1103 | * This is usually called from a wrapper module in a bus-specific COMEDI |
| 1104 | * module, which in turn is usually set as the bus device 'remove' function |
| 1105 | * in the low-level COMEDI driver. |
| 1106 | */ |
Ian Abbott | 8ed705a | 2012-10-27 21:44:14 +0100 | [diff] [blame] | 1107 | void comedi_auto_unconfig(struct device *hardware_device) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1108 | { |
H Hartley Sweeten | e285016 | 2015-03-04 12:15:29 -0700 | [diff] [blame] | 1109 | if (!hardware_device) |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 1110 | return; |
Ian Abbott | 3346b79 | 2013-04-04 14:58:47 +0100 | [diff] [blame] | 1111 | comedi_release_hardware_device(hardware_device); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1112 | } |
Ian Abbott | 8ed705a | 2012-10-27 21:44:14 +0100 | [diff] [blame] | 1113 | EXPORT_SYMBOL_GPL(comedi_auto_unconfig); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1114 | |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 1115 | /** |
| 1116 | * comedi_driver_register() - Register a low-level COMEDI driver |
| 1117 | * @driver: Low-level COMEDI driver. |
| 1118 | * |
| 1119 | * The low-level COMEDI driver is added to the list of registered COMEDI |
| 1120 | * drivers. This is used by the handler for the "/proc/comedi" file and is |
| 1121 | * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure |
| 1122 | * "legacy" COMEDI devices (for those low-level drivers that support it). |
| 1123 | * |
| 1124 | * Returns 0. |
| 1125 | */ |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1126 | int comedi_driver_register(struct comedi_driver *driver) |
| 1127 | { |
Ian Abbott | c383e2d | 2013-06-27 14:50:58 +0100 | [diff] [blame] | 1128 | mutex_lock(&comedi_drivers_list_lock); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1129 | driver->next = comedi_drivers; |
| 1130 | comedi_drivers = driver; |
Ian Abbott | c383e2d | 2013-06-27 14:50:58 +0100 | [diff] [blame] | 1131 | mutex_unlock(&comedi_drivers_list_lock); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1132 | |
| 1133 | return 0; |
| 1134 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 1135 | EXPORT_SYMBOL_GPL(comedi_driver_register); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1136 | |
Ian Abbott | 9b34845 | 2015-09-17 17:19:17 +0100 | [diff] [blame] | 1137 | /** |
| 1138 | * comedi_driver_unregister() - Unregister a low-level COMEDI driver |
| 1139 | * @driver: Low-level COMEDI driver. |
| 1140 | * |
| 1141 | * The low-level COMEDI driver is removed from the list of registered COMEDI |
| 1142 | * drivers. Detaches any COMEDI devices attached to the driver, which will |
| 1143 | * result in the low-level driver's 'detach' handler being called for those |
| 1144 | * devices before this function returns. |
| 1145 | */ |
Ian Abbott | 99c0e26 | 2013-06-27 14:50:57 +0100 | [diff] [blame] | 1146 | void comedi_driver_unregister(struct comedi_driver *driver) |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1147 | { |
| 1148 | struct comedi_driver *prev; |
| 1149 | int i; |
| 1150 | |
Ian Abbott | c383e2d | 2013-06-27 14:50:58 +0100 | [diff] [blame] | 1151 | /* unlink the driver */ |
| 1152 | mutex_lock(&comedi_drivers_list_lock); |
| 1153 | if (comedi_drivers == driver) { |
| 1154 | comedi_drivers = driver->next; |
| 1155 | } else { |
| 1156 | for (prev = comedi_drivers; prev->next; prev = prev->next) { |
| 1157 | if (prev->next == driver) { |
| 1158 | prev->next = driver->next; |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | mutex_unlock(&comedi_drivers_list_lock); |
| 1164 | |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1165 | /* check for devices using this driver */ |
| 1166 | for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { |
Ian Abbott | a200fad | 2013-11-08 15:03:35 +0000 | [diff] [blame] | 1167 | struct comedi_device *dev = comedi_dev_get_from_minor(i); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1168 | |
| 1169 | if (!dev) |
| 1170 | continue; |
| 1171 | |
| 1172 | mutex_lock(&dev->mutex); |
| 1173 | if (dev->attached && dev->driver == driver) { |
| 1174 | if (dev->use_count) |
| 1175 | dev_warn(dev->class_dev, |
| 1176 | "BUG! detaching device with use_count=%d\n", |
| 1177 | dev->use_count); |
| 1178 | comedi_device_detach(dev); |
| 1179 | } |
| 1180 | mutex_unlock(&dev->mutex); |
Ian Abbott | a200fad | 2013-11-08 15:03:35 +0000 | [diff] [blame] | 1181 | comedi_dev_put(dev); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1182 | } |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1183 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 1184 | EXPORT_SYMBOL_GPL(comedi_driver_unregister); |