debugfs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * debugfs attributes for Wilco EC
  4. *
  5. * Copyright 2019 Google LLC
  6. *
  7. * See Documentation/ABI/testing/debugfs-wilco-ec for usage.
  8. */
  9. #include <linux/ctype.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/fs.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_data/wilco-ec.h>
  15. #include <linux/platform_device.h>
  16. #define DRV_NAME "wilco-ec-debugfs"
  17. /* The raw bytes will take up more space when represented as a hex string */
  18. #define FORMATTED_BUFFER_SIZE (EC_MAILBOX_DATA_SIZE * 4)
  19. struct wilco_ec_debugfs {
  20. struct wilco_ec_device *ec;
  21. struct dentry *dir;
  22. size_t response_size;
  23. u8 raw_data[EC_MAILBOX_DATA_SIZE];
  24. u8 formatted_data[FORMATTED_BUFFER_SIZE];
  25. };
  26. static struct wilco_ec_debugfs *debug_info;
  27. /**
  28. * parse_hex_sentence() - Convert a ascii hex representation into byte array.
  29. * @in: Input buffer of ascii.
  30. * @isize: Length of input buffer.
  31. * @out: Output buffer.
  32. * @osize: Length of output buffer, e.g. max number of bytes to parse.
  33. *
  34. * An valid input is a series of ascii hexadecimal numbers, separated by spaces.
  35. * An example valid input is
  36. * " 00 f2 0 000076 6 0 ff"
  37. *
  38. * If an individual "word" within the hex sentence is longer than MAX_WORD_SIZE,
  39. * then the sentence is illegal, and parsing will fail.
  40. *
  41. * Return: Number of bytes parsed, or negative error code on failure.
  42. */
  43. static int parse_hex_sentence(const char *in, int isize, u8 *out, int osize)
  44. {
  45. int n_parsed = 0;
  46. int word_start = 0;
  47. int word_end;
  48. int word_len;
  49. /* Temp buffer for holding a "word" of chars that represents one byte */
  50. #define MAX_WORD_SIZE 16
  51. char tmp[MAX_WORD_SIZE + 1];
  52. u8 byte;
  53. while (word_start < isize && n_parsed < osize) {
  54. /* Find the start of the next word */
  55. while (word_start < isize && isspace(in[word_start]))
  56. word_start++;
  57. /* reached the end of the input before next word? */
  58. if (word_start >= isize)
  59. break;
  60. /* Find the end of this word */
  61. word_end = word_start;
  62. while (word_end < isize && !isspace(in[word_end]))
  63. word_end++;
  64. /* Copy to a tmp NULL terminated string */
  65. word_len = word_end - word_start;
  66. if (word_len > MAX_WORD_SIZE)
  67. return -EINVAL;
  68. memcpy(tmp, in + word_start, word_len);
  69. tmp[word_len] = '\0';
  70. /*
  71. * Convert from hex string, place in output. If fails to parse,
  72. * just return -EINVAL because specific error code is only
  73. * relevant for this one word, returning it would be confusing.
  74. */
  75. if (kstrtou8(tmp, 16, &byte))
  76. return -EINVAL;
  77. out[n_parsed++] = byte;
  78. word_start = word_end;
  79. }
  80. return n_parsed;
  81. }
  82. /* The message type takes up two bytes*/
  83. #define TYPE_AND_DATA_SIZE ((EC_MAILBOX_DATA_SIZE) + 2)
  84. static ssize_t raw_write(struct file *file, const char __user *user_buf,
  85. size_t count, loff_t *ppos)
  86. {
  87. char *buf = debug_info->formatted_data;
  88. struct wilco_ec_message msg;
  89. u8 request_data[TYPE_AND_DATA_SIZE];
  90. ssize_t kcount;
  91. int ret;
  92. if (count > FORMATTED_BUFFER_SIZE)
  93. return -EINVAL;
  94. kcount = simple_write_to_buffer(buf, FORMATTED_BUFFER_SIZE, ppos,
  95. user_buf, count);
  96. if (kcount < 0)
  97. return kcount;
  98. ret = parse_hex_sentence(buf, kcount, request_data, TYPE_AND_DATA_SIZE);
  99. if (ret < 0)
  100. return ret;
  101. /* Need at least two bytes for message type and one byte of data */
  102. if (ret < 3)
  103. return -EINVAL;
  104. msg.type = request_data[0] << 8 | request_data[1];
  105. msg.flags = 0;
  106. msg.request_data = request_data + 2;
  107. msg.request_size = ret - 2;
  108. memset(debug_info->raw_data, 0, sizeof(debug_info->raw_data));
  109. msg.response_data = debug_info->raw_data;
  110. msg.response_size = EC_MAILBOX_DATA_SIZE;
  111. ret = wilco_ec_mailbox(debug_info->ec, &msg);
  112. if (ret < 0)
  113. return ret;
  114. debug_info->response_size = ret;
  115. return count;
  116. }
  117. static ssize_t raw_read(struct file *file, char __user *user_buf, size_t count,
  118. loff_t *ppos)
  119. {
  120. int fmt_len = 0;
  121. if (debug_info->response_size) {
  122. fmt_len = hex_dump_to_buffer(debug_info->raw_data,
  123. debug_info->response_size,
  124. 16, 1, debug_info->formatted_data,
  125. sizeof(debug_info->formatted_data),
  126. true);
  127. /* Only return response the first time it is read */
  128. debug_info->response_size = 0;
  129. }
  130. return simple_read_from_buffer(user_buf, count, ppos,
  131. debug_info->formatted_data, fmt_len);
  132. }
  133. static const struct file_operations fops_raw = {
  134. .owner = THIS_MODULE,
  135. .read = raw_read,
  136. .write = raw_write,
  137. };
  138. #define CMD_KB_CHROME 0x88
  139. #define SUB_CMD_H1_GPIO 0x0A
  140. #define SUB_CMD_TEST_EVENT 0x0B
  141. struct ec_request {
  142. u8 cmd; /* Always CMD_KB_CHROME */
  143. u8 reserved;
  144. u8 sub_cmd;
  145. } __packed;
  146. struct ec_response {
  147. u8 status; /* 0 if allowed */
  148. u8 val;
  149. } __packed;
  150. static int send_ec_cmd(struct wilco_ec_device *ec, u8 sub_cmd, u8 *out_val)
  151. {
  152. struct ec_request rq;
  153. struct ec_response rs;
  154. struct wilco_ec_message msg;
  155. int ret;
  156. memset(&rq, 0, sizeof(rq));
  157. rq.cmd = CMD_KB_CHROME;
  158. rq.sub_cmd = sub_cmd;
  159. memset(&msg, 0, sizeof(msg));
  160. msg.type = WILCO_EC_MSG_LEGACY;
  161. msg.request_data = &rq;
  162. msg.request_size = sizeof(rq);
  163. msg.response_data = &rs;
  164. msg.response_size = sizeof(rs);
  165. ret = wilco_ec_mailbox(ec, &msg);
  166. if (ret < 0)
  167. return ret;
  168. if (rs.status)
  169. return -EIO;
  170. *out_val = rs.val;
  171. return 0;
  172. }
  173. /**
  174. * h1_gpio_get() - Gets h1 gpio status.
  175. * @arg: The wilco EC device.
  176. * @val: BIT(0)=ENTRY_TO_FACT_MODE, BIT(1)=SPI_CHROME_SEL
  177. */
  178. static int h1_gpio_get(void *arg, u64 *val)
  179. {
  180. int ret;
  181. ret = send_ec_cmd(arg, SUB_CMD_H1_GPIO, (u8 *)val);
  182. if (ret == 0)
  183. *val &= 0xFF;
  184. return ret;
  185. }
  186. DEFINE_DEBUGFS_ATTRIBUTE(fops_h1_gpio, h1_gpio_get, NULL, "0x%02llx\n");
  187. /**
  188. * test_event_set() - Sends command to EC to cause an EC test event.
  189. * @arg: The wilco EC device.
  190. * @val: unused.
  191. */
  192. static int test_event_set(void *arg, u64 val)
  193. {
  194. u8 ret;
  195. return send_ec_cmd(arg, SUB_CMD_TEST_EVENT, &ret);
  196. }
  197. /* Format is unused since it is only required for get method which is NULL */
  198. DEFINE_DEBUGFS_ATTRIBUTE(fops_test_event, NULL, test_event_set, "%llu\n");
  199. /**
  200. * wilco_ec_debugfs_probe() - Create the debugfs node
  201. * @pdev: The platform device, probably created in core.c
  202. *
  203. * Try to create a debugfs node. If it fails, then we don't want to change
  204. * behavior at all, this is for debugging after all. Just fail silently.
  205. *
  206. * Return: 0 always.
  207. */
  208. static int wilco_ec_debugfs_probe(struct platform_device *pdev)
  209. {
  210. struct wilco_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  211. debug_info = devm_kzalloc(&pdev->dev, sizeof(*debug_info), GFP_KERNEL);
  212. if (!debug_info)
  213. return 0;
  214. debug_info->ec = ec;
  215. debug_info->dir = debugfs_create_dir("wilco_ec", NULL);
  216. debugfs_create_file("raw", 0644, debug_info->dir, NULL, &fops_raw);
  217. debugfs_create_file("h1_gpio", 0444, debug_info->dir, ec,
  218. &fops_h1_gpio);
  219. debugfs_create_file("test_event", 0200, debug_info->dir, ec,
  220. &fops_test_event);
  221. return 0;
  222. }
  223. static void wilco_ec_debugfs_remove(struct platform_device *pdev)
  224. {
  225. debugfs_remove_recursive(debug_info->dir);
  226. }
  227. static const struct platform_device_id wilco_ec_debugfs_id[] = {
  228. { DRV_NAME, 0 },
  229. {}
  230. };
  231. MODULE_DEVICE_TABLE(platform, wilco_ec_debugfs_id);
  232. static struct platform_driver wilco_ec_debugfs_driver = {
  233. .driver = {
  234. .name = DRV_NAME,
  235. },
  236. .probe = wilco_ec_debugfs_probe,
  237. .remove_new = wilco_ec_debugfs_remove,
  238. .id_table = wilco_ec_debugfs_id,
  239. };
  240. module_platform_driver(wilco_ec_debugfs_driver);
  241. MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
  242. MODULE_LICENSE("GPL v2");
  243. MODULE_DESCRIPTION("Wilco EC debugfs driver");