xref: /linux/drivers/hid/intel-ish-hid/ishtp/bus.c (revision 79b95d74470dd97d7d0908d5a3c0734a23e51aa4)
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  */
ishtp_recv(struct ishtp_device * dev)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  */
ishtp_send_msg(struct ishtp_device * dev,struct ishtp_msg_hdr * hdr,void * msg,void (* ipc_send_compl)(void *),void * ipc_send_compl_prm)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  */
ishtp_write_message(struct ishtp_device * dev,struct ishtp_msg_hdr * hdr,void * buf)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  */
ishtp_fw_cl_by_uuid(struct ishtp_device * dev,const guid_t * uuid)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  */
ishtp_fw_cl_get_client(struct ishtp_device * dev,const guid_t * uuid)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  */
ishtp_get_fw_client_id(struct ishtp_fw_client * fw_client)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  */
ishtp_fw_cl_by_id(struct ishtp_device * dev,uint8_t client_id)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  */
ishtp_cl_device_probe(struct device * dev)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  */
ishtp_cl_bus_match(struct device * dev,const struct device_driver * drv)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 	struct ishtp_fw_client *client = device->fw_client;
244 	const struct ishtp_device_id *id;
245 
246 	if (client) {
247 		for (id = driver->id; !guid_is_null(&id->guid); id++) {
248 			if (guid_equal(&id->guid, &client->props.protocol_name))
249 				return 1;
250 		}
251 	}
252 
253 	return 0;
254 }
255 
256 /**
257  * ishtp_cl_device_remove() - Bus remove() callback
258  * @dev: the device structure
259  *
260  * This is a bus remove callback and calls the drive remove function.
261  * Since the ISH driver model supports only built in, this is
262  * primarily can be called during pci driver init failure.
263  *
264  * Return: Return value from driver remove() call.
265  */
ishtp_cl_device_remove(struct device * dev)266 static void ishtp_cl_device_remove(struct device *dev)
267 {
268 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
269 	struct ishtp_cl_driver *driver = to_ishtp_cl_driver(dev->driver);
270 
271 	if (device->event_cb) {
272 		device->event_cb = NULL;
273 		cancel_work_sync(&device->event_work);
274 	}
275 
276 	if (driver->remove)
277 		driver->remove(device);
278 }
279 
280 /**
281  * ishtp_cl_device_suspend() - Bus suspend callback
282  * @dev:	device
283  *
284  * Called during device suspend process.
285  *
286  * Return: Return value from driver suspend() call.
287  */
ishtp_cl_device_suspend(struct device * dev)288 static int ishtp_cl_device_suspend(struct device *dev)
289 {
290 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
291 	struct ishtp_cl_driver *driver;
292 	int ret = 0;
293 
294 	if (!device)
295 		return 0;
296 
297 	driver = to_ishtp_cl_driver(dev->driver);
298 	if (driver && driver->driver.pm) {
299 		if (driver->driver.pm->suspend)
300 			ret = driver->driver.pm->suspend(dev);
301 	}
302 
303 	return ret;
304 }
305 
306 /**
307  * ishtp_cl_device_resume() - Bus resume callback
308  * @dev:	device
309  *
310  * Called during device resume process.
311  *
312  * Return: Return value from driver resume() call.
313  */
ishtp_cl_device_resume(struct device * dev)314 static int ishtp_cl_device_resume(struct device *dev)
315 {
316 	struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
317 	struct ishtp_cl_driver *driver;
318 	int ret = 0;
319 
320 	if (!device)
321 		return 0;
322 
323 	driver = to_ishtp_cl_driver(dev->driver);
324 	if (driver && driver->driver.pm) {
325 		if (driver->driver.pm->resume)
326 			ret = driver->driver.pm->resume(dev);
327 	}
328 
329 	return ret;
330 }
331 
332 /**
333  * ishtp_cl_device_reset() - Reset callback
334  * @device:	ishtp client device instance
335  *
336  * This is a callback when HW reset is done and the device need
337  * reinit.
338  *
339  * Return: Return value from driver reset() call.
340  */
ishtp_cl_device_reset(struct ishtp_cl_device * device)341 static int ishtp_cl_device_reset(struct ishtp_cl_device *device)
342 {
343 	struct ishtp_cl_driver *driver;
344 	int ret = 0;
345 
346 	device->event_cb = NULL;
347 	cancel_work_sync(&device->event_work);
348 
349 	driver = to_ishtp_cl_driver(device->dev.driver);
350 	if (driver && driver->reset)
351 		ret = driver->reset(device);
352 
353 	return ret;
354 }
355 
modalias_show(struct device * dev,struct device_attribute * a,char * buf)356 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
357 	char *buf)
358 {
359 	int len;
360 
361 	len = snprintf(buf, PAGE_SIZE, ISHTP_MODULE_PREFIX "%s\n", dev_name(dev));
362 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
363 }
364 static DEVICE_ATTR_RO(modalias);
365 
366 static struct attribute *ishtp_cl_dev_attrs[] = {
367 	&dev_attr_modalias.attr,
368 	NULL,
369 };
370 ATTRIBUTE_GROUPS(ishtp_cl_dev);
371 
ishtp_cl_uevent(const struct device * dev,struct kobj_uevent_env * env)372 static int ishtp_cl_uevent(const struct device *dev, struct kobj_uevent_env *env)
373 {
374 	if (add_uevent_var(env, "MODALIAS=" ISHTP_MODULE_PREFIX "%s", dev_name(dev)))
375 		return -ENOMEM;
376 	return 0;
377 }
378 
379 static const struct dev_pm_ops ishtp_cl_bus_dev_pm_ops = {
380 	/* Suspend callbacks */
381 	.suspend = ishtp_cl_device_suspend,
382 	.resume = ishtp_cl_device_resume,
383 	/* Hibernate callbacks */
384 	.freeze = ishtp_cl_device_suspend,
385 	.thaw = ishtp_cl_device_resume,
386 	.restore = ishtp_cl_device_resume,
387 };
388 
389 static const struct bus_type ishtp_cl_bus_type = {
390 	.name		= "ishtp",
391 	.dev_groups	= ishtp_cl_dev_groups,
392 	.probe		= ishtp_cl_device_probe,
393 	.match		= ishtp_cl_bus_match,
394 	.remove		= ishtp_cl_device_remove,
395 	.pm		= &ishtp_cl_bus_dev_pm_ops,
396 	.uevent		= ishtp_cl_uevent,
397 };
398 
ishtp_cl_dev_release(struct device * dev)399 static void ishtp_cl_dev_release(struct device *dev)
400 {
401 	kfree(to_ishtp_cl_device(dev));
402 }
403 
404 static const struct device_type ishtp_cl_device_type = {
405 	.release	= ishtp_cl_dev_release,
406 };
407 
408 /**
409  * ishtp_bus_add_device() - Function to create device on bus
410  * @dev:	ishtp device
411  * @uuid:	uuid of the client
412  * @name:	Name of the client
413  *
414  * Allocate ISHTP bus client device, attach it to uuid
415  * and register with ISHTP bus.
416  *
417  * Return: ishtp_cl_device pointer or NULL on failure
418  */
ishtp_bus_add_device(struct ishtp_device * dev,guid_t uuid,char * name)419 static struct ishtp_cl_device *ishtp_bus_add_device(struct ishtp_device *dev,
420 						    guid_t uuid, char *name)
421 {
422 	struct ishtp_cl_device *device;
423 	int status;
424 	unsigned long flags;
425 
426 	spin_lock_irqsave(&dev->device_list_lock, flags);
427 	list_for_each_entry(device, &dev->device_list, device_link) {
428 		if (!strcmp(name, dev_name(&device->dev))) {
429 			device->fw_client = &dev->fw_clients[
430 				dev->fw_client_presentation_num - 1];
431 			spin_unlock_irqrestore(&dev->device_list_lock, flags);
432 			ishtp_cl_device_reset(device);
433 			return device;
434 		}
435 	}
436 	spin_unlock_irqrestore(&dev->device_list_lock, flags);
437 
438 	device = kzalloc(sizeof(struct ishtp_cl_device), GFP_KERNEL);
439 	if (!device)
440 		return NULL;
441 
442 	device->dev.parent = dev->devc;
443 	device->dev.bus = &ishtp_cl_bus_type;
444 	device->dev.type = &ishtp_cl_device_type;
445 	device->ishtp_dev = dev;
446 
447 	device->fw_client =
448 		&dev->fw_clients[dev->fw_client_presentation_num - 1];
449 
450 	dev_set_name(&device->dev, "%s", name);
451 
452 	spin_lock_irqsave(&dev->device_list_lock, flags);
453 	list_add_tail(&device->device_link, &dev->device_list);
454 	spin_unlock_irqrestore(&dev->device_list_lock, flags);
455 
456 	status = device_register(&device->dev);
457 	if (status) {
458 		spin_lock_irqsave(&dev->device_list_lock, flags);
459 		list_del(&device->device_link);
460 		spin_unlock_irqrestore(&dev->device_list_lock, flags);
461 		dev_err(dev->devc, "Failed to register ISHTP client device\n");
462 		put_device(&device->dev);
463 		return NULL;
464 	}
465 
466 	ishtp_device_ready = true;
467 
468 	return device;
469 }
470 
471 /**
472  * ishtp_bus_remove_device() - Function to relase device on bus
473  * @device:	client device instance
474  *
475  * This is a counterpart of ishtp_bus_add_device.
476  * Device is unregistered.
477  * the device structure is freed in 'ishtp_cl_dev_release' function
478  * Called only during error in pci driver init path.
479  */
ishtp_bus_remove_device(struct ishtp_cl_device * device)480 static void ishtp_bus_remove_device(struct ishtp_cl_device *device)
481 {
482 	device_unregister(&device->dev);
483 }
484 
485 /**
486  * ishtp_cl_driver_register() - Client driver register
487  * @driver:	the client driver instance
488  * @owner:	Owner of this driver module
489  *
490  * Once a client driver is probed, it created a client
491  * instance and registers with the bus.
492  *
493  * Return: Return value of driver_register or -ENODEV if not ready
494  */
ishtp_cl_driver_register(struct ishtp_cl_driver * driver,struct module * owner)495 int ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
496 			     struct module *owner)
497 {
498 	if (!ishtp_device_ready)
499 		return -ENODEV;
500 
501 	driver->driver.name = driver->name;
502 	driver->driver.owner = owner;
503 	driver->driver.bus = &ishtp_cl_bus_type;
504 
505 	return driver_register(&driver->driver);
506 }
507 EXPORT_SYMBOL(ishtp_cl_driver_register);
508 
509 /**
510  * ishtp_cl_driver_unregister() - Client driver unregister
511  * @driver:	the client driver instance
512  *
513  * Unregister client during device removal process.
514  */
ishtp_cl_driver_unregister(struct ishtp_cl_driver * driver)515 void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver)
516 {
517 	driver_unregister(&driver->driver);
518 }
519 EXPORT_SYMBOL(ishtp_cl_driver_unregister);
520 
521 /**
522  * ishtp_bus_event_work() - event work function
523  * @work:	work struct pointer
524  *
525  * Once an event is received for a client this work
526  * function is called. If the device has registered a
527  * callback then the callback is called.
528  */
ishtp_bus_event_work(struct work_struct * work)529 static void ishtp_bus_event_work(struct work_struct *work)
530 {
531 	struct ishtp_cl_device *device;
532 
533 	device = container_of(work, struct ishtp_cl_device, event_work);
534 
535 	if (device->event_cb)
536 		device->event_cb(device);
537 }
538 
539 /**
540  * ishtp_cl_bus_rx_event() - schedule event work
541  * @device:	client device instance
542  *
543  * Once an event is received for a client this schedules
544  * a work function to process.
545  */
ishtp_cl_bus_rx_event(struct ishtp_cl_device * device)546 void ishtp_cl_bus_rx_event(struct ishtp_cl_device *device)
547 {
548 	if (!device || !device->event_cb)
549 		return;
550 
551 	if (device->event_cb)
552 		queue_work(device->ishtp_dev->unbound_wq, &device->event_work);
553 }
554 
555 /**
556  * ishtp_register_event_cb() - Register callback
557  * @device:	client device instance
558  * @event_cb:	Event processor for an client
559  *
560  * Register a callback for events, called from client driver
561  *
562  * Return: Return 0 or -EALREADY if already registered
563  */
ishtp_register_event_cb(struct ishtp_cl_device * device,void (* event_cb)(struct ishtp_cl_device *))564 int ishtp_register_event_cb(struct ishtp_cl_device *device,
565 	void (*event_cb)(struct ishtp_cl_device *))
566 {
567 	if (device->event_cb)
568 		return -EALREADY;
569 
570 	device->event_cb = event_cb;
571 	INIT_WORK(&device->event_work, ishtp_bus_event_work);
572 
573 	return 0;
574 }
575 EXPORT_SYMBOL(ishtp_register_event_cb);
576 
577 /**
578  * ishtp_get_device() - update usage count for the device
579  * @cl_device:	client device instance
580  *
581  * Increment the usage count. The device can't be deleted
582  */
ishtp_get_device(struct ishtp_cl_device * cl_device)583 void ishtp_get_device(struct ishtp_cl_device *cl_device)
584 {
585 	cl_device->reference_count++;
586 }
587 EXPORT_SYMBOL(ishtp_get_device);
588 
589 /**
590  * ishtp_put_device() - decrement usage count for the device
591  * @cl_device:	client device instance
592  *
593  * Decrement the usage count. The device can be deleted is count = 0
594  */
ishtp_put_device(struct ishtp_cl_device * cl_device)595 void ishtp_put_device(struct ishtp_cl_device *cl_device)
596 {
597 	cl_device->reference_count--;
598 }
599 EXPORT_SYMBOL(ishtp_put_device);
600 
601 /**
602  * ishtp_set_drvdata() - set client driver data
603  * @cl_device:	client device instance
604  * @data:	driver data need to be set
605  *
606  * Set client driver data to cl_device->driver_data.
607  */
ishtp_set_drvdata(struct ishtp_cl_device * cl_device,void * data)608 void ishtp_set_drvdata(struct ishtp_cl_device *cl_device, void *data)
609 {
610 	cl_device->driver_data = data;
611 }
612 EXPORT_SYMBOL(ishtp_set_drvdata);
613 
614 /**
615  * ishtp_get_drvdata() - get client driver data
616  * @cl_device:	client device instance
617  *
618  * Get client driver data from cl_device->driver_data.
619  *
620  * Return: pointer of driver data
621  */
ishtp_get_drvdata(struct ishtp_cl_device * cl_device)622 void *ishtp_get_drvdata(struct ishtp_cl_device *cl_device)
623 {
624 	return cl_device->driver_data;
625 }
626 EXPORT_SYMBOL(ishtp_get_drvdata);
627 
628 /**
629  * ishtp_dev_to_cl_device() - get ishtp_cl_device instance from device instance
630  * @device: device instance
631  *
632  * Get ish_cl_device instance which embeds device instance in it.
633  *
634  * Return: pointer to ishtp_cl_device instance
635  */
ishtp_dev_to_cl_device(struct device * device)636 struct ishtp_cl_device *ishtp_dev_to_cl_device(struct device *device)
637 {
638 	return to_ishtp_cl_device(device);
639 }
640 EXPORT_SYMBOL(ishtp_dev_to_cl_device);
641 
642 /**
643  * ishtp_bus_new_client() - Create a new client
644  * @dev:	ISHTP device instance
645  *
646  * Once bus protocol enumerates a client, this is called
647  * to add a device for the client.
648  *
649  * Return: 0 on success or error code on failure
650  */
ishtp_bus_new_client(struct ishtp_device * dev)651 int ishtp_bus_new_client(struct ishtp_device *dev)
652 {
653 	int	i;
654 	char	*dev_name;
655 	struct ishtp_cl_device	*cl_device;
656 	guid_t	device_uuid;
657 
658 	/*
659 	 * For all reported clients, create an unconnected client and add its
660 	 * device to ISHTP bus.
661 	 * If appropriate driver has loaded, this will trigger its probe().
662 	 * Otherwise, probe() will be called when driver is loaded
663 	 */
664 	i = dev->fw_client_presentation_num - 1;
665 	device_uuid = dev->fw_clients[i].props.protocol_name;
666 	dev_name = kasprintf(GFP_KERNEL, "{%pUL}", &device_uuid);
667 	if (!dev_name)
668 		return	-ENOMEM;
669 
670 	cl_device = ishtp_bus_add_device(dev, device_uuid, dev_name);
671 	if (!cl_device) {
672 		kfree(dev_name);
673 		return	-ENOENT;
674 	}
675 
676 	kfree(dev_name);
677 
678 	return	0;
679 }
680 
681 /**
682  * ishtp_cl_device_bind() - bind a device
683  * @cl:		ishtp client device
684  *
685  * Binds connected ishtp_cl to ISHTP bus device
686  *
687  * Return: 0 on success or fault code
688  */
ishtp_cl_device_bind(struct ishtp_cl * cl)689 int ishtp_cl_device_bind(struct ishtp_cl *cl)
690 {
691 	struct ishtp_cl_device	*cl_device;
692 	unsigned long flags;
693 	int	rv;
694 
695 	if (!cl->fw_client_id || cl->state != ISHTP_CL_CONNECTED)
696 		return	-EFAULT;
697 
698 	rv = -ENOENT;
699 	spin_lock_irqsave(&cl->dev->device_list_lock, flags);
700 	list_for_each_entry(cl_device, &cl->dev->device_list,
701 			device_link) {
702 		if (cl_device->fw_client &&
703 		    cl_device->fw_client->client_id == cl->fw_client_id) {
704 			cl->device = cl_device;
705 			rv = 0;
706 			break;
707 		}
708 	}
709 	spin_unlock_irqrestore(&cl->dev->device_list_lock, flags);
710 	return	rv;
711 }
712 
713 /**
714  * ishtp_bus_remove_all_clients() - Remove all clients
715  * @ishtp_dev:		ishtp device
716  * @warm_reset:		Reset due to FW reset dure to errors or S3 suspend
717  *
718  * This is part of reset/remove flow. This function the main processing
719  * only targets error processing, if the FW has forced reset or
720  * error to remove connected clients. When warm reset the client devices are
721  * not removed.
722  */
ishtp_bus_remove_all_clients(struct ishtp_device * ishtp_dev,bool warm_reset)723 void ishtp_bus_remove_all_clients(struct ishtp_device *ishtp_dev,
724 				  bool warm_reset)
725 {
726 	struct ishtp_cl_device	*cl_device, *n;
727 	struct ishtp_cl	*cl;
728 	unsigned long	flags;
729 
730 	spin_lock_irqsave(&ishtp_dev->cl_list_lock, flags);
731 	list_for_each_entry(cl, &ishtp_dev->cl_list, link) {
732 		cl->state = ISHTP_CL_DISCONNECTED;
733 		if (warm_reset && cl->device->reference_count)
734 			continue;
735 
736 		/*
737 		 * Wake any pending process. The waiter would check dev->state
738 		 * and determine that it's not enabled already,
739 		 * and will return error to its caller
740 		 */
741 		wake_up_interruptible(&cl->wait_ctrl_res);
742 
743 		/* Disband any pending read/write requests and free rb */
744 		ishtp_cl_flush_queues(cl);
745 
746 		/* Remove all free and in_process rings, both Rx and Tx */
747 		ishtp_cl_free_rx_ring(cl);
748 		ishtp_cl_free_tx_ring(cl);
749 
750 		/*
751 		 * Free client and ISHTP bus client device structures
752 		 * don't free host client because it is part of the OS fd
753 		 * structure
754 		 */
755 	}
756 	spin_unlock_irqrestore(&ishtp_dev->cl_list_lock, flags);
757 
758 	/* Release DMA buffers for client messages */
759 	ishtp_cl_free_dma_buf(ishtp_dev);
760 
761 	/* remove bus clients */
762 	spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
763 	list_for_each_entry_safe(cl_device, n, &ishtp_dev->device_list,
764 				 device_link) {
765 		cl_device->fw_client = NULL;
766 		if (warm_reset && cl_device->reference_count)
767 			continue;
768 
769 		list_del(&cl_device->device_link);
770 		spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
771 		ishtp_bus_remove_device(cl_device);
772 		spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
773 	}
774 	spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
775 
776 	/* Free all client structures */
777 	spin_lock_irqsave(&ishtp_dev->fw_clients_lock, flags);
778 	kfree(ishtp_dev->fw_clients);
779 	ishtp_dev->fw_clients = NULL;
780 	ishtp_dev->fw_clients_num = 0;
781 	ishtp_dev->fw_client_presentation_num = 0;
782 	ishtp_dev->fw_client_index = 0;
783 	bitmap_zero(ishtp_dev->fw_clients_map, ISHTP_CLIENTS_MAX);
784 	spin_unlock_irqrestore(&ishtp_dev->fw_clients_lock, flags);
785 }
786 EXPORT_SYMBOL(ishtp_bus_remove_all_clients);
787 
788 /**
789  * ishtp_reset_handler() - IPC reset handler
790  * @dev:	ishtp device
791  *
792  * ISHTP Handler for IPC_RESET notification
793  */
ishtp_reset_handler(struct ishtp_device * dev)794 void ishtp_reset_handler(struct ishtp_device *dev)
795 {
796 	unsigned long	flags;
797 
798 	/* Handle FW-initiated reset */
799 	dev->dev_state = ISHTP_DEV_RESETTING;
800 
801 	/* Clear BH processing queue - no further HBMs */
802 	spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
803 	dev->rd_msg_fifo_head = dev->rd_msg_fifo_tail = 0;
804 	spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
805 
806 	/* Handle ISH FW reset against upper layers */
807 	ishtp_bus_remove_all_clients(dev, true);
808 }
809 EXPORT_SYMBOL(ishtp_reset_handler);
810 
811 /**
812  * ishtp_reset_compl_handler() - Reset completion handler
813  * @dev:	ishtp device
814  *
815  * ISHTP handler for IPC_RESET sequence completion to start
816  * host message bus start protocol sequence.
817  */
ishtp_reset_compl_handler(struct ishtp_device * dev)818 void ishtp_reset_compl_handler(struct ishtp_device *dev)
819 {
820 	dev->dev_state = ISHTP_DEV_INIT_CLIENTS;
821 	dev->hbm_state = ISHTP_HBM_START;
822 	ishtp_hbm_start_req(dev);
823 }
824 EXPORT_SYMBOL(ishtp_reset_compl_handler);
825 
826 /**
827  * ishtp_use_dma_transfer() - Function to use DMA
828  *
829  * This interface is used to enable usage of DMA
830  *
831  * Return non zero if DMA can be enabled
832  */
ishtp_use_dma_transfer(void)833 int ishtp_use_dma_transfer(void)
834 {
835 	return ishtp_use_dma;
836 }
837 
838 /**
839  * ishtp_device() - Return device pointer
840  * @device: ISH-TP client device instance
841  *
842  * This interface is used to return device pointer from ishtp_cl_device
843  * instance.
844  *
845  * Return: device *.
846  */
ishtp_device(struct ishtp_cl_device * device)847 struct device *ishtp_device(struct ishtp_cl_device *device)
848 {
849 	return &device->dev;
850 }
851 EXPORT_SYMBOL(ishtp_device);
852 
853 /**
854  * ishtp_wait_resume() - Wait for IPC resume
855  * @dev: ishtp device
856  *
857  * Wait for IPC resume
858  *
859  * Return: resume complete or not
860  */
ishtp_wait_resume(struct ishtp_device * dev)861 bool ishtp_wait_resume(struct ishtp_device *dev)
862 {
863 	/* Waiting to get resume response */
864 	if (dev->resume_flag)
865 		wait_event_interruptible_timeout(dev->resume_wait,
866 						 !dev->resume_flag,
867 						 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
868 
869 	return (!dev->resume_flag);
870 }
871 EXPORT_SYMBOL_GPL(ishtp_wait_resume);
872 
873 /**
874  * ishtp_get_pci_device() - Return PCI device dev pointer
875  * This interface is used to return PCI device pointer
876  * from ishtp_cl_device instance.
877  * @device: ISH-TP client device instance
878  *
879  * Return: device *.
880  */
ishtp_get_pci_device(struct ishtp_cl_device * device)881 struct device *ishtp_get_pci_device(struct ishtp_cl_device *device)
882 {
883 	return device->ishtp_dev->devc;
884 }
885 EXPORT_SYMBOL(ishtp_get_pci_device);
886 
887 /**
888  * ishtp_get_workqueue - Retrieve the workqueue associated with an ISHTP device
889  * @cl_device: Pointer to the ISHTP client device structure
890  *
891  * Returns the workqueue_struct pointer (unbound_wq) associated with the given
892  * ISHTP client device. This workqueue is typically used for scheduling work
893  * related to the device.
894  *
895  * Return: Pointer to struct workqueue_struct.
896  */
ishtp_get_workqueue(struct ishtp_cl_device * cl_device)897 struct workqueue_struct *ishtp_get_workqueue(struct ishtp_cl_device *cl_device)
898 {
899 	return cl_device->ishtp_dev->unbound_wq;
900 }
901 EXPORT_SYMBOL(ishtp_get_workqueue);
902 
903 /**
904  * ishtp_trace_callback() - Return trace callback
905  * @cl_device: ISH-TP client device instance
906  *
907  * This interface is used to return trace callback function pointer.
908  *
909  * Return: *ishtp_print_log()
910  */
ishtp_trace_callback(struct ishtp_cl_device * cl_device)911 ishtp_print_log ishtp_trace_callback(struct ishtp_cl_device *cl_device)
912 {
913 	return cl_device->ishtp_dev->print_log;
914 }
915 EXPORT_SYMBOL(ishtp_trace_callback);
916 
917 /**
918  * ish_hw_reset() - Call HW reset IPC callback
919  * @dev:	ISHTP device instance
920  *
921  * This interface is used to reset HW in case of error.
922  *
923  * Return: value from IPC hw_reset callback
924  */
ish_hw_reset(struct ishtp_device * dev)925 int ish_hw_reset(struct ishtp_device *dev)
926 {
927 	return dev->ops->hw_reset(dev);
928 }
929 EXPORT_SYMBOL(ish_hw_reset);
930 
931 /**
932  * ishtp_bus_register() - Function to register bus
933  *
934  * This register ishtp bus
935  *
936  * Return: Return output of bus_register
937  */
ishtp_bus_register(void)938 static int  __init ishtp_bus_register(void)
939 {
940 	return bus_register(&ishtp_cl_bus_type);
941 }
942 
943 /**
944  * ishtp_bus_unregister() - Function to unregister bus
945  *
946  * This unregister ishtp bus
947  */
ishtp_bus_unregister(void)948 static void __exit ishtp_bus_unregister(void)
949 {
950 	bus_unregister(&ishtp_cl_bus_type);
951 }
952 
953 module_init(ishtp_bus_register);
954 module_exit(ishtp_bus_unregister);
955 
956 MODULE_DESCRIPTION("ISHTP bus driver");
957 MODULE_LICENSE("GPL");
958