From patchwork Thu Apr 3 23:34:08 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?RGF2aWQgSMODwqRyZGVtYW4=?= X-Patchwork-Id: 3934991 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id F34CF9F334 for ; Thu, 3 Apr 2014 23:34:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E56A620265 for ; Thu, 3 Apr 2014 23:34:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DCC1020158 for ; Thu, 3 Apr 2014 23:34:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753941AbaDCXeM (ORCPT ); Thu, 3 Apr 2014 19:34:12 -0400 Received: from hardeman.nu ([95.142.160.32]:40331 "EHLO hardeman.nu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753854AbaDCXeJ (ORCPT ); Thu, 3 Apr 2014 19:34:09 -0400 Received: from zeus.hardeman.nu (unknown [IPv6:2001:a60:16f7:7000:b8ab:ce09:22ee:d164]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by vader.hardeman.nu (Postfix) with ESMTPSA id 5CC45321; Fri, 4 Apr 2014 01:34:08 +0200 (CEST) Received: by zeus.hardeman.nu (Postfix, from userid 1000) id 0391CC600FA; Fri, 4 Apr 2014 01:34:08 +0200 (CEST) Subject: [PATCH 34/49] rc-core: add ioctls for adding/removing keytables from userspace From: David =?utf-8?b?SMOkcmRlbWFu?= To: linux-media@vger.kernel.org Cc: m.chehab@samsung.com Date: Fri, 04 Apr 2014 01:34:08 +0200 Message-ID: <20140403233407.27099.87278.stgit@zeus.muc.hardeman.nu> In-Reply-To: <20140403232420.27099.94872.stgit@zeus.muc.hardeman.nu> References: <20140403232420.27099.94872.stgit@zeus.muc.hardeman.nu> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP As all the basics are now in place, we can finally add the ioctls for adding/removing keytables from userspace. Signed-off-by: David Härdeman --- drivers/media/rc/rc-core-priv.h | 2 - drivers/media/rc/rc-keytable.c | 5 ++ drivers/media/rc/rc-main.c | 81 ++++++++++++++++++++++++++++++++++++--- include/media/rc-core.h | 27 +++++++++++++ 4 files changed, 106 insertions(+), 9 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-media" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/media/rc/rc-core-priv.h b/drivers/media/rc/rc-core-priv.h index 02b538c..0159836 100644 --- a/drivers/media/rc/rc-core-priv.h +++ b/drivers/media/rc/rc-core-priv.h @@ -162,7 +162,7 @@ void rc_keytable_keyup(struct rc_keytable *kt); void rc_keytable_repeat(struct rc_keytable *kt); void rc_keytable_keydown(struct rc_keytable *kt, enum rc_type protocol, u32 scancode, u8 toggle, bool autokeyup); -struct rc_keytable *rc_keytable_create(struct rc_dev *dev, struct rc_map *rc_map); +struct rc_keytable *rc_keytable_create(struct rc_dev *dev, const char *name, struct rc_map *rc_map); void rc_keytable_destroy(struct rc_keytable *kt); /* Only to be used by rc-core and ir-lirc-codec */ diff --git a/drivers/media/rc/rc-keytable.c b/drivers/media/rc/rc-keytable.c index 412d342..6d04b8f 100644 --- a/drivers/media/rc/rc-keytable.c +++ b/drivers/media/rc/rc-keytable.c @@ -825,6 +825,7 @@ static void rc_input_close(struct input_dev *idev) /** * rc_keytable_create() - create a new keytable * @dev: the struct rc_dev device this keytable should belong to + * @name: the userfriendly name of this keymap * @rc_map: the keymap to use for the new keytable * @return: zero on success or a negative error code * @@ -832,7 +833,8 @@ static void rc_input_close(struct input_dev *idev) * keytable and an input device along with some state (whether a key is * currently pressed or not, etc). */ -struct rc_keytable *rc_keytable_create(struct rc_dev *dev, struct rc_map *rc_map) +struct rc_keytable *rc_keytable_create(struct rc_dev *dev, const char *name, + struct rc_map *rc_map) { struct rc_keytable *kt; struct input_dev *idev = NULL; @@ -854,6 +856,7 @@ struct rc_keytable *rc_keytable_create(struct rc_dev *dev, struct rc_map *rc_map kt->dev = dev; spin_lock_init(&kt->keylock); spin_lock_init(&kt->rc_map.lock); + snprintf(kt->name, sizeof(*kt->name), name ? name : "undefined"); idev->getkeycode = ir_getkeycode; idev->setkeycode = ir_setkeycode; idev->open = rc_input_open; diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c index bc2d479..ad784c8 100644 --- a/drivers/media/rc/rc-main.c +++ b/drivers/media/rc/rc-main.c @@ -201,7 +201,8 @@ void rc_repeat(struct rc_dev *dev) } EXPORT_SYMBOL_GPL(rc_repeat); -static int rc_add_keytable(struct rc_dev *dev, struct rc_map *rc_map) +static int rc_add_keytable(struct rc_dev *dev, const char *name, + struct rc_map *rc_map) { struct rc_keytable *kt; unsigned i; @@ -219,7 +220,7 @@ static int rc_add_keytable(struct rc_dev *dev, struct rc_map *rc_map) if (i >= ARRAY_SIZE(dev->keytables)) return -ENFILE; - kt = rc_keytable_create(dev, rc_map); + kt = rc_keytable_create(dev, name, rc_map); if (IS_ERR(kt)) return PTR_ERR(kt); @@ -229,19 +230,24 @@ static int rc_add_keytable(struct rc_dev *dev, struct rc_map *rc_map) return 0; } -static void rc_remove_keytable(struct rc_dev *dev, unsigned i) +static int rc_remove_keytable(struct rc_dev *dev, unsigned i) { struct rc_keytable *kt; if (i >= ARRAY_SIZE(dev->keytables)) - return; + return -EINVAL; kt = dev->keytables[i]; rcu_assign_pointer(dev->keytables[i], NULL); if (kt) list_del_rcu(&kt->node); synchronize_rcu(); + + if (!kt) + return -EINVAL; + rc_keytable_destroy(kt); + return 0; } /* class for /sys/class/rc */ @@ -946,6 +952,8 @@ static long rc_do_ioctl(struct rc_dev *dev, unsigned int cmd, unsigned long arg) unsigned int __user *ip = (unsigned int __user *)p; struct rc_ir_rx rx; struct rc_ir_tx tx; + struct rc_keytable_ioctl ktio; + struct rc_keytable *kt; int error; switch (cmd) { @@ -1007,8 +1015,69 @@ static long rc_do_ioctl(struct rc_dev *dev, unsigned int cmd, unsigned long arg) return -EFAULT; return 0; - } + case RCIOCADDTABLE: + if (copy_from_user(&ktio, p, sizeof(ktio))) + return -EFAULT; + + if (ktio.id >= RC_MAX_KEYTABLES) + return -EINVAL; + + if (ktio.flags) + return -EINVAL; + + ktio.name[sizeof(ktio.name) - 1] = '\0'; + if (strlen(ktio.name) < 1) + return -EINVAL; + + error = rc_add_keytable(dev, ktio.name, NULL); + if (error < 0) + return error; + ktio.id = error; + + if (copy_to_user(p, &ktio, sizeof(ktio))) + return -EFAULT; + + return 0; + + case RCIOCGTABLENAME: + if (copy_from_user(&ktio, p, sizeof(ktio))) + return -EFAULT; + + if (ktio.id >= RC_MAX_KEYTABLES) + return -EINVAL; + + if (ktio.flags) + return -EINVAL; + + rcu_read_lock(); + kt = rcu_dereference(dev->keytables[ktio.id]); + if (kt) { + ktio.name[0] = '\0'; + strncat(ktio.name, kt->name, sizeof(ktio.name)); + } + rcu_read_unlock(); + + if (!kt) + return -EINVAL; + + if (copy_to_user(p, &ktio, sizeof(ktio))) + return -EFAULT; + + return 0; + + case RCIOCDELTABLE: + if (copy_from_user(&ktio, p, sizeof(ktio))) + return -EFAULT; + + if (ktio.id >= RC_MAX_KEYTABLES) + return -EINVAL; + + if (ktio.flags) + return -EINVAL; + + return rc_remove_keytable(dev, ktio.id); + } return -EINVAL; } @@ -1276,7 +1345,7 @@ int rc_register_device(struct rc_dev *dev) if (rc) goto out_cdev; - rc = rc_add_keytable(dev, rc_map); + rc = rc_add_keytable(dev, dev->map_name, rc_map); if (rc) goto out_dev; diff --git a/include/media/rc-core.h b/include/media/rc-core.h index f48d5cd..6f282e6 100644 --- a/include/media/rc-core.h +++ b/include/media/rc-core.h @@ -153,6 +153,29 @@ struct rc_ir_tx { __u32 reserved[9]; } __packed; +/* add a keytable */ +#define RCIOCADDTABLE _IOC(_IOC_READ | _IOC_WRITE, RC_IOC_MAGIC, 0x06, sizeof(struct rc_keytable_ioctl)) + +/* get the name of a keytable */ +#define RCIOCGTABLENAME _IOC(_IOC_READ, RC_IOC_MAGIC, 0x06, sizeof(struct rc_keytable_ioctl)) + +/* remove a keytable */ +#define RCIOCDELTABLE _IOC(_IOC_WRITE, RC_IOC_MAGIC, 0x06, sizeof(struct rc_keytable_ioctl)) + +/** + * struct rc_keytable_ioctl - used to alter keytables + * @id: the id of the keytable + * @flags: flags for the keytable + * @reserved: for future use, set to zero + * @name: a user-friendly name for the keytable + */ +#define RC_KEYTABLE_NAME_SIZE 128 +struct rc_keytable_ioctl { + __u32 id; + __u32 flags; + __u32 reserved[4]; + char name[RC_KEYTABLE_NAME_SIZE]; +} __packed; enum rc_driver_type { RC_DRIVER_SCANCODE = 0, /* Driver or hardware generates a scancode */ @@ -303,7 +326,7 @@ enum rc_filter_type { * @get_ir_tx: allow driver to provide tx settings * @set_ir_tx: allow driver to change tx settings */ -#define RC_MAX_KEYTABLES 1 +#define RC_MAX_KEYTABLES 32 struct rc_dev { struct device dev; struct cdev cdev; @@ -367,6 +390,7 @@ struct rc_dev { * @node: used to iterate over all keytables for a rc_dev device * @dev: the rc_dev device this keytable belongs to * @idev: the input_dev device which belongs to this keytable + * @name: the user-friendly name of this keytable * @rc_map: holds the scancode <-> keycode mappings * @keypressed: whether a key is currently pressed or not * @keyup_jiffies: when the key should be auto-released @@ -381,6 +405,7 @@ struct rc_keytable { struct list_head node; struct rc_dev *dev; struct input_dev *idev; + char name[RC_KEYTABLE_NAME_SIZE]; struct rc_map rc_map; bool keypressed; unsigned long keyup_jiffies;