diag_bridge_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2011, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/crc-ccitt.h>
  19. #include <mach/diag_bridge.h>
  20. #define DRIVER_DESC "USB host diag bridge driver test"
  21. #define DRIVER_VERSION "1.0"
  22. #define RD_BUF_SIZE 2048
  23. #define DIAG_TEST_CONNECTED 0
  24. struct diag_test_dev {
  25. char *read_buf;
  26. struct work_struct read_w;
  27. unsigned long flags;
  28. struct diag_bridge_ops ops;
  29. };
  30. static struct diag_test_dev *__dev;
  31. static struct dentry *dent;
  32. static void
  33. diag_test_read_complete_cb(void *d, char *buf, size_t size, size_t actual)
  34. {
  35. if (actual < 0) {
  36. pr_err("%s: read complete err\n", __func__);
  37. return;
  38. }
  39. print_hex_dump(KERN_INFO, "to_host:", 0, 1, 1, buf, actual, false);
  40. }
  41. static void diag_test_read_work(struct work_struct *w)
  42. {
  43. struct diag_test_dev *dev =
  44. container_of(w, struct diag_test_dev, read_w);
  45. memset(dev->read_buf, 0, RD_BUF_SIZE);
  46. diag_bridge_read(dev->read_buf, RD_BUF_SIZE);
  47. }
  48. static void
  49. diag_test_write_complete_cb(void *d, char *buf, size_t size, size_t actual)
  50. {
  51. struct diag_test_dev *dev = d;
  52. if (actual > 0)
  53. schedule_work(&dev->read_w);
  54. }
  55. #if defined(CONFIG_DEBUG_FS)
  56. #define DEBUG_BUF_SIZE 1024
  57. static ssize_t send_ping_cmd(struct file *file, const char __user *ubuf,
  58. size_t count, loff_t *ppos)
  59. {
  60. struct diag_test_dev *dev = __dev;
  61. unsigned char *buf;
  62. int temp = sizeof(unsigned char) * 4;
  63. if (!dev)
  64. return -ENODEV;
  65. buf = kmalloc(temp, GFP_KERNEL);
  66. if (!buf) {
  67. pr_err("%s: unable to allocate mem for ping cmd\n",
  68. __func__);
  69. return -ENOMEM;
  70. }
  71. /* hdlc encoded ping command */
  72. buf[0] = 0x0C;
  73. buf[1] = 0x14;
  74. buf[2] = 0x3A;
  75. buf[3] = 0x7E;
  76. diag_bridge_write(buf, temp);
  77. return count;
  78. }
  79. const struct file_operations diag_test_ping_ops = {
  80. .write = send_ping_cmd,
  81. };
  82. static void diag_test_debug_init(void)
  83. {
  84. struct dentry *dfile;
  85. dent = debugfs_create_dir("diag_test", 0);
  86. if (IS_ERR(dent))
  87. return;
  88. dfile = debugfs_create_file("send_ping", 0444, dent,
  89. 0, &diag_test_ping_ops);
  90. if (!dfile || IS_ERR(dfile))
  91. debugfs_remove(dent);
  92. }
  93. #else
  94. static void diag_test_debug_init(void) { }
  95. #endif
  96. static int diag_test_remove(struct platform_device *pdev)
  97. {
  98. diag_bridge_close();
  99. if (dent) {
  100. debugfs_remove_recursive(dent);
  101. dent = NULL;
  102. }
  103. return 0;
  104. }
  105. static int diag_test_probe(struct platform_device *pdev)
  106. {
  107. struct diag_test_dev *dev = __dev;
  108. int ret = 0;
  109. pr_info("%s:\n", __func__);
  110. ret = diag_bridge_open(&dev->ops);
  111. if (ret)
  112. pr_err("diag open failed: %d", ret);
  113. diag_test_debug_init();
  114. return ret;
  115. }
  116. static struct platform_driver diag_test = {
  117. .remove = diag_test_remove,
  118. .probe = diag_test_probe,
  119. .driver = {
  120. .name = "diag_bridge",
  121. .owner = THIS_MODULE,
  122. },
  123. };
  124. static int __init diag_test_init(void)
  125. {
  126. struct diag_test_dev *dev;
  127. int ret = 0;
  128. pr_info("%s\n", __func__);
  129. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  130. if (!dev)
  131. return -ENOMEM;
  132. __dev = dev;
  133. dev->ops.read_complete_cb = diag_test_read_complete_cb;
  134. dev->ops.write_complete_cb = diag_test_write_complete_cb;
  135. dev->read_buf = kmalloc(RD_BUF_SIZE, GFP_KERNEL);
  136. if (!dev->read_buf) {
  137. pr_err("%s: unable to allocate read buffer\n", __func__);
  138. kfree(dev);
  139. return -ENOMEM;
  140. }
  141. dev->ops.ctxt = dev;
  142. INIT_WORK(&dev->read_w, diag_test_read_work);
  143. ret = platform_driver_register(&diag_test);
  144. if (ret)
  145. pr_err("%s: platform driver %s register failed %d\n",
  146. __func__, diag_test.driver.name, ret);
  147. return ret;
  148. }
  149. static void __exit diag_test_exit(void)
  150. {
  151. struct diag_test_dev *dev = __dev;
  152. pr_info("%s:\n", __func__);
  153. if (test_bit(DIAG_TEST_CONNECTED, &dev->flags))
  154. diag_bridge_close();
  155. kfree(dev->read_buf);
  156. kfree(dev);
  157. }
  158. module_init(diag_test_init);
  159. module_exit(diag_test_exit);
  160. MODULE_DESCRIPTION(DRIVER_DESC);
  161. MODULE_VERSION(DRIVER_VERSION);
  162. MODULE_LICENSE("GPL v2");