Jack Pham | ccbbfab | 2012-04-09 19:50:20 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | /* add additional information to our printk's */ |
| 14 | #define pr_fmt(fmt) "%s: " fmt "\n", __func__ |
| 15 | |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/debugfs.h> |
| 20 | #include <linux/uaccess.h> |
| 21 | |
| 22 | #include "hsic_sysmon.h" |
| 23 | #include "sysmon.h" |
| 24 | |
| 25 | #define DRIVER_DESC "HSIC System monitor driver test" |
| 26 | |
| 27 | #define RD_BUF_SIZE 4096 |
| 28 | |
| 29 | struct sysmon_test_dev { |
| 30 | int buflen; |
| 31 | char buf[RD_BUF_SIZE]; |
| 32 | }; |
| 33 | static struct sysmon_test_dev *sysmon_dev; |
| 34 | |
| 35 | static ssize_t sysmon_test_read(struct file *file, char __user *ubuf, |
| 36 | size_t count, loff_t *ppos) |
| 37 | { |
| 38 | struct sysmon_test_dev *dev = sysmon_dev; |
| 39 | int ret; |
| 40 | |
| 41 | if (!dev) |
| 42 | return -ENODEV; |
| 43 | |
| 44 | ret = hsic_sysmon_read(HSIC_SYSMON_DEV_EXT_MODEM, dev->buf, RD_BUF_SIZE, |
| 45 | &dev->buflen, 3000); |
| 46 | if (!ret) |
| 47 | return simple_read_from_buffer(ubuf, count, ppos, |
| 48 | dev->buf, dev->buflen); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static ssize_t sysmon_test_write(struct file *file, const char __user *ubuf, |
| 54 | size_t count, loff_t *ppos) |
| 55 | { |
| 56 | struct sysmon_test_dev *dev = sysmon_dev; |
| 57 | int ret; |
| 58 | |
| 59 | if (!dev) |
| 60 | return -ENODEV; |
| 61 | |
| 62 | if (copy_from_user(dev->buf, ubuf, count)) { |
| 63 | pr_err("error copying for writing"); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | ret = hsic_sysmon_write(HSIC_SYSMON_DEV_EXT_MODEM, |
| 68 | dev->buf, count, 1000); |
| 69 | if (ret < 0) { |
| 70 | pr_err("error writing to hsic_sysmon"); |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | return count; |
| 75 | } |
| 76 | |
| 77 | static int sysmon_test_open(struct inode *inode, struct file *file) |
| 78 | { |
| 79 | return hsic_sysmon_open(HSIC_SYSMON_DEV_EXT_MODEM); |
| 80 | } |
| 81 | |
| 82 | static int sysmon_test_release(struct inode *inode, struct file *file) |
| 83 | { |
| 84 | hsic_sysmon_close(HSIC_SYSMON_DEV_EXT_MODEM); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static struct dentry *dfile; |
| 89 | const struct file_operations sysmon_test_ops = { |
| 90 | .read = sysmon_test_read, |
| 91 | .write = sysmon_test_write, |
| 92 | .open = sysmon_test_open, |
| 93 | .release = sysmon_test_release |
| 94 | }; |
| 95 | |
| 96 | static int __init sysmon_test_init(void) |
| 97 | { |
| 98 | sysmon_dev = kzalloc(sizeof(*sysmon_dev), GFP_KERNEL); |
| 99 | if (!sysmon_dev) |
| 100 | return -ENOMEM; |
| 101 | |
| 102 | dfile = debugfs_create_file("hsic_sysmon_test", 0666, NULL, |
| 103 | 0, &sysmon_test_ops); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static void __exit sysmon_test_exit(void) |
| 108 | { |
| 109 | if (dfile) |
| 110 | debugfs_remove(dfile); |
| 111 | kfree(sysmon_dev); |
| 112 | } |
| 113 | |
| 114 | module_init(sysmon_test_init); |
| 115 | module_exit(sysmon_test_exit); |
| 116 | |
| 117 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 118 | MODULE_LICENSE("GPL v2"); |