From patchwork Tue Jan 15 12:55:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 1977771 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork2.kernel.org (Postfix) with ESMTP id DC387DF5B1 for ; Tue, 15 Jan 2013 13:06:44 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1Tv689-000826-6w; Tue, 15 Jan 2013 13:00:45 +0000 Received: from mail-wg0-x229.google.com ([2a00:1450:400c:c00::229]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1Tv64I-0006aY-QX for linux-arm-kernel@lists.infradead.org; Tue, 15 Jan 2013 12:56:45 +0000 Received: by mail-wg0-f41.google.com with SMTP id ds1so1848769wgb.0 for ; Tue, 15 Jan 2013 04:56:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=vbW5wvqBDPz7I1/s8nba5FRzI2pejG280z+/zRhrOrM=; b=MZmHMVIthq3rKZMdmsua6tyt8f1Mb3rhDs4J/jriCByMtXCgquWhpARaODibCQXIRb vec6zIeE+Oc5LIPSabPlKIY/jfC/o124KxCdjNSi214dutGxum5d3lTCZlOh2j5PJz1t +sTBcSIyL9vWpBp8H9kC0udSuhkGa4XZu7VGQuzte2qmJWR7kWBh+GU7HZKE/dfXEB+t umMxhZXWJu7ZrlboHFkHQz4IwmAggKKl9MOIx1fuvo/a+Ox60jfar81rKrC7x+TQx16j xjYvCmPVPCyRbnmi7ixHBJ8y+Zfz81k3BZEUXRVS4uGibU4AXhlcYgbuSNgi2UXYQzWB NJRg== X-Received: by 10.180.95.228 with SMTP id dn4mr3585326wib.16.1358254600646; Tue, 15 Jan 2013 04:56:40 -0800 (PST) Received: from localhost.localdomain (cpc1-aztw13-0-0-cust473.18-1.cable.virginmedia.com. [77.102.241.218]) by mx.google.com with ESMTPS id eo10sm3672833wib.9.2013.01.15.04.56.39 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 15 Jan 2013 04:56:39 -0800 (PST) From: Lee Jones To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 09/26] mfd: ab8500-debugfs: Provide a means for a user subscribe to IRQs Date: Tue, 15 Jan 2013 12:55:49 +0000 Message-Id: <1358254566-12419-10-git-send-email-lee.jones@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1358254566-12419-1-git-send-email-lee.jones@linaro.org> References: <1358254566-12419-1-git-send-email-lee.jones@linaro.org> X-Gm-Message-State: ALoCoQlClUmexAh841aCjv0NihmOOHdOA2IX/19Qc2F5xJ0m90iny8uMm6g715AT0PX9zJgq/ofm X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130115_075643_607526_85C3F2BE X-CRM114-Status: GOOD ( 21.20 ) X-Spam-Score: -1.9 (-) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-1.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Lee Jones , linus.walleij@stericsson.com, arnd@arndb.de, sameo@linux.intel.com X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Allow users to subscribe to and view IRQ events live from debugfs. Signed-off-by: Lee Jones --- drivers/mfd/ab8500-debugfs.c | 197 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) diff --git a/drivers/mfd/ab8500-debugfs.c b/drivers/mfd/ab8500-debugfs.c index c4cb806..71df8e4 100644 --- a/drivers/mfd/ab8500-debugfs.c +++ b/drivers/mfd/ab8500-debugfs.c @@ -11,6 +11,9 @@ #include #include #include +#include +#include +#include #include #include @@ -18,6 +21,9 @@ static u32 debug_bank; static u32 debug_address; +static int irq_first; +static int irq_last; + /** * struct ab8500_reg_range * @first: the first address of the range @@ -354,6 +360,21 @@ static struct ab8500_prcmu_ranges debug_ranges[AB8500_NUM_BANKS] = { }, }; +static irqreturn_t ab8500_debug_handler(int irq, void *data) +{ + char buf[16]; + struct kobject *kobj = (struct kobject *)data; + + /* + * This makes it possible to use poll for events (POLLPRI | POLLERR) + * from userspace on sysfs file named irq- + */ + sprintf(buf, "irq-%d", irq); + sysfs_notify(kobj, NULL, buf); + + return IRQ_HANDLED; +} + static int ab8500_registers_print(struct seq_file *s, void *p) { struct device *dev = s->private; @@ -519,6 +540,130 @@ static ssize_t ab8500_val_write(struct file *file, return count; } +static int ab8500_subscribe_unsubscribe_print(struct seq_file *s, void *p) +{ + seq_printf(s, "%d\n", irq_first); + + return 0; +} + +static int ab8500_subscribe_unsubscribe_open(struct inode *inode, + struct file *file) +{ + return single_open(file, ab8500_subscribe_unsubscribe_print, + inode->i_private); +} + +/* + * This function is used for all interrupts and will always print + * the same string. It is however this file sysfs_notify called on. + * Userspace should read this file and then poll. When an event occur + * the blocking poll will be released. + */ +static ssize_t show_irq(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return sprintf(buf, "irq\n"); +} + +static struct device_attribute *dev_attr[AB8500_NR_IRQS]; +static char *event_name[AB8500_NR_IRQS]; + +static ssize_t ab8500_subscribe_write(struct file *file, + const char __user *user_buf, + size_t count, loff_t *ppos) +{ + struct device *dev = ((struct seq_file *)(file->private_data))->private; + char buf[32]; + int buf_size; + unsigned long user_val; + int err; + + /* Get userspace string and assure termination */ + buf_size = min(count, (sizeof(buf)-1)); + if (copy_from_user(buf, user_buf, buf_size)) + return -EFAULT; + buf[buf_size] = 0; + + err = strict_strtoul(buf, 0, &user_val); + if (err) + return -EINVAL; + if (user_val < irq_first) { + dev_err(dev, "debugfs error input < %d\n", irq_first); + return -EINVAL; + } + if (user_val > irq_last) { + dev_err(dev, "debugfs error input > %d\n", irq_last); + return -EINVAL; + } + + /* + * This will create a sysfs file named irq- which userspace can + * use to select or poll and get the AB8500 events + */ + dev_attr[user_val] = kmalloc(sizeof(struct device_attribute), + GFP_KERNEL); + event_name[user_val] = kmalloc(buf_size, GFP_KERNEL); + sprintf(event_name[user_val], "irq-%lu", user_val); + dev_attr[user_val]->show = show_irq; + dev_attr[user_val]->store = NULL; + dev_attr[user_val]->attr.name = event_name[user_val]; + dev_attr[user_val]->attr.mode = S_IRUGO; + err = sysfs_create_file(&dev->kobj, &dev_attr[user_val]->attr); + if (err < 0) { + printk(KERN_ERR "sysfs_create_file failed %d\n", err); + return err; + } + + err = request_threaded_irq(user_val, NULL, ab8500_debug_handler, + 0, "ab8500-debug", &dev->kobj); + if (err < 0) { + printk(KERN_ERR "request_threaded_irq failed %d, %lu\n", + err, user_val); + return err; + } + + return buf_size; +} + +static ssize_t ab8500_unsubscribe_write(struct file *file, + const char __user *user_buf, + size_t count, loff_t *ppos) +{ + struct device *dev = ((struct seq_file *)(file->private_data))->private; + char buf[32]; + int buf_size; + unsigned long user_val; + int err; + + /* Get userspace string and assure termination */ + buf_size = min(count, (sizeof(buf)-1)); + if (copy_from_user(buf, user_buf, buf_size)) + return -EFAULT; + buf[buf_size] = 0; + + err = strict_strtoul(buf, 0, &user_val); + if (err) + return -EINVAL; + if (user_val < irq_first) { + dev_err(dev, "debugfs error input < %d\n", irq_first); + return -EINVAL; + } + if (user_val > irq_last) { + dev_err(dev, "debugfs error input > %d\n", irq_last); + return -EINVAL; + } + + free_irq(user_val, &dev->kobj); + kfree(event_name[user_val]); + kfree(dev_attr[user_val]); + + if (dev_attr[user_val]) + sysfs_remove_file(&dev->kobj, &dev_attr[user_val]->attr); + + return buf_size; +} + static const struct file_operations ab8500_bank_fops = { .open = ab8500_bank_open, .write = ab8500_bank_write, @@ -546,17 +691,51 @@ static const struct file_operations ab8500_val_fops = { .owner = THIS_MODULE, }; +static const struct file_operations ab8500_subscribe_fops = { + .open = ab8500_subscribe_unsubscribe_open, + .write = ab8500_subscribe_write, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, + .owner = THIS_MODULE, +}; + +static const struct file_operations ab8500_unsubscribe_fops = { + .open = ab8500_subscribe_unsubscribe_open, + .write = ab8500_unsubscribe_write, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, + .owner = THIS_MODULE, +}; + static struct dentry *ab8500_dir; static struct dentry *ab8500_reg_file; static struct dentry *ab8500_bank_file; static struct dentry *ab8500_address_file; static struct dentry *ab8500_val_file; +static struct dentry *ab8500_subscribe_file; +static struct dentry *ab8500_unsubscribe_file; static int __devinit ab8500_debug_probe(struct platform_device *plf) { debug_bank = AB8500_MISC; debug_address = AB8500_REV_REG & 0x00FF; + irq_first = platform_get_irq_byname(plf, "IRQ_FIRST"); + if (irq_first < 0) { + dev_err(&plf->dev, "First irq not found, err %d\n", + irq_first); + return irq_first; + } + + irq_last = platform_get_irq_byname(plf, "IRQ_LAST"); + if (irq_last < 0) { + dev_err(&plf->dev, "Last irq not found, err %d\n", + irq_last); + return irq_last; + } + ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL); if (!ab8500_dir) goto exit_no_debugfs; @@ -582,8 +761,26 @@ static int __devinit ab8500_debug_probe(struct platform_device *plf) if (!ab8500_val_file) goto exit_destroy_address; + ab8500_subscribe_file = + debugfs_create_file("irq-subscribe", + (S_IRUGO | S_IWUGO), ab8500_dir, &plf->dev, + &ab8500_subscribe_fops); + if (!ab8500_subscribe_file) + goto exit_destroy_val; + + ab8500_unsubscribe_file = + debugfs_create_file("irq-unsubscribe", + (S_IRUGO | S_IWUGO), ab8500_dir, &plf->dev, + &ab8500_unsubscribe_fops); + if (!ab8500_unsubscribe_file) + goto exit_destroy_subscribe; + return 0; +exit_destroy_subscribe: + debugfs_remove(ab8500_subscribe_file); +exit_destroy_val: + debugfs_remove(ab8500_val_file); exit_destroy_address: debugfs_remove(ab8500_address_file); exit_destroy_bank: