Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2// Expose the ChromeOS EC through sysfs
  3//
  4// Copyright (C) 2014 Google, Inc.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  5
  6#include <linux/ctype.h>
  7#include <linux/delay.h>
  8#include <linux/device.h>
  9#include <linux/fs.h>
 10#include <linux/kobject.h>
 11#include <linux/mod_devicetable.h>
 
 12#include <linux/module.h>
 13#include <linux/platform_data/cros_ec_commands.h>
 14#include <linux/platform_data/cros_ec_proto.h>
 15#include <linux/platform_device.h>
 16#include <linux/printk.h>
 17#include <linux/slab.h>
 18#include <linux/stat.h>
 19#include <linux/types.h>
 20#include <linux/uaccess.h>
 21
 22#define DRV_NAME "cros-ec-sysfs"
 23
 24/* Accessor functions */
 25
 26static ssize_t reboot_show(struct device *dev,
 27			   struct device_attribute *attr, char *buf)
 28{
 29	int count = 0;
 30
 31	count += sysfs_emit_at(buf, count,
 32			       "ro|rw|cancel|cold|disable-jump|hibernate|cold-ap-off");
 33	count += sysfs_emit_at(buf, count, " [at-shutdown]\n");
 
 34	return count;
 35}
 36
 37static ssize_t reboot_store(struct device *dev,
 38			    struct device_attribute *attr,
 39			    const char *buf, size_t count)
 40{
 41	static const struct {
 42		const char * const str;
 43		uint8_t cmd;
 44		uint8_t flags;
 45	} words[] = {
 46		{"cancel",       EC_REBOOT_CANCEL, 0},
 47		{"ro",           EC_REBOOT_JUMP_RO, 0},
 48		{"rw",           EC_REBOOT_JUMP_RW, 0},
 49		{"cold-ap-off",  EC_REBOOT_COLD_AP_OFF, 0},
 50		{"cold",         EC_REBOOT_COLD, 0},
 51		{"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
 52		{"hibernate",    EC_REBOOT_HIBERNATE, 0},
 53		{"at-shutdown",  -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
 54	};
 55	struct cros_ec_command *msg;
 56	struct ec_params_reboot_ec *param;
 57	int got_cmd = 0, offset = 0;
 58	int i;
 59	int ret;
 60	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
 61
 62	msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
 63	if (!msg)
 64		return -ENOMEM;
 65
 66	param = (struct ec_params_reboot_ec *)msg->data;
 67
 68	param->flags = 0;
 69	while (1) {
 70		/* Find word to start scanning */
 71		while (buf[offset] && isspace(buf[offset]))
 72			offset++;
 73		if (!buf[offset])
 74			break;
 75
 76		for (i = 0; i < ARRAY_SIZE(words); i++) {
 77			if (!strncasecmp(words[i].str, buf+offset,
 78					 strlen(words[i].str))) {
 79				if (words[i].flags) {
 80					param->flags |= words[i].flags;
 81				} else {
 82					param->cmd = words[i].cmd;
 83					got_cmd = 1;
 84				}
 85				break;
 86			}
 87		}
 88
 89		/* On to the next word, if any */
 90		while (buf[offset] && !isspace(buf[offset]))
 91			offset++;
 92	}
 93
 94	if (!got_cmd) {
 95		count = -EINVAL;
 96		goto exit;
 97	}
 98
 99	msg->version = 0;
100	msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
101	msg->outsize = sizeof(*param);
102	msg->insize = 0;
103	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
104	if (ret < 0)
105		count = ret;
106exit:
107	kfree(msg);
108	return count;
109}
110
111static ssize_t version_show(struct device *dev,
112			    struct device_attribute *attr, char *buf)
113{
114	static const char * const image_names[] = {"unknown", "RO", "RW"};
115	struct ec_response_get_version *r_ver;
116	struct ec_response_get_chip_info *r_chip;
117	struct ec_response_board_version *r_board;
118	struct cros_ec_command *msg;
119	int ret;
120	int count = 0;
121	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
122
123	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
124	if (!msg)
125		return -ENOMEM;
126
127	/* Get versions. RW may change. */
128	msg->version = 0;
129	msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
130	msg->insize = sizeof(*r_ver);
131	msg->outsize = 0;
132	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
133	if (ret < 0) {
134		count = ret;
135		goto exit;
136	}
137	r_ver = (struct ec_response_get_version *)msg->data;
138	/* Strings should be null-terminated, but let's be sure. */
139	r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
140	r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
141	count += sysfs_emit_at(buf, count, "RO version:    %s\n", r_ver->version_string_ro);
142	count += sysfs_emit_at(buf, count, "RW version:    %s\n", r_ver->version_string_rw);
143	count += sysfs_emit_at(buf, count, "Firmware copy: %s\n",
 
 
 
144			   (r_ver->current_image < ARRAY_SIZE(image_names) ?
145			    image_names[r_ver->current_image] : "?"));
146
147	/* Get build info. */
148	msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
149	msg->insize = EC_HOST_PARAM_SIZE;
150	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
151	if (ret < 0) {
152		count += sysfs_emit_at(buf, count,
153				   "Build info:    XFER / EC ERROR %d / %d\n",
154				   ret, msg->result);
155	} else {
 
 
156		msg->data[EC_HOST_PARAM_SIZE - 1] = '\0';
157		count += sysfs_emit_at(buf, count, "Build info:    %s\n", msg->data);
 
158	}
159
160	/* Get chip info. */
161	msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset;
162	msg->insize = sizeof(*r_chip);
163	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
164	if (ret < 0) {
165		count += sysfs_emit_at(buf, count,
166				   "Chip info:     XFER / EC ERROR %d / %d\n",
167				   ret, msg->result);
168	} else {
 
 
169		r_chip = (struct ec_response_get_chip_info *)msg->data;
170
171		r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
172		r_chip->name[sizeof(r_chip->name) - 1] = '\0';
173		r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
174		count += sysfs_emit_at(buf, count, "Chip vendor:   %s\n", r_chip->vendor);
175		count += sysfs_emit_at(buf, count, "Chip name:     %s\n", r_chip->name);
176		count += sysfs_emit_at(buf, count, "Chip revision: %s\n", r_chip->revision);
 
 
 
177	}
178
179	/* Get board version */
180	msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset;
181	msg->insize = sizeof(*r_board);
182	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
183	if (ret < 0) {
184		count += sysfs_emit_at(buf, count,
185				   "Board version: XFER / EC ERROR %d / %d\n",
186				   ret, msg->result);
187	} else {
 
 
188		r_board = (struct ec_response_board_version *)msg->data;
189
190		count += sysfs_emit_at(buf, count,
191				   "Board version: %d\n",
192				   r_board->board_version);
193	}
194
195exit:
196	kfree(msg);
197	return count;
198}
199
200static ssize_t flashinfo_show(struct device *dev,
201			      struct device_attribute *attr, char *buf)
202{
203	struct ec_response_flash_info *resp;
204	struct cros_ec_command *msg;
205	int ret;
206	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
207
208	msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
209	if (!msg)
210		return -ENOMEM;
211
212	/* The flash info shouldn't ever change, but ask each time anyway. */
213	msg->version = 0;
214	msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
215	msg->insize = sizeof(*resp);
216	msg->outsize = 0;
217	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
218	if (ret < 0)
219		goto exit;
220
221	resp = (struct ec_response_flash_info *)msg->data;
222
223	ret = sysfs_emit(buf,
224			"FlashSize %d\nWriteSize %d\n"
225			"EraseSize %d\nProtectSize %d\n",
226			resp->flash_size, resp->write_block_size,
227			resp->erase_block_size, resp->protect_block_size);
228exit:
229	kfree(msg);
230	return ret;
231}
232
233/* Keyboard wake angle control */
234static ssize_t kb_wake_angle_show(struct device *dev,
235				  struct device_attribute *attr, char *buf)
236{
237	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
238	struct ec_response_motion_sense *resp;
239	struct ec_params_motion_sense *param;
240	struct cros_ec_command *msg;
241	int ret;
242
243	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
244	if (!msg)
245		return -ENOMEM;
246
247	param = (struct ec_params_motion_sense *)msg->data;
248	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
249	msg->version = 2;
250	param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
251	param->kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
252	msg->outsize = sizeof(*param);
253	msg->insize = sizeof(*resp);
254
255	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
256	if (ret < 0)
257		goto exit;
258
259	resp = (struct ec_response_motion_sense *)msg->data;
260	ret = sysfs_emit(buf, "%d\n", resp->kb_wake_angle.ret);
261exit:
262	kfree(msg);
263	return ret;
264}
265
266static ssize_t kb_wake_angle_store(struct device *dev,
267				   struct device_attribute *attr,
268				   const char *buf, size_t count)
269{
270	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
271	struct ec_params_motion_sense *param;
272	struct cros_ec_command *msg;
273	u16 angle;
274	int ret;
275
276	ret = kstrtou16(buf, 0, &angle);
277	if (ret)
278		return ret;
279
280	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
281	if (!msg)
282		return -ENOMEM;
283
284	param = (struct ec_params_motion_sense *)msg->data;
285	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
286	msg->version = 2;
287	param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
288	param->kb_wake_angle.data = angle;
289	msg->outsize = sizeof(*param);
290	msg->insize = sizeof(struct ec_response_motion_sense);
291
292	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
293	kfree(msg);
294	if (ret < 0)
295		return ret;
296	return count;
297}
298
299/* Module initialization */
300
301static DEVICE_ATTR_RW(reboot);
302static DEVICE_ATTR_RO(version);
303static DEVICE_ATTR_RO(flashinfo);
304static DEVICE_ATTR_RW(kb_wake_angle);
305
306static struct attribute *__ec_attrs[] = {
307	&dev_attr_kb_wake_angle.attr,
308	&dev_attr_reboot.attr,
309	&dev_attr_version.attr,
310	&dev_attr_flashinfo.attr,
311	NULL,
312};
313
314static umode_t cros_ec_ctrl_visible(struct kobject *kobj,
315				    struct attribute *a, int n)
316{
317	struct device *dev = kobj_to_dev(kobj);
318	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
319
320	if (a == &dev_attr_kb_wake_angle.attr && !ec->has_kb_wake_angle)
321		return 0;
322
323	return a->mode;
324}
325
326static const struct attribute_group cros_ec_attr_group = {
327	.attrs = __ec_attrs,
328	.is_visible = cros_ec_ctrl_visible,
329};
330
331static int cros_ec_sysfs_probe(struct platform_device *pd)
332{
333	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
334	struct device *dev = &pd->dev;
335	int ret;
336
337	ret = sysfs_create_group(&ec_dev->class_dev.kobj, &cros_ec_attr_group);
338	if (ret < 0)
339		dev_err(dev, "failed to create attributes. err=%d\n", ret);
340
341	return ret;
342}
343
344static void cros_ec_sysfs_remove(struct platform_device *pd)
345{
346	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
347
348	sysfs_remove_group(&ec_dev->class_dev.kobj, &cros_ec_attr_group);
349}
350
351static const struct platform_device_id cros_ec_sysfs_id[] = {
352	{ DRV_NAME, 0 },
353	{}
354};
355MODULE_DEVICE_TABLE(platform, cros_ec_sysfs_id);
356
357static struct platform_driver cros_ec_sysfs_driver = {
358	.driver = {
359		.name = DRV_NAME,
360	},
361	.probe = cros_ec_sysfs_probe,
362	.remove = cros_ec_sysfs_remove,
363	.id_table = cros_ec_sysfs_id,
364};
365
366module_platform_driver(cros_ec_sysfs_driver);
367
368MODULE_LICENSE("GPL");
369MODULE_DESCRIPTION("Expose the ChromeOS EC through sysfs");
v4.17
  1/*
  2 * cros_ec_sysfs - expose the Chrome OS EC through sysfs
  3 *
  4 * Copyright (C) 2014 Google, Inc.
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#define pr_fmt(fmt) "cros_ec_sysfs: " fmt
 21
 22#include <linux/ctype.h>
 23#include <linux/delay.h>
 24#include <linux/device.h>
 25#include <linux/fs.h>
 26#include <linux/kobject.h>
 27#include <linux/mfd/cros_ec.h>
 28#include <linux/mfd/cros_ec_commands.h>
 29#include <linux/module.h>
 
 
 30#include <linux/platform_device.h>
 31#include <linux/printk.h>
 32#include <linux/slab.h>
 33#include <linux/stat.h>
 34#include <linux/types.h>
 35#include <linux/uaccess.h>
 36
 37#define to_cros_ec_dev(dev)  container_of(dev, struct cros_ec_dev, class_dev)
 38
 39/* Accessor functions */
 40
 41static ssize_t reboot_show(struct device *dev,
 42			   struct device_attribute *attr, char *buf)
 43{
 44	int count = 0;
 45
 46	count += scnprintf(buf + count, PAGE_SIZE - count,
 47			   "ro|rw|cancel|cold|disable-jump|hibernate");
 48	count += scnprintf(buf + count, PAGE_SIZE - count,
 49			   " [at-shutdown]\n");
 50	return count;
 51}
 52
 53static ssize_t reboot_store(struct device *dev,
 54			    struct device_attribute *attr,
 55			    const char *buf, size_t count)
 56{
 57	static const struct {
 58		const char * const str;
 59		uint8_t cmd;
 60		uint8_t flags;
 61	} words[] = {
 62		{"cancel",       EC_REBOOT_CANCEL, 0},
 63		{"ro",           EC_REBOOT_JUMP_RO, 0},
 64		{"rw",           EC_REBOOT_JUMP_RW, 0},
 
 65		{"cold",         EC_REBOOT_COLD, 0},
 66		{"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
 67		{"hibernate",    EC_REBOOT_HIBERNATE, 0},
 68		{"at-shutdown",  -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
 69	};
 70	struct cros_ec_command *msg;
 71	struct ec_params_reboot_ec *param;
 72	int got_cmd = 0, offset = 0;
 73	int i;
 74	int ret;
 75	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
 76
 77	msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
 78	if (!msg)
 79		return -ENOMEM;
 80
 81	param = (struct ec_params_reboot_ec *)msg->data;
 82
 83	param->flags = 0;
 84	while (1) {
 85		/* Find word to start scanning */
 86		while (buf[offset] && isspace(buf[offset]))
 87			offset++;
 88		if (!buf[offset])
 89			break;
 90
 91		for (i = 0; i < ARRAY_SIZE(words); i++) {
 92			if (!strncasecmp(words[i].str, buf+offset,
 93					 strlen(words[i].str))) {
 94				if (words[i].flags) {
 95					param->flags |= words[i].flags;
 96				} else {
 97					param->cmd = words[i].cmd;
 98					got_cmd = 1;
 99				}
100				break;
101			}
102		}
103
104		/* On to the next word, if any */
105		while (buf[offset] && !isspace(buf[offset]))
106			offset++;
107	}
108
109	if (!got_cmd) {
110		count = -EINVAL;
111		goto exit;
112	}
113
114	msg->version = 0;
115	msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
116	msg->outsize = sizeof(*param);
117	msg->insize = 0;
118	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
119	if (ret < 0)
120		count = ret;
121exit:
122	kfree(msg);
123	return count;
124}
125
126static ssize_t version_show(struct device *dev,
127			    struct device_attribute *attr, char *buf)
128{
129	static const char * const image_names[] = {"unknown", "RO", "RW"};
130	struct ec_response_get_version *r_ver;
131	struct ec_response_get_chip_info *r_chip;
132	struct ec_response_board_version *r_board;
133	struct cros_ec_command *msg;
134	int ret;
135	int count = 0;
136	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
137
138	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
139	if (!msg)
140		return -ENOMEM;
141
142	/* Get versions. RW may change. */
143	msg->version = 0;
144	msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
145	msg->insize = sizeof(*r_ver);
146	msg->outsize = 0;
147	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
148	if (ret < 0) {
149		count = ret;
150		goto exit;
151	}
152	r_ver = (struct ec_response_get_version *)msg->data;
153	/* Strings should be null-terminated, but let's be sure. */
154	r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
155	r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
156	count += scnprintf(buf + count, PAGE_SIZE - count,
157			   "RO version:    %s\n", r_ver->version_string_ro);
158	count += scnprintf(buf + count, PAGE_SIZE - count,
159			   "RW version:    %s\n", r_ver->version_string_rw);
160	count += scnprintf(buf + count, PAGE_SIZE - count,
161			   "Firmware copy: %s\n",
162			   (r_ver->current_image < ARRAY_SIZE(image_names) ?
163			    image_names[r_ver->current_image] : "?"));
164
165	/* Get build info. */
166	msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
167	msg->insize = EC_HOST_PARAM_SIZE;
168	ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
169	if (ret < 0)
170		count += scnprintf(buf + count, PAGE_SIZE - count,
171				   "Build info:    XFER ERROR %d\n", ret);
172	else if (msg->result != EC_RES_SUCCESS)
173		count += scnprintf(buf + count, PAGE_SIZE - count,
174				   "Build info:    EC error %d\n", msg->result);
175	else {
176		msg->data[EC_HOST_PARAM_SIZE - 1] = '\0';
177		count += scnprintf(buf + count, PAGE_SIZE - count,
178				   "Build info:    %s\n", msg->data);
179	}
180
181	/* Get chip info. */
182	msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset;
183	msg->insize = sizeof(*r_chip);
184	ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
185	if (ret < 0)
186		count += scnprintf(buf + count, PAGE_SIZE - count,
187				   "Chip info:     XFER ERROR %d\n", ret);
188	else if (msg->result != EC_RES_SUCCESS)
189		count += scnprintf(buf + count, PAGE_SIZE - count,
190				   "Chip info:     EC error %d\n", msg->result);
191	else {
192		r_chip = (struct ec_response_get_chip_info *)msg->data;
193
194		r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
195		r_chip->name[sizeof(r_chip->name) - 1] = '\0';
196		r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
197		count += scnprintf(buf + count, PAGE_SIZE - count,
198				   "Chip vendor:   %s\n", r_chip->vendor);
199		count += scnprintf(buf + count, PAGE_SIZE - count,
200				   "Chip name:     %s\n", r_chip->name);
201		count += scnprintf(buf + count, PAGE_SIZE - count,
202				   "Chip revision: %s\n", r_chip->revision);
203	}
204
205	/* Get board version */
206	msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset;
207	msg->insize = sizeof(*r_board);
208	ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
209	if (ret < 0)
210		count += scnprintf(buf + count, PAGE_SIZE - count,
211				   "Board version: XFER ERROR %d\n", ret);
212	else if (msg->result != EC_RES_SUCCESS)
213		count += scnprintf(buf + count, PAGE_SIZE - count,
214				   "Board version: EC error %d\n", msg->result);
215	else {
216		r_board = (struct ec_response_board_version *)msg->data;
217
218		count += scnprintf(buf + count, PAGE_SIZE - count,
219				   "Board version: %d\n",
220				   r_board->board_version);
221	}
222
223exit:
224	kfree(msg);
225	return count;
226}
227
228static ssize_t flashinfo_show(struct device *dev,
229			      struct device_attribute *attr, char *buf)
230{
231	struct ec_response_flash_info *resp;
232	struct cros_ec_command *msg;
233	int ret;
234	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
235
236	msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
237	if (!msg)
238		return -ENOMEM;
239
240	/* The flash info shouldn't ever change, but ask each time anyway. */
241	msg->version = 0;
242	msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
243	msg->insize = sizeof(*resp);
244	msg->outsize = 0;
245	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
246	if (ret < 0)
247		goto exit;
248
249	resp = (struct ec_response_flash_info *)msg->data;
250
251	ret = scnprintf(buf, PAGE_SIZE,
252			"FlashSize %d\nWriteSize %d\n"
253			"EraseSize %d\nProtectSize %d\n",
254			resp->flash_size, resp->write_block_size,
255			resp->erase_block_size, resp->protect_block_size);
256exit:
257	kfree(msg);
258	return ret;
259}
260
261/* Keyboard wake angle control */
262static ssize_t kb_wake_angle_show(struct device *dev,
263				  struct device_attribute *attr, char *buf)
264{
265	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
266	struct ec_response_motion_sense *resp;
267	struct ec_params_motion_sense *param;
268	struct cros_ec_command *msg;
269	int ret;
270
271	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
272	if (!msg)
273		return -ENOMEM;
274
275	param = (struct ec_params_motion_sense *)msg->data;
276	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
277	msg->version = 2;
278	param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
279	param->kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
280	msg->outsize = sizeof(*param);
281	msg->insize = sizeof(*resp);
282
283	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
284	if (ret < 0)
285		goto exit;
286
287	resp = (struct ec_response_motion_sense *)msg->data;
288	ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->kb_wake_angle.ret);
289exit:
290	kfree(msg);
291	return ret;
292}
293
294static ssize_t kb_wake_angle_store(struct device *dev,
295				   struct device_attribute *attr,
296				   const char *buf, size_t count)
297{
298	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
299	struct ec_params_motion_sense *param;
300	struct cros_ec_command *msg;
301	u16 angle;
302	int ret;
303
304	ret = kstrtou16(buf, 0, &angle);
305	if (ret)
306		return ret;
307
308	msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
309	if (!msg)
310		return -ENOMEM;
311
312	param = (struct ec_params_motion_sense *)msg->data;
313	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
314	msg->version = 2;
315	param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
316	param->kb_wake_angle.data = angle;
317	msg->outsize = sizeof(*param);
318	msg->insize = sizeof(struct ec_response_motion_sense);
319
320	ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
321	kfree(msg);
322	if (ret < 0)
323		return ret;
324	return count;
325}
326
327/* Module initialization */
328
329static DEVICE_ATTR_RW(reboot);
330static DEVICE_ATTR_RO(version);
331static DEVICE_ATTR_RO(flashinfo);
332static DEVICE_ATTR_RW(kb_wake_angle);
333
334static struct attribute *__ec_attrs[] = {
335	&dev_attr_kb_wake_angle.attr,
336	&dev_attr_reboot.attr,
337	&dev_attr_version.attr,
338	&dev_attr_flashinfo.attr,
339	NULL,
340};
341
342static umode_t cros_ec_ctrl_visible(struct kobject *kobj,
343				    struct attribute *a, int n)
344{
345	struct device *dev = container_of(kobj, struct device, kobj);
346	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
347
348	if (a == &dev_attr_kb_wake_angle.attr && !ec->has_kb_wake_angle)
349		return 0;
350
351	return a->mode;
352}
353
354struct attribute_group cros_ec_attr_group = {
355	.attrs = __ec_attrs,
356	.is_visible = cros_ec_ctrl_visible,
357};
358EXPORT_SYMBOL(cros_ec_attr_group);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
360MODULE_LICENSE("GPL");
361MODULE_DESCRIPTION("ChromeOS EC control driver");