18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * debugfs attributes for Wilco EC 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2019 Google LLC 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * See Documentation/ABI/testing/debugfs-wilco-ec for usage. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/ctype.h> 118c2ecf20Sopenharmony_ci#include <linux/debugfs.h> 128c2ecf20Sopenharmony_ci#include <linux/fs.h> 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/platform_data/wilco-ec.h> 158c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define DRV_NAME "wilco-ec-debugfs" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* The raw bytes will take up more space when represented as a hex string */ 208c2ecf20Sopenharmony_ci#define FORMATTED_BUFFER_SIZE (EC_MAILBOX_DATA_SIZE * 4) 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistruct wilco_ec_debugfs { 238c2ecf20Sopenharmony_ci struct wilco_ec_device *ec; 248c2ecf20Sopenharmony_ci struct dentry *dir; 258c2ecf20Sopenharmony_ci size_t response_size; 268c2ecf20Sopenharmony_ci u8 raw_data[EC_MAILBOX_DATA_SIZE]; 278c2ecf20Sopenharmony_ci u8 formatted_data[FORMATTED_BUFFER_SIZE]; 288c2ecf20Sopenharmony_ci}; 298c2ecf20Sopenharmony_cistatic struct wilco_ec_debugfs *debug_info; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci/** 328c2ecf20Sopenharmony_ci * parse_hex_sentence() - Convert a ascii hex representation into byte array. 338c2ecf20Sopenharmony_ci * @in: Input buffer of ascii. 348c2ecf20Sopenharmony_ci * @isize: Length of input buffer. 358c2ecf20Sopenharmony_ci * @out: Output buffer. 368c2ecf20Sopenharmony_ci * @osize: Length of output buffer, e.g. max number of bytes to parse. 378c2ecf20Sopenharmony_ci * 388c2ecf20Sopenharmony_ci * An valid input is a series of ascii hexadecimal numbers, separated by spaces. 398c2ecf20Sopenharmony_ci * An example valid input is 408c2ecf20Sopenharmony_ci * " 00 f2 0 000076 6 0 ff" 418c2ecf20Sopenharmony_ci * 428c2ecf20Sopenharmony_ci * If an individual "word" within the hex sentence is longer than MAX_WORD_SIZE, 438c2ecf20Sopenharmony_ci * then the sentence is illegal, and parsing will fail. 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * Return: Number of bytes parsed, or negative error code on failure. 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_cistatic int parse_hex_sentence(const char *in, int isize, u8 *out, int osize) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci int n_parsed = 0; 508c2ecf20Sopenharmony_ci int word_start = 0; 518c2ecf20Sopenharmony_ci int word_end; 528c2ecf20Sopenharmony_ci int word_len; 538c2ecf20Sopenharmony_ci /* Temp buffer for holding a "word" of chars that represents one byte */ 548c2ecf20Sopenharmony_ci #define MAX_WORD_SIZE 16 558c2ecf20Sopenharmony_ci char tmp[MAX_WORD_SIZE + 1]; 568c2ecf20Sopenharmony_ci u8 byte; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci while (word_start < isize && n_parsed < osize) { 598c2ecf20Sopenharmony_ci /* Find the start of the next word */ 608c2ecf20Sopenharmony_ci while (word_start < isize && isspace(in[word_start])) 618c2ecf20Sopenharmony_ci word_start++; 628c2ecf20Sopenharmony_ci /* reached the end of the input before next word? */ 638c2ecf20Sopenharmony_ci if (word_start >= isize) 648c2ecf20Sopenharmony_ci break; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci /* Find the end of this word */ 678c2ecf20Sopenharmony_ci word_end = word_start; 688c2ecf20Sopenharmony_ci while (word_end < isize && !isspace(in[word_end])) 698c2ecf20Sopenharmony_ci word_end++; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* Copy to a tmp NULL terminated string */ 728c2ecf20Sopenharmony_ci word_len = word_end - word_start; 738c2ecf20Sopenharmony_ci if (word_len > MAX_WORD_SIZE) 748c2ecf20Sopenharmony_ci return -EINVAL; 758c2ecf20Sopenharmony_ci memcpy(tmp, in + word_start, word_len); 768c2ecf20Sopenharmony_ci tmp[word_len] = '\0'; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci /* 798c2ecf20Sopenharmony_ci * Convert from hex string, place in output. If fails to parse, 808c2ecf20Sopenharmony_ci * just return -EINVAL because specific error code is only 818c2ecf20Sopenharmony_ci * relevant for this one word, returning it would be confusing. 828c2ecf20Sopenharmony_ci */ 838c2ecf20Sopenharmony_ci if (kstrtou8(tmp, 16, &byte)) 848c2ecf20Sopenharmony_ci return -EINVAL; 858c2ecf20Sopenharmony_ci out[n_parsed++] = byte; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci word_start = word_end; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci return n_parsed; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci/* The message type takes up two bytes*/ 938c2ecf20Sopenharmony_ci#define TYPE_AND_DATA_SIZE ((EC_MAILBOX_DATA_SIZE) + 2) 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic ssize_t raw_write(struct file *file, const char __user *user_buf, 968c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci char *buf = debug_info->formatted_data; 998c2ecf20Sopenharmony_ci struct wilco_ec_message msg; 1008c2ecf20Sopenharmony_ci u8 request_data[TYPE_AND_DATA_SIZE]; 1018c2ecf20Sopenharmony_ci ssize_t kcount; 1028c2ecf20Sopenharmony_ci int ret; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci if (count > FORMATTED_BUFFER_SIZE) 1058c2ecf20Sopenharmony_ci return -EINVAL; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci kcount = simple_write_to_buffer(buf, FORMATTED_BUFFER_SIZE, ppos, 1088c2ecf20Sopenharmony_ci user_buf, count); 1098c2ecf20Sopenharmony_ci if (kcount < 0) 1108c2ecf20Sopenharmony_ci return kcount; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci ret = parse_hex_sentence(buf, kcount, request_data, TYPE_AND_DATA_SIZE); 1138c2ecf20Sopenharmony_ci if (ret < 0) 1148c2ecf20Sopenharmony_ci return ret; 1158c2ecf20Sopenharmony_ci /* Need at least two bytes for message type and one byte of data */ 1168c2ecf20Sopenharmony_ci if (ret < 3) 1178c2ecf20Sopenharmony_ci return -EINVAL; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci msg.type = request_data[0] << 8 | request_data[1]; 1208c2ecf20Sopenharmony_ci msg.flags = 0; 1218c2ecf20Sopenharmony_ci msg.request_data = request_data + 2; 1228c2ecf20Sopenharmony_ci msg.request_size = ret - 2; 1238c2ecf20Sopenharmony_ci memset(debug_info->raw_data, 0, sizeof(debug_info->raw_data)); 1248c2ecf20Sopenharmony_ci msg.response_data = debug_info->raw_data; 1258c2ecf20Sopenharmony_ci msg.response_size = EC_MAILBOX_DATA_SIZE; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci ret = wilco_ec_mailbox(debug_info->ec, &msg); 1288c2ecf20Sopenharmony_ci if (ret < 0) 1298c2ecf20Sopenharmony_ci return ret; 1308c2ecf20Sopenharmony_ci debug_info->response_size = ret; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci return count; 1338c2ecf20Sopenharmony_ci} 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistatic ssize_t raw_read(struct file *file, char __user *user_buf, size_t count, 1368c2ecf20Sopenharmony_ci loff_t *ppos) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci int fmt_len = 0; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci if (debug_info->response_size) { 1418c2ecf20Sopenharmony_ci fmt_len = hex_dump_to_buffer(debug_info->raw_data, 1428c2ecf20Sopenharmony_ci debug_info->response_size, 1438c2ecf20Sopenharmony_ci 16, 1, debug_info->formatted_data, 1448c2ecf20Sopenharmony_ci sizeof(debug_info->formatted_data), 1458c2ecf20Sopenharmony_ci true); 1468c2ecf20Sopenharmony_ci /* Only return response the first time it is read */ 1478c2ecf20Sopenharmony_ci debug_info->response_size = 0; 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci return simple_read_from_buffer(user_buf, count, ppos, 1518c2ecf20Sopenharmony_ci debug_info->formatted_data, fmt_len); 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_cistatic const struct file_operations fops_raw = { 1558c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 1568c2ecf20Sopenharmony_ci .read = raw_read, 1578c2ecf20Sopenharmony_ci .write = raw_write, 1588c2ecf20Sopenharmony_ci .llseek = no_llseek, 1598c2ecf20Sopenharmony_ci}; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci#define CMD_KB_CHROME 0x88 1628c2ecf20Sopenharmony_ci#define SUB_CMD_H1_GPIO 0x0A 1638c2ecf20Sopenharmony_ci#define SUB_CMD_TEST_EVENT 0x0B 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistruct ec_request { 1668c2ecf20Sopenharmony_ci u8 cmd; /* Always CMD_KB_CHROME */ 1678c2ecf20Sopenharmony_ci u8 reserved; 1688c2ecf20Sopenharmony_ci u8 sub_cmd; 1698c2ecf20Sopenharmony_ci} __packed; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_cistruct ec_response { 1728c2ecf20Sopenharmony_ci u8 status; /* 0 if allowed */ 1738c2ecf20Sopenharmony_ci u8 val; 1748c2ecf20Sopenharmony_ci} __packed; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic int send_ec_cmd(struct wilco_ec_device *ec, u8 sub_cmd, u8 *out_val) 1778c2ecf20Sopenharmony_ci{ 1788c2ecf20Sopenharmony_ci struct ec_request rq; 1798c2ecf20Sopenharmony_ci struct ec_response rs; 1808c2ecf20Sopenharmony_ci struct wilco_ec_message msg; 1818c2ecf20Sopenharmony_ci int ret; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci memset(&rq, 0, sizeof(rq)); 1848c2ecf20Sopenharmony_ci rq.cmd = CMD_KB_CHROME; 1858c2ecf20Sopenharmony_ci rq.sub_cmd = sub_cmd; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci memset(&msg, 0, sizeof(msg)); 1888c2ecf20Sopenharmony_ci msg.type = WILCO_EC_MSG_LEGACY; 1898c2ecf20Sopenharmony_ci msg.request_data = &rq; 1908c2ecf20Sopenharmony_ci msg.request_size = sizeof(rq); 1918c2ecf20Sopenharmony_ci msg.response_data = &rs; 1928c2ecf20Sopenharmony_ci msg.response_size = sizeof(rs); 1938c2ecf20Sopenharmony_ci ret = wilco_ec_mailbox(ec, &msg); 1948c2ecf20Sopenharmony_ci if (ret < 0) 1958c2ecf20Sopenharmony_ci return ret; 1968c2ecf20Sopenharmony_ci if (rs.status) 1978c2ecf20Sopenharmony_ci return -EIO; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci *out_val = rs.val; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci return 0; 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci/** 2058c2ecf20Sopenharmony_ci * h1_gpio_get() - Gets h1 gpio status. 2068c2ecf20Sopenharmony_ci * @arg: The wilco EC device. 2078c2ecf20Sopenharmony_ci * @val: BIT(0)=ENTRY_TO_FACT_MODE, BIT(1)=SPI_CHROME_SEL 2088c2ecf20Sopenharmony_ci */ 2098c2ecf20Sopenharmony_cistatic int h1_gpio_get(void *arg, u64 *val) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci int ret; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci ret = send_ec_cmd(arg, SUB_CMD_H1_GPIO, (u8 *)val); 2148c2ecf20Sopenharmony_ci if (ret == 0) 2158c2ecf20Sopenharmony_ci *val &= 0xFF; 2168c2ecf20Sopenharmony_ci return ret; 2178c2ecf20Sopenharmony_ci} 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_h1_gpio, h1_gpio_get, NULL, "0x%02llx\n"); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci/** 2228c2ecf20Sopenharmony_ci * test_event_set() - Sends command to EC to cause an EC test event. 2238c2ecf20Sopenharmony_ci * @arg: The wilco EC device. 2248c2ecf20Sopenharmony_ci * @val: unused. 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_cistatic int test_event_set(void *arg, u64 val) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci u8 ret; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci return send_ec_cmd(arg, SUB_CMD_TEST_EVENT, &ret); 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci/* Format is unused since it is only required for get method which is NULL */ 2348c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_test_event, NULL, test_event_set, "%llu\n"); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci/** 2378c2ecf20Sopenharmony_ci * wilco_ec_debugfs_probe() - Create the debugfs node 2388c2ecf20Sopenharmony_ci * @pdev: The platform device, probably created in core.c 2398c2ecf20Sopenharmony_ci * 2408c2ecf20Sopenharmony_ci * Try to create a debugfs node. If it fails, then we don't want to change 2418c2ecf20Sopenharmony_ci * behavior at all, this is for debugging after all. Just fail silently. 2428c2ecf20Sopenharmony_ci * 2438c2ecf20Sopenharmony_ci * Return: 0 always. 2448c2ecf20Sopenharmony_ci */ 2458c2ecf20Sopenharmony_cistatic int wilco_ec_debugfs_probe(struct platform_device *pdev) 2468c2ecf20Sopenharmony_ci{ 2478c2ecf20Sopenharmony_ci struct wilco_ec_device *ec = dev_get_drvdata(pdev->dev.parent); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci debug_info = devm_kzalloc(&pdev->dev, sizeof(*debug_info), GFP_KERNEL); 2508c2ecf20Sopenharmony_ci if (!debug_info) 2518c2ecf20Sopenharmony_ci return 0; 2528c2ecf20Sopenharmony_ci debug_info->ec = ec; 2538c2ecf20Sopenharmony_ci debug_info->dir = debugfs_create_dir("wilco_ec", NULL); 2548c2ecf20Sopenharmony_ci if (!debug_info->dir) 2558c2ecf20Sopenharmony_ci return 0; 2568c2ecf20Sopenharmony_ci debugfs_create_file("raw", 0644, debug_info->dir, NULL, &fops_raw); 2578c2ecf20Sopenharmony_ci debugfs_create_file("h1_gpio", 0444, debug_info->dir, ec, 2588c2ecf20Sopenharmony_ci &fops_h1_gpio); 2598c2ecf20Sopenharmony_ci debugfs_create_file("test_event", 0200, debug_info->dir, ec, 2608c2ecf20Sopenharmony_ci &fops_test_event); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci return 0; 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_cistatic int wilco_ec_debugfs_remove(struct platform_device *pdev) 2668c2ecf20Sopenharmony_ci{ 2678c2ecf20Sopenharmony_ci debugfs_remove_recursive(debug_info->dir); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci return 0; 2708c2ecf20Sopenharmony_ci} 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_cistatic struct platform_driver wilco_ec_debugfs_driver = { 2738c2ecf20Sopenharmony_ci .driver = { 2748c2ecf20Sopenharmony_ci .name = DRV_NAME, 2758c2ecf20Sopenharmony_ci }, 2768c2ecf20Sopenharmony_ci .probe = wilco_ec_debugfs_probe, 2778c2ecf20Sopenharmony_ci .remove = wilco_ec_debugfs_remove, 2788c2ecf20Sopenharmony_ci}; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cimodule_platform_driver(wilco_ec_debugfs_driver); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRV_NAME); 2838c2ecf20Sopenharmony_ciMODULE_AUTHOR("Nick Crews <ncrews@chromium.org>"); 2848c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 2858c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Wilco EC debugfs driver"); 286