hsic_sysmon_test.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (c) 2012-2013, The Linux Foundation. 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. /* add additional information to our printk's */
  13. #define pr_fmt(fmt) "%s: " fmt "\n", __func__
  14. #include <linux/slab.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/uaccess.h>
  20. #include "hsic_sysmon.h"
  21. #include "sysmon.h"
  22. #define DRIVER_DESC "HSIC System monitor driver test"
  23. #define RD_BUF_SIZE 4096
  24. struct sysmon_test_dev {
  25. int buflen;
  26. char buf[RD_BUF_SIZE];
  27. };
  28. static struct sysmon_test_dev *sysmon_dev;
  29. static ssize_t sysmon_test_read(struct file *file, char __user *ubuf,
  30. size_t count, loff_t *ppos)
  31. {
  32. struct sysmon_test_dev *dev = sysmon_dev;
  33. enum hsic_sysmon_device_id id =
  34. (enum hsic_sysmon_device_id)file->private_data;
  35. int ret;
  36. if (!dev)
  37. return -ENODEV;
  38. ret = hsic_sysmon_read(id, dev->buf, RD_BUF_SIZE, &dev->buflen, 3000);
  39. if (!ret)
  40. return simple_read_from_buffer(ubuf, count, ppos,
  41. dev->buf, dev->buflen);
  42. return 0;
  43. }
  44. static ssize_t sysmon_test_write(struct file *file, const char __user *ubuf,
  45. size_t count, loff_t *ppos)
  46. {
  47. struct sysmon_test_dev *dev = sysmon_dev;
  48. enum hsic_sysmon_device_id id =
  49. (enum hsic_sysmon_device_id)file->private_data;
  50. int ret;
  51. if (!dev)
  52. return -ENODEV;
  53. /* Add check for user buf count greater than RD_BUF_SIZE */
  54. if (count > RD_BUF_SIZE)
  55. count = RD_BUF_SIZE;
  56. if (copy_from_user(dev->buf, ubuf, count)) {
  57. pr_err("error copying for writing");
  58. return -EFAULT;
  59. }
  60. ret = hsic_sysmon_write(id, dev->buf, count, 1000);
  61. if (ret < 0) {
  62. pr_err("error writing to hsic_sysmon");
  63. return ret;
  64. }
  65. return count;
  66. }
  67. static int sysmon_test_open(struct inode *inode, struct file *file)
  68. {
  69. file->private_data = inode->i_private;
  70. return hsic_sysmon_open((enum hsic_sysmon_device_id)inode->i_private);
  71. }
  72. static int sysmon_test_release(struct inode *inode, struct file *file)
  73. {
  74. hsic_sysmon_close((enum hsic_sysmon_device_id)inode->i_private);
  75. return 0;
  76. }
  77. static const struct file_operations sysmon_test_ops = {
  78. .read = sysmon_test_read,
  79. .write = sysmon_test_write,
  80. .open = sysmon_test_open,
  81. .release = sysmon_test_release
  82. };
  83. static struct dentry *dfile0, *dfile1;
  84. static int __init sysmon_test_init(void)
  85. {
  86. sysmon_dev = kzalloc(sizeof(*sysmon_dev), GFP_KERNEL);
  87. if (!sysmon_dev)
  88. return -ENOMEM;
  89. dfile0 = debugfs_create_file("hsic_sysmon_test.0", 0666, NULL,
  90. (void *)HSIC_SYSMON_DEV_EXT_MODEM, &sysmon_test_ops);
  91. dfile1 = debugfs_create_file("hsic_sysmon_test.1", 0666, NULL,
  92. (void *)HSIC_SYSMON_DEV_EXT_MODEM_2, &sysmon_test_ops);
  93. return 0;
  94. }
  95. static void __exit sysmon_test_exit(void)
  96. {
  97. if (dfile0)
  98. debugfs_remove(dfile0);
  99. if (dfile1)
  100. debugfs_remove(dfile1);
  101. kfree(sysmon_dev);
  102. }
  103. module_init(sysmon_test_init);
  104. module_exit(sysmon_test_exit);
  105. MODULE_DESCRIPTION(DRIVER_DESC);
  106. MODULE_LICENSE("GPL v2");