mei: bus: split RX and async notification callbacks
[linux-2.6/btrfs-unstable.git] / drivers / misc / mei / bus.c
blob2fd254ecde2fa7cdf3af24871fa3b015fe3afcff
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 * @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 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
78 if (!cb) {
79 rets = -ENOMEM;
80 goto out;
83 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
84 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
85 memcpy(cb->buf.data, buf, length);
87 rets = mei_cl_write(cl, cb);
89 out:
90 mutex_unlock(&bus->device_lock);
92 return rets;
95 /**
96 * __mei_cl_recv - internal client receive (read)
98 * @cl: host client
99 * @buf: buffer to receive
100 * @length: buffer length
102 * Return: read size in bytes of < 0 on error
104 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
106 struct mei_device *bus;
107 struct mei_cl_cb *cb;
108 size_t r_length;
109 ssize_t rets;
111 if (WARN_ON(!cl || !cl->dev))
112 return -ENODEV;
114 bus = cl->dev;
116 mutex_lock(&bus->device_lock);
117 if (bus->dev_state != MEI_DEV_ENABLED) {
118 rets = -ENODEV;
119 goto out;
122 cb = mei_cl_read_cb(cl, NULL);
123 if (cb)
124 goto copy;
126 rets = mei_cl_read_start(cl, length, NULL);
127 if (rets && rets != -EBUSY)
128 goto out;
130 /* wait on event only if there is no other waiter */
131 /* synchronized under device mutex */
132 if (!waitqueue_active(&cl->rx_wait)) {
134 mutex_unlock(&bus->device_lock);
136 if (wait_event_interruptible(cl->rx_wait,
137 (!list_empty(&cl->rd_completed)) ||
138 (!mei_cl_is_connected(cl)))) {
140 if (signal_pending(current))
141 return -EINTR;
142 return -ERESTARTSYS;
145 mutex_lock(&bus->device_lock);
147 if (!mei_cl_is_connected(cl)) {
148 rets = -ENODEV;
149 goto out;
153 cb = mei_cl_read_cb(cl, NULL);
154 if (!cb) {
155 rets = 0;
156 goto out;
159 copy:
160 if (cb->status) {
161 rets = cb->status;
162 goto free;
165 r_length = min_t(size_t, length, cb->buf_idx);
166 memcpy(buf, cb->buf.data, r_length);
167 rets = r_length;
169 free:
170 mei_io_cb_free(cb);
171 out:
172 mutex_unlock(&bus->device_lock);
174 return rets;
178 * mei_cldev_send - me device send (write)
180 * @cldev: me client device
181 * @buf: buffer to send
182 * @length: buffer length
184 * Return: written size in bytes or < 0 on error
186 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
188 struct mei_cl *cl = cldev->cl;
190 return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING);
192 EXPORT_SYMBOL_GPL(mei_cldev_send);
195 * mei_cldev_recv - client receive (read)
197 * @cldev: me client device
198 * @buf: buffer to receive
199 * @length: buffer length
201 * Return: read size in bytes of < 0 on error
203 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
205 struct mei_cl *cl = cldev->cl;
207 return __mei_cl_recv(cl, buf, length);
209 EXPORT_SYMBOL_GPL(mei_cldev_recv);
212 * mei_cl_bus_rx_work - dispatch rx event for a bus device
214 * @work: work
216 static void mei_cl_bus_rx_work(struct work_struct *work)
218 struct mei_cl_device *cldev;
219 struct mei_device *bus;
221 cldev = container_of(work, struct mei_cl_device, rx_work);
223 bus = cldev->bus;
225 if (cldev->rx_cb)
226 cldev->rx_cb(cldev);
228 mutex_lock(&bus->device_lock);
229 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
230 mutex_unlock(&bus->device_lock);
234 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
236 * @work: work
238 static void mei_cl_bus_notif_work(struct work_struct *work)
240 struct mei_cl_device *cldev;
242 cldev = container_of(work, struct mei_cl_device, notif_work);
244 if (cldev->notif_cb)
245 cldev->notif_cb(cldev);
249 * mei_cl_bus_notify_event - schedule notify cb on bus client
251 * @cl: host client
253 * Return: true if event was scheduled
254 * false if the client is not waiting for event
256 bool mei_cl_bus_notify_event(struct mei_cl *cl)
258 struct mei_cl_device *cldev = cl->cldev;
260 if (!cldev || !cldev->notif_cb)
261 return false;
263 if (!cl->notify_ev)
264 return false;
266 schedule_work(&cldev->notif_work);
268 cl->notify_ev = false;
270 return true;
274 * mei_cl_bus_rx_event - schedule rx event
276 * @cl: host client
278 * Return: true if event was scheduled
279 * false if the client is not waiting for event
281 bool mei_cl_bus_rx_event(struct mei_cl *cl)
283 struct mei_cl_device *cldev = cl->cldev;
285 if (!cldev || !cldev->rx_cb)
286 return false;
288 schedule_work(&cldev->rx_work);
290 return true;
294 * mei_cldev_register_rx_cb - register Rx event callback
296 * @cldev: me client devices
297 * @rx_cb: callback function
299 * Return: 0 on success
300 * -EALREADY if an callback is already registered
301 * <0 on other errors
303 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
305 struct mei_device *bus = cldev->bus;
306 int ret;
308 if (!rx_cb)
309 return -EINVAL;
310 if (cldev->rx_cb)
311 return -EALREADY;
313 cldev->rx_cb = rx_cb;
314 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
316 mutex_lock(&bus->device_lock);
317 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
318 mutex_unlock(&bus->device_lock);
319 if (ret && ret != -EBUSY)
320 return ret;
322 return 0;
324 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
327 * mei_cldev_register_notif_cb - register FW notification event callback
329 * @cldev: me client devices
330 * @notif_cb: callback function
332 * Return: 0 on success
333 * -EALREADY if an callback is already registered
334 * <0 on other errors
336 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
337 mei_cldev_cb_t notif_cb)
339 struct mei_device *bus = cldev->bus;
340 int ret;
342 if (!notif_cb)
343 return -EINVAL;
345 if (cldev->notif_cb)
346 return -EALREADY;
348 cldev->notif_cb = notif_cb;
349 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
351 mutex_lock(&bus->device_lock);
352 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
353 mutex_unlock(&bus->device_lock);
354 if (ret)
355 return ret;
357 return 0;
359 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
362 * mei_cldev_get_drvdata - driver data getter
364 * @cldev: mei client device
366 * Return: driver private data
368 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
370 return dev_get_drvdata(&cldev->dev);
372 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
375 * mei_cldev_set_drvdata - driver data setter
377 * @cldev: mei client device
378 * @data: data to store
380 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
382 dev_set_drvdata(&cldev->dev, data);
384 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
387 * mei_cldev_uuid - return uuid of the underlying me client
389 * @cldev: mei client device
391 * Return: me client uuid
393 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
395 return mei_me_cl_uuid(cldev->me_cl);
397 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
400 * mei_cldev_ver - return protocol version of the underlying me client
402 * @cldev: mei client device
404 * Return: me client protocol version
406 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
408 return mei_me_cl_ver(cldev->me_cl);
410 EXPORT_SYMBOL_GPL(mei_cldev_ver);
413 * mei_cldev_enabled - check whether the device is enabled
415 * @cldev: mei client device
417 * Return: true if me client is initialized and connected
419 bool mei_cldev_enabled(struct mei_cl_device *cldev)
421 return mei_cl_is_connected(cldev->cl);
423 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
426 * mei_cldev_enable_device - enable me client device
427 * create connection with me client
429 * @cldev: me client device
431 * Return: 0 on success and < 0 on error
433 int mei_cldev_enable(struct mei_cl_device *cldev)
435 struct mei_device *bus = cldev->bus;
436 struct mei_cl *cl;
437 int ret;
439 cl = cldev->cl;
441 if (cl->state == MEI_FILE_UNINITIALIZED) {
442 mutex_lock(&bus->device_lock);
443 ret = mei_cl_link(cl);
444 mutex_unlock(&bus->device_lock);
445 if (ret)
446 return ret;
447 /* update pointers */
448 cl->cldev = cldev;
451 mutex_lock(&bus->device_lock);
452 if (mei_cl_is_connected(cl)) {
453 ret = 0;
454 goto out;
457 if (!mei_me_cl_is_active(cldev->me_cl)) {
458 dev_err(&cldev->dev, "me client is not active\n");
459 ret = -ENOTTY;
460 goto out;
463 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
464 if (ret < 0)
465 dev_err(&cldev->dev, "cannot connect\n");
467 out:
468 mutex_unlock(&bus->device_lock);
470 return ret;
472 EXPORT_SYMBOL_GPL(mei_cldev_enable);
475 * mei_cldev_disable - disable me client device
476 * disconnect form the me client
478 * @cldev: me client device
480 * Return: 0 on success and < 0 on error
482 int mei_cldev_disable(struct mei_cl_device *cldev)
484 struct mei_device *bus;
485 struct mei_cl *cl;
486 int err;
488 if (!cldev)
489 return -ENODEV;
491 cl = cldev->cl;
493 bus = cldev->bus;
495 mutex_lock(&bus->device_lock);
497 if (!mei_cl_is_connected(cl)) {
498 dev_dbg(bus->dev, "Already disconnected");
499 err = 0;
500 goto out;
503 err = mei_cl_disconnect(cl);
504 if (err < 0)
505 dev_err(bus->dev, "Could not disconnect from the ME client");
507 out:
508 /* Flush queues and remove any pending read */
509 mei_cl_flush_queues(cl, NULL);
510 mei_cl_unlink(cl);
512 mutex_unlock(&bus->device_lock);
513 return err;
515 EXPORT_SYMBOL_GPL(mei_cldev_disable);
518 * mei_cl_device_find - find matching entry in the driver id table
520 * @cldev: me client device
521 * @cldrv: me client driver
523 * Return: id on success; NULL if no id is matching
525 static const
526 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
527 struct mei_cl_driver *cldrv)
529 const struct mei_cl_device_id *id;
530 const uuid_le *uuid;
531 u8 version;
532 bool match;
534 uuid = mei_me_cl_uuid(cldev->me_cl);
535 version = mei_me_cl_ver(cldev->me_cl);
537 id = cldrv->id_table;
538 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
539 if (!uuid_le_cmp(*uuid, id->uuid)) {
540 match = true;
542 if (cldev->name[0])
543 if (strncmp(cldev->name, id->name,
544 sizeof(id->name)))
545 match = false;
547 if (id->version != MEI_CL_VERSION_ANY)
548 if (id->version != version)
549 match = false;
550 if (match)
551 return id;
554 id++;
557 return NULL;
561 * mei_cl_device_match - device match function
563 * @dev: device
564 * @drv: driver
566 * Return: 1 if matching device was found 0 otherwise
568 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
570 struct mei_cl_device *cldev = to_mei_cl_device(dev);
571 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
572 const struct mei_cl_device_id *found_id;
574 if (!cldev)
575 return 0;
577 if (!cldev->do_match)
578 return 0;
580 if (!cldrv || !cldrv->id_table)
581 return 0;
583 found_id = mei_cl_device_find(cldev, cldrv);
584 if (found_id)
585 return 1;
587 return 0;
591 * mei_cl_device_probe - bus probe function
593 * @dev: device
595 * Return: 0 on success; < 0 otherwise
597 static int mei_cl_device_probe(struct device *dev)
599 struct mei_cl_device *cldev;
600 struct mei_cl_driver *cldrv;
601 const struct mei_cl_device_id *id;
602 int ret;
604 cldev = to_mei_cl_device(dev);
605 cldrv = to_mei_cl_driver(dev->driver);
607 if (!cldev)
608 return 0;
610 if (!cldrv || !cldrv->probe)
611 return -ENODEV;
613 id = mei_cl_device_find(cldev, cldrv);
614 if (!id)
615 return -ENODEV;
617 ret = cldrv->probe(cldev, id);
618 if (ret)
619 return ret;
621 __module_get(THIS_MODULE);
622 return 0;
626 * mei_cl_device_remove - remove device from the bus
628 * @dev: device
630 * Return: 0 on success; < 0 otherwise
632 static int mei_cl_device_remove(struct device *dev)
634 struct mei_cl_device *cldev = to_mei_cl_device(dev);
635 struct mei_cl_driver *cldrv;
636 int ret = 0;
638 if (!cldev || !dev->driver)
639 return 0;
641 if (cldev->rx_cb) {
642 cancel_work_sync(&cldev->rx_work);
643 cldev->rx_cb = NULL;
645 if (cldev->notif_cb) {
646 cancel_work_sync(&cldev->notif_work);
647 cldev->notif_cb = NULL;
650 cldrv = to_mei_cl_driver(dev->driver);
651 if (cldrv->remove)
652 ret = cldrv->remove(cldev);
654 module_put(THIS_MODULE);
655 dev->driver = NULL;
656 return ret;
660 static ssize_t name_show(struct device *dev, struct device_attribute *a,
661 char *buf)
663 struct mei_cl_device *cldev = to_mei_cl_device(dev);
665 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
667 static DEVICE_ATTR_RO(name);
669 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
670 char *buf)
672 struct mei_cl_device *cldev = to_mei_cl_device(dev);
673 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
675 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
677 static DEVICE_ATTR_RO(uuid);
679 static ssize_t version_show(struct device *dev, struct device_attribute *a,
680 char *buf)
682 struct mei_cl_device *cldev = to_mei_cl_device(dev);
683 u8 version = mei_me_cl_ver(cldev->me_cl);
685 return scnprintf(buf, PAGE_SIZE, "%02X", version);
687 static DEVICE_ATTR_RO(version);
689 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
690 char *buf)
692 struct mei_cl_device *cldev = to_mei_cl_device(dev);
693 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
695 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
697 static DEVICE_ATTR_RO(modalias);
699 static struct attribute *mei_cldev_attrs[] = {
700 &dev_attr_name.attr,
701 &dev_attr_uuid.attr,
702 &dev_attr_version.attr,
703 &dev_attr_modalias.attr,
704 NULL,
706 ATTRIBUTE_GROUPS(mei_cldev);
709 * mei_cl_device_uevent - me client bus uevent handler
711 * @dev: device
712 * @env: uevent kobject
714 * Return: 0 on success -ENOMEM on when add_uevent_var fails
716 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
718 struct mei_cl_device *cldev = to_mei_cl_device(dev);
719 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
720 u8 version = mei_me_cl_ver(cldev->me_cl);
722 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
723 return -ENOMEM;
725 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
726 return -ENOMEM;
728 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
729 return -ENOMEM;
731 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
732 cldev->name, uuid, version))
733 return -ENOMEM;
735 return 0;
738 static struct bus_type mei_cl_bus_type = {
739 .name = "mei",
740 .dev_groups = mei_cldev_groups,
741 .match = mei_cl_device_match,
742 .probe = mei_cl_device_probe,
743 .remove = mei_cl_device_remove,
744 .uevent = mei_cl_device_uevent,
747 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
749 if (bus)
750 get_device(bus->dev);
752 return bus;
755 static void mei_dev_bus_put(struct mei_device *bus)
757 if (bus)
758 put_device(bus->dev);
761 static void mei_cl_bus_dev_release(struct device *dev)
763 struct mei_cl_device *cldev = to_mei_cl_device(dev);
765 if (!cldev)
766 return;
768 mei_me_cl_put(cldev->me_cl);
769 mei_dev_bus_put(cldev->bus);
770 kfree(cldev->cl);
771 kfree(cldev);
774 static struct device_type mei_cl_device_type = {
775 .release = mei_cl_bus_dev_release,
779 * mei_cl_bus_set_name - set device name for me client device
781 * @cldev: me client device
783 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
785 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
786 cldev->name,
787 mei_me_cl_uuid(cldev->me_cl),
788 mei_me_cl_ver(cldev->me_cl));
792 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
794 * @bus: mei device
795 * @me_cl: me client
797 * Return: allocated device structur or NULL on allocation failure
799 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
800 struct mei_me_client *me_cl)
802 struct mei_cl_device *cldev;
803 struct mei_cl *cl;
805 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
806 if (!cldev)
807 return NULL;
809 cl = mei_cl_allocate(bus);
810 if (!cl) {
811 kfree(cldev);
812 return NULL;
815 device_initialize(&cldev->dev);
816 cldev->dev.parent = bus->dev;
817 cldev->dev.bus = &mei_cl_bus_type;
818 cldev->dev.type = &mei_cl_device_type;
819 cldev->bus = mei_dev_bus_get(bus);
820 cldev->me_cl = mei_me_cl_get(me_cl);
821 cldev->cl = cl;
822 mei_cl_bus_set_name(cldev);
823 cldev->is_added = 0;
824 INIT_LIST_HEAD(&cldev->bus_list);
826 return cldev;
830 * mei_cl_dev_setup - setup me client device
831 * run fix up routines and set the device name
833 * @bus: mei device
834 * @cldev: me client device
836 * Return: true if the device is eligible for enumeration
838 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
839 struct mei_cl_device *cldev)
841 cldev->do_match = 1;
842 mei_cl_bus_dev_fixup(cldev);
844 /* the device name can change during fix up */
845 if (cldev->do_match)
846 mei_cl_bus_set_name(cldev);
848 return cldev->do_match == 1;
852 * mei_cl_bus_dev_add - add me client devices
854 * @cldev: me client device
856 * Return: 0 on success; < 0 on failre
858 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
860 int ret;
862 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
863 mei_me_cl_uuid(cldev->me_cl),
864 mei_me_cl_ver(cldev->me_cl));
865 ret = device_add(&cldev->dev);
866 if (!ret)
867 cldev->is_added = 1;
869 return ret;
873 * mei_cl_bus_dev_stop - stop the driver
875 * @cldev: me client device
877 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
879 if (cldev->is_added)
880 device_release_driver(&cldev->dev);
884 * mei_cl_bus_dev_destroy - destroy me client devices object
886 * @cldev: me client device
888 * Locking: called under "dev->cl_bus_lock" lock
890 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
893 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
895 if (!cldev->is_added)
896 return;
898 device_del(&cldev->dev);
900 list_del_init(&cldev->bus_list);
902 cldev->is_added = 0;
903 put_device(&cldev->dev);
907 * mei_cl_bus_remove_device - remove a devices form the bus
909 * @cldev: me client device
911 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
913 mei_cl_bus_dev_stop(cldev);
914 mei_cl_bus_dev_destroy(cldev);
918 * mei_cl_bus_remove_devices - remove all devices form the bus
920 * @bus: mei device
922 void mei_cl_bus_remove_devices(struct mei_device *bus)
924 struct mei_cl_device *cldev, *next;
926 mutex_lock(&bus->cl_bus_lock);
927 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
928 mei_cl_bus_remove_device(cldev);
929 mutex_unlock(&bus->cl_bus_lock);
934 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
935 * based on me client
937 * @bus: mei device
938 * @me_cl: me client
940 * Locking: called under "dev->cl_bus_lock" lock
942 static void mei_cl_bus_dev_init(struct mei_device *bus,
943 struct mei_me_client *me_cl)
945 struct mei_cl_device *cldev;
947 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
949 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
951 if (me_cl->bus_added)
952 return;
954 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
955 if (!cldev)
956 return;
958 me_cl->bus_added = true;
959 list_add_tail(&cldev->bus_list, &bus->device_list);
964 * mei_cl_bus_rescan - scan me clients list and add create
965 * devices for eligible clients
967 * @bus: mei device
969 void mei_cl_bus_rescan(struct mei_device *bus)
971 struct mei_cl_device *cldev, *n;
972 struct mei_me_client *me_cl;
974 mutex_lock(&bus->cl_bus_lock);
976 down_read(&bus->me_clients_rwsem);
977 list_for_each_entry(me_cl, &bus->me_clients, list)
978 mei_cl_bus_dev_init(bus, me_cl);
979 up_read(&bus->me_clients_rwsem);
981 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
983 if (!mei_me_cl_is_active(cldev->me_cl)) {
984 mei_cl_bus_remove_device(cldev);
985 continue;
988 if (cldev->is_added)
989 continue;
991 if (mei_cl_bus_dev_setup(bus, cldev))
992 mei_cl_bus_dev_add(cldev);
993 else {
994 list_del_init(&cldev->bus_list);
995 put_device(&cldev->dev);
998 mutex_unlock(&bus->cl_bus_lock);
1000 dev_dbg(bus->dev, "rescan end");
1003 void mei_cl_bus_rescan_work(struct work_struct *work)
1005 struct mei_device *bus =
1006 container_of(work, struct mei_device, bus_rescan_work);
1007 struct mei_me_client *me_cl;
1009 me_cl = mei_me_cl_by_uuid(bus, &mei_amthif_guid);
1010 if (me_cl)
1011 mei_amthif_host_init(bus, me_cl);
1012 mei_me_cl_put(me_cl);
1014 mei_cl_bus_rescan(bus);
1017 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1018 struct module *owner)
1020 int err;
1022 cldrv->driver.name = cldrv->name;
1023 cldrv->driver.owner = owner;
1024 cldrv->driver.bus = &mei_cl_bus_type;
1026 err = driver_register(&cldrv->driver);
1027 if (err)
1028 return err;
1030 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1032 return 0;
1034 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1036 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1038 driver_unregister(&cldrv->driver);
1040 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1042 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1045 int __init mei_cl_bus_init(void)
1047 return bus_register(&mei_cl_bus_type);
1050 void __exit mei_cl_bus_exit(void)
1052 bus_unregister(&mei_cl_bus_type);