xref: /OK3568_Linux_fs/kernel/drivers/platform/chrome/wilco_ec/telemetry.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Telemetry communication for Wilco EC
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2019 Google LLC
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * The Wilco Embedded Controller is able to send telemetry data
8*4882a593Smuzhiyun  * which is useful for enterprise applications. A daemon running on
9*4882a593Smuzhiyun  * the OS sends a command to the EC via a write() to a char device,
10*4882a593Smuzhiyun  * and can read the response with a read(). The write() request is
11*4882a593Smuzhiyun  * verified by the driver to ensure that it is performing only one
12*4882a593Smuzhiyun  * of the allowlisted commands, and that no extraneous data is
13*4882a593Smuzhiyun  * being transmitted to the EC. The response is passed directly
14*4882a593Smuzhiyun  * back to the reader with no modification.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * The character device will appear as /dev/wilco_telemN, where N
17*4882a593Smuzhiyun  * is some small non-negative integer, starting with 0. Only one
18*4882a593Smuzhiyun  * process may have the file descriptor open at a time. The calling
19*4882a593Smuzhiyun  * userspace program needs to keep the device file descriptor open
20*4882a593Smuzhiyun  * between the calls to write() and read() in order to preserve the
21*4882a593Smuzhiyun  * response. Up to 32 bytes will be available for reading.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * For testing purposes, try requesting the EC's firmware build
24*4882a593Smuzhiyun  * date, by sending the WILCO_EC_TELEM_GET_VERSION command with
25*4882a593Smuzhiyun  * argument index=3. i.e. write [0x38, 0x00, 0x03]
26*4882a593Smuzhiyun  * to the device node. An ASCII string of the build date is
27*4882a593Smuzhiyun  * returned.
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include <linux/cdev.h>
31*4882a593Smuzhiyun #include <linux/device.h>
32*4882a593Smuzhiyun #include <linux/fs.h>
33*4882a593Smuzhiyun #include <linux/module.h>
34*4882a593Smuzhiyun #include <linux/platform_data/wilco-ec.h>
35*4882a593Smuzhiyun #include <linux/platform_device.h>
36*4882a593Smuzhiyun #include <linux/slab.h>
37*4882a593Smuzhiyun #include <linux/types.h>
38*4882a593Smuzhiyun #include <linux/uaccess.h>
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define TELEM_DEV_NAME		"wilco_telem"
41*4882a593Smuzhiyun #define TELEM_CLASS_NAME	TELEM_DEV_NAME
42*4882a593Smuzhiyun #define DRV_NAME		TELEM_DEV_NAME
43*4882a593Smuzhiyun #define TELEM_DEV_NAME_FMT	(TELEM_DEV_NAME "%d")
44*4882a593Smuzhiyun static struct class telem_class = {
45*4882a593Smuzhiyun 	.owner	= THIS_MODULE,
46*4882a593Smuzhiyun 	.name	= TELEM_CLASS_NAME,
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /* Keep track of all the device numbers used. */
50*4882a593Smuzhiyun #define TELEM_MAX_DEV 128
51*4882a593Smuzhiyun static int telem_major;
52*4882a593Smuzhiyun static DEFINE_IDA(telem_ida);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /* EC telemetry command codes */
55*4882a593Smuzhiyun #define WILCO_EC_TELEM_GET_LOG			0x99
56*4882a593Smuzhiyun #define WILCO_EC_TELEM_GET_VERSION		0x38
57*4882a593Smuzhiyun #define WILCO_EC_TELEM_GET_FAN_INFO		0x2E
58*4882a593Smuzhiyun #define WILCO_EC_TELEM_GET_DIAG_INFO		0xFA
59*4882a593Smuzhiyun #define WILCO_EC_TELEM_GET_TEMP_INFO		0x95
60*4882a593Smuzhiyun #define WILCO_EC_TELEM_GET_TEMP_READ		0x2C
61*4882a593Smuzhiyun #define WILCO_EC_TELEM_GET_BATT_EXT_INFO	0x07
62*4882a593Smuzhiyun #define WILCO_EC_TELEM_GET_BATT_PPID_INFO	0x8A
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #define TELEM_ARGS_SIZE_MAX	30
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun  * The following telem_args_get_* structs are embedded within the |args| field
68*4882a593Smuzhiyun  * of wilco_ec_telem_request.
69*4882a593Smuzhiyun  */
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun struct telem_args_get_log {
72*4882a593Smuzhiyun 	u8 log_type;
73*4882a593Smuzhiyun 	u8 log_index;
74*4882a593Smuzhiyun } __packed;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun  * Get a piece of info about the EC firmware version:
78*4882a593Smuzhiyun  * 0 = label
79*4882a593Smuzhiyun  * 1 = svn_rev
80*4882a593Smuzhiyun  * 2 = model_no
81*4882a593Smuzhiyun  * 3 = build_date
82*4882a593Smuzhiyun  * 4 = frio_version
83*4882a593Smuzhiyun  */
84*4882a593Smuzhiyun struct telem_args_get_version {
85*4882a593Smuzhiyun 	u8 index;
86*4882a593Smuzhiyun } __packed;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun struct telem_args_get_fan_info {
89*4882a593Smuzhiyun 	u8 command;
90*4882a593Smuzhiyun 	u8 fan_number;
91*4882a593Smuzhiyun 	u8 arg;
92*4882a593Smuzhiyun } __packed;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun struct telem_args_get_diag_info {
95*4882a593Smuzhiyun 	u8 type;
96*4882a593Smuzhiyun 	u8 sub_type;
97*4882a593Smuzhiyun } __packed;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun struct telem_args_get_temp_info {
100*4882a593Smuzhiyun 	u8 command;
101*4882a593Smuzhiyun 	u8 index;
102*4882a593Smuzhiyun 	u8 field;
103*4882a593Smuzhiyun 	u8 zone;
104*4882a593Smuzhiyun } __packed;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun struct telem_args_get_temp_read {
107*4882a593Smuzhiyun 	u8 sensor_index;
108*4882a593Smuzhiyun } __packed;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun struct telem_args_get_batt_ext_info {
111*4882a593Smuzhiyun 	u8 var_args[5];
112*4882a593Smuzhiyun } __packed;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun struct telem_args_get_batt_ppid_info {
115*4882a593Smuzhiyun 	u8 always1; /* Should always be 1 */
116*4882a593Smuzhiyun } __packed;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun  * struct wilco_ec_telem_request - Telemetry command and arguments sent to EC.
120*4882a593Smuzhiyun  * @command: One of WILCO_EC_TELEM_GET_* command codes.
121*4882a593Smuzhiyun  * @reserved: Must be 0.
122*4882a593Smuzhiyun  * @args: The first N bytes are one of telem_args_get_* structs, the rest is 0.
123*4882a593Smuzhiyun  */
124*4882a593Smuzhiyun struct wilco_ec_telem_request {
125*4882a593Smuzhiyun 	u8 command;
126*4882a593Smuzhiyun 	u8 reserved;
127*4882a593Smuzhiyun 	union {
128*4882a593Smuzhiyun 		u8 buf[TELEM_ARGS_SIZE_MAX];
129*4882a593Smuzhiyun 		struct telem_args_get_log		get_log;
130*4882a593Smuzhiyun 		struct telem_args_get_version		get_version;
131*4882a593Smuzhiyun 		struct telem_args_get_fan_info		get_fan_info;
132*4882a593Smuzhiyun 		struct telem_args_get_diag_info		get_diag_info;
133*4882a593Smuzhiyun 		struct telem_args_get_temp_info		get_temp_info;
134*4882a593Smuzhiyun 		struct telem_args_get_temp_read		get_temp_read;
135*4882a593Smuzhiyun 		struct telem_args_get_batt_ext_info	get_batt_ext_info;
136*4882a593Smuzhiyun 		struct telem_args_get_batt_ppid_info	get_batt_ppid_info;
137*4882a593Smuzhiyun 	} args;
138*4882a593Smuzhiyun } __packed;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun  * check_telem_request() - Ensure that a request from userspace is valid.
142*4882a593Smuzhiyun  * @rq: Request buffer copied from userspace.
143*4882a593Smuzhiyun  * @size: Number of bytes copied from userspace.
144*4882a593Smuzhiyun  *
145*4882a593Smuzhiyun  * Return: 0 if valid, -EINVAL if bad command or reserved byte is non-zero,
146*4882a593Smuzhiyun  *         -EMSGSIZE if the request is too long.
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * We do not want to allow userspace to send arbitrary telemetry commands to
149*4882a593Smuzhiyun  * the EC. Therefore we check to ensure that
150*4882a593Smuzhiyun  * 1. The request follows the format of struct wilco_ec_telem_request.
151*4882a593Smuzhiyun  * 2. The supplied command code is one of the allowlisted commands.
152*4882a593Smuzhiyun  * 3. The request only contains the necessary data for the header and arguments.
153*4882a593Smuzhiyun  */
check_telem_request(struct wilco_ec_telem_request * rq,size_t size)154*4882a593Smuzhiyun static int check_telem_request(struct wilco_ec_telem_request *rq,
155*4882a593Smuzhiyun 			       size_t size)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	size_t max_size = offsetof(struct wilco_ec_telem_request, args);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (rq->reserved)
160*4882a593Smuzhiyun 		return -EINVAL;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	switch (rq->command) {
163*4882a593Smuzhiyun 	case WILCO_EC_TELEM_GET_LOG:
164*4882a593Smuzhiyun 		max_size += sizeof(rq->args.get_log);
165*4882a593Smuzhiyun 		break;
166*4882a593Smuzhiyun 	case WILCO_EC_TELEM_GET_VERSION:
167*4882a593Smuzhiyun 		max_size += sizeof(rq->args.get_version);
168*4882a593Smuzhiyun 		break;
169*4882a593Smuzhiyun 	case WILCO_EC_TELEM_GET_FAN_INFO:
170*4882a593Smuzhiyun 		max_size += sizeof(rq->args.get_fan_info);
171*4882a593Smuzhiyun 		break;
172*4882a593Smuzhiyun 	case WILCO_EC_TELEM_GET_DIAG_INFO:
173*4882a593Smuzhiyun 		max_size += sizeof(rq->args.get_diag_info);
174*4882a593Smuzhiyun 		break;
175*4882a593Smuzhiyun 	case WILCO_EC_TELEM_GET_TEMP_INFO:
176*4882a593Smuzhiyun 		max_size += sizeof(rq->args.get_temp_info);
177*4882a593Smuzhiyun 		break;
178*4882a593Smuzhiyun 	case WILCO_EC_TELEM_GET_TEMP_READ:
179*4882a593Smuzhiyun 		max_size += sizeof(rq->args.get_temp_read);
180*4882a593Smuzhiyun 		break;
181*4882a593Smuzhiyun 	case WILCO_EC_TELEM_GET_BATT_EXT_INFO:
182*4882a593Smuzhiyun 		max_size += sizeof(rq->args.get_batt_ext_info);
183*4882a593Smuzhiyun 		break;
184*4882a593Smuzhiyun 	case WILCO_EC_TELEM_GET_BATT_PPID_INFO:
185*4882a593Smuzhiyun 		if (rq->args.get_batt_ppid_info.always1 != 1)
186*4882a593Smuzhiyun 			return -EINVAL;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 		max_size += sizeof(rq->args.get_batt_ppid_info);
189*4882a593Smuzhiyun 		break;
190*4882a593Smuzhiyun 	default:
191*4882a593Smuzhiyun 		return -EINVAL;
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	return (size <= max_size) ? 0 : -EMSGSIZE;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /**
198*4882a593Smuzhiyun  * struct telem_device_data - Data for a Wilco EC device that queries telemetry.
199*4882a593Smuzhiyun  * @cdev: Char dev that userspace reads and polls from.
200*4882a593Smuzhiyun  * @dev: Device associated with the %cdev.
201*4882a593Smuzhiyun  * @ec: Wilco EC that we will be communicating with using the mailbox interface.
202*4882a593Smuzhiyun  * @available: Boolean of if the device can be opened.
203*4882a593Smuzhiyun  */
204*4882a593Smuzhiyun struct telem_device_data {
205*4882a593Smuzhiyun 	struct device dev;
206*4882a593Smuzhiyun 	struct cdev cdev;
207*4882a593Smuzhiyun 	struct wilco_ec_device *ec;
208*4882a593Smuzhiyun 	atomic_t available;
209*4882a593Smuzhiyun };
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun #define TELEM_RESPONSE_SIZE	EC_MAILBOX_DATA_SIZE
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun /**
214*4882a593Smuzhiyun  * struct telem_session_data - Data that exists between open() and release().
215*4882a593Smuzhiyun  * @dev_data: Pointer to get back to the device data and EC.
216*4882a593Smuzhiyun  * @request: Command and arguments sent to EC.
217*4882a593Smuzhiyun  * @response: Response buffer of data from EC.
218*4882a593Smuzhiyun  * @has_msg: Is there data available to read from a previous write?
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun struct telem_session_data {
221*4882a593Smuzhiyun 	struct telem_device_data *dev_data;
222*4882a593Smuzhiyun 	struct wilco_ec_telem_request request;
223*4882a593Smuzhiyun 	u8 response[TELEM_RESPONSE_SIZE];
224*4882a593Smuzhiyun 	bool has_msg;
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /**
228*4882a593Smuzhiyun  * telem_open() - Callback for when the device node is opened.
229*4882a593Smuzhiyun  * @inode: inode for this char device node.
230*4882a593Smuzhiyun  * @filp: file for this char device node.
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  * We need to ensure that after writing a command to the device,
233*4882a593Smuzhiyun  * the same userspace process reads the corresponding result.
234*4882a593Smuzhiyun  * Therefore, we increment a refcount on opening the device, so that
235*4882a593Smuzhiyun  * only one process can communicate with the EC at a time.
236*4882a593Smuzhiyun  *
237*4882a593Smuzhiyun  * Return: 0 on success, or negative error code on failure.
238*4882a593Smuzhiyun  */
telem_open(struct inode * inode,struct file * filp)239*4882a593Smuzhiyun static int telem_open(struct inode *inode, struct file *filp)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	struct telem_device_data *dev_data;
242*4882a593Smuzhiyun 	struct telem_session_data *sess_data;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* Ensure device isn't already open */
245*4882a593Smuzhiyun 	dev_data = container_of(inode->i_cdev, struct telem_device_data, cdev);
246*4882a593Smuzhiyun 	if (atomic_cmpxchg(&dev_data->available, 1, 0) == 0)
247*4882a593Smuzhiyun 		return -EBUSY;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	get_device(&dev_data->dev);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	sess_data = kzalloc(sizeof(*sess_data), GFP_KERNEL);
252*4882a593Smuzhiyun 	if (!sess_data) {
253*4882a593Smuzhiyun 		atomic_set(&dev_data->available, 1);
254*4882a593Smuzhiyun 		return -ENOMEM;
255*4882a593Smuzhiyun 	}
256*4882a593Smuzhiyun 	sess_data->dev_data = dev_data;
257*4882a593Smuzhiyun 	sess_data->has_msg = false;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	nonseekable_open(inode, filp);
260*4882a593Smuzhiyun 	filp->private_data = sess_data;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	return 0;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
telem_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)265*4882a593Smuzhiyun static ssize_t telem_write(struct file *filp, const char __user *buf,
266*4882a593Smuzhiyun 			   size_t count, loff_t *pos)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	struct telem_session_data *sess_data = filp->private_data;
269*4882a593Smuzhiyun 	struct wilco_ec_message msg = {};
270*4882a593Smuzhiyun 	int ret;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	if (count > sizeof(sess_data->request))
273*4882a593Smuzhiyun 		return -EMSGSIZE;
274*4882a593Smuzhiyun 	memset(&sess_data->request, 0, sizeof(sess_data->request));
275*4882a593Smuzhiyun 	if (copy_from_user(&sess_data->request, buf, count))
276*4882a593Smuzhiyun 		return -EFAULT;
277*4882a593Smuzhiyun 	ret = check_telem_request(&sess_data->request, count);
278*4882a593Smuzhiyun 	if (ret < 0)
279*4882a593Smuzhiyun 		return ret;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	memset(sess_data->response, 0, sizeof(sess_data->response));
282*4882a593Smuzhiyun 	msg.type = WILCO_EC_MSG_TELEMETRY;
283*4882a593Smuzhiyun 	msg.request_data = &sess_data->request;
284*4882a593Smuzhiyun 	msg.request_size = sizeof(sess_data->request);
285*4882a593Smuzhiyun 	msg.response_data = sess_data->response;
286*4882a593Smuzhiyun 	msg.response_size = sizeof(sess_data->response);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	ret = wilco_ec_mailbox(sess_data->dev_data->ec, &msg);
289*4882a593Smuzhiyun 	if (ret < 0)
290*4882a593Smuzhiyun 		return ret;
291*4882a593Smuzhiyun 	if (ret != sizeof(sess_data->response))
292*4882a593Smuzhiyun 		return -EMSGSIZE;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	sess_data->has_msg = true;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	return count;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
telem_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)299*4882a593Smuzhiyun static ssize_t telem_read(struct file *filp, char __user *buf, size_t count,
300*4882a593Smuzhiyun 			  loff_t *pos)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	struct telem_session_data *sess_data = filp->private_data;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (!sess_data->has_msg)
305*4882a593Smuzhiyun 		return -ENODATA;
306*4882a593Smuzhiyun 	if (count > sizeof(sess_data->response))
307*4882a593Smuzhiyun 		return -EINVAL;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	if (copy_to_user(buf, sess_data->response, count))
310*4882a593Smuzhiyun 		return -EFAULT;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	sess_data->has_msg = false;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	return count;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun 
telem_release(struct inode * inode,struct file * filp)317*4882a593Smuzhiyun static int telem_release(struct inode *inode, struct file *filp)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	struct telem_session_data *sess_data = filp->private_data;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	atomic_set(&sess_data->dev_data->available, 1);
322*4882a593Smuzhiyun 	put_device(&sess_data->dev_data->dev);
323*4882a593Smuzhiyun 	kfree(sess_data);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	return 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun static const struct file_operations telem_fops = {
329*4882a593Smuzhiyun 	.open = telem_open,
330*4882a593Smuzhiyun 	.write = telem_write,
331*4882a593Smuzhiyun 	.read = telem_read,
332*4882a593Smuzhiyun 	.release = telem_release,
333*4882a593Smuzhiyun 	.llseek = no_llseek,
334*4882a593Smuzhiyun 	.owner = THIS_MODULE,
335*4882a593Smuzhiyun };
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun /**
338*4882a593Smuzhiyun  * telem_device_free() - Callback to free the telem_device_data structure.
339*4882a593Smuzhiyun  * @d: The device embedded in our device data, which we have been ref counting.
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * Once all open file descriptors are closed and the device has been removed,
342*4882a593Smuzhiyun  * the refcount of the device will fall to 0 and this will be called.
343*4882a593Smuzhiyun  */
telem_device_free(struct device * d)344*4882a593Smuzhiyun static void telem_device_free(struct device *d)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	struct telem_device_data *dev_data;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	dev_data = container_of(d, struct telem_device_data, dev);
349*4882a593Smuzhiyun 	kfree(dev_data);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun /**
353*4882a593Smuzhiyun  * telem_device_probe() - Callback when creating a new device.
354*4882a593Smuzhiyun  * @pdev: platform device that we will be receiving telems from.
355*4882a593Smuzhiyun  *
356*4882a593Smuzhiyun  * This finds a free minor number for the device, allocates and initializes
357*4882a593Smuzhiyun  * some device data, and creates a new device and char dev node.
358*4882a593Smuzhiyun  *
359*4882a593Smuzhiyun  * Return: 0 on success, negative error code on failure.
360*4882a593Smuzhiyun  */
telem_device_probe(struct platform_device * pdev)361*4882a593Smuzhiyun static int telem_device_probe(struct platform_device *pdev)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	struct telem_device_data *dev_data;
364*4882a593Smuzhiyun 	int error, minor;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	/* Get the next available device number */
367*4882a593Smuzhiyun 	minor = ida_alloc_max(&telem_ida, TELEM_MAX_DEV-1, GFP_KERNEL);
368*4882a593Smuzhiyun 	if (minor < 0) {
369*4882a593Smuzhiyun 		error = minor;
370*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to find minor number: %d\n", error);
371*4882a593Smuzhiyun 		return error;
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
375*4882a593Smuzhiyun 	if (!dev_data) {
376*4882a593Smuzhiyun 		ida_simple_remove(&telem_ida, minor);
377*4882a593Smuzhiyun 		return -ENOMEM;
378*4882a593Smuzhiyun 	}
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	/* Initialize the device data */
381*4882a593Smuzhiyun 	dev_data->ec = dev_get_platdata(&pdev->dev);
382*4882a593Smuzhiyun 	atomic_set(&dev_data->available, 1);
383*4882a593Smuzhiyun 	platform_set_drvdata(pdev, dev_data);
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	/* Initialize the device */
386*4882a593Smuzhiyun 	dev_data->dev.devt = MKDEV(telem_major, minor);
387*4882a593Smuzhiyun 	dev_data->dev.class = &telem_class;
388*4882a593Smuzhiyun 	dev_data->dev.release = telem_device_free;
389*4882a593Smuzhiyun 	dev_set_name(&dev_data->dev, TELEM_DEV_NAME_FMT, minor);
390*4882a593Smuzhiyun 	device_initialize(&dev_data->dev);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	/* Initialize the character device and add it to userspace */;
393*4882a593Smuzhiyun 	cdev_init(&dev_data->cdev, &telem_fops);
394*4882a593Smuzhiyun 	error = cdev_device_add(&dev_data->cdev, &dev_data->dev);
395*4882a593Smuzhiyun 	if (error) {
396*4882a593Smuzhiyun 		put_device(&dev_data->dev);
397*4882a593Smuzhiyun 		ida_simple_remove(&telem_ida, minor);
398*4882a593Smuzhiyun 		return error;
399*4882a593Smuzhiyun 	}
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	return 0;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
telem_device_remove(struct platform_device * pdev)404*4882a593Smuzhiyun static int telem_device_remove(struct platform_device *pdev)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	struct telem_device_data *dev_data = platform_get_drvdata(pdev);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	cdev_device_del(&dev_data->cdev, &dev_data->dev);
409*4882a593Smuzhiyun 	ida_simple_remove(&telem_ida, MINOR(dev_data->dev.devt));
410*4882a593Smuzhiyun 	put_device(&dev_data->dev);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	return 0;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun static struct platform_driver telem_driver = {
416*4882a593Smuzhiyun 	.probe = telem_device_probe,
417*4882a593Smuzhiyun 	.remove = telem_device_remove,
418*4882a593Smuzhiyun 	.driver = {
419*4882a593Smuzhiyun 		.name = DRV_NAME,
420*4882a593Smuzhiyun 	},
421*4882a593Smuzhiyun };
422*4882a593Smuzhiyun 
telem_module_init(void)423*4882a593Smuzhiyun static int __init telem_module_init(void)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	dev_t dev_num = 0;
426*4882a593Smuzhiyun 	int ret;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	ret = class_register(&telem_class);
429*4882a593Smuzhiyun 	if (ret) {
430*4882a593Smuzhiyun 		pr_err(DRV_NAME ": Failed registering class: %d\n", ret);
431*4882a593Smuzhiyun 		return ret;
432*4882a593Smuzhiyun 	}
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/* Request the kernel for device numbers, starting with minor=0 */
435*4882a593Smuzhiyun 	ret = alloc_chrdev_region(&dev_num, 0, TELEM_MAX_DEV, TELEM_DEV_NAME);
436*4882a593Smuzhiyun 	if (ret) {
437*4882a593Smuzhiyun 		pr_err(DRV_NAME ": Failed allocating dev numbers: %d\n", ret);
438*4882a593Smuzhiyun 		goto destroy_class;
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun 	telem_major = MAJOR(dev_num);
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	ret = platform_driver_register(&telem_driver);
443*4882a593Smuzhiyun 	if (ret < 0) {
444*4882a593Smuzhiyun 		pr_err(DRV_NAME ": Failed registering driver: %d\n", ret);
445*4882a593Smuzhiyun 		goto unregister_region;
446*4882a593Smuzhiyun 	}
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	return 0;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun unregister_region:
451*4882a593Smuzhiyun 	unregister_chrdev_region(MKDEV(telem_major, 0), TELEM_MAX_DEV);
452*4882a593Smuzhiyun destroy_class:
453*4882a593Smuzhiyun 	class_unregister(&telem_class);
454*4882a593Smuzhiyun 	ida_destroy(&telem_ida);
455*4882a593Smuzhiyun 	return ret;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun 
telem_module_exit(void)458*4882a593Smuzhiyun static void __exit telem_module_exit(void)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	platform_driver_unregister(&telem_driver);
461*4882a593Smuzhiyun 	unregister_chrdev_region(MKDEV(telem_major, 0), TELEM_MAX_DEV);
462*4882a593Smuzhiyun 	class_unregister(&telem_class);
463*4882a593Smuzhiyun 	ida_destroy(&telem_ida);
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun module_init(telem_module_init);
467*4882a593Smuzhiyun module_exit(telem_module_exit);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
470*4882a593Smuzhiyun MODULE_DESCRIPTION("Wilco EC telemetry driver");
471*4882a593Smuzhiyun MODULE_LICENSE("GPL");
472*4882a593Smuzhiyun MODULE_ALIAS("platform:" DRV_NAME);
473