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 TW |
128 | /* wait on event only if there is no other waiter */ |
129 | if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) { | |
e5354107 | 130 | |
62382997 | 131 | mutex_unlock(&bus->device_lock); |
3e833295 | 132 | |
62382997 TW |
133 | if (wait_event_interruptible(cl->rx_wait, |
134 | (!list_empty(&cl->rd_completed)) || | |
135 | (!mei_cl_is_connected(cl)))) { | |
e5354107 | 136 | |
62382997 TW |
137 | if (signal_pending(current)) |
138 | return -EINTR; | |
139 | return -ERESTARTSYS; | |
140 | } | |
141 | ||
142 | mutex_lock(&bus->device_lock); | |
143 | ||
144 | if (!mei_cl_is_connected(cl)) { | |
145 | rets = -EBUSY; | |
146 | goto out; | |
147 | } | |
e5354107 SO |
148 | } |
149 | ||
62382997 TW |
150 | cb = mei_cl_read_cb(cl, NULL); |
151 | if (!cb) { | |
152 | rets = 0; | |
153 | goto out; | |
154 | } | |
e5354107 | 155 | |
62382997 TW |
156 | copy: |
157 | if (cb->status) { | |
158 | rets = cb->status; | |
159 | goto free; | |
160 | } | |
007d64eb | 161 | |
62382997 TW |
162 | r_length = min_t(size_t, length, cb->buf_idx); |
163 | memcpy(buf, cb->buf.data, r_length); | |
164 | rets = r_length; | |
007d64eb | 165 | |
62382997 TW |
166 | free: |
167 | mei_io_cb_free(cb); | |
168 | out: | |
169 | mutex_unlock(&bus->device_lock); | |
170 | ||
171 | return rets; | |
007d64eb | 172 | } |
007d64eb | 173 | |
62382997 | 174 | /** |
d49dc5e7 | 175 | * mei_cldev_send - me device send (write) |
62382997 TW |
176 | * |
177 | * @cldev: me client device | |
178 | * @buf: buffer to send | |
179 | * @length: buffer length | |
180 | * | |
181 | * Return: written size in bytes or < 0 on error | |
182 | */ | |
d49dc5e7 | 183 | ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length) |
007d64eb | 184 | { |
62382997 | 185 | struct mei_cl *cl = cldev->cl; |
007d64eb | 186 | |
62382997 TW |
187 | if (cl == NULL) |
188 | return -ENODEV; | |
007d64eb | 189 | |
62382997 | 190 | return __mei_cl_send(cl, buf, length, 1); |
007d64eb | 191 | } |
d49dc5e7 | 192 | EXPORT_SYMBOL_GPL(mei_cldev_send); |
007d64eb | 193 | |
62382997 | 194 | /** |
d49dc5e7 | 195 | * mei_cldev_recv - client receive (read) |
62382997 TW |
196 | * |
197 | * @cldev: me client device | |
df7f5447 | 198 | * @buf: buffer to receive |
62382997 TW |
199 | * @length: buffer length |
200 | * | |
201 | * Return: read size in bytes of < 0 on error | |
202 | */ | |
d49dc5e7 | 203 | ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length) |
e5354107 | 204 | { |
62382997 | 205 | struct mei_cl *cl = cldev->cl; |
e5354107 | 206 | |
62382997 TW |
207 | if (cl == NULL) |
208 | return -ENODEV; | |
e5354107 | 209 | |
62382997 | 210 | return __mei_cl_recv(cl, buf, length); |
e5354107 | 211 | } |
d49dc5e7 | 212 | EXPORT_SYMBOL_GPL(mei_cldev_recv); |
e5354107 | 213 | |
62382997 | 214 | /** |
d49dc5e7 | 215 | * mei_cl_bus_event_work - dispatch rx event for a bus device |
62382997 TW |
216 | * and schedule new work |
217 | * | |
218 | * @work: work | |
219 | */ | |
d49dc5e7 | 220 | static void mei_cl_bus_event_work(struct work_struct *work) |
e5354107 | 221 | { |
62382997 | 222 | struct mei_cl_device *cldev; |
bc46b45a | 223 | struct mei_device *bus; |
c93b76b3 | 224 | |
62382997 | 225 | cldev = container_of(work, struct mei_cl_device, event_work); |
007d64eb | 226 | |
bc46b45a AU |
227 | bus = cldev->bus; |
228 | ||
62382997 TW |
229 | if (cldev->event_cb) |
230 | cldev->event_cb(cldev, cldev->events, cldev->event_context); | |
007d64eb | 231 | |
62382997 | 232 | cldev->events = 0; |
e5354107 | 233 | |
62382997 | 234 | /* Prepare for the next read */ |
bc46b45a AU |
235 | if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) { |
236 | mutex_lock(&bus->device_lock); | |
bb2ef9c3 | 237 | mei_cl_read_start(cldev->cl, 0, NULL); |
bc46b45a AU |
238 | mutex_unlock(&bus->device_lock); |
239 | } | |
bb2ef9c3 AU |
240 | } |
241 | ||
242 | /** | |
243 | * mei_cl_bus_notify_event - schedule notify cb on bus client | |
244 | * | |
245 | * @cl: host client | |
850f8940 TW |
246 | * |
247 | * Return: true if event was scheduled | |
248 | * false if the client is not waiting for event | |
bb2ef9c3 | 249 | */ |
850f8940 | 250 | bool mei_cl_bus_notify_event(struct mei_cl *cl) |
bb2ef9c3 AU |
251 | { |
252 | struct mei_cl_device *cldev = cl->cldev; | |
253 | ||
254 | if (!cldev || !cldev->event_cb) | |
850f8940 | 255 | return false; |
bb2ef9c3 AU |
256 | |
257 | if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF))) | |
850f8940 | 258 | return false; |
bb2ef9c3 AU |
259 | |
260 | if (!cl->notify_ev) | |
850f8940 | 261 | return false; |
bb2ef9c3 AU |
262 | |
263 | set_bit(MEI_CL_EVENT_NOTIF, &cldev->events); | |
264 | ||
265 | schedule_work(&cldev->event_work); | |
266 | ||
267 | cl->notify_ev = false; | |
850f8940 TW |
268 | |
269 | return true; | |
e5354107 SO |
270 | } |
271 | ||
62382997 | 272 | /** |
a1f9ae2b | 273 | * mei_cl_bus_rx_event - schedule rx event |
62382997 TW |
274 | * |
275 | * @cl: host client | |
a1f9ae2b TW |
276 | * |
277 | * Return: true if event was scheduled | |
278 | * false if the client is not waiting for event | |
62382997 | 279 | */ |
a1f9ae2b | 280 | bool mei_cl_bus_rx_event(struct mei_cl *cl) |
e5354107 | 281 | { |
62382997 | 282 | struct mei_cl_device *cldev = cl->cldev; |
d49ed64a | 283 | |
62382997 | 284 | if (!cldev || !cldev->event_cb) |
a1f9ae2b | 285 | return false; |
d49ed64a | 286 | |
bb2ef9c3 | 287 | if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX))) |
a1f9ae2b | 288 | return false; |
bb2ef9c3 | 289 | |
62382997 | 290 | set_bit(MEI_CL_EVENT_RX, &cldev->events); |
a7b71bc0 | 291 | |
62382997 | 292 | schedule_work(&cldev->event_work); |
a1f9ae2b TW |
293 | |
294 | return true; | |
a7b71bc0 | 295 | } |
d49ed64a | 296 | |
62382997 | 297 | /** |
d49dc5e7 | 298 | * mei_cldev_register_event_cb - register event callback |
62382997 TW |
299 | * |
300 | * @cldev: me client devices | |
301 | * @event_cb: callback function | |
bb2ef9c3 | 302 | * @events_mask: requested events bitmask |
62382997 TW |
303 | * @context: driver context data |
304 | * | |
305 | * Return: 0 on success | |
306 | * -EALREADY if an callback is already registered | |
307 | * <0 on other errors | |
308 | */ | |
d49dc5e7 TW |
309 | int mei_cldev_register_event_cb(struct mei_cl_device *cldev, |
310 | unsigned long events_mask, | |
311 | mei_cldev_event_cb_t event_cb, void *context) | |
e5354107 | 312 | { |
bc46b45a | 313 | struct mei_device *bus = cldev->bus; |
48168f45 TW |
314 | int ret; |
315 | ||
62382997 TW |
316 | if (cldev->event_cb) |
317 | return -EALREADY; | |
e5354107 | 318 | |
62382997 | 319 | cldev->events = 0; |
bb2ef9c3 | 320 | cldev->events_mask = events_mask; |
62382997 TW |
321 | cldev->event_cb = event_cb; |
322 | cldev->event_context = context; | |
d49dc5e7 | 323 | INIT_WORK(&cldev->event_work, mei_cl_bus_event_work); |
a7b71bc0 | 324 | |
bb2ef9c3 | 325 | if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) { |
bc46b45a | 326 | mutex_lock(&bus->device_lock); |
bb2ef9c3 | 327 | ret = mei_cl_read_start(cldev->cl, 0, NULL); |
bc46b45a | 328 | mutex_unlock(&bus->device_lock); |
bb2ef9c3 AU |
329 | if (ret && ret != -EBUSY) |
330 | return ret; | |
331 | } | |
332 | ||
333 | if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) { | |
bc46b45a | 334 | mutex_lock(&bus->device_lock); |
bb2ef9c3 | 335 | ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0); |
bc46b45a | 336 | mutex_unlock(&bus->device_lock); |
bb2ef9c3 AU |
337 | if (ret) |
338 | return ret; | |
339 | } | |
e5354107 | 340 | |
62382997 TW |
341 | return 0; |
342 | } | |
d49dc5e7 | 343 | EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb); |
e5354107 | 344 | |
62382997 | 345 | /** |
d49dc5e7 | 346 | * mei_cldev_get_drvdata - driver data getter |
62382997 TW |
347 | * |
348 | * @cldev: mei client device | |
349 | * | |
350 | * Return: driver private data | |
351 | */ | |
d49dc5e7 | 352 | void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev) |
62382997 TW |
353 | { |
354 | return dev_get_drvdata(&cldev->dev); | |
e5354107 | 355 | } |
d49dc5e7 | 356 | EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata); |
e5354107 | 357 | |
62382997 | 358 | /** |
d49dc5e7 | 359 | * mei_cldev_set_drvdata - driver data setter |
62382997 TW |
360 | * |
361 | * @cldev: mei client device | |
362 | * @data: data to store | |
363 | */ | |
d49dc5e7 | 364 | void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data) |
e5354107 | 365 | { |
62382997 | 366 | dev_set_drvdata(&cldev->dev, data); |
e5354107 | 367 | } |
d49dc5e7 | 368 | EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata); |
333e4ee0 | 369 | |
baeacd03 TW |
370 | /** |
371 | * mei_cldev_uuid - return uuid of the underlying me client | |
372 | * | |
373 | * @cldev: mei client device | |
374 | * | |
375 | * Return: me client uuid | |
376 | */ | |
377 | const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev) | |
378 | { | |
379 | return mei_me_cl_uuid(cldev->me_cl); | |
380 | } | |
381 | EXPORT_SYMBOL_GPL(mei_cldev_uuid); | |
382 | ||
383 | /** | |
384 | * mei_cldev_ver - return protocol version of the underlying me client | |
385 | * | |
386 | * @cldev: mei client device | |
387 | * | |
388 | * Return: me client protocol version | |
389 | */ | |
390 | u8 mei_cldev_ver(const struct mei_cl_device *cldev) | |
391 | { | |
392 | return mei_me_cl_ver(cldev->me_cl); | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(mei_cldev_ver); | |
395 | ||
01a14ede TW |
396 | /** |
397 | * mei_cldev_enabled - check whether the device is enabled | |
398 | * | |
399 | * @cldev: mei client device | |
400 | * | |
401 | * Return: true if me client is initialized and connected | |
402 | */ | |
403 | bool mei_cldev_enabled(struct mei_cl_device *cldev) | |
404 | { | |
405 | return cldev->cl && mei_cl_is_connected(cldev->cl); | |
406 | } | |
407 | EXPORT_SYMBOL_GPL(mei_cldev_enabled); | |
408 | ||
62382997 | 409 | /** |
d49dc5e7 | 410 | * mei_cldev_enable_device - enable me client device |
62382997 TW |
411 | * create connection with me client |
412 | * | |
413 | * @cldev: me client device | |
414 | * | |
415 | * Return: 0 on success and < 0 on error | |
416 | */ | |
d49dc5e7 | 417 | int mei_cldev_enable(struct mei_cl_device *cldev) |
333e4ee0 | 418 | { |
6009595a TW |
419 | struct mei_device *bus = cldev->bus; |
420 | struct mei_cl *cl; | |
421 | int ret; | |
333e4ee0 | 422 | |
6009595a | 423 | cl = cldev->cl; |
333e4ee0 | 424 | |
6009595a TW |
425 | if (!cl) { |
426 | mutex_lock(&bus->device_lock); | |
7851e008 | 427 | cl = mei_cl_alloc_linked(bus); |
6009595a TW |
428 | mutex_unlock(&bus->device_lock); |
429 | if (IS_ERR(cl)) | |
430 | return PTR_ERR(cl); | |
431 | /* update pointers */ | |
432 | cldev->cl = cl; | |
433 | cl->cldev = cldev; | |
434 | } | |
333e4ee0 | 435 | |
62382997 | 436 | mutex_lock(&bus->device_lock); |
62382997 | 437 | if (mei_cl_is_connected(cl)) { |
6009595a TW |
438 | ret = 0; |
439 | goto out; | |
62382997 | 440 | } |
333e4ee0 | 441 | |
6009595a TW |
442 | if (!mei_me_cl_is_active(cldev->me_cl)) { |
443 | dev_err(&cldev->dev, "me client is not active\n"); | |
444 | ret = -ENOTTY; | |
445 | goto out; | |
62382997 TW |
446 | } |
447 | ||
6009595a TW |
448 | ret = mei_cl_connect(cl, cldev->me_cl, NULL); |
449 | if (ret < 0) | |
450 | dev_err(&cldev->dev, "cannot connect\n"); | |
451 | ||
452 | out: | |
62382997 TW |
453 | mutex_unlock(&bus->device_lock); |
454 | ||
6009595a | 455 | return ret; |
333e4ee0 | 456 | } |
d49dc5e7 | 457 | EXPORT_SYMBOL_GPL(mei_cldev_enable); |
3e833295 | 458 | |
62382997 | 459 | /** |
d49dc5e7 | 460 | * mei_cldev_disable - disable me client device |
62382997 TW |
461 | * disconnect form the me client |
462 | * | |
463 | * @cldev: me client device | |
464 | * | |
465 | * Return: 0 on success and < 0 on error | |
466 | */ | |
d49dc5e7 | 467 | int mei_cldev_disable(struct mei_cl_device *cldev) |
3e833295 | 468 | { |
b37719c3 | 469 | struct mei_device *bus; |
6009595a TW |
470 | struct mei_cl *cl; |
471 | int err; | |
3e833295 | 472 | |
6009595a | 473 | if (!cldev || !cldev->cl) |
3e833295 SO |
474 | return -ENODEV; |
475 | ||
6009595a TW |
476 | cl = cldev->cl; |
477 | ||
478 | bus = cldev->bus; | |
4234a6de | 479 | |
62382997 | 480 | cldev->event_cb = NULL; |
3e833295 | 481 | |
62382997 | 482 | mutex_lock(&bus->device_lock); |
4234a6de | 483 | |
62382997 TW |
484 | if (!mei_cl_is_connected(cl)) { |
485 | dev_err(bus->dev, "Already disconnected"); | |
486 | err = 0; | |
79563db9 TW |
487 | goto out; |
488 | } | |
4234a6de | 489 | |
62382997 | 490 | err = mei_cl_disconnect(cl); |
6009595a | 491 | if (err < 0) |
62382997 | 492 | dev_err(bus->dev, "Could not disconnect from the ME client"); |
3e833295 | 493 | |
6009595a | 494 | out: |
62382997 TW |
495 | /* Flush queues and remove any pending read */ |
496 | mei_cl_flush_queues(cl, NULL); | |
6009595a TW |
497 | mei_cl_unlink(cl); |
498 | ||
499 | kfree(cl); | |
500 | cldev->cl = NULL; | |
3e833295 | 501 | |
b37719c3 | 502 | mutex_unlock(&bus->device_lock); |
62382997 | 503 | return err; |
3e833295 | 504 | } |
d49dc5e7 | 505 | EXPORT_SYMBOL_GPL(mei_cldev_disable); |
3e833295 | 506 | |
688a9cce TW |
507 | /** |
508 | * mei_cl_device_find - find matching entry in the driver id table | |
509 | * | |
510 | * @cldev: me client device | |
511 | * @cldrv: me client driver | |
512 | * | |
513 | * Return: id on success; NULL if no id is matching | |
514 | */ | |
515 | static const | |
516 | struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev, | |
517 | struct mei_cl_driver *cldrv) | |
3e833295 | 518 | { |
62382997 TW |
519 | const struct mei_cl_device_id *id; |
520 | const uuid_le *uuid; | |
b26864ca TW |
521 | u8 version; |
522 | bool match; | |
3e833295 | 523 | |
62382997 | 524 | uuid = mei_me_cl_uuid(cldev->me_cl); |
b26864ca | 525 | version = mei_me_cl_ver(cldev->me_cl); |
3e833295 | 526 | |
62382997 | 527 | id = cldrv->id_table; |
62382997 | 528 | while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { |
62382997 | 529 | if (!uuid_le_cmp(*uuid, id->uuid)) { |
b26864ca | 530 | match = true; |
688a9cce | 531 | |
b26864ca TW |
532 | if (cldev->name[0]) |
533 | if (strncmp(cldev->name, id->name, | |
534 | sizeof(id->name))) | |
535 | match = false; | |
688a9cce | 536 | |
b26864ca TW |
537 | if (id->version != MEI_CL_VERSION_ANY) |
538 | if (id->version != version) | |
539 | match = false; | |
540 | if (match) | |
688a9cce | 541 | return id; |
62382997 | 542 | } |
e2b31644 | 543 | |
62382997 TW |
544 | id++; |
545 | } | |
3e833295 | 546 | |
688a9cce TW |
547 | return NULL; |
548 | } | |
549 | ||
550 | /** | |
551 | * mei_cl_device_match - device match function | |
552 | * | |
553 | * @dev: device | |
554 | * @drv: driver | |
555 | * | |
556 | * Return: 1 if matching device was found 0 otherwise | |
557 | */ | |
558 | static int mei_cl_device_match(struct device *dev, struct device_driver *drv) | |
559 | { | |
560 | struct mei_cl_device *cldev = to_mei_cl_device(dev); | |
561 | struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); | |
562 | const struct mei_cl_device_id *found_id; | |
563 | ||
564 | if (!cldev) | |
565 | return 0; | |
566 | ||
71ce7891 TW |
567 | if (!cldev->do_match) |
568 | return 0; | |
569 | ||
688a9cce TW |
570 | if (!cldrv || !cldrv->id_table) |
571 | return 0; | |
572 | ||
573 | found_id = mei_cl_device_find(cldev, cldrv); | |
574 | if (found_id) | |
575 | return 1; | |
576 | ||
62382997 TW |
577 | return 0; |
578 | } | |
e2b31644 | 579 | |
feb8cd0f TW |
580 | /** |
581 | * mei_cl_device_probe - bus probe function | |
582 | * | |
583 | * @dev: device | |
584 | * | |
585 | * Return: 0 on success; < 0 otherwise | |
586 | */ | |
62382997 TW |
587 | static int mei_cl_device_probe(struct device *dev) |
588 | { | |
feb8cd0f | 589 | struct mei_cl_device *cldev; |
62382997 | 590 | struct mei_cl_driver *cldrv; |
feb8cd0f | 591 | const struct mei_cl_device_id *id; |
b9c79543 | 592 | int ret; |
feb8cd0f TW |
593 | |
594 | cldev = to_mei_cl_device(dev); | |
595 | cldrv = to_mei_cl_driver(dev->driver); | |
3e833295 | 596 | |
62382997 TW |
597 | if (!cldev) |
598 | return 0; | |
3e833295 | 599 | |
62382997 TW |
600 | if (!cldrv || !cldrv->probe) |
601 | return -ENODEV; | |
602 | ||
feb8cd0f TW |
603 | id = mei_cl_device_find(cldev, cldrv); |
604 | if (!id) | |
605 | return -ENODEV; | |
62382997 | 606 | |
b9c79543 AK |
607 | ret = cldrv->probe(cldev, id); |
608 | if (ret) | |
609 | return ret; | |
62382997 | 610 | |
b9c79543 AK |
611 | __module_get(THIS_MODULE); |
612 | return 0; | |
62382997 | 613 | } |
3e833295 | 614 | |
feb8cd0f TW |
615 | /** |
616 | * mei_cl_device_remove - remove device from the bus | |
617 | * | |
618 | * @dev: device | |
619 | * | |
620 | * Return: 0 on success; < 0 otherwise | |
621 | */ | |
62382997 TW |
622 | static int mei_cl_device_remove(struct device *dev) |
623 | { | |
624 | struct mei_cl_device *cldev = to_mei_cl_device(dev); | |
625 | struct mei_cl_driver *cldrv; | |
feb8cd0f | 626 | int ret = 0; |
3e833295 | 627 | |
62382997 TW |
628 | if (!cldev || !dev->driver) |
629 | return 0; | |
630 | ||
631 | if (cldev->event_cb) { | |
632 | cldev->event_cb = NULL; | |
633 | cancel_work_sync(&cldev->event_work); | |
3d33ff24 TW |
634 | } |
635 | ||
62382997 | 636 | cldrv = to_mei_cl_driver(dev->driver); |
feb8cd0f TW |
637 | if (cldrv->remove) |
638 | ret = cldrv->remove(cldev); | |
3e833295 | 639 | |
feb8cd0f TW |
640 | module_put(THIS_MODULE); |
641 | dev->driver = NULL; | |
642 | return ret; | |
3e833295 | 643 | |
3e833295 SO |
644 | } |
645 | ||
62382997 TW |
646 | static ssize_t name_show(struct device *dev, struct device_attribute *a, |
647 | char *buf) | |
3e833295 | 648 | { |
62382997 | 649 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
3e833295 | 650 | |
423de92f | 651 | return scnprintf(buf, PAGE_SIZE, "%s", cldev->name); |
3e833295 | 652 | } |
62382997 | 653 | static DEVICE_ATTR_RO(name); |
3e833295 | 654 | |
62382997 TW |
655 | static ssize_t uuid_show(struct device *dev, struct device_attribute *a, |
656 | char *buf) | |
3e833295 | 657 | { |
62382997 TW |
658 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
659 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); | |
3e833295 | 660 | |
423de92f | 661 | return scnprintf(buf, PAGE_SIZE, "%pUl", uuid); |
3e833295 | 662 | } |
62382997 | 663 | static DEVICE_ATTR_RO(uuid); |
3e833295 | 664 | |
40b7320e TW |
665 | static ssize_t version_show(struct device *dev, struct device_attribute *a, |
666 | char *buf) | |
667 | { | |
668 | struct mei_cl_device *cldev = to_mei_cl_device(dev); | |
669 | u8 version = mei_me_cl_ver(cldev->me_cl); | |
40b7320e | 670 | |
423de92f | 671 | return scnprintf(buf, PAGE_SIZE, "%02X", version); |
40b7320e TW |
672 | } |
673 | static DEVICE_ATTR_RO(version); | |
674 | ||
62382997 TW |
675 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
676 | char *buf) | |
3e833295 | 677 | { |
62382997 TW |
678 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
679 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); | |
3e833295 | 680 | |
423de92f | 681 | return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid); |
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); | |
025fb792 AU |
984 | struct mei_me_client *me_cl; |
985 | ||
986 | mutex_lock(&bus->device_lock); | |
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); | |
991 | mutex_unlock(&bus->device_lock); | |
a816a00e AU |
992 | |
993 | mei_cl_bus_rescan(bus); | |
994 | } | |
995 | ||
d49dc5e7 TW |
996 | int __mei_cldev_driver_register(struct mei_cl_driver *cldrv, |
997 | struct module *owner) | |
62382997 TW |
998 | { |
999 | int err; | |
e46980a1 | 1000 | |
62382997 TW |
1001 | cldrv->driver.name = cldrv->name; |
1002 | cldrv->driver.owner = owner; | |
1003 | cldrv->driver.bus = &mei_cl_bus_type; | |
e46980a1 | 1004 | |
62382997 TW |
1005 | err = driver_register(&cldrv->driver); |
1006 | if (err) | |
1007 | return err; | |
e46980a1 | 1008 | |
62382997 | 1009 | pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); |
e46980a1 | 1010 | |
62382997 | 1011 | return 0; |
e46980a1 | 1012 | } |
d49dc5e7 | 1013 | EXPORT_SYMBOL_GPL(__mei_cldev_driver_register); |
e46980a1 | 1014 | |
d49dc5e7 | 1015 | void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv) |
cf3baefb | 1016 | { |
62382997 | 1017 | driver_unregister(&cldrv->driver); |
cf3baefb | 1018 | |
62382997 | 1019 | pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); |
cf3baefb | 1020 | } |
d49dc5e7 | 1021 | EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister); |
cf3baefb | 1022 | |
6009595a | 1023 | |
cf3baefb SO |
1024 | int __init mei_cl_bus_init(void) |
1025 | { | |
1026 | return bus_register(&mei_cl_bus_type); | |
1027 | } | |
1028 | ||
1029 | void __exit mei_cl_bus_exit(void) | |
1030 | { | |
1031 | bus_unregister(&mei_cl_bus_type); | |
1032 | } |