aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/comedi/comedi_fops.c
AgeCommit message (Collapse)Author
2020-04-29staging: comedi: Fix comedi_device refcnt leak in comedi_openXiyu Yang
commit 332e0e17ad49e084b7db670ef43b5eb59abd9e34 upstream. comedi_open() invokes comedi_dev_get_from_minor(), which returns a reference of the COMEDI device to "dev" with increased refcount. When comedi_open() returns, "dev" becomes invalid, so the refcount should be decreased to keep refcount balanced. The reference counting issue happens in one exception handling path of comedi_open(). When "cfp" allocation is failed, the refcnt increased by comedi_dev_get_from_minor() is not decreased, causing a refcnt leak. Fix this issue by calling comedi_dev_put() on this error path when "cfp" allocation is failed. Fixes: 20f083c07565 ("staging: comedi: prepare support for per-file read and write subdevices") Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn> Cc: stable <stable@vger.kernel.org> Signed-off-by: Xin Tan <tanxin.ctf@gmail.com> Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Link: https://lore.kernel.org/r/1587361459-83622-1-git-send-email-xiyuyang19@fudan.edu.cn Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-07-16staging: comedi: comedi_fops: Shift assignment operator '=' to previous lineNishad Kamdar
Shift '=' assignment operator to the end of previous line to conform to preferred kernel style line wrapping. Issue reported by checkpatch CHECK. Signed-off-by: Nishad Kamdar <nishadkamdar@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-06-17staging: comedi: comedi_fops: make bool bit-field unsigned int bit-fields.Giulio Benetti
Checkpatch complains on bool bitfields to be an int or u8/u16/u32 bitfield. Make bool bit-fields to be unsigned int bit-fields. Signed-off-by: Giulio Benetti <giulio.benetti@micronovasrl.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-02-11vfs: do bulk POLL* -> EPOLL* replacementLinus Torvalds
This is the mindless scripted replacement of kernel use of POLL* variables as described by Al, done by this script: for V in IN OUT PRI ERR RDNORM RDBAND WRNORM WRBAND HUP RDHUP NVAL MSG; do L=`git grep -l -w POLL$V | grep -v '^t' | grep -v /um/ | grep -v '^sa' | grep -v '/poll.h$'|grep -v '^D'` for f in $L; do sed -i "-es/^\([^\"]*\)\(\<POLL$V\>\)/\\1E\\2/" $f; done done with de-mangling cleanups yet to come. NOTE! On almost all architectures, the EPOLL* constants have the same values as the POLL* constants do. But they keyword here is "almost". For various bad reasons they aren't the same, and epoll() doesn't actually work quite correctly in some cases due to this on Sparc et al. The next patch from Al will sort out the final differences, and we should be all done. Scripted-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-02-01Merge tag 'staging-4.16-rc1' of ↵Linus Torvalds
git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging Pull staging/IIO updates from Greg KH: "Here is the big Staging and IIO driver patches for 4.16-rc1. There is the normal amount of new IIO drivers added, like all releases. The networking IPX and the ncpfs filesystem are moved into the staging tree, as they are on their way out of the kernel due to lack of use anymore. The visorbus subsystem finall has started moving out of the staging tree to the "real" part of the kernel, and the most and fsl-mc codebases are almost ready to move out, that will probably happen for 4.17-rc1 if all goes well. Other than that, there is a bunch of license header cleanups in the tree, along with the normal amount of coding style churn that we all know and love for this codebase. I also got frustrated at the Meltdown/Spectre mess and took it out on the dgnc tty driver, deleting huge chunks of it that were never even being used. Full details of everything is in the shortlog. All of these patches have been in linux-next for a while with no reported issues" * tag 'staging-4.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (627 commits) staging: rtlwifi: remove redundant initialization of 'cfg_cmd' staging: rtl8723bs: remove a couple of redundant initializations staging: comedi: reformat lines to 80 chars or less staging: lustre: separate a connection destroy from free struct kib_conn Staging: rtl8723bs: Use !x instead of NULL comparison Staging: rtl8723bs: Remove dead code Staging: rtl8723bs: Change names to conform to the kernel code staging: ccree: Fix missing blank line after declaration staging: rtl8188eu: remove redundant initialization of 'pwrcfgcmd' staging: rtlwifi: remove unused RTLHALMAC_ST and RTLPHYDM_ST staging: fbtft: remove unused FB_TFT_SSD1325 kconfig staging: comedi: dt2811: remove redundant initialization of 'ns' staging: wilc1000: fix alignments to match open parenthesis staging: wilc1000: removed unnecessary defined enums typedef staging: wilc1000: remove unnecessary use of parentheses staging: rtl8192u: remove redundant initialization of 'timeout' staging: sm750fb: fix CamelCase for dispSet var staging: lustre: lnet/selftest: fix compile error on UP build staging: rtl8723bs: hal_com_phycfg: Remove unneeded semicolons staging: rts5208: Fix "seg_no" calculation in reset_ms_card() ...
2017-11-28the rest of drivers/*: annotate ->poll() instancesAl Viro
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2017-11-28staging: comedi: Remove redundant license textGreg Kroah-Hartman
Now that the SPDX tag is in all comedi files, that identifies the license in a specific and legally-defined manner. So the extra GPL text wording can be removed as it is no longer needed at all. This is done on a quest to remove the 700+ different ways that files in the kernel describe the GPL license text. And there's unneeded stuff like the address (sometimes incorrect) for the FSF which is never needed. No copyright headers or other non-license-description text was removed. Cc: Ian Abbott <abbotti@mev.co.uk> Cc: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-28staging: comedi: add SPDX identifiers to all greybus driver filesGreg Kroah-Hartman
It's good to have SPDX identifiers in all files to make it easier to audit the kernel tree for correct licenses. Update the drivers/staging/comedi files files with the correct SPDX license identifier based on the license text in the file itself. The SPDX identifier is a legally binding shorthand, which can be used instead of the full boiler plate text. This work is based on a script and data from Thomas Gleixner, Philippe Ombredanne, and Kate Stewart. Cc: Ian Abbott <abbotti@mev.co.uk> Cc: H Hartley Sweeten <hsweeten@visionengravers.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Kate Stewart <kstewart@linuxfoundation.org> Cc: Philippe Ombredanne <pombredanne@nexb.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-08-23Staging: comedi: comedi_fops: fix dev_err() warning styleBryan Garza
Changed dev_err() call to use function name constant instead of hardcoded string. Issue found by checkpatch. Signed-off-by: Bryan Garza <bry@riseup.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-07-30staging: comedi: comedi_fops: do not call blocking ops when !TASK_RUNNINGIan Abbott
Comedi's read and write file operation handlers (`comedi_read()` and `comedi_write()`) currently call `copy_to_user()` or `copy_from_user()` whilst in the `TASK_INTERRUPTIBLE` state, which falls foul of the `might_fault()` checks when enabled. Fix it by setting the current task state back to `TASK_RUNNING` a bit earlier before calling these functions. Reported-by: Piotr Gregor <piotrgregor@rsyncme.org> Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Cc: <stable@vger.kernel.org> # 4.5+ Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-06-24staging: comedi: use centralized error clean-up in comedi_init()Ian Abbott
Centralize the "clean-up on error" handling in `comedi_init()` using `goto` statements. Also change some of the explicit `-EIO` return values to the error return values from the failing functions as there is no good reason to use `-EIO` explicitly. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-06-24staging: comedi: fix clean-up of comedi_class in comedi_init()Ian Abbott
There is a clean-up bug in the core comedi module initialization functions, `comedi_init()`. If the `comedi_num_legacy_minors` module parameter is non-zero (and valid), it creates that many "legacy" devices and registers them in SysFS. A failure causes the function to clean up and return an error. Unfortunately, it fails to destroy the "comedi" class that was created earlier. Fix it by adding a call to `class_destroy(comedi_class)` at the appropriate place in the clean-up sequence. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Cc: <stable@vger.kernel.org> # 3.9+ Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-04-25staging: comedi: support vm_access_process for mmap'd bufferIan Abbott
If a process that has mmap'd a COMEDI buffer is being run under a debugger such as GDB, the buffer contents are inaccessible from the debugger. Support the `access()` VM operation to allow the buffer contents to be accessed by another process. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-03-09Staging: comedi: comedi_fops: Change comedi_num_legacy_minors typeCheah Kok Cheong
Change to unsigned to allow removal of negative value check in init section. Use smaller data type since the max possible value currently is 48. Signed-off-by: Cheah Kok Cheong <thrust73@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-03-02sched/headers: Prepare to move signal wakeup & sigpending methods from ↵Ingo Molnar
<linux/sched.h> into <linux/sched/signal.h> Fix up affected files that include this signal functionality via sched.h. Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-10Staging: comedi: comedi_fops: Remove unused stat.h headerCheah Kok Cheong
Unused after commit 6e3029397698 ("staging: comedi: comedi_fops: coding style fixes") - Fixed coding style in comedi_fops.c Symbolic to octal permission. Anyway it's included in module.h Signed-off-by: Cheah Kok Cheong <thrust73@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-01-10Staging: comedi: comedi_fops: Remove unused vmalloc.h headerCheah Kok Cheong
Unused after commit d18431325be0 ("staging: comedi: deprecate loading firmware with comedi_config"). Signed-off-by: Cheah Kok Cheong <thrust73@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-01-10Staging: comedi: comedi_fops: Remove redundant init.h headerCheah Kok Cheong
After commit 0fd972a7d91d ("module: relocate module_init from init.h to module.h"), including module.h will do and init.h is also thrown in. Signed-off-by: Cheah Kok Cheong <thrust73@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-01-10Staging: comedi: comedi_fops: Remove unused kmod.h headerCheah Kok Cheong
Unused after commit f30f2c2d417b ("staging: comedi: remove check for CONFIG_KMOD"). Anyway it's included in module.h Signed-off-by: Cheah Kok Cheong <thrust73@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-01-03Staging: comedi: comedi_fops: Avoid orphaned proc entryCheah Kok Cheong
Move comedi_proc_init to the end to avoid orphaned proc entry if module loading failed. Signed-off-by: Cheah Kok Cheong <thrust73@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-09-20staging: comedi: Use vma_pages function on vma object instead of explicit ↵sayli karnik
computation This patch uses vma_pages function on vma object instead of explicit computation. Signed-off-by: sayli karnik <karniksayli1995@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-09-13staging: comedi: comedi_fops: coding style fixesMatias Mucciolo
- Fixed coding style in comedi_fops.c Symbolic to octal permission. Signed-off-by: Matias Mucciolo <mmucciolo@suteba.org.ar> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-06-17staging: comedi: avoid using timevalArnd Bergmann
Comedi uses 32-bit seconds for its timestamps, on both 32-bit and 64-bit machines. For all I can tell, this was originally meant as a 'timespec', which would overflow in 2038 because of the use of a signed 'long' on 32-bit machines, but it is now used as an array of two unsigned 'lsampl_t' values in comedilib, which will only overflow in 2106, on both 32-bit and 64-bit machines. In an effort to get rid of all uses of 'struct timeval' in the kernel, this replaces the internal code with a call to ktime_get_real_ts64() and a comment at the location of the conversion. Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-06-17Staging: comedi: comedi_fops.c: Fixed coding style issueShyam Saini
Fixed following checkpatch.pl warnings WARNING: Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR> WARNING: Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>) Signed-off-by: Shyam Saini <mayhs11saini@gmail.com> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-03-29staging: comedi: comedi_fops.c: fix lines over 80 charactersIan Abbott
Fix checkpatch.pl warnings about lines over 80 characters in "comedi_fops.c". Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-03-28Staging: comedi: comedi_fops: Replace 'unsigned' with 'unsigned int'Leslie Klein
Fix checkpatch warning: Prefer 'unsigned int' to bare use of 'unsigned' in file comedi_fops.c Signed-off-by: Leslie Klein <lesliebklein@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-03-10Staging: comedi: Use predefined macro offset_in_page() instead of (addr & ↵Sandhya Bankar
~PAGE_MASK). Use predefined macro offset_in_page() instead of (addr & ~PAGE_MASK). Signed-off-by: Sandhya Bankar <bankarsandhya512@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-02-20staging: comedi: COMEDI_BUFINFO: terminate "write" command when stoppedIan Abbott
The `COMEDI_BUFINFO` ioctl is used to advance the current position in the buffer by a specified amount (which can be 0) and get the current position. An asynchronous command in the "read" direction is terminated automatically once it has stopped and information about the final position and error has been reported back to the user. That is not currently done for commands in the "write" direction. Change it to terminate the command in the "write" direction automatically. If the command stopped with an error, report an `EPIPE` error back to the user, otherwise just report the final buffer position back to the user. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-02-20staging: comedi: COMEDI_BUFINFO: return -EPIPE for abnormal readIan Abbott
The `COMEDI_BUFINFO` ioctl is used to advance the current position in the buffer by a specified amount (which can be 0) and get the current position. If an asynchronous command in the "read" direction has stopped normally, the command is terminated as soon as the position has been advanced to the end of all available data. This is not currently done if the command terminated with an error. Change it to allow the command to be terminated even if it stopped with an error, but report an `EPIPE` error to the user first. The `EPIPE` error will not be reported until the "read" position reported back to the user has been advanced to the end of all available data. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-02-20staging: comedi: COMEDI_BUFINFO: become non-busy even if bytes_read is 0Ian Abbott
The `COMEDI_BUFINFO` ioctl is used to advance the current position in the buffer by a specified amount (which can be 0) and get the new position. On input, the `bytes_read` member of `struct comedi_bufinfo` specified the amount to advance the "read" position for an asynchronous command in the "read" direction. If the command has already stopped normally, and the "read" position has been advanced to the end of all available data, the command is terminated by calling `do_become_nonbusy()`. (That is not currently done if the command stopped with an error.) Currently, the command is only terminated if the user is trying to advance the "read" position by a non-zero amount. Change it to allow the command to be terminated even if the user is not trying to advance the "read" position. This is justifiable, as the only time a command stops without error is when it has been set up to read a finite amount of data. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-02-20staging: comedi: COMEDI_BUFINFO: return error if no active commandIan Abbott
The `COMEDI_BUFINFO` ioctl is used to advance the current position in the buffer and/or get the current buffer position. If no asynchronous command is active (started via the file object that issued this ioctl), this information is meaningless. Change it to return an error (`-EINVAL`) in this case. Prior to this change, if a command was started via a different file object, the ioctl returned `-EACCES`, but now it will return `-EINVAL`, which is consistent with the current behavior of the "read" and "write" file operation handlers. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-02-20staging: comedi: COMEDI_BUFINFO: force bytes_written to 0 if stoppedIan Abbott
The `COMEDI_BUFINFO` ioctl is used to advance the current position in the buffer by a specified amount (which can be 0) and get the new position. On input, the `bytes_written` member of `struct comedi_bufinfo` specifies the amount to advance the "write" position for an asynchronous command in the "write" direction. On output, the member indicates the amount the "write" position has actually been advanced. Advancing the "write" position is current done even if the command has stopped and cannot use any more written data. Change it to force the amount successfully written to 0 in that case. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-02-20staging: comedi: COMEDI_BUFINFO: update buffer before becoming non-busyIan Abbott
The `COMEDI_BUFINFO` ioctl is used to advance the current position in the buffer by a specified amount (which can be 0) and get the new position. For an asynchronous command in the "read" direction, if the command has finished acquiring data normally, `do_become_nonbusy()` is called to terminate the command. That resets the buffer position, and currently, the position information returned back to the user is after the buffer has been reset. It should be more useful to return the buffer position before the reset, so move the call to `do_become_nonbusy()` after the code that gets the updated buffer position. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-02-20staging: comedi: COMEDI_BUFINFO: force bytes_read or bytes_written to 0Ian Abbott
The `COMEDI_BUFINFO` ioctl is used to advance the current position in the buffer by a specified amount (which can be 0) and get the new position. On input, the `bytes_read` member of `struct comedi_bufinfo` specifies the amount to advance the "read" position for an asynchronous command in the "read" direction, and the `bytes_written` member specifies the amount to advance the "write" position for a command in the "write" direction. The handler `do_bufinfo_ioctl()` may adjust these by the amount the position is actually advanced before copying them back to the user. Currently, it ignores the specified `bytes_read` value for a command in the "write" direction, and ignores the specified `bytes_written` for a command in the "read" direction, so the values copied back to the user are unchanged. Change it to force the ignored value to 0 before copying the values back to the user. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-02-20staging: comedi: COMEDI_BUFINFO: get amount freed, not amount allocatedIan Abbott
The `COMEDI_BUFINFO` ioctl is used to advance the current position in the buffer by a specified amount (which can be 0) and get the new position. On input, the `bytes_read` member of `struct comedi_bufinfo` specifies the amount to advance the "read" position for an asynchronous command in the "read" direction, and the `bytes_written` member specifies the amount to advance the "write" position for a command in the "write" direction. The handler `do_bufinfo_ioctl()` may limit the specified values according to amount of readable or writable space in the buffer. On output, the `struct comedi_bufinfo` is filled in with the updated position information, along with the adjusted `bytes_read` and `bytes_written` members. Advancing the buffer position occurs in two steps: first, some buffer space is allocated, and second, it is freed, advancing the current "read" or "write" position. Currently, `do_bufinfo_ioctl()` limits `bytes_read` or `bytes_written` to the amount it could allocate in the first step, but that is invisible and irrelevant to the ioctl user. It's mostly irrelevant to the COMEDI internals as well, apart from limiting how much can be freed in the second step. Change it to ignore how much it managed to allocate in the first step and just use the amount that was actually freed in the second step, which is the amount the current buffer position was actually moved by this ioctl call. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-12-21staging: comedi: check for more errors for zero-length writeIan Abbott
If the "write" file operation handler, `comedi_write()` is passed 0 for the amount to write, some error conditions are currently skipped and the function just returns 0. Change it to check those error conditions and return an error value if appropriate. The trickiest case is the check for when the previously set up asynchronous command has terminated with an error. In that case, `-EPIPE` is returned (as it is for a write of non-zero length) and the subdevice gets marked as non-busy. A zero-length write that returns 0 has no other effects, in particular, it does not cause the subdevice to be marked as non-busy. 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>
2015-12-21staging: comedi: simplify returned errors for comedi_write()Ian Abbott
In order to perform a "write" file operation, an asynchronous COMEDI command in the "write" direction needs to have been set up by the current file object on the COMEDI "write" subdevice associated with the file object. If there is a "write" subdevice, but a command has not been set up by the file object (or is has been set-up in the wrong direction), `comedi_write()` currently returns one of two error values `-EINVAL` or `-EACCES`. `-EACCES` is returned if the command was set up by a different subdevice, or somewhat randomly, if a COMEDI "instruction" is currently being processed. `-EINVAL` is returned in other cases. Simplify it by returning `-EINVAL` for all these cases. 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>
2015-12-21staging: comedi: return error on "write" if no command set upIan Abbott
The "write" file operation handler, `comedi_write()` returns an error for pretty much any condition that prevents a "write" going ahead. One of the conditions that prevents a "write" going ahead is that no asynchronous command has been set up, but that currently results in a return value of 0 (unless COMEDI instructions are being processed or an asynchronous command has been set up by a different file object). Change it to return `-EINVAL` in this case. 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>
2015-12-21staging: comedi: allow buffer wraparound in comedi_write()Ian Abbott
`comedi_write()` copies data from the user buffer to the acquisition data buffer, which is cyclic, using a single call to `copy_from_user()`. It currently avoids having to deal with wraparound of the cyclic buffer by limiting the amount it copies (and the amount returned to the user). Change it to deal with the wraparound using two calls to `copy_from_user()` if necessary. 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>
2015-12-21staging: comedi: avoid bad truncation of a size_t in comedi_write()Ian Abbott
At one point in `comedi_write()`, the variable `n` gets assigned to the minimum of the parameter `nbytes` and the amount of writeable buffer space. The way that is done currently is unsafe in the unlikely case that `nbytes` exceeds `UINT_MAX`, so fix 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>
2015-12-21staging: comedi: make some variables unsigned in comedi_write()Ian Abbott
In `comedi_write()`, the `n` and `m` variables are of type `int`. Change them to `unsigned int` as they are used to measure a positive number of bytes. The `count` variable is also of type `int` and holds the returned number of bytes written. Change it to type `ssize_t` to match the function's return type. 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>
2015-12-21staging: comedi: do extra checks for becoming non-busy for "write"Ian Abbott
`comedi_write()` is the handler for the "write" file operation for COMEDI devices. It mostly runs without using the main mutex of the COMEDI device, but uses the `attach_lock` rw_semaphore to protect against the COMEDI device becoming "detached". A file object can write data for a COMEDI asynchonous command if it initiated the command. The COMEDI subdevice is marked as busy when the command is started. At some point, the "write" handler detects that the command has terminated and so marks the subdevice as non-busy. In order to mark the subdevice as non-busy, the "write" handler needs to release the `attach_lock` rw_semaphore and `acquire the main `mutex`. There is a vulnerable point between the two, so it checks that the device is still attached after acquiring the mutex. However, it does not currently check that the conditions for becoming non-busy still hold. Add some more checks that the subdevice is still busy with a command initiated by the same file object, and that the command is in the correct direction (in case the subdevice supports both "read" and "write"). 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>
2015-12-21staging: comedi: rearrange comedi_write() codeIan Abbott
Rearrange the code in `comedi_write()` to reduce the amount of indentation. The code never reiterates the `while` loop once `count` has become non-zero, so we can check that in the `while` condition to save an indentation level. (Note that `nbytes` has been checked to be non-zero before entering the loop, so we can remove that check.) Move the code that makes the subdevice "become non-busy" outside the `while` loop, using a new flag variable `become_nonbusy` to decide whether it needs to be done. This simplifies the wait queue handling so there is a single place where the task is removed from the wait queue, and we can remove the `on_wait_queue` flag variable. 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>
2015-10-13staging: comedi: check for more errors for zero-length readIan Abbott
If the "read" file operation handler, `comedi_read()` is passed 0 for the amount to read, some error conditions are currently skipped and the function just returns 0. Change it to check those error conditions and return an error value if appropriate. The trickiest case is the check for when the previously set up asynchronous command has terminated with an error. In that case, `-EPIPE` is returned (as it is for a read of non-zero length) and the subdevice gets marked as non-busy. A zero-length read that returns 0 has no other effects, in particular, it does not cause the subdevice to be marked as non-busy, and the return value does not indicate an "end-of-file" condition. 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>
2015-10-13staging: comedi: simplify returned errors for comedi_read()Ian Abbott
In order to perform a "read" file operation, an asynchronous COMEDI command in the "read" direction needs to have been set up by the current file object on the COMEDI "read" subdevice associated with the file object. If there is a "read" subdevice, but a command has not been set up by the file object (or is has been set-up in the wrong direction), `comedi_read()` currently returns one of two error values `-EINVAL` or `-EACCES`. `-EACCES` is returned if the command was set up by a different subdevice, or somewhat randomly, if a COMEDI "instruction" is currently being processed. `-EINVAL` is returned in other cases. Simplify it by returning `-EINVAL` for all these cases. 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>
2015-10-13staging: comedi: return error on "read" if no command set upIan Abbott
The "read" file operation handler, `comedi_read()` returns an error for pretty much any condition that prevents a "read" going ahead. One of the conditions that prevents a "read" going ahead is that no asynchronous command has been set up, but that currently results in a return value of 0 (unless COMEDI instructions are being processed or an asynchronous command has been set up by a different file object). Change it to return `-EINVAL` in this case. 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>
2015-10-13staging: comedi: remove superfluous retval = 0 in comedi_read()Ian Abbott
`comedi_read()` initializes `retval` to 0. The other `retval = 0` assignments are superfluous, 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>
2015-10-13staging: comedi: allow buffer wraparound in comedi_read()Ian Abbott
`comedi_read()` copies data from the acquisition data buffer, which is cyclic, to the user buffer using a single call to `copy_to_user()`. It currently avoids having to deal with wraparound of the cyclic buffer by limiting the amount it copies (and the amount returned to the user). Change it to deal with the wraparound using two calls to `copy_to_user()` if necessary. 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>
2015-10-13staging: comedi: avoid bad truncation of a size_t in comedi_read()Ian Abbott
At one point in `comedi_read()`, the variable `n` gets assigned to the minimum of the parameter `nbytes` and the amount of readable buffer space `m`. The way that is done currently is unsafe in the unlikely case that `nbytes` exceeds `UINT_MAX`, so fix 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>
2015-10-13staging: comedi: make some variables unsigned in comedi_read()Ian Abbott
In `comedi_read()`, the `n` and `m` variables are of type `int`. Change them to `unsigned int` as they are used to measure a positive number of bytes. The `count` variable is also of type `int` and holds the returned number of bytes. Change it to type `ssize_t` to match the function's return type. 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>