Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  module/drivers.c
0004  *  functions for manipulating drivers
0005  *
0006  *  COMEDI - Linux Control and Measurement Device Interface
0007  *  Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
0008  *  Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
0009  */
0010 
0011 #include <linux/device.h>
0012 #include <linux/module.h>
0013 #include <linux/errno.h>
0014 #include <linux/kernel.h>
0015 #include <linux/ioport.h>
0016 #include <linux/slab.h>
0017 #include <linux/dma-direction.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/firmware.h>
0020 #include <linux/comedi/comedidev.h>
0021 #include "comedi_internal.h"
0022 
0023 struct comedi_driver *comedi_drivers;
0024 /* protects access to comedi_drivers */
0025 DEFINE_MUTEX(comedi_drivers_list_lock);
0026 
0027 /**
0028  * comedi_set_hw_dev() - Set hardware device associated with COMEDI device
0029  * @dev: COMEDI device.
0030  * @hw_dev: Hardware device.
0031  *
0032  * For automatically configured COMEDI devices (resulting from a call to
0033  * comedi_auto_config() or one of its wrappers from the low-level COMEDI
0034  * driver), comedi_set_hw_dev() is called automatically by the COMEDI core
0035  * to associate the COMEDI device with the hardware device.  It can also be
0036  * called directly by "legacy" low-level COMEDI drivers that rely on the
0037  * %COMEDI_DEVCONFIG ioctl to configure the hardware as long as the hardware
0038  * has a &struct device.
0039  *
0040  * If @dev->hw_dev is NULL, it gets a reference to @hw_dev and sets
0041  * @dev->hw_dev, otherwise, it does nothing.  Calling it multiple times
0042  * with the same hardware device is not considered an error.  If it gets
0043  * a reference to the hardware device, it will be automatically 'put' when
0044  * the device is detached from COMEDI.
0045  *
0046  * Returns 0 if @dev->hw_dev was NULL or the same as @hw_dev, otherwise
0047  * returns -EEXIST.
0048  */
0049 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
0050 {
0051     if (hw_dev == dev->hw_dev)
0052         return 0;
0053     if (dev->hw_dev)
0054         return -EEXIST;
0055     dev->hw_dev = get_device(hw_dev);
0056     return 0;
0057 }
0058 EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
0059 
0060 static void comedi_clear_hw_dev(struct comedi_device *dev)
0061 {
0062     put_device(dev->hw_dev);
0063     dev->hw_dev = NULL;
0064 }
0065 
0066 /**
0067  * comedi_alloc_devpriv() - Allocate memory for the device private data
0068  * @dev: COMEDI device.
0069  * @size: Size of the memory to allocate.
0070  *
0071  * The allocated memory is zero-filled.  @dev->private points to it on
0072  * return.  The memory will be automatically freed when the COMEDI device is
0073  * "detached".
0074  *
0075  * Returns a pointer to the allocated memory, or NULL on failure.
0076  */
0077 void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
0078 {
0079     dev->private = kzalloc(size, GFP_KERNEL);
0080     return dev->private;
0081 }
0082 EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
0083 
0084 /**
0085  * comedi_alloc_subdevices() - Allocate subdevices for COMEDI device
0086  * @dev: COMEDI device.
0087  * @num_subdevices: Number of subdevices to allocate.
0088  *
0089  * Allocates and initializes an array of &struct comedi_subdevice for the
0090  * COMEDI device.  If successful, sets @dev->subdevices to point to the
0091  * first one and @dev->n_subdevices to the number.
0092  *
0093  * Returns 0 on success, -EINVAL if @num_subdevices is < 1, or -ENOMEM if
0094  * failed to allocate the memory.
0095  */
0096 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
0097 {
0098     struct comedi_subdevice *s;
0099     int i;
0100 
0101     if (num_subdevices < 1)
0102         return -EINVAL;
0103 
0104     s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
0105     if (!s)
0106         return -ENOMEM;
0107     dev->subdevices = s;
0108     dev->n_subdevices = num_subdevices;
0109 
0110     for (i = 0; i < num_subdevices; ++i) {
0111         s = &dev->subdevices[i];
0112         s->device = dev;
0113         s->index = i;
0114         s->async_dma_dir = DMA_NONE;
0115         spin_lock_init(&s->spin_lock);
0116         s->minor = -1;
0117     }
0118     return 0;
0119 }
0120 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
0121 
0122 /**
0123  * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback
0124  * @s: COMEDI subdevice.
0125  *
0126  * This is called by low-level COMEDI drivers to allocate an array to record
0127  * the last values written to a subdevice's analog output channels (at least
0128  * by the %INSN_WRITE instruction), to allow them to be read back by an
0129  * %INSN_READ instruction.  It also provides a default handler for the
0130  * %INSN_READ instruction unless one has already been set.
0131  *
0132  * On success, @s->readback points to the first element of the array, which
0133  * is zero-filled.  The low-level driver is responsible for updating its
0134  * contents.  @s->insn_read will be set to comedi_readback_insn_read()
0135  * unless it is already non-NULL.
0136  *
0137  * Returns 0 on success, -EINVAL if the subdevice has no channels, or
0138  * -ENOMEM on allocation failure.
0139  */
0140 int comedi_alloc_subdev_readback(struct comedi_subdevice *s)
0141 {
0142     if (!s->n_chan)
0143         return -EINVAL;
0144 
0145     s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL);
0146     if (!s->readback)
0147         return -ENOMEM;
0148 
0149     if (!s->insn_read)
0150         s->insn_read = comedi_readback_insn_read;
0151 
0152     return 0;
0153 }
0154 EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
0155 
0156 static void comedi_device_detach_cleanup(struct comedi_device *dev)
0157 {
0158     int i;
0159     struct comedi_subdevice *s;
0160 
0161     lockdep_assert_held(&dev->attach_lock);
0162     lockdep_assert_held(&dev->mutex);
0163     if (dev->subdevices) {
0164         for (i = 0; i < dev->n_subdevices; i++) {
0165             s = &dev->subdevices[i];
0166             if (comedi_can_auto_free_spriv(s))
0167                 kfree(s->private);
0168             comedi_free_subdevice_minor(s);
0169             if (s->async) {
0170                 comedi_buf_alloc(dev, s, 0);
0171                 kfree(s->async);
0172             }
0173             kfree(s->readback);
0174         }
0175         kfree(dev->subdevices);
0176         dev->subdevices = NULL;
0177         dev->n_subdevices = 0;
0178     }
0179     kfree(dev->private);
0180     kfree(dev->pacer);
0181     dev->private = NULL;
0182     dev->pacer = NULL;
0183     dev->driver = NULL;
0184     dev->board_name = NULL;
0185     dev->board_ptr = NULL;
0186     dev->mmio = NULL;
0187     dev->iobase = 0;
0188     dev->iolen = 0;
0189     dev->ioenabled = false;
0190     dev->irq = 0;
0191     dev->read_subdev = NULL;
0192     dev->write_subdev = NULL;
0193     dev->open = NULL;
0194     dev->close = NULL;
0195     comedi_clear_hw_dev(dev);
0196 }
0197 
0198 void comedi_device_detach(struct comedi_device *dev)
0199 {
0200     lockdep_assert_held(&dev->mutex);
0201     comedi_device_cancel_all(dev);
0202     down_write(&dev->attach_lock);
0203     dev->attached = false;
0204     dev->detach_count++;
0205     if (dev->driver)
0206         dev->driver->detach(dev);
0207     comedi_device_detach_cleanup(dev);
0208     up_write(&dev->attach_lock);
0209 }
0210 
0211 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
0212 {
0213     return -EINVAL;
0214 }
0215 
0216 static int insn_device_inval(struct comedi_device *dev,
0217                  struct comedi_insn *insn, unsigned int *data)
0218 {
0219     return -EINVAL;
0220 }
0221 
0222 static unsigned int get_zero_valid_routes(struct comedi_device *dev,
0223                       unsigned int n_pairs,
0224                       unsigned int *pair_data)
0225 {
0226     return 0;
0227 }
0228 
0229 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
0230            struct comedi_insn *insn, unsigned int *data)
0231 {
0232     return -EINVAL;
0233 }
0234 
0235 /**
0236  * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback.
0237  * @dev: COMEDI device.
0238  * @s: COMEDI subdevice.
0239  * @insn: COMEDI instruction.
0240  * @data: Pointer to return the readback data.
0241  *
0242  * Handles the %INSN_READ instruction for subdevices that use the readback
0243  * array allocated by comedi_alloc_subdev_readback().  It may be used
0244  * directly as the subdevice's handler (@s->insn_read) or called via a
0245  * wrapper.
0246  *
0247  * @insn->n is normally 1, which will read a single value.  If higher, the
0248  * same element of the readback array will be read multiple times.
0249  *
0250  * Returns @insn->n on success, or -EINVAL if @s->readback is NULL.
0251  */
0252 int comedi_readback_insn_read(struct comedi_device *dev,
0253                   struct comedi_subdevice *s,
0254                   struct comedi_insn *insn,
0255                   unsigned int *data)
0256 {
0257     unsigned int chan = CR_CHAN(insn->chanspec);
0258     int i;
0259 
0260     if (!s->readback)
0261         return -EINVAL;
0262 
0263     for (i = 0; i < insn->n; i++)
0264         data[i] = s->readback[chan];
0265 
0266     return insn->n;
0267 }
0268 EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
0269 
0270 /**
0271  * comedi_timeout() - Busy-wait for a driver condition to occur
0272  * @dev: COMEDI device.
0273  * @s: COMEDI subdevice.
0274  * @insn: COMEDI instruction.
0275  * @cb: Callback to check for the condition.
0276  * @context: Private context from the driver.
0277  *
0278  * Busy-waits for up to a second (%COMEDI_TIMEOUT_MS) for the condition or
0279  * some error (other than -EBUSY) to occur.  The parameters @dev, @s, @insn,
0280  * and @context are passed to the callback function, which returns -EBUSY to
0281  * continue waiting or some other value to stop waiting (generally 0 if the
0282  * condition occurred, or some error value).
0283  *
0284  * Returns -ETIMEDOUT if timed out, otherwise the return value from the
0285  * callback function.
0286  */
0287 int comedi_timeout(struct comedi_device *dev,
0288            struct comedi_subdevice *s,
0289            struct comedi_insn *insn,
0290            int (*cb)(struct comedi_device *dev,
0291                  struct comedi_subdevice *s,
0292                  struct comedi_insn *insn,
0293                  unsigned long context),
0294            unsigned long context)
0295 {
0296     unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS);
0297     int ret;
0298 
0299     while (time_before(jiffies, timeout)) {
0300         ret = cb(dev, s, insn, context);
0301         if (ret != -EBUSY)
0302             return ret; /* success (0) or non EBUSY errno */
0303         cpu_relax();
0304     }
0305     return -ETIMEDOUT;
0306 }
0307 EXPORT_SYMBOL_GPL(comedi_timeout);
0308 
0309 /**
0310  * comedi_dio_insn_config() - Boilerplate (*insn_config) for DIO subdevices
0311  * @dev: COMEDI device.
0312  * @s: COMEDI subdevice.
0313  * @insn: COMEDI instruction.
0314  * @data: Instruction parameters and return data.
0315  * @mask: io_bits mask for grouped channels, or 0 for single channel.
0316  *
0317  * If @mask is 0, it is replaced with a single-bit mask corresponding to the
0318  * channel number specified by @insn->chanspec.  Otherwise, @mask
0319  * corresponds to a group of channels (which should include the specified
0320  * channel) that are always configured together as inputs or outputs.
0321  *
0322  * Partially handles the %INSN_CONFIG_DIO_INPUT, %INSN_CONFIG_DIO_OUTPUTS,
0323  * and %INSN_CONFIG_DIO_QUERY instructions.  The first two update
0324  * @s->io_bits to record the directions of the masked channels.  The last
0325  * one sets @data[1] to the current direction of the group of channels
0326  * (%COMEDI_INPUT) or %COMEDI_OUTPUT) as recorded in @s->io_bits.
0327  *
0328  * The caller is responsible for updating the DIO direction in the hardware
0329  * registers if this function returns 0.
0330  *
0331  * Returns 0 for a %INSN_CONFIG_DIO_INPUT or %INSN_CONFIG_DIO_OUTPUT
0332  * instruction, @insn->n (> 0) for a %INSN_CONFIG_DIO_QUERY instruction, or
0333  * -EINVAL for some other instruction.
0334  */
0335 int comedi_dio_insn_config(struct comedi_device *dev,
0336                struct comedi_subdevice *s,
0337                struct comedi_insn *insn,
0338                unsigned int *data,
0339                unsigned int mask)
0340 {
0341     unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
0342 
0343     if (!mask)
0344         mask = chan_mask;
0345 
0346     switch (data[0]) {
0347     case INSN_CONFIG_DIO_INPUT:
0348         s->io_bits &= ~mask;
0349         break;
0350 
0351     case INSN_CONFIG_DIO_OUTPUT:
0352         s->io_bits |= mask;
0353         break;
0354 
0355     case INSN_CONFIG_DIO_QUERY:
0356         data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
0357         return insn->n;
0358 
0359     default:
0360         return -EINVAL;
0361     }
0362 
0363     return 0;
0364 }
0365 EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
0366 
0367 /**
0368  * comedi_dio_update_state() - Update the internal state of DIO subdevices
0369  * @s: COMEDI subdevice.
0370  * @data: The channel mask and bits to update.
0371  *
0372  * Updates @s->state which holds the internal state of the outputs for DIO
0373  * or DO subdevices (up to 32 channels).  @data[0] contains a bit-mask of
0374  * the channels to be updated.  @data[1] contains a bit-mask of those
0375  * channels to be set to '1'.  The caller is responsible for updating the
0376  * outputs in hardware according to @s->state.  As a minimum, the channels
0377  * in the returned bit-mask need to be updated.
0378  *
0379  * Returns @mask with non-existent channels removed.
0380  */
0381 unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
0382                      unsigned int *data)
0383 {
0384     unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
0385                          : 0xffffffff;
0386     unsigned int mask = data[0] & chanmask;
0387     unsigned int bits = data[1];
0388 
0389     if (mask) {
0390         s->state &= ~mask;
0391         s->state |= (bits & mask);
0392     }
0393 
0394     return mask;
0395 }
0396 EXPORT_SYMBOL_GPL(comedi_dio_update_state);
0397 
0398 /**
0399  * comedi_bytes_per_scan_cmd() - Get length of asynchronous command "scan" in
0400  * bytes
0401  * @s: COMEDI subdevice.
0402  * @cmd: COMEDI command.
0403  *
0404  * Determines the overall scan length according to the subdevice type and the
0405  * number of channels in the scan for the specified command.
0406  *
0407  * For digital input, output or input/output subdevices, samples for
0408  * multiple channels are assumed to be packed into one or more unsigned
0409  * short or unsigned int values according to the subdevice's %SDF_LSAMPL
0410  * flag.  For other types of subdevice, samples are assumed to occupy a
0411  * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag.
0412  *
0413  * Returns the overall scan length in bytes.
0414  */
0415 unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s,
0416                        struct comedi_cmd *cmd)
0417 {
0418     unsigned int num_samples;
0419     unsigned int bits_per_sample;
0420 
0421     switch (s->type) {
0422     case COMEDI_SUBD_DI:
0423     case COMEDI_SUBD_DO:
0424     case COMEDI_SUBD_DIO:
0425         bits_per_sample = 8 * comedi_bytes_per_sample(s);
0426         num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample);
0427         break;
0428     default:
0429         num_samples = cmd->scan_end_arg;
0430         break;
0431     }
0432     return comedi_samples_to_bytes(s, num_samples);
0433 }
0434 EXPORT_SYMBOL_GPL(comedi_bytes_per_scan_cmd);
0435 
0436 /**
0437  * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes
0438  * @s: COMEDI subdevice.
0439  *
0440  * Determines the overall scan length according to the subdevice type and the
0441  * number of channels in the scan for the current command.
0442  *
0443  * For digital input, output or input/output subdevices, samples for
0444  * multiple channels are assumed to be packed into one or more unsigned
0445  * short or unsigned int values according to the subdevice's %SDF_LSAMPL
0446  * flag.  For other types of subdevice, samples are assumed to occupy a
0447  * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag.
0448  *
0449  * Returns the overall scan length in bytes.
0450  */
0451 unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
0452 {
0453     struct comedi_cmd *cmd = &s->async->cmd;
0454 
0455     return comedi_bytes_per_scan_cmd(s, cmd);
0456 }
0457 EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
0458 
0459 static unsigned int __comedi_nscans_left(struct comedi_subdevice *s,
0460                      unsigned int nscans)
0461 {
0462     struct comedi_async *async = s->async;
0463     struct comedi_cmd *cmd = &async->cmd;
0464 
0465     if (cmd->stop_src == TRIG_COUNT) {
0466         unsigned int scans_left = 0;
0467 
0468         if (async->scans_done < cmd->stop_arg)
0469             scans_left = cmd->stop_arg - async->scans_done;
0470 
0471         if (nscans > scans_left)
0472             nscans = scans_left;
0473     }
0474     return nscans;
0475 }
0476 
0477 /**
0478  * comedi_nscans_left() - Return the number of scans left in the command
0479  * @s: COMEDI subdevice.
0480  * @nscans: The expected number of scans or 0 for all available scans.
0481  *
0482  * If @nscans is 0, it is set to the number of scans available in the
0483  * async buffer.
0484  *
0485  * If the async command has a stop_src of %TRIG_COUNT, the @nscans will be
0486  * checked against the number of scans remaining to complete the command.
0487  *
0488  * The return value will then be either the expected number of scans or the
0489  * number of scans remaining to complete the command, whichever is fewer.
0490  */
0491 unsigned int comedi_nscans_left(struct comedi_subdevice *s,
0492                 unsigned int nscans)
0493 {
0494     if (nscans == 0) {
0495         unsigned int nbytes = comedi_buf_read_n_available(s);
0496 
0497         nscans = nbytes / comedi_bytes_per_scan(s);
0498     }
0499     return __comedi_nscans_left(s, nscans);
0500 }
0501 EXPORT_SYMBOL_GPL(comedi_nscans_left);
0502 
0503 /**
0504  * comedi_nsamples_left() - Return the number of samples left in the command
0505  * @s: COMEDI subdevice.
0506  * @nsamples: The expected number of samples.
0507  *
0508  * Returns the number of samples remaining to complete the command, or the
0509  * specified expected number of samples (@nsamples), whichever is fewer.
0510  */
0511 unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
0512                   unsigned int nsamples)
0513 {
0514     struct comedi_async *async = s->async;
0515     struct comedi_cmd *cmd = &async->cmd;
0516     unsigned long long scans_left;
0517     unsigned long long samples_left;
0518 
0519     if (cmd->stop_src != TRIG_COUNT)
0520         return nsamples;
0521 
0522     scans_left = __comedi_nscans_left(s, cmd->stop_arg);
0523     if (!scans_left)
0524         return 0;
0525 
0526     samples_left = scans_left * cmd->scan_end_arg -
0527         comedi_bytes_to_samples(s, async->scan_progress);
0528 
0529     if (samples_left < nsamples)
0530         return samples_left;
0531     return nsamples;
0532 }
0533 EXPORT_SYMBOL_GPL(comedi_nsamples_left);
0534 
0535 /**
0536  * comedi_inc_scan_progress() - Update scan progress in asynchronous command
0537  * @s: COMEDI subdevice.
0538  * @num_bytes: Amount of data in bytes to increment scan progress.
0539  *
0540  * Increments the scan progress by the number of bytes specified by @num_bytes.
0541  * If the scan progress reaches or exceeds the scan length in bytes, reduce
0542  * it modulo the scan length in bytes and set the "end of scan" asynchronous
0543  * event flag (%COMEDI_CB_EOS) to be processed later.
0544  */
0545 void comedi_inc_scan_progress(struct comedi_subdevice *s,
0546                   unsigned int num_bytes)
0547 {
0548     struct comedi_async *async = s->async;
0549     struct comedi_cmd *cmd = &async->cmd;
0550     unsigned int scan_length = comedi_bytes_per_scan(s);
0551 
0552     /* track the 'cur_chan' for non-SDF_PACKED subdevices */
0553     if (!(s->subdev_flags & SDF_PACKED)) {
0554         async->cur_chan += comedi_bytes_to_samples(s, num_bytes);
0555         async->cur_chan %= cmd->chanlist_len;
0556     }
0557 
0558     async->scan_progress += num_bytes;
0559     if (async->scan_progress >= scan_length) {
0560         unsigned int nscans = async->scan_progress / scan_length;
0561 
0562         if (async->scans_done < (UINT_MAX - nscans))
0563             async->scans_done += nscans;
0564         else
0565             async->scans_done = UINT_MAX;
0566 
0567         async->scan_progress %= scan_length;
0568         async->events |= COMEDI_CB_EOS;
0569     }
0570 }
0571 EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
0572 
0573 /**
0574  * comedi_handle_events() - Handle events and possibly stop acquisition
0575  * @dev: COMEDI device.
0576  * @s: COMEDI subdevice.
0577  *
0578  * Handles outstanding asynchronous acquisition event flags associated
0579  * with the subdevice.  Call the subdevice's @s->cancel() handler if the
0580  * "end of acquisition", "error" or "overflow" event flags are set in order
0581  * to stop the acquisition at the driver level.
0582  *
0583  * Calls comedi_event() to further process the event flags, which may mark
0584  * the asynchronous command as no longer running, possibly terminated with
0585  * an error, and may wake up tasks.
0586  *
0587  * Return a bit-mask of the handled events.
0588  */
0589 unsigned int comedi_handle_events(struct comedi_device *dev,
0590                   struct comedi_subdevice *s)
0591 {
0592     unsigned int events = s->async->events;
0593 
0594     if (events == 0)
0595         return events;
0596 
0597     if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel)
0598         s->cancel(dev, s);
0599 
0600     comedi_event(dev, s);
0601 
0602     return events;
0603 }
0604 EXPORT_SYMBOL_GPL(comedi_handle_events);
0605 
0606 static int insn_rw_emulate_bits(struct comedi_device *dev,
0607                 struct comedi_subdevice *s,
0608                 struct comedi_insn *insn,
0609                 unsigned int *data)
0610 {
0611     struct comedi_insn _insn;
0612     unsigned int chan = CR_CHAN(insn->chanspec);
0613     unsigned int base_chan = (chan < 32) ? 0 : chan;
0614     unsigned int _data[2];
0615     int ret;
0616 
0617     memset(_data, 0, sizeof(_data));
0618     memset(&_insn, 0, sizeof(_insn));
0619     _insn.insn = INSN_BITS;
0620     _insn.chanspec = base_chan;
0621     _insn.n = 2;
0622     _insn.subdev = insn->subdev;
0623 
0624     if (insn->insn == INSN_WRITE) {
0625         if (!(s->subdev_flags & SDF_WRITABLE))
0626             return -EINVAL;
0627         _data[0] = 1 << (chan - base_chan);         /* mask */
0628         _data[1] = data[0] ? (1 << (chan - base_chan)) : 0; /* bits */
0629     }
0630 
0631     ret = s->insn_bits(dev, s, &_insn, _data);
0632     if (ret < 0)
0633         return ret;
0634 
0635     if (insn->insn == INSN_READ)
0636         data[0] = (_data[1] >> (chan - base_chan)) & 1;
0637 
0638     return 1;
0639 }
0640 
0641 static int __comedi_device_postconfig_async(struct comedi_device *dev,
0642                         struct comedi_subdevice *s)
0643 {
0644     struct comedi_async *async;
0645     unsigned int buf_size;
0646     int ret;
0647 
0648     lockdep_assert_held(&dev->mutex);
0649     if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
0650         dev_warn(dev->class_dev,
0651              "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
0652         return -EINVAL;
0653     }
0654     if (!s->do_cmdtest) {
0655         dev_warn(dev->class_dev,
0656              "async subdevices must have a do_cmdtest() function\n");
0657         return -EINVAL;
0658     }
0659     if (!s->cancel)
0660         dev_warn(dev->class_dev,
0661              "async subdevices should have a cancel() function\n");
0662 
0663     async = kzalloc(sizeof(*async), GFP_KERNEL);
0664     if (!async)
0665         return -ENOMEM;
0666 
0667     init_waitqueue_head(&async->wait_head);
0668     s->async = async;
0669 
0670     async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
0671     buf_size = comedi_default_buf_size_kb * 1024;
0672     if (buf_size > async->max_bufsize)
0673         buf_size = async->max_bufsize;
0674 
0675     if (comedi_buf_alloc(dev, s, buf_size) < 0) {
0676         dev_warn(dev->class_dev, "Buffer allocation failed\n");
0677         return -ENOMEM;
0678     }
0679     if (s->buf_change) {
0680         ret = s->buf_change(dev, s);
0681         if (ret < 0)
0682             return ret;
0683     }
0684 
0685     comedi_alloc_subdevice_minor(s);
0686 
0687     return 0;
0688 }
0689 
0690 static int __comedi_device_postconfig(struct comedi_device *dev)
0691 {
0692     struct comedi_subdevice *s;
0693     int ret;
0694     int i;
0695 
0696     lockdep_assert_held(&dev->mutex);
0697     if (!dev->insn_device_config)
0698         dev->insn_device_config = insn_device_inval;
0699 
0700     if (!dev->get_valid_routes)
0701         dev->get_valid_routes = get_zero_valid_routes;
0702 
0703     for (i = 0; i < dev->n_subdevices; i++) {
0704         s = &dev->subdevices[i];
0705 
0706         if (s->type == COMEDI_SUBD_UNUSED)
0707             continue;
0708 
0709         if (s->type == COMEDI_SUBD_DO) {
0710             if (s->n_chan < 32)
0711                 s->io_bits = (1 << s->n_chan) - 1;
0712             else
0713                 s->io_bits = 0xffffffff;
0714         }
0715 
0716         if (s->len_chanlist == 0)
0717             s->len_chanlist = 1;
0718 
0719         if (s->do_cmd) {
0720             ret = __comedi_device_postconfig_async(dev, s);
0721             if (ret)
0722                 return ret;
0723         }
0724 
0725         if (!s->range_table && !s->range_table_list)
0726             s->range_table = &range_unknown;
0727 
0728         if (!s->insn_read && s->insn_bits)
0729             s->insn_read = insn_rw_emulate_bits;
0730         if (!s->insn_write && s->insn_bits)
0731             s->insn_write = insn_rw_emulate_bits;
0732 
0733         if (!s->insn_read)
0734             s->insn_read = insn_inval;
0735         if (!s->insn_write)
0736             s->insn_write = insn_inval;
0737         if (!s->insn_bits)
0738             s->insn_bits = insn_inval;
0739         if (!s->insn_config)
0740             s->insn_config = insn_inval;
0741 
0742         if (!s->poll)
0743             s->poll = poll_invalid;
0744     }
0745 
0746     return 0;
0747 }
0748 
0749 /* do a little post-config cleanup */
0750 static int comedi_device_postconfig(struct comedi_device *dev)
0751 {
0752     int ret;
0753 
0754     lockdep_assert_held(&dev->mutex);
0755     ret = __comedi_device_postconfig(dev);
0756     if (ret < 0)
0757         return ret;
0758     down_write(&dev->attach_lock);
0759     dev->attached = true;
0760     up_write(&dev->attach_lock);
0761     return 0;
0762 }
0763 
0764 /*
0765  * Generic recognize function for drivers that register their supported
0766  * board names.
0767  *
0768  * 'driv->board_name' points to a 'const char *' member within the
0769  * zeroth element of an array of some private board information
0770  * structure, say 'struct foo_board' containing a member 'const char
0771  * *board_name' that is initialized to point to a board name string that
0772  * is one of the candidates matched against this function's 'name'
0773  * parameter.
0774  *
0775  * 'driv->offset' is the size of the private board information
0776  * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
0777  * the length of the array of private board information structures.
0778  *
0779  * If one of the board names in the array of private board information
0780  * structures matches the name supplied to this function, the function
0781  * returns a pointer to the pointer to the board name, otherwise it
0782  * returns NULL.  The return value ends up in the 'board_ptr' member of
0783  * a 'struct comedi_device' that the low-level comedi driver's
0784  * 'attach()' hook can convert to a point to a particular element of its
0785  * array of private board information structures by subtracting the
0786  * offset of the member that points to the board name.  (No subtraction
0787  * is required if the board name pointer is the first member of the
0788  * private board information structure, which is generally the case.)
0789  */
0790 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
0791 {
0792     char **name_ptr = (char **)driv->board_name;
0793     int i;
0794 
0795     for (i = 0; i < driv->num_names; i++) {
0796         if (strcmp(*name_ptr, name) == 0)
0797             return name_ptr;
0798         name_ptr = (void *)name_ptr + driv->offset;
0799     }
0800 
0801     return NULL;
0802 }
0803 
0804 static void comedi_report_boards(struct comedi_driver *driv)
0805 {
0806     unsigned int i;
0807     const char *const *name_ptr;
0808 
0809     pr_info("comedi: valid board names for %s driver are:\n",
0810         driv->driver_name);
0811 
0812     name_ptr = driv->board_name;
0813     for (i = 0; i < driv->num_names; i++) {
0814         pr_info(" %s\n", *name_ptr);
0815         name_ptr = (const char **)((char *)name_ptr + driv->offset);
0816     }
0817 
0818     if (driv->num_names == 0)
0819         pr_info(" %s\n", driv->driver_name);
0820 }
0821 
0822 /**
0823  * comedi_load_firmware() - Request and load firmware for a device
0824  * @dev: COMEDI device.
0825  * @device: Hardware device.
0826  * @name: The name of the firmware image.
0827  * @cb: Callback to the upload the firmware image.
0828  * @context: Private context from the driver.
0829  *
0830  * Sends a firmware request for the hardware device and waits for it.  Calls
0831  * the callback function to upload the firmware to the device, them releases
0832  * the firmware.
0833  *
0834  * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number
0835  * from the firmware request or the callback function.
0836  */
0837 int comedi_load_firmware(struct comedi_device *dev,
0838              struct device *device,
0839              const char *name,
0840              int (*cb)(struct comedi_device *dev,
0841                    const u8 *data, size_t size,
0842                    unsigned long context),
0843              unsigned long context)
0844 {
0845     const struct firmware *fw;
0846     int ret;
0847 
0848     if (!cb)
0849         return -EINVAL;
0850 
0851     ret = request_firmware(&fw, name, device);
0852     if (ret == 0) {
0853         ret = cb(dev, fw->data, fw->size, context);
0854         release_firmware(fw);
0855     }
0856 
0857     return min(ret, 0);
0858 }
0859 EXPORT_SYMBOL_GPL(comedi_load_firmware);
0860 
0861 /**
0862  * __comedi_request_region() - Request an I/O region for a legacy driver
0863  * @dev: COMEDI device.
0864  * @start: Base address of the I/O region.
0865  * @len: Length of the I/O region.
0866  *
0867  * Requests the specified I/O port region which must start at a non-zero
0868  * address.
0869  *
0870  * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
0871  * fails.
0872  */
0873 int __comedi_request_region(struct comedi_device *dev,
0874                 unsigned long start, unsigned long len)
0875 {
0876     if (!start) {
0877         dev_warn(dev->class_dev,
0878              "%s: a I/O base address must be specified\n",
0879              dev->board_name);
0880         return -EINVAL;
0881     }
0882 
0883     if (!request_region(start, len, dev->board_name)) {
0884         dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
0885              dev->board_name, start, len);
0886         return -EIO;
0887     }
0888 
0889     return 0;
0890 }
0891 EXPORT_SYMBOL_GPL(__comedi_request_region);
0892 
0893 /**
0894  * comedi_request_region() - Request an I/O region for a legacy driver
0895  * @dev: COMEDI device.
0896  * @start: Base address of the I/O region.
0897  * @len: Length of the I/O region.
0898  *
0899  * Requests the specified I/O port region which must start at a non-zero
0900  * address.
0901  *
0902  * On success, @dev->iobase is set to the base address of the region and
0903  * @dev->iolen is set to its length.
0904  *
0905  * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request
0906  * fails.
0907  */
0908 int comedi_request_region(struct comedi_device *dev,
0909               unsigned long start, unsigned long len)
0910 {
0911     int ret;
0912 
0913     ret = __comedi_request_region(dev, start, len);
0914     if (ret == 0) {
0915         dev->iobase = start;
0916         dev->iolen = len;
0917     }
0918 
0919     return ret;
0920 }
0921 EXPORT_SYMBOL_GPL(comedi_request_region);
0922 
0923 /**
0924  * comedi_legacy_detach() - A generic (*detach) function for legacy drivers
0925  * @dev: COMEDI device.
0926  *
0927  * This is a simple, generic 'detach' handler for legacy COMEDI devices that
0928  * just use a single I/O port region and possibly an IRQ and that don't need
0929  * any special clean-up for their private device or subdevice storage.  It
0930  * can also be called by a driver-specific 'detach' handler.
0931  *
0932  * If @dev->irq is non-zero, the IRQ will be freed.  If @dev->iobase and
0933  * @dev->iolen are both non-zero, the I/O port region will be released.
0934  */
0935 void comedi_legacy_detach(struct comedi_device *dev)
0936 {
0937     if (dev->irq) {
0938         free_irq(dev->irq, dev);
0939         dev->irq = 0;
0940     }
0941     if (dev->iobase && dev->iolen) {
0942         release_region(dev->iobase, dev->iolen);
0943         dev->iobase = 0;
0944         dev->iolen = 0;
0945     }
0946 }
0947 EXPORT_SYMBOL_GPL(comedi_legacy_detach);
0948 
0949 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
0950 {
0951     struct comedi_driver *driv;
0952     int ret;
0953 
0954     lockdep_assert_held(&dev->mutex);
0955     if (dev->attached)
0956         return -EBUSY;
0957 
0958     mutex_lock(&comedi_drivers_list_lock);
0959     for (driv = comedi_drivers; driv; driv = driv->next) {
0960         if (!try_module_get(driv->module))
0961             continue;
0962         if (driv->num_names) {
0963             dev->board_ptr = comedi_recognize(driv, it->board_name);
0964             if (dev->board_ptr)
0965                 break;
0966         } else if (strcmp(driv->driver_name, it->board_name) == 0) {
0967             break;
0968         }
0969         module_put(driv->module);
0970     }
0971     if (!driv) {
0972         /*  recognize has failed if we get here */
0973         /*  report valid board names before returning error */
0974         for (driv = comedi_drivers; driv; driv = driv->next) {
0975             if (!try_module_get(driv->module))
0976                 continue;
0977             comedi_report_boards(driv);
0978             module_put(driv->module);
0979         }
0980         ret = -EIO;
0981         goto out;
0982     }
0983     if (!driv->attach) {
0984         /* driver does not support manual configuration */
0985         dev_warn(dev->class_dev,
0986              "driver '%s' does not support attach using comedi_config\n",
0987              driv->driver_name);
0988         module_put(driv->module);
0989         ret = -EIO;
0990         goto out;
0991     }
0992     dev->driver = driv;
0993     dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
0994                      : dev->driver->driver_name;
0995     ret = driv->attach(dev, it);
0996     if (ret >= 0)
0997         ret = comedi_device_postconfig(dev);
0998     if (ret < 0) {
0999         comedi_device_detach(dev);
1000         module_put(driv->module);
1001     }
1002     /* On success, the driver module count has been incremented. */
1003 out:
1004     mutex_unlock(&comedi_drivers_list_lock);
1005     return ret;
1006 }
1007 
1008 /**
1009  * comedi_auto_config() - Create a COMEDI device for a hardware device
1010  * @hardware_device: Hardware device.
1011  * @driver: COMEDI low-level driver for the hardware device.
1012  * @context: Driver context for the auto_attach handler.
1013  *
1014  * Allocates a new COMEDI device for the hardware device and calls the
1015  * low-level driver's 'auto_attach' handler to set-up the hardware and
1016  * allocate the COMEDI subdevices.  Additional "post-configuration" setting
1017  * up is performed on successful return from the 'auto_attach' handler.
1018  * If the 'auto_attach' handler fails, the low-level driver's 'detach'
1019  * handler will be called as part of the clean-up.
1020  *
1021  * This is usually called from a wrapper function in a bus-specific COMEDI
1022  * module, which in turn is usually called from a bus device 'probe'
1023  * function in the low-level driver.
1024  *
1025  * Returns 0 on success, -EINVAL if the parameters are invalid or the
1026  * post-configuration determines the driver has set the COMEDI device up
1027  * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of
1028  * COMEDI minor device numbers, or some negative error number returned by
1029  * the driver's 'auto_attach' handler.
1030  */
1031 int comedi_auto_config(struct device *hardware_device,
1032                struct comedi_driver *driver, unsigned long context)
1033 {
1034     struct comedi_device *dev;
1035     int ret;
1036 
1037     if (!hardware_device) {
1038         pr_warn("BUG! %s called with NULL hardware_device\n", __func__);
1039         return -EINVAL;
1040     }
1041     if (!driver) {
1042         dev_warn(hardware_device,
1043              "BUG! %s called with NULL comedi driver\n", __func__);
1044         return -EINVAL;
1045     }
1046 
1047     if (!driver->auto_attach) {
1048         dev_warn(hardware_device,
1049              "BUG! comedi driver '%s' has no auto_attach handler\n",
1050              driver->driver_name);
1051         return -EINVAL;
1052     }
1053 
1054     dev = comedi_alloc_board_minor(hardware_device);
1055     if (IS_ERR(dev)) {
1056         dev_warn(hardware_device,
1057              "driver '%s' could not create device.\n",
1058              driver->driver_name);
1059         return PTR_ERR(dev);
1060     }
1061     /* Note: comedi_alloc_board_minor() locked dev->mutex. */
1062     lockdep_assert_held(&dev->mutex);
1063 
1064     dev->driver = driver;
1065     dev->board_name = dev->driver->driver_name;
1066     ret = driver->auto_attach(dev, context);
1067     if (ret >= 0)
1068         ret = comedi_device_postconfig(dev);
1069 
1070     if (ret < 0) {
1071         dev_warn(hardware_device,
1072              "driver '%s' failed to auto-configure device.\n",
1073              driver->driver_name);
1074         mutex_unlock(&dev->mutex);
1075         comedi_release_hardware_device(hardware_device);
1076     } else {
1077         /*
1078          * class_dev should be set properly here
1079          *  after a successful auto config
1080          */
1081         dev_info(dev->class_dev,
1082              "driver '%s' has successfully auto-configured '%s'.\n",
1083              driver->driver_name, dev->board_name);
1084         mutex_unlock(&dev->mutex);
1085     }
1086     return ret;
1087 }
1088 EXPORT_SYMBOL_GPL(comedi_auto_config);
1089 
1090 /**
1091  * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device
1092  * @hardware_device: Hardware device previously passed to
1093  *                   comedi_auto_config().
1094  *
1095  * Cleans up and eventually destroys the COMEDI device allocated by
1096  * comedi_auto_config() for the same hardware device.  As part of this
1097  * clean-up, the low-level COMEDI driver's 'detach' handler will be called.
1098  * (The COMEDI device itself will persist in an unattached state if it is
1099  * still open, until it is released, and any mmapped buffers will persist
1100  * until they are munmapped.)
1101  *
1102  * This is usually called from a wrapper module in a bus-specific COMEDI
1103  * module, which in turn is usually set as the bus device 'remove' function
1104  * in the low-level COMEDI driver.
1105  */
1106 void comedi_auto_unconfig(struct device *hardware_device)
1107 {
1108     if (!hardware_device)
1109         return;
1110     comedi_release_hardware_device(hardware_device);
1111 }
1112 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
1113 
1114 /**
1115  * comedi_driver_register() - Register a low-level COMEDI driver
1116  * @driver: Low-level COMEDI driver.
1117  *
1118  * The low-level COMEDI driver is added to the list of registered COMEDI
1119  * drivers.  This is used by the handler for the "/proc/comedi" file and is
1120  * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure
1121  * "legacy" COMEDI devices (for those low-level drivers that support it).
1122  *
1123  * Returns 0.
1124  */
1125 int comedi_driver_register(struct comedi_driver *driver)
1126 {
1127     mutex_lock(&comedi_drivers_list_lock);
1128     driver->next = comedi_drivers;
1129     comedi_drivers = driver;
1130     mutex_unlock(&comedi_drivers_list_lock);
1131 
1132     return 0;
1133 }
1134 EXPORT_SYMBOL_GPL(comedi_driver_register);
1135 
1136 /**
1137  * comedi_driver_unregister() - Unregister a low-level COMEDI driver
1138  * @driver: Low-level COMEDI driver.
1139  *
1140  * The low-level COMEDI driver is removed from the list of registered COMEDI
1141  * drivers.  Detaches any COMEDI devices attached to the driver, which will
1142  * result in the low-level driver's 'detach' handler being called for those
1143  * devices before this function returns.
1144  */
1145 void comedi_driver_unregister(struct comedi_driver *driver)
1146 {
1147     struct comedi_driver *prev;
1148     int i;
1149 
1150     /* unlink the driver */
1151     mutex_lock(&comedi_drivers_list_lock);
1152     if (comedi_drivers == driver) {
1153         comedi_drivers = driver->next;
1154     } else {
1155         for (prev = comedi_drivers; prev->next; prev = prev->next) {
1156             if (prev->next == driver) {
1157                 prev->next = driver->next;
1158                 break;
1159             }
1160         }
1161     }
1162     mutex_unlock(&comedi_drivers_list_lock);
1163 
1164     /* check for devices using this driver */
1165     for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
1166         struct comedi_device *dev = comedi_dev_get_from_minor(i);
1167 
1168         if (!dev)
1169             continue;
1170 
1171         mutex_lock(&dev->mutex);
1172         if (dev->attached && dev->driver == driver) {
1173             if (dev->use_count)
1174                 dev_warn(dev->class_dev,
1175                      "BUG! detaching device with use_count=%d\n",
1176                      dev->use_count);
1177             comedi_device_detach(dev);
1178         }
1179         mutex_unlock(&dev->mutex);
1180         comedi_dev_put(dev);
1181     }
1182 }
1183 EXPORT_SYMBOL_GPL(comedi_driver_unregister);