mei: bus: fix hw module get/put balance
[linux-2.6/btrfs-unstable.git] / drivers / misc / mei / bus.c
blob13c6c9a2248a578e6527fd253f47d89f6514fdc7
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)
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 while (cl->tx_cb_queued >= bus->tx_queue_limit) {
78 mutex_unlock(&bus->device_lock);
79 rets = wait_event_interruptible(cl->tx_wait,
80 cl->writing_state == MEI_WRITE_COMPLETE ||
81 (!mei_cl_is_connected(cl)));
82 mutex_lock(&bus->device_lock);
83 if (rets) {
84 if (signal_pending(current))
85 rets = -EINTR;
86 goto out;
88 if (!mei_cl_is_connected(cl)) {
89 rets = -ENODEV;
90 goto out;
94 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
95 if (!cb) {
96 rets = -ENOMEM;
97 goto out;
100 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
101 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
102 memcpy(cb->buf.data, buf, length);
104 rets = mei_cl_write(cl, cb);
106 out:
107 mutex_unlock(&bus->device_lock);
109 return rets;
113 * __mei_cl_recv - internal client receive (read)
115 * @cl: host client
116 * @buf: buffer to receive
117 * @length: buffer length
118 * @mode: io mode
119 * @timeout: recv timeout, 0 for infinite timeout
121 * Return: read size in bytes of < 0 on error
123 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
124 unsigned int mode, unsigned long timeout)
126 struct mei_device *bus;
127 struct mei_cl_cb *cb;
128 size_t r_length;
129 ssize_t rets;
130 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
132 if (WARN_ON(!cl || !cl->dev))
133 return -ENODEV;
135 bus = cl->dev;
137 mutex_lock(&bus->device_lock);
138 if (bus->dev_state != MEI_DEV_ENABLED) {
139 rets = -ENODEV;
140 goto out;
143 cb = mei_cl_read_cb(cl, NULL);
144 if (cb)
145 goto copy;
147 rets = mei_cl_read_start(cl, length, NULL);
148 if (rets && rets != -EBUSY)
149 goto out;
151 if (nonblock) {
152 rets = -EAGAIN;
153 goto out;
156 /* wait on event only if there is no other waiter */
157 /* synchronized under device mutex */
158 if (!waitqueue_active(&cl->rx_wait)) {
160 mutex_unlock(&bus->device_lock);
162 if (timeout) {
163 rets = wait_event_interruptible_timeout
164 (cl->rx_wait,
165 (!list_empty(&cl->rd_completed)) ||
166 (!mei_cl_is_connected(cl)),
167 msecs_to_jiffies(timeout));
168 if (rets == 0)
169 return -ETIME;
170 if (rets < 0) {
171 if (signal_pending(current))
172 return -EINTR;
173 return -ERESTARTSYS;
175 } else {
176 if (wait_event_interruptible
177 (cl->rx_wait,
178 (!list_empty(&cl->rd_completed)) ||
179 (!mei_cl_is_connected(cl)))) {
180 if (signal_pending(current))
181 return -EINTR;
182 return -ERESTARTSYS;
186 mutex_lock(&bus->device_lock);
188 if (!mei_cl_is_connected(cl)) {
189 rets = -ENODEV;
190 goto out;
194 cb = mei_cl_read_cb(cl, NULL);
195 if (!cb) {
196 rets = 0;
197 goto out;
200 copy:
201 if (cb->status) {
202 rets = cb->status;
203 goto free;
206 r_length = min_t(size_t, length, cb->buf_idx);
207 memcpy(buf, cb->buf.data, r_length);
208 rets = r_length;
210 free:
211 mei_io_cb_free(cb);
212 out:
213 mutex_unlock(&bus->device_lock);
215 return rets;
219 * mei_cldev_send - me device send (write)
221 * @cldev: me client device
222 * @buf: buffer to send
223 * @length: buffer length
225 * Return: written size in bytes or < 0 on error
227 ssize_t mei_cldev_send(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, MEI_CL_IO_TX_BLOCKING);
233 EXPORT_SYMBOL_GPL(mei_cldev_send);
236 * mei_cldev_recv_nonblock - non block client receive (read)
238 * @cldev: me client device
239 * @buf: buffer to receive
240 * @length: buffer length
242 * Return: read size in bytes of < 0 on error
243 * -EAGAIN if function will block.
245 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
246 size_t length)
248 struct mei_cl *cl = cldev->cl;
250 return __mei_cl_recv(cl, buf, length, MEI_CL_IO_RX_NONBLOCK, 0);
252 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
255 * mei_cldev_recv - client receive (read)
257 * @cldev: me client device
258 * @buf: buffer to receive
259 * @length: buffer length
261 * Return: read size in bytes of < 0 on error
263 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
265 struct mei_cl *cl = cldev->cl;
267 return __mei_cl_recv(cl, buf, length, 0, 0);
269 EXPORT_SYMBOL_GPL(mei_cldev_recv);
272 * mei_cl_bus_rx_work - dispatch rx event for a bus device
274 * @work: work
276 static void mei_cl_bus_rx_work(struct work_struct *work)
278 struct mei_cl_device *cldev;
279 struct mei_device *bus;
281 cldev = container_of(work, struct mei_cl_device, rx_work);
283 bus = cldev->bus;
285 if (cldev->rx_cb)
286 cldev->rx_cb(cldev);
288 mutex_lock(&bus->device_lock);
289 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
290 mutex_unlock(&bus->device_lock);
294 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
296 * @work: work
298 static void mei_cl_bus_notif_work(struct work_struct *work)
300 struct mei_cl_device *cldev;
302 cldev = container_of(work, struct mei_cl_device, notif_work);
304 if (cldev->notif_cb)
305 cldev->notif_cb(cldev);
309 * mei_cl_bus_notify_event - schedule notify cb on bus client
311 * @cl: host client
313 * Return: true if event was scheduled
314 * false if the client is not waiting for event
316 bool mei_cl_bus_notify_event(struct mei_cl *cl)
318 struct mei_cl_device *cldev = cl->cldev;
320 if (!cldev || !cldev->notif_cb)
321 return false;
323 if (!cl->notify_ev)
324 return false;
326 schedule_work(&cldev->notif_work);
328 cl->notify_ev = false;
330 return true;
334 * mei_cl_bus_rx_event - schedule rx event
336 * @cl: host client
338 * Return: true if event was scheduled
339 * false if the client is not waiting for event
341 bool mei_cl_bus_rx_event(struct mei_cl *cl)
343 struct mei_cl_device *cldev = cl->cldev;
345 if (!cldev || !cldev->rx_cb)
346 return false;
348 schedule_work(&cldev->rx_work);
350 return true;
354 * mei_cldev_register_rx_cb - register Rx event callback
356 * @cldev: me client devices
357 * @rx_cb: callback function
359 * Return: 0 on success
360 * -EALREADY if an callback is already registered
361 * <0 on other errors
363 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
365 struct mei_device *bus = cldev->bus;
366 int ret;
368 if (!rx_cb)
369 return -EINVAL;
370 if (cldev->rx_cb)
371 return -EALREADY;
373 cldev->rx_cb = rx_cb;
374 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
376 mutex_lock(&bus->device_lock);
377 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
378 mutex_unlock(&bus->device_lock);
379 if (ret && ret != -EBUSY)
380 return ret;
382 return 0;
384 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
387 * mei_cldev_register_notif_cb - register FW notification event callback
389 * @cldev: me client devices
390 * @notif_cb: callback function
392 * Return: 0 on success
393 * -EALREADY if an callback is already registered
394 * <0 on other errors
396 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
397 mei_cldev_cb_t notif_cb)
399 struct mei_device *bus = cldev->bus;
400 int ret;
402 if (!notif_cb)
403 return -EINVAL;
405 if (cldev->notif_cb)
406 return -EALREADY;
408 cldev->notif_cb = notif_cb;
409 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
411 mutex_lock(&bus->device_lock);
412 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
413 mutex_unlock(&bus->device_lock);
414 if (ret)
415 return ret;
417 return 0;
419 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
422 * mei_cldev_get_drvdata - driver data getter
424 * @cldev: mei client device
426 * Return: driver private data
428 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
430 return dev_get_drvdata(&cldev->dev);
432 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
435 * mei_cldev_set_drvdata - driver data setter
437 * @cldev: mei client device
438 * @data: data to store
440 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
442 dev_set_drvdata(&cldev->dev, data);
444 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
447 * mei_cldev_uuid - return uuid of the underlying me client
449 * @cldev: mei client device
451 * Return: me client uuid
453 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
455 return mei_me_cl_uuid(cldev->me_cl);
457 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
460 * mei_cldev_ver - return protocol version of the underlying me client
462 * @cldev: mei client device
464 * Return: me client protocol version
466 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
468 return mei_me_cl_ver(cldev->me_cl);
470 EXPORT_SYMBOL_GPL(mei_cldev_ver);
473 * mei_cldev_enabled - check whether the device is enabled
475 * @cldev: mei client device
477 * Return: true if me client is initialized and connected
479 bool mei_cldev_enabled(struct mei_cl_device *cldev)
481 return mei_cl_is_connected(cldev->cl);
483 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
486 * mei_cl_bus_module_get - acquire module of the underlying
487 * hw driver.
489 * @cldev: mei client device
491 * Return: true on success; false if the module was removed.
493 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
495 return try_module_get(cldev->bus->dev->driver->owner);
499 * mei_cl_bus_module_put - release the underlying hw module.
501 * @cldev: mei client device
503 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
505 module_put(cldev->bus->dev->driver->owner);
509 * mei_cldev_enable - enable me client device
510 * create connection with me client
512 * @cldev: me client device
514 * Return: 0 on success and < 0 on error
516 int mei_cldev_enable(struct mei_cl_device *cldev)
518 struct mei_device *bus = cldev->bus;
519 struct mei_cl *cl;
520 int ret;
522 cl = cldev->cl;
524 if (cl->state == MEI_FILE_UNINITIALIZED) {
525 mutex_lock(&bus->device_lock);
526 ret = mei_cl_link(cl);
527 mutex_unlock(&bus->device_lock);
528 if (ret)
529 return ret;
530 /* update pointers */
531 cl->cldev = cldev;
534 mutex_lock(&bus->device_lock);
535 if (mei_cl_is_connected(cl)) {
536 ret = 0;
537 goto out;
540 if (!mei_me_cl_is_active(cldev->me_cl)) {
541 dev_err(&cldev->dev, "me client is not active\n");
542 ret = -ENOTTY;
543 goto out;
546 if (!mei_cl_bus_module_get(cldev)) {
547 dev_err(&cldev->dev, "get hw module failed");
548 ret = -ENODEV;
549 goto out;
552 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
553 if (ret < 0) {
554 dev_err(&cldev->dev, "cannot connect\n");
555 mei_cl_bus_module_put(cldev);
558 out:
559 mutex_unlock(&bus->device_lock);
561 return ret;
563 EXPORT_SYMBOL_GPL(mei_cldev_enable);
566 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
567 * callbacks.
569 * @cldev: client device
571 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
573 if (cldev->rx_cb) {
574 cancel_work_sync(&cldev->rx_work);
575 cldev->rx_cb = NULL;
578 if (cldev->notif_cb) {
579 cancel_work_sync(&cldev->notif_work);
580 cldev->notif_cb = NULL;
585 * mei_cldev_disable - disable me client device
586 * disconnect form the me client
588 * @cldev: me client device
590 * Return: 0 on success and < 0 on error
592 int mei_cldev_disable(struct mei_cl_device *cldev)
594 struct mei_device *bus;
595 struct mei_cl *cl;
596 int err;
598 if (!cldev)
599 return -ENODEV;
601 cl = cldev->cl;
603 bus = cldev->bus;
605 mei_cldev_unregister_callbacks(cldev);
607 mutex_lock(&bus->device_lock);
609 if (!mei_cl_is_connected(cl)) {
610 dev_dbg(bus->dev, "Already disconnected\n");
611 err = 0;
612 goto out;
615 err = mei_cl_disconnect(cl);
616 if (err < 0)
617 dev_err(bus->dev, "Could not disconnect from the ME client\n");
619 mei_cl_bus_module_put(cldev);
620 out:
621 /* Flush queues and remove any pending read */
622 mei_cl_flush_queues(cl, NULL);
623 mei_cl_unlink(cl);
625 mutex_unlock(&bus->device_lock);
626 return err;
628 EXPORT_SYMBOL_GPL(mei_cldev_disable);
631 * mei_cl_device_find - find matching entry in the driver id table
633 * @cldev: me client device
634 * @cldrv: me client driver
636 * Return: id on success; NULL if no id is matching
638 static const
639 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
640 struct mei_cl_driver *cldrv)
642 const struct mei_cl_device_id *id;
643 const uuid_le *uuid;
644 u8 version;
645 bool match;
647 uuid = mei_me_cl_uuid(cldev->me_cl);
648 version = mei_me_cl_ver(cldev->me_cl);
650 id = cldrv->id_table;
651 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
652 if (!uuid_le_cmp(*uuid, id->uuid)) {
653 match = true;
655 if (cldev->name[0])
656 if (strncmp(cldev->name, id->name,
657 sizeof(id->name)))
658 match = false;
660 if (id->version != MEI_CL_VERSION_ANY)
661 if (id->version != version)
662 match = false;
663 if (match)
664 return id;
667 id++;
670 return NULL;
674 * mei_cl_device_match - device match function
676 * @dev: device
677 * @drv: driver
679 * Return: 1 if matching device was found 0 otherwise
681 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
683 struct mei_cl_device *cldev = to_mei_cl_device(dev);
684 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
685 const struct mei_cl_device_id *found_id;
687 if (!cldev)
688 return 0;
690 if (!cldev->do_match)
691 return 0;
693 if (!cldrv || !cldrv->id_table)
694 return 0;
696 found_id = mei_cl_device_find(cldev, cldrv);
697 if (found_id)
698 return 1;
700 return 0;
704 * mei_cl_device_probe - bus probe function
706 * @dev: device
708 * Return: 0 on success; < 0 otherwise
710 static int mei_cl_device_probe(struct device *dev)
712 struct mei_cl_device *cldev;
713 struct mei_cl_driver *cldrv;
714 const struct mei_cl_device_id *id;
715 int ret;
717 cldev = to_mei_cl_device(dev);
718 cldrv = to_mei_cl_driver(dev->driver);
720 if (!cldev)
721 return 0;
723 if (!cldrv || !cldrv->probe)
724 return -ENODEV;
726 id = mei_cl_device_find(cldev, cldrv);
727 if (!id)
728 return -ENODEV;
730 ret = cldrv->probe(cldev, id);
731 if (ret)
732 return ret;
734 __module_get(THIS_MODULE);
735 return 0;
739 * mei_cl_device_remove - remove device from the bus
741 * @dev: device
743 * Return: 0 on success; < 0 otherwise
745 static int mei_cl_device_remove(struct device *dev)
747 struct mei_cl_device *cldev = to_mei_cl_device(dev);
748 struct mei_cl_driver *cldrv;
749 int ret = 0;
751 if (!cldev || !dev->driver)
752 return 0;
754 cldrv = to_mei_cl_driver(dev->driver);
755 if (cldrv->remove)
756 ret = cldrv->remove(cldev);
758 mei_cldev_unregister_callbacks(cldev);
760 module_put(THIS_MODULE);
761 dev->driver = NULL;
762 return ret;
766 static ssize_t name_show(struct device *dev, struct device_attribute *a,
767 char *buf)
769 struct mei_cl_device *cldev = to_mei_cl_device(dev);
771 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
773 static DEVICE_ATTR_RO(name);
775 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
776 char *buf)
778 struct mei_cl_device *cldev = to_mei_cl_device(dev);
779 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
781 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
783 static DEVICE_ATTR_RO(uuid);
785 static ssize_t version_show(struct device *dev, struct device_attribute *a,
786 char *buf)
788 struct mei_cl_device *cldev = to_mei_cl_device(dev);
789 u8 version = mei_me_cl_ver(cldev->me_cl);
791 return scnprintf(buf, PAGE_SIZE, "%02X", version);
793 static DEVICE_ATTR_RO(version);
795 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
796 char *buf)
798 struct mei_cl_device *cldev = to_mei_cl_device(dev);
799 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
800 u8 version = mei_me_cl_ver(cldev->me_cl);
802 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
803 cldev->name, uuid, version);
805 static DEVICE_ATTR_RO(modalias);
807 static struct attribute *mei_cldev_attrs[] = {
808 &dev_attr_name.attr,
809 &dev_attr_uuid.attr,
810 &dev_attr_version.attr,
811 &dev_attr_modalias.attr,
812 NULL,
814 ATTRIBUTE_GROUPS(mei_cldev);
817 * mei_cl_device_uevent - me client bus uevent handler
819 * @dev: device
820 * @env: uevent kobject
822 * Return: 0 on success -ENOMEM on when add_uevent_var fails
824 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
826 struct mei_cl_device *cldev = to_mei_cl_device(dev);
827 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
828 u8 version = mei_me_cl_ver(cldev->me_cl);
830 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
831 return -ENOMEM;
833 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
834 return -ENOMEM;
836 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
837 return -ENOMEM;
839 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
840 cldev->name, uuid, version))
841 return -ENOMEM;
843 return 0;
846 static struct bus_type mei_cl_bus_type = {
847 .name = "mei",
848 .dev_groups = mei_cldev_groups,
849 .match = mei_cl_device_match,
850 .probe = mei_cl_device_probe,
851 .remove = mei_cl_device_remove,
852 .uevent = mei_cl_device_uevent,
855 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
857 if (bus)
858 get_device(bus->dev);
860 return bus;
863 static void mei_dev_bus_put(struct mei_device *bus)
865 if (bus)
866 put_device(bus->dev);
869 static void mei_cl_bus_dev_release(struct device *dev)
871 struct mei_cl_device *cldev = to_mei_cl_device(dev);
873 if (!cldev)
874 return;
876 mei_me_cl_put(cldev->me_cl);
877 mei_dev_bus_put(cldev->bus);
878 kfree(cldev->cl);
879 kfree(cldev);
882 static const struct device_type mei_cl_device_type = {
883 .release = mei_cl_bus_dev_release,
887 * mei_cl_bus_set_name - set device name for me client device
889 * @cldev: me client device
891 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
893 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
894 cldev->name,
895 mei_me_cl_uuid(cldev->me_cl),
896 mei_me_cl_ver(cldev->me_cl));
900 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
902 * @bus: mei device
903 * @me_cl: me client
905 * Return: allocated device structur or NULL on allocation failure
907 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
908 struct mei_me_client *me_cl)
910 struct mei_cl_device *cldev;
911 struct mei_cl *cl;
913 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
914 if (!cldev)
915 return NULL;
917 cl = mei_cl_allocate(bus);
918 if (!cl) {
919 kfree(cldev);
920 return NULL;
923 device_initialize(&cldev->dev);
924 cldev->dev.parent = bus->dev;
925 cldev->dev.bus = &mei_cl_bus_type;
926 cldev->dev.type = &mei_cl_device_type;
927 cldev->bus = mei_dev_bus_get(bus);
928 cldev->me_cl = mei_me_cl_get(me_cl);
929 cldev->cl = cl;
930 mei_cl_bus_set_name(cldev);
931 cldev->is_added = 0;
932 INIT_LIST_HEAD(&cldev->bus_list);
934 return cldev;
938 * mei_cl_dev_setup - setup me client device
939 * run fix up routines and set the device name
941 * @bus: mei device
942 * @cldev: me client device
944 * Return: true if the device is eligible for enumeration
946 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
947 struct mei_cl_device *cldev)
949 cldev->do_match = 1;
950 mei_cl_bus_dev_fixup(cldev);
952 /* the device name can change during fix up */
953 if (cldev->do_match)
954 mei_cl_bus_set_name(cldev);
956 return cldev->do_match == 1;
960 * mei_cl_bus_dev_add - add me client devices
962 * @cldev: me client device
964 * Return: 0 on success; < 0 on failre
966 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
968 int ret;
970 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
971 mei_me_cl_uuid(cldev->me_cl),
972 mei_me_cl_ver(cldev->me_cl));
973 ret = device_add(&cldev->dev);
974 if (!ret)
975 cldev->is_added = 1;
977 return ret;
981 * mei_cl_bus_dev_stop - stop the driver
983 * @cldev: me client device
985 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
987 if (cldev->is_added)
988 device_release_driver(&cldev->dev);
992 * mei_cl_bus_dev_destroy - destroy me client devices object
994 * @cldev: me client device
996 * Locking: called under "dev->cl_bus_lock" lock
998 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
1001 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1003 if (!cldev->is_added)
1004 return;
1006 device_del(&cldev->dev);
1008 list_del_init(&cldev->bus_list);
1010 cldev->is_added = 0;
1011 put_device(&cldev->dev);
1015 * mei_cl_bus_remove_device - remove a devices form the bus
1017 * @cldev: me client device
1019 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1021 mei_cl_bus_dev_stop(cldev);
1022 mei_cl_bus_dev_destroy(cldev);
1026 * mei_cl_bus_remove_devices - remove all devices form the bus
1028 * @bus: mei device
1030 void mei_cl_bus_remove_devices(struct mei_device *bus)
1032 struct mei_cl_device *cldev, *next;
1034 mutex_lock(&bus->cl_bus_lock);
1035 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1036 mei_cl_bus_remove_device(cldev);
1037 mutex_unlock(&bus->cl_bus_lock);
1042 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1043 * based on me client
1045 * @bus: mei device
1046 * @me_cl: me client
1048 * Locking: called under "dev->cl_bus_lock" lock
1050 static void mei_cl_bus_dev_init(struct mei_device *bus,
1051 struct mei_me_client *me_cl)
1053 struct mei_cl_device *cldev;
1055 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1057 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1059 if (me_cl->bus_added)
1060 return;
1062 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1063 if (!cldev)
1064 return;
1066 me_cl->bus_added = true;
1067 list_add_tail(&cldev->bus_list, &bus->device_list);
1072 * mei_cl_bus_rescan - scan me clients list and add create
1073 * devices for eligible clients
1075 * @bus: mei device
1077 static void mei_cl_bus_rescan(struct mei_device *bus)
1079 struct mei_cl_device *cldev, *n;
1080 struct mei_me_client *me_cl;
1082 mutex_lock(&bus->cl_bus_lock);
1084 down_read(&bus->me_clients_rwsem);
1085 list_for_each_entry(me_cl, &bus->me_clients, list)
1086 mei_cl_bus_dev_init(bus, me_cl);
1087 up_read(&bus->me_clients_rwsem);
1089 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1091 if (!mei_me_cl_is_active(cldev->me_cl)) {
1092 mei_cl_bus_remove_device(cldev);
1093 continue;
1096 if (cldev->is_added)
1097 continue;
1099 if (mei_cl_bus_dev_setup(bus, cldev))
1100 mei_cl_bus_dev_add(cldev);
1101 else {
1102 list_del_init(&cldev->bus_list);
1103 put_device(&cldev->dev);
1106 mutex_unlock(&bus->cl_bus_lock);
1108 dev_dbg(bus->dev, "rescan end");
1111 void mei_cl_bus_rescan_work(struct work_struct *work)
1113 struct mei_device *bus =
1114 container_of(work, struct mei_device, bus_rescan_work);
1116 mei_cl_bus_rescan(bus);
1119 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1120 struct module *owner)
1122 int err;
1124 cldrv->driver.name = cldrv->name;
1125 cldrv->driver.owner = owner;
1126 cldrv->driver.bus = &mei_cl_bus_type;
1128 err = driver_register(&cldrv->driver);
1129 if (err)
1130 return err;
1132 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1134 return 0;
1136 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1138 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1140 driver_unregister(&cldrv->driver);
1142 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1144 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1147 int __init mei_cl_bus_init(void)
1149 return bus_register(&mei_cl_bus_type);
1152 void __exit mei_cl_bus_exit(void)
1154 bus_unregister(&mei_cl_bus_type);