summaryrefslogtreecommitdiff
path: root/drivers/staging/comedi/comedi_fops.c
AgeCommit message (Collapse)Author
2013-11-29staging: comedi: avoid memleak for subdevice privateIan Abbott
commit 67aa4acbc97f6a55b328e4e2305ef19cbe949d85 upstream. `comedi_alloc_spriv()` allocates private storage for a comedi subdevice and sets the `SRF_FREE_SPRIV` flag in the `runflags` member of the subdevice to allow the private storage to be automatically freed when the comedi device is being cleaned up. Unfortunately, the flag gets clobbered by `do_cmd_ioctl()` which calls `comedi_set_subdevice_runflags()` with a mask value `~0` and only the `SRF_USER` and `SRF_RUNNING` flags set, all the other SRF flags being cleared. Change the calls to `comedi_set_subdevice_runflags()` that currently use a mask value of `~0` to use a more relevant mask value. For `do_cmd_ioctl()`, the relevant SRF flags are `SRF_USER`, `SRF_ERROR` and `SRF_RUNNING`. (At one time, `SRF_RT` would be included in that set of flags, but it is no longer used.) For `comedi_alloc_spriv()` replace the call to `comedi_set_subdevice_runflags()` with a simple OR-assignment to avoid unnecessary use of a spin-lock. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-09-03Merge tag 'staging-3.12-rc1' of ↵Linus Torvalds
git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging Pull staging tree merge from Greg KH: "Here's the bit staging tree pull request for 3.12-rc1. Lots of staging driver updates, and fixes. Lustre is finally enabled in the build, and lots of cleanup started happening in it. There's a new wireless driver in here, and 2 new TTY drivers, which cause the overall lines added/removed to be quite large on the "added" side. The IIO driver updates are also coming through here, as they are tied to the staging iio drivers" * tag 'staging-3.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (942 commits) staging: dwc2: make dwc2_core_params documentation more complete staging: dwc2: validate the value for phy_utmi_width staging: dwc2: interpret all hwcfg and related register at init time staging: dwc2: properly mask the GRXFSIZ register staging: dwc2: remove redundant register reads staging: dwc2: re-use hptxfsiz variable staging: dwc2: simplify debug output in dwc_hc_init staging: dwc2: add missing shift staging: dwc2: simplify register shift expressions staging: dwc2: only read the snpsid register once staging: dwc2: unshift non-bool register value constants staging: dwc2: fix off-by-one in check for max_packet_count parameter staging: dwc2: remove specific fifo size constants Staging:BCM:DDRInit.c:Renaming __FUNCTION__ staging: bcm: remove Version.h file. staging: rtl8188eu: off by one in rtw_set_802_11_add_wep() staging: r8188eu: copying one byte too much staging: rtl8188eu: || vs && typo staging: r8188eu: off by one bugs staging: crystalhd: Resolve sparse 'different base types' warnings. ...
2013-07-29Merge 3.11-rc3 into driver-core-nextGreg Kroah-Hartman
We want these fixes in this branch. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-07-29Merge 3.11-rc3 into staging-nextGreg Kroah-Hartman
We want these fixes here. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-07-24staging: comedi: convert class code to use dev_groupsGreg Kroah-Hartman
The dev_attrs field of struct class is going away soon, dev_groups should be used instead. This converts the comedi class code to use the correct field. Cc: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Acked-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-07-23staging: comedi: fix a race between do_cmd_ioctl() and read/writeIan Abbott
`do_cmd_ioctl()` is called with the comedi device's mutex locked to process the `COMEDI_CMD` ioctl to set up comedi's asynchronous command handling on a comedi subdevice. `comedi_read()` and `comedi_write()` are the `read` and `write` handlers for the comedi device, but do not lock the mutex (for performance reasons, as some things can hold the mutex for quite a long time). There is a race condition if `comedi_read()` or `comedi_write()` is running at the same time and for the same file object and comedi subdevice as `do_cmd_ioctl()`. `do_cmd_ioctl()` sets the subdevice's `busy` pointer to the file object way before it sets the `SRF_RUNNING` flag in the subdevice's `runflags` member. `comedi_read() and `comedi_write()` check the subdevice's `busy` pointer is pointing to the current file object, then if the `SRF_RUNNING` flag is not set, will call `do_become_nonbusy()` to shut down the asyncronous command. Bad things can happen if the asynchronous command is being shutdown and set up at the same time. To prevent the race, don't set the `busy` pointer until after the `SRF_RUNNING` flag has been set. Also, make sure the mutex is held in `comedi_read()` and `comedi_write()` while calling `do_become_nonbusy()` in order to avoid moving the race condition to a point within that function. Change some error handling `goto cleanup` statements in `do_cmd_ioctl()` to simple `return -ERRFOO` statements as a result of changing when the `busy` pointer is set. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Cc: stable <stable@vger.kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-07-23staging: comedi: COMEDI_CANCEL ioctl should wake up read/writeIan Abbott
Comedi devices can do blocking read() or write() (or poll()) if an asynchronous command has been set up, blocking for data (for read()) or buffer space (for write()). Various events associated with the asynchronous command will wake up the blocked reader or writer (or poller). It is also possible to force the asynchronous command to terminate by issuing a `COMEDI_CANCEL` ioctl. That shuts down the asynchronous command, but does not currently wake up the blocked reader or writer (or poller). If the blocked task could be woken up, it would see that the command is no longer active and return. The caller of the `COMEDI_CANCEL` ioctl could attempt to wake up the blocked task by sending a signal, but that's a nasty workaround. Change `do_cancel_ioctl()` to wake up the wait queue after it returns from `do_cancel()`. `do_cancel()` can propagate an error return value from the low-level comedi driver's cancel routine, but it always shuts the command down regardless, so `do_cancel_ioctl()` can wake up he wait queue regardless of the return value from `do_cancel()`. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Cc: stable <stable@vger.kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-07-23staging: comedi: drivers do not need <linux/ioport.h>H Hartley Sweeten
All the ioport resources are managed by the comedi core. None of the drivers depend on <linux/ioport.h>. Remove the includes. Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com> Cc: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-07-03Merge branch 'for-linus' of ↵Linus Torvalds
git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs Pull second set of VFS changes from Al Viro: "Assorted f_pos race fixes, making do_splice_direct() safe to call with i_mutex on parent, O_TMPFILE support, Jeff's locks.c series, ->d_hash/->d_compare calling conventions changes from Linus, misc stuff all over the place." * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: (63 commits) Document ->tmpfile() ext4: ->tmpfile() support vfs: export lseek_execute() to modules lseek_execute() doesn't need an inode passed to it block_dev: switch to fixed_size_llseek() cpqphp_sysfs: switch to fixed_size_llseek() tile-srom: switch to fixed_size_llseek() proc_powerpc: switch to fixed_size_llseek() ubi/cdev: switch to fixed_size_llseek() pci/proc: switch to fixed_size_llseek() isapnp: switch to fixed_size_llseek() lpfc: switch to fixed_size_llseek() locks: give the blocked_hash its own spinlock locks: add a new "lm_owner_key" lock operation locks: turn the blocked_list into a hashtable locks: convert fl_link to a hlist_node locks: avoid taking global lock if possible when waking up blocked waiters locks: protect most of the file_lock handling with i_lock locks: encapsulate the fl_link list handling locks: make "added" in __posix_lock_file a bool ...
2013-06-29comedi: quit wanking with FASYNC in ->release()Al Viro
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2013-06-24staging: comedi: have comedi_set_spriv() allocate the memoryH Hartley Sweeten
As suggested by Ian Abbott, comedi_set_spriv() can only be used to set the subdevice->private pointer to something that can be kfree()'d. Rename the function to comedi_alloc_spriv() and have it kzalloc() the memory as well as set the private pointer. This saves a function call in the drivers and avoids the possibility of incorrectly calling comedi_set_spriv() for some pointer that is not meant to be kfree()'d. Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-06-17staging: comedi: drivers: let core handle freeing s->privateH Hartley Sweeten
Introduce a new subdevice runflags, SRF_FREE_SPRIV, and a new helper function, comedi_set_spriv(), that the drivers can use to set the comedi_subdevice private data pointer. The helper function will also set SRF_FREE_SPRIV to allow the comedi core to automatically free the subdevice private data during the cleanup_device() stage of the detach. Currently s->private is only allocated by the 8255, addi_watchdog, amplc_dio200_common, and ni_65xx drivers. All users of those drivers can then have the comedi_spriv_free() calls removed and in many cases the (*detach) can then simply be the appropriate comedi core provided function. The ni_65xx driver uses a helper function, ni_65xx_alloc_subdevice_private(), to allocate the private data. Refactor the function to return an errno or call comedi_set_spriv() instead of returning a pointer to the private data and requiring the caller to handle it. Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-06-09staging: comedi: remove 'bi.subdevice < 0' check in do_bufinfo_ioctl()Güngör Erseymen
Type of variable 'bi' is struct comedi_bufinfo and 'subdevice' is defined as unsigned, so there is no need to check if it is less then zero. Signed-off-by: Güngör Erseymen <gelurine@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-06-09staging: comedi: remove 'bc.subdevice < 0' check in do_bufconfig_ioctl()Güngör Erseymen
Type of variable 'bc' is struct comedi_bufconfig and 'subdevice' is defined as unsigned, so there is no need to check if it is less then zero. Signed-off-by: Güngör Erseymen <gelurine@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-05-27Merge 3.10-rc3 into staging-nextGreg Kroah-Hartman
We want the changes here, and we resolve the merge conflict that was happening in the nvec_kbd.c file. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-05-13staging: comedi: allow buffer resize if previous resize failedIan Abbott
When a comedi device is successfully attached, those subdevices that support asynchronous commands will have had buffers allocated successfully. It is possible to resize the buffers afterwards, but if the resize fails the subdevice is left with no buffer (`s->async->prealloc_buf == NULL`). Currently, this also causes any subsequent attempts to resize the buffer to fail with an error, which seems like a bad idea. Remove the check in `resize_async_buffer()` that causes the resize to fail if the subdevice currently has no buffer (presumably due to the failure of a previous resize attempt). Callers of `resize_async_buffer()` have already checked that the subdevice is allowed to have a buffer. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-05-13staging: comedi: remove FSF address from boilerplate textH Hartley Sweeten
Addresses change... Remove the paragraph with the FSF address from all the comedi source files. Also, remove the paragraph about the finding the complete GPL in the COPYING file since it's unnecessary. Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com> Cc: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-12staging: comedi: use EXPORT_SYMBOL_GPL() for all exported symbolsH Hartley Sweeten
Comedi is licensed under GPL. Some if its exports are currently EXPORT_SYMBOL() and others are EXPORT_SYMBOL_GPL(). Change them all to EXPORT_SYMBOL_GPL() and see if anyone reports any fall out. If any of the symbols "need" to be EXPORT_SYMBOL() they will be addressed as needed. Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com> Cc: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: add comedi_clear_board_dev()Ian Abbott
Add local function `comedi_clear_board_dev()` as a safer alternative to `comedi_clear_board_minor()` when we already have a pointer to a `struct comedi_device`. It uses the board minor device number stored in the `struct comedi_device` (which must have already been initialized) and only clears the entry in `comedi_board_minor_table[]` if it points to the specified `struct comedi_device`. Rather than returning the old table entry, it returns `true` if the entry matched (and so has just been cleared) and returns `false` otherwise. Call `comedi_clear_board_dev()` instead of `comedi_clear_board_minor()` in `comedi_unlocked_ioctl()` (in the code that frees a dynamically allocated comedi device detached by the `COMEDI_DEVCONFIG` ioctl). That ought to return `true` but check it just in case before freeing the device. There is still a race condition here which needs to be dealt with once we've implemented reference counting for `struct comedi_device`s. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: remove struct comedi_file_infoIan Abbott
`struct comedi_file_info` is no longer used so remove it. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: simplify comedi_board_minor_table[]Ian Abbott
`comedi_alloc_board_minor()` allocates and initializes a `struct comedi_file_info` and a `struct comedi_device`, and assigns a board minor device number (if there are any available), storing a pointer to the allocated `struct comedi_file_info` in `comedi_board_minor_table[minor]` where `minor` is the board minor device number. There is no longer anything useful in the `struct comedi_file_info` apart from the pointer to the `struct comedi_device` that was allocated, so the `struct comedi_file_info` is superfluous. Change `comedi_board_minor_table[]` to hold pointers to the actual `struct comedi_device`'s. `comedi_alloc_board_minor()` no longer needs to allocate a `struct comedi_file_info`. Replace `comedi_free_board_file_info()` with `comedi_free_board_dev()` with its parameter pointing to the `struct comedi_device` to be freed (there is no longer a `struct comedi_file_info` to be freed). There are consequential changes to `comedi_dev_from_board_minor()`, `comedi_clear_board_minor()` (which now returns a `struct comedi_device *`), `comedi_free_board_minor()`, `comedi_release_hardware_device()` and `comedi_unlocked_ioctl()` (when dealing with detachment of a dynamically allocated comedi device by the `COMEDI_DEVCONFIG` ioctl). `comedi_dev_from_file_info()` is no longer used as a result of the above changes so remove it. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: simplify comedi_subdevice_minor_table[]Ian Abbott
`comedi_alloc_subdevice_minor()` allocates and initializes a `struct comedi_file_info` and assigns a subdevice minor device number (if there are any available), storing a pointer to the allocated `struct comedi_file_info` in `comedi_subdevice_minor_table[i]` where `i` is the array index corresponding to the subdevice minor device number (indexed by subdevice minor device number minus `COMEDI_NUM_BOARD_MINORS`). The information stored in the `struct comedi_file_info` can be derived from the subdevice structure (`struct comedi_subdevice`) itself, so the `struct comedi_file_info` is superfluous. Change `comedi_subdevice_minor_table[]` to hold pointers to the actual `struct comedi_subdevice`'s. `comedi_alloc_subdevice_minor()` no longer needs to allocate a `struct comedi_file_info` and `comedi_free_subdevice_info()` no longer has a `struct comedi_file_info` to free. Replace `comedi_file_info_from_minor()` with `comedi_subdevice_from_minor()`, returning a (possibly NULL) pointer to a `struct comedi_subdevice` from the table. This has knock-on effects for `comedi_dev_from_subdevice_minor()`, `comedi_read_subdevice()` and `comedi_write_subdevice()`. In particular, `comedi_read_subdevice()` and `comedi_write_subdevice()` now need to check the subdevice flags to see if the determine whether to override the comedi device's default read/write subdevice. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: remove comedi_file_info_from_board_minor()Ian Abbott
Merge the code from `comedi_file_info_from_board_minor()` into `comedi_dev_from_board_minor()` and remove it since nothing else calls it. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: remove comedi_clear_subdevice_minor()Ian Abbott
Merge the code from `comedi_clear_subdevice_minor()` into `comedi_free_subdevice_minor()` and remove it since nothing else calls it. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: refactor comedi_dev_from_minor()Ian Abbott
Refactor `comedi_dev_from_minor()` to call one of two new functions `comedi_dev_from_board_minor()` (for minor device numbers less than `COMEDI_NUM_BOARD_MINORS`) or `comedi_dev_from_subdevice_minor()` (for minor device numbers greater than or equal to `COMEDI_NUM_BOARD_MINORS`, which are subdevice minor device numbers). Remove `comedi_file_info_from_minor()` as it is no longer used. This is a step towards removing `struct comedi_file_info`. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: reduce use of struct comedi_file_infoIan Abbott
Since the parameters of `comedi_read_subdevice()` and `comedi_write_subdevice()` have changed, there is no longer a need to keep a local variable `struct comedi_file_info *info` hanging around in the file operation functions and device attribute access functions. Remove the variable from those function and replace the call sequence `info = comedi_file_info_from_minor(minor)`, `dev = comedi_dev_from_file_info(info)` with the simpler `dev = comedi_dev_from_minor(minor)`. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: change comedi_read/write_subdevice() parametersIan Abbott
`comedi_read_subdevice()` and `comedi_write_subdevice()` currently take a single parameter pointing to a `struct comedi_file_info`. I'm trying to get rid of `struct comedi_file_info` so as part of that plan, `comedi_read_subdevice()` and `comedi_write_subdevice()` need to change. Change them to take two parameters: a pointer to a `struct comedi_device` and a minor device number. If the minor device number is a "board" minor device number (`minor < COMEDI_NUM_BOARD_MINORS`) we'll always return the default read or write subdevice. If the minor device number if a "subdevice" minor device number a different read or write subdevice may be returned. In that case, use the subdevice minor device number to look up the information about whether the default read or write subdevice needs to be overridden. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: use minor device number in attribute functionsIan Abbott
The comedi device attribute functions such as `show_max_read_buffer_kb()` call `dev_get_drvdata()` to get a pointer to a `struct comedi_file_info` from the private driver data field of class device. Change them to use the minor device number to look up this pointer value so they behave more like the file operation functions. Check the pointer is non-NULL as the entry in the minor device table could have been set to NULL. Note that there is still a race condition in the use of this pointer value after acquiring the mutex which needs to be dealt with once reference counting has been implemented for comedi devices. The calls to `dev_set_drvdata()` from `comedi_alloc_board_minor()` and `comedi_alloc_subdevice_minor()` are no longer needed so remove them. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: get mutex before subdevice in attribute functionsIan Abbott
The comedi device attribute functions such as `show_read_buffer_kb()` or `show_write_buffer_kb()` call `comedi_read_subdevice()` or `comedi_write_subdevice()` without acquiring the comedi device's mutex first, although the functions do acquire the mutex afterwards. Change them to acquire the mutex first. This is consistent with most of the comedi device file operation functions (apart from `comedi_read()` and `comedi_write()` which probably need looking at). Despite the use of the mutex, there are still race conditions as the `struct comedi_file_info *info` variable value set before acquiring the mutex could be stale after acquiring the mutex. This problem will be dealt with once reference counting has been implemented for the comedi devices. This patch also adds local variable `struct comedi_device *dev` to the functions to reduce the use of the `info` variable a little bit. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: rename dev parameter of device attribute functionsIan Abbott
Most of the comedi core uses the identifier `dev` to point to a `struct comedi_device`. The device sysfs attribute functions such as `show_max_read_buffer_kb()` use the parameter id `dev` to point to a `struct device`. Rename the parameter to `csdev` for "class device" for consistency with the functions that call `device_create()` to create these class devices (`comedi_alloc_board_minor()` and `comedi_alloc_subdevice_minor()`). Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: remove hardware_device from struct comedi_device_infoIan Abbott
The `hardware_device` member of `struct comedi_device_info` is only set to point to a hardware device by the auto-configuration code (specifically `comedi_alloc_board_minor() with a non-NULL `hardware_device` parameter) so that it can be found again by the auto-unconfiguration code (specifically `comedi_release_hardware_device()`). However, as `info->device->hw_dev` (where `info` is a pointer to the `struct comedi_device_info`) is set to the same value as `info->hardware_device` (by calling `comedi_set_hw_dev()` with the same hardware device pointer) we can look for that instead, so there is no need for the `hardware_device` member any more. Get rid of it. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: remove comedi_clear_minor()Ian Abbott
`comedi_clear_minor()` calls either `comedi_clear_board_minor()` or `comedi_clear_subdevice_minor()` depending on its minor device number parameter. The one it calls only depends on where it is called from, so bypass the calls to `comedi_clear_minor()` and remove it. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: separate board and subdevice minor tablesIan Abbott
The comedi core reserves minor device numbers from 0 to `COMEDI_NUM_BOARD_MINORS - 1` (0 to 0x30 - 1) for the main comedi "board" devices and reserves minor device numbers from `COMEDI_NUM_BOARD_MINORS` to `COMEDI_NUM_MINORS - 1` (0x30 to 0x100 - 1) for comedi subdevices (or at least those that claim to support asynchronous comedi commands). There is an array `comedi_file_info_table[COMEDI_NUM_MINORS]` used to hold pointers to information for each board minor device number and subdevice minor device number that has been allocated (with NULL pointers for those not allocated), along with a protective lock `comedi_file_info_table_lock`. Since the ranges of board minor device numbers and subdevice minor device numbers do not overlap, we can use separate tables and separate locks for the different types of minor device numbers. This will allow us to use different pointer types for the elements of each table in the future without just using a generic `void *`. (At the moment, the table elements point to a `struct comedi_file_info` allocated dynamically for each allocated board minor device or subdevice minor device, but I plan to get rid of that data structure.) Replace `comedi_file_info_table[COMEDI_NUM_MINORS]` with two new arrays of the same type, `comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS]` for board minors, and `comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS]` for subdevice minors (where `COMEDI_NUM_SUBDEVICE_MINORS` is `COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS`). `comedi_subdevice_minor_table[]` is indexed by the subdevice minor number minus `COMEDI_NUM_BOARD_MINORS` since `COMEDI_NUM_BOARD_MINORS` is the first valid subdevice minor number. Replace `comedi_file_info_table_lock` with `comedi_board_minor_table_lock` for board minors and `comedi_subdevice_minor_table_lock` for subdevice minors. Refactor `comedi_clear_minor()` to call one of two new functions `comedi_clear_board_minor()` and `comedi_clear_subdevice_minor()` depending on the minor device number passed as a parameter. Similarly, refactor `comedi_file_info_from_minor()` to call one of two new functions `comedi_file_info_from_board_minor()` and `comedi_file_info_from_subdevice_minor()` depending on the minor device number parameter. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: remove COMEDI_FIRST_SUBDEVICE_MINORIan Abbott
The macro `COMEDI_FIRST_SUBDEVICE_MINOR` just expands to another macro `COMEDI_NUM_BOARD_MINORS`. Replace uses of `COMEDI_FIRST_SUBDEVICE_MINOR` with `COMEDI_NUM_BOARD_MINORS` and get rid of the former. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: move COMEDI_NUM_MINORS and COMEDI_FIRST_SUBDEVICE_MINORIan Abbott
The macro definitions `COMEDI_NUM_MINORS` and `COMEDI_FIRST_SUBDEVICE_MINOR` are only used in "comedi_fops.c" so move them to there from "comedidev.h". Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: change comedi_file_info_table_lock to mutexIan Abbott
The spin-lock `comedi_file_info_table_lock` is used to protect against simultaneous modification and access of `comedi_file_info_table[]`. Change it from a spin-lock to a mutex as it is only used in contexts where sleeping is allowed. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: set hw_dev in comedi_alloc_board_minor()Ian Abbott
Call `comedi_set_hw_dev()` to set the `hw_dev` member of `struct comedi_device` in `comedi_alloc_board_minor()` instead of in `comedi_auto_config()`. Don't bother to check for an error returned by `comedi_set_hw_dev()` here; it only fails when changing a non-NULL pointer to a different non-NULL pointer and since the `struct comedi_device` has just been allocated and initialized, its `hw_dev` will be NULL already. Calling `comedi_set_hw_dev()` with a non-NULL hardware device pointer increments the kref counter for the hardware device. If `comedi_alloc_board_minor()` fails further down the function, we rely on its call to `comedi_device_cleanup()` to call `comedi_clear_hw_dev()` (via `comedi_device_detach()` and `cleanup_device()`) to clear `hw_dev` and decrement its kref counter. (That's the "beneficial side-effect" mentioned in the patch that replaced `__comedi_device_detach()`.) Remove the call to `comedi_set_hw_dev()` from `comedi_auto_config()` as the call to `comedi_alloc_board_minor()` does it for us. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: simplify driver module countingIan Abbott
For a legacy device attachment with the `COMEDI_DEVCONFIG` ioctl, `do_devconfig_ioctl()` calls `comedi_device_attach()` to find a matching device driver and attach the device. It then tries to increment the device driver's module count and if that fails it detaches the device. So on successful attachment of a device by the `COMEDI_DEVCONFIG` ioctl, the device driver's module count will have been incremented. `comedi_device_attach()` is called from nowhere else. It already increments the device driver's module count temporarily and decrements it again; if it gets as far as calling `comedi_device_postconfig()` the module count is decremented within that function. Simplify the above by removing the decrement of the device driver module count from `comedi_device_postconfig()`. If the call to `comedi_device_postconfig()` succeeds, `comedi_device_attach()` will return with the module count still incremented, otherwise decrement the module count before returning the error. Don't try and increment the module count in `do_devconfig_ioctl()` after a successful return from `comedi_device_attach()` as the module count has now already been incremented. `comedi_device_postconfig()` is also called by `comedi_auto_config()` which currently has to increment the device driver's module count temporarily so that `comedi_device_postconfig()` can decrement it, but always returns with no overall change to the module count. Remove all the module count manipulations from `comedi_device_postconfig()`. There is no other reason for `comedi_auto_config()` to increment the device driver's module count temporarily, since it is only called (indirectly) from the device driver itself (usually via one of the wrappers `comedi_pci_auto_config()` or `comedi_usb_auto_config()`). Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: avoid forward declarationsIan Abbott
Move a few functions and variables to avoid a couple of forward declarations. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: remove manually unconfigured dynamic devicesIan Abbott
If a dynamically allocated (non-legacy, and automatically configured) comedi device has been successfully unconfigured by use of the `COMEDI_DEVCONFIG` ioctl, then remove the device afterwards. (Dynamically identified comedi devices are identified by their minor device number being `comedi_num_legacy_minors` or greater.) This is done in `comedi_unlocked_ioctl()` on return from `do_devconfig_ioctl()`. Note that there is an unlikely race condition with some other thread that has just called `comedi_file_info_from_minor()` or `comedi_dev_from_minor()` and is about to use the device, but that race condition also exists for automatically removed devices and will be dealt with properly once reference counting of comedi devices has been implemented. We do avoid a race condition between automatic removal and removal by the `COMEDI_DEVCONFIG` ioctl though. Also add an extra precaution in `do_devconfig_ioctl()` to avoid configuring a dynamically allocated device since there is a tight window avoiding the race condition where this could happen and the device is about to be removed anyway. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: no need to evade comedi_auto_unconfig()Ian Abbott
When `comedi_unlocked_ioctl()` handled the `COMEDI_DEVCONFIG` ioctl via a call to `do_devconfig()`, this used to be allowed to attach a legacy hardware device to any board minor device, and the auto-unconfiguration code for non-legacy used to search all board minor devices to find the hardware device being removed. After calling `do_devconfig()` successfully, `comedi_unlocked_ioctl()` sets the `hardware_device` member of the `struct comedi_file_info` to NULL to stop the auto-unconfiguration code finding it by accident and unconfiguring the wrong comedi device as a result. The above is no longer necessary because `do_devconfig()` now only allows legacy hardware devices to be attached to comedi board minor device numbers below `comedi_num_legacy_minors` and the auto-unconfiguration code (particularly `comedi_release_hardware_device()`) only searches comedi board minor device numbers greater than or equal to `comedi_num_legacy_minors`, so there is no overlap. Remove the now unnecessary setting of `info->hardware_device = NULL` in `comedi_unlocked_ioctl()`. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: avoid releasing legacy minors automaticallyIan Abbott
`comedi_alloc_board_minor()` is called for both reserved "legacy" devices created during module initialization, and for dynamically created devices (via `comedi_auto_config()`). All the minor device numbers from 0 to `comedi_num_legacy_minors - 1` are for legacy devices and all those from `comedi_num_legacy_minors` to `COMEDI_NUM_BOARD_MINORS - 1` are for dynamically created devices. `comedi_release_hardware_device()` is called (via `comedi_auto_unconfig()`) when a dynamically created device is being removed. It needs to search the table of minor device numbers to see which one is associated with the hardware device. It currently starts the search at minor device number 0. Change it to start from `comedi_num_legacy_minors` to skip over those belonging to legacy devices. Also change `comedi_alloc_board_minor()` to skip the legacy devices when searching for a free minor device number for the dynamically created device. (The validity of the `hardware_device` parameter is used to distinguish the legacy devices from the dynamically created ones.) Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: pre-lock mutex on creation of comedi deviceIan Abbott
Return from `comedi_alloc_board_minor()` with the mutex of the created `struct comedi_device` pre-locked. This allows further initialization by the caller without the worry of something getting in there first. `comedi_auto_config()` no longer needs to check if the device is already "attached" since whatever was trying to attach the device would need to have locked the mutex first. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: change comedi_alloc_board_minor() to return pointerIan Abbott
Change `comedi_alloc_board_minor()` to return a pointer to the allocated `struct comedi_device` instead of a minor device number. Return an `ERR_PTR()` value on error instead of a negative error number. This saves a call to `comedi_dev_from_minor()` in `comedi_auto_config()`. Also change it to use a local variable `dev` to hold the pointer to the `struct comedi_device` instead of using `info->device` all the time. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: make comedi_free_board_minor() staticIan Abbott
`comedi_free_board_minor()` is now only called from the same .c file it is defined in, so give it static linkage. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: add comedi_release_hardware_device()Ian Abbott
Add `comedi_release_hardware_device()` as a replacement for the call sequence `comedi_find_board_minor()`, `comedi_free_board_minor()`. This is slightly safer as we can make sure nothing funny happens to the found `comedi_file_info_table[]` entry in the middle of the sequence. Change `comedi_auto_unconfig()` to call the new function instead of the old sequence. Remove `comedi_find_board_minor()` as it has no other callers. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: refactor comedi_free_board_minor()Ian Abbott
Split out the part of `comedi_free_board_minor()` that clears the `comedi_file_info_table[]` element into new function `comedi_clear_minor()`. Split out the remainder of the original function into new function `comedi_free_board_file_info()`. Also re-use a call to `comedi_clear_minor()` in `comedi_free_subdevice_minor()` instead of doing the same thing inline. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: adjust module count on device cleanupIan Abbott
`comedi_device_cleanup()` is called just before freeing a comedi device. It is possible for the device to still be open in which case the module reference counts for the core comedi module and possibly the low-level driver module will remain out of whack because `comedi_close()` will not find the comedi device and so will not decrement the module counts. This really needs to be handled better, but for now decrement the module counts in `comedi_device_cleanup()` according to the number of outstanding opens. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: remove memset() call from comedi_device_init()Ian Abbott
`comedi_device_init()` is only called from one place (`comedi_alloc_board_minor()`) and the `struct comedi_device` has already been zeroed out by `kzalloc()`. Don't bother zeroing it out again with `memset()`. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-04-05staging: comedi: no need to zero out comedi_file_info_table[]Ian Abbott
In the comedi core module, `comedi_file_info_table[]` is tentatively defined in the .bss section, so will already be zeroed out on initialization. Don't bother zeroing it out again in the module initialization function `comedi_init()`. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>