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