Linux 5.1.15
[linux/fpc-iii.git] / drivers / misc / mei / bus.c
blob65bec998eb6ecd8a52e9b3e5fa9e860f7c9e55e4
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/signal.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)
32 /**
33 * __mei_cl_send - internal client send (write)
35 * @cl: host client
36 * @buf: buffer to send
37 * @length: buffer length
38 * @mode: sending mode
40 * Return: written size bytes or < 0 on error
42 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
43 unsigned int mode)
45 struct mei_device *bus;
46 struct mei_cl_cb *cb;
47 ssize_t rets;
49 if (WARN_ON(!cl || !cl->dev))
50 return -ENODEV;
52 bus = cl->dev;
54 mutex_lock(&bus->device_lock);
55 if (bus->dev_state != MEI_DEV_ENABLED) {
56 rets = -ENODEV;
57 goto out;
60 if (!mei_cl_is_connected(cl)) {
61 rets = -ENODEV;
62 goto out;
65 /* Check if we have an ME client device */
66 if (!mei_me_cl_is_active(cl->me_cl)) {
67 rets = -ENOTTY;
68 goto out;
71 if (length > mei_cl_mtu(cl)) {
72 rets = -EFBIG;
73 goto out;
76 while (cl->tx_cb_queued >= bus->tx_queue_limit) {
77 mutex_unlock(&bus->device_lock);
78 rets = wait_event_interruptible(cl->tx_wait,
79 cl->writing_state == MEI_WRITE_COMPLETE ||
80 (!mei_cl_is_connected(cl)));
81 mutex_lock(&bus->device_lock);
82 if (rets) {
83 if (signal_pending(current))
84 rets = -EINTR;
85 goto out;
87 if (!mei_cl_is_connected(cl)) {
88 rets = -ENODEV;
89 goto out;
93 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
94 if (!cb) {
95 rets = -ENOMEM;
96 goto out;
99 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
100 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
101 memcpy(cb->buf.data, buf, length);
103 rets = mei_cl_write(cl, cb);
105 out:
106 mutex_unlock(&bus->device_lock);
108 return rets;
112 * __mei_cl_recv - internal client receive (read)
114 * @cl: host client
115 * @buf: buffer to receive
116 * @length: buffer length
117 * @mode: io mode
118 * @timeout: recv timeout, 0 for infinite timeout
120 * Return: read size in bytes of < 0 on error
122 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
123 unsigned int mode, unsigned long timeout)
125 struct mei_device *bus;
126 struct mei_cl_cb *cb;
127 size_t r_length;
128 ssize_t rets;
129 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
131 if (WARN_ON(!cl || !cl->dev))
132 return -ENODEV;
134 bus = cl->dev;
136 mutex_lock(&bus->device_lock);
137 if (bus->dev_state != MEI_DEV_ENABLED) {
138 rets = -ENODEV;
139 goto out;
142 cb = mei_cl_read_cb(cl, NULL);
143 if (cb)
144 goto copy;
146 rets = mei_cl_read_start(cl, length, NULL);
147 if (rets && rets != -EBUSY)
148 goto out;
150 if (nonblock) {
151 rets = -EAGAIN;
152 goto out;
155 /* wait on event only if there is no other waiter */
156 /* synchronized under device mutex */
157 if (!waitqueue_active(&cl->rx_wait)) {
159 mutex_unlock(&bus->device_lock);
161 if (timeout) {
162 rets = wait_event_interruptible_timeout
163 (cl->rx_wait,
164 (!list_empty(&cl->rd_completed)) ||
165 (!mei_cl_is_connected(cl)),
166 msecs_to_jiffies(timeout));
167 if (rets == 0)
168 return -ETIME;
169 if (rets < 0) {
170 if (signal_pending(current))
171 return -EINTR;
172 return -ERESTARTSYS;
174 } else {
175 if (wait_event_interruptible
176 (cl->rx_wait,
177 (!list_empty(&cl->rd_completed)) ||
178 (!mei_cl_is_connected(cl)))) {
179 if (signal_pending(current))
180 return -EINTR;
181 return -ERESTARTSYS;
185 mutex_lock(&bus->device_lock);
187 if (!mei_cl_is_connected(cl)) {
188 rets = -ENODEV;
189 goto out;
193 cb = mei_cl_read_cb(cl, NULL);
194 if (!cb) {
195 rets = 0;
196 goto out;
199 copy:
200 if (cb->status) {
201 rets = cb->status;
202 goto free;
205 r_length = min_t(size_t, length, cb->buf_idx);
206 memcpy(buf, cb->buf.data, r_length);
207 rets = r_length;
209 free:
210 mei_io_cb_free(cb);
211 out:
212 mutex_unlock(&bus->device_lock);
214 return rets;
218 * mei_cldev_send - me device send (write)
220 * @cldev: me client device
221 * @buf: buffer to send
222 * @length: buffer length
224 * Return: written size in bytes or < 0 on error
226 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
228 struct mei_cl *cl = cldev->cl;
230 return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING);
232 EXPORT_SYMBOL_GPL(mei_cldev_send);
235 * mei_cldev_recv_nonblock - non block client receive (read)
237 * @cldev: me client device
238 * @buf: buffer to receive
239 * @length: buffer length
241 * Return: read size in bytes of < 0 on error
242 * -EAGAIN if function will block.
244 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
245 size_t length)
247 struct mei_cl *cl = cldev->cl;
249 return __mei_cl_recv(cl, buf, length, MEI_CL_IO_RX_NONBLOCK, 0);
251 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
254 * mei_cldev_recv - client receive (read)
256 * @cldev: me client device
257 * @buf: buffer to receive
258 * @length: buffer length
260 * Return: read size in bytes of < 0 on error
262 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
264 struct mei_cl *cl = cldev->cl;
266 return __mei_cl_recv(cl, buf, length, 0, 0);
268 EXPORT_SYMBOL_GPL(mei_cldev_recv);
271 * mei_cl_bus_rx_work - dispatch rx event for a bus device
273 * @work: work
275 static void mei_cl_bus_rx_work(struct work_struct *work)
277 struct mei_cl_device *cldev;
278 struct mei_device *bus;
280 cldev = container_of(work, struct mei_cl_device, rx_work);
282 bus = cldev->bus;
284 if (cldev->rx_cb)
285 cldev->rx_cb(cldev);
287 mutex_lock(&bus->device_lock);
288 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
289 mutex_unlock(&bus->device_lock);
293 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
295 * @work: work
297 static void mei_cl_bus_notif_work(struct work_struct *work)
299 struct mei_cl_device *cldev;
301 cldev = container_of(work, struct mei_cl_device, notif_work);
303 if (cldev->notif_cb)
304 cldev->notif_cb(cldev);
308 * mei_cl_bus_notify_event - schedule notify cb on bus client
310 * @cl: host client
312 * Return: true if event was scheduled
313 * false if the client is not waiting for event
315 bool mei_cl_bus_notify_event(struct mei_cl *cl)
317 struct mei_cl_device *cldev = cl->cldev;
319 if (!cldev || !cldev->notif_cb)
320 return false;
322 if (!cl->notify_ev)
323 return false;
325 schedule_work(&cldev->notif_work);
327 cl->notify_ev = false;
329 return true;
333 * mei_cl_bus_rx_event - schedule rx event
335 * @cl: host client
337 * Return: true if event was scheduled
338 * false if the client is not waiting for event
340 bool mei_cl_bus_rx_event(struct mei_cl *cl)
342 struct mei_cl_device *cldev = cl->cldev;
344 if (!cldev || !cldev->rx_cb)
345 return false;
347 schedule_work(&cldev->rx_work);
349 return true;
353 * mei_cldev_register_rx_cb - register Rx event callback
355 * @cldev: me client devices
356 * @rx_cb: callback function
358 * Return: 0 on success
359 * -EALREADY if an callback is already registered
360 * <0 on other errors
362 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
364 struct mei_device *bus = cldev->bus;
365 int ret;
367 if (!rx_cb)
368 return -EINVAL;
369 if (cldev->rx_cb)
370 return -EALREADY;
372 cldev->rx_cb = rx_cb;
373 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
375 mutex_lock(&bus->device_lock);
376 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
377 mutex_unlock(&bus->device_lock);
378 if (ret && ret != -EBUSY)
379 return ret;
381 return 0;
383 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
386 * mei_cldev_register_notif_cb - register FW notification event callback
388 * @cldev: me client devices
389 * @notif_cb: callback function
391 * Return: 0 on success
392 * -EALREADY if an callback is already registered
393 * <0 on other errors
395 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
396 mei_cldev_cb_t notif_cb)
398 struct mei_device *bus = cldev->bus;
399 int ret;
401 if (!notif_cb)
402 return -EINVAL;
404 if (cldev->notif_cb)
405 return -EALREADY;
407 cldev->notif_cb = notif_cb;
408 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
410 mutex_lock(&bus->device_lock);
411 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
412 mutex_unlock(&bus->device_lock);
413 if (ret)
414 return ret;
416 return 0;
418 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
421 * mei_cldev_get_drvdata - driver data getter
423 * @cldev: mei client device
425 * Return: driver private data
427 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
429 return dev_get_drvdata(&cldev->dev);
431 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
434 * mei_cldev_set_drvdata - driver data setter
436 * @cldev: mei client device
437 * @data: data to store
439 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
441 dev_set_drvdata(&cldev->dev, data);
443 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
446 * mei_cldev_uuid - return uuid of the underlying me client
448 * @cldev: mei client device
450 * Return: me client uuid
452 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
454 return mei_me_cl_uuid(cldev->me_cl);
456 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
459 * mei_cldev_ver - return protocol version of the underlying me client
461 * @cldev: mei client device
463 * Return: me client protocol version
465 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
467 return mei_me_cl_ver(cldev->me_cl);
469 EXPORT_SYMBOL_GPL(mei_cldev_ver);
472 * mei_cldev_enabled - check whether the device is enabled
474 * @cldev: mei client device
476 * Return: true if me client is initialized and connected
478 bool mei_cldev_enabled(struct mei_cl_device *cldev)
480 return mei_cl_is_connected(cldev->cl);
482 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
485 * mei_cl_bus_module_get - acquire module of the underlying
486 * hw driver.
488 * @cldev: mei client device
490 * Return: true on success; false if the module was removed.
492 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
494 return try_module_get(cldev->bus->dev->driver->owner);
498 * mei_cl_bus_module_put - release the underlying hw module.
500 * @cldev: mei client device
502 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
504 module_put(cldev->bus->dev->driver->owner);
508 * mei_cldev_enable - enable me client device
509 * create connection with me client
511 * @cldev: me client device
513 * Return: 0 on success and < 0 on error
515 int mei_cldev_enable(struct mei_cl_device *cldev)
517 struct mei_device *bus = cldev->bus;
518 struct mei_cl *cl;
519 int ret;
521 cl = cldev->cl;
523 mutex_lock(&bus->device_lock);
524 if (cl->state == MEI_FILE_UNINITIALIZED) {
525 ret = mei_cl_link(cl);
526 if (ret)
527 goto out;
528 /* update pointers */
529 cl->cldev = cldev;
532 if (mei_cl_is_connected(cl)) {
533 ret = 0;
534 goto out;
537 if (!mei_me_cl_is_active(cldev->me_cl)) {
538 dev_err(&cldev->dev, "me client is not active\n");
539 ret = -ENOTTY;
540 goto out;
543 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
544 if (ret < 0)
545 dev_err(&cldev->dev, "cannot connect\n");
547 out:
548 mutex_unlock(&bus->device_lock);
550 return ret;
552 EXPORT_SYMBOL_GPL(mei_cldev_enable);
555 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
556 * callbacks.
558 * @cldev: client device
560 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
562 if (cldev->rx_cb) {
563 cancel_work_sync(&cldev->rx_work);
564 cldev->rx_cb = NULL;
567 if (cldev->notif_cb) {
568 cancel_work_sync(&cldev->notif_work);
569 cldev->notif_cb = NULL;
574 * mei_cldev_disable - disable me client device
575 * disconnect form the me client
577 * @cldev: me client device
579 * Return: 0 on success and < 0 on error
581 int mei_cldev_disable(struct mei_cl_device *cldev)
583 struct mei_device *bus;
584 struct mei_cl *cl;
585 int err;
587 if (!cldev)
588 return -ENODEV;
590 cl = cldev->cl;
592 bus = cldev->bus;
594 mei_cldev_unregister_callbacks(cldev);
596 mutex_lock(&bus->device_lock);
598 if (!mei_cl_is_connected(cl)) {
599 dev_dbg(bus->dev, "Already disconnected\n");
600 err = 0;
601 goto out;
604 err = mei_cl_disconnect(cl);
605 if (err < 0)
606 dev_err(bus->dev, "Could not disconnect from the ME client\n");
608 out:
609 /* Flush queues and remove any pending read */
610 mei_cl_flush_queues(cl, NULL);
611 mei_cl_unlink(cl);
613 mutex_unlock(&bus->device_lock);
614 return err;
616 EXPORT_SYMBOL_GPL(mei_cldev_disable);
619 * mei_cl_device_find - find matching entry in the driver id table
621 * @cldev: me client device
622 * @cldrv: me client driver
624 * Return: id on success; NULL if no id is matching
626 static const
627 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
628 struct mei_cl_driver *cldrv)
630 const struct mei_cl_device_id *id;
631 const uuid_le *uuid;
632 u8 version;
633 bool match;
635 uuid = mei_me_cl_uuid(cldev->me_cl);
636 version = mei_me_cl_ver(cldev->me_cl);
638 id = cldrv->id_table;
639 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
640 if (!uuid_le_cmp(*uuid, id->uuid)) {
641 match = true;
643 if (cldev->name[0])
644 if (strncmp(cldev->name, id->name,
645 sizeof(id->name)))
646 match = false;
648 if (id->version != MEI_CL_VERSION_ANY)
649 if (id->version != version)
650 match = false;
651 if (match)
652 return id;
655 id++;
658 return NULL;
662 * mei_cl_device_match - device match function
664 * @dev: device
665 * @drv: driver
667 * Return: 1 if matching device was found 0 otherwise
669 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
671 struct mei_cl_device *cldev = to_mei_cl_device(dev);
672 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
673 const struct mei_cl_device_id *found_id;
675 if (!cldev)
676 return 0;
678 if (!cldev->do_match)
679 return 0;
681 if (!cldrv || !cldrv->id_table)
682 return 0;
684 found_id = mei_cl_device_find(cldev, cldrv);
685 if (found_id)
686 return 1;
688 return 0;
692 * mei_cl_device_probe - bus probe function
694 * @dev: device
696 * Return: 0 on success; < 0 otherwise
698 static int mei_cl_device_probe(struct device *dev)
700 struct mei_cl_device *cldev;
701 struct mei_cl_driver *cldrv;
702 const struct mei_cl_device_id *id;
703 int ret;
705 cldev = to_mei_cl_device(dev);
706 cldrv = to_mei_cl_driver(dev->driver);
708 if (!cldev)
709 return 0;
711 if (!cldrv || !cldrv->probe)
712 return -ENODEV;
714 id = mei_cl_device_find(cldev, cldrv);
715 if (!id)
716 return -ENODEV;
718 if (!mei_cl_bus_module_get(cldev)) {
719 dev_err(&cldev->dev, "get hw module failed");
720 return -ENODEV;
723 ret = cldrv->probe(cldev, id);
724 if (ret) {
725 mei_cl_bus_module_put(cldev);
726 return ret;
729 __module_get(THIS_MODULE);
730 return 0;
734 * mei_cl_device_remove - remove device from the bus
736 * @dev: device
738 * Return: 0 on success; < 0 otherwise
740 static int mei_cl_device_remove(struct device *dev)
742 struct mei_cl_device *cldev = to_mei_cl_device(dev);
743 struct mei_cl_driver *cldrv;
744 int ret = 0;
746 if (!cldev || !dev->driver)
747 return 0;
749 cldrv = to_mei_cl_driver(dev->driver);
750 if (cldrv->remove)
751 ret = cldrv->remove(cldev);
753 mei_cldev_unregister_callbacks(cldev);
755 mei_cl_bus_module_put(cldev);
756 module_put(THIS_MODULE);
757 dev->driver = NULL;
758 return ret;
762 static ssize_t name_show(struct device *dev, struct device_attribute *a,
763 char *buf)
765 struct mei_cl_device *cldev = to_mei_cl_device(dev);
767 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
769 static DEVICE_ATTR_RO(name);
771 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
772 char *buf)
774 struct mei_cl_device *cldev = to_mei_cl_device(dev);
775 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
777 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
779 static DEVICE_ATTR_RO(uuid);
781 static ssize_t version_show(struct device *dev, struct device_attribute *a,
782 char *buf)
784 struct mei_cl_device *cldev = to_mei_cl_device(dev);
785 u8 version = mei_me_cl_ver(cldev->me_cl);
787 return scnprintf(buf, PAGE_SIZE, "%02X", version);
789 static DEVICE_ATTR_RO(version);
791 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
792 char *buf)
794 struct mei_cl_device *cldev = to_mei_cl_device(dev);
795 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
796 u8 version = mei_me_cl_ver(cldev->me_cl);
798 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
799 cldev->name, uuid, version);
801 static DEVICE_ATTR_RO(modalias);
803 static struct attribute *mei_cldev_attrs[] = {
804 &dev_attr_name.attr,
805 &dev_attr_uuid.attr,
806 &dev_attr_version.attr,
807 &dev_attr_modalias.attr,
808 NULL,
810 ATTRIBUTE_GROUPS(mei_cldev);
813 * mei_cl_device_uevent - me client bus uevent handler
815 * @dev: device
816 * @env: uevent kobject
818 * Return: 0 on success -ENOMEM on when add_uevent_var fails
820 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
822 struct mei_cl_device *cldev = to_mei_cl_device(dev);
823 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
824 u8 version = mei_me_cl_ver(cldev->me_cl);
826 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
827 return -ENOMEM;
829 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
830 return -ENOMEM;
832 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
833 return -ENOMEM;
835 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
836 cldev->name, uuid, version))
837 return -ENOMEM;
839 return 0;
842 static struct bus_type mei_cl_bus_type = {
843 .name = "mei",
844 .dev_groups = mei_cldev_groups,
845 .match = mei_cl_device_match,
846 .probe = mei_cl_device_probe,
847 .remove = mei_cl_device_remove,
848 .uevent = mei_cl_device_uevent,
851 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
853 if (bus)
854 get_device(bus->dev);
856 return bus;
859 static void mei_dev_bus_put(struct mei_device *bus)
861 if (bus)
862 put_device(bus->dev);
865 static void mei_cl_bus_dev_release(struct device *dev)
867 struct mei_cl_device *cldev = to_mei_cl_device(dev);
869 if (!cldev)
870 return;
872 mei_me_cl_put(cldev->me_cl);
873 mei_dev_bus_put(cldev->bus);
874 mei_cl_unlink(cldev->cl);
875 kfree(cldev->cl);
876 kfree(cldev);
879 static const struct device_type mei_cl_device_type = {
880 .release = mei_cl_bus_dev_release,
884 * mei_cl_bus_set_name - set device name for me client device
886 * @cldev: me client device
888 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
890 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
891 cldev->name,
892 mei_me_cl_uuid(cldev->me_cl),
893 mei_me_cl_ver(cldev->me_cl));
897 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
899 * @bus: mei device
900 * @me_cl: me client
902 * Return: allocated device structur or NULL on allocation failure
904 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
905 struct mei_me_client *me_cl)
907 struct mei_cl_device *cldev;
908 struct mei_cl *cl;
910 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
911 if (!cldev)
912 return NULL;
914 cl = mei_cl_allocate(bus);
915 if (!cl) {
916 kfree(cldev);
917 return NULL;
920 device_initialize(&cldev->dev);
921 cldev->dev.parent = bus->dev;
922 cldev->dev.bus = &mei_cl_bus_type;
923 cldev->dev.type = &mei_cl_device_type;
924 cldev->bus = mei_dev_bus_get(bus);
925 cldev->me_cl = mei_me_cl_get(me_cl);
926 cldev->cl = cl;
927 mei_cl_bus_set_name(cldev);
928 cldev->is_added = 0;
929 INIT_LIST_HEAD(&cldev->bus_list);
931 return cldev;
935 * mei_cl_dev_setup - setup me client device
936 * run fix up routines and set the device name
938 * @bus: mei device
939 * @cldev: me client device
941 * Return: true if the device is eligible for enumeration
943 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
944 struct mei_cl_device *cldev)
946 cldev->do_match = 1;
947 mei_cl_bus_dev_fixup(cldev);
949 /* the device name can change during fix up */
950 if (cldev->do_match)
951 mei_cl_bus_set_name(cldev);
953 return cldev->do_match == 1;
957 * mei_cl_bus_dev_add - add me client devices
959 * @cldev: me client device
961 * Return: 0 on success; < 0 on failre
963 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
965 int ret;
967 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
968 mei_me_cl_uuid(cldev->me_cl),
969 mei_me_cl_ver(cldev->me_cl));
970 ret = device_add(&cldev->dev);
971 if (!ret)
972 cldev->is_added = 1;
974 return ret;
978 * mei_cl_bus_dev_stop - stop the driver
980 * @cldev: me client device
982 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
984 if (cldev->is_added)
985 device_release_driver(&cldev->dev);
989 * mei_cl_bus_dev_destroy - destroy me client devices object
991 * @cldev: me client device
993 * Locking: called under "dev->cl_bus_lock" lock
995 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
998 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1000 if (!cldev->is_added)
1001 return;
1003 device_del(&cldev->dev);
1005 list_del_init(&cldev->bus_list);
1007 cldev->is_added = 0;
1008 put_device(&cldev->dev);
1012 * mei_cl_bus_remove_device - remove a devices form the bus
1014 * @cldev: me client device
1016 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1018 mei_cl_bus_dev_stop(cldev);
1019 mei_cl_bus_dev_destroy(cldev);
1023 * mei_cl_bus_remove_devices - remove all devices form the bus
1025 * @bus: mei device
1027 void mei_cl_bus_remove_devices(struct mei_device *bus)
1029 struct mei_cl_device *cldev, *next;
1031 mutex_lock(&bus->cl_bus_lock);
1032 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1033 mei_cl_bus_remove_device(cldev);
1034 mutex_unlock(&bus->cl_bus_lock);
1039 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1040 * based on me client
1042 * @bus: mei device
1043 * @me_cl: me client
1045 * Locking: called under "dev->cl_bus_lock" lock
1047 static void mei_cl_bus_dev_init(struct mei_device *bus,
1048 struct mei_me_client *me_cl)
1050 struct mei_cl_device *cldev;
1052 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1054 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1056 if (me_cl->bus_added)
1057 return;
1059 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1060 if (!cldev)
1061 return;
1063 me_cl->bus_added = true;
1064 list_add_tail(&cldev->bus_list, &bus->device_list);
1069 * mei_cl_bus_rescan - scan me clients list and add create
1070 * devices for eligible clients
1072 * @bus: mei device
1074 static void mei_cl_bus_rescan(struct mei_device *bus)
1076 struct mei_cl_device *cldev, *n;
1077 struct mei_me_client *me_cl;
1079 mutex_lock(&bus->cl_bus_lock);
1081 down_read(&bus->me_clients_rwsem);
1082 list_for_each_entry(me_cl, &bus->me_clients, list)
1083 mei_cl_bus_dev_init(bus, me_cl);
1084 up_read(&bus->me_clients_rwsem);
1086 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1088 if (!mei_me_cl_is_active(cldev->me_cl)) {
1089 mei_cl_bus_remove_device(cldev);
1090 continue;
1093 if (cldev->is_added)
1094 continue;
1096 if (mei_cl_bus_dev_setup(bus, cldev))
1097 mei_cl_bus_dev_add(cldev);
1098 else {
1099 list_del_init(&cldev->bus_list);
1100 put_device(&cldev->dev);
1103 mutex_unlock(&bus->cl_bus_lock);
1105 dev_dbg(bus->dev, "rescan end");
1108 void mei_cl_bus_rescan_work(struct work_struct *work)
1110 struct mei_device *bus =
1111 container_of(work, struct mei_device, bus_rescan_work);
1113 mei_cl_bus_rescan(bus);
1116 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1117 struct module *owner)
1119 int err;
1121 cldrv->driver.name = cldrv->name;
1122 cldrv->driver.owner = owner;
1123 cldrv->driver.bus = &mei_cl_bus_type;
1125 err = driver_register(&cldrv->driver);
1126 if (err)
1127 return err;
1129 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1131 return 0;
1133 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1135 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1137 driver_unregister(&cldrv->driver);
1139 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1141 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1144 int __init mei_cl_bus_init(void)
1146 return bus_register(&mei_cl_bus_type);
1149 void __exit mei_cl_bus_exit(void)
1151 bus_unregister(&mei_cl_bus_type);