powercap: restrict energy meter to root access
[linux/fpc-iii.git] / drivers / misc / mei / bus.c
blob610e87bdb5dc3d0c38742698c64d1dc34ddffaa3
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;
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 memcpy(cb->buf.data, buf, length);
85 rets = mei_cl_write(cl, cb, blocking);
87 out:
88 mutex_unlock(&bus->device_lock);
90 return rets;
93 /**
94 * __mei_cl_recv - internal client receive (read)
96 * @cl: host client
97 * @buf: buffer to receive
98 * @length: buffer length
100 * Return: read size in bytes of < 0 on error
102 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
104 struct mei_device *bus;
105 struct mei_cl_cb *cb;
106 size_t r_length;
107 ssize_t rets;
109 if (WARN_ON(!cl || !cl->dev))
110 return -ENODEV;
112 bus = cl->dev;
114 mutex_lock(&bus->device_lock);
115 if (bus->dev_state != MEI_DEV_ENABLED) {
116 rets = -ENODEV;
117 goto out;
120 cb = mei_cl_read_cb(cl, NULL);
121 if (cb)
122 goto copy;
124 rets = mei_cl_read_start(cl, length, NULL);
125 if (rets && rets != -EBUSY)
126 goto out;
128 /* wait on event only if there is no other waiter */
129 /* synchronized under device mutex */
130 if (!waitqueue_active(&cl->rx_wait)) {
132 mutex_unlock(&bus->device_lock);
134 if (wait_event_interruptible(cl->rx_wait,
135 (!list_empty(&cl->rd_completed)) ||
136 (!mei_cl_is_connected(cl)))) {
138 if (signal_pending(current))
139 return -EINTR;
140 return -ERESTARTSYS;
143 mutex_lock(&bus->device_lock);
145 if (!mei_cl_is_connected(cl)) {
146 rets = -ENODEV;
147 goto out;
151 cb = mei_cl_read_cb(cl, NULL);
152 if (!cb) {
153 rets = 0;
154 goto out;
157 copy:
158 if (cb->status) {
159 rets = cb->status;
160 goto free;
163 r_length = min_t(size_t, length, cb->buf_idx);
164 memcpy(buf, cb->buf.data, r_length);
165 rets = r_length;
167 free:
168 mei_io_cb_free(cb);
169 out:
170 mutex_unlock(&bus->device_lock);
172 return rets;
176 * mei_cldev_send - me device send (write)
178 * @cldev: me client device
179 * @buf: buffer to send
180 * @length: buffer length
182 * Return: written size in bytes or < 0 on error
184 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
186 struct mei_cl *cl = cldev->cl;
188 if (cl == NULL)
189 return -ENODEV;
191 return __mei_cl_send(cl, buf, length, 1);
193 EXPORT_SYMBOL_GPL(mei_cldev_send);
196 * mei_cldev_recv - client receive (read)
198 * @cldev: me client device
199 * @buf: buffer to receive
200 * @length: buffer length
202 * Return: read size in bytes of < 0 on error
204 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
206 struct mei_cl *cl = cldev->cl;
208 if (cl == NULL)
209 return -ENODEV;
211 return __mei_cl_recv(cl, buf, length);
213 EXPORT_SYMBOL_GPL(mei_cldev_recv);
216 * mei_cl_bus_event_work - dispatch rx event for a bus device
217 * and schedule new work
219 * @work: work
221 static void mei_cl_bus_event_work(struct work_struct *work)
223 struct mei_cl_device *cldev;
224 struct mei_device *bus;
226 cldev = container_of(work, struct mei_cl_device, event_work);
228 bus = cldev->bus;
230 if (cldev->event_cb)
231 cldev->event_cb(cldev, cldev->events, cldev->event_context);
233 cldev->events = 0;
235 /* Prepare for the next read */
236 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
237 mutex_lock(&bus->device_lock);
238 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
239 mutex_unlock(&bus->device_lock);
244 * mei_cl_bus_notify_event - schedule notify cb on bus client
246 * @cl: host client
248 * Return: true if event was scheduled
249 * false if the client is not waiting for event
251 bool mei_cl_bus_notify_event(struct mei_cl *cl)
253 struct mei_cl_device *cldev = cl->cldev;
255 if (!cldev || !cldev->event_cb)
256 return false;
258 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)))
259 return false;
261 if (!cl->notify_ev)
262 return false;
264 set_bit(MEI_CL_EVENT_NOTIF, &cldev->events);
266 schedule_work(&cldev->event_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->event_cb)
286 return false;
288 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX)))
289 return false;
291 set_bit(MEI_CL_EVENT_RX, &cldev->events);
293 schedule_work(&cldev->event_work);
295 return true;
299 * mei_cldev_register_event_cb - register event callback
301 * @cldev: me client devices
302 * @event_cb: callback function
303 * @events_mask: requested events bitmask
304 * @context: driver context data
306 * Return: 0 on success
307 * -EALREADY if an callback is already registered
308 * <0 on other errors
310 int mei_cldev_register_event_cb(struct mei_cl_device *cldev,
311 unsigned long events_mask,
312 mei_cldev_event_cb_t event_cb, void *context)
314 struct mei_device *bus = cldev->bus;
315 int ret;
317 if (cldev->event_cb)
318 return -EALREADY;
320 cldev->events = 0;
321 cldev->events_mask = events_mask;
322 cldev->event_cb = event_cb;
323 cldev->event_context = context;
324 INIT_WORK(&cldev->event_work, mei_cl_bus_event_work);
326 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
327 mutex_lock(&bus->device_lock);
328 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
329 mutex_unlock(&bus->device_lock);
330 if (ret && ret != -EBUSY)
331 return ret;
334 if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) {
335 mutex_lock(&bus->device_lock);
336 ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0);
337 mutex_unlock(&bus->device_lock);
338 if (ret)
339 return ret;
342 return 0;
344 EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb);
347 * mei_cldev_get_drvdata - driver data getter
349 * @cldev: mei client device
351 * Return: driver private data
353 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
355 return dev_get_drvdata(&cldev->dev);
357 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
360 * mei_cldev_set_drvdata - driver data setter
362 * @cldev: mei client device
363 * @data: data to store
365 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
367 dev_set_drvdata(&cldev->dev, data);
369 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
372 * mei_cldev_uuid - return uuid of the underlying me client
374 * @cldev: mei client device
376 * Return: me client uuid
378 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
380 return mei_me_cl_uuid(cldev->me_cl);
382 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
385 * mei_cldev_ver - return protocol version of the underlying me client
387 * @cldev: mei client device
389 * Return: me client protocol version
391 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
393 return mei_me_cl_ver(cldev->me_cl);
395 EXPORT_SYMBOL_GPL(mei_cldev_ver);
398 * mei_cldev_enabled - check whether the device is enabled
400 * @cldev: mei client device
402 * Return: true if me client is initialized and connected
404 bool mei_cldev_enabled(struct mei_cl_device *cldev)
406 return cldev->cl && mei_cl_is_connected(cldev->cl);
408 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
411 * mei_cldev_enable - enable me client device
412 * create connection with me client
414 * @cldev: me client device
416 * Return: 0 on success and < 0 on error
418 int mei_cldev_enable(struct mei_cl_device *cldev)
420 struct mei_device *bus = cldev->bus;
421 struct mei_cl *cl;
422 int ret;
424 cl = cldev->cl;
426 if (!cl) {
427 mutex_lock(&bus->device_lock);
428 cl = mei_cl_alloc_linked(bus);
429 mutex_unlock(&bus->device_lock);
430 if (IS_ERR(cl))
431 return PTR_ERR(cl);
432 /* update pointers */
433 cldev->cl = cl;
434 cl->cldev = cldev;
437 mutex_lock(&bus->device_lock);
438 if (mei_cl_is_connected(cl)) {
439 ret = 0;
440 goto out;
443 if (!mei_me_cl_is_active(cldev->me_cl)) {
444 dev_err(&cldev->dev, "me client is not active\n");
445 ret = -ENOTTY;
446 goto out;
449 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
450 if (ret < 0)
451 dev_err(&cldev->dev, "cannot connect\n");
453 out:
454 mutex_unlock(&bus->device_lock);
456 return ret;
458 EXPORT_SYMBOL_GPL(mei_cldev_enable);
461 * mei_cldev_disable - disable me client device
462 * disconnect form the me client
464 * @cldev: me client device
466 * Return: 0 on success and < 0 on error
468 int mei_cldev_disable(struct mei_cl_device *cldev)
470 struct mei_device *bus;
471 struct mei_cl *cl;
472 int err;
474 if (!cldev || !cldev->cl)
475 return -ENODEV;
477 cl = cldev->cl;
479 bus = cldev->bus;
481 cldev->event_cb = NULL;
483 mutex_lock(&bus->device_lock);
485 if (!mei_cl_is_connected(cl)) {
486 dev_err(bus->dev, "Already disconnected");
487 err = 0;
488 goto out;
491 err = mei_cl_disconnect(cl);
492 if (err < 0)
493 dev_err(bus->dev, "Could not disconnect from the ME client");
495 out:
496 /* Flush queues and remove any pending read */
497 mei_cl_flush_queues(cl, NULL);
498 mei_cl_unlink(cl);
500 kfree(cl);
501 cldev->cl = NULL;
503 mutex_unlock(&bus->device_lock);
504 return err;
506 EXPORT_SYMBOL_GPL(mei_cldev_disable);
509 * mei_cl_device_find - find matching entry in the driver id table
511 * @cldev: me client device
512 * @cldrv: me client driver
514 * Return: id on success; NULL if no id is matching
516 static const
517 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
518 struct mei_cl_driver *cldrv)
520 const struct mei_cl_device_id *id;
521 const uuid_le *uuid;
522 u8 version;
523 bool match;
525 uuid = mei_me_cl_uuid(cldev->me_cl);
526 version = mei_me_cl_ver(cldev->me_cl);
528 id = cldrv->id_table;
529 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
530 if (!uuid_le_cmp(*uuid, id->uuid)) {
531 match = true;
533 if (cldev->name[0])
534 if (strncmp(cldev->name, id->name,
535 sizeof(id->name)))
536 match = false;
538 if (id->version != MEI_CL_VERSION_ANY)
539 if (id->version != version)
540 match = false;
541 if (match)
542 return id;
545 id++;
548 return NULL;
552 * mei_cl_device_match - device match function
554 * @dev: device
555 * @drv: driver
557 * Return: 1 if matching device was found 0 otherwise
559 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
561 struct mei_cl_device *cldev = to_mei_cl_device(dev);
562 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
563 const struct mei_cl_device_id *found_id;
565 if (!cldev)
566 return 0;
568 if (!cldev->do_match)
569 return 0;
571 if (!cldrv || !cldrv->id_table)
572 return 0;
574 found_id = mei_cl_device_find(cldev, cldrv);
575 if (found_id)
576 return 1;
578 return 0;
582 * mei_cl_device_probe - bus probe function
584 * @dev: device
586 * Return: 0 on success; < 0 otherwise
588 static int mei_cl_device_probe(struct device *dev)
590 struct mei_cl_device *cldev;
591 struct mei_cl_driver *cldrv;
592 const struct mei_cl_device_id *id;
593 int ret;
595 cldev = to_mei_cl_device(dev);
596 cldrv = to_mei_cl_driver(dev->driver);
598 if (!cldev)
599 return 0;
601 if (!cldrv || !cldrv->probe)
602 return -ENODEV;
604 id = mei_cl_device_find(cldev, cldrv);
605 if (!id)
606 return -ENODEV;
608 ret = cldrv->probe(cldev, id);
609 if (ret)
610 return ret;
612 __module_get(THIS_MODULE);
613 return 0;
617 * mei_cl_device_remove - remove device from the bus
619 * @dev: device
621 * Return: 0 on success; < 0 otherwise
623 static int mei_cl_device_remove(struct device *dev)
625 struct mei_cl_device *cldev = to_mei_cl_device(dev);
626 struct mei_cl_driver *cldrv;
627 int ret = 0;
629 if (!cldev || !dev->driver)
630 return 0;
632 if (cldev->event_cb) {
633 cldev->event_cb = NULL;
634 cancel_work_sync(&cldev->event_work);
637 cldrv = to_mei_cl_driver(dev->driver);
638 if (cldrv->remove)
639 ret = cldrv->remove(cldev);
641 module_put(THIS_MODULE);
643 return ret;
646 static ssize_t name_show(struct device *dev, struct device_attribute *a,
647 char *buf)
649 struct mei_cl_device *cldev = to_mei_cl_device(dev);
651 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
653 static DEVICE_ATTR_RO(name);
655 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
656 char *buf)
658 struct mei_cl_device *cldev = to_mei_cl_device(dev);
659 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
661 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
663 static DEVICE_ATTR_RO(uuid);
665 static ssize_t version_show(struct device *dev, struct device_attribute *a,
666 char *buf)
668 struct mei_cl_device *cldev = to_mei_cl_device(dev);
669 u8 version = mei_me_cl_ver(cldev->me_cl);
671 return scnprintf(buf, PAGE_SIZE, "%02X", version);
673 static DEVICE_ATTR_RO(version);
675 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
676 char *buf)
678 struct mei_cl_device *cldev = to_mei_cl_device(dev);
679 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
680 u8 version = mei_me_cl_ver(cldev->me_cl);
682 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
683 cldev->name, uuid, version);
685 static DEVICE_ATTR_RO(modalias);
687 static struct attribute *mei_cldev_attrs[] = {
688 &dev_attr_name.attr,
689 &dev_attr_uuid.attr,
690 &dev_attr_version.attr,
691 &dev_attr_modalias.attr,
692 NULL,
694 ATTRIBUTE_GROUPS(mei_cldev);
697 * mei_cl_device_uevent - me client bus uevent handler
699 * @dev: device
700 * @env: uevent kobject
702 * Return: 0 on success -ENOMEM on when add_uevent_var fails
704 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
706 struct mei_cl_device *cldev = to_mei_cl_device(dev);
707 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
708 u8 version = mei_me_cl_ver(cldev->me_cl);
710 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
711 return -ENOMEM;
713 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
714 return -ENOMEM;
716 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
717 return -ENOMEM;
719 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
720 cldev->name, uuid, version))
721 return -ENOMEM;
723 return 0;
726 static struct bus_type mei_cl_bus_type = {
727 .name = "mei",
728 .dev_groups = mei_cldev_groups,
729 .match = mei_cl_device_match,
730 .probe = mei_cl_device_probe,
731 .remove = mei_cl_device_remove,
732 .uevent = mei_cl_device_uevent,
735 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
737 if (bus)
738 get_device(bus->dev);
740 return bus;
743 static void mei_dev_bus_put(struct mei_device *bus)
745 if (bus)
746 put_device(bus->dev);
749 static void mei_cl_bus_dev_release(struct device *dev)
751 struct mei_cl_device *cldev = to_mei_cl_device(dev);
753 if (!cldev)
754 return;
756 mei_me_cl_put(cldev->me_cl);
757 mei_dev_bus_put(cldev->bus);
758 kfree(cldev);
761 static struct device_type mei_cl_device_type = {
762 .release = mei_cl_bus_dev_release,
766 * mei_cl_bus_set_name - set device name for me client device
767 * <controller>-<client device>
768 * Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb
770 * @cldev: me client device
772 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
774 dev_set_name(&cldev->dev, "%s-%pUl",
775 dev_name(cldev->bus->dev),
776 mei_me_cl_uuid(cldev->me_cl));
780 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
782 * @bus: mei device
783 * @me_cl: me client
785 * Return: allocated device structur or NULL on allocation failure
787 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
788 struct mei_me_client *me_cl)
790 struct mei_cl_device *cldev;
792 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
793 if (!cldev)
794 return NULL;
796 device_initialize(&cldev->dev);
797 cldev->dev.parent = bus->dev;
798 cldev->dev.bus = &mei_cl_bus_type;
799 cldev->dev.type = &mei_cl_device_type;
800 cldev->bus = mei_dev_bus_get(bus);
801 cldev->me_cl = mei_me_cl_get(me_cl);
802 mei_cl_bus_set_name(cldev);
803 cldev->is_added = 0;
804 INIT_LIST_HEAD(&cldev->bus_list);
806 return cldev;
810 * mei_cl_dev_setup - setup me client device
811 * run fix up routines and set the device name
813 * @bus: mei device
814 * @cldev: me client device
816 * Return: true if the device is eligible for enumeration
818 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
819 struct mei_cl_device *cldev)
821 cldev->do_match = 1;
822 mei_cl_bus_dev_fixup(cldev);
824 /* the device name can change during fix up */
825 if (cldev->do_match)
826 mei_cl_bus_set_name(cldev);
828 return cldev->do_match == 1;
832 * mei_cl_bus_dev_add - add me client devices
834 * @cldev: me client device
836 * Return: 0 on success; < 0 on failre
838 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
840 int ret;
842 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
843 mei_me_cl_uuid(cldev->me_cl),
844 mei_me_cl_ver(cldev->me_cl));
845 ret = device_add(&cldev->dev);
846 if (!ret)
847 cldev->is_added = 1;
849 return ret;
853 * mei_cl_bus_dev_stop - stop the driver
855 * @cldev: me client device
857 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
859 if (cldev->is_added)
860 device_release_driver(&cldev->dev);
864 * mei_cl_bus_dev_destroy - destroy me client devices object
866 * @cldev: me client device
868 * Locking: called under "dev->cl_bus_lock" lock
870 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
873 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
875 if (!cldev->is_added)
876 return;
878 device_del(&cldev->dev);
880 list_del_init(&cldev->bus_list);
882 cldev->is_added = 0;
883 put_device(&cldev->dev);
887 * mei_cl_bus_remove_device - remove a devices form the bus
889 * @cldev: me client device
891 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
893 mei_cl_bus_dev_stop(cldev);
894 mei_cl_bus_dev_destroy(cldev);
898 * mei_cl_bus_remove_devices - remove all devices form the bus
900 * @bus: mei device
902 void mei_cl_bus_remove_devices(struct mei_device *bus)
904 struct mei_cl_device *cldev, *next;
906 mutex_lock(&bus->cl_bus_lock);
907 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
908 mei_cl_bus_remove_device(cldev);
909 mutex_unlock(&bus->cl_bus_lock);
914 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
915 * based on me client
917 * @bus: mei device
918 * @me_cl: me client
920 * Locking: called under "dev->cl_bus_lock" lock
922 static void mei_cl_bus_dev_init(struct mei_device *bus,
923 struct mei_me_client *me_cl)
925 struct mei_cl_device *cldev;
927 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
929 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
931 if (me_cl->bus_added)
932 return;
934 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
935 if (!cldev)
936 return;
938 me_cl->bus_added = true;
939 list_add_tail(&cldev->bus_list, &bus->device_list);
944 * mei_cl_bus_rescan - scan me clients list and add create
945 * devices for eligible clients
947 * @bus: mei device
949 void mei_cl_bus_rescan(struct mei_device *bus)
951 struct mei_cl_device *cldev, *n;
952 struct mei_me_client *me_cl;
954 mutex_lock(&bus->cl_bus_lock);
956 down_read(&bus->me_clients_rwsem);
957 list_for_each_entry(me_cl, &bus->me_clients, list)
958 mei_cl_bus_dev_init(bus, me_cl);
959 up_read(&bus->me_clients_rwsem);
961 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
963 if (!mei_me_cl_is_active(cldev->me_cl)) {
964 mei_cl_bus_remove_device(cldev);
965 continue;
968 if (cldev->is_added)
969 continue;
971 if (mei_cl_bus_dev_setup(bus, cldev))
972 mei_cl_bus_dev_add(cldev);
973 else {
974 list_del_init(&cldev->bus_list);
975 put_device(&cldev->dev);
978 mutex_unlock(&bus->cl_bus_lock);
980 dev_dbg(bus->dev, "rescan end");
983 void mei_cl_bus_rescan_work(struct work_struct *work)
985 struct mei_device *bus =
986 container_of(work, struct mei_device, bus_rescan_work);
987 struct mei_me_client *me_cl;
989 me_cl = mei_me_cl_by_uuid(bus, &mei_amthif_guid);
990 if (me_cl)
991 mei_amthif_host_init(bus, me_cl);
992 mei_me_cl_put(me_cl);
994 mei_cl_bus_rescan(bus);
997 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
998 struct module *owner)
1000 int err;
1002 cldrv->driver.name = cldrv->name;
1003 cldrv->driver.owner = owner;
1004 cldrv->driver.bus = &mei_cl_bus_type;
1006 err = driver_register(&cldrv->driver);
1007 if (err)
1008 return err;
1010 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1012 return 0;
1014 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1016 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1018 driver_unregister(&cldrv->driver);
1020 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1022 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1025 int __init mei_cl_bus_init(void)
1027 return bus_register(&mei_cl_bus_type);
1030 void __exit mei_cl_bus_exit(void)
1032 bus_unregister(&mei_cl_bus_type);