1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012-2019, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
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>
21 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
24 * __mei_cl_send - internal client send (write)
27 * @buf: buffer to send
28 * @length: buffer length
32 * Return: written size bytes or < 0 on error
34 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, u8 vtag,
37 struct mei_device *bus;
41 if (WARN_ON(!cl || !cl->dev))
46 mutex_lock(&bus->device_lock);
47 if (bus->dev_state != MEI_DEV_ENABLED) {
52 if (!mei_cl_is_connected(cl)) {
57 /* Check if we have an ME client device */
58 if (!mei_me_cl_is_active(cl->me_cl)) {
63 if (length > mei_cl_mtu(cl)) {
68 while (cl->tx_cb_queued >= bus->tx_queue_limit) {
69 mutex_unlock(&bus->device_lock);
70 rets = wait_event_interruptible(cl->tx_wait,
71 cl->writing_state == MEI_WRITE_COMPLETE ||
72 (!mei_cl_is_connected(cl)));
73 mutex_lock(&bus->device_lock);
75 if (signal_pending(current))
79 if (!mei_cl_is_connected(cl)) {
85 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
92 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
93 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
94 memcpy(cb->buf.data, buf, length);
96 rets = mei_cl_write(cl, cb);
99 mutex_unlock(&bus->device_lock);
105 * __mei_cl_recv - internal client receive (read)
108 * @buf: buffer to receive
109 * @length: buffer length
112 * @timeout: recv timeout, 0 for infinite timeout
114 * Return: read size in bytes of < 0 on error
116 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag,
117 unsigned int mode, unsigned long timeout)
119 struct mei_device *bus;
120 struct mei_cl_cb *cb;
123 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
125 if (WARN_ON(!cl || !cl->dev))
130 mutex_lock(&bus->device_lock);
131 if (bus->dev_state != MEI_DEV_ENABLED) {
136 cb = mei_cl_read_cb(cl, NULL);
140 rets = mei_cl_read_start(cl, length, NULL);
141 if (rets && rets != -EBUSY)
149 /* wait on event only if there is no other waiter */
150 /* synchronized under device mutex */
151 if (!waitqueue_active(&cl->rx_wait)) {
153 mutex_unlock(&bus->device_lock);
156 rets = wait_event_interruptible_timeout
158 mei_cl_read_cb(cl, NULL) ||
159 (!mei_cl_is_connected(cl)),
160 msecs_to_jiffies(timeout));
164 if (signal_pending(current))
169 if (wait_event_interruptible
171 mei_cl_read_cb(cl, NULL) ||
172 (!mei_cl_is_connected(cl)))) {
173 if (signal_pending(current))
179 mutex_lock(&bus->device_lock);
181 if (!mei_cl_is_connected(cl)) {
187 cb = mei_cl_read_cb(cl, NULL);
199 r_length = min_t(size_t, length, cb->buf_idx);
200 memcpy(buf, cb->buf.data, r_length);
206 mei_cl_del_rd_completed(cl, cb);
208 mutex_unlock(&bus->device_lock);
214 * mei_cldev_send_vtag - me device send with vtag (write)
216 * @cldev: me client device
217 * @buf: buffer to send
218 * @length: buffer length
222 * * written size in bytes
226 ssize_t mei_cldev_send_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length,
229 struct mei_cl *cl = cldev->cl;
231 return __mei_cl_send(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING);
233 EXPORT_SYMBOL_GPL(mei_cldev_send_vtag);
236 * mei_cldev_recv_vtag - client receive with vtag (read)
238 * @cldev: me client device
239 * @buf: buffer to receive
240 * @length: buffer length
244 * * read size in bytes
248 ssize_t mei_cldev_recv_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length,
251 struct mei_cl *cl = cldev->cl;
253 return __mei_cl_recv(cl, buf, length, vtag, 0, 0);
255 EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag);
258 * mei_cldev_recv_nonblock_vtag - non block client receive with vtag (read)
260 * @cldev: me client device
261 * @buf: buffer to receive
262 * @length: buffer length
266 * * read size in bytes
267 * * -EAGAIN if function will block.
268 * * < 0 on other error
270 ssize_t mei_cldev_recv_nonblock_vtag(struct mei_cl_device *cldev, u8 *buf,
271 size_t length, u8 *vtag)
273 struct mei_cl *cl = cldev->cl;
275 return __mei_cl_recv(cl, buf, length, vtag, MEI_CL_IO_RX_NONBLOCK, 0);
277 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock_vtag);
280 * mei_cldev_send - me device send (write)
282 * @cldev: me client device
283 * @buf: buffer to send
284 * @length: buffer length
287 * * written size in bytes
290 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
292 return mei_cldev_send_vtag(cldev, buf, length, 0);
294 EXPORT_SYMBOL_GPL(mei_cldev_send);
297 * mei_cldev_recv - client receive (read)
299 * @cldev: me client device
300 * @buf: buffer to receive
301 * @length: buffer length
303 * Return: read size in bytes of < 0 on error
305 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
307 return mei_cldev_recv_vtag(cldev, buf, length, NULL);
309 EXPORT_SYMBOL_GPL(mei_cldev_recv);
312 * mei_cldev_recv_nonblock - non block client receive (read)
314 * @cldev: me client device
315 * @buf: buffer to receive
316 * @length: buffer length
318 * Return: read size in bytes of < 0 on error
319 * -EAGAIN if function will block.
321 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
324 return mei_cldev_recv_nonblock_vtag(cldev, buf, length, NULL);
326 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
329 * mei_cl_bus_rx_work - dispatch rx event for a bus device
333 static void mei_cl_bus_rx_work(struct work_struct *work)
335 struct mei_cl_device *cldev;
336 struct mei_device *bus;
338 cldev = container_of(work, struct mei_cl_device, rx_work);
345 mutex_lock(&bus->device_lock);
346 if (mei_cl_is_connected(cldev->cl))
347 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
348 mutex_unlock(&bus->device_lock);
352 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
356 static void mei_cl_bus_notif_work(struct work_struct *work)
358 struct mei_cl_device *cldev;
360 cldev = container_of(work, struct mei_cl_device, notif_work);
363 cldev->notif_cb(cldev);
367 * mei_cl_bus_notify_event - schedule notify cb on bus client
371 * Return: true if event was scheduled
372 * false if the client is not waiting for event
374 bool mei_cl_bus_notify_event(struct mei_cl *cl)
376 struct mei_cl_device *cldev = cl->cldev;
378 if (!cldev || !cldev->notif_cb)
384 schedule_work(&cldev->notif_work);
386 cl->notify_ev = false;
392 * mei_cl_bus_rx_event - schedule rx event
396 * Return: true if event was scheduled
397 * false if the client is not waiting for event
399 bool mei_cl_bus_rx_event(struct mei_cl *cl)
401 struct mei_cl_device *cldev = cl->cldev;
403 if (!cldev || !cldev->rx_cb)
406 schedule_work(&cldev->rx_work);
412 * mei_cldev_register_rx_cb - register Rx event callback
414 * @cldev: me client devices
415 * @rx_cb: callback function
417 * Return: 0 on success
418 * -EALREADY if an callback is already registered
421 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
423 struct mei_device *bus = cldev->bus;
431 cldev->rx_cb = rx_cb;
432 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
434 mutex_lock(&bus->device_lock);
435 if (mei_cl_is_connected(cldev->cl))
436 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
439 mutex_unlock(&bus->device_lock);
440 if (ret && ret != -EBUSY) {
441 cancel_work_sync(&cldev->rx_work);
448 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
451 * mei_cldev_register_notif_cb - register FW notification event callback
453 * @cldev: me client devices
454 * @notif_cb: callback function
456 * Return: 0 on success
457 * -EALREADY if an callback is already registered
460 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
461 mei_cldev_cb_t notif_cb)
463 struct mei_device *bus = cldev->bus;
472 cldev->notif_cb = notif_cb;
473 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
475 mutex_lock(&bus->device_lock);
476 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
477 mutex_unlock(&bus->device_lock);
479 cancel_work_sync(&cldev->notif_work);
480 cldev->notif_cb = NULL;
486 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
489 * mei_cldev_get_drvdata - driver data getter
491 * @cldev: mei client device
493 * Return: driver private data
495 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
497 return dev_get_drvdata(&cldev->dev);
499 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
502 * mei_cldev_set_drvdata - driver data setter
504 * @cldev: mei client device
505 * @data: data to store
507 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
509 dev_set_drvdata(&cldev->dev, data);
511 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
514 * mei_cldev_uuid - return uuid of the underlying me client
516 * @cldev: mei client device
518 * Return: me client uuid
520 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
522 return mei_me_cl_uuid(cldev->me_cl);
524 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
527 * mei_cldev_ver - return protocol version of the underlying me client
529 * @cldev: mei client device
531 * Return: me client protocol version
533 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
535 return mei_me_cl_ver(cldev->me_cl);
537 EXPORT_SYMBOL_GPL(mei_cldev_ver);
540 * mei_cldev_enabled - check whether the device is enabled
542 * @cldev: mei client device
544 * Return: true if me client is initialized and connected
546 bool mei_cldev_enabled(struct mei_cl_device *cldev)
548 return mei_cl_is_connected(cldev->cl);
550 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
553 * mei_cl_bus_module_get - acquire module of the underlying
556 * @cldev: mei client device
558 * Return: true on success; false if the module was removed.
560 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
562 return try_module_get(cldev->bus->dev->driver->owner);
566 * mei_cl_bus_module_put - release the underlying hw module.
568 * @cldev: mei client device
570 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
572 module_put(cldev->bus->dev->driver->owner);
576 * mei_cl_bus_vtag - get bus vtag entry wrapper
577 * The tag for bus client is always first.
581 * Return: bus vtag or NULL
583 static inline struct mei_cl_vtag *mei_cl_bus_vtag(struct mei_cl *cl)
585 return list_first_entry_or_null(&cl->vtag_map,
586 struct mei_cl_vtag, list);
590 * mei_cl_bus_vtag_alloc - add bus client entry to vtag map
592 * @cldev: me client device
596 * * -ENOMEM if memory allocation failed
598 static int mei_cl_bus_vtag_alloc(struct mei_cl_device *cldev)
600 struct mei_cl *cl = cldev->cl;
601 struct mei_cl_vtag *cl_vtag;
604 * Bail out if the client does not supports vtags
605 * or has already allocated one
607 if (mei_cl_vt_support_check(cl) || mei_cl_bus_vtag(cl))
610 cl_vtag = mei_cl_vtag_alloc(NULL, 0);
614 list_add_tail(&cl_vtag->list, &cl->vtag_map);
620 * mei_cl_bus_vtag_free - remove the bus entry from vtag map
622 * @cldev: me client device
624 static void mei_cl_bus_vtag_free(struct mei_cl_device *cldev)
626 struct mei_cl *cl = cldev->cl;
627 struct mei_cl_vtag *cl_vtag;
629 cl_vtag = mei_cl_bus_vtag(cl);
633 list_del(&cl_vtag->list);
638 * mei_cldev_enable - enable me client device
639 * create connection with me client
641 * @cldev: me client device
643 * Return: 0 on success and < 0 on error
645 int mei_cldev_enable(struct mei_cl_device *cldev)
647 struct mei_device *bus = cldev->bus;
653 mutex_lock(&bus->device_lock);
654 if (cl->state == MEI_FILE_UNINITIALIZED) {
655 ret = mei_cl_link(cl);
658 /* update pointers */
662 if (mei_cl_is_connected(cl)) {
667 if (!mei_me_cl_is_active(cldev->me_cl)) {
668 dev_err(&cldev->dev, "me client is not active\n");
673 ret = mei_cl_bus_vtag_alloc(cldev);
677 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
679 dev_err(&cldev->dev, "cannot connect\n");
680 mei_cl_bus_vtag_free(cldev);
684 mutex_unlock(&bus->device_lock);
688 EXPORT_SYMBOL_GPL(mei_cldev_enable);
691 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
694 * @cldev: client device
696 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
699 cancel_work_sync(&cldev->rx_work);
703 if (cldev->notif_cb) {
704 cancel_work_sync(&cldev->notif_work);
705 cldev->notif_cb = NULL;
710 * mei_cldev_disable - disable me client device
711 * disconnect form the me client
713 * @cldev: me client device
715 * Return: 0 on success and < 0 on error
717 int mei_cldev_disable(struct mei_cl_device *cldev)
719 struct mei_device *bus;
730 mei_cldev_unregister_callbacks(cldev);
732 mutex_lock(&bus->device_lock);
734 mei_cl_bus_vtag_free(cldev);
736 if (!mei_cl_is_connected(cl)) {
737 dev_dbg(bus->dev, "Already disconnected\n");
742 err = mei_cl_disconnect(cl);
744 dev_err(bus->dev, "Could not disconnect from the ME client\n");
747 /* Flush queues and remove any pending read */
748 mei_cl_flush_queues(cl, NULL);
751 mutex_unlock(&bus->device_lock);
754 EXPORT_SYMBOL_GPL(mei_cldev_disable);
757 * mei_cl_device_find - find matching entry in the driver id table
759 * @cldev: me client device
760 * @cldrv: me client driver
762 * Return: id on success; NULL if no id is matching
765 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
766 struct mei_cl_driver *cldrv)
768 const struct mei_cl_device_id *id;
773 uuid = mei_me_cl_uuid(cldev->me_cl);
774 version = mei_me_cl_ver(cldev->me_cl);
776 id = cldrv->id_table;
777 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
778 if (!uuid_le_cmp(*uuid, id->uuid)) {
782 if (strncmp(cldev->name, id->name,
786 if (id->version != MEI_CL_VERSION_ANY)
787 if (id->version != version)
800 * mei_cl_device_match - device match function
805 * Return: 1 if matching device was found 0 otherwise
807 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
809 struct mei_cl_device *cldev = to_mei_cl_device(dev);
810 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
811 const struct mei_cl_device_id *found_id;
816 if (!cldev->do_match)
819 if (!cldrv || !cldrv->id_table)
822 found_id = mei_cl_device_find(cldev, cldrv);
830 * mei_cl_device_probe - bus probe function
834 * Return: 0 on success; < 0 otherwise
836 static int mei_cl_device_probe(struct device *dev)
838 struct mei_cl_device *cldev;
839 struct mei_cl_driver *cldrv;
840 const struct mei_cl_device_id *id;
843 cldev = to_mei_cl_device(dev);
844 cldrv = to_mei_cl_driver(dev->driver);
849 if (!cldrv || !cldrv->probe)
852 id = mei_cl_device_find(cldev, cldrv);
856 if (!mei_cl_bus_module_get(cldev)) {
857 dev_err(&cldev->dev, "get hw module failed");
861 ret = cldrv->probe(cldev, id);
863 mei_cl_bus_module_put(cldev);
867 __module_get(THIS_MODULE);
872 * mei_cl_device_remove - remove device from the bus
876 * Return: 0 on success; < 0 otherwise
878 static int mei_cl_device_remove(struct device *dev)
880 struct mei_cl_device *cldev = to_mei_cl_device(dev);
881 struct mei_cl_driver *cldrv;
884 if (!cldev || !dev->driver)
887 cldrv = to_mei_cl_driver(dev->driver);
889 ret = cldrv->remove(cldev);
891 mei_cldev_unregister_callbacks(cldev);
893 mei_cl_bus_module_put(cldev);
894 module_put(THIS_MODULE);
899 static ssize_t name_show(struct device *dev, struct device_attribute *a,
902 struct mei_cl_device *cldev = to_mei_cl_device(dev);
904 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
906 static DEVICE_ATTR_RO(name);
908 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
911 struct mei_cl_device *cldev = to_mei_cl_device(dev);
912 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
914 return sprintf(buf, "%pUl", uuid);
916 static DEVICE_ATTR_RO(uuid);
918 static ssize_t version_show(struct device *dev, struct device_attribute *a,
921 struct mei_cl_device *cldev = to_mei_cl_device(dev);
922 u8 version = mei_me_cl_ver(cldev->me_cl);
924 return sprintf(buf, "%02X", version);
926 static DEVICE_ATTR_RO(version);
928 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
931 struct mei_cl_device *cldev = to_mei_cl_device(dev);
932 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
933 u8 version = mei_me_cl_ver(cldev->me_cl);
935 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
936 cldev->name, uuid, version);
938 static DEVICE_ATTR_RO(modalias);
940 static ssize_t max_conn_show(struct device *dev, struct device_attribute *a,
943 struct mei_cl_device *cldev = to_mei_cl_device(dev);
944 u8 maxconn = mei_me_cl_max_conn(cldev->me_cl);
946 return sprintf(buf, "%d", maxconn);
948 static DEVICE_ATTR_RO(max_conn);
950 static ssize_t fixed_show(struct device *dev, struct device_attribute *a,
953 struct mei_cl_device *cldev = to_mei_cl_device(dev);
954 u8 fixed = mei_me_cl_fixed(cldev->me_cl);
956 return sprintf(buf, "%d", fixed);
958 static DEVICE_ATTR_RO(fixed);
960 static ssize_t vtag_show(struct device *dev, struct device_attribute *a,
963 struct mei_cl_device *cldev = to_mei_cl_device(dev);
964 bool vt = mei_me_cl_vt(cldev->me_cl);
966 return sprintf(buf, "%d", vt);
968 static DEVICE_ATTR_RO(vtag);
970 static ssize_t max_len_show(struct device *dev, struct device_attribute *a,
973 struct mei_cl_device *cldev = to_mei_cl_device(dev);
974 u32 maxlen = mei_me_cl_max_len(cldev->me_cl);
976 return sprintf(buf, "%u", maxlen);
978 static DEVICE_ATTR_RO(max_len);
980 static struct attribute *mei_cldev_attrs[] = {
983 &dev_attr_version.attr,
984 &dev_attr_modalias.attr,
985 &dev_attr_max_conn.attr,
986 &dev_attr_fixed.attr,
988 &dev_attr_max_len.attr,
991 ATTRIBUTE_GROUPS(mei_cldev);
994 * mei_cl_device_uevent - me client bus uevent handler
997 * @env: uevent kobject
999 * Return: 0 on success -ENOMEM on when add_uevent_var fails
1001 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1003 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1004 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
1005 u8 version = mei_me_cl_ver(cldev->me_cl);
1007 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
1010 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
1013 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
1016 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
1017 cldev->name, uuid, version))
1023 static struct bus_type mei_cl_bus_type = {
1025 .dev_groups = mei_cldev_groups,
1026 .match = mei_cl_device_match,
1027 .probe = mei_cl_device_probe,
1028 .remove = mei_cl_device_remove,
1029 .uevent = mei_cl_device_uevent,
1032 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
1035 get_device(bus->dev);
1040 static void mei_dev_bus_put(struct mei_device *bus)
1043 put_device(bus->dev);
1046 static void mei_cl_bus_dev_release(struct device *dev)
1048 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1053 mei_me_cl_put(cldev->me_cl);
1054 mei_dev_bus_put(cldev->bus);
1055 mei_cl_unlink(cldev->cl);
1060 static const struct device_type mei_cl_device_type = {
1061 .release = mei_cl_bus_dev_release,
1065 * mei_cl_bus_set_name - set device name for me client device
1066 * <controller>-<client device>
1067 * Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb
1069 * @cldev: me client device
1071 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
1073 dev_set_name(&cldev->dev, "%s-%pUl",
1074 dev_name(cldev->bus->dev),
1075 mei_me_cl_uuid(cldev->me_cl));
1079 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
1084 * Return: allocated device structur or NULL on allocation failure
1086 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
1087 struct mei_me_client *me_cl)
1089 struct mei_cl_device *cldev;
1092 cldev = kzalloc(sizeof(*cldev), GFP_KERNEL);
1096 cl = mei_cl_allocate(bus);
1102 device_initialize(&cldev->dev);
1103 cldev->dev.parent = bus->dev;
1104 cldev->dev.bus = &mei_cl_bus_type;
1105 cldev->dev.type = &mei_cl_device_type;
1106 cldev->bus = mei_dev_bus_get(bus);
1107 cldev->me_cl = mei_me_cl_get(me_cl);
1109 mei_cl_bus_set_name(cldev);
1110 cldev->is_added = 0;
1111 INIT_LIST_HEAD(&cldev->bus_list);
1117 * mei_cl_bus_dev_setup - setup me client device
1118 * run fix up routines and set the device name
1121 * @cldev: me client device
1123 * Return: true if the device is eligible for enumeration
1125 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
1126 struct mei_cl_device *cldev)
1128 cldev->do_match = 1;
1129 mei_cl_bus_dev_fixup(cldev);
1131 /* the device name can change during fix up */
1132 if (cldev->do_match)
1133 mei_cl_bus_set_name(cldev);
1135 return cldev->do_match == 1;
1139 * mei_cl_bus_dev_add - add me client devices
1141 * @cldev: me client device
1143 * Return: 0 on success; < 0 on failre
1145 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
1149 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
1150 mei_me_cl_uuid(cldev->me_cl),
1151 mei_me_cl_ver(cldev->me_cl));
1152 ret = device_add(&cldev->dev);
1154 cldev->is_added = 1;
1160 * mei_cl_bus_dev_stop - stop the driver
1162 * @cldev: me client device
1164 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
1166 if (cldev->is_added)
1167 device_release_driver(&cldev->dev);
1171 * mei_cl_bus_dev_destroy - destroy me client devices object
1173 * @cldev: me client device
1175 * Locking: called under "dev->cl_bus_lock" lock
1177 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
1180 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1182 if (!cldev->is_added)
1185 device_del(&cldev->dev);
1187 list_del_init(&cldev->bus_list);
1189 cldev->is_added = 0;
1190 put_device(&cldev->dev);
1194 * mei_cl_bus_remove_device - remove a devices form the bus
1196 * @cldev: me client device
1198 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1200 mei_cl_bus_dev_stop(cldev);
1201 mei_cl_bus_dev_destroy(cldev);
1205 * mei_cl_bus_remove_devices - remove all devices form the bus
1209 void mei_cl_bus_remove_devices(struct mei_device *bus)
1211 struct mei_cl_device *cldev, *next;
1213 mutex_lock(&bus->cl_bus_lock);
1214 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1215 mei_cl_bus_remove_device(cldev);
1216 mutex_unlock(&bus->cl_bus_lock);
1221 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1222 * based on me client
1227 * Locking: called under "dev->cl_bus_lock" lock
1229 static void mei_cl_bus_dev_init(struct mei_device *bus,
1230 struct mei_me_client *me_cl)
1232 struct mei_cl_device *cldev;
1234 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1236 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1238 if (me_cl->bus_added)
1241 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1245 me_cl->bus_added = true;
1246 list_add_tail(&cldev->bus_list, &bus->device_list);
1251 * mei_cl_bus_rescan - scan me clients list and add create
1252 * devices for eligible clients
1256 static void mei_cl_bus_rescan(struct mei_device *bus)
1258 struct mei_cl_device *cldev, *n;
1259 struct mei_me_client *me_cl;
1261 mutex_lock(&bus->cl_bus_lock);
1263 down_read(&bus->me_clients_rwsem);
1264 list_for_each_entry(me_cl, &bus->me_clients, list)
1265 mei_cl_bus_dev_init(bus, me_cl);
1266 up_read(&bus->me_clients_rwsem);
1268 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1270 if (!mei_me_cl_is_active(cldev->me_cl)) {
1271 mei_cl_bus_remove_device(cldev);
1275 if (cldev->is_added)
1278 if (mei_cl_bus_dev_setup(bus, cldev))
1279 mei_cl_bus_dev_add(cldev);
1281 list_del_init(&cldev->bus_list);
1282 put_device(&cldev->dev);
1285 mutex_unlock(&bus->cl_bus_lock);
1287 dev_dbg(bus->dev, "rescan end");
1290 void mei_cl_bus_rescan_work(struct work_struct *work)
1292 struct mei_device *bus =
1293 container_of(work, struct mei_device, bus_rescan_work);
1295 mei_cl_bus_rescan(bus);
1298 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1299 struct module *owner)
1303 cldrv->driver.name = cldrv->name;
1304 cldrv->driver.owner = owner;
1305 cldrv->driver.bus = &mei_cl_bus_type;
1307 err = driver_register(&cldrv->driver);
1311 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1315 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1317 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1319 driver_unregister(&cldrv->driver);
1321 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1323 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1326 int __init mei_cl_bus_init(void)
1328 return bus_register(&mei_cl_bus_type);
1331 void __exit mei_cl_bus_exit(void)
1333 bus_unregister(&mei_cl_bus_type);