]>
Commit | Line | Data |
---|---|---|
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 | */ | |
43 | ssize_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 | ||
82 | out: | |
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 | |
94 | * @buf: buffer to send | |
95 | * @length: buffer length | |
96 | * | |
97 | * Return: read size in bytes of < 0 on error | |
98 | */ | |
99 | ssize_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 |
149 | copy: |
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 |
159 | free: |
160 | mei_io_cb_free(cb); | |
161 | out: | |
162 | mutex_unlock(&bus->device_lock); | |
163 | ||
164 | return rets; | |
007d64eb | 165 | } |
007d64eb | 166 | |
62382997 TW |
167 | /** |
168 | * mei_cl_send - me device send (write) | |
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 | */ | |
176 | ssize_t mei_cl_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 | } |
62382997 | 185 | EXPORT_SYMBOL_GPL(mei_cl_send); |
007d64eb | 186 | |
62382997 TW |
187 | /** |
188 | * mei_cl_recv - client receive (read) | |
189 | * | |
190 | * @cldev: me client device | |
191 | * @buf: buffer to send | |
192 | * @length: buffer length | |
193 | * | |
194 | * Return: read size in bytes of < 0 on error | |
195 | */ | |
196 | ssize_t mei_cl_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 | } |
62382997 | 205 | EXPORT_SYMBOL_GPL(mei_cl_recv); |
e5354107 | 206 | |
62382997 TW |
207 | /** |
208 | * mei_bus_event_work - dispatch rx event for a bus device | |
209 | * and schedule new work | |
210 | * | |
211 | * @work: work | |
212 | */ | |
213 | static void mei_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 | |
233 | */ | |
234 | void mei_cl_bus_notify_event(struct mei_cl *cl) | |
235 | { | |
236 | struct mei_cl_device *cldev = cl->cldev; | |
237 | ||
238 | if (!cldev || !cldev->event_cb) | |
239 | return; | |
240 | ||
241 | if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF))) | |
242 | return; | |
243 | ||
244 | if (!cl->notify_ev) | |
245 | return; | |
246 | ||
247 | set_bit(MEI_CL_EVENT_NOTIF, &cldev->events); | |
248 | ||
249 | schedule_work(&cldev->event_work); | |
250 | ||
251 | cl->notify_ev = false; | |
e5354107 SO |
252 | } |
253 | ||
62382997 TW |
254 | /** |
255 | * mei_cl_bus_rx_event - schedule rx evenet | |
256 | * | |
257 | * @cl: host client | |
258 | */ | |
259 | void mei_cl_bus_rx_event(struct mei_cl *cl) | |
e5354107 | 260 | { |
62382997 | 261 | struct mei_cl_device *cldev = cl->cldev; |
d49ed64a | 262 | |
62382997 | 263 | if (!cldev || !cldev->event_cb) |
d49ed64a AU |
264 | return; |
265 | ||
bb2ef9c3 AU |
266 | if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX))) |
267 | return; | |
268 | ||
62382997 | 269 | set_bit(MEI_CL_EVENT_RX, &cldev->events); |
a7b71bc0 | 270 | |
62382997 | 271 | schedule_work(&cldev->event_work); |
a7b71bc0 | 272 | } |
d49ed64a | 273 | |
62382997 TW |
274 | /** |
275 | * mei_cl_register_event_cb - register event callback | |
276 | * | |
277 | * @cldev: me client devices | |
278 | * @event_cb: callback function | |
bb2ef9c3 | 279 | * @events_mask: requested events bitmask |
62382997 TW |
280 | * @context: driver context data |
281 | * | |
282 | * Return: 0 on success | |
283 | * -EALREADY if an callback is already registered | |
284 | * <0 on other errors | |
285 | */ | |
286 | int mei_cl_register_event_cb(struct mei_cl_device *cldev, | |
bb2ef9c3 | 287 | unsigned long events_mask, |
62382997 | 288 | mei_cl_event_cb_t event_cb, void *context) |
e5354107 | 289 | { |
48168f45 TW |
290 | int ret; |
291 | ||
62382997 TW |
292 | if (cldev->event_cb) |
293 | return -EALREADY; | |
e5354107 | 294 | |
62382997 | 295 | cldev->events = 0; |
bb2ef9c3 | 296 | cldev->events_mask = events_mask; |
62382997 TW |
297 | cldev->event_cb = event_cb; |
298 | cldev->event_context = context; | |
299 | INIT_WORK(&cldev->event_work, mei_bus_event_work); | |
a7b71bc0 | 300 | |
bb2ef9c3 AU |
301 | if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) { |
302 | ret = mei_cl_read_start(cldev->cl, 0, NULL); | |
303 | if (ret && ret != -EBUSY) | |
304 | return ret; | |
305 | } | |
306 | ||
307 | if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) { | |
308 | mutex_lock(&cldev->cl->dev->device_lock); | |
309 | ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0); | |
310 | mutex_unlock(&cldev->cl->dev->device_lock); | |
311 | if (ret) | |
312 | return ret; | |
313 | } | |
e5354107 | 314 | |
62382997 TW |
315 | return 0; |
316 | } | |
317 | EXPORT_SYMBOL_GPL(mei_cl_register_event_cb); | |
e5354107 | 318 | |
62382997 TW |
319 | /** |
320 | * mei_cl_get_drvdata - driver data getter | |
321 | * | |
322 | * @cldev: mei client device | |
323 | * | |
324 | * Return: driver private data | |
325 | */ | |
326 | void *mei_cl_get_drvdata(const struct mei_cl_device *cldev) | |
327 | { | |
328 | return dev_get_drvdata(&cldev->dev); | |
e5354107 | 329 | } |
62382997 | 330 | EXPORT_SYMBOL_GPL(mei_cl_get_drvdata); |
e5354107 | 331 | |
62382997 TW |
332 | /** |
333 | * mei_cl_set_drvdata - driver data setter | |
334 | * | |
335 | * @cldev: mei client device | |
336 | * @data: data to store | |
337 | */ | |
338 | void mei_cl_set_drvdata(struct mei_cl_device *cldev, void *data) | |
e5354107 | 339 | { |
62382997 | 340 | dev_set_drvdata(&cldev->dev, data); |
e5354107 | 341 | } |
62382997 | 342 | EXPORT_SYMBOL_GPL(mei_cl_set_drvdata); |
333e4ee0 | 343 | |
62382997 TW |
344 | /** |
345 | * mei_cl_enable_device - enable me client device | |
346 | * create connection with me client | |
347 | * | |
348 | * @cldev: me client device | |
349 | * | |
350 | * Return: 0 on success and < 0 on error | |
351 | */ | |
352 | int mei_cl_enable_device(struct mei_cl_device *cldev) | |
333e4ee0 | 353 | { |
6009595a TW |
354 | struct mei_device *bus = cldev->bus; |
355 | struct mei_cl *cl; | |
356 | int ret; | |
333e4ee0 | 357 | |
6009595a | 358 | cl = cldev->cl; |
333e4ee0 | 359 | |
6009595a TW |
360 | if (!cl) { |
361 | mutex_lock(&bus->device_lock); | |
362 | cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); | |
363 | mutex_unlock(&bus->device_lock); | |
364 | if (IS_ERR(cl)) | |
365 | return PTR_ERR(cl); | |
366 | /* update pointers */ | |
367 | cldev->cl = cl; | |
368 | cl->cldev = cldev; | |
369 | } | |
333e4ee0 | 370 | |
62382997 | 371 | mutex_lock(&bus->device_lock); |
62382997 | 372 | if (mei_cl_is_connected(cl)) { |
6009595a TW |
373 | ret = 0; |
374 | goto out; | |
62382997 | 375 | } |
333e4ee0 | 376 | |
6009595a TW |
377 | if (!mei_me_cl_is_active(cldev->me_cl)) { |
378 | dev_err(&cldev->dev, "me client is not active\n"); | |
379 | ret = -ENOTTY; | |
380 | goto out; | |
62382997 TW |
381 | } |
382 | ||
6009595a TW |
383 | ret = mei_cl_connect(cl, cldev->me_cl, NULL); |
384 | if (ret < 0) | |
385 | dev_err(&cldev->dev, "cannot connect\n"); | |
386 | ||
387 | out: | |
62382997 TW |
388 | mutex_unlock(&bus->device_lock); |
389 | ||
6009595a | 390 | return ret; |
333e4ee0 | 391 | } |
62382997 | 392 | EXPORT_SYMBOL_GPL(mei_cl_enable_device); |
3e833295 | 393 | |
62382997 TW |
394 | /** |
395 | * mei_cl_disable_device - disable me client device | |
396 | * disconnect form the me client | |
397 | * | |
398 | * @cldev: me client device | |
399 | * | |
400 | * Return: 0 on success and < 0 on error | |
401 | */ | |
402 | int mei_cl_disable_device(struct mei_cl_device *cldev) | |
3e833295 | 403 | { |
b37719c3 | 404 | struct mei_device *bus; |
6009595a TW |
405 | struct mei_cl *cl; |
406 | int err; | |
3e833295 | 407 | |
6009595a | 408 | if (!cldev || !cldev->cl) |
3e833295 SO |
409 | return -ENODEV; |
410 | ||
6009595a TW |
411 | cl = cldev->cl; |
412 | ||
413 | bus = cldev->bus; | |
4234a6de | 414 | |
62382997 | 415 | cldev->event_cb = NULL; |
3e833295 | 416 | |
62382997 | 417 | mutex_lock(&bus->device_lock); |
4234a6de | 418 | |
62382997 TW |
419 | if (!mei_cl_is_connected(cl)) { |
420 | dev_err(bus->dev, "Already disconnected"); | |
421 | err = 0; | |
79563db9 TW |
422 | goto out; |
423 | } | |
4234a6de | 424 | |
62382997 | 425 | err = mei_cl_disconnect(cl); |
6009595a | 426 | if (err < 0) |
62382997 | 427 | dev_err(bus->dev, "Could not disconnect from the ME client"); |
3e833295 | 428 | |
6009595a | 429 | out: |
62382997 TW |
430 | /* Flush queues and remove any pending read */ |
431 | mei_cl_flush_queues(cl, NULL); | |
6009595a TW |
432 | mei_cl_unlink(cl); |
433 | ||
434 | kfree(cl); | |
435 | cldev->cl = NULL; | |
3e833295 | 436 | |
b37719c3 | 437 | mutex_unlock(&bus->device_lock); |
62382997 | 438 | return err; |
3e833295 | 439 | } |
62382997 | 440 | EXPORT_SYMBOL_GPL(mei_cl_disable_device); |
3e833295 | 441 | |
688a9cce TW |
442 | /** |
443 | * mei_cl_device_find - find matching entry in the driver id table | |
444 | * | |
445 | * @cldev: me client device | |
446 | * @cldrv: me client driver | |
447 | * | |
448 | * Return: id on success; NULL if no id is matching | |
449 | */ | |
450 | static const | |
451 | struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev, | |
452 | struct mei_cl_driver *cldrv) | |
3e833295 | 453 | { |
62382997 TW |
454 | const struct mei_cl_device_id *id; |
455 | const uuid_le *uuid; | |
3e833295 | 456 | |
62382997 | 457 | uuid = mei_me_cl_uuid(cldev->me_cl); |
3e833295 | 458 | |
62382997 | 459 | id = cldrv->id_table; |
62382997 | 460 | while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { |
62382997 | 461 | if (!uuid_le_cmp(*uuid, id->uuid)) { |
688a9cce TW |
462 | |
463 | if (!cldev->name[0]) | |
464 | return id; | |
465 | ||
466 | if (!strncmp(cldev->name, id->name, sizeof(id->name))) | |
467 | return id; | |
62382997 | 468 | } |
e2b31644 | 469 | |
62382997 TW |
470 | id++; |
471 | } | |
3e833295 | 472 | |
688a9cce TW |
473 | return NULL; |
474 | } | |
475 | ||
476 | /** | |
477 | * mei_cl_device_match - device match function | |
478 | * | |
479 | * @dev: device | |
480 | * @drv: driver | |
481 | * | |
482 | * Return: 1 if matching device was found 0 otherwise | |
483 | */ | |
484 | static int mei_cl_device_match(struct device *dev, struct device_driver *drv) | |
485 | { | |
486 | struct mei_cl_device *cldev = to_mei_cl_device(dev); | |
487 | struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); | |
488 | const struct mei_cl_device_id *found_id; | |
489 | ||
490 | if (!cldev) | |
491 | return 0; | |
492 | ||
71ce7891 TW |
493 | if (!cldev->do_match) |
494 | return 0; | |
495 | ||
688a9cce TW |
496 | if (!cldrv || !cldrv->id_table) |
497 | return 0; | |
498 | ||
499 | found_id = mei_cl_device_find(cldev, cldrv); | |
500 | if (found_id) | |
501 | return 1; | |
502 | ||
62382997 TW |
503 | return 0; |
504 | } | |
e2b31644 | 505 | |
feb8cd0f TW |
506 | /** |
507 | * mei_cl_device_probe - bus probe function | |
508 | * | |
509 | * @dev: device | |
510 | * | |
511 | * Return: 0 on success; < 0 otherwise | |
512 | */ | |
62382997 TW |
513 | static int mei_cl_device_probe(struct device *dev) |
514 | { | |
feb8cd0f | 515 | struct mei_cl_device *cldev; |
62382997 | 516 | struct mei_cl_driver *cldrv; |
feb8cd0f TW |
517 | const struct mei_cl_device_id *id; |
518 | ||
519 | cldev = to_mei_cl_device(dev); | |
520 | cldrv = to_mei_cl_driver(dev->driver); | |
3e833295 | 521 | |
62382997 TW |
522 | if (!cldev) |
523 | return 0; | |
3e833295 | 524 | |
62382997 TW |
525 | if (!cldrv || !cldrv->probe) |
526 | return -ENODEV; | |
527 | ||
feb8cd0f TW |
528 | id = mei_cl_device_find(cldev, cldrv); |
529 | if (!id) | |
530 | return -ENODEV; | |
62382997 | 531 | |
feb8cd0f | 532 | __module_get(THIS_MODULE); |
62382997 | 533 | |
feb8cd0f | 534 | return cldrv->probe(cldev, id); |
62382997 | 535 | } |
3e833295 | 536 | |
feb8cd0f TW |
537 | /** |
538 | * mei_cl_device_remove - remove device from the bus | |
539 | * | |
540 | * @dev: device | |
541 | * | |
542 | * Return: 0 on success; < 0 otherwise | |
543 | */ | |
62382997 TW |
544 | static int mei_cl_device_remove(struct device *dev) |
545 | { | |
546 | struct mei_cl_device *cldev = to_mei_cl_device(dev); | |
547 | struct mei_cl_driver *cldrv; | |
feb8cd0f | 548 | int ret = 0; |
3e833295 | 549 | |
62382997 TW |
550 | if (!cldev || !dev->driver) |
551 | return 0; | |
552 | ||
553 | if (cldev->event_cb) { | |
554 | cldev->event_cb = NULL; | |
555 | cancel_work_sync(&cldev->event_work); | |
3d33ff24 TW |
556 | } |
557 | ||
62382997 | 558 | cldrv = to_mei_cl_driver(dev->driver); |
feb8cd0f TW |
559 | if (cldrv->remove) |
560 | ret = cldrv->remove(cldev); | |
3e833295 | 561 | |
feb8cd0f TW |
562 | module_put(THIS_MODULE); |
563 | dev->driver = NULL; | |
564 | return ret; | |
3e833295 | 565 | |
3e833295 SO |
566 | } |
567 | ||
62382997 TW |
568 | static ssize_t name_show(struct device *dev, struct device_attribute *a, |
569 | char *buf) | |
3e833295 | 570 | { |
62382997 TW |
571 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
572 | size_t len; | |
3e833295 | 573 | |
62382997 | 574 | len = snprintf(buf, PAGE_SIZE, "%s", cldev->name); |
3e833295 | 575 | |
62382997 | 576 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
3e833295 | 577 | } |
62382997 | 578 | static DEVICE_ATTR_RO(name); |
3e833295 | 579 | |
62382997 TW |
580 | static ssize_t uuid_show(struct device *dev, struct device_attribute *a, |
581 | char *buf) | |
3e833295 | 582 | { |
62382997 TW |
583 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
584 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); | |
585 | size_t len; | |
3e833295 | 586 | |
62382997 | 587 | len = snprintf(buf, PAGE_SIZE, "%pUl", uuid); |
3e833295 | 588 | |
62382997 | 589 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
3e833295 | 590 | } |
62382997 | 591 | static DEVICE_ATTR_RO(uuid); |
3e833295 | 592 | |
62382997 TW |
593 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
594 | char *buf) | |
3e833295 | 595 | { |
62382997 TW |
596 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
597 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); | |
598 | size_t len; | |
3e833295 | 599 | |
62382997 TW |
600 | len = snprintf(buf, PAGE_SIZE, "mei:%s:" MEI_CL_UUID_FMT ":", |
601 | cldev->name, MEI_CL_UUID_ARGS(uuid->b)); | |
3e833295 | 602 | |
62382997 | 603 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
3e833295 | 604 | } |
62382997 | 605 | static DEVICE_ATTR_RO(modalias); |
3e833295 | 606 | |
62382997 TW |
607 | static struct attribute *mei_cl_dev_attrs[] = { |
608 | &dev_attr_name.attr, | |
609 | &dev_attr_uuid.attr, | |
610 | &dev_attr_modalias.attr, | |
611 | NULL, | |
612 | }; | |
613 | ATTRIBUTE_GROUPS(mei_cl_dev); | |
614 | ||
38d3c00d TW |
615 | /** |
616 | * mei_cl_device_uevent - me client bus uevent handler | |
617 | * | |
618 | * @dev: device | |
619 | * @env: uevent kobject | |
620 | * | |
621 | * Return: 0 on success -ENOMEM on when add_uevent_var fails | |
622 | */ | |
623 | static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env) | |
3e833295 | 624 | { |
62382997 TW |
625 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
626 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); | |
3e833295 | 627 | |
62382997 TW |
628 | if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) |
629 | return -ENOMEM; | |
3e833295 | 630 | |
62382997 TW |
631 | if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) |
632 | return -ENOMEM; | |
633 | ||
634 | if (add_uevent_var(env, "MODALIAS=mei:%s:" MEI_CL_UUID_FMT ":", | |
635 | cldev->name, MEI_CL_UUID_ARGS(uuid->b))) | |
636 | return -ENOMEM; | |
3e833295 SO |
637 | |
638 | return 0; | |
639 | } | |
cf3baefb | 640 | |
62382997 TW |
641 | static struct bus_type mei_cl_bus_type = { |
642 | .name = "mei", | |
643 | .dev_groups = mei_cl_dev_groups, | |
644 | .match = mei_cl_device_match, | |
645 | .probe = mei_cl_device_probe, | |
646 | .remove = mei_cl_device_remove, | |
38d3c00d | 647 | .uevent = mei_cl_device_uevent, |
62382997 TW |
648 | }; |
649 | ||
512f64d9 TW |
650 | static struct mei_device *mei_dev_bus_get(struct mei_device *bus) |
651 | { | |
652 | if (bus) | |
653 | get_device(bus->dev); | |
654 | ||
655 | return bus; | |
656 | } | |
657 | ||
658 | static void mei_dev_bus_put(struct mei_device *bus) | |
659 | { | |
660 | if (bus) | |
661 | put_device(bus->dev); | |
662 | } | |
663 | ||
62382997 | 664 | static void mei_cl_dev_release(struct device *dev) |
aa6aef21 | 665 | { |
62382997 TW |
666 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
667 | ||
668 | if (!cldev) | |
669 | return; | |
670 | ||
671 | mei_me_cl_put(cldev->me_cl); | |
512f64d9 | 672 | mei_dev_bus_put(cldev->bus); |
62382997 | 673 | kfree(cldev); |
aa6aef21 | 674 | } |
aa6aef21 | 675 | |
62382997 TW |
676 | static struct device_type mei_cl_device_type = { |
677 | .release = mei_cl_dev_release, | |
678 | }; | |
679 | ||
71ce7891 TW |
680 | /** |
681 | * mei_cl_dev_alloc - initialize and allocate mei client device | |
682 | * | |
683 | * @bus: mei device | |
684 | * @me_cl: me client | |
685 | * | |
686 | * Return: allocated device structur or NULL on allocation failure | |
687 | */ | |
688 | static struct mei_cl_device *mei_cl_dev_alloc(struct mei_device *bus, | |
689 | struct mei_me_client *me_cl) | |
690 | { | |
691 | struct mei_cl_device *cldev; | |
692 | ||
693 | cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); | |
694 | if (!cldev) | |
695 | return NULL; | |
696 | ||
697 | device_initialize(&cldev->dev); | |
698 | cldev->dev.parent = bus->dev; | |
699 | cldev->dev.bus = &mei_cl_bus_type; | |
700 | cldev->dev.type = &mei_cl_device_type; | |
701 | cldev->bus = mei_dev_bus_get(bus); | |
702 | cldev->me_cl = mei_me_cl_get(me_cl); | |
703 | cldev->is_added = 0; | |
704 | INIT_LIST_HEAD(&cldev->bus_list); | |
705 | ||
706 | return cldev; | |
707 | } | |
708 | ||
709 | /** | |
710 | * mei_cl_dev_setup - setup me client device | |
711 | * run fix up routines and set the device name | |
712 | * | |
713 | * @bus: mei device | |
714 | * @cldev: me client device | |
715 | * | |
716 | * Return: true if the device is eligible for enumeration | |
717 | */ | |
718 | static bool mei_cl_dev_setup(struct mei_device *bus, | |
719 | struct mei_cl_device *cldev) | |
720 | { | |
721 | cldev->do_match = 1; | |
722 | mei_cl_dev_fixup(cldev); | |
723 | ||
724 | if (cldev->do_match) | |
725 | dev_set_name(&cldev->dev, "mei:%s:%pUl", | |
726 | cldev->name, mei_me_cl_uuid(cldev->me_cl)); | |
727 | ||
728 | return cldev->do_match == 1; | |
729 | } | |
730 | ||
731 | /** | |
732 | * mei_cl_bus_dev_add - add me client devices | |
733 | * | |
734 | * @cldev: me client device | |
735 | * | |
736 | * Return: 0 on success; < 0 on failre | |
737 | */ | |
738 | static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) | |
739 | { | |
740 | int ret; | |
741 | ||
742 | dev_dbg(cldev->bus->dev, "adding %pUL\n", mei_me_cl_uuid(cldev->me_cl)); | |
743 | ret = device_add(&cldev->dev); | |
744 | if (!ret) | |
745 | cldev->is_added = 1; | |
746 | ||
747 | return ret; | |
748 | } | |
749 | ||
6009595a TW |
750 | /** |
751 | * mei_cl_bus_dev_stop - stop the driver | |
752 | * | |
753 | * @cldev: me client device | |
754 | */ | |
755 | static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev) | |
756 | { | |
757 | if (cldev->is_added) | |
758 | device_release_driver(&cldev->dev); | |
759 | } | |
760 | ||
761 | /** | |
762 | * mei_cl_bus_dev_destroy - destroy me client devices object | |
763 | * | |
764 | * @cldev: me client device | |
765 | */ | |
766 | static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev) | |
767 | { | |
768 | if (!cldev->is_added) | |
769 | return; | |
770 | ||
771 | device_del(&cldev->dev); | |
772 | ||
773 | mutex_lock(&cldev->bus->cl_bus_lock); | |
774 | list_del_init(&cldev->bus_list); | |
775 | mutex_unlock(&cldev->bus->cl_bus_lock); | |
776 | ||
777 | cldev->is_added = 0; | |
778 | put_device(&cldev->dev); | |
779 | } | |
780 | ||
781 | /** | |
782 | * mei_cl_bus_remove_device - remove a devices form the bus | |
783 | * | |
784 | * @cldev: me client device | |
785 | */ | |
786 | static void mei_cl_bus_remove_device(struct mei_cl_device *cldev) | |
787 | { | |
788 | mei_cl_bus_dev_stop(cldev); | |
789 | mei_cl_bus_dev_destroy(cldev); | |
790 | } | |
791 | ||
792 | /** | |
793 | * mei_cl_bus_remove_devices - remove all devices form the bus | |
794 | * | |
795 | * @bus: mei device | |
796 | */ | |
797 | void mei_cl_bus_remove_devices(struct mei_device *bus) | |
798 | { | |
799 | struct mei_cl_device *cldev, *next; | |
800 | ||
801 | list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list) | |
802 | mei_cl_bus_remove_device(cldev); | |
803 | } | |
804 | ||
805 | ||
806 | /** | |
807 | * mei_cl_dev_init - allocate and initializes an mei client devices | |
808 | * based on me client | |
809 | * | |
810 | * @bus: mei device | |
811 | * @me_cl: me client | |
812 | */ | |
813 | static void mei_cl_dev_init(struct mei_device *bus, struct mei_me_client *me_cl) | |
e46980a1 | 814 | { |
62382997 | 815 | struct mei_cl_device *cldev; |
6009595a TW |
816 | |
817 | dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl)); | |
818 | ||
819 | if (me_cl->bus_added) | |
820 | return; | |
e46980a1 | 821 | |
71ce7891 | 822 | cldev = mei_cl_dev_alloc(bus, me_cl); |
62382997 | 823 | if (!cldev) |
6009595a | 824 | return; |
e46980a1 | 825 | |
6009595a TW |
826 | mutex_lock(&cldev->bus->cl_bus_lock); |
827 | me_cl->bus_added = true; | |
828 | list_add_tail(&cldev->bus_list, &bus->device_list); | |
829 | mutex_unlock(&cldev->bus->cl_bus_lock); | |
0c53357c | 830 | |
6009595a | 831 | } |
e46980a1 | 832 | |
6009595a TW |
833 | /** |
834 | * mei_cl_bus_rescan - scan me clients list and add create | |
835 | * devices for eligible clients | |
836 | * | |
837 | * @bus: mei device | |
838 | */ | |
839 | void mei_cl_bus_rescan(struct mei_device *bus) | |
840 | { | |
841 | struct mei_cl_device *cldev, *n; | |
842 | struct mei_me_client *me_cl; | |
e46980a1 | 843 | |
6009595a TW |
844 | down_read(&bus->me_clients_rwsem); |
845 | list_for_each_entry(me_cl, &bus->me_clients, list) | |
846 | mei_cl_dev_init(bus, me_cl); | |
847 | up_read(&bus->me_clients_rwsem); | |
e46980a1 | 848 | |
6009595a TW |
849 | mutex_lock(&bus->cl_bus_lock); |
850 | list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) { | |
e46980a1 | 851 | |
6009595a TW |
852 | if (!mei_me_cl_is_active(cldev->me_cl)) { |
853 | mei_cl_bus_remove_device(cldev); | |
854 | continue; | |
855 | } | |
e46980a1 | 856 | |
6009595a TW |
857 | if (cldev->is_added) |
858 | continue; | |
859 | ||
860 | if (mei_cl_dev_setup(bus, cldev)) | |
861 | mei_cl_bus_dev_add(cldev); | |
862 | else { | |
863 | list_del_init(&cldev->bus_list); | |
864 | put_device(&cldev->dev); | |
865 | } | |
866 | } | |
867 | mutex_unlock(&bus->cl_bus_lock); | |
868 | ||
869 | dev_dbg(bus->dev, "rescan end"); | |
62382997 | 870 | } |
e46980a1 | 871 | |
62382997 TW |
872 | int __mei_cl_driver_register(struct mei_cl_driver *cldrv, struct module *owner) |
873 | { | |
874 | int err; | |
e46980a1 | 875 | |
62382997 TW |
876 | cldrv->driver.name = cldrv->name; |
877 | cldrv->driver.owner = owner; | |
878 | cldrv->driver.bus = &mei_cl_bus_type; | |
e46980a1 | 879 | |
62382997 TW |
880 | err = driver_register(&cldrv->driver); |
881 | if (err) | |
882 | return err; | |
e46980a1 | 883 | |
62382997 | 884 | pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); |
e46980a1 | 885 | |
62382997 | 886 | return 0; |
e46980a1 | 887 | } |
62382997 | 888 | EXPORT_SYMBOL_GPL(__mei_cl_driver_register); |
e46980a1 | 889 | |
62382997 | 890 | void mei_cl_driver_unregister(struct mei_cl_driver *cldrv) |
cf3baefb | 891 | { |
62382997 | 892 | driver_unregister(&cldrv->driver); |
cf3baefb | 893 | |
62382997 | 894 | pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); |
cf3baefb | 895 | } |
62382997 | 896 | EXPORT_SYMBOL_GPL(mei_cl_driver_unregister); |
cf3baefb | 897 | |
6009595a | 898 | |
cf3baefb SO |
899 | int __init mei_cl_bus_init(void) |
900 | { | |
901 | return bus_register(&mei_cl_bus_type); | |
902 | } | |
903 | ||
904 | void __exit mei_cl_bus_exit(void) | |
905 | { | |
906 | bus_unregister(&mei_cl_bus_type); | |
907 | } |