Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / misc / mei / bus.c
blob2907db260fba5ead002155992ac513e0fca3104a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012-2019, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
5 */
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>
18 #include "mei_dev.h"
19 #include "client.h"
21 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
23 /**
24 * __mei_cl_send - internal client send (write)
26 * @cl: host client
27 * @buf: buffer to send
28 * @length: buffer length
29 * @vtag: virtual tag
30 * @mode: sending mode
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,
35 unsigned int mode)
37 struct mei_device *bus;
38 struct mei_cl_cb *cb;
39 ssize_t rets;
41 if (WARN_ON(!cl || !cl->dev))
42 return -ENODEV;
44 bus = cl->dev;
46 mutex_lock(&bus->device_lock);
47 if (bus->dev_state != MEI_DEV_ENABLED) {
48 rets = -ENODEV;
49 goto out;
52 if (!mei_cl_is_connected(cl)) {
53 rets = -ENODEV;
54 goto out;
57 /* Check if we have an ME client device */
58 if (!mei_me_cl_is_active(cl->me_cl)) {
59 rets = -ENOTTY;
60 goto out;
63 if (length > mei_cl_mtu(cl)) {
64 rets = -EFBIG;
65 goto out;
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);
74 if (rets) {
75 if (signal_pending(current))
76 rets = -EINTR;
77 goto out;
79 if (!mei_cl_is_connected(cl)) {
80 rets = -ENODEV;
81 goto out;
85 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
86 if (!cb) {
87 rets = -ENOMEM;
88 goto out;
90 cb->vtag = vtag;
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);
98 out:
99 mutex_unlock(&bus->device_lock);
101 return rets;
105 * __mei_cl_recv - internal client receive (read)
107 * @cl: host client
108 * @buf: buffer to receive
109 * @length: buffer length
110 * @mode: io mode
111 * @vtag: virtual tag
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;
121 size_t r_length;
122 ssize_t rets;
123 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
125 if (WARN_ON(!cl || !cl->dev))
126 return -ENODEV;
128 bus = cl->dev;
130 mutex_lock(&bus->device_lock);
131 if (bus->dev_state != MEI_DEV_ENABLED) {
132 rets = -ENODEV;
133 goto out;
136 cb = mei_cl_read_cb(cl, NULL);
137 if (cb)
138 goto copy;
140 rets = mei_cl_read_start(cl, length, NULL);
141 if (rets && rets != -EBUSY)
142 goto out;
144 if (nonblock) {
145 rets = -EAGAIN;
146 goto out;
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);
155 if (timeout) {
156 rets = wait_event_interruptible_timeout
157 (cl->rx_wait,
158 mei_cl_read_cb(cl, NULL) ||
159 (!mei_cl_is_connected(cl)),
160 msecs_to_jiffies(timeout));
161 if (rets == 0)
162 return -ETIME;
163 if (rets < 0) {
164 if (signal_pending(current))
165 return -EINTR;
166 return -ERESTARTSYS;
168 } else {
169 if (wait_event_interruptible
170 (cl->rx_wait,
171 mei_cl_read_cb(cl, NULL) ||
172 (!mei_cl_is_connected(cl)))) {
173 if (signal_pending(current))
174 return -EINTR;
175 return -ERESTARTSYS;
179 mutex_lock(&bus->device_lock);
181 if (!mei_cl_is_connected(cl)) {
182 rets = -ENODEV;
183 goto out;
187 cb = mei_cl_read_cb(cl, NULL);
188 if (!cb) {
189 rets = 0;
190 goto out;
193 copy:
194 if (cb->status) {
195 rets = cb->status;
196 goto free;
199 r_length = min_t(size_t, length, cb->buf_idx);
200 memcpy(buf, cb->buf.data, r_length);
201 rets = r_length;
202 if (vtag)
203 *vtag = cb->vtag;
205 free:
206 mei_cl_del_rd_completed(cl, cb);
207 out:
208 mutex_unlock(&bus->device_lock);
210 return rets;
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
219 * @vtag: virtual tag
221 * Return:
222 * * written size in bytes
223 * * < 0 on error
226 ssize_t mei_cldev_send_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length,
227 u8 vtag)
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
241 * @vtag: virtual tag
243 * Return:
244 * * read size in bytes
245 * * < 0 on error
248 ssize_t mei_cldev_recv_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length,
249 u8 *vtag)
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
263 * @vtag: virtual tag
265 * Return:
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
286 * Return:
287 * * written size in bytes
288 * * < 0 on error
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,
322 size_t length)
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
331 * @work: work
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);
340 bus = cldev->bus;
342 if (cldev->rx_cb)
343 cldev->rx_cb(cldev);
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
354 * @work: work
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);
362 if (cldev->notif_cb)
363 cldev->notif_cb(cldev);
367 * mei_cl_bus_notify_event - schedule notify cb on bus client
369 * @cl: host 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)
379 return false;
381 if (!cl->notify_ev)
382 return false;
384 schedule_work(&cldev->notif_work);
386 cl->notify_ev = false;
388 return true;
392 * mei_cl_bus_rx_event - schedule rx event
394 * @cl: host client
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)
404 return false;
406 schedule_work(&cldev->rx_work);
408 return true;
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
419 * <0 on other errors
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;
424 int ret;
426 if (!rx_cb)
427 return -EINVAL;
428 if (cldev->rx_cb)
429 return -EALREADY;
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);
437 else
438 ret = -ENODEV;
439 mutex_unlock(&bus->device_lock);
440 if (ret && ret != -EBUSY) {
441 cancel_work_sync(&cldev->rx_work);
442 cldev->rx_cb = NULL;
443 return ret;
446 return 0;
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
458 * <0 on other errors
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;
464 int ret;
466 if (!notif_cb)
467 return -EINVAL;
469 if (cldev->notif_cb)
470 return -EALREADY;
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);
478 if (ret) {
479 cancel_work_sync(&cldev->notif_work);
480 cldev->notif_cb = NULL;
481 return ret;
484 return 0;
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
554 * hw driver.
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.
579 * @cl: host client
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
594 * Return:
595 * * 0 on success
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))
608 return 0;
610 cl_vtag = mei_cl_vtag_alloc(NULL, 0);
611 if (IS_ERR(cl_vtag))
612 return -ENOMEM;
614 list_add_tail(&cl_vtag->list, &cl->vtag_map);
616 return 0;
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);
630 if (!cl_vtag)
631 return;
633 list_del(&cl_vtag->list);
634 kfree(cl_vtag);
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;
648 struct mei_cl *cl;
649 int ret;
651 cl = cldev->cl;
653 mutex_lock(&bus->device_lock);
654 if (cl->state == MEI_FILE_UNINITIALIZED) {
655 ret = mei_cl_link(cl);
656 if (ret)
657 goto out;
658 /* update pointers */
659 cl->cldev = cldev;
662 if (mei_cl_is_connected(cl)) {
663 ret = 0;
664 goto out;
667 if (!mei_me_cl_is_active(cldev->me_cl)) {
668 dev_err(&cldev->dev, "me client is not active\n");
669 ret = -ENOTTY;
670 goto out;
673 ret = mei_cl_bus_vtag_alloc(cldev);
674 if (ret)
675 goto out;
677 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
678 if (ret < 0) {
679 dev_err(&cldev->dev, "cannot connect\n");
680 mei_cl_bus_vtag_free(cldev);
683 out:
684 mutex_unlock(&bus->device_lock);
686 return ret;
688 EXPORT_SYMBOL_GPL(mei_cldev_enable);
691 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
692 * callbacks.
694 * @cldev: client device
696 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
698 if (cldev->rx_cb) {
699 cancel_work_sync(&cldev->rx_work);
700 cldev->rx_cb = NULL;
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;
720 struct mei_cl *cl;
721 int err;
723 if (!cldev)
724 return -ENODEV;
726 cl = cldev->cl;
728 bus = cldev->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");
738 err = 0;
739 goto out;
742 err = mei_cl_disconnect(cl);
743 if (err < 0)
744 dev_err(bus->dev, "Could not disconnect from the ME client\n");
746 out:
747 /* Flush queues and remove any pending read */
748 mei_cl_flush_queues(cl, NULL);
749 mei_cl_unlink(cl);
751 mutex_unlock(&bus->device_lock);
752 return err;
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
764 static const
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;
769 const uuid_le *uuid;
770 u8 version;
771 bool match;
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)) {
779 match = true;
781 if (cldev->name[0])
782 if (strncmp(cldev->name, id->name,
783 sizeof(id->name)))
784 match = false;
786 if (id->version != MEI_CL_VERSION_ANY)
787 if (id->version != version)
788 match = false;
789 if (match)
790 return id;
793 id++;
796 return NULL;
800 * mei_cl_device_match - device match function
802 * @dev: device
803 * @drv: driver
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;
813 if (!cldev)
814 return 0;
816 if (!cldev->do_match)
817 return 0;
819 if (!cldrv || !cldrv->id_table)
820 return 0;
822 found_id = mei_cl_device_find(cldev, cldrv);
823 if (found_id)
824 return 1;
826 return 0;
830 * mei_cl_device_probe - bus probe function
832 * @dev: device
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;
841 int ret;
843 cldev = to_mei_cl_device(dev);
844 cldrv = to_mei_cl_driver(dev->driver);
846 if (!cldev)
847 return 0;
849 if (!cldrv || !cldrv->probe)
850 return -ENODEV;
852 id = mei_cl_device_find(cldev, cldrv);
853 if (!id)
854 return -ENODEV;
856 if (!mei_cl_bus_module_get(cldev)) {
857 dev_err(&cldev->dev, "get hw module failed");
858 return -ENODEV;
861 ret = cldrv->probe(cldev, id);
862 if (ret) {
863 mei_cl_bus_module_put(cldev);
864 return ret;
867 __module_get(THIS_MODULE);
868 return 0;
872 * mei_cl_device_remove - remove device from the bus
874 * @dev: device
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;
882 int ret = 0;
884 if (!cldev || !dev->driver)
885 return 0;
887 cldrv = to_mei_cl_driver(dev->driver);
888 if (cldrv->remove)
889 ret = cldrv->remove(cldev);
891 mei_cldev_unregister_callbacks(cldev);
893 mei_cl_bus_module_put(cldev);
894 module_put(THIS_MODULE);
896 return ret;
899 static ssize_t name_show(struct device *dev, struct device_attribute *a,
900 char *buf)
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,
909 char *buf)
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,
919 char *buf)
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,
929 char *buf)
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,
941 char *buf)
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,
951 char *buf)
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,
961 char *buf)
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,
971 char *buf)
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[] = {
981 &dev_attr_name.attr,
982 &dev_attr_uuid.attr,
983 &dev_attr_version.attr,
984 &dev_attr_modalias.attr,
985 &dev_attr_max_conn.attr,
986 &dev_attr_fixed.attr,
987 &dev_attr_vtag.attr,
988 &dev_attr_max_len.attr,
989 NULL,
991 ATTRIBUTE_GROUPS(mei_cldev);
994 * mei_cl_device_uevent - me client bus uevent handler
996 * @dev: device
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))
1008 return -ENOMEM;
1010 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
1011 return -ENOMEM;
1013 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
1014 return -ENOMEM;
1016 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
1017 cldev->name, uuid, version))
1018 return -ENOMEM;
1020 return 0;
1023 static struct bus_type mei_cl_bus_type = {
1024 .name = "mei",
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)
1034 if (bus)
1035 get_device(bus->dev);
1037 return bus;
1040 static void mei_dev_bus_put(struct mei_device *bus)
1042 if (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);
1050 if (!cldev)
1051 return;
1053 mei_me_cl_put(cldev->me_cl);
1054 mei_dev_bus_put(cldev->bus);
1055 mei_cl_unlink(cldev->cl);
1056 kfree(cldev->cl);
1057 kfree(cldev);
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
1081 * @bus: mei device
1082 * @me_cl: me client
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;
1090 struct mei_cl *cl;
1092 cldev = kzalloc(sizeof(*cldev), GFP_KERNEL);
1093 if (!cldev)
1094 return NULL;
1096 cl = mei_cl_allocate(bus);
1097 if (!cl) {
1098 kfree(cldev);
1099 return NULL;
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);
1108 cldev->cl = cl;
1109 mei_cl_bus_set_name(cldev);
1110 cldev->is_added = 0;
1111 INIT_LIST_HEAD(&cldev->bus_list);
1113 return cldev;
1117 * mei_cl_bus_dev_setup - setup me client device
1118 * run fix up routines and set the device name
1120 * @bus: mei device
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)
1147 int ret;
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);
1153 if (!ret)
1154 cldev->is_added = 1;
1156 return ret;
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)
1183 return;
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
1207 * @bus: mei device
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
1224 * @bus: mei device
1225 * @me_cl: 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)
1239 return;
1241 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1242 if (!cldev)
1243 return;
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
1254 * @bus: mei device
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);
1272 continue;
1275 if (cldev->is_added)
1276 continue;
1278 if (mei_cl_bus_dev_setup(bus, cldev))
1279 mei_cl_bus_dev_add(cldev);
1280 else {
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)
1301 int err;
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);
1308 if (err)
1309 return err;
1311 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1313 return 0;
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);