xref: /linux/drivers/hid/intel-ish-hid/ishtp/bus.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ISHTP bus driver
4  *
5  * Copyright (c) 2012-2016, Intel Corporation.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include "bus.h"
15 #include "ishtp-dev.h"
16 #include "client.h"
17 #include "hbm.h"
18 
19 static int ishtp_use_dma;
20 module_param_named(ishtp_use_dma, ishtp_use_dma, int, 0600);
21 MODULE_PARM_DESC(ishtp_use_dma, "Use DMA to send messages");
22 
23 #define to_ishtp_cl_driver(d) container_of(d, struct ishtp_cl_driver, driver)
24 #define to_ishtp_cl_device(d) container_of(d, struct ishtp_cl_device, dev)
25 static bool ishtp_device_ready;
26 
27 /**
28  * ishtp_recv() - process ishtp message
29  * @dev: ishtp device
30  *
31  * If a message with valid header and size is received, then
32  * this function calls appropriate handler. The host or firmware
33  * address is zero, then they are host bus management message,
34  * otherwise they are message fo clients.
35  */
36 void ishtp_recv(struct ishtp_device *dev)
37 {
38 	uint32_t	msg_hdr;
39 	struct ishtp_msg_hdr	*ishtp_hdr;
40 
41 	/* Read ISHTP header dword */
42 	msg_hdr = dev->ops->ishtp_read_hdr(dev);
43 	if (!msg_hdr)
44 		return;
45 
46 	dev->ops->sync_fw_clock(dev);
47 
48 	ishtp_hdr = (struct ishtp_msg_hdr *)&msg_hdr;
49 	dev->ishtp_msg_hdr = msg_hdr;
50 
51 	/* Sanity check: ISHTP frag. length in header */
52 	if (ishtp_hdr->length > dev->mtu) {
53 		dev_err(dev->devc,
54 			"ISHTP hdr - bad length: %u; dropped [%08X]\n",
55 			(unsigned int)ishtp_hdr->length, msg_hdr);
56 		return;
57 	}
58 
59 	/* ISHTP bus message */
60 	if (!ishtp_hdr->host_addr && !ishtp_hdr->fw_addr)
61 		recv_hbm(dev, ishtp_hdr);
62 	/* ISHTP fixed-client message */
63 	else if (!ishtp_hdr->host_addr)
64 		recv_fixed_cl_msg(dev, ishtp_hdr);
65 	else
66 		/* ISHTP client message */
67 		recv_ishtp_cl_msg(dev, ishtp_hdr);
68 }
69 EXPORT_SYMBOL(ishtp_recv);
70 
71 /**
72  * ishtp_send_msg() - Send ishtp message
73  * @dev: ishtp device
74  * @hdr: Message header
75  * @msg: Message contents
76  * @ipc_send_compl: completion callback
77  * @ipc_send_compl_prm: completion callback parameter
78  *
79  * Send a multi fragment message via IPC. After sending the first fragment
80  * the completion callback is called to schedule transmit of next fragment.
81  *
82  * Return: This returns IPC send message status.
83  */
84 int ishtp_send_msg(struct ishtp_device *dev, struct ishtp_msg_hdr *hdr,
85 		       void *msg, void(*ipc_send_compl)(void *),
86 		       void *ipc_send_compl_prm)
87 {
88 	unsigned char	ipc_msg[IPC_FULL_MSG_SIZE];
89 	uint32_t	drbl_val;
90 
91 	drbl_val = dev->ops->ipc_get_header(dev, hdr->length +
92 					    sizeof(struct ishtp_msg_hdr),
93 					    1);
94 
95 	memcpy(ipc_msg, &drbl_val, sizeof(uint32_t));
96 	memcpy(ipc_msg + sizeof(uint32_t), hdr, sizeof(uint32_t));
97 	memcpy(ipc_msg + 2 * sizeof(uint32_t), msg, hdr->length);
98 	return	dev->ops->write(dev, ipc_send_compl, ipc_send_compl_prm,
99 				ipc_msg, 2 * sizeof(uint32_t) + hdr->length);
100 }
101 
102 /**
103  * ishtp_write_message() - Send ishtp single fragment message
104  * @dev: ishtp device
105  * @hdr: Message header
106  * @buf: message data
107  *
108  * Send a single fragment message via IPC.  This returns IPC send message
109  * status.
110  *
111  * Return: This returns IPC send message status.
112  */
113 int ishtp_write_message(struct ishtp_device *dev, struct ishtp_msg_hdr *hdr,
114 			void *buf)
115 {
116 	return ishtp_send_msg(dev, hdr, buf, NULL, NULL);
117 }
118 
119 /**
120  * ishtp_fw_cl_by_uuid() - locate index of fw client
121  * @dev: ishtp device
122  * @uuid: uuid of the client to search
123  *
124  * Search firmware client using UUID.
125  *
126  * Return: fw client index or -ENOENT if not found
127  */
128 int ishtp_fw_cl_by_uuid(struct ishtp_device *dev, const guid_t *uuid)
129 {
130 	unsigned int i;
131 
132 	for (i = 0; i < dev->fw_clients_num; ++i) {
133 		if (guid_equal(uuid, &dev->fw_clients[i].props.protocol_name))
134 			return i;
135 	}
136 	return -ENOENT;
137 }
138 EXPORT_SYMBOL(ishtp_fw_cl_by_uuid);
139 
140 /**
141  * ishtp_fw_cl_get_client() - return client information to client
142  * @dev: the ishtp device structure
143  * @uuid: uuid of the client to search
144  *
145  * Search firmware client using UUID and reture related client information.
146  *
147  * Return: pointer of client information on success, NULL on failure.
148  */
149 struct ishtp_fw_client *ishtp_fw_cl_get_client(struct ishtp_device *dev,
150 					       const guid_t *uuid)
151 {
152 	int i;
153 	unsigned long flags;
154 
155 	spin_lock_irqsave(&dev->fw_clients_lock, flags);
156 	i = ishtp_fw_cl_by_uuid(dev, uuid);
157 	spin_unlock_irqrestore(&dev->fw_clients_lock, flags);
158 	if (i < 0 || dev->fw_clients[i].props.fixed_address)
159 		return NULL;
160 
161 	return &dev->fw_clients[i];
162 }
163 EXPORT_SYMBOL(ishtp_fw_cl_get_client);
164 
165 /**
166  * ishtp_get_fw_client_id() - Get fw client id
167  * @fw_client:	firmware client used to fetch the ID
168  *
169  * This interface is used to reset HW get FW client id.
170  *
171  * Return: firmware client id.
172  */
173 int ishtp_get_fw_client_id(struct ishtp_fw_client *fw_client)
174 {
175 	return fw_client->client_id;
176 }
177 EXPORT_SYMBOL(ishtp_get_fw_client_id);
178 
179 /**
180  * ishtp_fw_cl_by_id() - return index to fw_clients for client_id
181  * @dev: the ishtp device structure
182  * @client_id: fw client id to search
183  *
184  * Search firmware client using client id.
185  *
186  * Return: index on success, -ENOENT on failure.
187  */
188 int ishtp_fw_cl_by_id(struct ishtp_device *dev, uint8_t client_id)
189 {
190 	int i, res = -ENOENT;
191 	unsigned long	flags;
192 
193 	spin_lock_irqsave(&dev->fw_clients_lock, flags);
194 	for (i = 0; i < dev->fw_clients_num; i++) {
195 		if (dev->fw_clients[i].client_id == client_id) {
196 			res = i;
197 			break;
198 		}
199 	}
200 	spin_unlock_irqrestore(&dev->fw_clients_lock, flags);
201 
202 	return res;
203 }
204 
205 /**
206  * ishtp_cl_device_probe() - Bus probe() callback
207  * @dev: the device structure
208  *
209  * This is a bus probe callback and calls the drive probe function.
210  *
211  * Return: Return value from driver probe() call.
212  */
213 static int ishtp_cl_device_probe(struct device *dev)
214 {
215 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
216 	struct ishtp_cl_driver *driver;
217 
218 	if (!device)
219 		return 0;
220 
221 	driver = to_ishtp_cl_driver(dev->driver);
222 	if (!driver || !driver->probe)
223 		return -ENODEV;
224 
225 	return driver->probe(device);
226 }
227 
228 /**
229  * ishtp_cl_bus_match() - Bus match() callback
230  * @dev: the device structure
231  * @drv: the driver structure
232  *
233  * This is a bus match callback, called when a new ishtp_cl_device is
234  * registered during ishtp bus client enumeration. Use the guid_t in
235  * drv and dev to decide whether they match or not.
236  *
237  * Return: 1 if dev & drv matches, 0 otherwise.
238  */
239 static int ishtp_cl_bus_match(struct device *dev, const struct device_driver *drv)
240 {
241 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
242 	struct ishtp_cl_driver *driver = to_ishtp_cl_driver(drv);
243 
244 	return(device->fw_client ? guid_equal(&driver->id[0].guid,
245 	       &device->fw_client->props.protocol_name) : 0);
246 }
247 
248 /**
249  * ishtp_cl_device_remove() - Bus remove() callback
250  * @dev: the device structure
251  *
252  * This is a bus remove callback and calls the drive remove function.
253  * Since the ISH driver model supports only built in, this is
254  * primarily can be called during pci driver init failure.
255  *
256  * Return: Return value from driver remove() call.
257  */
258 static void ishtp_cl_device_remove(struct device *dev)
259 {
260 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
261 	struct ishtp_cl_driver *driver = to_ishtp_cl_driver(dev->driver);
262 
263 	if (device->event_cb) {
264 		device->event_cb = NULL;
265 		cancel_work_sync(&device->event_work);
266 	}
267 
268 	if (driver->remove)
269 		driver->remove(device);
270 }
271 
272 /**
273  * ishtp_cl_device_suspend() - Bus suspend callback
274  * @dev:	device
275  *
276  * Called during device suspend process.
277  *
278  * Return: Return value from driver suspend() call.
279  */
280 static int ishtp_cl_device_suspend(struct device *dev)
281 {
282 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
283 	struct ishtp_cl_driver *driver;
284 	int ret = 0;
285 
286 	if (!device)
287 		return 0;
288 
289 	driver = to_ishtp_cl_driver(dev->driver);
290 	if (driver && driver->driver.pm) {
291 		if (driver->driver.pm->suspend)
292 			ret = driver->driver.pm->suspend(dev);
293 	}
294 
295 	return ret;
296 }
297 
298 /**
299  * ishtp_cl_device_resume() - Bus resume callback
300  * @dev:	device
301  *
302  * Called during device resume process.
303  *
304  * Return: Return value from driver resume() call.
305  */
306 static int ishtp_cl_device_resume(struct device *dev)
307 {
308 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
309 	struct ishtp_cl_driver *driver;
310 	int ret = 0;
311 
312 	if (!device)
313 		return 0;
314 
315 	driver = to_ishtp_cl_driver(dev->driver);
316 	if (driver && driver->driver.pm) {
317 		if (driver->driver.pm->resume)
318 			ret = driver->driver.pm->resume(dev);
319 	}
320 
321 	return ret;
322 }
323 
324 /**
325  * ishtp_cl_device_reset() - Reset callback
326  * @device:	ishtp client device instance
327  *
328  * This is a callback when HW reset is done and the device need
329  * reinit.
330  *
331  * Return: Return value from driver reset() call.
332  */
333 static int ishtp_cl_device_reset(struct ishtp_cl_device *device)
334 {
335 	struct ishtp_cl_driver *driver;
336 	int ret = 0;
337 
338 	device->event_cb = NULL;
339 	cancel_work_sync(&device->event_work);
340 
341 	driver = to_ishtp_cl_driver(device->dev.driver);
342 	if (driver && driver->reset)
343 		ret = driver->reset(device);
344 
345 	return ret;
346 }
347 
348 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
349 	char *buf)
350 {
351 	int len;
352 
353 	len = snprintf(buf, PAGE_SIZE, ISHTP_MODULE_PREFIX "%s\n", dev_name(dev));
354 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
355 }
356 static DEVICE_ATTR_RO(modalias);
357 
358 static struct attribute *ishtp_cl_dev_attrs[] = {
359 	&dev_attr_modalias.attr,
360 	NULL,
361 };
362 ATTRIBUTE_GROUPS(ishtp_cl_dev);
363 
364 static int ishtp_cl_uevent(const struct device *dev, struct kobj_uevent_env *env)
365 {
366 	if (add_uevent_var(env, "MODALIAS=" ISHTP_MODULE_PREFIX "%s", dev_name(dev)))
367 		return -ENOMEM;
368 	return 0;
369 }
370 
371 static const struct dev_pm_ops ishtp_cl_bus_dev_pm_ops = {
372 	/* Suspend callbacks */
373 	.suspend = ishtp_cl_device_suspend,
374 	.resume = ishtp_cl_device_resume,
375 	/* Hibernate callbacks */
376 	.freeze = ishtp_cl_device_suspend,
377 	.thaw = ishtp_cl_device_resume,
378 	.restore = ishtp_cl_device_resume,
379 };
380 
381 static const struct bus_type ishtp_cl_bus_type = {
382 	.name		= "ishtp",
383 	.dev_groups	= ishtp_cl_dev_groups,
384 	.probe		= ishtp_cl_device_probe,
385 	.match		= ishtp_cl_bus_match,
386 	.remove		= ishtp_cl_device_remove,
387 	.pm		= &ishtp_cl_bus_dev_pm_ops,
388 	.uevent		= ishtp_cl_uevent,
389 };
390 
391 static void ishtp_cl_dev_release(struct device *dev)
392 {
393 	kfree(to_ishtp_cl_device(dev));
394 }
395 
396 static const struct device_type ishtp_cl_device_type = {
397 	.release	= ishtp_cl_dev_release,
398 };
399 
400 /**
401  * ishtp_bus_add_device() - Function to create device on bus
402  * @dev:	ishtp device
403  * @uuid:	uuid of the client
404  * @name:	Name of the client
405  *
406  * Allocate ISHTP bus client device, attach it to uuid
407  * and register with ISHTP bus.
408  *
409  * Return: ishtp_cl_device pointer or NULL on failure
410  */
411 static struct ishtp_cl_device *ishtp_bus_add_device(struct ishtp_device *dev,
412 						    guid_t uuid, char *name)
413 {
414 	struct ishtp_cl_device *device;
415 	int status;
416 	unsigned long flags;
417 
418 	spin_lock_irqsave(&dev->device_list_lock, flags);
419 	list_for_each_entry(device, &dev->device_list, device_link) {
420 		if (!strcmp(name, dev_name(&device->dev))) {
421 			device->fw_client = &dev->fw_clients[
422 				dev->fw_client_presentation_num - 1];
423 			spin_unlock_irqrestore(&dev->device_list_lock, flags);
424 			ishtp_cl_device_reset(device);
425 			return device;
426 		}
427 	}
428 	spin_unlock_irqrestore(&dev->device_list_lock, flags);
429 
430 	device = kzalloc(sizeof(struct ishtp_cl_device), GFP_KERNEL);
431 	if (!device)
432 		return NULL;
433 
434 	device->dev.parent = dev->devc;
435 	device->dev.bus = &ishtp_cl_bus_type;
436 	device->dev.type = &ishtp_cl_device_type;
437 	device->ishtp_dev = dev;
438 
439 	device->fw_client =
440 		&dev->fw_clients[dev->fw_client_presentation_num - 1];
441 
442 	dev_set_name(&device->dev, "%s", name);
443 
444 	spin_lock_irqsave(&dev->device_list_lock, flags);
445 	list_add_tail(&device->device_link, &dev->device_list);
446 	spin_unlock_irqrestore(&dev->device_list_lock, flags);
447 
448 	status = device_register(&device->dev);
449 	if (status) {
450 		spin_lock_irqsave(&dev->device_list_lock, flags);
451 		list_del(&device->device_link);
452 		spin_unlock_irqrestore(&dev->device_list_lock, flags);
453 		dev_err(dev->devc, "Failed to register ISHTP client device\n");
454 		put_device(&device->dev);
455 		return NULL;
456 	}
457 
458 	ishtp_device_ready = true;
459 
460 	return device;
461 }
462 
463 /**
464  * ishtp_bus_remove_device() - Function to relase device on bus
465  * @device:	client device instance
466  *
467  * This is a counterpart of ishtp_bus_add_device.
468  * Device is unregistered.
469  * the device structure is freed in 'ishtp_cl_dev_release' function
470  * Called only during error in pci driver init path.
471  */
472 static void ishtp_bus_remove_device(struct ishtp_cl_device *device)
473 {
474 	device_unregister(&device->dev);
475 }
476 
477 /**
478  * ishtp_cl_driver_register() - Client driver register
479  * @driver:	the client driver instance
480  * @owner:	Owner of this driver module
481  *
482  * Once a client driver is probed, it created a client
483  * instance and registers with the bus.
484  *
485  * Return: Return value of driver_register or -ENODEV if not ready
486  */
487 int ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
488 			     struct module *owner)
489 {
490 	if (!ishtp_device_ready)
491 		return -ENODEV;
492 
493 	driver->driver.name = driver->name;
494 	driver->driver.owner = owner;
495 	driver->driver.bus = &ishtp_cl_bus_type;
496 
497 	return driver_register(&driver->driver);
498 }
499 EXPORT_SYMBOL(ishtp_cl_driver_register);
500 
501 /**
502  * ishtp_cl_driver_unregister() - Client driver unregister
503  * @driver:	the client driver instance
504  *
505  * Unregister client during device removal process.
506  */
507 void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver)
508 {
509 	driver_unregister(&driver->driver);
510 }
511 EXPORT_SYMBOL(ishtp_cl_driver_unregister);
512 
513 /**
514  * ishtp_bus_event_work() - event work function
515  * @work:	work struct pointer
516  *
517  * Once an event is received for a client this work
518  * function is called. If the device has registered a
519  * callback then the callback is called.
520  */
521 static void ishtp_bus_event_work(struct work_struct *work)
522 {
523 	struct ishtp_cl_device *device;
524 
525 	device = container_of(work, struct ishtp_cl_device, event_work);
526 
527 	if (device->event_cb)
528 		device->event_cb(device);
529 }
530 
531 /**
532  * ishtp_cl_bus_rx_event() - schedule event work
533  * @device:	client device instance
534  *
535  * Once an event is received for a client this schedules
536  * a work function to process.
537  */
538 void ishtp_cl_bus_rx_event(struct ishtp_cl_device *device)
539 {
540 	if (!device || !device->event_cb)
541 		return;
542 
543 	if (device->event_cb)
544 		schedule_work(&device->event_work);
545 }
546 
547 /**
548  * ishtp_register_event_cb() - Register callback
549  * @device:	client device instance
550  * @event_cb:	Event processor for an client
551  *
552  * Register a callback for events, called from client driver
553  *
554  * Return: Return 0 or -EALREADY if already registered
555  */
556 int ishtp_register_event_cb(struct ishtp_cl_device *device,
557 	void (*event_cb)(struct ishtp_cl_device *))
558 {
559 	if (device->event_cb)
560 		return -EALREADY;
561 
562 	device->event_cb = event_cb;
563 	INIT_WORK(&device->event_work, ishtp_bus_event_work);
564 
565 	return 0;
566 }
567 EXPORT_SYMBOL(ishtp_register_event_cb);
568 
569 /**
570  * ishtp_get_device() - update usage count for the device
571  * @cl_device:	client device instance
572  *
573  * Increment the usage count. The device can't be deleted
574  */
575 void ishtp_get_device(struct ishtp_cl_device *cl_device)
576 {
577 	cl_device->reference_count++;
578 }
579 EXPORT_SYMBOL(ishtp_get_device);
580 
581 /**
582  * ishtp_put_device() - decrement usage count for the device
583  * @cl_device:	client device instance
584  *
585  * Decrement the usage count. The device can be deleted is count = 0
586  */
587 void ishtp_put_device(struct ishtp_cl_device *cl_device)
588 {
589 	cl_device->reference_count--;
590 }
591 EXPORT_SYMBOL(ishtp_put_device);
592 
593 /**
594  * ishtp_set_drvdata() - set client driver data
595  * @cl_device:	client device instance
596  * @data:	driver data need to be set
597  *
598  * Set client driver data to cl_device->driver_data.
599  */
600 void ishtp_set_drvdata(struct ishtp_cl_device *cl_device, void *data)
601 {
602 	cl_device->driver_data = data;
603 }
604 EXPORT_SYMBOL(ishtp_set_drvdata);
605 
606 /**
607  * ishtp_get_drvdata() - get client driver data
608  * @cl_device:	client device instance
609  *
610  * Get client driver data from cl_device->driver_data.
611  *
612  * Return: pointer of driver data
613  */
614 void *ishtp_get_drvdata(struct ishtp_cl_device *cl_device)
615 {
616 	return cl_device->driver_data;
617 }
618 EXPORT_SYMBOL(ishtp_get_drvdata);
619 
620 /**
621  * ishtp_dev_to_cl_device() - get ishtp_cl_device instance from device instance
622  * @device: device instance
623  *
624  * Get ish_cl_device instance which embeds device instance in it.
625  *
626  * Return: pointer to ishtp_cl_device instance
627  */
628 struct ishtp_cl_device *ishtp_dev_to_cl_device(struct device *device)
629 {
630 	return to_ishtp_cl_device(device);
631 }
632 EXPORT_SYMBOL(ishtp_dev_to_cl_device);
633 
634 /**
635  * ishtp_bus_new_client() - Create a new client
636  * @dev:	ISHTP device instance
637  *
638  * Once bus protocol enumerates a client, this is called
639  * to add a device for the client.
640  *
641  * Return: 0 on success or error code on failure
642  */
643 int ishtp_bus_new_client(struct ishtp_device *dev)
644 {
645 	int	i;
646 	char	*dev_name;
647 	struct ishtp_cl_device	*cl_device;
648 	guid_t	device_uuid;
649 
650 	/*
651 	 * For all reported clients, create an unconnected client and add its
652 	 * device to ISHTP bus.
653 	 * If appropriate driver has loaded, this will trigger its probe().
654 	 * Otherwise, probe() will be called when driver is loaded
655 	 */
656 	i = dev->fw_client_presentation_num - 1;
657 	device_uuid = dev->fw_clients[i].props.protocol_name;
658 	dev_name = kasprintf(GFP_KERNEL, "{%pUL}", &device_uuid);
659 	if (!dev_name)
660 		return	-ENOMEM;
661 
662 	cl_device = ishtp_bus_add_device(dev, device_uuid, dev_name);
663 	if (!cl_device) {
664 		kfree(dev_name);
665 		return	-ENOENT;
666 	}
667 
668 	kfree(dev_name);
669 
670 	return	0;
671 }
672 
673 /**
674  * ishtp_cl_device_bind() - bind a device
675  * @cl:		ishtp client device
676  *
677  * Binds connected ishtp_cl to ISHTP bus device
678  *
679  * Return: 0 on success or fault code
680  */
681 int ishtp_cl_device_bind(struct ishtp_cl *cl)
682 {
683 	struct ishtp_cl_device	*cl_device;
684 	unsigned long flags;
685 	int	rv;
686 
687 	if (!cl->fw_client_id || cl->state != ISHTP_CL_CONNECTED)
688 		return	-EFAULT;
689 
690 	rv = -ENOENT;
691 	spin_lock_irqsave(&cl->dev->device_list_lock, flags);
692 	list_for_each_entry(cl_device, &cl->dev->device_list,
693 			device_link) {
694 		if (cl_device->fw_client &&
695 		    cl_device->fw_client->client_id == cl->fw_client_id) {
696 			cl->device = cl_device;
697 			rv = 0;
698 			break;
699 		}
700 	}
701 	spin_unlock_irqrestore(&cl->dev->device_list_lock, flags);
702 	return	rv;
703 }
704 
705 /**
706  * ishtp_bus_remove_all_clients() - Remove all clients
707  * @ishtp_dev:		ishtp device
708  * @warm_reset:		Reset due to FW reset dure to errors or S3 suspend
709  *
710  * This is part of reset/remove flow. This function the main processing
711  * only targets error processing, if the FW has forced reset or
712  * error to remove connected clients. When warm reset the client devices are
713  * not removed.
714  */
715 void ishtp_bus_remove_all_clients(struct ishtp_device *ishtp_dev,
716 				  bool warm_reset)
717 {
718 	struct ishtp_cl_device	*cl_device, *n;
719 	struct ishtp_cl	*cl;
720 	unsigned long	flags;
721 
722 	spin_lock_irqsave(&ishtp_dev->cl_list_lock, flags);
723 	list_for_each_entry(cl, &ishtp_dev->cl_list, link) {
724 		cl->state = ISHTP_CL_DISCONNECTED;
725 		if (warm_reset && cl->device->reference_count)
726 			continue;
727 
728 		/*
729 		 * Wake any pending process. The waiter would check dev->state
730 		 * and determine that it's not enabled already,
731 		 * and will return error to its caller
732 		 */
733 		wake_up_interruptible(&cl->wait_ctrl_res);
734 
735 		/* Disband any pending read/write requests and free rb */
736 		ishtp_cl_flush_queues(cl);
737 
738 		/* Remove all free and in_process rings, both Rx and Tx */
739 		ishtp_cl_free_rx_ring(cl);
740 		ishtp_cl_free_tx_ring(cl);
741 
742 		/*
743 		 * Free client and ISHTP bus client device structures
744 		 * don't free host client because it is part of the OS fd
745 		 * structure
746 		 */
747 	}
748 	spin_unlock_irqrestore(&ishtp_dev->cl_list_lock, flags);
749 
750 	/* Release DMA buffers for client messages */
751 	ishtp_cl_free_dma_buf(ishtp_dev);
752 
753 	/* remove bus clients */
754 	spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
755 	list_for_each_entry_safe(cl_device, n, &ishtp_dev->device_list,
756 				 device_link) {
757 		cl_device->fw_client = NULL;
758 		if (warm_reset && cl_device->reference_count)
759 			continue;
760 
761 		list_del(&cl_device->device_link);
762 		spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
763 		ishtp_bus_remove_device(cl_device);
764 		spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
765 	}
766 	spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
767 
768 	/* Free all client structures */
769 	spin_lock_irqsave(&ishtp_dev->fw_clients_lock, flags);
770 	kfree(ishtp_dev->fw_clients);
771 	ishtp_dev->fw_clients = NULL;
772 	ishtp_dev->fw_clients_num = 0;
773 	ishtp_dev->fw_client_presentation_num = 0;
774 	ishtp_dev->fw_client_index = 0;
775 	bitmap_zero(ishtp_dev->fw_clients_map, ISHTP_CLIENTS_MAX);
776 	spin_unlock_irqrestore(&ishtp_dev->fw_clients_lock, flags);
777 }
778 EXPORT_SYMBOL(ishtp_bus_remove_all_clients);
779 
780 /**
781  * ishtp_reset_handler() - IPC reset handler
782  * @dev:	ishtp device
783  *
784  * ISHTP Handler for IPC_RESET notification
785  */
786 void ishtp_reset_handler(struct ishtp_device *dev)
787 {
788 	unsigned long	flags;
789 
790 	/* Handle FW-initiated reset */
791 	dev->dev_state = ISHTP_DEV_RESETTING;
792 
793 	/* Clear BH processing queue - no further HBMs */
794 	spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
795 	dev->rd_msg_fifo_head = dev->rd_msg_fifo_tail = 0;
796 	spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
797 
798 	/* Handle ISH FW reset against upper layers */
799 	ishtp_bus_remove_all_clients(dev, true);
800 }
801 EXPORT_SYMBOL(ishtp_reset_handler);
802 
803 /**
804  * ishtp_reset_compl_handler() - Reset completion handler
805  * @dev:	ishtp device
806  *
807  * ISHTP handler for IPC_RESET sequence completion to start
808  * host message bus start protocol sequence.
809  */
810 void ishtp_reset_compl_handler(struct ishtp_device *dev)
811 {
812 	dev->dev_state = ISHTP_DEV_INIT_CLIENTS;
813 	dev->hbm_state = ISHTP_HBM_START;
814 	ishtp_hbm_start_req(dev);
815 }
816 EXPORT_SYMBOL(ishtp_reset_compl_handler);
817 
818 /**
819  * ishtp_use_dma_transfer() - Function to use DMA
820  *
821  * This interface is used to enable usage of DMA
822  *
823  * Return non zero if DMA can be enabled
824  */
825 int ishtp_use_dma_transfer(void)
826 {
827 	return ishtp_use_dma;
828 }
829 
830 /**
831  * ishtp_device() - Return device pointer
832  * @device: ISH-TP client device instance
833  *
834  * This interface is used to return device pointer from ishtp_cl_device
835  * instance.
836  *
837  * Return: device *.
838  */
839 struct device *ishtp_device(struct ishtp_cl_device *device)
840 {
841 	return &device->dev;
842 }
843 EXPORT_SYMBOL(ishtp_device);
844 
845 /**
846  * ishtp_wait_resume() - Wait for IPC resume
847  * @dev: ishtp device
848  *
849  * Wait for IPC resume
850  *
851  * Return: resume complete or not
852  */
853 bool ishtp_wait_resume(struct ishtp_device *dev)
854 {
855 	/* 50ms to get resume response */
856 	#define WAIT_FOR_RESUME_ACK_MS		50
857 
858 	/* Waiting to get resume response */
859 	if (dev->resume_flag)
860 		wait_event_interruptible_timeout(dev->resume_wait,
861 						 !dev->resume_flag,
862 						 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
863 
864 	return (!dev->resume_flag);
865 }
866 EXPORT_SYMBOL_GPL(ishtp_wait_resume);
867 
868 /**
869  * ishtp_get_pci_device() - Return PCI device dev pointer
870  * This interface is used to return PCI device pointer
871  * from ishtp_cl_device instance.
872  * @device: ISH-TP client device instance
873  *
874  * Return: device *.
875  */
876 struct device *ishtp_get_pci_device(struct ishtp_cl_device *device)
877 {
878 	return device->ishtp_dev->devc;
879 }
880 EXPORT_SYMBOL(ishtp_get_pci_device);
881 
882 /**
883  * ishtp_trace_callback() - Return trace callback
884  * @cl_device: ISH-TP client device instance
885  *
886  * This interface is used to return trace callback function pointer.
887  *
888  * Return: *ishtp_print_log()
889  */
890 ishtp_print_log ishtp_trace_callback(struct ishtp_cl_device *cl_device)
891 {
892 	return cl_device->ishtp_dev->print_log;
893 }
894 EXPORT_SYMBOL(ishtp_trace_callback);
895 
896 /**
897  * ish_hw_reset() - Call HW reset IPC callback
898  * @dev:	ISHTP device instance
899  *
900  * This interface is used to reset HW in case of error.
901  *
902  * Return: value from IPC hw_reset callback
903  */
904 int ish_hw_reset(struct ishtp_device *dev)
905 {
906 	return dev->ops->hw_reset(dev);
907 }
908 EXPORT_SYMBOL(ish_hw_reset);
909 
910 /**
911  * ishtp_bus_register() - Function to register bus
912  *
913  * This register ishtp bus
914  *
915  * Return: Return output of bus_register
916  */
917 static int  __init ishtp_bus_register(void)
918 {
919 	return bus_register(&ishtp_cl_bus_type);
920 }
921 
922 /**
923  * ishtp_bus_unregister() - Function to unregister bus
924  *
925  * This unregister ishtp bus
926  */
927 static void __exit ishtp_bus_unregister(void)
928 {
929 	bus_unregister(&ishtp_cl_bus_type);
930 }
931 
932 module_init(ishtp_bus_register);
933 module_exit(ishtp_bus_unregister);
934 
935 MODULE_DESCRIPTION("ISHTP bus driver");
936 MODULE_LICENSE("GPL");
937