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