2019-05-29 22:18:11 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2007-05-14 15:57:40 +08:00
|
|
|
/*
|
|
|
|
* HID raw devices, giving access to raw HID events.
|
|
|
|
*
|
|
|
|
* In comparison to hiddev, this device does not process the
|
|
|
|
* hid events at all (no parsing, no lookups). This lets applications
|
|
|
|
* to work on raw hid events as they want to, and avoids a need to
|
|
|
|
* use a transport-specific userspace libhid/libusb libraries.
|
|
|
|
*
|
2014-01-06 23:43:27 +08:00
|
|
|
* Copyright (c) 2007-2014 Jiri Kosina
|
2007-05-14 15:57:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-12-10 11:29:03 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2007-05-14 15:57:40 +08:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/cdev.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/major.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2007-05-14 15:57:40 +08:00
|
|
|
#include <linux/hid.h>
|
|
|
|
#include <linux/mutex.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2016-03-17 07:59:38 +08:00
|
|
|
#include <linux/string.h>
|
2007-05-14 15:57:40 +08:00
|
|
|
|
|
|
|
#include <linux/hidraw.h>
|
|
|
|
|
|
|
|
static int hidraw_major;
|
|
|
|
static struct cdev hidraw_cdev;
|
|
|
|
static struct class *hidraw_class;
|
|
|
|
static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
|
2008-10-31 07:07:23 +08:00
|
|
|
static DEFINE_MUTEX(minors_lock);
|
2007-05-14 15:57:40 +08:00
|
|
|
|
|
|
|
static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct hidraw_list *list = file->private_data;
|
|
|
|
int ret = 0, len;
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
|
2009-10-12 17:25:56 +08:00
|
|
|
mutex_lock(&list->read_mutex);
|
2007-05-14 15:57:40 +08:00
|
|
|
|
2009-10-12 17:25:56 +08:00
|
|
|
while (ret == 0) {
|
2007-05-14 15:57:40 +08:00
|
|
|
if (list->head == list->tail) {
|
|
|
|
add_wait_queue(&list->hidraw->wait, &wait);
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
|
|
|
|
while (list->head == list->tail) {
|
|
|
|
if (signal_pending(current)) {
|
|
|
|
ret = -ERESTARTSYS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!list->hidraw->exist) {
|
|
|
|
ret = -EIO;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-21 15:20:31 +08:00
|
|
|
if (file->f_flags & O_NONBLOCK) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
2007-05-14 15:57:40 +08:00
|
|
|
|
|
|
|
/* allow O_NONBLOCK to work well from other threads */
|
|
|
|
mutex_unlock(&list->read_mutex);
|
|
|
|
schedule();
|
|
|
|
mutex_lock(&list->read_mutex);
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_current_state(TASK_RUNNING);
|
|
|
|
remove_wait_queue(&list->hidraw->wait, &wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
len = list->buffer[list->tail].len > count ?
|
|
|
|
count : list->buffer[list->tail].len;
|
|
|
|
|
2012-04-27 06:56:08 +08:00
|
|
|
if (list->buffer[list->tail].value) {
|
|
|
|
if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = len;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kfree(list->buffer[list->tail].value);
|
2012-06-28 22:51:56 +08:00
|
|
|
list->buffer[list->tail].value = NULL;
|
2007-05-14 15:57:40 +08:00
|
|
|
list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
mutex_unlock(&list->read_mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-06 23:43:27 +08:00
|
|
|
/*
|
|
|
|
* The first byte of the report buffer is expected to be a report number.
|
|
|
|
*/
|
2011-01-18 16:04:39 +08:00
|
|
|
static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
|
2007-05-14 15:57:40 +08:00
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
unsigned int minor = iminor(file_inode(file));
|
2010-03-25 21:15:11 +08:00
|
|
|
struct hid_device *dev;
|
2007-05-14 15:57:40 +08:00
|
|
|
__u8 *buf;
|
|
|
|
int ret = 0;
|
|
|
|
|
2018-11-09 05:38:42 +08:00
|
|
|
lockdep_assert_held(&minors_lock);
|
|
|
|
|
2013-07-22 18:03:13 +08:00
|
|
|
if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
|
2010-10-05 23:20:17 +08:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-03-25 21:15:11 +08:00
|
|
|
dev = hidraw_table[minor]->hid;
|
|
|
|
|
2008-09-18 01:41:58 +08:00
|
|
|
if (count > HID_MAX_BUFFER_SIZE) {
|
2010-12-10 11:29:03 +08:00
|
|
|
hid_warn(dev, "pid %d passed too large report\n",
|
|
|
|
task_pid_nr(current));
|
2010-03-25 21:15:11 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count < 2) {
|
2010-12-10 11:29:03 +08:00
|
|
|
hid_warn(dev, "pid %d passed too short report\n",
|
|
|
|
task_pid_nr(current));
|
2010-03-25 21:15:11 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
|
2016-03-17 07:59:38 +08:00
|
|
|
buf = memdup_user(buffer, count);
|
|
|
|
if (IS_ERR(buf)) {
|
|
|
|
ret = PTR_ERR(buf);
|
2010-03-25 21:15:11 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2007-05-14 15:57:40 +08:00
|
|
|
|
2014-03-09 11:52:42 +08:00
|
|
|
if ((report_type == HID_OUTPUT_REPORT) &&
|
|
|
|
!(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
|
2014-02-21 04:24:50 +08:00
|
|
|
ret = hid_hw_output_report(dev, buf, count);
|
|
|
|
/*
|
|
|
|
* compatibility with old implementation of USB-HID and I2C-HID:
|
|
|
|
* if the device does not support receiving output reports,
|
|
|
|
* on an interrupt endpoint, fallback to SET_REPORT HID command.
|
|
|
|
*/
|
|
|
|
if (ret != -ENOSYS)
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
|
|
|
|
HID_REQ_SET_REPORT);
|
|
|
|
|
2010-03-25 21:15:11 +08:00
|
|
|
out_free:
|
2007-05-14 15:57:40 +08:00
|
|
|
kfree(buf);
|
2010-03-25 21:15:11 +08:00
|
|
|
out:
|
2011-01-18 16:04:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
mutex_lock(&minors_lock);
|
|
|
|
ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
|
2010-03-25 21:15:11 +08:00
|
|
|
mutex_unlock(&minors_lock);
|
2007-05-14 15:57:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-18 16:04:39 +08:00
|
|
|
|
2014-01-06 23:43:27 +08:00
|
|
|
/*
|
|
|
|
* This function performs a Get_Report transfer over the control endpoint
|
2011-03-28 02:29:02 +08:00
|
|
|
* per section 7.2.1 of the HID specification, version 1.1. The first byte
|
|
|
|
* of buffer is the report number to request, or 0x0 if the defice does not
|
|
|
|
* use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
|
2014-01-06 23:43:27 +08:00
|
|
|
* or HID_INPUT_REPORT.
|
|
|
|
*/
|
2011-01-18 16:04:39 +08:00
|
|
|
static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
|
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
unsigned int minor = iminor(file_inode(file));
|
2011-01-18 16:04:39 +08:00
|
|
|
struct hid_device *dev;
|
|
|
|
__u8 *buf;
|
|
|
|
int ret = 0, len;
|
|
|
|
unsigned char report_number;
|
|
|
|
|
2018-11-09 05:38:42 +08:00
|
|
|
lockdep_assert_held(&minors_lock);
|
|
|
|
|
2018-04-06 07:09:36 +08:00
|
|
|
if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-01-18 16:04:39 +08:00
|
|
|
dev = hidraw_table[minor]->hid;
|
|
|
|
|
2014-02-06 05:33:22 +08:00
|
|
|
if (!dev->ll_driver->raw_request) {
|
2011-01-18 16:04:39 +08:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > HID_MAX_BUFFER_SIZE) {
|
|
|
|
printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
|
|
|
|
task_pid_nr(current));
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < 2) {
|
|
|
|
printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
|
|
|
|
task_pid_nr(current));
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:
kmalloc(a * b, gfp)
with:
kmalloc_array(a * b, gfp)
as well as handling cases of:
kmalloc(a * b * c, gfp)
with:
kmalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kmalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kmalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kmalloc
+ kmalloc_array
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kmalloc(sizeof(THING) * C2, ...)
|
kmalloc(sizeof(TYPE) * C2, ...)
|
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * E2
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 04:55:00 +08:00
|
|
|
buf = kmalloc(count, GFP_KERNEL);
|
2011-01-18 16:04:39 +08:00
|
|
|
if (!buf) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-01-06 23:43:27 +08:00
|
|
|
/*
|
|
|
|
* Read the first byte from the user. This is the report number,
|
2014-02-06 05:33:22 +08:00
|
|
|
* which is passed to hid_hw_raw_request().
|
2014-01-06 23:43:27 +08:00
|
|
|
*/
|
2011-01-18 16:04:39 +08:00
|
|
|
if (copy_from_user(&report_number, buffer, 1)) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
2014-02-06 05:33:22 +08:00
|
|
|
ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
|
|
|
|
HID_REQ_GET_REPORT);
|
2011-01-18 16:04:39 +08:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
len = (ret < count) ? ret : count;
|
|
|
|
|
|
|
|
if (copy_to_user(buffer, buf, len)) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = len;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree(buf);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:39:46 +08:00
|
|
|
static __poll_t hidraw_poll(struct file *file, poll_table *wait)
|
2007-05-14 15:57:40 +08:00
|
|
|
{
|
|
|
|
struct hidraw_list *list = file->private_data;
|
2024-06-11 20:08:33 +08:00
|
|
|
__poll_t mask = EPOLLOUT | EPOLLWRNORM; /* hidraw is always writable */
|
2007-05-14 15:57:40 +08:00
|
|
|
|
|
|
|
poll_wait(file, &list->hidraw->wait, wait);
|
|
|
|
if (list->head != list->tail)
|
2024-06-11 20:08:33 +08:00
|
|
|
mask |= EPOLLIN | EPOLLRDNORM;
|
2007-05-14 15:57:40 +08:00
|
|
|
if (!list->hidraw->exist)
|
2024-06-11 20:08:33 +08:00
|
|
|
mask |= EPOLLERR | EPOLLHUP;
|
|
|
|
return mask;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hidraw_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
unsigned int minor = iminor(inode);
|
|
|
|
struct hidraw *dev;
|
|
|
|
struct hidraw_list *list;
|
2013-08-26 23:38:35 +08:00
|
|
|
unsigned long flags;
|
2007-05-14 15:57:40 +08:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-10-31 07:07:23 +08:00
|
|
|
mutex_lock(&minors_lock);
|
2013-07-22 18:03:13 +08:00
|
|
|
if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
|
2007-05-14 15:57:40 +08:00
|
|
|
err = -ENODEV;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = hidraw_table[minor];
|
2008-10-31 07:07:23 +08:00
|
|
|
if (!dev->open++) {
|
2010-12-08 15:02:48 +08:00
|
|
|
err = hid_hw_power(dev->hid, PM_HINT_FULLON);
|
2011-09-28 01:41:58 +08:00
|
|
|
if (err < 0) {
|
|
|
|
dev->open--;
|
2010-12-08 15:02:48 +08:00
|
|
|
goto out_unlock;
|
2011-09-28 01:41:58 +08:00
|
|
|
}
|
2010-12-08 15:02:48 +08:00
|
|
|
|
|
|
|
err = hid_hw_open(dev->hid);
|
2008-12-17 22:38:03 +08:00
|
|
|
if (err < 0) {
|
2010-12-08 15:02:48 +08:00
|
|
|
hid_hw_power(dev->hid, PM_HINT_NORMAL);
|
2008-10-31 07:07:23 +08:00
|
|
|
dev->open--;
|
2013-08-26 23:38:35 +08:00
|
|
|
goto out_unlock;
|
2008-12-17 22:38:03 +08:00
|
|
|
}
|
2008-10-31 07:07:23 +08:00
|
|
|
}
|
2007-05-14 15:57:40 +08:00
|
|
|
|
2013-08-26 23:38:35 +08:00
|
|
|
list->hidraw = hidraw_table[minor];
|
|
|
|
mutex_init(&list->read_mutex);
|
|
|
|
spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
|
|
|
|
list_add_tail(&list->node, &hidraw_table[minor]->list);
|
|
|
|
spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
|
|
|
|
file->private_data = list;
|
2007-05-14 15:57:40 +08:00
|
|
|
out_unlock:
|
2008-10-31 07:07:23 +08:00
|
|
|
mutex_unlock(&minors_lock);
|
|
|
|
out:
|
2011-09-07 19:48:47 +08:00
|
|
|
if (err < 0)
|
|
|
|
kfree(list);
|
2007-05-14 15:57:40 +08:00
|
|
|
return err;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-28 11:02:27 +08:00
|
|
|
static int hidraw_fasync(int fd, struct file *file, int on)
|
|
|
|
{
|
|
|
|
struct hidraw_list *list = file->private_data;
|
|
|
|
|
|
|
|
return fasync_helper(fd, file, on, &list->fasync);
|
|
|
|
}
|
|
|
|
|
2013-07-22 18:03:13 +08:00
|
|
|
static void drop_ref(struct hidraw *hidraw, int exists_bit)
|
|
|
|
{
|
|
|
|
if (exists_bit) {
|
|
|
|
hidraw->exist = 0;
|
2013-10-01 18:09:00 +08:00
|
|
|
if (hidraw->open) {
|
|
|
|
hid_hw_close(hidraw->hid);
|
2013-07-22 18:03:13 +08:00
|
|
|
wake_up_interruptible(&hidraw->wait);
|
2013-10-01 18:09:00 +08:00
|
|
|
}
|
2014-02-26 15:51:24 +08:00
|
|
|
device_destroy(hidraw_class,
|
|
|
|
MKDEV(hidraw_major, hidraw->minor));
|
2013-07-22 18:03:13 +08:00
|
|
|
} else {
|
|
|
|
--hidraw->open;
|
|
|
|
}
|
2013-10-01 18:09:00 +08:00
|
|
|
if (!hidraw->open) {
|
|
|
|
if (!hidraw->exist) {
|
|
|
|
hidraw_table[hidraw->minor] = NULL;
|
|
|
|
kfree(hidraw);
|
|
|
|
} else {
|
|
|
|
/* close device for last reader */
|
|
|
|
hid_hw_close(hidraw->hid);
|
2017-09-20 09:37:46 +08:00
|
|
|
hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
|
2013-10-01 18:09:00 +08:00
|
|
|
}
|
2013-07-22 18:03:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-14 15:57:40 +08:00
|
|
|
static int hidraw_release(struct inode * inode, struct file * file)
|
|
|
|
{
|
|
|
|
unsigned int minor = iminor(inode);
|
|
|
|
struct hidraw_list *list = file->private_data;
|
2013-08-26 23:38:35 +08:00
|
|
|
unsigned long flags;
|
2012-11-01 18:33:26 +08:00
|
|
|
|
|
|
|
mutex_lock(&minors_lock);
|
2007-05-14 15:57:40 +08:00
|
|
|
|
2013-08-26 23:38:35 +08:00
|
|
|
spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
|
2007-05-14 15:57:40 +08:00
|
|
|
list_del(&list->node);
|
2013-08-26 23:38:35 +08:00
|
|
|
spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
|
2008-06-24 20:45:27 +08:00
|
|
|
kfree(list);
|
2012-11-01 18:33:26 +08:00
|
|
|
|
2013-07-22 18:03:13 +08:00
|
|
|
drop_ref(hidraw_table[minor], 0);
|
|
|
|
|
|
|
|
mutex_unlock(&minors_lock);
|
|
|
|
return 0;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
|
2008-05-26 17:25:26 +08:00
|
|
|
static long hidraw_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
2007-05-14 15:57:40 +08:00
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
struct inode *inode = file_inode(file);
|
2007-05-14 15:57:40 +08:00
|
|
|
unsigned int minor = iminor(inode);
|
2008-05-26 17:25:26 +08:00
|
|
|
long ret = 0;
|
2010-03-25 21:15:11 +08:00
|
|
|
struct hidraw *dev;
|
2007-05-14 15:57:40 +08:00
|
|
|
void __user *user_arg = (void __user*) arg;
|
|
|
|
|
2010-03-25 21:15:11 +08:00
|
|
|
mutex_lock(&minors_lock);
|
|
|
|
dev = hidraw_table[minor];
|
2019-08-22 01:27:12 +08:00
|
|
|
if (!dev || !dev->exist) {
|
2010-10-05 23:20:16 +08:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-03-25 21:15:11 +08:00
|
|
|
|
2007-05-14 15:57:40 +08:00
|
|
|
switch (cmd) {
|
|
|
|
case HIDIOCGRDESCSIZE:
|
|
|
|
if (put_user(dev->hid->rsize, (int __user *)arg))
|
2008-05-26 17:25:26 +08:00
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
2007-05-14 15:57:40 +08:00
|
|
|
|
|
|
|
case HIDIOCGRDESC:
|
|
|
|
{
|
|
|
|
__u32 len;
|
|
|
|
|
|
|
|
if (get_user(len, (int __user *)arg))
|
2008-05-26 17:25:26 +08:00
|
|
|
ret = -EFAULT;
|
|
|
|
else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
|
|
|
|
ret = -EINVAL;
|
|
|
|
else if (copy_to_user(user_arg + offsetof(
|
|
|
|
struct hidraw_report_descriptor,
|
|
|
|
value[0]),
|
|
|
|
dev->hid->rdesc,
|
|
|
|
min(dev->hid->rsize, len)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
case HIDIOCGRAWINFO:
|
|
|
|
{
|
|
|
|
struct hidraw_devinfo dinfo;
|
|
|
|
|
|
|
|
dinfo.bustype = dev->hid->bus;
|
|
|
|
dinfo.vendor = dev->hid->vendor;
|
|
|
|
dinfo.product = dev->hid->product;
|
|
|
|
if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
|
2008-05-26 17:25:26 +08:00
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
default:
|
2008-11-12 23:14:08 +08:00
|
|
|
{
|
|
|
|
struct hid_device *hid = dev->hid;
|
2011-01-18 16:04:39 +08:00
|
|
|
if (_IOC_TYPE(cmd) != 'H') {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
|
|
|
|
int len = _IOC_SIZE(cmd);
|
|
|
|
ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
|
|
|
|
int len = _IOC_SIZE(cmd);
|
|
|
|
ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Begin Read-only ioctls. */
|
|
|
|
if (_IOC_DIR(cmd) != _IOC_READ) {
|
2009-02-03 21:35:17 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-12 23:14:08 +08:00
|
|
|
|
|
|
|
if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
|
2011-05-16 00:07:42 +08:00
|
|
|
int len = strlen(hid->name) + 1;
|
2008-11-12 23:14:08 +08:00
|
|
|
if (len > _IOC_SIZE(cmd))
|
|
|
|
len = _IOC_SIZE(cmd);
|
2009-02-03 21:35:17 +08:00
|
|
|
ret = copy_to_user(user_arg, hid->name, len) ?
|
2008-11-12 23:14:08 +08:00
|
|
|
-EFAULT : len;
|
2009-02-03 21:35:17 +08:00
|
|
|
break;
|
2008-11-12 23:14:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
|
2011-05-16 00:07:42 +08:00
|
|
|
int len = strlen(hid->phys) + 1;
|
2008-11-12 23:14:08 +08:00
|
|
|
if (len > _IOC_SIZE(cmd))
|
|
|
|
len = _IOC_SIZE(cmd);
|
2009-02-03 21:35:17 +08:00
|
|
|
ret = copy_to_user(user_arg, hid->phys, len) ?
|
2008-11-12 23:14:08 +08:00
|
|
|
-EFAULT : len;
|
2009-02-03 21:35:17 +08:00
|
|
|
break;
|
2008-11-12 23:14:08 +08:00
|
|
|
}
|
2011-01-18 16:04:39 +08:00
|
|
|
}
|
2008-11-12 23:14:08 +08:00
|
|
|
|
2009-02-03 21:35:17 +08:00
|
|
|
ret = -ENOTTY;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
2010-10-05 23:20:16 +08:00
|
|
|
out:
|
2010-03-25 21:15:11 +08:00
|
|
|
mutex_unlock(&minors_lock);
|
2008-05-26 17:25:26 +08:00
|
|
|
return ret;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations hidraw_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.read = hidraw_read,
|
|
|
|
.write = hidraw_write,
|
|
|
|
.poll = hidraw_poll,
|
|
|
|
.open = hidraw_open,
|
|
|
|
.release = hidraw_release,
|
2008-05-26 17:25:26 +08:00
|
|
|
.unlocked_ioctl = hidraw_ioctl,
|
2012-11-28 11:02:27 +08:00
|
|
|
.fasync = hidraw_fasync,
|
2011-01-04 13:37:22 +08:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = hidraw_ioctl,
|
|
|
|
#endif
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = noop_llseek,
|
2007-05-14 15:57:40 +08:00
|
|
|
};
|
|
|
|
|
2012-04-27 06:56:08 +08:00
|
|
|
int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
|
2007-05-14 15:57:40 +08:00
|
|
|
{
|
|
|
|
struct hidraw *dev = hid->hidraw;
|
|
|
|
struct hidraw_list *list;
|
2012-04-27 06:56:08 +08:00
|
|
|
int ret = 0;
|
2013-08-26 23:38:35 +08:00
|
|
|
unsigned long flags;
|
2007-05-14 15:57:40 +08:00
|
|
|
|
2013-08-26 23:38:35 +08:00
|
|
|
spin_lock_irqsave(&dev->list_lock, flags);
|
2007-05-14 15:57:40 +08:00
|
|
|
list_for_each_entry(list, &dev->list, node) {
|
2012-06-28 22:51:56 +08:00
|
|
|
int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
if (new_head == list->tail)
|
|
|
|
continue;
|
|
|
|
|
2012-04-27 06:56:08 +08:00
|
|
|
if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
2007-05-14 15:57:40 +08:00
|
|
|
list->buffer[list->head].len = len;
|
2012-06-28 22:51:56 +08:00
|
|
|
list->head = new_head;
|
2007-05-14 15:57:40 +08:00
|
|
|
kill_fasync(&list->fasync, SIGIO, POLL_IN);
|
|
|
|
}
|
2013-08-26 23:38:35 +08:00
|
|
|
spin_unlock_irqrestore(&dev->list_lock, flags);
|
2007-05-14 15:57:40 +08:00
|
|
|
|
|
|
|
wake_up_interruptible(&dev->wait);
|
2012-04-27 06:56:08 +08:00
|
|
|
return ret;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(hidraw_report_event);
|
|
|
|
|
|
|
|
int hidraw_connect(struct hid_device *hid)
|
|
|
|
{
|
2007-09-27 17:24:55 +08:00
|
|
|
int minor, result;
|
2007-05-14 15:57:40 +08:00
|
|
|
struct hidraw *dev;
|
|
|
|
|
2014-01-06 23:43:27 +08:00
|
|
|
/* we accept any HID device, all applications */
|
2007-05-14 15:57:40 +08:00
|
|
|
|
2007-09-27 17:24:55 +08:00
|
|
|
dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
|
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
result = -EINVAL;
|
2007-05-14 15:57:40 +08:00
|
|
|
|
2008-10-31 07:07:23 +08:00
|
|
|
mutex_lock(&minors_lock);
|
2007-05-14 15:57:40 +08:00
|
|
|
|
|
|
|
for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
|
|
|
|
if (hidraw_table[minor])
|
|
|
|
continue;
|
|
|
|
hidraw_table[minor] = dev;
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-09-27 17:24:55 +08:00
|
|
|
if (result) {
|
2008-10-31 07:07:23 +08:00
|
|
|
mutex_unlock(&minors_lock);
|
2007-09-27 17:24:55 +08:00
|
|
|
kfree(dev);
|
2007-05-14 15:57:40 +08:00
|
|
|
goto out;
|
2007-09-27 17:24:55 +08:00
|
|
|
}
|
2007-05-14 15:57:40 +08:00
|
|
|
|
2008-12-04 23:16:46 +08:00
|
|
|
dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
|
2008-07-22 11:03:34 +08:00
|
|
|
NULL, "%s%d", "hidraw", minor);
|
2007-05-14 15:57:40 +08:00
|
|
|
|
|
|
|
if (IS_ERR(dev->dev)) {
|
|
|
|
hidraw_table[minor] = NULL;
|
2008-10-31 07:07:23 +08:00
|
|
|
mutex_unlock(&minors_lock);
|
2007-05-14 15:57:40 +08:00
|
|
|
result = PTR_ERR(dev->dev);
|
2007-09-27 17:24:55 +08:00
|
|
|
kfree(dev);
|
2007-05-14 15:57:40 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_waitqueue_head(&dev->wait);
|
2013-08-26 23:38:35 +08:00
|
|
|
spin_lock_init(&dev->list_lock);
|
2007-05-14 15:57:40 +08:00
|
|
|
INIT_LIST_HEAD(&dev->list);
|
|
|
|
|
|
|
|
dev->hid = hid;
|
|
|
|
dev->minor = minor;
|
|
|
|
|
|
|
|
dev->exist = 1;
|
|
|
|
hid->hidraw = dev;
|
|
|
|
|
2013-07-30 14:16:10 +08:00
|
|
|
mutex_unlock(&minors_lock);
|
2007-05-14 15:57:40 +08:00
|
|
|
out:
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(hidraw_connect);
|
|
|
|
|
|
|
|
void hidraw_disconnect(struct hid_device *hid)
|
|
|
|
{
|
|
|
|
struct hidraw *hidraw = hid->hidraw;
|
2012-11-01 18:33:26 +08:00
|
|
|
|
|
|
|
mutex_lock(&minors_lock);
|
|
|
|
|
2013-07-22 18:03:13 +08:00
|
|
|
drop_ref(hidraw, 1);
|
2012-11-01 18:33:26 +08:00
|
|
|
|
|
|
|
mutex_unlock(&minors_lock);
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(hidraw_disconnect);
|
|
|
|
|
|
|
|
int __init hidraw_init(void)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
dev_t dev_id;
|
|
|
|
|
|
|
|
result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
|
|
|
|
HIDRAW_MAX_DEVICES, "hidraw");
|
|
|
|
if (result < 0) {
|
2010-12-10 11:29:03 +08:00
|
|
|
pr_warn("can't get major number\n");
|
2007-05-14 15:57:40 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-04-02 12:45:01 +08:00
|
|
|
hidraw_major = MAJOR(dev_id);
|
|
|
|
|
2007-05-14 15:57:40 +08:00
|
|
|
hidraw_class = class_create(THIS_MODULE, "hidraw");
|
|
|
|
if (IS_ERR(hidraw_class)) {
|
|
|
|
result = PTR_ERR(hidraw_class);
|
2012-08-16 03:31:45 +08:00
|
|
|
goto error_cdev;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cdev_init(&hidraw_cdev, &hidraw_ops);
|
2012-08-16 03:31:45 +08:00
|
|
|
result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
|
|
|
|
if (result < 0)
|
|
|
|
goto error_class;
|
|
|
|
|
2013-02-19 21:05:34 +08:00
|
|
|
printk(KERN_INFO "hidraw: raw HID events driver (C) Jiri Kosina\n");
|
2007-05-14 15:57:40 +08:00
|
|
|
out:
|
|
|
|
return result;
|
2012-08-16 03:31:45 +08:00
|
|
|
|
|
|
|
error_class:
|
|
|
|
class_destroy(hidraw_class);
|
|
|
|
error_cdev:
|
|
|
|
unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
|
|
|
|
goto out;
|
2007-05-14 15:57:40 +08:00
|
|
|
}
|
|
|
|
|
2008-10-18 00:04:48 +08:00
|
|
|
void hidraw_exit(void)
|
2007-05-14 15:57:40 +08:00
|
|
|
{
|
|
|
|
dev_t dev_id = MKDEV(hidraw_major, 0);
|
|
|
|
|
|
|
|
cdev_del(&hidraw_cdev);
|
|
|
|
class_destroy(hidraw_class);
|
|
|
|
unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
|
|
|
|
|
|
|
|
}
|