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