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
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>
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)
34 * __mei_cl_send - internal client send (write)
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,
46 struct mei_device *bus;
47 struct mei_cl_cb *cb = NULL;
50 if (WARN_ON(!cl || !cl->dev))
55 mutex_lock(&bus->device_lock);
56 if (bus->dev_state != MEI_DEV_ENABLED) {
61 if (!mei_cl_is_connected(cl)) {
66 /* Check if we have an ME client device */
67 if (!mei_me_cl_is_active(cl->me_cl)) {
72 if (length > mei_cl_mtu(cl)) {
77 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
83 memcpy(cb->buf.data, buf, length);
85 rets = mei_cl_write(cl, cb, blocking);
88 mutex_unlock(&bus->device_lock);
96 * __mei_cl_recv - internal client receive (read)
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;
111 if (WARN_ON(!cl || !cl->dev))
116 mutex_lock(&bus->device_lock);
117 if (bus->dev_state != MEI_DEV_ENABLED) {
122 cb = mei_cl_read_cb(cl, NULL);
126 rets = mei_cl_read_start(cl, length, NULL);
127 if (rets && rets != -EBUSY)
130 /* wait on event only if there is no other waiter */
131 if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
133 mutex_unlock(&bus->device_lock);
135 if (wait_event_interruptible(cl->rx_wait,
136 (!list_empty(&cl->rd_completed)) ||
137 (!mei_cl_is_connected(cl)))) {
139 if (signal_pending(current))
144 mutex_lock(&bus->device_lock);
146 if (!mei_cl_is_connected(cl)) {
152 cb = mei_cl_read_cb(cl, NULL);
164 r_length = min_t(size_t, length, cb->buf_idx);
165 memcpy(buf, cb->buf.data, r_length);
171 mutex_unlock(&bus->device_lock);
177 * mei_cldev_send - me device send (write)
179 * @cldev: me client device
180 * @buf: buffer to send
181 * @length: buffer length
183 * Return: written size in bytes or < 0 on error
185 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
187 struct mei_cl *cl = cldev->cl;
192 return __mei_cl_send(cl, buf, length, 1);
194 EXPORT_SYMBOL_GPL(mei_cldev_send);
197 * mei_cldev_recv - client receive (read)
199 * @cldev: me client device
200 * @buf: buffer to receive
201 * @length: buffer length
203 * Return: read size in bytes of < 0 on error
205 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
207 struct mei_cl *cl = cldev->cl;
212 return __mei_cl_recv(cl, buf, length);
214 EXPORT_SYMBOL_GPL(mei_cldev_recv);
217 * mei_cl_bus_event_work - dispatch rx event for a bus device
218 * and schedule new work
222 static void mei_cl_bus_event_work(struct work_struct *work)
224 struct mei_cl_device *cldev;
225 struct mei_device *bus;
227 cldev = container_of(work, struct mei_cl_device, event_work);
232 cldev->event_cb(cldev, cldev->events, cldev->event_context);
236 /* Prepare for the next read */
237 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
238 mutex_lock(&bus->device_lock);
239 mei_cl_read_start(cldev->cl, 0, NULL);
240 mutex_unlock(&bus->device_lock);
245 * mei_cl_bus_notify_event - schedule notify cb on bus client
249 void mei_cl_bus_notify_event(struct mei_cl *cl)
251 struct mei_cl_device *cldev = cl->cldev;
253 if (!cldev || !cldev->event_cb)
256 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)))
262 set_bit(MEI_CL_EVENT_NOTIF, &cldev->events);
264 schedule_work(&cldev->event_work);
266 cl->notify_ev = false;
270 * mei_cl_bus_rx_event - schedule rx evenet
274 void mei_cl_bus_rx_event(struct mei_cl *cl)
276 struct mei_cl_device *cldev = cl->cldev;
278 if (!cldev || !cldev->event_cb)
281 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX)))
284 set_bit(MEI_CL_EVENT_RX, &cldev->events);
286 schedule_work(&cldev->event_work);
290 * mei_cldev_register_event_cb - register event callback
292 * @cldev: me client devices
293 * @event_cb: callback function
294 * @events_mask: requested events bitmask
295 * @context: driver context data
297 * Return: 0 on success
298 * -EALREADY if an callback is already registered
301 int mei_cldev_register_event_cb(struct mei_cl_device *cldev,
302 unsigned long events_mask,
303 mei_cldev_event_cb_t event_cb, void *context)
305 struct mei_device *bus = cldev->bus;
312 cldev->events_mask = events_mask;
313 cldev->event_cb = event_cb;
314 cldev->event_context = context;
315 INIT_WORK(&cldev->event_work, mei_cl_bus_event_work);
317 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
318 mutex_lock(&bus->device_lock);
319 ret = mei_cl_read_start(cldev->cl, 0, NULL);
320 mutex_unlock(&bus->device_lock);
321 if (ret && ret != -EBUSY)
325 if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) {
326 mutex_lock(&bus->device_lock);
327 ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0);
328 mutex_unlock(&bus->device_lock);
335 EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb);
338 * mei_cldev_get_drvdata - driver data getter
340 * @cldev: mei client device
342 * Return: driver private data
344 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
346 return dev_get_drvdata(&cldev->dev);
348 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
351 * mei_cldev_set_drvdata - driver data setter
353 * @cldev: mei client device
354 * @data: data to store
356 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
358 dev_set_drvdata(&cldev->dev, data);
360 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
363 * mei_cldev_uuid - return uuid of the underlying me client
365 * @cldev: mei client device
367 * Return: me client uuid
369 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
371 return mei_me_cl_uuid(cldev->me_cl);
373 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
376 * mei_cldev_ver - return protocol version of the underlying me client
378 * @cldev: mei client device
380 * Return: me client protocol version
382 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
384 return mei_me_cl_ver(cldev->me_cl);
386 EXPORT_SYMBOL_GPL(mei_cldev_ver);
389 * mei_cldev_enabled - check whether the device is enabled
391 * @cldev: mei client device
393 * Return: true if me client is initialized and connected
395 bool mei_cldev_enabled(struct mei_cl_device *cldev)
397 return cldev->cl && mei_cl_is_connected(cldev->cl);
399 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
402 * mei_cldev_enable - enable me client device
403 * create connection with me client
405 * @cldev: me client device
407 * Return: 0 on success and < 0 on error
409 int mei_cldev_enable(struct mei_cl_device *cldev)
411 struct mei_device *bus = cldev->bus;
418 mutex_lock(&bus->device_lock);
419 cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY);
420 mutex_unlock(&bus->device_lock);
423 /* update pointers */
428 mutex_lock(&bus->device_lock);
429 if (mei_cl_is_connected(cl)) {
434 if (!mei_me_cl_is_active(cldev->me_cl)) {
435 dev_err(&cldev->dev, "me client is not active\n");
440 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
442 dev_err(&cldev->dev, "cannot connect\n");
445 mutex_unlock(&bus->device_lock);
449 EXPORT_SYMBOL_GPL(mei_cldev_enable);
452 * mei_cldev_disable - disable me client device
453 * disconnect form the me client
455 * @cldev: me client device
457 * Return: 0 on success and < 0 on error
459 int mei_cldev_disable(struct mei_cl_device *cldev)
461 struct mei_device *bus;
465 if (!cldev || !cldev->cl)
472 cldev->event_cb = NULL;
474 mutex_lock(&bus->device_lock);
476 if (!mei_cl_is_connected(cl)) {
477 dev_err(bus->dev, "Already disconnected");
482 err = mei_cl_disconnect(cl);
484 dev_err(bus->dev, "Could not disconnect from the ME client");
487 /* Flush queues and remove any pending read */
488 mei_cl_flush_queues(cl, NULL);
494 mutex_unlock(&bus->device_lock);
497 EXPORT_SYMBOL_GPL(mei_cldev_disable);
500 * mei_cl_device_find - find matching entry in the driver id table
502 * @cldev: me client device
503 * @cldrv: me client driver
505 * Return: id on success; NULL if no id is matching
508 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
509 struct mei_cl_driver *cldrv)
511 const struct mei_cl_device_id *id;
516 uuid = mei_me_cl_uuid(cldev->me_cl);
517 version = mei_me_cl_ver(cldev->me_cl);
519 id = cldrv->id_table;
520 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
521 if (!uuid_le_cmp(*uuid, id->uuid)) {
525 if (strncmp(cldev->name, id->name,
529 if (id->version != MEI_CL_VERSION_ANY)
530 if (id->version != version)
543 * mei_cl_device_match - device match function
548 * Return: 1 if matching device was found 0 otherwise
550 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
552 struct mei_cl_device *cldev = to_mei_cl_device(dev);
553 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
554 const struct mei_cl_device_id *found_id;
559 if (!cldev->do_match)
562 if (!cldrv || !cldrv->id_table)
565 found_id = mei_cl_device_find(cldev, cldrv);
573 * mei_cl_device_probe - bus probe function
577 * Return: 0 on success; < 0 otherwise
579 static int mei_cl_device_probe(struct device *dev)
581 struct mei_cl_device *cldev;
582 struct mei_cl_driver *cldrv;
583 const struct mei_cl_device_id *id;
585 cldev = to_mei_cl_device(dev);
586 cldrv = to_mei_cl_driver(dev->driver);
591 if (!cldrv || !cldrv->probe)
594 id = mei_cl_device_find(cldev, cldrv);
598 __module_get(THIS_MODULE);
600 return cldrv->probe(cldev, id);
604 * mei_cl_device_remove - remove device from the bus
608 * Return: 0 on success; < 0 otherwise
610 static int mei_cl_device_remove(struct device *dev)
612 struct mei_cl_device *cldev = to_mei_cl_device(dev);
613 struct mei_cl_driver *cldrv;
616 if (!cldev || !dev->driver)
619 if (cldev->event_cb) {
620 cldev->event_cb = NULL;
621 cancel_work_sync(&cldev->event_work);
624 cldrv = to_mei_cl_driver(dev->driver);
626 ret = cldrv->remove(cldev);
628 module_put(THIS_MODULE);
634 static ssize_t name_show(struct device *dev, struct device_attribute *a,
637 struct mei_cl_device *cldev = to_mei_cl_device(dev);
640 len = snprintf(buf, PAGE_SIZE, "%s", cldev->name);
642 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
644 static DEVICE_ATTR_RO(name);
646 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
649 struct mei_cl_device *cldev = to_mei_cl_device(dev);
650 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
653 len = snprintf(buf, PAGE_SIZE, "%pUl", uuid);
655 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
657 static DEVICE_ATTR_RO(uuid);
659 static ssize_t version_show(struct device *dev, struct device_attribute *a,
662 struct mei_cl_device *cldev = to_mei_cl_device(dev);
663 u8 version = mei_me_cl_ver(cldev->me_cl);
666 len = snprintf(buf, PAGE_SIZE, "%02X", version);
668 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
670 static DEVICE_ATTR_RO(version);
672 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
675 struct mei_cl_device *cldev = to_mei_cl_device(dev);
676 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
679 len = snprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
680 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
682 static DEVICE_ATTR_RO(modalias);
684 static struct attribute *mei_cldev_attrs[] = {
687 &dev_attr_version.attr,
688 &dev_attr_modalias.attr,
691 ATTRIBUTE_GROUPS(mei_cldev);
694 * mei_cl_device_uevent - me client bus uevent handler
697 * @env: uevent kobject
699 * Return: 0 on success -ENOMEM on when add_uevent_var fails
701 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
703 struct mei_cl_device *cldev = to_mei_cl_device(dev);
704 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
705 u8 version = mei_me_cl_ver(cldev->me_cl);
707 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
710 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
713 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
716 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
717 cldev->name, uuid, version))
723 static struct bus_type mei_cl_bus_type = {
725 .dev_groups = mei_cldev_groups,
726 .match = mei_cl_device_match,
727 .probe = mei_cl_device_probe,
728 .remove = mei_cl_device_remove,
729 .uevent = mei_cl_device_uevent,
732 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
735 get_device(bus->dev);
740 static void mei_dev_bus_put(struct mei_device *bus)
743 put_device(bus->dev);
746 static void mei_cl_bus_dev_release(struct device *dev)
748 struct mei_cl_device *cldev = to_mei_cl_device(dev);
753 mei_me_cl_put(cldev->me_cl);
754 mei_dev_bus_put(cldev->bus);
758 static struct device_type mei_cl_device_type = {
759 .release = mei_cl_bus_dev_release,
763 * mei_cl_bus_set_name - set device name for me client device
765 * @cldev: me client device
767 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
769 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
771 mei_me_cl_uuid(cldev->me_cl),
772 mei_me_cl_ver(cldev->me_cl));
776 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
781 * Return: allocated device structur or NULL on allocation failure
783 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
784 struct mei_me_client *me_cl)
786 struct mei_cl_device *cldev;
788 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
792 device_initialize(&cldev->dev);
793 cldev->dev.parent = bus->dev;
794 cldev->dev.bus = &mei_cl_bus_type;
795 cldev->dev.type = &mei_cl_device_type;
796 cldev->bus = mei_dev_bus_get(bus);
797 cldev->me_cl = mei_me_cl_get(me_cl);
798 mei_cl_bus_set_name(cldev);
800 INIT_LIST_HEAD(&cldev->bus_list);
806 * mei_cl_dev_setup - setup me client device
807 * run fix up routines and set the device name
810 * @cldev: me client device
812 * Return: true if the device is eligible for enumeration
814 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
815 struct mei_cl_device *cldev)
818 mei_cl_bus_dev_fixup(cldev);
820 /* the device name can change during fix up */
822 mei_cl_bus_set_name(cldev);
824 return cldev->do_match == 1;
828 * mei_cl_bus_dev_add - add me client devices
830 * @cldev: me client device
832 * Return: 0 on success; < 0 on failre
834 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
838 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
839 mei_me_cl_uuid(cldev->me_cl),
840 mei_me_cl_ver(cldev->me_cl));
841 ret = device_add(&cldev->dev);
849 * mei_cl_bus_dev_stop - stop the driver
851 * @cldev: me client device
853 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
856 device_release_driver(&cldev->dev);
860 * mei_cl_bus_dev_destroy - destroy me client devices object
862 * @cldev: me client device
864 * Locking: called under "dev->cl_bus_lock" lock
866 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
869 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
871 if (!cldev->is_added)
874 device_del(&cldev->dev);
876 list_del_init(&cldev->bus_list);
879 put_device(&cldev->dev);
883 * mei_cl_bus_remove_device - remove a devices form the bus
885 * @cldev: me client device
887 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
889 mei_cl_bus_dev_stop(cldev);
890 mei_cl_bus_dev_destroy(cldev);
894 * mei_cl_bus_remove_devices - remove all devices form the bus
898 void mei_cl_bus_remove_devices(struct mei_device *bus)
900 struct mei_cl_device *cldev, *next;
902 mutex_lock(&bus->cl_bus_lock);
903 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
904 mei_cl_bus_remove_device(cldev);
905 mutex_unlock(&bus->cl_bus_lock);
910 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
916 * Locking: called under "dev->cl_bus_lock" lock
918 static void mei_cl_bus_dev_init(struct mei_device *bus,
919 struct mei_me_client *me_cl)
921 struct mei_cl_device *cldev;
923 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
925 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
927 if (me_cl->bus_added)
930 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
934 me_cl->bus_added = true;
935 list_add_tail(&cldev->bus_list, &bus->device_list);
940 * mei_cl_bus_rescan - scan me clients list and add create
941 * devices for eligible clients
945 void mei_cl_bus_rescan(struct mei_device *bus)
947 struct mei_cl_device *cldev, *n;
948 struct mei_me_client *me_cl;
950 mutex_lock(&bus->cl_bus_lock);
952 down_read(&bus->me_clients_rwsem);
953 list_for_each_entry(me_cl, &bus->me_clients, list)
954 mei_cl_bus_dev_init(bus, me_cl);
955 up_read(&bus->me_clients_rwsem);
957 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
959 if (!mei_me_cl_is_active(cldev->me_cl)) {
960 mei_cl_bus_remove_device(cldev);
967 if (mei_cl_bus_dev_setup(bus, cldev))
968 mei_cl_bus_dev_add(cldev);
970 list_del_init(&cldev->bus_list);
971 put_device(&cldev->dev);
974 mutex_unlock(&bus->cl_bus_lock);
976 dev_dbg(bus->dev, "rescan end");
979 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
980 struct module *owner)
984 cldrv->driver.name = cldrv->name;
985 cldrv->driver.owner = owner;
986 cldrv->driver.bus = &mei_cl_bus_type;
988 err = driver_register(&cldrv->driver);
992 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
996 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
998 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1000 driver_unregister(&cldrv->driver);
1002 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1004 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1007 int __init mei_cl_bus_init(void)
1009 return bus_register(&mei_cl_bus_type);
1012 void __exit mei_cl_bus_exit(void)
1014 bus_unregister(&mei_cl_bus_type);