Merge tag 'armsoc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[linux-2.6-block.git] / drivers / misc / mei / bus.c
CommitLineData
e5354107
SO
1/*
2 * Intel Management Engine Interface (Intel MEI) Linux driver
3 * Copyright (c) 2012-2013, Intel Corporation.
4 *
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.
8 *
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.
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/device.h>
18#include <linux/kernel.h>
174cd4b1 19#include <linux/sched/signal.h>
e5354107
SO
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>
e5354107
SO
25#include <linux/mei_cl_bus.h>
26
27#include "mei_dev.h"
3e833295 28#include "client.h"
e5354107
SO
29
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)
32
62382997
TW
33/**
34 * __mei_cl_send - internal client send (write)
35 *
36 * @cl: host client
37 * @buf: buffer to send
38 * @length: buffer length
e0cb6b2f 39 * @mode: sending mode
62382997
TW
40 *
41 * Return: written size bytes or < 0 on error
42 */
43ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
e0cb6b2f 44 unsigned int mode)
e5354107 45{
62382997 46 struct mei_device *bus;
6cbb097f 47 struct mei_cl_cb *cb;
62382997 48 ssize_t rets;
e5354107 49
62382997
TW
50 if (WARN_ON(!cl || !cl->dev))
51 return -ENODEV;
c93b76b3 52
62382997 53 bus = cl->dev;
e5354107 54
62382997 55 mutex_lock(&bus->device_lock);
15c13dfc
AU
56 if (bus->dev_state != MEI_DEV_ENABLED) {
57 rets = -ENODEV;
58 goto out;
59 }
60
62382997
TW
61 if (!mei_cl_is_connected(cl)) {
62 rets = -ENODEV;
63 goto out;
64 }
e5354107 65
62382997
TW
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;
70 }
c93b76b3 71
62382997
TW
72 if (length > mei_cl_mtu(cl)) {
73 rets = -EFBIG;
74 goto out;
75 }
e5354107 76
62382997
TW
77 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
78 if (!cb) {
79 rets = -ENOMEM;
80 goto out;
e5354107
SO
81 }
82
e0cb6b2f
AU
83 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
84 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
62382997
TW
85 memcpy(cb->buf.data, buf, length);
86
e0cb6b2f 87 rets = mei_cl_write(cl, cb);
62382997
TW
88
89out:
90 mutex_unlock(&bus->device_lock);
62382997
TW
91
92 return rets;
e5354107
SO
93}
94
62382997
TW
95/**
96 * __mei_cl_recv - internal client receive (read)
97 *
98 * @cl: host client
df7f5447 99 * @buf: buffer to receive
62382997 100 * @length: buffer length
076802d0 101 * @mode: io mode
62382997
TW
102 *
103 * Return: read size in bytes of < 0 on error
104 */
076802d0
AU
105ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
106 unsigned int mode)
e5354107 107{
62382997
TW
108 struct mei_device *bus;
109 struct mei_cl_cb *cb;
110 size_t r_length;
111 ssize_t rets;
076802d0 112 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
e5354107 113
62382997 114 if (WARN_ON(!cl || !cl->dev))
e5354107
SO
115 return -ENODEV;
116
62382997 117 bus = cl->dev;
e5354107 118
62382997 119 mutex_lock(&bus->device_lock);
15c13dfc
AU
120 if (bus->dev_state != MEI_DEV_ENABLED) {
121 rets = -ENODEV;
122 goto out;
123 }
e5354107 124
62382997
TW
125 cb = mei_cl_read_cb(cl, NULL);
126 if (cb)
127 goto copy;
e5354107 128
62382997
TW
129 rets = mei_cl_read_start(cl, length, NULL);
130 if (rets && rets != -EBUSY)
131 goto out;
e5354107 132
076802d0
AU
133 if (nonblock) {
134 rets = -EAGAIN;
135 goto out;
136 }
137
62382997 138 /* wait on event only if there is no other waiter */
1eb5bd4d
AU
139 /* synchronized under device mutex */
140 if (!waitqueue_active(&cl->rx_wait)) {
e5354107 141
62382997 142 mutex_unlock(&bus->device_lock);
3e833295 143
62382997
TW
144 if (wait_event_interruptible(cl->rx_wait,
145 (!list_empty(&cl->rd_completed)) ||
146 (!mei_cl_is_connected(cl)))) {
e5354107 147
62382997
TW
148 if (signal_pending(current))
149 return -EINTR;
150 return -ERESTARTSYS;
151 }
152
153 mutex_lock(&bus->device_lock);
154
155 if (!mei_cl_is_connected(cl)) {
2d4d5481 156 rets = -ENODEV;
62382997
TW
157 goto out;
158 }
e5354107
SO
159 }
160
62382997
TW
161 cb = mei_cl_read_cb(cl, NULL);
162 if (!cb) {
163 rets = 0;
164 goto out;
165 }
e5354107 166
62382997
TW
167copy:
168 if (cb->status) {
169 rets = cb->status;
170 goto free;
171 }
007d64eb 172
62382997
TW
173 r_length = min_t(size_t, length, cb->buf_idx);
174 memcpy(buf, cb->buf.data, r_length);
175 rets = r_length;
007d64eb 176
62382997
TW
177free:
178 mei_io_cb_free(cb);
179out:
180 mutex_unlock(&bus->device_lock);
181
182 return rets;
007d64eb 183}
007d64eb 184
62382997 185/**
d49dc5e7 186 * mei_cldev_send - me device send (write)
62382997
TW
187 *
188 * @cldev: me client device
189 * @buf: buffer to send
190 * @length: buffer length
191 *
192 * Return: written size in bytes or < 0 on error
193 */
d49dc5e7 194ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
007d64eb 195{
62382997 196 struct mei_cl *cl = cldev->cl;
007d64eb 197
e0cb6b2f 198 return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING);
007d64eb 199}
d49dc5e7 200EXPORT_SYMBOL_GPL(mei_cldev_send);
007d64eb 201
076802d0
AU
202/**
203 * mei_cldev_recv_nonblock - non block client receive (read)
204 *
205 * @cldev: me client device
206 * @buf: buffer to receive
207 * @length: buffer length
208 *
209 * Return: read size in bytes of < 0 on error
210 * -EAGAIN if function will block.
211 */
212ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
213 size_t length)
214{
215 struct mei_cl *cl = cldev->cl;
216
217 return __mei_cl_recv(cl, buf, length, MEI_CL_IO_RX_NONBLOCK);
218}
219EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
220
62382997 221/**
d49dc5e7 222 * mei_cldev_recv - client receive (read)
62382997
TW
223 *
224 * @cldev: me client device
df7f5447 225 * @buf: buffer to receive
62382997
TW
226 * @length: buffer length
227 *
228 * Return: read size in bytes of < 0 on error
229 */
d49dc5e7 230ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
e5354107 231{
62382997 232 struct mei_cl *cl = cldev->cl;
e5354107 233
076802d0 234 return __mei_cl_recv(cl, buf, length, 0);
e5354107 235}
d49dc5e7 236EXPORT_SYMBOL_GPL(mei_cldev_recv);
e5354107 237
62382997 238/**
7c7a6077 239 * mei_cl_bus_rx_work - dispatch rx event for a bus device
62382997
TW
240 *
241 * @work: work
242 */
7c7a6077 243static void mei_cl_bus_rx_work(struct work_struct *work)
e5354107 244{
62382997 245 struct mei_cl_device *cldev;
bc46b45a 246 struct mei_device *bus;
c93b76b3 247
7c7a6077 248 cldev = container_of(work, struct mei_cl_device, rx_work);
007d64eb 249
bc46b45a
AU
250 bus = cldev->bus;
251
7c7a6077
AU
252 if (cldev->rx_cb)
253 cldev->rx_cb(cldev);
007d64eb 254
7c7a6077
AU
255 mutex_lock(&bus->device_lock);
256 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
257 mutex_unlock(&bus->device_lock);
258}
e5354107 259
7c7a6077
AU
260/**
261 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
262 *
263 * @work: work
264 */
265static void mei_cl_bus_notif_work(struct work_struct *work)
266{
267 struct mei_cl_device *cldev;
268
269 cldev = container_of(work, struct mei_cl_device, notif_work);
270
271 if (cldev->notif_cb)
272 cldev->notif_cb(cldev);
bb2ef9c3
AU
273}
274
275/**
276 * mei_cl_bus_notify_event - schedule notify cb on bus client
277 *
278 * @cl: host client
850f8940
TW
279 *
280 * Return: true if event was scheduled
281 * false if the client is not waiting for event
bb2ef9c3 282 */
850f8940 283bool mei_cl_bus_notify_event(struct mei_cl *cl)
bb2ef9c3
AU
284{
285 struct mei_cl_device *cldev = cl->cldev;
286
7c7a6077 287 if (!cldev || !cldev->notif_cb)
850f8940 288 return false;
bb2ef9c3
AU
289
290 if (!cl->notify_ev)
850f8940 291 return false;
bb2ef9c3 292
7c7a6077 293 schedule_work(&cldev->notif_work);
bb2ef9c3
AU
294
295 cl->notify_ev = false;
850f8940
TW
296
297 return true;
e5354107
SO
298}
299
62382997 300/**
7c7a6077 301 * mei_cl_bus_rx_event - schedule rx event
62382997
TW
302 *
303 * @cl: host client
a1f9ae2b
TW
304 *
305 * Return: true if event was scheduled
306 * false if the client is not waiting for event
62382997 307 */
a1f9ae2b 308bool mei_cl_bus_rx_event(struct mei_cl *cl)
e5354107 309{
62382997 310 struct mei_cl_device *cldev = cl->cldev;
d49ed64a 311
7c7a6077 312 if (!cldev || !cldev->rx_cb)
a1f9ae2b 313 return false;
bb2ef9c3 314
7c7a6077 315 schedule_work(&cldev->rx_work);
a1f9ae2b
TW
316
317 return true;
a7b71bc0 318}
d49ed64a 319
62382997 320/**
7c7a6077 321 * mei_cldev_register_rx_cb - register Rx event callback
62382997
TW
322 *
323 * @cldev: me client devices
7c7a6077 324 * @rx_cb: callback function
62382997
TW
325 *
326 * Return: 0 on success
327 * -EALREADY if an callback is already registered
328 * <0 on other errors
329 */
7c7a6077 330int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
e5354107 331{
bc46b45a 332 struct mei_device *bus = cldev->bus;
48168f45
TW
333 int ret;
334
7c7a6077
AU
335 if (!rx_cb)
336 return -EINVAL;
337 if (cldev->rx_cb)
62382997 338 return -EALREADY;
e5354107 339
7c7a6077
AU
340 cldev->rx_cb = rx_cb;
341 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
a7b71bc0 342
7c7a6077
AU
343 mutex_lock(&bus->device_lock);
344 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
345 mutex_unlock(&bus->device_lock);
346 if (ret && ret != -EBUSY)
347 return ret;
bb2ef9c3 348
7c7a6077
AU
349 return 0;
350}
351EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
352
353/**
354 * mei_cldev_register_notif_cb - register FW notification event callback
355 *
356 * @cldev: me client devices
357 * @notif_cb: callback function
358 *
359 * Return: 0 on success
360 * -EALREADY if an callback is already registered
361 * <0 on other errors
362 */
363int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
364 mei_cldev_cb_t notif_cb)
365{
366 struct mei_device *bus = cldev->bus;
367 int ret;
368
369 if (!notif_cb)
370 return -EINVAL;
371
372 if (cldev->notif_cb)
373 return -EALREADY;
374
375 cldev->notif_cb = notif_cb;
376 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
377
378 mutex_lock(&bus->device_lock);
379 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
380 mutex_unlock(&bus->device_lock);
381 if (ret)
382 return ret;
e5354107 383
62382997
TW
384 return 0;
385}
7c7a6077 386EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
e5354107 387
62382997 388/**
d49dc5e7 389 * mei_cldev_get_drvdata - driver data getter
62382997
TW
390 *
391 * @cldev: mei client device
392 *
393 * Return: driver private data
394 */
d49dc5e7 395void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
62382997
TW
396{
397 return dev_get_drvdata(&cldev->dev);
e5354107 398}
d49dc5e7 399EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
e5354107 400
62382997 401/**
d49dc5e7 402 * mei_cldev_set_drvdata - driver data setter
62382997
TW
403 *
404 * @cldev: mei client device
405 * @data: data to store
406 */
d49dc5e7 407void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
e5354107 408{
62382997 409 dev_set_drvdata(&cldev->dev, data);
e5354107 410}
d49dc5e7 411EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
333e4ee0 412
baeacd03
TW
413/**
414 * mei_cldev_uuid - return uuid of the underlying me client
415 *
416 * @cldev: mei client device
417 *
418 * Return: me client uuid
419 */
420const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
421{
422 return mei_me_cl_uuid(cldev->me_cl);
423}
424EXPORT_SYMBOL_GPL(mei_cldev_uuid);
425
426/**
427 * mei_cldev_ver - return protocol version of the underlying me client
428 *
429 * @cldev: mei client device
430 *
431 * Return: me client protocol version
432 */
433u8 mei_cldev_ver(const struct mei_cl_device *cldev)
434{
435 return mei_me_cl_ver(cldev->me_cl);
436}
437EXPORT_SYMBOL_GPL(mei_cldev_ver);
438
01a14ede
TW
439/**
440 * mei_cldev_enabled - check whether the device is enabled
441 *
442 * @cldev: mei client device
443 *
444 * Return: true if me client is initialized and connected
445 */
446bool mei_cldev_enabled(struct mei_cl_device *cldev)
447{
c110cdb1 448 return mei_cl_is_connected(cldev->cl);
01a14ede
TW
449}
450EXPORT_SYMBOL_GPL(mei_cldev_enabled);
451
62382997 452/**
5026c9cb 453 * mei_cldev_enable - enable me client device
62382997
TW
454 * create connection with me client
455 *
456 * @cldev: me client device
457 *
458 * Return: 0 on success and < 0 on error
459 */
d49dc5e7 460int mei_cldev_enable(struct mei_cl_device *cldev)
333e4ee0 461{
6009595a
TW
462 struct mei_device *bus = cldev->bus;
463 struct mei_cl *cl;
464 int ret;
333e4ee0 465
6009595a 466 cl = cldev->cl;
333e4ee0 467
c110cdb1 468 if (cl->state == MEI_FILE_UNINITIALIZED) {
6009595a 469 mutex_lock(&bus->device_lock);
c110cdb1 470 ret = mei_cl_link(cl);
6009595a 471 mutex_unlock(&bus->device_lock);
c110cdb1
AU
472 if (ret)
473 return ret;
6009595a 474 /* update pointers */
6009595a
TW
475 cl->cldev = cldev;
476 }
333e4ee0 477
62382997 478 mutex_lock(&bus->device_lock);
62382997 479 if (mei_cl_is_connected(cl)) {
6009595a
TW
480 ret = 0;
481 goto out;
62382997 482 }
333e4ee0 483
6009595a
TW
484 if (!mei_me_cl_is_active(cldev->me_cl)) {
485 dev_err(&cldev->dev, "me client is not active\n");
486 ret = -ENOTTY;
487 goto out;
62382997
TW
488 }
489
6009595a
TW
490 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
491 if (ret < 0)
492 dev_err(&cldev->dev, "cannot connect\n");
493
494out:
62382997
TW
495 mutex_unlock(&bus->device_lock);
496
6009595a 497 return ret;
333e4ee0 498}
d49dc5e7 499EXPORT_SYMBOL_GPL(mei_cldev_enable);
3e833295 500
57080e88
AU
501/**
502 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
503 * callbacks.
504 *
505 * @cldev: client device
506 */
507static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
508{
509 if (cldev->rx_cb) {
510 cancel_work_sync(&cldev->rx_work);
511 cldev->rx_cb = NULL;
512 }
513
514 if (cldev->notif_cb) {
515 cancel_work_sync(&cldev->notif_work);
516 cldev->notif_cb = NULL;
517 }
518}
519
62382997 520/**
d49dc5e7 521 * mei_cldev_disable - disable me client device
62382997
TW
522 * disconnect form the me client
523 *
524 * @cldev: me client device
525 *
526 * Return: 0 on success and < 0 on error
527 */
d49dc5e7 528int mei_cldev_disable(struct mei_cl_device *cldev)
3e833295 529{
b37719c3 530 struct mei_device *bus;
6009595a
TW
531 struct mei_cl *cl;
532 int err;
3e833295 533
c110cdb1 534 if (!cldev)
3e833295
SO
535 return -ENODEV;
536
6009595a
TW
537 cl = cldev->cl;
538
539 bus = cldev->bus;
4234a6de 540
57080e88
AU
541 mei_cldev_unregister_callbacks(cldev);
542
62382997 543 mutex_lock(&bus->device_lock);
4234a6de 544
62382997 545 if (!mei_cl_is_connected(cl)) {
8d52af67
TW
546 dev_dbg(bus->dev, "Already disconnected\n");
547 err = 0;
548 goto out;
549 }
550
62382997 551 err = mei_cl_disconnect(cl);
6009595a 552 if (err < 0)
8d52af67 553 dev_err(bus->dev, "Could not disconnect from the ME client\n");
3e833295 554
6009595a 555out:
62382997
TW
556 /* Flush queues and remove any pending read */
557 mei_cl_flush_queues(cl, NULL);
6009595a
TW
558 mei_cl_unlink(cl);
559
b37719c3 560 mutex_unlock(&bus->device_lock);
62382997 561 return err;
3e833295 562}
d49dc5e7 563EXPORT_SYMBOL_GPL(mei_cldev_disable);
3e833295 564
5d882460
AU
565/**
566 * mei_cl_bus_module_get - acquire module of the underlying
567 * hw module.
568 *
569 * @cl: host client
570 *
571 * Return: true on success; false if the module was removed.
572 */
573bool mei_cl_bus_module_get(struct mei_cl *cl)
574{
575 struct mei_cl_device *cldev = cl->cldev;
576
577 if (!cldev)
578 return true;
579
580 return try_module_get(cldev->bus->dev->driver->owner);
581}
582
583/**
584 * mei_cl_bus_module_put - release the underlying hw module.
585 *
586 * @cl: host client
587 */
588void mei_cl_bus_module_put(struct mei_cl *cl)
589{
590 struct mei_cl_device *cldev = cl->cldev;
591
592 if (cldev)
593 module_put(cldev->bus->dev->driver->owner);
594}
595
688a9cce
TW
596/**
597 * mei_cl_device_find - find matching entry in the driver id table
598 *
599 * @cldev: me client device
600 * @cldrv: me client driver
601 *
602 * Return: id on success; NULL if no id is matching
603 */
604static const
605struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
606 struct mei_cl_driver *cldrv)
3e833295 607{
62382997
TW
608 const struct mei_cl_device_id *id;
609 const uuid_le *uuid;
b26864ca
TW
610 u8 version;
611 bool match;
3e833295 612
62382997 613 uuid = mei_me_cl_uuid(cldev->me_cl);
b26864ca 614 version = mei_me_cl_ver(cldev->me_cl);
3e833295 615
62382997 616 id = cldrv->id_table;
62382997 617 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
62382997 618 if (!uuid_le_cmp(*uuid, id->uuid)) {
b26864ca 619 match = true;
688a9cce 620
b26864ca
TW
621 if (cldev->name[0])
622 if (strncmp(cldev->name, id->name,
623 sizeof(id->name)))
624 match = false;
688a9cce 625
b26864ca
TW
626 if (id->version != MEI_CL_VERSION_ANY)
627 if (id->version != version)
628 match = false;
629 if (match)
688a9cce 630 return id;
62382997 631 }
e2b31644 632
62382997
TW
633 id++;
634 }
3e833295 635
688a9cce
TW
636 return NULL;
637}
638
639/**
640 * mei_cl_device_match - device match function
641 *
642 * @dev: device
643 * @drv: driver
644 *
645 * Return: 1 if matching device was found 0 otherwise
646 */
647static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
648{
649 struct mei_cl_device *cldev = to_mei_cl_device(dev);
650 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
651 const struct mei_cl_device_id *found_id;
652
653 if (!cldev)
654 return 0;
655
71ce7891
TW
656 if (!cldev->do_match)
657 return 0;
658
688a9cce
TW
659 if (!cldrv || !cldrv->id_table)
660 return 0;
661
662 found_id = mei_cl_device_find(cldev, cldrv);
663 if (found_id)
664 return 1;
665
62382997
TW
666 return 0;
667}
e2b31644 668
feb8cd0f
TW
669/**
670 * mei_cl_device_probe - bus probe function
671 *
672 * @dev: device
673 *
674 * Return: 0 on success; < 0 otherwise
675 */
62382997
TW
676static int mei_cl_device_probe(struct device *dev)
677{
feb8cd0f 678 struct mei_cl_device *cldev;
62382997 679 struct mei_cl_driver *cldrv;
feb8cd0f 680 const struct mei_cl_device_id *id;
b9c79543 681 int ret;
feb8cd0f
TW
682
683 cldev = to_mei_cl_device(dev);
684 cldrv = to_mei_cl_driver(dev->driver);
3e833295 685
62382997
TW
686 if (!cldev)
687 return 0;
3e833295 688
62382997
TW
689 if (!cldrv || !cldrv->probe)
690 return -ENODEV;
691
feb8cd0f
TW
692 id = mei_cl_device_find(cldev, cldrv);
693 if (!id)
694 return -ENODEV;
62382997 695
b9c79543
AK
696 ret = cldrv->probe(cldev, id);
697 if (ret)
698 return ret;
62382997 699
b9c79543
AK
700 __module_get(THIS_MODULE);
701 return 0;
62382997 702}
3e833295 703
feb8cd0f
TW
704/**
705 * mei_cl_device_remove - remove device from the bus
706 *
707 * @dev: device
708 *
709 * Return: 0 on success; < 0 otherwise
710 */
62382997
TW
711static int mei_cl_device_remove(struct device *dev)
712{
713 struct mei_cl_device *cldev = to_mei_cl_device(dev);
714 struct mei_cl_driver *cldrv;
feb8cd0f 715 int ret = 0;
3e833295 716
62382997
TW
717 if (!cldev || !dev->driver)
718 return 0;
719
9ecb839f
AU
720 cldrv = to_mei_cl_driver(dev->driver);
721 if (cldrv->remove)
722 ret = cldrv->remove(cldev);
723
57080e88 724 mei_cldev_unregister_callbacks(cldev);
3d33ff24 725
feb8cd0f
TW
726 module_put(THIS_MODULE);
727 dev->driver = NULL;
728 return ret;
3e833295 729
3e833295
SO
730}
731
62382997
TW
732static ssize_t name_show(struct device *dev, struct device_attribute *a,
733 char *buf)
3e833295 734{
62382997 735 struct mei_cl_device *cldev = to_mei_cl_device(dev);
3e833295 736
423de92f 737 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
3e833295 738}
62382997 739static DEVICE_ATTR_RO(name);
3e833295 740
62382997
TW
741static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
742 char *buf)
3e833295 743{
62382997
TW
744 struct mei_cl_device *cldev = to_mei_cl_device(dev);
745 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
3e833295 746
423de92f 747 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
3e833295 748}
62382997 749static DEVICE_ATTR_RO(uuid);
3e833295 750
40b7320e
TW
751static ssize_t version_show(struct device *dev, struct device_attribute *a,
752 char *buf)
753{
754 struct mei_cl_device *cldev = to_mei_cl_device(dev);
755 u8 version = mei_me_cl_ver(cldev->me_cl);
40b7320e 756
423de92f 757 return scnprintf(buf, PAGE_SIZE, "%02X", version);
40b7320e
TW
758}
759static DEVICE_ATTR_RO(version);
760
62382997
TW
761static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
762 char *buf)
3e833295 763{
62382997
TW
764 struct mei_cl_device *cldev = to_mei_cl_device(dev);
765 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
6f9193ec 766 u8 version = mei_me_cl_ver(cldev->me_cl);
3e833295 767
6f9193ec
PA
768 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
769 cldev->name, uuid, version);
3e833295 770}
62382997 771static DEVICE_ATTR_RO(modalias);
3e833295 772
d49dc5e7 773static struct attribute *mei_cldev_attrs[] = {
62382997
TW
774 &dev_attr_name.attr,
775 &dev_attr_uuid.attr,
40b7320e 776 &dev_attr_version.attr,
62382997
TW
777 &dev_attr_modalias.attr,
778 NULL,
779};
d49dc5e7 780ATTRIBUTE_GROUPS(mei_cldev);
62382997 781
38d3c00d
TW
782/**
783 * mei_cl_device_uevent - me client bus uevent handler
784 *
785 * @dev: device
786 * @env: uevent kobject
787 *
788 * Return: 0 on success -ENOMEM on when add_uevent_var fails
789 */
790static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
3e833295 791{
62382997
TW
792 struct mei_cl_device *cldev = to_mei_cl_device(dev);
793 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
40b7320e
TW
794 u8 version = mei_me_cl_ver(cldev->me_cl);
795
796 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
797 return -ENOMEM;
3e833295 798
62382997
TW
799 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
800 return -ENOMEM;
3e833295 801
62382997
TW
802 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
803 return -ENOMEM;
804
b26864ca
TW
805 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
806 cldev->name, uuid, version))
62382997 807 return -ENOMEM;
3e833295
SO
808
809 return 0;
810}
cf3baefb 811
62382997
TW
812static struct bus_type mei_cl_bus_type = {
813 .name = "mei",
d49dc5e7 814 .dev_groups = mei_cldev_groups,
62382997
TW
815 .match = mei_cl_device_match,
816 .probe = mei_cl_device_probe,
817 .remove = mei_cl_device_remove,
38d3c00d 818 .uevent = mei_cl_device_uevent,
62382997
TW
819};
820
512f64d9
TW
821static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
822{
823 if (bus)
824 get_device(bus->dev);
825
826 return bus;
827}
828
829static void mei_dev_bus_put(struct mei_device *bus)
830{
831 if (bus)
832 put_device(bus->dev);
833}
834
ae48d74d 835static void mei_cl_bus_dev_release(struct device *dev)
aa6aef21 836{
62382997
TW
837 struct mei_cl_device *cldev = to_mei_cl_device(dev);
838
839 if (!cldev)
840 return;
841
842 mei_me_cl_put(cldev->me_cl);
512f64d9 843 mei_dev_bus_put(cldev->bus);
c110cdb1 844 kfree(cldev->cl);
62382997 845 kfree(cldev);
aa6aef21 846}
aa6aef21 847
b8d01b7f 848static const struct device_type mei_cl_device_type = {
ae48d74d 849 .release = mei_cl_bus_dev_release,
62382997
TW
850};
851
213dd193
TW
852/**
853 * mei_cl_bus_set_name - set device name for me client device
854 *
855 * @cldev: me client device
856 */
857static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
858{
859 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
860 cldev->name,
861 mei_me_cl_uuid(cldev->me_cl),
862 mei_me_cl_ver(cldev->me_cl));
863}
864
71ce7891 865/**
ae48d74d 866 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
71ce7891
TW
867 *
868 * @bus: mei device
869 * @me_cl: me client
870 *
871 * Return: allocated device structur or NULL on allocation failure
872 */
ae48d74d
TW
873static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
874 struct mei_me_client *me_cl)
71ce7891
TW
875{
876 struct mei_cl_device *cldev;
c110cdb1 877 struct mei_cl *cl;
71ce7891
TW
878
879 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
880 if (!cldev)
881 return NULL;
882
c110cdb1
AU
883 cl = mei_cl_allocate(bus);
884 if (!cl) {
885 kfree(cldev);
886 return NULL;
887 }
888
71ce7891
TW
889 device_initialize(&cldev->dev);
890 cldev->dev.parent = bus->dev;
891 cldev->dev.bus = &mei_cl_bus_type;
892 cldev->dev.type = &mei_cl_device_type;
893 cldev->bus = mei_dev_bus_get(bus);
894 cldev->me_cl = mei_me_cl_get(me_cl);
c110cdb1 895 cldev->cl = cl;
213dd193 896 mei_cl_bus_set_name(cldev);
71ce7891
TW
897 cldev->is_added = 0;
898 INIT_LIST_HEAD(&cldev->bus_list);
899
900 return cldev;
901}
902
903/**
904 * mei_cl_dev_setup - setup me client device
905 * run fix up routines and set the device name
906 *
907 * @bus: mei device
908 * @cldev: me client device
909 *
910 * Return: true if the device is eligible for enumeration
911 */
ae48d74d
TW
912static bool mei_cl_bus_dev_setup(struct mei_device *bus,
913 struct mei_cl_device *cldev)
71ce7891
TW
914{
915 cldev->do_match = 1;
ae48d74d 916 mei_cl_bus_dev_fixup(cldev);
71ce7891 917
213dd193 918 /* the device name can change during fix up */
71ce7891 919 if (cldev->do_match)
213dd193 920 mei_cl_bus_set_name(cldev);
71ce7891
TW
921
922 return cldev->do_match == 1;
923}
924
925/**
926 * mei_cl_bus_dev_add - add me client devices
927 *
928 * @cldev: me client device
929 *
930 * Return: 0 on success; < 0 on failre
931 */
932static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
933{
934 int ret;
935
b26864ca
TW
936 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
937 mei_me_cl_uuid(cldev->me_cl),
938 mei_me_cl_ver(cldev->me_cl));
71ce7891
TW
939 ret = device_add(&cldev->dev);
940 if (!ret)
941 cldev->is_added = 1;
942
943 return ret;
944}
945
6009595a
TW
946/**
947 * mei_cl_bus_dev_stop - stop the driver
948 *
949 * @cldev: me client device
950 */
951static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
952{
953 if (cldev->is_added)
954 device_release_driver(&cldev->dev);
955}
956
957/**
958 * mei_cl_bus_dev_destroy - destroy me client devices object
959 *
960 * @cldev: me client device
2da55cfd
TW
961 *
962 * Locking: called under "dev->cl_bus_lock" lock
6009595a
TW
963 */
964static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
965{
2da55cfd
TW
966
967 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
968
6009595a
TW
969 if (!cldev->is_added)
970 return;
971
972 device_del(&cldev->dev);
973
6009595a 974 list_del_init(&cldev->bus_list);
6009595a
TW
975
976 cldev->is_added = 0;
977 put_device(&cldev->dev);
978}
979
980/**
981 * mei_cl_bus_remove_device - remove a devices form the bus
982 *
983 * @cldev: me client device
984 */
985static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
986{
987 mei_cl_bus_dev_stop(cldev);
988 mei_cl_bus_dev_destroy(cldev);
989}
990
991/**
992 * mei_cl_bus_remove_devices - remove all devices form the bus
993 *
994 * @bus: mei device
995 */
996void mei_cl_bus_remove_devices(struct mei_device *bus)
997{
998 struct mei_cl_device *cldev, *next;
999
2da55cfd 1000 mutex_lock(&bus->cl_bus_lock);
6009595a
TW
1001 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1002 mei_cl_bus_remove_device(cldev);
2da55cfd 1003 mutex_unlock(&bus->cl_bus_lock);
6009595a
TW
1004}
1005
1006
1007/**
ae48d74d 1008 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
6009595a
TW
1009 * based on me client
1010 *
1011 * @bus: mei device
1012 * @me_cl: me client
2da55cfd
TW
1013 *
1014 * Locking: called under "dev->cl_bus_lock" lock
6009595a 1015 */
ae48d74d
TW
1016static void mei_cl_bus_dev_init(struct mei_device *bus,
1017 struct mei_me_client *me_cl)
e46980a1 1018{
62382997 1019 struct mei_cl_device *cldev;
6009595a 1020
2da55cfd
TW
1021 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1022
6009595a
TW
1023 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1024
1025 if (me_cl->bus_added)
1026 return;
e46980a1 1027
ae48d74d 1028 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
62382997 1029 if (!cldev)
6009595a 1030 return;
e46980a1 1031
6009595a
TW
1032 me_cl->bus_added = true;
1033 list_add_tail(&cldev->bus_list, &bus->device_list);
0c53357c 1034
6009595a 1035}
e46980a1 1036
6009595a
TW
1037/**
1038 * mei_cl_bus_rescan - scan me clients list and add create
1039 * devices for eligible clients
1040 *
1041 * @bus: mei device
1042 */
8bb2d27f 1043static void mei_cl_bus_rescan(struct mei_device *bus)
6009595a
TW
1044{
1045 struct mei_cl_device *cldev, *n;
1046 struct mei_me_client *me_cl;
e46980a1 1047
2da55cfd
TW
1048 mutex_lock(&bus->cl_bus_lock);
1049
6009595a
TW
1050 down_read(&bus->me_clients_rwsem);
1051 list_for_each_entry(me_cl, &bus->me_clients, list)
ae48d74d 1052 mei_cl_bus_dev_init(bus, me_cl);
6009595a 1053 up_read(&bus->me_clients_rwsem);
e46980a1 1054
6009595a 1055 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
e46980a1 1056
6009595a
TW
1057 if (!mei_me_cl_is_active(cldev->me_cl)) {
1058 mei_cl_bus_remove_device(cldev);
1059 continue;
1060 }
e46980a1 1061
6009595a
TW
1062 if (cldev->is_added)
1063 continue;
1064
ae48d74d 1065 if (mei_cl_bus_dev_setup(bus, cldev))
6009595a
TW
1066 mei_cl_bus_dev_add(cldev);
1067 else {
1068 list_del_init(&cldev->bus_list);
1069 put_device(&cldev->dev);
1070 }
1071 }
1072 mutex_unlock(&bus->cl_bus_lock);
1073
1074 dev_dbg(bus->dev, "rescan end");
62382997 1075}
e46980a1 1076
a816a00e
AU
1077void mei_cl_bus_rescan_work(struct work_struct *work)
1078{
1079 struct mei_device *bus =
1080 container_of(work, struct mei_device, bus_rescan_work);
1081
1082 mei_cl_bus_rescan(bus);
1083}
1084
d49dc5e7
TW
1085int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1086 struct module *owner)
62382997
TW
1087{
1088 int err;
e46980a1 1089
62382997
TW
1090 cldrv->driver.name = cldrv->name;
1091 cldrv->driver.owner = owner;
1092 cldrv->driver.bus = &mei_cl_bus_type;
e46980a1 1093
62382997
TW
1094 err = driver_register(&cldrv->driver);
1095 if (err)
1096 return err;
e46980a1 1097
62382997 1098 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
e46980a1 1099
62382997 1100 return 0;
e46980a1 1101}
d49dc5e7 1102EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
e46980a1 1103
d49dc5e7 1104void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
cf3baefb 1105{
62382997 1106 driver_unregister(&cldrv->driver);
cf3baefb 1107
62382997 1108 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
cf3baefb 1109}
d49dc5e7 1110EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
cf3baefb 1111
6009595a 1112
cf3baefb
SO
1113int __init mei_cl_bus_init(void)
1114{
1115 return bus_register(&mei_cl_bus_type);
1116}
1117
1118void __exit mei_cl_bus_exit(void)
1119{
1120 bus_unregister(&mei_cl_bus_type);
1121}