of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / misc / mei / bus.c
blob0b05aa9387996484759e748831eba5304ac3a409
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.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 * @blocking: wait for write completion
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 bool blocking)
46 struct mei_device *bus;
47 struct mei_cl_cb *cb = NULL;
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 (!mei_cl_is_connected(cl)) {
57 rets = -ENODEV;
58 goto out;
61 /* Check if we have an ME client device */
62 if (!mei_me_cl_is_active(cl->me_cl)) {
63 rets = -ENOTTY;
64 goto out;
67 if (length > mei_cl_mtu(cl)) {
68 rets = -EFBIG;
69 goto out;
72 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
73 if (!cb) {
74 rets = -ENOMEM;
75 goto out;
78 memcpy(cb->buf.data, buf, length);
80 rets = mei_cl_write(cl, cb, blocking);
82 out:
83 mutex_unlock(&bus->device_lock);
84 if (rets < 0)
85 mei_io_cb_free(cb);
87 return rets;
90 /**
91 * __mei_cl_recv - internal client receive (read)
93 * @cl: host client
94 * @buf: buffer to receive
95 * @length: buffer length
97 * Return: read size in bytes of < 0 on error
99 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
101 struct mei_device *bus;
102 struct mei_cl_cb *cb;
103 size_t r_length;
104 ssize_t rets;
106 if (WARN_ON(!cl || !cl->dev))
107 return -ENODEV;
109 bus = cl->dev;
111 mutex_lock(&bus->device_lock);
113 cb = mei_cl_read_cb(cl, NULL);
114 if (cb)
115 goto copy;
117 rets = mei_cl_read_start(cl, length, NULL);
118 if (rets && rets != -EBUSY)
119 goto out;
121 /* wait on event only if there is no other waiter */
122 if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
124 mutex_unlock(&bus->device_lock);
126 if (wait_event_interruptible(cl->rx_wait,
127 (!list_empty(&cl->rd_completed)) ||
128 (!mei_cl_is_connected(cl)))) {
130 if (signal_pending(current))
131 return -EINTR;
132 return -ERESTARTSYS;
135 mutex_lock(&bus->device_lock);
137 if (!mei_cl_is_connected(cl)) {
138 rets = -EBUSY;
139 goto out;
143 cb = mei_cl_read_cb(cl, NULL);
144 if (!cb) {
145 rets = 0;
146 goto out;
149 copy:
150 if (cb->status) {
151 rets = cb->status;
152 goto free;
155 r_length = min_t(size_t, length, cb->buf_idx);
156 memcpy(buf, cb->buf.data, r_length);
157 rets = r_length;
159 free:
160 mei_io_cb_free(cb);
161 out:
162 mutex_unlock(&bus->device_lock);
164 return rets;
168 * mei_cldev_send - me device send (write)
170 * @cldev: me client device
171 * @buf: buffer to send
172 * @length: buffer length
174 * Return: written size in bytes or < 0 on error
176 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
178 struct mei_cl *cl = cldev->cl;
180 if (cl == NULL)
181 return -ENODEV;
183 return __mei_cl_send(cl, buf, length, 1);
185 EXPORT_SYMBOL_GPL(mei_cldev_send);
188 * mei_cldev_recv - client receive (read)
190 * @cldev: me client device
191 * @buf: buffer to receive
192 * @length: buffer length
194 * Return: read size in bytes of < 0 on error
196 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
198 struct mei_cl *cl = cldev->cl;
200 if (cl == NULL)
201 return -ENODEV;
203 return __mei_cl_recv(cl, buf, length);
205 EXPORT_SYMBOL_GPL(mei_cldev_recv);
208 * mei_cl_bus_event_work - dispatch rx event for a bus device
209 * and schedule new work
211 * @work: work
213 static void mei_cl_bus_event_work(struct work_struct *work)
215 struct mei_cl_device *cldev;
217 cldev = container_of(work, struct mei_cl_device, event_work);
219 if (cldev->event_cb)
220 cldev->event_cb(cldev, cldev->events, cldev->event_context);
222 cldev->events = 0;
224 /* Prepare for the next read */
225 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX))
226 mei_cl_read_start(cldev->cl, 0, NULL);
230 * mei_cl_bus_notify_event - schedule notify cb on bus client
232 * @cl: host client
234 void mei_cl_bus_notify_event(struct mei_cl *cl)
236 struct mei_cl_device *cldev = cl->cldev;
238 if (!cldev || !cldev->event_cb)
239 return;
241 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)))
242 return;
244 if (!cl->notify_ev)
245 return;
247 set_bit(MEI_CL_EVENT_NOTIF, &cldev->events);
249 schedule_work(&cldev->event_work);
251 cl->notify_ev = false;
255 * mei_cl_bus_rx_event - schedule rx evenet
257 * @cl: host client
259 void mei_cl_bus_rx_event(struct mei_cl *cl)
261 struct mei_cl_device *cldev = cl->cldev;
263 if (!cldev || !cldev->event_cb)
264 return;
266 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX)))
267 return;
269 set_bit(MEI_CL_EVENT_RX, &cldev->events);
271 schedule_work(&cldev->event_work);
275 * mei_cldev_register_event_cb - register event callback
277 * @cldev: me client devices
278 * @event_cb: callback function
279 * @events_mask: requested events bitmask
280 * @context: driver context data
282 * Return: 0 on success
283 * -EALREADY if an callback is already registered
284 * <0 on other errors
286 int mei_cldev_register_event_cb(struct mei_cl_device *cldev,
287 unsigned long events_mask,
288 mei_cldev_event_cb_t event_cb, void *context)
290 int ret;
292 if (cldev->event_cb)
293 return -EALREADY;
295 cldev->events = 0;
296 cldev->events_mask = events_mask;
297 cldev->event_cb = event_cb;
298 cldev->event_context = context;
299 INIT_WORK(&cldev->event_work, mei_cl_bus_event_work);
301 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
302 ret = mei_cl_read_start(cldev->cl, 0, NULL);
303 if (ret && ret != -EBUSY)
304 return ret;
307 if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) {
308 mutex_lock(&cldev->cl->dev->device_lock);
309 ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0);
310 mutex_unlock(&cldev->cl->dev->device_lock);
311 if (ret)
312 return ret;
315 return 0;
317 EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb);
320 * mei_cldev_get_drvdata - driver data getter
322 * @cldev: mei client device
324 * Return: driver private data
326 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
328 return dev_get_drvdata(&cldev->dev);
330 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
333 * mei_cldev_set_drvdata - driver data setter
335 * @cldev: mei client device
336 * @data: data to store
338 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
340 dev_set_drvdata(&cldev->dev, data);
342 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
345 * mei_cldev_uuid - return uuid of the underlying me client
347 * @cldev: mei client device
349 * Return: me client uuid
351 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
353 return mei_me_cl_uuid(cldev->me_cl);
355 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
358 * mei_cldev_ver - return protocol version of the underlying me client
360 * @cldev: mei client device
362 * Return: me client protocol version
364 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
366 return mei_me_cl_ver(cldev->me_cl);
368 EXPORT_SYMBOL_GPL(mei_cldev_ver);
371 * mei_cldev_enabled - check whether the device is enabled
373 * @cldev: mei client device
375 * Return: true if me client is initialized and connected
377 bool mei_cldev_enabled(struct mei_cl_device *cldev)
379 return cldev->cl && mei_cl_is_connected(cldev->cl);
381 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
384 * mei_cldev_enable_device - enable me client device
385 * create connection with me client
387 * @cldev: me client device
389 * Return: 0 on success and < 0 on error
391 int mei_cldev_enable(struct mei_cl_device *cldev)
393 struct mei_device *bus = cldev->bus;
394 struct mei_cl *cl;
395 int ret;
397 cl = cldev->cl;
399 if (!cl) {
400 mutex_lock(&bus->device_lock);
401 cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY);
402 mutex_unlock(&bus->device_lock);
403 if (IS_ERR(cl))
404 return PTR_ERR(cl);
405 /* update pointers */
406 cldev->cl = cl;
407 cl->cldev = cldev;
410 mutex_lock(&bus->device_lock);
411 if (mei_cl_is_connected(cl)) {
412 ret = 0;
413 goto out;
416 if (!mei_me_cl_is_active(cldev->me_cl)) {
417 dev_err(&cldev->dev, "me client is not active\n");
418 ret = -ENOTTY;
419 goto out;
422 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
423 if (ret < 0)
424 dev_err(&cldev->dev, "cannot connect\n");
426 out:
427 mutex_unlock(&bus->device_lock);
429 return ret;
431 EXPORT_SYMBOL_GPL(mei_cldev_enable);
434 * mei_cldev_disable - disable me client device
435 * disconnect form the me client
437 * @cldev: me client device
439 * Return: 0 on success and < 0 on error
441 int mei_cldev_disable(struct mei_cl_device *cldev)
443 struct mei_device *bus;
444 struct mei_cl *cl;
445 int err;
447 if (!cldev || !cldev->cl)
448 return -ENODEV;
450 cl = cldev->cl;
452 bus = cldev->bus;
454 cldev->event_cb = NULL;
456 mutex_lock(&bus->device_lock);
458 if (!mei_cl_is_connected(cl)) {
459 dev_err(bus->dev, "Already disconnected");
460 err = 0;
461 goto out;
464 err = mei_cl_disconnect(cl);
465 if (err < 0)
466 dev_err(bus->dev, "Could not disconnect from the ME client");
468 out:
469 /* Flush queues and remove any pending read */
470 mei_cl_flush_queues(cl, NULL);
471 mei_cl_unlink(cl);
473 kfree(cl);
474 cldev->cl = NULL;
476 mutex_unlock(&bus->device_lock);
477 return err;
479 EXPORT_SYMBOL_GPL(mei_cldev_disable);
482 * mei_cl_device_find - find matching entry in the driver id table
484 * @cldev: me client device
485 * @cldrv: me client driver
487 * Return: id on success; NULL if no id is matching
489 static const
490 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
491 struct mei_cl_driver *cldrv)
493 const struct mei_cl_device_id *id;
494 const uuid_le *uuid;
495 u8 version;
496 bool match;
498 uuid = mei_me_cl_uuid(cldev->me_cl);
499 version = mei_me_cl_ver(cldev->me_cl);
501 id = cldrv->id_table;
502 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
503 if (!uuid_le_cmp(*uuid, id->uuid)) {
504 match = true;
506 if (cldev->name[0])
507 if (strncmp(cldev->name, id->name,
508 sizeof(id->name)))
509 match = false;
511 if (id->version != MEI_CL_VERSION_ANY)
512 if (id->version != version)
513 match = false;
514 if (match)
515 return id;
518 id++;
521 return NULL;
525 * mei_cl_device_match - device match function
527 * @dev: device
528 * @drv: driver
530 * Return: 1 if matching device was found 0 otherwise
532 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
534 struct mei_cl_device *cldev = to_mei_cl_device(dev);
535 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
536 const struct mei_cl_device_id *found_id;
538 if (!cldev)
539 return 0;
541 if (!cldev->do_match)
542 return 0;
544 if (!cldrv || !cldrv->id_table)
545 return 0;
547 found_id = mei_cl_device_find(cldev, cldrv);
548 if (found_id)
549 return 1;
551 return 0;
555 * mei_cl_device_probe - bus probe function
557 * @dev: device
559 * Return: 0 on success; < 0 otherwise
561 static int mei_cl_device_probe(struct device *dev)
563 struct mei_cl_device *cldev;
564 struct mei_cl_driver *cldrv;
565 const struct mei_cl_device_id *id;
567 cldev = to_mei_cl_device(dev);
568 cldrv = to_mei_cl_driver(dev->driver);
570 if (!cldev)
571 return 0;
573 if (!cldrv || !cldrv->probe)
574 return -ENODEV;
576 id = mei_cl_device_find(cldev, cldrv);
577 if (!id)
578 return -ENODEV;
580 __module_get(THIS_MODULE);
582 return cldrv->probe(cldev, id);
586 * mei_cl_device_remove - remove device from the bus
588 * @dev: device
590 * Return: 0 on success; < 0 otherwise
592 static int mei_cl_device_remove(struct device *dev)
594 struct mei_cl_device *cldev = to_mei_cl_device(dev);
595 struct mei_cl_driver *cldrv;
596 int ret = 0;
598 if (!cldev || !dev->driver)
599 return 0;
601 if (cldev->event_cb) {
602 cldev->event_cb = NULL;
603 cancel_work_sync(&cldev->event_work);
606 cldrv = to_mei_cl_driver(dev->driver);
607 if (cldrv->remove)
608 ret = cldrv->remove(cldev);
610 module_put(THIS_MODULE);
611 dev->driver = NULL;
612 return ret;
616 static ssize_t name_show(struct device *dev, struct device_attribute *a,
617 char *buf)
619 struct mei_cl_device *cldev = to_mei_cl_device(dev);
620 size_t len;
622 len = snprintf(buf, PAGE_SIZE, "%s", cldev->name);
624 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
626 static DEVICE_ATTR_RO(name);
628 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
629 char *buf)
631 struct mei_cl_device *cldev = to_mei_cl_device(dev);
632 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
633 size_t len;
635 len = snprintf(buf, PAGE_SIZE, "%pUl", uuid);
637 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
639 static DEVICE_ATTR_RO(uuid);
641 static ssize_t version_show(struct device *dev, struct device_attribute *a,
642 char *buf)
644 struct mei_cl_device *cldev = to_mei_cl_device(dev);
645 u8 version = mei_me_cl_ver(cldev->me_cl);
646 size_t len;
648 len = snprintf(buf, PAGE_SIZE, "%02X", version);
650 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
652 static DEVICE_ATTR_RO(version);
654 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
655 char *buf)
657 struct mei_cl_device *cldev = to_mei_cl_device(dev);
658 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
659 size_t len;
661 len = snprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
662 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
664 static DEVICE_ATTR_RO(modalias);
666 static struct attribute *mei_cldev_attrs[] = {
667 &dev_attr_name.attr,
668 &dev_attr_uuid.attr,
669 &dev_attr_version.attr,
670 &dev_attr_modalias.attr,
671 NULL,
673 ATTRIBUTE_GROUPS(mei_cldev);
676 * mei_cl_device_uevent - me client bus uevent handler
678 * @dev: device
679 * @env: uevent kobject
681 * Return: 0 on success -ENOMEM on when add_uevent_var fails
683 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
685 struct mei_cl_device *cldev = to_mei_cl_device(dev);
686 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
687 u8 version = mei_me_cl_ver(cldev->me_cl);
689 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
690 return -ENOMEM;
692 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
693 return -ENOMEM;
695 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
696 return -ENOMEM;
698 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
699 cldev->name, uuid, version))
700 return -ENOMEM;
702 return 0;
705 static struct bus_type mei_cl_bus_type = {
706 .name = "mei",
707 .dev_groups = mei_cldev_groups,
708 .match = mei_cl_device_match,
709 .probe = mei_cl_device_probe,
710 .remove = mei_cl_device_remove,
711 .uevent = mei_cl_device_uevent,
714 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
716 if (bus)
717 get_device(bus->dev);
719 return bus;
722 static void mei_dev_bus_put(struct mei_device *bus)
724 if (bus)
725 put_device(bus->dev);
728 static void mei_cl_bus_dev_release(struct device *dev)
730 struct mei_cl_device *cldev = to_mei_cl_device(dev);
732 if (!cldev)
733 return;
735 mei_me_cl_put(cldev->me_cl);
736 mei_dev_bus_put(cldev->bus);
737 kfree(cldev);
740 static struct device_type mei_cl_device_type = {
741 .release = mei_cl_bus_dev_release,
745 * mei_cl_bus_set_name - set device name for me client device
747 * @cldev: me client device
749 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
751 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
752 cldev->name,
753 mei_me_cl_uuid(cldev->me_cl),
754 mei_me_cl_ver(cldev->me_cl));
758 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
760 * @bus: mei device
761 * @me_cl: me client
763 * Return: allocated device structur or NULL on allocation failure
765 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
766 struct mei_me_client *me_cl)
768 struct mei_cl_device *cldev;
770 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
771 if (!cldev)
772 return NULL;
774 device_initialize(&cldev->dev);
775 cldev->dev.parent = bus->dev;
776 cldev->dev.bus = &mei_cl_bus_type;
777 cldev->dev.type = &mei_cl_device_type;
778 cldev->bus = mei_dev_bus_get(bus);
779 cldev->me_cl = mei_me_cl_get(me_cl);
780 mei_cl_bus_set_name(cldev);
781 cldev->is_added = 0;
782 INIT_LIST_HEAD(&cldev->bus_list);
784 return cldev;
788 * mei_cl_dev_setup - setup me client device
789 * run fix up routines and set the device name
791 * @bus: mei device
792 * @cldev: me client device
794 * Return: true if the device is eligible for enumeration
796 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
797 struct mei_cl_device *cldev)
799 cldev->do_match = 1;
800 mei_cl_bus_dev_fixup(cldev);
802 /* the device name can change during fix up */
803 if (cldev->do_match)
804 mei_cl_bus_set_name(cldev);
806 return cldev->do_match == 1;
810 * mei_cl_bus_dev_add - add me client devices
812 * @cldev: me client device
814 * Return: 0 on success; < 0 on failre
816 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
818 int ret;
820 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
821 mei_me_cl_uuid(cldev->me_cl),
822 mei_me_cl_ver(cldev->me_cl));
823 ret = device_add(&cldev->dev);
824 if (!ret)
825 cldev->is_added = 1;
827 return ret;
831 * mei_cl_bus_dev_stop - stop the driver
833 * @cldev: me client device
835 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
837 if (cldev->is_added)
838 device_release_driver(&cldev->dev);
842 * mei_cl_bus_dev_destroy - destroy me client devices object
844 * @cldev: me client device
846 * Locking: called under "dev->cl_bus_lock" lock
848 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
851 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
853 if (!cldev->is_added)
854 return;
856 device_del(&cldev->dev);
858 list_del_init(&cldev->bus_list);
860 cldev->is_added = 0;
861 put_device(&cldev->dev);
865 * mei_cl_bus_remove_device - remove a devices form the bus
867 * @cldev: me client device
869 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
871 mei_cl_bus_dev_stop(cldev);
872 mei_cl_bus_dev_destroy(cldev);
876 * mei_cl_bus_remove_devices - remove all devices form the bus
878 * @bus: mei device
880 void mei_cl_bus_remove_devices(struct mei_device *bus)
882 struct mei_cl_device *cldev, *next;
884 mutex_lock(&bus->cl_bus_lock);
885 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
886 mei_cl_bus_remove_device(cldev);
887 mutex_unlock(&bus->cl_bus_lock);
892 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
893 * based on me client
895 * @bus: mei device
896 * @me_cl: me client
898 * Locking: called under "dev->cl_bus_lock" lock
900 static void mei_cl_bus_dev_init(struct mei_device *bus,
901 struct mei_me_client *me_cl)
903 struct mei_cl_device *cldev;
905 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
907 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
909 if (me_cl->bus_added)
910 return;
912 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
913 if (!cldev)
914 return;
916 me_cl->bus_added = true;
917 list_add_tail(&cldev->bus_list, &bus->device_list);
922 * mei_cl_bus_rescan - scan me clients list and add create
923 * devices for eligible clients
925 * @bus: mei device
927 void mei_cl_bus_rescan(struct mei_device *bus)
929 struct mei_cl_device *cldev, *n;
930 struct mei_me_client *me_cl;
932 mutex_lock(&bus->cl_bus_lock);
934 down_read(&bus->me_clients_rwsem);
935 list_for_each_entry(me_cl, &bus->me_clients, list)
936 mei_cl_bus_dev_init(bus, me_cl);
937 up_read(&bus->me_clients_rwsem);
939 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
941 if (!mei_me_cl_is_active(cldev->me_cl)) {
942 mei_cl_bus_remove_device(cldev);
943 continue;
946 if (cldev->is_added)
947 continue;
949 if (mei_cl_bus_dev_setup(bus, cldev))
950 mei_cl_bus_dev_add(cldev);
951 else {
952 list_del_init(&cldev->bus_list);
953 put_device(&cldev->dev);
956 mutex_unlock(&bus->cl_bus_lock);
958 dev_dbg(bus->dev, "rescan end");
961 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
962 struct module *owner)
964 int err;
966 cldrv->driver.name = cldrv->name;
967 cldrv->driver.owner = owner;
968 cldrv->driver.bus = &mei_cl_bus_type;
970 err = driver_register(&cldrv->driver);
971 if (err)
972 return err;
974 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
976 return 0;
978 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
980 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
982 driver_unregister(&cldrv->driver);
984 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
986 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
989 int __init mei_cl_bus_init(void)
991 return bus_register(&mei_cl_bus_type);
994 void __exit mei_cl_bus_exit(void)
996 bus_unregister(&mei_cl_bus_type);