From patchwork Fri Aug 29 08:35:27 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dudley Du X-Patchwork-Id: 4809311 Return-Path: X-Original-To: patchwork-linux-input@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 58F089F375 for ; Fri, 29 Aug 2014 08:41:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5A9A420123 for ; Fri, 29 Aug 2014 08:41:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 38DF82011E for ; Fri, 29 Aug 2014 08:41:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753102AbaH2IhM (ORCPT ); Fri, 29 Aug 2014 04:37:12 -0400 Received: from mail-pd0-f182.google.com ([209.85.192.182]:34237 "EHLO mail-pd0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753310AbaH2IhG convert rfc822-to-8bit (ORCPT ); Fri, 29 Aug 2014 04:37:06 -0400 Received: by mail-pd0-f182.google.com with SMTP id fp1so7391pdb.27 for ; Fri, 29 Aug 2014 01:37:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:from:to:cc:subject:date:mime-version:content-type :content-transfer-encoding:thread-index:content-language; bh=X1x2ivWKCowNVpsO420oteSfy+Fmbnob9wa9QKU413g=; b=txGOLJnaz/Fqkqtnhgj+ZwnrfKj3Mkv6/5b2ipLCzDT8tZWSwltKoYUBSXzX+gWvgH xOp8KcY+9JGwx3nEB4pb61DtHeYV5LSOHSJvvhU4VYVEz4wDGxzcNCLk0cTws4JMlsBH 0GYoMbh1UQ3xk4Mnnakc+sYMGfxfVBu0fVYxwxlPwckurYfiz0uWGdvocbS/aJUWUjLi uv3iClVLwOAFMEIG94OsssdHCL1NUsakNqumnDubUDtT7J32Yyt9b8OfUdPMWX08vREA nYq7mUAvV2Gp4yyk9TpWPSarz62zSNQqKbnc0dCJh33v5dtGO5zUaqt6rY/rrkTyl+FJ JPiA== X-Received: by 10.70.94.228 with SMTP id df4mr11957065pdb.64.1409301426263; Fri, 29 Aug 2014 01:37:06 -0700 (PDT) Received: from dudllaptop ([140.207.206.26]) by mx.google.com with ESMTPSA id h12sm8970709pdk.48.2014.08.29.01.37.00 for (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 29 Aug 2014 01:37:05 -0700 (PDT) Message-ID: <54003bb1.ac26460a.73fc.ffffe977@mx.google.com> X-Google-Original-Message-ID: <000901cfc364$693dafb0$3bb90f10$@dulixin@gmail.com> From: "Dudley Du" To: , Cc: , , , Subject: [PATCH V5 05/14] input: cyapa: add read firmware image and raw data interfaces in debugfs system Date: Fri, 29 Aug 2014 16:35:27 +0800 MIME-Version: 1.0 X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Ac/DWH3WZ0XrQcPbQp2lAwHFQ3BZvQ== Content-Language: zh-cn Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable 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 Add read_fw and raw_data debugfs interfaces for easier issues location and collection when report by user. TEST=test on Chromebooks. Signed-off-by: Dudley Du --- drivers/input/mouse/cyapa.c | 221 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) diff --git a/drivers/input/mouse/cyapa.c b/drivers/input/mouse/cyapa.c index 96f28e7..d588938 100644 --- a/drivers/input/mouse/cyapa.c +++ b/drivers/input/mouse/cyapa.c @@ -39,6 +39,8 @@ const char unique_str[] = "CYTRA"; +/* Global root node of the cyapa debugfs directory. */ +static struct dentry *cyapa_debugfs_root; ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len, @@ -464,6 +466,205 @@ done: } /* + ************************************************************** + * debugfs interface + ************************************************************** +*/ +static int cyapa_debugfs_open(struct inode *inode, struct file *file) +{ + struct cyapa *cyapa = inode->i_private; + int ret; + + if (!cyapa) + return -ENODEV; + + ret = mutex_lock_interruptible(&cyapa->debugfs_mutex); + if (ret) + return ret; + + if (!get_device(&cyapa->client->dev)) { + ret = -ENODEV; + goto out; + } + + file->private_data = cyapa; + + if (cyapa->fw_image) { + ret = 0; + goto out; + } + + mutex_lock(&cyapa->state_sync_lock); + /* + * If firmware hasn't been read yet, read it all in one pass. + * Subsequent opens will reuse the data in this same buffer. + */ + if (cyapa->ops->read_fw) + ret = cyapa->ops->read_fw(cyapa); + else + ret = -EPERM; + mutex_unlock(&cyapa->state_sync_lock); + + /* Redetect trackpad device states. */ + cyapa_detect_async(cyapa, 0); + +out: + mutex_unlock(&cyapa->debugfs_mutex); + return ret; +} + +static int cyapa_debugfs_release(struct inode *inode, struct file *file) +{ + struct cyapa *cyapa = file->private_data; + int ret; + + if (!cyapa) + return 0; + + ret = mutex_lock_interruptible(&cyapa->debugfs_mutex); + if (ret) + return ret; + file->private_data = NULL; + put_device(&cyapa->client->dev); + mutex_unlock(&cyapa->debugfs_mutex); + + return 0; +} + +/* Return some bytes from the buffered firmware image, starting from *ppos */ +static ssize_t cyapa_debugfs_read_fw(struct file *file, char __user *buffer, + size_t count, loff_t *ppos) +{ + struct cyapa *cyapa = file->private_data; + + if (!cyapa->fw_image) + return -EINVAL; + + if (*ppos >= cyapa->fw_image_size) + return 0; + + if (count + *ppos > cyapa->fw_image_size) + count = cyapa->fw_image_size - *ppos; + + if (copy_to_user(buffer, &cyapa->fw_image[*ppos], count)) + return -EFAULT; + + *ppos += count; + return count; +} + +static const struct file_operations cyapa_read_fw_fops = { + .open = cyapa_debugfs_open, + .release = cyapa_debugfs_release, + .read = cyapa_debugfs_read_fw +}; + +static int cyapa_debugfs_raw_data_open(struct inode *inode, struct file *file) +{ + struct cyapa *cyapa = inode->i_private; + int ret; + + if (!cyapa) + return -ENODEV; + + /* Start to be supported after Gen5 trackpad devices. */ + if (cyapa->gen < CYAPA_GEN5) + return -ENOTSUPP; + + ret = mutex_lock_interruptible(&cyapa->debugfs_mutex); + if (ret) + return ret; + + if (!get_device(&cyapa->client->dev)) { + ret = -ENODEV; + goto out; + } + + file->private_data = cyapa; + + mutex_lock(&cyapa->state_sync_lock); + if (cyapa->ops->read_raw_data) + ret = cyapa->ops->read_raw_data(cyapa); + else + ret = -EPERM; + mutex_unlock(&cyapa->state_sync_lock); +out: + mutex_unlock(&cyapa->debugfs_mutex); + return ret; +} + +static int cyapa_debugfs_raw_data_release(struct inode *inode, + struct file *file) +{ + struct cyapa *cyapa = file->private_data; + int ret; + + if (!cyapa) + return 0; + + ret = mutex_lock_interruptible(&cyapa->debugfs_mutex); + if (ret) + return ret; + file->private_data = NULL; + put_device(&cyapa->client->dev); + mutex_unlock(&cyapa->debugfs_mutex); + + return 0; +} + +/* Always return the sensors' latest raw data from trackpad device. */ +static ssize_t cyapa_debugfs_read_raw_data(struct file *file, + char __user *buffer, + size_t count, loff_t *ppos) +{ + struct cyapa *cyapa = file->private_data; + + if (!cyapa->tp_raw_data) + return -EINVAL; + + if (*ppos >= cyapa->tp_raw_data_size) + return 0; + + if (count + *ppos > cyapa->tp_raw_data_size) + count = cyapa->tp_raw_data_size - *ppos; + + if (copy_to_user(buffer, &cyapa->tp_raw_data[*ppos], count)) + return -EFAULT; + + *ppos += count; + return count; +} + +static const struct file_operations cyapa_read_raw_data_fops = { + .open = cyapa_debugfs_raw_data_open, + .release = cyapa_debugfs_raw_data_release, + .read = cyapa_debugfs_read_raw_data +}; + +static int cyapa_debugfs_init(struct cyapa *cyapa) +{ + struct device *dev = &cyapa->client->dev; + + if (!cyapa_debugfs_root) + return -ENODEV; + + cyapa->dentry_dev = debugfs_create_dir(kobject_name(&dev->kobj), + cyapa_debugfs_root); + + if (!cyapa->dentry_dev) + return -ENODEV; + + mutex_init(&cyapa->debugfs_mutex); + + debugfs_create_file(CYAPA_DEBUGFS_READ_FW, S_IRUSR, cyapa->dentry_dev, + cyapa, &cyapa_read_fw_fops); + + debugfs_create_file(CYAPA_DEBUGFS_RAW_DATA, S_IRUSR, cyapa->dentry_dev, + cyapa, &cyapa_read_raw_data_fops); + return 0; +} + +/* * Sysfs Interface. */ @@ -881,6 +1082,9 @@ static int cyapa_probe(struct i2c_client *client, if (sysfs_create_group(&client->dev.kobj, &cyapa_sysfs_group)) dev_warn(dev, "error creating sysfs entries.\n"); + if (cyapa_debugfs_init(cyapa)) + dev_warn(dev, "error creating debugfs entries.\n"); + #ifdef CONFIG_PM_SLEEP if (device_can_wakeup(dev) && sysfs_merge_group(&client->dev.kobj, &cyapa_power_wakeup_group)) @@ -922,6 +1126,16 @@ static int cyapa_remove(struct i2c_client *client) free_irq(cyapa->irq, cyapa); + debugfs_remove_recursive(cyapa->dentry_dev); + mutex_destroy(&cyapa->debugfs_mutex); + + kfree(cyapa->fw_image); + cyapa->fw_image = NULL; + cyapa->fw_image_size = 0; + kfree(cyapa->tp_raw_data); + cyapa->tp_raw_data = NULL; + cyapa->tp_raw_data_size = 0; + input_unregister_device(cyapa->input); if (cyapa->ops->set_power_mode) cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0); @@ -1058,6 +1272,11 @@ static int __init cyapa_init(void) { int ret; + /* Create a global debugfs root for all cyapa devices */ + cyapa_debugfs_root = debugfs_create_dir("cyapa", NULL); + if (cyapa_debugfs_root == ERR_PTR(-ENODEV)) + cyapa_debugfs_root = NULL; + ret = i2c_add_driver(&cyapa_driver); if (ret) { pr_err("cyapa driver register FAILED.\n"); @@ -1069,6 +1288,8 @@ static int __init cyapa_init(void) static void __exit cyapa_exit(void) { + debugfs_remove_recursive(cyapa_debugfs_root); + i2c_del_driver(&cyapa_driver); }