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