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