treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / mei / bus.c
blob8d468e0a950a6cd659d310b2855539e3a3d1699d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012-2019, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
5 */
7 #include <linux/module.h>
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/sched/signal.h>
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/interrupt.h>
16 #include <linux/mei_cl_bus.h>
18 #include "mei_dev.h"
19 #include "client.h"
21 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
23 /**
24 * __mei_cl_send - internal client send (write)
26 * @cl: host client
27 * @buf: buffer to send
28 * @length: buffer length
29 * @mode: sending mode
31 * Return: written size bytes or < 0 on error
33 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
34 unsigned int mode)
36 struct mei_device *bus;
37 struct mei_cl_cb *cb;
38 ssize_t rets;
40 if (WARN_ON(!cl || !cl->dev))
41 return -ENODEV;
43 bus = cl->dev;
45 mutex_lock(&bus->device_lock);
46 if (bus->dev_state != MEI_DEV_ENABLED) {
47 rets = -ENODEV;
48 goto out;
51 if (!mei_cl_is_connected(cl)) {
52 rets = -ENODEV;
53 goto out;
56 /* Check if we have an ME client device */
57 if (!mei_me_cl_is_active(cl->me_cl)) {
58 rets = -ENOTTY;
59 goto out;
62 if (length > mei_cl_mtu(cl)) {
63 rets = -EFBIG;
64 goto out;
67 while (cl->tx_cb_queued >= bus->tx_queue_limit) {
68 mutex_unlock(&bus->device_lock);
69 rets = wait_event_interruptible(cl->tx_wait,
70 cl->writing_state == MEI_WRITE_COMPLETE ||
71 (!mei_cl_is_connected(cl)));
72 mutex_lock(&bus->device_lock);
73 if (rets) {
74 if (signal_pending(current))
75 rets = -EINTR;
76 goto out;
78 if (!mei_cl_is_connected(cl)) {
79 rets = -ENODEV;
80 goto out;
84 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
85 if (!cb) {
86 rets = -ENOMEM;
87 goto out;
90 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
91 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
92 memcpy(cb->buf.data, buf, length);
94 rets = mei_cl_write(cl, cb);
96 out:
97 mutex_unlock(&bus->device_lock);
99 return rets;
103 * __mei_cl_recv - internal client receive (read)
105 * @cl: host client
106 * @buf: buffer to receive
107 * @length: buffer length
108 * @mode: io mode
109 * @timeout: recv timeout, 0 for infinite timeout
111 * Return: read size in bytes of < 0 on error
113 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
114 unsigned int mode, unsigned long timeout)
116 struct mei_device *bus;
117 struct mei_cl_cb *cb;
118 size_t r_length;
119 ssize_t rets;
120 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
122 if (WARN_ON(!cl || !cl->dev))
123 return -ENODEV;
125 bus = cl->dev;
127 mutex_lock(&bus->device_lock);
128 if (bus->dev_state != MEI_DEV_ENABLED) {
129 rets = -ENODEV;
130 goto out;
133 cb = mei_cl_read_cb(cl, NULL);
134 if (cb)
135 goto copy;
137 rets = mei_cl_read_start(cl, length, NULL);
138 if (rets && rets != -EBUSY)
139 goto out;
141 if (nonblock) {
142 rets = -EAGAIN;
143 goto out;
146 /* wait on event only if there is no other waiter */
147 /* synchronized under device mutex */
148 if (!waitqueue_active(&cl->rx_wait)) {
150 mutex_unlock(&bus->device_lock);
152 if (timeout) {
153 rets = wait_event_interruptible_timeout
154 (cl->rx_wait,
155 (!list_empty(&cl->rd_completed)) ||
156 (!mei_cl_is_connected(cl)),
157 msecs_to_jiffies(timeout));
158 if (rets == 0)
159 return -ETIME;
160 if (rets < 0) {
161 if (signal_pending(current))
162 return -EINTR;
163 return -ERESTARTSYS;
165 } else {
166 if (wait_event_interruptible
167 (cl->rx_wait,
168 (!list_empty(&cl->rd_completed)) ||
169 (!mei_cl_is_connected(cl)))) {
170 if (signal_pending(current))
171 return -EINTR;
172 return -ERESTARTSYS;
176 mutex_lock(&bus->device_lock);
178 if (!mei_cl_is_connected(cl)) {
179 rets = -ENODEV;
180 goto out;
184 cb = mei_cl_read_cb(cl, NULL);
185 if (!cb) {
186 rets = 0;
187 goto out;
190 copy:
191 if (cb->status) {
192 rets = cb->status;
193 goto free;
196 r_length = min_t(size_t, length, cb->buf_idx);
197 memcpy(buf, cb->buf.data, r_length);
198 rets = r_length;
200 free:
201 mei_io_cb_free(cb);
202 out:
203 mutex_unlock(&bus->device_lock);
205 return rets;
209 * mei_cldev_send - me device send (write)
211 * @cldev: me client device
212 * @buf: buffer to send
213 * @length: buffer length
215 * Return: written size in bytes or < 0 on error
217 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
219 struct mei_cl *cl = cldev->cl;
221 return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING);
223 EXPORT_SYMBOL_GPL(mei_cldev_send);
226 * mei_cldev_recv_nonblock - non block client receive (read)
228 * @cldev: me client device
229 * @buf: buffer to receive
230 * @length: buffer length
232 * Return: read size in bytes of < 0 on error
233 * -EAGAIN if function will block.
235 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
236 size_t length)
238 struct mei_cl *cl = cldev->cl;
240 return __mei_cl_recv(cl, buf, length, MEI_CL_IO_RX_NONBLOCK, 0);
242 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
245 * mei_cldev_recv - client receive (read)
247 * @cldev: me client device
248 * @buf: buffer to receive
249 * @length: buffer length
251 * Return: read size in bytes of < 0 on error
253 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
255 struct mei_cl *cl = cldev->cl;
257 return __mei_cl_recv(cl, buf, length, 0, 0);
259 EXPORT_SYMBOL_GPL(mei_cldev_recv);
262 * mei_cl_bus_rx_work - dispatch rx event for a bus device
264 * @work: work
266 static void mei_cl_bus_rx_work(struct work_struct *work)
268 struct mei_cl_device *cldev;
269 struct mei_device *bus;
271 cldev = container_of(work, struct mei_cl_device, rx_work);
273 bus = cldev->bus;
275 if (cldev->rx_cb)
276 cldev->rx_cb(cldev);
278 mutex_lock(&bus->device_lock);
279 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
280 mutex_unlock(&bus->device_lock);
284 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
286 * @work: work
288 static void mei_cl_bus_notif_work(struct work_struct *work)
290 struct mei_cl_device *cldev;
292 cldev = container_of(work, struct mei_cl_device, notif_work);
294 if (cldev->notif_cb)
295 cldev->notif_cb(cldev);
299 * mei_cl_bus_notify_event - schedule notify cb on bus client
301 * @cl: host client
303 * Return: true if event was scheduled
304 * false if the client is not waiting for event
306 bool mei_cl_bus_notify_event(struct mei_cl *cl)
308 struct mei_cl_device *cldev = cl->cldev;
310 if (!cldev || !cldev->notif_cb)
311 return false;
313 if (!cl->notify_ev)
314 return false;
316 schedule_work(&cldev->notif_work);
318 cl->notify_ev = false;
320 return true;
324 * mei_cl_bus_rx_event - schedule rx event
326 * @cl: host client
328 * Return: true if event was scheduled
329 * false if the client is not waiting for event
331 bool mei_cl_bus_rx_event(struct mei_cl *cl)
333 struct mei_cl_device *cldev = cl->cldev;
335 if (!cldev || !cldev->rx_cb)
336 return false;
338 schedule_work(&cldev->rx_work);
340 return true;
344 * mei_cldev_register_rx_cb - register Rx event callback
346 * @cldev: me client devices
347 * @rx_cb: callback function
349 * Return: 0 on success
350 * -EALREADY if an callback is already registered
351 * <0 on other errors
353 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
355 struct mei_device *bus = cldev->bus;
356 int ret;
358 if (!rx_cb)
359 return -EINVAL;
360 if (cldev->rx_cb)
361 return -EALREADY;
363 cldev->rx_cb = rx_cb;
364 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
366 mutex_lock(&bus->device_lock);
367 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
368 mutex_unlock(&bus->device_lock);
369 if (ret && ret != -EBUSY)
370 return ret;
372 return 0;
374 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
377 * mei_cldev_register_notif_cb - register FW notification event callback
379 * @cldev: me client devices
380 * @notif_cb: callback function
382 * Return: 0 on success
383 * -EALREADY if an callback is already registered
384 * <0 on other errors
386 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
387 mei_cldev_cb_t notif_cb)
389 struct mei_device *bus = cldev->bus;
390 int ret;
392 if (!notif_cb)
393 return -EINVAL;
395 if (cldev->notif_cb)
396 return -EALREADY;
398 cldev->notif_cb = notif_cb;
399 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
401 mutex_lock(&bus->device_lock);
402 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
403 mutex_unlock(&bus->device_lock);
404 if (ret)
405 return ret;
407 return 0;
409 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
412 * mei_cldev_get_drvdata - driver data getter
414 * @cldev: mei client device
416 * Return: driver private data
418 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
420 return dev_get_drvdata(&cldev->dev);
422 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
425 * mei_cldev_set_drvdata - driver data setter
427 * @cldev: mei client device
428 * @data: data to store
430 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
432 dev_set_drvdata(&cldev->dev, data);
434 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
437 * mei_cldev_uuid - return uuid of the underlying me client
439 * @cldev: mei client device
441 * Return: me client uuid
443 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
445 return mei_me_cl_uuid(cldev->me_cl);
447 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
450 * mei_cldev_ver - return protocol version of the underlying me client
452 * @cldev: mei client device
454 * Return: me client protocol version
456 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
458 return mei_me_cl_ver(cldev->me_cl);
460 EXPORT_SYMBOL_GPL(mei_cldev_ver);
463 * mei_cldev_enabled - check whether the device is enabled
465 * @cldev: mei client device
467 * Return: true if me client is initialized and connected
469 bool mei_cldev_enabled(struct mei_cl_device *cldev)
471 return mei_cl_is_connected(cldev->cl);
473 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
476 * mei_cl_bus_module_get - acquire module of the underlying
477 * hw driver.
479 * @cldev: mei client device
481 * Return: true on success; false if the module was removed.
483 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
485 return try_module_get(cldev->bus->dev->driver->owner);
489 * mei_cl_bus_module_put - release the underlying hw module.
491 * @cldev: mei client device
493 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
495 module_put(cldev->bus->dev->driver->owner);
499 * mei_cldev_enable - enable me client device
500 * create connection with me client
502 * @cldev: me client device
504 * Return: 0 on success and < 0 on error
506 int mei_cldev_enable(struct mei_cl_device *cldev)
508 struct mei_device *bus = cldev->bus;
509 struct mei_cl *cl;
510 int ret;
512 cl = cldev->cl;
514 mutex_lock(&bus->device_lock);
515 if (cl->state == MEI_FILE_UNINITIALIZED) {
516 ret = mei_cl_link(cl);
517 if (ret)
518 goto out;
519 /* update pointers */
520 cl->cldev = cldev;
523 if (mei_cl_is_connected(cl)) {
524 ret = 0;
525 goto out;
528 if (!mei_me_cl_is_active(cldev->me_cl)) {
529 dev_err(&cldev->dev, "me client is not active\n");
530 ret = -ENOTTY;
531 goto out;
534 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
535 if (ret < 0)
536 dev_err(&cldev->dev, "cannot connect\n");
538 out:
539 mutex_unlock(&bus->device_lock);
541 return ret;
543 EXPORT_SYMBOL_GPL(mei_cldev_enable);
546 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
547 * callbacks.
549 * @cldev: client device
551 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
553 if (cldev->rx_cb) {
554 cancel_work_sync(&cldev->rx_work);
555 cldev->rx_cb = NULL;
558 if (cldev->notif_cb) {
559 cancel_work_sync(&cldev->notif_work);
560 cldev->notif_cb = NULL;
565 * mei_cldev_disable - disable me client device
566 * disconnect form the me client
568 * @cldev: me client device
570 * Return: 0 on success and < 0 on error
572 int mei_cldev_disable(struct mei_cl_device *cldev)
574 struct mei_device *bus;
575 struct mei_cl *cl;
576 int err;
578 if (!cldev)
579 return -ENODEV;
581 cl = cldev->cl;
583 bus = cldev->bus;
585 mei_cldev_unregister_callbacks(cldev);
587 mutex_lock(&bus->device_lock);
589 if (!mei_cl_is_connected(cl)) {
590 dev_dbg(bus->dev, "Already disconnected\n");
591 err = 0;
592 goto out;
595 err = mei_cl_disconnect(cl);
596 if (err < 0)
597 dev_err(bus->dev, "Could not disconnect from the ME client\n");
599 out:
600 /* Flush queues and remove any pending read */
601 mei_cl_flush_queues(cl, NULL);
602 mei_cl_unlink(cl);
604 mutex_unlock(&bus->device_lock);
605 return err;
607 EXPORT_SYMBOL_GPL(mei_cldev_disable);
610 * mei_cl_device_find - find matching entry in the driver id table
612 * @cldev: me client device
613 * @cldrv: me client driver
615 * Return: id on success; NULL if no id is matching
617 static const
618 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
619 struct mei_cl_driver *cldrv)
621 const struct mei_cl_device_id *id;
622 const uuid_le *uuid;
623 u8 version;
624 bool match;
626 uuid = mei_me_cl_uuid(cldev->me_cl);
627 version = mei_me_cl_ver(cldev->me_cl);
629 id = cldrv->id_table;
630 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
631 if (!uuid_le_cmp(*uuid, id->uuid)) {
632 match = true;
634 if (cldev->name[0])
635 if (strncmp(cldev->name, id->name,
636 sizeof(id->name)))
637 match = false;
639 if (id->version != MEI_CL_VERSION_ANY)
640 if (id->version != version)
641 match = false;
642 if (match)
643 return id;
646 id++;
649 return NULL;
653 * mei_cl_device_match - device match function
655 * @dev: device
656 * @drv: driver
658 * Return: 1 if matching device was found 0 otherwise
660 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
662 struct mei_cl_device *cldev = to_mei_cl_device(dev);
663 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
664 const struct mei_cl_device_id *found_id;
666 if (!cldev)
667 return 0;
669 if (!cldev->do_match)
670 return 0;
672 if (!cldrv || !cldrv->id_table)
673 return 0;
675 found_id = mei_cl_device_find(cldev, cldrv);
676 if (found_id)
677 return 1;
679 return 0;
683 * mei_cl_device_probe - bus probe function
685 * @dev: device
687 * Return: 0 on success; < 0 otherwise
689 static int mei_cl_device_probe(struct device *dev)
691 struct mei_cl_device *cldev;
692 struct mei_cl_driver *cldrv;
693 const struct mei_cl_device_id *id;
694 int ret;
696 cldev = to_mei_cl_device(dev);
697 cldrv = to_mei_cl_driver(dev->driver);
699 if (!cldev)
700 return 0;
702 if (!cldrv || !cldrv->probe)
703 return -ENODEV;
705 id = mei_cl_device_find(cldev, cldrv);
706 if (!id)
707 return -ENODEV;
709 if (!mei_cl_bus_module_get(cldev)) {
710 dev_err(&cldev->dev, "get hw module failed");
711 return -ENODEV;
714 ret = cldrv->probe(cldev, id);
715 if (ret) {
716 mei_cl_bus_module_put(cldev);
717 return ret;
720 __module_get(THIS_MODULE);
721 return 0;
725 * mei_cl_device_remove - remove device from the bus
727 * @dev: device
729 * Return: 0 on success; < 0 otherwise
731 static int mei_cl_device_remove(struct device *dev)
733 struct mei_cl_device *cldev = to_mei_cl_device(dev);
734 struct mei_cl_driver *cldrv;
735 int ret = 0;
737 if (!cldev || !dev->driver)
738 return 0;
740 cldrv = to_mei_cl_driver(dev->driver);
741 if (cldrv->remove)
742 ret = cldrv->remove(cldev);
744 mei_cldev_unregister_callbacks(cldev);
746 mei_cl_bus_module_put(cldev);
747 module_put(THIS_MODULE);
748 dev->driver = NULL;
749 return ret;
753 static ssize_t name_show(struct device *dev, struct device_attribute *a,
754 char *buf)
756 struct mei_cl_device *cldev = to_mei_cl_device(dev);
758 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
760 static DEVICE_ATTR_RO(name);
762 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
763 char *buf)
765 struct mei_cl_device *cldev = to_mei_cl_device(dev);
766 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
768 return sprintf(buf, "%pUl", uuid);
770 static DEVICE_ATTR_RO(uuid);
772 static ssize_t version_show(struct device *dev, struct device_attribute *a,
773 char *buf)
775 struct mei_cl_device *cldev = to_mei_cl_device(dev);
776 u8 version = mei_me_cl_ver(cldev->me_cl);
778 return sprintf(buf, "%02X", version);
780 static DEVICE_ATTR_RO(version);
782 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
783 char *buf)
785 struct mei_cl_device *cldev = to_mei_cl_device(dev);
786 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
787 u8 version = mei_me_cl_ver(cldev->me_cl);
789 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
790 cldev->name, uuid, version);
792 static DEVICE_ATTR_RO(modalias);
794 static ssize_t max_conn_show(struct device *dev, struct device_attribute *a,
795 char *buf)
797 struct mei_cl_device *cldev = to_mei_cl_device(dev);
798 u8 maxconn = mei_me_cl_max_conn(cldev->me_cl);
800 return sprintf(buf, "%d", maxconn);
802 static DEVICE_ATTR_RO(max_conn);
804 static ssize_t fixed_show(struct device *dev, struct device_attribute *a,
805 char *buf)
807 struct mei_cl_device *cldev = to_mei_cl_device(dev);
808 u8 fixed = mei_me_cl_fixed(cldev->me_cl);
810 return sprintf(buf, "%d", fixed);
812 static DEVICE_ATTR_RO(fixed);
814 static ssize_t max_len_show(struct device *dev, struct device_attribute *a,
815 char *buf)
817 struct mei_cl_device *cldev = to_mei_cl_device(dev);
818 u32 maxlen = mei_me_cl_max_len(cldev->me_cl);
820 return sprintf(buf, "%u", maxlen);
822 static DEVICE_ATTR_RO(max_len);
824 static struct attribute *mei_cldev_attrs[] = {
825 &dev_attr_name.attr,
826 &dev_attr_uuid.attr,
827 &dev_attr_version.attr,
828 &dev_attr_modalias.attr,
829 &dev_attr_max_conn.attr,
830 &dev_attr_fixed.attr,
831 &dev_attr_max_len.attr,
832 NULL,
834 ATTRIBUTE_GROUPS(mei_cldev);
837 * mei_cl_device_uevent - me client bus uevent handler
839 * @dev: device
840 * @env: uevent kobject
842 * Return: 0 on success -ENOMEM on when add_uevent_var fails
844 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
846 struct mei_cl_device *cldev = to_mei_cl_device(dev);
847 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
848 u8 version = mei_me_cl_ver(cldev->me_cl);
850 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
851 return -ENOMEM;
853 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
854 return -ENOMEM;
856 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
857 return -ENOMEM;
859 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
860 cldev->name, uuid, version))
861 return -ENOMEM;
863 return 0;
866 static struct bus_type mei_cl_bus_type = {
867 .name = "mei",
868 .dev_groups = mei_cldev_groups,
869 .match = mei_cl_device_match,
870 .probe = mei_cl_device_probe,
871 .remove = mei_cl_device_remove,
872 .uevent = mei_cl_device_uevent,
875 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
877 if (bus)
878 get_device(bus->dev);
880 return bus;
883 static void mei_dev_bus_put(struct mei_device *bus)
885 if (bus)
886 put_device(bus->dev);
889 static void mei_cl_bus_dev_release(struct device *dev)
891 struct mei_cl_device *cldev = to_mei_cl_device(dev);
893 if (!cldev)
894 return;
896 mei_me_cl_put(cldev->me_cl);
897 mei_dev_bus_put(cldev->bus);
898 mei_cl_unlink(cldev->cl);
899 kfree(cldev->cl);
900 kfree(cldev);
903 static const struct device_type mei_cl_device_type = {
904 .release = mei_cl_bus_dev_release,
908 * mei_cl_bus_set_name - set device name for me client device
909 * <controller>-<client device>
910 * Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb
912 * @cldev: me client device
914 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
916 dev_set_name(&cldev->dev, "%s-%pUl",
917 dev_name(cldev->bus->dev),
918 mei_me_cl_uuid(cldev->me_cl));
922 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
924 * @bus: mei device
925 * @me_cl: me client
927 * Return: allocated device structur or NULL on allocation failure
929 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
930 struct mei_me_client *me_cl)
932 struct mei_cl_device *cldev;
933 struct mei_cl *cl;
935 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
936 if (!cldev)
937 return NULL;
939 cl = mei_cl_allocate(bus);
940 if (!cl) {
941 kfree(cldev);
942 return NULL;
945 device_initialize(&cldev->dev);
946 cldev->dev.parent = bus->dev;
947 cldev->dev.bus = &mei_cl_bus_type;
948 cldev->dev.type = &mei_cl_device_type;
949 cldev->bus = mei_dev_bus_get(bus);
950 cldev->me_cl = mei_me_cl_get(me_cl);
951 cldev->cl = cl;
952 mei_cl_bus_set_name(cldev);
953 cldev->is_added = 0;
954 INIT_LIST_HEAD(&cldev->bus_list);
956 return cldev;
960 * mei_cl_dev_setup - setup me client device
961 * run fix up routines and set the device name
963 * @bus: mei device
964 * @cldev: me client device
966 * Return: true if the device is eligible for enumeration
968 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
969 struct mei_cl_device *cldev)
971 cldev->do_match = 1;
972 mei_cl_bus_dev_fixup(cldev);
974 /* the device name can change during fix up */
975 if (cldev->do_match)
976 mei_cl_bus_set_name(cldev);
978 return cldev->do_match == 1;
982 * mei_cl_bus_dev_add - add me client devices
984 * @cldev: me client device
986 * Return: 0 on success; < 0 on failre
988 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
990 int ret;
992 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
993 mei_me_cl_uuid(cldev->me_cl),
994 mei_me_cl_ver(cldev->me_cl));
995 ret = device_add(&cldev->dev);
996 if (!ret)
997 cldev->is_added = 1;
999 return ret;
1003 * mei_cl_bus_dev_stop - stop the driver
1005 * @cldev: me client device
1007 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
1009 if (cldev->is_added)
1010 device_release_driver(&cldev->dev);
1014 * mei_cl_bus_dev_destroy - destroy me client devices object
1016 * @cldev: me client device
1018 * Locking: called under "dev->cl_bus_lock" lock
1020 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
1023 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1025 if (!cldev->is_added)
1026 return;
1028 device_del(&cldev->dev);
1030 list_del_init(&cldev->bus_list);
1032 cldev->is_added = 0;
1033 put_device(&cldev->dev);
1037 * mei_cl_bus_remove_device - remove a devices form the bus
1039 * @cldev: me client device
1041 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1043 mei_cl_bus_dev_stop(cldev);
1044 mei_cl_bus_dev_destroy(cldev);
1048 * mei_cl_bus_remove_devices - remove all devices form the bus
1050 * @bus: mei device
1052 void mei_cl_bus_remove_devices(struct mei_device *bus)
1054 struct mei_cl_device *cldev, *next;
1056 mutex_lock(&bus->cl_bus_lock);
1057 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1058 mei_cl_bus_remove_device(cldev);
1059 mutex_unlock(&bus->cl_bus_lock);
1064 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1065 * based on me client
1067 * @bus: mei device
1068 * @me_cl: me client
1070 * Locking: called under "dev->cl_bus_lock" lock
1072 static void mei_cl_bus_dev_init(struct mei_device *bus,
1073 struct mei_me_client *me_cl)
1075 struct mei_cl_device *cldev;
1077 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1079 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1081 if (me_cl->bus_added)
1082 return;
1084 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1085 if (!cldev)
1086 return;
1088 me_cl->bus_added = true;
1089 list_add_tail(&cldev->bus_list, &bus->device_list);
1094 * mei_cl_bus_rescan - scan me clients list and add create
1095 * devices for eligible clients
1097 * @bus: mei device
1099 static void mei_cl_bus_rescan(struct mei_device *bus)
1101 struct mei_cl_device *cldev, *n;
1102 struct mei_me_client *me_cl;
1104 mutex_lock(&bus->cl_bus_lock);
1106 down_read(&bus->me_clients_rwsem);
1107 list_for_each_entry(me_cl, &bus->me_clients, list)
1108 mei_cl_bus_dev_init(bus, me_cl);
1109 up_read(&bus->me_clients_rwsem);
1111 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1113 if (!mei_me_cl_is_active(cldev->me_cl)) {
1114 mei_cl_bus_remove_device(cldev);
1115 continue;
1118 if (cldev->is_added)
1119 continue;
1121 if (mei_cl_bus_dev_setup(bus, cldev))
1122 mei_cl_bus_dev_add(cldev);
1123 else {
1124 list_del_init(&cldev->bus_list);
1125 put_device(&cldev->dev);
1128 mutex_unlock(&bus->cl_bus_lock);
1130 dev_dbg(bus->dev, "rescan end");
1133 void mei_cl_bus_rescan_work(struct work_struct *work)
1135 struct mei_device *bus =
1136 container_of(work, struct mei_device, bus_rescan_work);
1138 mei_cl_bus_rescan(bus);
1141 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1142 struct module *owner)
1144 int err;
1146 cldrv->driver.name = cldrv->name;
1147 cldrv->driver.owner = owner;
1148 cldrv->driver.bus = &mei_cl_bus_type;
1150 err = driver_register(&cldrv->driver);
1151 if (err)
1152 return err;
1154 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1156 return 0;
1158 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1160 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1162 driver_unregister(&cldrv->driver);
1164 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1166 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1169 int __init mei_cl_bus_init(void)
1171 return bus_register(&mei_cl_bus_type);
1174 void __exit mei_cl_bus_exit(void)
1176 bus_unregister(&mei_cl_bus_type);