Linux 4.18.10
[linux/fpc-iii.git] / drivers / misc / mei / bus.c
blob692b2f9a18cb2b174840a6e29d5a1af92ae27c81
1 /*
2 * Intel Management Engine Interface (Intel MEI) Linux driver
3 * Copyright (c) 2012-2013, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/kernel.h>
19 #include <linux/sched/signal.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/interrupt.h>
25 #include <linux/mei_cl_bus.h>
27 #include "mei_dev.h"
28 #include "client.h"
30 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
31 #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
33 /**
34 * __mei_cl_send - internal client send (write)
36 * @cl: host client
37 * @buf: buffer to send
38 * @length: buffer length
39 * @mode: sending mode
41 * Return: written size bytes or < 0 on error
43 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
44 unsigned int mode)
46 struct mei_device *bus;
47 struct mei_cl_cb *cb;
48 ssize_t rets;
50 if (WARN_ON(!cl || !cl->dev))
51 return -ENODEV;
53 bus = cl->dev;
55 mutex_lock(&bus->device_lock);
56 if (bus->dev_state != MEI_DEV_ENABLED) {
57 rets = -ENODEV;
58 goto out;
61 if (!mei_cl_is_connected(cl)) {
62 rets = -ENODEV;
63 goto out;
66 /* Check if we have an ME client device */
67 if (!mei_me_cl_is_active(cl->me_cl)) {
68 rets = -ENOTTY;
69 goto out;
72 if (length > mei_cl_mtu(cl)) {
73 rets = -EFBIG;
74 goto out;
77 while (cl->tx_cb_queued >= bus->tx_queue_limit) {
78 mutex_unlock(&bus->device_lock);
79 rets = wait_event_interruptible(cl->tx_wait,
80 cl->writing_state == MEI_WRITE_COMPLETE ||
81 (!mei_cl_is_connected(cl)));
82 mutex_lock(&bus->device_lock);
83 if (rets) {
84 if (signal_pending(current))
85 rets = -EINTR;
86 goto out;
88 if (!mei_cl_is_connected(cl)) {
89 rets = -ENODEV;
90 goto out;
94 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
95 if (!cb) {
96 rets = -ENOMEM;
97 goto out;
100 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
101 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
102 memcpy(cb->buf.data, buf, length);
104 rets = mei_cl_write(cl, cb);
106 out:
107 mutex_unlock(&bus->device_lock);
109 return rets;
113 * __mei_cl_recv - internal client receive (read)
115 * @cl: host client
116 * @buf: buffer to receive
117 * @length: buffer length
118 * @mode: io mode
120 * Return: read size in bytes of < 0 on error
122 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
123 unsigned int mode)
125 struct mei_device *bus;
126 struct mei_cl_cb *cb;
127 size_t r_length;
128 ssize_t rets;
129 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
131 if (WARN_ON(!cl || !cl->dev))
132 return -ENODEV;
134 bus = cl->dev;
136 mutex_lock(&bus->device_lock);
137 if (bus->dev_state != MEI_DEV_ENABLED) {
138 rets = -ENODEV;
139 goto out;
142 cb = mei_cl_read_cb(cl, NULL);
143 if (cb)
144 goto copy;
146 rets = mei_cl_read_start(cl, length, NULL);
147 if (rets && rets != -EBUSY)
148 goto out;
150 if (nonblock) {
151 rets = -EAGAIN;
152 goto out;
155 /* wait on event only if there is no other waiter */
156 /* synchronized under device mutex */
157 if (!waitqueue_active(&cl->rx_wait)) {
159 mutex_unlock(&bus->device_lock);
161 if (wait_event_interruptible(cl->rx_wait,
162 (!list_empty(&cl->rd_completed)) ||
163 (!mei_cl_is_connected(cl)))) {
165 if (signal_pending(current))
166 return -EINTR;
167 return -ERESTARTSYS;
170 mutex_lock(&bus->device_lock);
172 if (!mei_cl_is_connected(cl)) {
173 rets = -ENODEV;
174 goto out;
178 cb = mei_cl_read_cb(cl, NULL);
179 if (!cb) {
180 rets = 0;
181 goto out;
184 copy:
185 if (cb->status) {
186 rets = cb->status;
187 goto free;
190 r_length = min_t(size_t, length, cb->buf_idx);
191 memcpy(buf, cb->buf.data, r_length);
192 rets = r_length;
194 free:
195 mei_io_cb_free(cb);
196 out:
197 mutex_unlock(&bus->device_lock);
199 return rets;
203 * mei_cldev_send - me device send (write)
205 * @cldev: me client device
206 * @buf: buffer to send
207 * @length: buffer length
209 * Return: written size in bytes or < 0 on error
211 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
213 struct mei_cl *cl = cldev->cl;
215 return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING);
217 EXPORT_SYMBOL_GPL(mei_cldev_send);
220 * mei_cldev_recv_nonblock - non block client receive (read)
222 * @cldev: me client device
223 * @buf: buffer to receive
224 * @length: buffer length
226 * Return: read size in bytes of < 0 on error
227 * -EAGAIN if function will block.
229 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
230 size_t length)
232 struct mei_cl *cl = cldev->cl;
234 return __mei_cl_recv(cl, buf, length, MEI_CL_IO_RX_NONBLOCK);
236 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
239 * mei_cldev_recv - client receive (read)
241 * @cldev: me client device
242 * @buf: buffer to receive
243 * @length: buffer length
245 * Return: read size in bytes of < 0 on error
247 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
249 struct mei_cl *cl = cldev->cl;
251 return __mei_cl_recv(cl, buf, length, 0);
253 EXPORT_SYMBOL_GPL(mei_cldev_recv);
256 * mei_cl_bus_rx_work - dispatch rx event for a bus device
258 * @work: work
260 static void mei_cl_bus_rx_work(struct work_struct *work)
262 struct mei_cl_device *cldev;
263 struct mei_device *bus;
265 cldev = container_of(work, struct mei_cl_device, rx_work);
267 bus = cldev->bus;
269 if (cldev->rx_cb)
270 cldev->rx_cb(cldev);
272 mutex_lock(&bus->device_lock);
273 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
274 mutex_unlock(&bus->device_lock);
278 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
280 * @work: work
282 static void mei_cl_bus_notif_work(struct work_struct *work)
284 struct mei_cl_device *cldev;
286 cldev = container_of(work, struct mei_cl_device, notif_work);
288 if (cldev->notif_cb)
289 cldev->notif_cb(cldev);
293 * mei_cl_bus_notify_event - schedule notify cb on bus client
295 * @cl: host client
297 * Return: true if event was scheduled
298 * false if the client is not waiting for event
300 bool mei_cl_bus_notify_event(struct mei_cl *cl)
302 struct mei_cl_device *cldev = cl->cldev;
304 if (!cldev || !cldev->notif_cb)
305 return false;
307 if (!cl->notify_ev)
308 return false;
310 schedule_work(&cldev->notif_work);
312 cl->notify_ev = false;
314 return true;
318 * mei_cl_bus_rx_event - schedule rx event
320 * @cl: host client
322 * Return: true if event was scheduled
323 * false if the client is not waiting for event
325 bool mei_cl_bus_rx_event(struct mei_cl *cl)
327 struct mei_cl_device *cldev = cl->cldev;
329 if (!cldev || !cldev->rx_cb)
330 return false;
332 schedule_work(&cldev->rx_work);
334 return true;
338 * mei_cldev_register_rx_cb - register Rx event callback
340 * @cldev: me client devices
341 * @rx_cb: callback function
343 * Return: 0 on success
344 * -EALREADY if an callback is already registered
345 * <0 on other errors
347 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
349 struct mei_device *bus = cldev->bus;
350 int ret;
352 if (!rx_cb)
353 return -EINVAL;
354 if (cldev->rx_cb)
355 return -EALREADY;
357 cldev->rx_cb = rx_cb;
358 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
360 mutex_lock(&bus->device_lock);
361 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
362 mutex_unlock(&bus->device_lock);
363 if (ret && ret != -EBUSY)
364 return ret;
366 return 0;
368 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
371 * mei_cldev_register_notif_cb - register FW notification event callback
373 * @cldev: me client devices
374 * @notif_cb: callback function
376 * Return: 0 on success
377 * -EALREADY if an callback is already registered
378 * <0 on other errors
380 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
381 mei_cldev_cb_t notif_cb)
383 struct mei_device *bus = cldev->bus;
384 int ret;
386 if (!notif_cb)
387 return -EINVAL;
389 if (cldev->notif_cb)
390 return -EALREADY;
392 cldev->notif_cb = notif_cb;
393 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
395 mutex_lock(&bus->device_lock);
396 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
397 mutex_unlock(&bus->device_lock);
398 if (ret)
399 return ret;
401 return 0;
403 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
406 * mei_cldev_get_drvdata - driver data getter
408 * @cldev: mei client device
410 * Return: driver private data
412 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
414 return dev_get_drvdata(&cldev->dev);
416 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
419 * mei_cldev_set_drvdata - driver data setter
421 * @cldev: mei client device
422 * @data: data to store
424 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
426 dev_set_drvdata(&cldev->dev, data);
428 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
431 * mei_cldev_uuid - return uuid of the underlying me client
433 * @cldev: mei client device
435 * Return: me client uuid
437 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
439 return mei_me_cl_uuid(cldev->me_cl);
441 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
444 * mei_cldev_ver - return protocol version of the underlying me client
446 * @cldev: mei client device
448 * Return: me client protocol version
450 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
452 return mei_me_cl_ver(cldev->me_cl);
454 EXPORT_SYMBOL_GPL(mei_cldev_ver);
457 * mei_cldev_enabled - check whether the device is enabled
459 * @cldev: mei client device
461 * Return: true if me client is initialized and connected
463 bool mei_cldev_enabled(struct mei_cl_device *cldev)
465 return mei_cl_is_connected(cldev->cl);
467 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
470 * mei_cl_bus_module_get - acquire module of the underlying
471 * hw driver.
473 * @cldev: mei client device
475 * Return: true on success; false if the module was removed.
477 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
479 return try_module_get(cldev->bus->dev->driver->owner);
483 * mei_cl_bus_module_put - release the underlying hw module.
485 * @cldev: mei client device
487 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
489 module_put(cldev->bus->dev->driver->owner);
493 * mei_cldev_enable - enable me client device
494 * create connection with me client
496 * @cldev: me client device
498 * Return: 0 on success and < 0 on error
500 int mei_cldev_enable(struct mei_cl_device *cldev)
502 struct mei_device *bus = cldev->bus;
503 struct mei_cl *cl;
504 int ret;
506 cl = cldev->cl;
508 mutex_lock(&bus->device_lock);
509 if (cl->state == MEI_FILE_UNINITIALIZED) {
510 ret = mei_cl_link(cl);
511 if (ret)
512 goto out;
513 /* update pointers */
514 cl->cldev = cldev;
517 if (mei_cl_is_connected(cl)) {
518 ret = 0;
519 goto out;
522 if (!mei_me_cl_is_active(cldev->me_cl)) {
523 dev_err(&cldev->dev, "me client is not active\n");
524 ret = -ENOTTY;
525 goto out;
528 if (!mei_cl_bus_module_get(cldev)) {
529 dev_err(&cldev->dev, "get hw module failed");
530 ret = -ENODEV;
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");
537 mei_cl_bus_module_put(cldev);
540 out:
541 mutex_unlock(&bus->device_lock);
543 return ret;
545 EXPORT_SYMBOL_GPL(mei_cldev_enable);
548 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
549 * callbacks.
551 * @cldev: client device
553 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
555 if (cldev->rx_cb) {
556 cancel_work_sync(&cldev->rx_work);
557 cldev->rx_cb = NULL;
560 if (cldev->notif_cb) {
561 cancel_work_sync(&cldev->notif_work);
562 cldev->notif_cb = NULL;
567 * mei_cldev_disable - disable me client device
568 * disconnect form the me client
570 * @cldev: me client device
572 * Return: 0 on success and < 0 on error
574 int mei_cldev_disable(struct mei_cl_device *cldev)
576 struct mei_device *bus;
577 struct mei_cl *cl;
578 int err;
580 if (!cldev)
581 return -ENODEV;
583 cl = cldev->cl;
585 bus = cldev->bus;
587 mei_cldev_unregister_callbacks(cldev);
589 mutex_lock(&bus->device_lock);
591 if (!mei_cl_is_connected(cl)) {
592 dev_dbg(bus->dev, "Already disconnected\n");
593 err = 0;
594 goto out;
597 err = mei_cl_disconnect(cl);
598 if (err < 0)
599 dev_err(bus->dev, "Could not disconnect from the ME client\n");
601 mei_cl_bus_module_put(cldev);
602 out:
603 /* Flush queues and remove any pending read */
604 mei_cl_flush_queues(cl, NULL);
605 mei_cl_unlink(cl);
607 mutex_unlock(&bus->device_lock);
608 return err;
610 EXPORT_SYMBOL_GPL(mei_cldev_disable);
613 * mei_cl_device_find - find matching entry in the driver id table
615 * @cldev: me client device
616 * @cldrv: me client driver
618 * Return: id on success; NULL if no id is matching
620 static const
621 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
622 struct mei_cl_driver *cldrv)
624 const struct mei_cl_device_id *id;
625 const uuid_le *uuid;
626 u8 version;
627 bool match;
629 uuid = mei_me_cl_uuid(cldev->me_cl);
630 version = mei_me_cl_ver(cldev->me_cl);
632 id = cldrv->id_table;
633 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
634 if (!uuid_le_cmp(*uuid, id->uuid)) {
635 match = true;
637 if (cldev->name[0])
638 if (strncmp(cldev->name, id->name,
639 sizeof(id->name)))
640 match = false;
642 if (id->version != MEI_CL_VERSION_ANY)
643 if (id->version != version)
644 match = false;
645 if (match)
646 return id;
649 id++;
652 return NULL;
656 * mei_cl_device_match - device match function
658 * @dev: device
659 * @drv: driver
661 * Return: 1 if matching device was found 0 otherwise
663 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
665 struct mei_cl_device *cldev = to_mei_cl_device(dev);
666 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
667 const struct mei_cl_device_id *found_id;
669 if (!cldev)
670 return 0;
672 if (!cldev->do_match)
673 return 0;
675 if (!cldrv || !cldrv->id_table)
676 return 0;
678 found_id = mei_cl_device_find(cldev, cldrv);
679 if (found_id)
680 return 1;
682 return 0;
686 * mei_cl_device_probe - bus probe function
688 * @dev: device
690 * Return: 0 on success; < 0 otherwise
692 static int mei_cl_device_probe(struct device *dev)
694 struct mei_cl_device *cldev;
695 struct mei_cl_driver *cldrv;
696 const struct mei_cl_device_id *id;
697 int ret;
699 cldev = to_mei_cl_device(dev);
700 cldrv = to_mei_cl_driver(dev->driver);
702 if (!cldev)
703 return 0;
705 if (!cldrv || !cldrv->probe)
706 return -ENODEV;
708 id = mei_cl_device_find(cldev, cldrv);
709 if (!id)
710 return -ENODEV;
712 ret = cldrv->probe(cldev, id);
713 if (ret)
714 return ret;
716 __module_get(THIS_MODULE);
717 return 0;
721 * mei_cl_device_remove - remove device from the bus
723 * @dev: device
725 * Return: 0 on success; < 0 otherwise
727 static int mei_cl_device_remove(struct device *dev)
729 struct mei_cl_device *cldev = to_mei_cl_device(dev);
730 struct mei_cl_driver *cldrv;
731 int ret = 0;
733 if (!cldev || !dev->driver)
734 return 0;
736 cldrv = to_mei_cl_driver(dev->driver);
737 if (cldrv->remove)
738 ret = cldrv->remove(cldev);
740 mei_cldev_unregister_callbacks(cldev);
742 module_put(THIS_MODULE);
743 dev->driver = NULL;
744 return ret;
748 static ssize_t name_show(struct device *dev, struct device_attribute *a,
749 char *buf)
751 struct mei_cl_device *cldev = to_mei_cl_device(dev);
753 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
755 static DEVICE_ATTR_RO(name);
757 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
758 char *buf)
760 struct mei_cl_device *cldev = to_mei_cl_device(dev);
761 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
763 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
765 static DEVICE_ATTR_RO(uuid);
767 static ssize_t version_show(struct device *dev, struct device_attribute *a,
768 char *buf)
770 struct mei_cl_device *cldev = to_mei_cl_device(dev);
771 u8 version = mei_me_cl_ver(cldev->me_cl);
773 return scnprintf(buf, PAGE_SIZE, "%02X", version);
775 static DEVICE_ATTR_RO(version);
777 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
778 char *buf)
780 struct mei_cl_device *cldev = to_mei_cl_device(dev);
781 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
782 u8 version = mei_me_cl_ver(cldev->me_cl);
784 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
785 cldev->name, uuid, version);
787 static DEVICE_ATTR_RO(modalias);
789 static struct attribute *mei_cldev_attrs[] = {
790 &dev_attr_name.attr,
791 &dev_attr_uuid.attr,
792 &dev_attr_version.attr,
793 &dev_attr_modalias.attr,
794 NULL,
796 ATTRIBUTE_GROUPS(mei_cldev);
799 * mei_cl_device_uevent - me client bus uevent handler
801 * @dev: device
802 * @env: uevent kobject
804 * Return: 0 on success -ENOMEM on when add_uevent_var fails
806 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
808 struct mei_cl_device *cldev = to_mei_cl_device(dev);
809 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
810 u8 version = mei_me_cl_ver(cldev->me_cl);
812 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
813 return -ENOMEM;
815 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
816 return -ENOMEM;
818 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
819 return -ENOMEM;
821 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
822 cldev->name, uuid, version))
823 return -ENOMEM;
825 return 0;
828 static struct bus_type mei_cl_bus_type = {
829 .name = "mei",
830 .dev_groups = mei_cldev_groups,
831 .match = mei_cl_device_match,
832 .probe = mei_cl_device_probe,
833 .remove = mei_cl_device_remove,
834 .uevent = mei_cl_device_uevent,
837 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
839 if (bus)
840 get_device(bus->dev);
842 return bus;
845 static void mei_dev_bus_put(struct mei_device *bus)
847 if (bus)
848 put_device(bus->dev);
851 static void mei_cl_bus_dev_release(struct device *dev)
853 struct mei_cl_device *cldev = to_mei_cl_device(dev);
855 if (!cldev)
856 return;
858 mei_me_cl_put(cldev->me_cl);
859 mei_dev_bus_put(cldev->bus);
860 mei_cl_unlink(cldev->cl);
861 kfree(cldev->cl);
862 kfree(cldev);
865 static const struct device_type mei_cl_device_type = {
866 .release = mei_cl_bus_dev_release,
870 * mei_cl_bus_set_name - set device name for me client device
872 * @cldev: me client device
874 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
876 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
877 cldev->name,
878 mei_me_cl_uuid(cldev->me_cl),
879 mei_me_cl_ver(cldev->me_cl));
883 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
885 * @bus: mei device
886 * @me_cl: me client
888 * Return: allocated device structur or NULL on allocation failure
890 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
891 struct mei_me_client *me_cl)
893 struct mei_cl_device *cldev;
894 struct mei_cl *cl;
896 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
897 if (!cldev)
898 return NULL;
900 cl = mei_cl_allocate(bus);
901 if (!cl) {
902 kfree(cldev);
903 return NULL;
906 device_initialize(&cldev->dev);
907 cldev->dev.parent = bus->dev;
908 cldev->dev.bus = &mei_cl_bus_type;
909 cldev->dev.type = &mei_cl_device_type;
910 cldev->bus = mei_dev_bus_get(bus);
911 cldev->me_cl = mei_me_cl_get(me_cl);
912 cldev->cl = cl;
913 mei_cl_bus_set_name(cldev);
914 cldev->is_added = 0;
915 INIT_LIST_HEAD(&cldev->bus_list);
917 return cldev;
921 * mei_cl_dev_setup - setup me client device
922 * run fix up routines and set the device name
924 * @bus: mei device
925 * @cldev: me client device
927 * Return: true if the device is eligible for enumeration
929 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
930 struct mei_cl_device *cldev)
932 cldev->do_match = 1;
933 mei_cl_bus_dev_fixup(cldev);
935 /* the device name can change during fix up */
936 if (cldev->do_match)
937 mei_cl_bus_set_name(cldev);
939 return cldev->do_match == 1;
943 * mei_cl_bus_dev_add - add me client devices
945 * @cldev: me client device
947 * Return: 0 on success; < 0 on failre
949 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
951 int ret;
953 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
954 mei_me_cl_uuid(cldev->me_cl),
955 mei_me_cl_ver(cldev->me_cl));
956 ret = device_add(&cldev->dev);
957 if (!ret)
958 cldev->is_added = 1;
960 return ret;
964 * mei_cl_bus_dev_stop - stop the driver
966 * @cldev: me client device
968 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
970 if (cldev->is_added)
971 device_release_driver(&cldev->dev);
975 * mei_cl_bus_dev_destroy - destroy me client devices object
977 * @cldev: me client device
979 * Locking: called under "dev->cl_bus_lock" lock
981 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
984 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
986 if (!cldev->is_added)
987 return;
989 device_del(&cldev->dev);
991 list_del_init(&cldev->bus_list);
993 cldev->is_added = 0;
994 put_device(&cldev->dev);
998 * mei_cl_bus_remove_device - remove a devices form the bus
1000 * @cldev: me client device
1002 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1004 mei_cl_bus_dev_stop(cldev);
1005 mei_cl_bus_dev_destroy(cldev);
1009 * mei_cl_bus_remove_devices - remove all devices form the bus
1011 * @bus: mei device
1013 void mei_cl_bus_remove_devices(struct mei_device *bus)
1015 struct mei_cl_device *cldev, *next;
1017 mutex_lock(&bus->cl_bus_lock);
1018 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1019 mei_cl_bus_remove_device(cldev);
1020 mutex_unlock(&bus->cl_bus_lock);
1025 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1026 * based on me client
1028 * @bus: mei device
1029 * @me_cl: me client
1031 * Locking: called under "dev->cl_bus_lock" lock
1033 static void mei_cl_bus_dev_init(struct mei_device *bus,
1034 struct mei_me_client *me_cl)
1036 struct mei_cl_device *cldev;
1038 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1040 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1042 if (me_cl->bus_added)
1043 return;
1045 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1046 if (!cldev)
1047 return;
1049 me_cl->bus_added = true;
1050 list_add_tail(&cldev->bus_list, &bus->device_list);
1055 * mei_cl_bus_rescan - scan me clients list and add create
1056 * devices for eligible clients
1058 * @bus: mei device
1060 static void mei_cl_bus_rescan(struct mei_device *bus)
1062 struct mei_cl_device *cldev, *n;
1063 struct mei_me_client *me_cl;
1065 mutex_lock(&bus->cl_bus_lock);
1067 down_read(&bus->me_clients_rwsem);
1068 list_for_each_entry(me_cl, &bus->me_clients, list)
1069 mei_cl_bus_dev_init(bus, me_cl);
1070 up_read(&bus->me_clients_rwsem);
1072 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1074 if (!mei_me_cl_is_active(cldev->me_cl)) {
1075 mei_cl_bus_remove_device(cldev);
1076 continue;
1079 if (cldev->is_added)
1080 continue;
1082 if (mei_cl_bus_dev_setup(bus, cldev))
1083 mei_cl_bus_dev_add(cldev);
1084 else {
1085 list_del_init(&cldev->bus_list);
1086 put_device(&cldev->dev);
1089 mutex_unlock(&bus->cl_bus_lock);
1091 dev_dbg(bus->dev, "rescan end");
1094 void mei_cl_bus_rescan_work(struct work_struct *work)
1096 struct mei_device *bus =
1097 container_of(work, struct mei_device, bus_rescan_work);
1099 mei_cl_bus_rescan(bus);
1102 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1103 struct module *owner)
1105 int err;
1107 cldrv->driver.name = cldrv->name;
1108 cldrv->driver.owner = owner;
1109 cldrv->driver.bus = &mei_cl_bus_type;
1111 err = driver_register(&cldrv->driver);
1112 if (err)
1113 return err;
1115 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1117 return 0;
1119 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1121 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1123 driver_unregister(&cldrv->driver);
1125 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1127 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1130 int __init mei_cl_bus_init(void)
1132 return bus_register(&mei_cl_bus_type);
1135 void __exit mei_cl_bus_exit(void)
1137 bus_unregister(&mei_cl_bus_type);