File Index
Files and directories
Sign in
FILE

ishtp-hid-client.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hid/intel-ish-hid/ishtp-hid-client.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ISHTP client driver for HID (ISH)
  4 *
  5 * Copyright (c) 2014-2016, Intel Corporation.
  6 */
  7
  8#include <linux/module.h>
  9#include <linux/hid.h>
 10#include <linux/intel-ish-client-if.h>
 11#include <linux/sched.h>
 12#include "ishtp-hid.h"
 13
 14/* ISH Transport protocol (ISHTP in short) GUID */
 15static const struct ishtp_device_id hid_ishtp_id_table[] = {
 16	{ .guid = GUID_INIT(0x33AECD58, 0xB679, 0x4E54,
 17		  0x9B, 0xD9, 0xA0, 0x4D, 0x34, 0xF0, 0xC2, 0x26), },
 18	{ }
 19};
 20MODULE_DEVICE_TABLE(ishtp, hid_ishtp_id_table);
 21
 22/* Rx ring buffer pool size */
 23#define HID_CL_RX_RING_SIZE	32
 24#define HID_CL_TX_RING_SIZE	16
 25
 26#define cl_data_to_dev(client_data) ishtp_device(client_data->cl_device)
 27
 28/**
 29 * report_bad_packet() - Report bad packets
 30 * @hid_ishtp_cl:	Client instance to get stats
 31 * @recv_buf:		Raw received host interface message
 32 * @cur_pos:		Current position index in payload
 33 * @payload_len:	Length of payload expected
 34 *
 35 * Dumps error in case bad packet is received
 36 */
 37static void report_bad_packet(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 38			      size_t cur_pos,  size_t payload_len)
 39{
 40	struct hostif_msg *recv_msg = recv_buf;
 41	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
 42
 43	dev_err(cl_data_to_dev(client_data), "[hid-ish]: BAD packet %02X\n"
 44		"total_bad=%u cur_pos=%u\n"
 45		"[%02X %02X %02X %02X]\n"
 46		"payload_len=%u\n"
 47		"multi_packet_cnt=%u\n"
 48		"is_response=%02X\n",
 49		recv_msg->hdr.command, client_data->bad_recv_cnt,
 50		(unsigned int)cur_pos,
 51		((unsigned char *)recv_msg)[0], ((unsigned char *)recv_msg)[1],
 52		((unsigned char *)recv_msg)[2], ((unsigned char *)recv_msg)[3],
 53		(unsigned int)payload_len, client_data->multi_packet_cnt,
 54		recv_msg->hdr.command & ~CMD_MASK);
 55}
 56
 57/**
 58 * process_recv() - Received and parse incoming packet
 59 * @hid_ishtp_cl:	Client instance to get stats
 60 * @recv_buf:		Raw received host interface message
 61 * @data_len:		length of the message
 62 *
 63 * Parse the incoming packet. If it is a response packet then it will update
 64 * per instance flags and wake up the caller waiting to for the response.
 65 */
 66static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 67			 size_t data_len)
 68{
 69	struct hostif_msg *recv_msg;
 70	unsigned char *payload;
 71	struct device_info *dev_info;
 72	int i, j;
 73	size_t	payload_len, total_len, cur_pos, raw_len, msg_len;
 74	int report_type;
 75	struct report_list *reports_list;
 76	struct report *report;
 77	size_t report_len;
 78	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
 79	int curr_hid_dev = client_data->cur_hid_dev;
 80	struct ishtp_hid_data *hid_data = NULL;
 81	struct hid_device *hid = NULL;
 82
 83	payload = recv_buf + sizeof(struct hostif_msg_hdr);
 84	total_len = data_len;
 85	cur_pos = 0;
 86
 87	do {
 88		if (cur_pos + sizeof(struct hostif_msg) > total_len) {
 89			dev_err(cl_data_to_dev(client_data),
 90				"[hid-ish]: error, received %u which is less than data header %u\n",
 91				(unsigned int)data_len,
 92				(unsigned int)sizeof(struct hostif_msg_hdr));
 93			++client_data->bad_recv_cnt;
 94			ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
 95			break;
 96		}
 97
 98		recv_msg = (struct hostif_msg *)(recv_buf + cur_pos);
 99		payload_len = recv_msg->hdr.size;
100
101		/* Sanity checks */
102		if (cur_pos + payload_len + sizeof(struct hostif_msg) >
103				total_len) {
104			++client_data->bad_recv_cnt;
105			report_bad_packet(hid_ishtp_cl, recv_msg, cur_pos,
106					  payload_len);
107			ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
108			break;
109		}
110
111		hid_ishtp_trace(client_data,  "%s %d\n",
112				__func__, recv_msg->hdr.command & CMD_MASK);
113
114		switch (recv_msg->hdr.command & CMD_MASK) {
115		case HOSTIF_DM_ENUM_DEVICES:
116			if ((!(recv_msg->hdr.command & ~CMD_MASK) ||
117					client_data->init_done)) {
118				++client_data->bad_recv_cnt;
119				report_bad_packet(hid_ishtp_cl, recv_msg,
120						  cur_pos,
121						  payload_len);
122				ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
123				break;
124			}
125			client_data->hid_dev_count = (unsigned int)*payload;
126			if (!client_data->hid_devices)
127				client_data->hid_devices = devm_kcalloc(
128						cl_data_to_dev(client_data),
129						client_data->hid_dev_count,
130						sizeof(struct device_info),
131						GFP_KERNEL);
132			if (!client_data->hid_devices) {
133				dev_err(cl_data_to_dev(client_data),
134				"Mem alloc failed for hid device info\n");
135				wake_up_interruptible(&client_data->init_wait);
136				break;
137			}
138			for (i = 0; i < client_data->hid_dev_count; ++i) {
139				if (1 + sizeof(struct device_info) * i >=
140						payload_len) {
141					dev_err(cl_data_to_dev(client_data),
142						"[hid-ish]: [ENUM_DEVICES]: content size %zu is bigger than payload_len %zu\n",
143						1 + sizeof(struct device_info)
144						* i, payload_len);
145				}
146
147				if (1 + sizeof(struct device_info) * i >=
148						data_len)
149					break;
150
151				dev_info = (struct device_info *)(payload + 1 +
152					sizeof(struct device_info) * i);
153				if (client_data->hid_devices)
154					memcpy(client_data->hid_devices + i,
155					       dev_info,
156					       sizeof(struct device_info));
157			}
158
159			client_data->enum_devices_done = true;
160			wake_up_interruptible(&client_data->init_wait);
161
162			break;
163
164		case HOSTIF_GET_HID_DESCRIPTOR:
165			if ((!(recv_msg->hdr.command & ~CMD_MASK) ||
166					client_data->init_done)) {
167				++client_data->bad_recv_cnt;
168				report_bad_packet(hid_ishtp_cl, recv_msg,
169						  cur_pos,
170						  payload_len);
171				ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
172				break;
173			}
174			if (!client_data->hid_descr[curr_hid_dev])
175				client_data->hid_descr[curr_hid_dev] =
176				devm_kmalloc(cl_data_to_dev(client_data),
177					     payload_len, GFP_KERNEL);
178			if (client_data->hid_descr[curr_hid_dev]) {
179				memcpy(client_data->hid_descr[curr_hid_dev],
180				       payload, payload_len);
181				client_data->hid_descr_size[curr_hid_dev] =
182					payload_len;
183				client_data->hid_descr_done = true;
184			}
185			wake_up_interruptible(&client_data->init_wait);
186
187			break;
188
189		case HOSTIF_GET_REPORT_DESCRIPTOR:
190			if ((!(recv_msg->hdr.command & ~CMD_MASK) ||
191					client_data->init_done)) {
192				++client_data->bad_recv_cnt;
193				report_bad_packet(hid_ishtp_cl, recv_msg,
194						  cur_pos,
195						  payload_len);
196				ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
197				break;
198			}
199			if (!client_data->report_descr[curr_hid_dev])
200				client_data->report_descr[curr_hid_dev] =
201				devm_kmalloc(cl_data_to_dev(client_data),
202					     payload_len, GFP_KERNEL);
203			if (client_data->report_descr[curr_hid_dev])  {
204				memcpy(client_data->report_descr[curr_hid_dev],
205				       payload,
206				       payload_len);
207				client_data->report_descr_size[curr_hid_dev] =
208					payload_len;
209				client_data->report_descr_done = true;
210			}
211			wake_up_interruptible(&client_data->init_wait);
212
213			break;
214
215		case HOSTIF_GET_FEATURE_REPORT:
216			report_type = HID_FEATURE_REPORT;
217			goto	do_get_report;
218
219		case HOSTIF_GET_INPUT_REPORT:
220			report_type = HID_INPUT_REPORT;
221do_get_report:
222			/* Get index of device that matches this id */
223			for (i = 0; i < client_data->num_hid_devices; ++i) {
224				if (recv_msg->hdr.device_id ==
225					  client_data->hid_devices[i].dev_id) {
226					hid = client_data->hid_sensor_hubs[i];
227					if (!hid)
228						break;
229
230					hid_data = hid->driver_data;
231					if (hid_data->raw_get_req) {
232						raw_len =
233						  (hid_data->raw_buf_size <
234								payload_len) ?
235						  hid_data->raw_buf_size :
236						  payload_len;
237
238						memcpy(hid_data->raw_buf,
239						       payload, raw_len);
240					} else {
241						hid_input_report
242							(hid, report_type,
243							 payload, payload_len,
244							 0);
245					}
246
247					ishtp_hid_wakeup(hid);
248					break;
249				}
250			}
251			break;
252
253		case HOSTIF_SET_FEATURE_REPORT:
254			/* Get index of device that matches this id */
255			for (i = 0; i < client_data->num_hid_devices; ++i) {
256				if (recv_msg->hdr.device_id ==
257					client_data->hid_devices[i].dev_id)
258					if (client_data->hid_sensor_hubs[i]) {
259						ishtp_hid_wakeup(
260						client_data->hid_sensor_hubs[
261							i]);
262						break;
263					}
264			}
265			break;
266
267		case HOSTIF_PUBLISH_INPUT_REPORT:
268			report_type = HID_INPUT_REPORT;
269			for (i = 0; i < client_data->num_hid_devices; ++i)
270				if (recv_msg->hdr.device_id ==
271					client_data->hid_devices[i].dev_id)
272					if (client_data->hid_sensor_hubs[i])
273						hid_input_report(
274						client_data->hid_sensor_hubs[
275									i],
276						report_type, payload,
277						payload_len, 0);
278			break;
279
280		case HOSTIF_PUBLISH_INPUT_REPORT_LIST:
281			report_type = HID_INPUT_REPORT;
282			reports_list = (struct report_list *)payload;
283			report = reports_list->reports;
284
285			for (j = 0; j < reports_list->num_of_reports; j++) {
286				recv_msg = container_of(&report->msg,
287							struct hostif_msg, hdr);
288				report_len = report->size;
289				payload = recv_msg->payload;
290				payload_len = report_len -
291					sizeof(struct hostif_msg_hdr);
292
293				for (i = 0; i < client_data->num_hid_devices;
294				     ++i)
295					if (recv_msg->hdr.device_id ==
296					client_data->hid_devices[i].dev_id &&
297					client_data->hid_sensor_hubs[i]) {
298						hid_input_report(
299						client_data->hid_sensor_hubs[
300									i],
301						report_type,
302						payload, payload_len,
303						0);
304					}
305
306				report += sizeof(*report) + payload_len;
307			}
308			break;
309		default:
310			++client_data->bad_recv_cnt;
311			report_bad_packet(hid_ishtp_cl, recv_msg, cur_pos,
312					  payload_len);
313			ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
314			break;
315
316		}
317
318		msg_len = payload_len + sizeof(struct hostif_msg);
319		if (!cur_pos && cur_pos + msg_len < total_len)
320			++client_data->multi_packet_cnt;
321
322		cur_pos += msg_len;
323		payload += msg_len;
324
325	} while (cur_pos < total_len);
326}
327
328/**
329 * ish_cl_event_cb() - bus driver callback for incoming message/packet
330 * @device:	Pointer to the ishtp client device for which this message
331 *		is targeted
332 *
333 * Remove the packet from the list and process the message by calling
334 * process_recv
335 */
336static void ish_cl_event_cb(struct ishtp_cl_device *device)
337{
338	struct ishtp_cl	*hid_ishtp_cl = ishtp_get_drvdata(device);
339	struct ishtp_cl_rb *rb_in_proc;
340	size_t r_length;
341
342	if (!hid_ishtp_cl)
343		return;
344
345	while ((rb_in_proc = ishtp_cl_rx_get_rb(hid_ishtp_cl)) != NULL) {
346		if (!rb_in_proc->buffer.data)
347			return;
348
349		r_length = rb_in_proc->buf_idx;
350
351		/* decide what to do with received data */
352		process_recv(hid_ishtp_cl, rb_in_proc->buffer.data, r_length);
353
354		ishtp_cl_io_rb_recycle(rb_in_proc);
355	}
356}
357
358/**
359 * hid_ishtp_set_feature() - send request to ISH FW to set a feature request
360 * @hid:	hid device instance for this request
361 * @buf:	feature buffer
362 * @len:	Length of feature buffer
363 * @report_id:	Report id for the feature set request
364 *
365 * This is called from hid core .request() callback. This function doesn't wait
366 * for response.
367 */
368void hid_ishtp_set_feature(struct hid_device *hid, char *buf, unsigned int len,
369			   int report_id)
370{
371	struct ishtp_hid_data *hid_data =  hid->driver_data;
372	struct ishtp_cl_data *client_data = hid_data->client_data;
373	struct hostif_msg *msg = (struct hostif_msg *)buf;
374	int	rv;
375	int	i;
376
377	hid_ishtp_trace(client_data,  "%s hid %p\n", __func__, hid);
378
379	rv = ishtp_hid_link_ready_wait(client_data);
380	if (rv) {
381		hid_ishtp_trace(client_data,  "%s hid %p link not ready\n",
382				__func__, hid);
383		return;
384	}
385
386	memset(msg, 0, sizeof(struct hostif_msg));
387	msg->hdr.command = HOSTIF_SET_FEATURE_REPORT;
388	for (i = 0; i < client_data->num_hid_devices; ++i) {
389		if (hid == client_data->hid_sensor_hubs[i]) {
390			msg->hdr.device_id =
391				client_data->hid_devices[i].dev_id;
392			break;
393		}
394	}
395
396	if (i == client_data->num_hid_devices)
397		return;
398
399	rv = ishtp_cl_send(client_data->hid_ishtp_cl, buf, len);
400	if (rv)
401		hid_ishtp_trace(client_data,  "%s hid %p send failed\n",
402				__func__, hid);
403}
404
405/**
406 * hid_ishtp_get_report() - request to get feature/input report
407 * @hid:	hid device instance for this request
408 * @report_id:	Report id for the get request
409 * @report_type:	Report type for the this request
410 *
411 * This is called from hid core .request() callback. This function will send
412 * request to FW and return without waiting for response.
413 */
414void hid_ishtp_get_report(struct hid_device *hid, int report_id,
415			  int report_type)
416{
417	struct ishtp_hid_data *hid_data =  hid->driver_data;
418	struct ishtp_cl_data *client_data = hid_data->client_data;
419	struct hostif_msg_to_sensor msg = {};
420	int	rv;
421	int	i;
422
423	hid_ishtp_trace(client_data,  "%s hid %p\n", __func__, hid);
424	rv = ishtp_hid_link_ready_wait(client_data);
425	if (rv) {
426		hid_ishtp_trace(client_data,  "%s hid %p link not ready\n",
427				__func__, hid);
428		return;
429	}
430
431	msg.hdr.command = (report_type == HID_FEATURE_REPORT) ?
432		HOSTIF_GET_FEATURE_REPORT : HOSTIF_GET_INPUT_REPORT;
433	for (i = 0; i < client_data->num_hid_devices; ++i) {
434		if (hid == client_data->hid_sensor_hubs[i]) {
435			msg.hdr.device_id =
436				client_data->hid_devices[i].dev_id;
437			break;
438		}
439	}
440
441	if (i == client_data->num_hid_devices)
442		return;
443
444	msg.report_id = report_id;
445	rv = ishtp_cl_send(client_data->hid_ishtp_cl, (uint8_t *)&msg,
446			    sizeof(msg));
447	if (rv)
448		hid_ishtp_trace(client_data,  "%s hid %p send failed\n",
449				__func__, hid);
450}
451
452/**
453 * ishtp_hid_link_ready_wait() - Wait for link ready
454 * @client_data:	client data instance
455 *
456 * If the transport link started suspend process, then wait, till either
457 * resumed or timeout
458 *
459 * Return: 0 on success, non zero on error
460 */
461int ishtp_hid_link_ready_wait(struct ishtp_cl_data *client_data)
462{
463	int rc;
464
465	if (client_data->suspended) {
466		hid_ishtp_trace(client_data,  "wait for link ready\n");
467		rc = wait_event_interruptible_timeout(
468					client_data->ishtp_resume_wait,
469					!client_data->suspended,
470					5 * HZ);
471
472		if (rc == 0) {
473			hid_ishtp_trace(client_data,  "link not ready\n");
474			return -EIO;
475		}
476		hid_ishtp_trace(client_data,  "link ready\n");
477	}
478
479	return 0;
480}
481
482/**
483 * ishtp_enum_enum_devices() - Enumerate hid devices
484 * @hid_ishtp_cl:	client instance
485 *
486 * Helper function to send request to firmware to enumerate HID devices
487 *
488 * Return: 0 on success, non zero on error
489 */
490static int ishtp_enum_enum_devices(struct ishtp_cl *hid_ishtp_cl)
491{
492	struct hostif_msg msg;
493	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
494	int retry_count;
495	int rv;
496
497	/* Send HOSTIF_DM_ENUM_DEVICES */
498	client_data->enum_devices_done = false;
499	memset(&msg, 0, sizeof(struct hostif_msg));
500	msg.hdr.command = HOSTIF_DM_ENUM_DEVICES;
501	rv = ishtp_cl_send(hid_ishtp_cl, (unsigned char *)&msg,
502			   sizeof(struct hostif_msg));
503	if (rv)
504		return rv;
505
506	retry_count = 0;
507	while (!client_data->enum_devices_done &&
508	       retry_count < 10) {
509		wait_event_interruptible_timeout(client_data->init_wait,
510					 client_data->enum_devices_done,
511					 3 * HZ);
512		++retry_count;
513		if (!client_data->enum_devices_done)
514			/* Send HOSTIF_DM_ENUM_DEVICES */
515			rv = ishtp_cl_send(hid_ishtp_cl,
516					   (unsigned char *) &msg,
517					   sizeof(struct hostif_msg));
518	}
519	if (!client_data->enum_devices_done) {
520		dev_err(cl_data_to_dev(client_data),
521			"[hid-ish]: timed out waiting for enum_devices\n");
522		return -ETIMEDOUT;
523	}
524	if (!client_data->hid_devices) {
525		dev_err(cl_data_to_dev(client_data),
526			"[hid-ish]: failed to allocate HID dev structures\n");
527		return -ENOMEM;
528	}
529
530	client_data->num_hid_devices = client_data->hid_dev_count;
531	dev_info(ishtp_device(client_data->cl_device),
532		"[hid-ish]: enum_devices_done OK, num_hid_devices=%d\n",
533		client_data->num_hid_devices);
534
535	return	0;
536}
537
538/**
539 * ishtp_get_hid_descriptor() - Get hid descriptor
540 * @hid_ishtp_cl:	client instance
541 * @index:		Index into the hid_descr array
542 *
543 * Helper function to send request to firmware get HID descriptor of a device
544 *
545 * Return: 0 on success, non zero on error
546 */
547static int ishtp_get_hid_descriptor(struct ishtp_cl *hid_ishtp_cl, int index)
548{
549	struct hostif_msg msg;
550	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
551	int rv;
552
553	/* Get HID descriptor */
554	client_data->hid_descr_done = false;
555	memset(&msg, 0, sizeof(struct hostif_msg));
556	msg.hdr.command = HOSTIF_GET_HID_DESCRIPTOR;
557	msg.hdr.device_id = client_data->hid_devices[index].dev_id;
558	rv = ishtp_cl_send(hid_ishtp_cl, (unsigned char *) &msg,
559			   sizeof(struct hostif_msg));
560	if (rv)
561		return rv;
562
563	if (!client_data->hid_descr_done) {
564		wait_event_interruptible_timeout(client_data->init_wait,
565						 client_data->hid_descr_done,
566						 3 * HZ);
567		if (!client_data->hid_descr_done) {
568			dev_err(cl_data_to_dev(client_data),
569				"[hid-ish]: timed out for hid_descr_done\n");
570			return -EIO;
571		}
572
573		if (!client_data->hid_descr[index]) {
574			dev_err(cl_data_to_dev(client_data),
575				"[hid-ish]: allocation HID desc fail\n");
576			return -ENOMEM;
577		}
578	}
579
580	return 0;
581}
582
583/**
584 * ishtp_get_report_descriptor() - Get report descriptor
585 * @hid_ishtp_cl:	client instance
586 * @index:		Index into the hid_descr array
587 *
588 * Helper function to send request to firmware get HID report descriptor of
589 * a device
590 *
591 * Return: 0 on success, non zero on error
592 */
593static int ishtp_get_report_descriptor(struct ishtp_cl *hid_ishtp_cl,
594				       int index)
595{
596	struct hostif_msg msg;
597	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
598	int rv;
599
600	/* Get report descriptor */
601	client_data->report_descr_done = false;
602	memset(&msg, 0, sizeof(struct hostif_msg));
603	msg.hdr.command = HOSTIF_GET_REPORT_DESCRIPTOR;
604	msg.hdr.device_id = client_data->hid_devices[index].dev_id;
605	rv = ishtp_cl_send(hid_ishtp_cl, (unsigned char *) &msg,
606			   sizeof(struct hostif_msg));
607	if (rv)
608		return rv;
609
610	if (!client_data->report_descr_done)
611		wait_event_interruptible_timeout(client_data->init_wait,
612					 client_data->report_descr_done,
613					 3 * HZ);
614	if (!client_data->report_descr_done) {
615		dev_err(cl_data_to_dev(client_data),
616				"[hid-ish]: timed out for report descr\n");
617		return -EIO;
618	}
619	if (!client_data->report_descr[index]) {
620		dev_err(cl_data_to_dev(client_data),
621			"[hid-ish]: failed to alloc report descr\n");
622		return -ENOMEM;
623	}
624
625	return 0;
626}
627
628/**
629 * hid_ishtp_cl_init() - Init function for ISHTP client
630 * @hid_ishtp_cl:	ISHTP client instance
631 * @reset:		true if called for init after reset
632 *
633 * This function complete the initializtion of the client. The summary of
634 * processing:
635 * - Send request to enumerate the hid clients
636 *	Get the HID descriptor for each enumearated device
637 *	Get report description of each device
638 *	Register each device wik hid core by calling ishtp_hid_probe
639 *
640 * Return: 0 on success, non zero on error
641 */
642static int hid_ishtp_cl_init(struct ishtp_cl *hid_ishtp_cl, bool reset)
643{
644	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
645	int i;
646	int rv;
647
648	dev_dbg(cl_data_to_dev(client_data), "%s\n", __func__);
649	hid_ishtp_trace(client_data,  "%s reset flag: %d\n", __func__, reset);
650
651	client_data->init_done = 0;
652
653	rv = ishtp_cl_establish_connection(hid_ishtp_cl,
654					   &hid_ishtp_id_table[0].guid,
655					   HID_CL_TX_RING_SIZE,
656					   HID_CL_RX_RING_SIZE,
657					   reset);
658	if (rv) {
659		dev_err(cl_data_to_dev(client_data),
660			"client connect fail\n");
661		goto err_cl_disconnect;
662	}
663
664	hid_ishtp_trace(client_data,  "%s client connected\n", __func__);
665
666	/* Register read callback */
667	ishtp_register_event_cb(client_data->cl_device, ish_cl_event_cb);
668
669	rv = ishtp_enum_enum_devices(hid_ishtp_cl);
670	if (rv)
671		goto err_cl_disconnect;
672
673	hid_ishtp_trace(client_data,  "%s enumerated device count %d\n",
674			__func__, client_data->num_hid_devices);
675
676	for (i = 0; i < client_data->num_hid_devices; ++i) {
677		client_data->cur_hid_dev = i;
678
679		rv = ishtp_get_hid_descriptor(hid_ishtp_cl, i);
680		if (rv)
681			goto err_cl_disconnect;
682
683		rv = ishtp_get_report_descriptor(hid_ishtp_cl, i);
684		if (rv)
685			goto err_cl_disconnect;
686
687		if (!reset) {
688			rv = ishtp_hid_probe(i, client_data);
689			if (rv) {
690				dev_err(cl_data_to_dev(client_data),
691				"[hid-ish]: HID probe for #%u failed: %d\n",
692				i, rv);
693				goto err_cl_disconnect;
694			}
695		}
696	} /* for() on all hid devices */
697
698	client_data->init_done = 1;
699	client_data->suspended = false;
700	wake_up_interruptible(&client_data->ishtp_resume_wait);
701	hid_ishtp_trace(client_data,  "%s successful init\n", __func__);
702	return 0;
703
704err_cl_disconnect:
705	ishtp_cl_destroy_connection(hid_ishtp_cl, reset);
706	return rv;
707}
708
709/**
710 * hid_ishtp_cl_deinit() - Deinit function for ISHTP client
711 * @hid_ishtp_cl:	ISHTP client instance
712 *
713 * Unlink and free hid client
714 */
715static void hid_ishtp_cl_deinit(struct ishtp_cl *hid_ishtp_cl)
716{
717	ishtp_cl_destroy_connection(hid_ishtp_cl, false);
718
719	/* disband and free all Tx and Rx client-level rings */
720	ishtp_cl_free(hid_ishtp_cl);
721}
722
723static void hid_ishtp_cl_reset_handler(struct work_struct *work)
724{
725	struct ishtp_cl_data *client_data;
726	struct ishtp_cl *hid_ishtp_cl;
727	int retry;
728	int rv;
729
730	client_data = container_of(work, struct ishtp_cl_data, work);
731
732	hid_ishtp_cl = client_data->hid_ishtp_cl;
733
734	hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
735			hid_ishtp_cl);
736	dev_dbg(ishtp_device(client_data->cl_device), "%s\n", __func__);
737
738	ishtp_cl_destroy_connection(hid_ishtp_cl, true);
739
740	client_data->num_hid_devices = 0;
741
742	for (retry = 0; retry < 3; ++retry) {
743		rv = hid_ishtp_cl_init(hid_ishtp_cl, true);
744		if (!rv)
745			break;
746		dev_err(cl_data_to_dev(client_data), "Retry reset init\n");
747	}
748	if (rv) {
749		dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
750		hid_ishtp_trace(client_data, "%s Failed hid_ishtp_cl %p\n",
751				__func__, hid_ishtp_cl);
752	}
753}
754
755static void hid_ishtp_cl_resume_handler(struct work_struct *work)
756{
757	struct ishtp_cl_data *client_data = container_of(work, struct ishtp_cl_data, resume_work);
758	struct ishtp_cl *hid_ishtp_cl = client_data->hid_ishtp_cl;
759
760	if (ishtp_wait_resume(ishtp_get_ishtp_device(hid_ishtp_cl))) {
761		/*
762		 * Clear the suspended flag only when the connection is established.
763		 * If the connection is not established, the suspended flag will be cleared after
764		 * the connection is made.
765		 */
766		if (ishtp_get_connection_state(hid_ishtp_cl) == ISHTP_CL_CONNECTED) {
767			client_data->suspended = false;
768			wake_up_interruptible(&client_data->ishtp_resume_wait);
769		}
770	} else {
771		hid_ishtp_trace(client_data, "hid client: wait for resume timed out");
772		dev_err(cl_data_to_dev(client_data), "wait for resume timed out");
773	}
774}
775
776ishtp_print_log ishtp_hid_print_trace;
777
778/**
779 * hid_ishtp_cl_probe() - ISHTP client driver probe
780 * @cl_device:		ISHTP client device instance
781 *
782 * This function gets called on device create on ISHTP bus
783 *
784 * Return: 0 on success, non zero on error
785 */
786static int hid_ishtp_cl_probe(struct ishtp_cl_device *cl_device)
787{
788	struct ishtp_cl *hid_ishtp_cl;
789	struct ishtp_cl_data *client_data;
790	int rv;
791
792	if (!cl_device)
793		return	-ENODEV;
794
795	client_data = devm_kzalloc(ishtp_device(cl_device),
796				   sizeof(*client_data),
797				   GFP_KERNEL);
798	if (!client_data)
799		return -ENOMEM;
800
801	hid_ishtp_cl = ishtp_cl_allocate(cl_device);
802	if (!hid_ishtp_cl)
803		return -ENOMEM;
804
805	ishtp_set_drvdata(cl_device, hid_ishtp_cl);
806	ishtp_set_client_data(hid_ishtp_cl, client_data);
807	client_data->hid_ishtp_cl = hid_ishtp_cl;
808	client_data->cl_device = cl_device;
809
810	init_waitqueue_head(&client_data->init_wait);
811	init_waitqueue_head(&client_data->ishtp_resume_wait);
812
813	INIT_WORK(&client_data->work, hid_ishtp_cl_reset_handler);
814	INIT_WORK(&client_data->resume_work, hid_ishtp_cl_resume_handler);
815
816
817	ishtp_hid_print_trace = ishtp_trace_callback(cl_device);
818
819	rv = hid_ishtp_cl_init(hid_ishtp_cl, false);
820	if (rv) {
821		ishtp_cl_free(hid_ishtp_cl);
822		return rv;
823	}
824	ishtp_get_device(cl_device);
825
826	return 0;
827}
828
829/**
830 * hid_ishtp_cl_remove() - ISHTP client driver remove
831 * @cl_device:		ISHTP client device instance
832 *
833 * This function gets called on device remove on ISHTP bus
834 *
835 * Return: 0
836 */
837static void hid_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
838{
839	struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
840	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
841
842	hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
843			hid_ishtp_cl);
844
845	dev_dbg(ishtp_device(cl_device), "%s\n", __func__);
846	ishtp_put_device(cl_device);
847	ishtp_hid_remove(client_data);
848	hid_ishtp_cl_deinit(hid_ishtp_cl);
849
850	hid_ishtp_cl = NULL;
851
852	client_data->num_hid_devices = 0;
853}
854
855/**
856 * hid_ishtp_cl_reset() - ISHTP client driver reset
857 * @cl_device:		ISHTP client device instance
858 *
859 * This function gets called on device reset on ISHTP bus
860 *
861 * Return: 0
862 */
863static int hid_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
864{
865	struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
866	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
867
868	hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
869			hid_ishtp_cl);
870
871	queue_work(ishtp_get_workqueue(cl_device), &client_data->work);
872
873	return 0;
874}
875
876/**
877 * hid_ishtp_cl_suspend() - ISHTP client driver suspend
878 * @device:	device instance
879 *
880 * This function gets called on system suspend
881 *
882 * Return: 0
883 */
884static int hid_ishtp_cl_suspend(struct device *device)
885{
886	struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
887	struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
888	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
889
890	hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
891			hid_ishtp_cl);
892	client_data->suspended = true;
893
894	return 0;
895}
896
897/**
898 * hid_ishtp_cl_resume() - ISHTP client driver resume
899 * @device:	device instance
900 *
901 * This function gets called on system resume
902 *
903 * Return: 0
904 */
905static int hid_ishtp_cl_resume(struct device *device)
906{
907	struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
908	struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
909	struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
910
911	hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
912			hid_ishtp_cl);
913	queue_work(ishtp_get_workqueue(cl_device), &client_data->resume_work);
914	return 0;
915}
916
917static const struct dev_pm_ops hid_ishtp_pm_ops = {
918	.suspend = hid_ishtp_cl_suspend,
919	.resume = hid_ishtp_cl_resume,
920};
921
922static struct ishtp_cl_driver	hid_ishtp_cl_driver = {
923	.name = "ish-hid",
924	.id = hid_ishtp_id_table,
925	.probe = hid_ishtp_cl_probe,
926	.remove = hid_ishtp_cl_remove,
927	.reset = hid_ishtp_cl_reset,
928	.driver.pm = &hid_ishtp_pm_ops,
929};
930
931static int __init ish_hid_init(void)
932{
933	int	rv;
934
935	/* Register ISHTP client device driver with ISHTP Bus */
936	rv = ishtp_cl_driver_register(&hid_ishtp_cl_driver, THIS_MODULE);
937
938	return rv;
939
940}
941
942static void __exit ish_hid_exit(void)
943{
944	ishtp_cl_driver_unregister(&hid_ishtp_cl_driver);
945}
946
947late_initcall(ish_hid_init);
948module_exit(ish_hid_exit);
949
950MODULE_DESCRIPTION("ISH ISHTP HID client driver");
951/* Primary author */
952MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
953/*
954 * Several modification for multi instance support
955 * suspend/resume and clean up
956 */
957MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
958
959MODULE_LICENSE("GPL");
~/ Read-only file preview.