blob: 2d9c5dd06e423266680294e4d806e2e736d32156 [file] [log] [blame]
Samuel Ortize5354102013-03-27 17:29:53 +02001/*
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>
Samuel Ortiz3e833292013-03-27 17:29:55 +020019#include <linux/sched.h>
Samuel Ortize5354102013-03-27 17:29:53 +020020#include <linux/init.h>
21#include <linux/errno.h>
22#include <linux/slab.h>
23#include <linux/mutex.h>
24#include <linux/interrupt.h>
Samuel Ortize5354102013-03-27 17:29:53 +020025#include <linux/mei_cl_bus.h>
26
27#include "mei_dev.h"
Samuel Ortiz3e833292013-03-27 17:29:55 +020028#include "client.h"
Samuel Ortize5354102013-03-27 17:29:53 +020029
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
Tomas Winkler62382992015-07-23 15:08:35 +030033/**
34 * __mei_cl_send - internal client send (write)
35 *
36 * @cl: host client
37 * @buf: buffer to send
38 * @length: buffer length
Alexander Usyskine0cb6b22016-11-08 18:26:08 +020039 * @mode: sending mode
Tomas Winkler62382992015-07-23 15:08:35 +030040 *
41 * Return: written size bytes or < 0 on error
42 */
43ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
Alexander Usyskine0cb6b22016-11-08 18:26:08 +020044 unsigned int mode)
Tomas Winkler62382992015-07-23 15:08:35 +030045{
46 struct mei_device *bus;
Alexander Usyskin6cbb0972016-02-10 23:57:26 +020047 struct mei_cl_cb *cb;
Tomas Winkler62382992015-07-23 15:08:35 +030048 ssize_t rets;
49
50 if (WARN_ON(!cl || !cl->dev))
51 return -ENODEV;
52
53 bus = cl->dev;
54
55 mutex_lock(&bus->device_lock);
Alexander Usyskin15c13df2016-02-07 23:35:32 +020056 if (bus->dev_state != MEI_DEV_ENABLED) {
57 rets = -ENODEV;
58 goto out;
59 }
60
Tomas Winkler62382992015-07-23 15:08:35 +030061 if (!mei_cl_is_connected(cl)) {
62 rets = -ENODEV;
63 goto out;
64 }
65
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 }
71
72 if (length > mei_cl_mtu(cl)) {
73 rets = -EFBIG;
74 goto out;
75 }
76
77 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
78 if (!cb) {
79 rets = -ENOMEM;
80 goto out;
81 }
82
Alexander Usyskine0cb6b22016-11-08 18:26:08 +020083 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
84 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
Tomas Winkler62382992015-07-23 15:08:35 +030085 memcpy(cb->buf.data, buf, length);
86
Alexander Usyskine0cb6b22016-11-08 18:26:08 +020087 rets = mei_cl_write(cl, cb);
Tomas Winkler62382992015-07-23 15:08:35 +030088
89out:
90 mutex_unlock(&bus->device_lock);
Tomas Winkler62382992015-07-23 15:08:35 +030091
92 return rets;
93}
94
95/**
96 * __mei_cl_recv - internal client receive (read)
97 *
98 * @cl: host client
Tomas Winklerdf7f5442015-10-13 15:02:40 +030099 * @buf: buffer to receive
Tomas Winkler62382992015-07-23 15:08:35 +0300100 * @length: buffer length
Alexander Usyskin076802d2016-12-03 00:15:25 +0200101 * @mode: io mode
Tomas Winkler62382992015-07-23 15:08:35 +0300102 *
103 * Return: read size in bytes of < 0 on error
104 */
Alexander Usyskin076802d2016-12-03 00:15:25 +0200105ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
106 unsigned int mode)
Tomas Winkler62382992015-07-23 15:08:35 +0300107{
108 struct mei_device *bus;
109 struct mei_cl_cb *cb;
110 size_t r_length;
111 ssize_t rets;
Alexander Usyskin076802d2016-12-03 00:15:25 +0200112 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
Tomas Winkler62382992015-07-23 15:08:35 +0300113
114 if (WARN_ON(!cl || !cl->dev))
115 return -ENODEV;
116
117 bus = cl->dev;
118
119 mutex_lock(&bus->device_lock);
Alexander Usyskin15c13df2016-02-07 23:35:32 +0200120 if (bus->dev_state != MEI_DEV_ENABLED) {
121 rets = -ENODEV;
122 goto out;
123 }
Tomas Winkler62382992015-07-23 15:08:35 +0300124
125 cb = mei_cl_read_cb(cl, NULL);
126 if (cb)
127 goto copy;
128
129 rets = mei_cl_read_start(cl, length, NULL);
130 if (rets && rets != -EBUSY)
131 goto out;
132
Alexander Usyskin076802d2016-12-03 00:15:25 +0200133 if (nonblock) {
134 rets = -EAGAIN;
135 goto out;
136 }
137
Tomas Winkler62382992015-07-23 15:08:35 +0300138 /* wait on event only if there is no other waiter */
Alexander Usyskin1eb5bd42016-06-16 17:58:54 +0300139 /* synchronized under device mutex */
140 if (!waitqueue_active(&cl->rx_wait)) {
Tomas Winkler62382992015-07-23 15:08:35 +0300141
142 mutex_unlock(&bus->device_lock);
143
144 if (wait_event_interruptible(cl->rx_wait,
145 (!list_empty(&cl->rd_completed)) ||
146 (!mei_cl_is_connected(cl)))) {
147
148 if (signal_pending(current))
149 return -EINTR;
150 return -ERESTARTSYS;
151 }
152
153 mutex_lock(&bus->device_lock);
154
155 if (!mei_cl_is_connected(cl)) {
Tomas Winkler2d4d5482016-06-23 00:25:31 +0300156 rets = -ENODEV;
Tomas Winkler62382992015-07-23 15:08:35 +0300157 goto out;
158 }
159 }
160
161 cb = mei_cl_read_cb(cl, NULL);
162 if (!cb) {
163 rets = 0;
164 goto out;
165 }
166
167copy:
168 if (cb->status) {
169 rets = cb->status;
170 goto free;
171 }
172
173 r_length = min_t(size_t, length, cb->buf_idx);
174 memcpy(buf, cb->buf.data, r_length);
175 rets = r_length;
176
177free:
178 mei_io_cb_free(cb);
179out:
180 mutex_unlock(&bus->device_lock);
181
182 return rets;
183}
184
185/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300186 * mei_cldev_send - me device send (write)
Tomas Winkler62382992015-07-23 15:08:35 +0300187 *
188 * @cldev: me client device
189 * @buf: buffer to send
190 * @length: buffer length
191 *
192 * Return: written size in bytes or < 0 on error
193 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300194ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
Tomas Winkler62382992015-07-23 15:08:35 +0300195{
196 struct mei_cl *cl = cldev->cl;
197
Alexander Usyskine0cb6b22016-11-08 18:26:08 +0200198 return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING);
Tomas Winkler62382992015-07-23 15:08:35 +0300199}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300200EXPORT_SYMBOL_GPL(mei_cldev_send);
Tomas Winkler62382992015-07-23 15:08:35 +0300201
202/**
Alexander Usyskin076802d2016-12-03 00:15:25 +0200203 * mei_cldev_recv_nonblock - non block client receive (read)
204 *
205 * @cldev: me client device
206 * @buf: buffer to receive
207 * @length: buffer length
208 *
209 * Return: read size in bytes of < 0 on error
210 * -EAGAIN if function will block.
211 */
212ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
213 size_t length)
214{
215 struct mei_cl *cl = cldev->cl;
216
217 return __mei_cl_recv(cl, buf, length, MEI_CL_IO_RX_NONBLOCK);
218}
219EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
220
221/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300222 * mei_cldev_recv - client receive (read)
Tomas Winkler62382992015-07-23 15:08:35 +0300223 *
224 * @cldev: me client device
Tomas Winklerdf7f5442015-10-13 15:02:40 +0300225 * @buf: buffer to receive
Tomas Winkler62382992015-07-23 15:08:35 +0300226 * @length: buffer length
227 *
228 * Return: read size in bytes of < 0 on error
229 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300230ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
Tomas Winkler62382992015-07-23 15:08:35 +0300231{
232 struct mei_cl *cl = cldev->cl;
233
Alexander Usyskin076802d2016-12-03 00:15:25 +0200234 return __mei_cl_recv(cl, buf, length, 0);
Tomas Winkler62382992015-07-23 15:08:35 +0300235}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300236EXPORT_SYMBOL_GPL(mei_cldev_recv);
Tomas Winkler62382992015-07-23 15:08:35 +0300237
238/**
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200239 * mei_cl_bus_rx_work - dispatch rx event for a bus device
Tomas Winkler62382992015-07-23 15:08:35 +0300240 *
241 * @work: work
242 */
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200243static void mei_cl_bus_rx_work(struct work_struct *work)
Tomas Winkler62382992015-07-23 15:08:35 +0300244{
245 struct mei_cl_device *cldev;
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400246 struct mei_device *bus;
Tomas Winkler62382992015-07-23 15:08:35 +0300247
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200248 cldev = container_of(work, struct mei_cl_device, rx_work);
Tomas Winkler62382992015-07-23 15:08:35 +0300249
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400250 bus = cldev->bus;
251
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200252 if (cldev->rx_cb)
253 cldev->rx_cb(cldev);
Tomas Winkler62382992015-07-23 15:08:35 +0300254
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200255 mutex_lock(&bus->device_lock);
256 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
257 mutex_unlock(&bus->device_lock);
258}
Tomas Winkler62382992015-07-23 15:08:35 +0300259
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200260/**
261 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
262 *
263 * @work: work
264 */
265static void mei_cl_bus_notif_work(struct work_struct *work)
266{
267 struct mei_cl_device *cldev;
268
269 cldev = container_of(work, struct mei_cl_device, notif_work);
270
271 if (cldev->notif_cb)
272 cldev->notif_cb(cldev);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300273}
274
275/**
276 * mei_cl_bus_notify_event - schedule notify cb on bus client
277 *
278 * @cl: host client
Tomas Winkler850f8942016-02-07 23:35:31 +0200279 *
280 * Return: true if event was scheduled
281 * false if the client is not waiting for event
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300282 */
Tomas Winkler850f8942016-02-07 23:35:31 +0200283bool mei_cl_bus_notify_event(struct mei_cl *cl)
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300284{
285 struct mei_cl_device *cldev = cl->cldev;
286
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200287 if (!cldev || !cldev->notif_cb)
Tomas Winkler850f8942016-02-07 23:35:31 +0200288 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300289
290 if (!cl->notify_ev)
Tomas Winkler850f8942016-02-07 23:35:31 +0200291 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300292
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200293 schedule_work(&cldev->notif_work);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300294
295 cl->notify_ev = false;
Tomas Winkler850f8942016-02-07 23:35:31 +0200296
297 return true;
Tomas Winkler62382992015-07-23 15:08:35 +0300298}
299
300/**
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200301 * mei_cl_bus_rx_event - schedule rx event
Tomas Winkler62382992015-07-23 15:08:35 +0300302 *
303 * @cl: host client
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200304 *
305 * Return: true if event was scheduled
306 * false if the client is not waiting for event
Tomas Winkler62382992015-07-23 15:08:35 +0300307 */
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200308bool mei_cl_bus_rx_event(struct mei_cl *cl)
Tomas Winkler62382992015-07-23 15:08:35 +0300309{
310 struct mei_cl_device *cldev = cl->cldev;
311
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200312 if (!cldev || !cldev->rx_cb)
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200313 return false;
Tomas Winkler62382992015-07-23 15:08:35 +0300314
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200315 schedule_work(&cldev->rx_work);
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200316
317 return true;
Tomas Winkler62382992015-07-23 15:08:35 +0300318}
319
320/**
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200321 * mei_cldev_register_rx_cb - register Rx event callback
Tomas Winkler62382992015-07-23 15:08:35 +0300322 *
323 * @cldev: me client devices
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200324 * @rx_cb: callback function
Tomas Winkler62382992015-07-23 15:08:35 +0300325 *
326 * Return: 0 on success
327 * -EALREADY if an callback is already registered
328 * <0 on other errors
329 */
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200330int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
Tomas Winkler62382992015-07-23 15:08:35 +0300331{
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400332 struct mei_device *bus = cldev->bus;
Tomas Winkler48168f42015-07-23 15:08:38 +0300333 int ret;
334
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200335 if (!rx_cb)
336 return -EINVAL;
337 if (cldev->rx_cb)
Tomas Winkler62382992015-07-23 15:08:35 +0300338 return -EALREADY;
339
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200340 cldev->rx_cb = rx_cb;
341 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
Tomas Winkler62382992015-07-23 15:08:35 +0300342
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200343 mutex_lock(&bus->device_lock);
344 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
345 mutex_unlock(&bus->device_lock);
346 if (ret && ret != -EBUSY)
347 return ret;
Tomas Winkler62382992015-07-23 15:08:35 +0300348
349 return 0;
350}
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200351EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
352
353/**
354 * mei_cldev_register_notif_cb - register FW notification event callback
355 *
356 * @cldev: me client devices
357 * @notif_cb: callback function
358 *
359 * Return: 0 on success
360 * -EALREADY if an callback is already registered
361 * <0 on other errors
362 */
363int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
364 mei_cldev_cb_t notif_cb)
365{
366 struct mei_device *bus = cldev->bus;
367 int ret;
368
369 if (!notif_cb)
370 return -EINVAL;
371
372 if (cldev->notif_cb)
373 return -EALREADY;
374
375 cldev->notif_cb = notif_cb;
376 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
377
378 mutex_lock(&bus->device_lock);
379 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
380 mutex_unlock(&bus->device_lock);
381 if (ret)
382 return ret;
383
384 return 0;
385}
386EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
Tomas Winkler62382992015-07-23 15:08:35 +0300387
388/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300389 * mei_cldev_get_drvdata - driver data getter
Tomas Winkler62382992015-07-23 15:08:35 +0300390 *
391 * @cldev: mei client device
392 *
393 * Return: driver private data
394 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300395void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300396{
397 return dev_get_drvdata(&cldev->dev);
398}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300399EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
Tomas Winkler62382992015-07-23 15:08:35 +0300400
401/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300402 * mei_cldev_set_drvdata - driver data setter
Tomas Winkler62382992015-07-23 15:08:35 +0300403 *
404 * @cldev: mei client device
405 * @data: data to store
406 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300407void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
Tomas Winkler62382992015-07-23 15:08:35 +0300408{
409 dev_set_drvdata(&cldev->dev, data);
410}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300411EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
Tomas Winkler62382992015-07-23 15:08:35 +0300412
413/**
Tomas Winklerbaeacd02015-09-10 10:18:02 +0300414 * mei_cldev_uuid - return uuid of the underlying me client
415 *
416 * @cldev: mei client device
417 *
418 * Return: me client uuid
419 */
420const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
421{
422 return mei_me_cl_uuid(cldev->me_cl);
423}
424EXPORT_SYMBOL_GPL(mei_cldev_uuid);
425
426/**
427 * mei_cldev_ver - return protocol version of the underlying me client
428 *
429 * @cldev: mei client device
430 *
431 * Return: me client protocol version
432 */
433u8 mei_cldev_ver(const struct mei_cl_device *cldev)
434{
435 return mei_me_cl_ver(cldev->me_cl);
436}
437EXPORT_SYMBOL_GPL(mei_cldev_ver);
438
439/**
Tomas Winkler01a14ed2015-09-10 10:18:03 +0300440 * mei_cldev_enabled - check whether the device is enabled
441 *
442 * @cldev: mei client device
443 *
444 * Return: true if me client is initialized and connected
445 */
446bool mei_cldev_enabled(struct mei_cl_device *cldev)
447{
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200448 return mei_cl_is_connected(cldev->cl);
Tomas Winkler01a14ed2015-09-10 10:18:03 +0300449}
450EXPORT_SYMBOL_GPL(mei_cldev_enabled);
451
452/**
Alexander Usyskin5026c9c2016-12-14 17:56:51 +0200453 * mei_cldev_enable - enable me client device
Tomas Winkler62382992015-07-23 15:08:35 +0300454 * create connection with me client
455 *
456 * @cldev: me client device
457 *
458 * Return: 0 on success and < 0 on error
459 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300460int mei_cldev_enable(struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300461{
Tomas Winkler60095952015-07-23 15:08:47 +0300462 struct mei_device *bus = cldev->bus;
463 struct mei_cl *cl;
464 int ret;
Tomas Winkler62382992015-07-23 15:08:35 +0300465
Tomas Winkler60095952015-07-23 15:08:47 +0300466 cl = cldev->cl;
Tomas Winkler62382992015-07-23 15:08:35 +0300467
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200468 if (cl->state == MEI_FILE_UNINITIALIZED) {
Tomas Winkler60095952015-07-23 15:08:47 +0300469 mutex_lock(&bus->device_lock);
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200470 ret = mei_cl_link(cl);
Tomas Winkler60095952015-07-23 15:08:47 +0300471 mutex_unlock(&bus->device_lock);
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200472 if (ret)
473 return ret;
Tomas Winkler60095952015-07-23 15:08:47 +0300474 /* update pointers */
Tomas Winkler60095952015-07-23 15:08:47 +0300475 cl->cldev = cldev;
476 }
Tomas Winkler62382992015-07-23 15:08:35 +0300477
478 mutex_lock(&bus->device_lock);
Tomas Winkler62382992015-07-23 15:08:35 +0300479 if (mei_cl_is_connected(cl)) {
Tomas Winkler60095952015-07-23 15:08:47 +0300480 ret = 0;
481 goto out;
Tomas Winkler62382992015-07-23 15:08:35 +0300482 }
483
Tomas Winkler60095952015-07-23 15:08:47 +0300484 if (!mei_me_cl_is_active(cldev->me_cl)) {
485 dev_err(&cldev->dev, "me client is not active\n");
486 ret = -ENOTTY;
487 goto out;
Tomas Winkler62382992015-07-23 15:08:35 +0300488 }
489
Tomas Winkler60095952015-07-23 15:08:47 +0300490 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
491 if (ret < 0)
492 dev_err(&cldev->dev, "cannot connect\n");
493
494out:
Tomas Winkler62382992015-07-23 15:08:35 +0300495 mutex_unlock(&bus->device_lock);
496
Tomas Winkler60095952015-07-23 15:08:47 +0300497 return ret;
Tomas Winkler62382992015-07-23 15:08:35 +0300498}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300499EXPORT_SYMBOL_GPL(mei_cldev_enable);
Tomas Winkler62382992015-07-23 15:08:35 +0300500
501/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300502 * mei_cldev_disable - disable me client device
Tomas Winkler62382992015-07-23 15:08:35 +0300503 * disconnect form the me client
504 *
505 * @cldev: me client device
506 *
507 * Return: 0 on success and < 0 on error
508 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300509int mei_cldev_disable(struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300510{
Tomas Winkler62382992015-07-23 15:08:35 +0300511 struct mei_device *bus;
Tomas Winkler60095952015-07-23 15:08:47 +0300512 struct mei_cl *cl;
513 int err;
Tomas Winkler62382992015-07-23 15:08:35 +0300514
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200515 if (!cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300516 return -ENODEV;
517
Tomas Winkler60095952015-07-23 15:08:47 +0300518 cl = cldev->cl;
519
520 bus = cldev->bus;
Tomas Winkler62382992015-07-23 15:08:35 +0300521
Tomas Winkler62382992015-07-23 15:08:35 +0300522 mutex_lock(&bus->device_lock);
523
524 if (!mei_cl_is_connected(cl)) {
Alexander Usyskind8820392016-10-30 01:42:19 +0200525 dev_dbg(bus->dev, "Already disconnected");
Tomas Winkler62382992015-07-23 15:08:35 +0300526 err = 0;
527 goto out;
528 }
529
530 err = mei_cl_disconnect(cl);
Tomas Winkler60095952015-07-23 15:08:47 +0300531 if (err < 0)
Tomas Winkler62382992015-07-23 15:08:35 +0300532 dev_err(bus->dev, "Could not disconnect from the ME client");
Tomas Winkler62382992015-07-23 15:08:35 +0300533
534out:
Tomas Winkler60095952015-07-23 15:08:47 +0300535 /* Flush queues and remove any pending read */
536 mei_cl_flush_queues(cl, NULL);
537 mei_cl_unlink(cl);
538
Tomas Winkler62382992015-07-23 15:08:35 +0300539 mutex_unlock(&bus->device_lock);
540 return err;
Tomas Winkler62382992015-07-23 15:08:35 +0300541}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300542EXPORT_SYMBOL_GPL(mei_cldev_disable);
Tomas Winkler62382992015-07-23 15:08:35 +0300543
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300544/**
545 * mei_cl_device_find - find matching entry in the driver id table
546 *
547 * @cldev: me client device
548 * @cldrv: me client driver
549 *
550 * Return: id on success; NULL if no id is matching
551 */
552static const
553struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
554 struct mei_cl_driver *cldrv)
Samuel Ortize5354102013-03-27 17:29:53 +0200555{
Samuel Ortize5354102013-03-27 17:29:53 +0200556 const struct mei_cl_device_id *id;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300557 const uuid_le *uuid;
Tomas Winklerb26864c2015-09-10 10:18:01 +0300558 u8 version;
559 bool match;
Samuel Ortize5354102013-03-27 17:29:53 +0200560
Tomas Winklerb37719c32015-07-23 15:08:33 +0300561 uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winklerb26864c2015-09-10 10:18:01 +0300562 version = mei_me_cl_ver(cldev->me_cl);
Samuel Ortize5354102013-03-27 17:29:53 +0200563
Tomas Winklerb37719c32015-07-23 15:08:33 +0300564 id = cldrv->id_table;
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700565 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700566 if (!uuid_le_cmp(*uuid, id->uuid)) {
Tomas Winklerb26864c2015-09-10 10:18:01 +0300567 match = true;
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300568
Tomas Winklerb26864c2015-09-10 10:18:01 +0300569 if (cldev->name[0])
570 if (strncmp(cldev->name, id->name,
571 sizeof(id->name)))
572 match = false;
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300573
Tomas Winklerb26864c2015-09-10 10:18:01 +0300574 if (id->version != MEI_CL_VERSION_ANY)
575 if (id->version != version)
576 match = false;
577 if (match)
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300578 return id;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300579 }
Samuel Ortize5354102013-03-27 17:29:53 +0200580
581 id++;
582 }
583
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300584 return NULL;
585}
586
587/**
588 * mei_cl_device_match - device match function
589 *
590 * @dev: device
591 * @drv: driver
592 *
593 * Return: 1 if matching device was found 0 otherwise
594 */
595static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
596{
597 struct mei_cl_device *cldev = to_mei_cl_device(dev);
598 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
599 const struct mei_cl_device_id *found_id;
600
601 if (!cldev)
602 return 0;
603
Tomas Winkler71ce7892015-07-23 15:08:43 +0300604 if (!cldev->do_match)
605 return 0;
606
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300607 if (!cldrv || !cldrv->id_table)
608 return 0;
609
610 found_id = mei_cl_device_find(cldev, cldrv);
611 if (found_id)
612 return 1;
613
Samuel Ortize5354102013-03-27 17:29:53 +0200614 return 0;
615}
616
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300617/**
618 * mei_cl_device_probe - bus probe function
619 *
620 * @dev: device
621 *
622 * Return: 0 on success; < 0 otherwise
623 */
Samuel Ortize5354102013-03-27 17:29:53 +0200624static int mei_cl_device_probe(struct device *dev)
625{
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300626 struct mei_cl_device *cldev;
Tomas Winklerb37719c32015-07-23 15:08:33 +0300627 struct mei_cl_driver *cldrv;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300628 const struct mei_cl_device_id *id;
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300629 int ret;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300630
631 cldev = to_mei_cl_device(dev);
632 cldrv = to_mei_cl_driver(dev->driver);
Samuel Ortize5354102013-03-27 17:29:53 +0200633
Tomas Winklerb37719c32015-07-23 15:08:33 +0300634 if (!cldev)
Samuel Ortize5354102013-03-27 17:29:53 +0200635 return 0;
636
Tomas Winklerb37719c32015-07-23 15:08:33 +0300637 if (!cldrv || !cldrv->probe)
Samuel Ortize5354102013-03-27 17:29:53 +0200638 return -ENODEV;
639
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300640 id = mei_cl_device_find(cldev, cldrv);
641 if (!id)
642 return -ENODEV;
Samuel Ortize5354102013-03-27 17:29:53 +0200643
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300644 ret = cldrv->probe(cldev, id);
645 if (ret)
646 return ret;
Samuel Ortize5354102013-03-27 17:29:53 +0200647
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300648 __module_get(THIS_MODULE);
649 return 0;
Samuel Ortize5354102013-03-27 17:29:53 +0200650}
651
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300652/**
653 * mei_cl_device_remove - remove device from the bus
654 *
655 * @dev: device
656 *
657 * Return: 0 on success; < 0 otherwise
658 */
Samuel Ortize5354102013-03-27 17:29:53 +0200659static int mei_cl_device_remove(struct device *dev)
660{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300661 struct mei_cl_device *cldev = to_mei_cl_device(dev);
662 struct mei_cl_driver *cldrv;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300663 int ret = 0;
Samuel Ortize5354102013-03-27 17:29:53 +0200664
Tomas Winklerb37719c32015-07-23 15:08:33 +0300665 if (!cldev || !dev->driver)
Samuel Ortize5354102013-03-27 17:29:53 +0200666 return 0;
667
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200668 if (cldev->rx_cb) {
669 cancel_work_sync(&cldev->rx_work);
670 cldev->rx_cb = NULL;
671 }
672 if (cldev->notif_cb) {
673 cancel_work_sync(&cldev->notif_work);
674 cldev->notif_cb = NULL;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200675 }
676
Tomas Winklerb37719c32015-07-23 15:08:33 +0300677 cldrv = to_mei_cl_driver(dev->driver);
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300678 if (cldrv->remove)
679 ret = cldrv->remove(cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200680
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300681 module_put(THIS_MODULE);
682 dev->driver = NULL;
683 return ret;
Samuel Ortize5354102013-03-27 17:29:53 +0200684
Samuel Ortize5354102013-03-27 17:29:53 +0200685}
686
Tomas Winkler007d64e2015-05-07 15:54:03 +0300687static ssize_t name_show(struct device *dev, struct device_attribute *a,
688 char *buf)
689{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300690 struct mei_cl_device *cldev = to_mei_cl_device(dev);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300691
Rasmus Villemoes423de922016-03-24 10:38:04 +0200692 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300693}
694static DEVICE_ATTR_RO(name);
695
696static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
697 char *buf)
698{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300699 struct mei_cl_device *cldev = to_mei_cl_device(dev);
700 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300701
Rasmus Villemoes423de922016-03-24 10:38:04 +0200702 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300703}
704static DEVICE_ATTR_RO(uuid);
705
Tomas Winkler40b73202015-09-10 10:18:00 +0300706static ssize_t version_show(struct device *dev, struct device_attribute *a,
707 char *buf)
708{
709 struct mei_cl_device *cldev = to_mei_cl_device(dev);
710 u8 version = mei_me_cl_ver(cldev->me_cl);
Tomas Winkler40b73202015-09-10 10:18:00 +0300711
Rasmus Villemoes423de922016-03-24 10:38:04 +0200712 return scnprintf(buf, PAGE_SIZE, "%02X", version);
Tomas Winkler40b73202015-09-10 10:18:00 +0300713}
714static DEVICE_ATTR_RO(version);
715
Samuel Ortize5354102013-03-27 17:29:53 +0200716static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
717 char *buf)
718{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300719 struct mei_cl_device *cldev = to_mei_cl_device(dev);
720 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Samuel Ortize5354102013-03-27 17:29:53 +0200721
Rasmus Villemoes423de922016-03-24 10:38:04 +0200722 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
Samuel Ortize5354102013-03-27 17:29:53 +0200723}
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700724static DEVICE_ATTR_RO(modalias);
Samuel Ortize5354102013-03-27 17:29:53 +0200725
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300726static struct attribute *mei_cldev_attrs[] = {
Tomas Winkler007d64e2015-05-07 15:54:03 +0300727 &dev_attr_name.attr,
728 &dev_attr_uuid.attr,
Tomas Winkler40b73202015-09-10 10:18:00 +0300729 &dev_attr_version.attr,
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700730 &dev_attr_modalias.attr,
731 NULL,
Samuel Ortize5354102013-03-27 17:29:53 +0200732};
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300733ATTRIBUTE_GROUPS(mei_cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200734
Tomas Winkler38d3c002015-07-23 15:08:36 +0300735/**
736 * mei_cl_device_uevent - me client bus uevent handler
737 *
738 * @dev: device
739 * @env: uevent kobject
740 *
741 * Return: 0 on success -ENOMEM on when add_uevent_var fails
742 */
743static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Samuel Ortize5354102013-03-27 17:29:53 +0200744{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300745 struct mei_cl_device *cldev = to_mei_cl_device(dev);
746 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winkler40b73202015-09-10 10:18:00 +0300747 u8 version = mei_me_cl_ver(cldev->me_cl);
748
749 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
750 return -ENOMEM;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300751
Tomas Winkler007d64e2015-05-07 15:54:03 +0300752 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
753 return -ENOMEM;
754
Tomas Winklerb37719c32015-07-23 15:08:33 +0300755 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
Tomas Winkler007d64e2015-05-07 15:54:03 +0300756 return -ENOMEM;
757
Tomas Winklerb26864c2015-09-10 10:18:01 +0300758 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
759 cldev->name, uuid, version))
Samuel Ortize5354102013-03-27 17:29:53 +0200760 return -ENOMEM;
761
762 return 0;
763}
764
765static struct bus_type mei_cl_bus_type = {
766 .name = "mei",
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300767 .dev_groups = mei_cldev_groups,
Samuel Ortize5354102013-03-27 17:29:53 +0200768 .match = mei_cl_device_match,
769 .probe = mei_cl_device_probe,
770 .remove = mei_cl_device_remove,
Tomas Winkler38d3c002015-07-23 15:08:36 +0300771 .uevent = mei_cl_device_uevent,
Samuel Ortize5354102013-03-27 17:29:53 +0200772};
773
Tomas Winkler512f64d2015-07-23 15:08:41 +0300774static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
775{
776 if (bus)
777 get_device(bus->dev);
778
779 return bus;
780}
781
782static void mei_dev_bus_put(struct mei_device *bus)
783{
784 if (bus)
785 put_device(bus->dev);
786}
787
Tomas Winklerae48d742015-09-10 10:18:06 +0300788static void mei_cl_bus_dev_release(struct device *dev)
Samuel Ortize5354102013-03-27 17:29:53 +0200789{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300790 struct mei_cl_device *cldev = to_mei_cl_device(dev);
Alexander Usyskind49ed642015-05-04 09:43:54 +0300791
Tomas Winklerb37719c32015-07-23 15:08:33 +0300792 if (!cldev)
Alexander Usyskind49ed642015-05-04 09:43:54 +0300793 return;
794
Tomas Winklerb37719c32015-07-23 15:08:33 +0300795 mei_me_cl_put(cldev->me_cl);
Tomas Winkler512f64d2015-07-23 15:08:41 +0300796 mei_dev_bus_put(cldev->bus);
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200797 kfree(cldev->cl);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300798 kfree(cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200799}
800
801static struct device_type mei_cl_device_type = {
Tomas Winklerae48d742015-09-10 10:18:06 +0300802 .release = mei_cl_bus_dev_release,
Samuel Ortize5354102013-03-27 17:29:53 +0200803};
804
Tomas Winkler71ce7892015-07-23 15:08:43 +0300805/**
Tomas Winkler213dd192015-10-28 14:34:35 +0200806 * mei_cl_bus_set_name - set device name for me client device
807 *
808 * @cldev: me client device
809 */
810static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
811{
812 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
813 cldev->name,
814 mei_me_cl_uuid(cldev->me_cl),
815 mei_me_cl_ver(cldev->me_cl));
816}
817
818/**
Tomas Winklerae48d742015-09-10 10:18:06 +0300819 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
Tomas Winkler71ce7892015-07-23 15:08:43 +0300820 *
821 * @bus: mei device
822 * @me_cl: me client
823 *
824 * Return: allocated device structur or NULL on allocation failure
825 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300826static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
827 struct mei_me_client *me_cl)
Tomas Winkler71ce7892015-07-23 15:08:43 +0300828{
829 struct mei_cl_device *cldev;
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200830 struct mei_cl *cl;
Tomas Winkler71ce7892015-07-23 15:08:43 +0300831
832 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
833 if (!cldev)
834 return NULL;
835
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200836 cl = mei_cl_allocate(bus);
837 if (!cl) {
838 kfree(cldev);
839 return NULL;
840 }
841
Tomas Winkler71ce7892015-07-23 15:08:43 +0300842 device_initialize(&cldev->dev);
843 cldev->dev.parent = bus->dev;
844 cldev->dev.bus = &mei_cl_bus_type;
845 cldev->dev.type = &mei_cl_device_type;
846 cldev->bus = mei_dev_bus_get(bus);
847 cldev->me_cl = mei_me_cl_get(me_cl);
Alexander Usyskinc110cdb2016-11-16 22:51:28 +0200848 cldev->cl = cl;
Tomas Winkler213dd192015-10-28 14:34:35 +0200849 mei_cl_bus_set_name(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300850 cldev->is_added = 0;
851 INIT_LIST_HEAD(&cldev->bus_list);
852
853 return cldev;
854}
855
856/**
857 * mei_cl_dev_setup - setup me client device
858 * run fix up routines and set the device name
859 *
860 * @bus: mei device
861 * @cldev: me client device
862 *
863 * Return: true if the device is eligible for enumeration
864 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300865static bool mei_cl_bus_dev_setup(struct mei_device *bus,
866 struct mei_cl_device *cldev)
Tomas Winkler71ce7892015-07-23 15:08:43 +0300867{
868 cldev->do_match = 1;
Tomas Winklerae48d742015-09-10 10:18:06 +0300869 mei_cl_bus_dev_fixup(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300870
Tomas Winkler213dd192015-10-28 14:34:35 +0200871 /* the device name can change during fix up */
Tomas Winkler71ce7892015-07-23 15:08:43 +0300872 if (cldev->do_match)
Tomas Winkler213dd192015-10-28 14:34:35 +0200873 mei_cl_bus_set_name(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300874
875 return cldev->do_match == 1;
876}
877
878/**
879 * mei_cl_bus_dev_add - add me client devices
880 *
881 * @cldev: me client device
882 *
883 * Return: 0 on success; < 0 on failre
884 */
885static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
886{
887 int ret;
888
Tomas Winklerb26864c2015-09-10 10:18:01 +0300889 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
890 mei_me_cl_uuid(cldev->me_cl),
891 mei_me_cl_ver(cldev->me_cl));
Tomas Winkler71ce7892015-07-23 15:08:43 +0300892 ret = device_add(&cldev->dev);
893 if (!ret)
894 cldev->is_added = 1;
895
896 return ret;
897}
898
Tomas Winkler60095952015-07-23 15:08:47 +0300899/**
900 * mei_cl_bus_dev_stop - stop the driver
901 *
902 * @cldev: me client device
903 */
904static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
905{
906 if (cldev->is_added)
907 device_release_driver(&cldev->dev);
908}
909
910/**
911 * mei_cl_bus_dev_destroy - destroy me client devices object
912 *
913 * @cldev: me client device
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200914 *
915 * Locking: called under "dev->cl_bus_lock" lock
Tomas Winkler60095952015-07-23 15:08:47 +0300916 */
917static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
918{
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200919
920 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
921
Tomas Winkler60095952015-07-23 15:08:47 +0300922 if (!cldev->is_added)
923 return;
924
925 device_del(&cldev->dev);
926
Tomas Winkler60095952015-07-23 15:08:47 +0300927 list_del_init(&cldev->bus_list);
Tomas Winkler60095952015-07-23 15:08:47 +0300928
929 cldev->is_added = 0;
930 put_device(&cldev->dev);
931}
932
933/**
934 * mei_cl_bus_remove_device - remove a devices form the bus
935 *
936 * @cldev: me client device
937 */
938static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
939{
940 mei_cl_bus_dev_stop(cldev);
941 mei_cl_bus_dev_destroy(cldev);
942}
943
944/**
945 * mei_cl_bus_remove_devices - remove all devices form the bus
946 *
947 * @bus: mei device
948 */
949void mei_cl_bus_remove_devices(struct mei_device *bus)
950{
951 struct mei_cl_device *cldev, *next;
952
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200953 mutex_lock(&bus->cl_bus_lock);
Tomas Winkler60095952015-07-23 15:08:47 +0300954 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
955 mei_cl_bus_remove_device(cldev);
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200956 mutex_unlock(&bus->cl_bus_lock);
Tomas Winkler60095952015-07-23 15:08:47 +0300957}
958
959
960/**
Tomas Winklerae48d742015-09-10 10:18:06 +0300961 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
Tomas Winkler60095952015-07-23 15:08:47 +0300962 * based on me client
963 *
964 * @bus: mei device
965 * @me_cl: me client
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200966 *
967 * Locking: called under "dev->cl_bus_lock" lock
Tomas Winkler60095952015-07-23 15:08:47 +0300968 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300969static void mei_cl_bus_dev_init(struct mei_device *bus,
970 struct mei_me_client *me_cl)
Samuel Ortize5354102013-03-27 17:29:53 +0200971{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300972 struct mei_cl_device *cldev;
Tomas Winkler60095952015-07-23 15:08:47 +0300973
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200974 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
975
Tomas Winkler60095952015-07-23 15:08:47 +0300976 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
977
978 if (me_cl->bus_added)
979 return;
Samuel Ortize5354102013-03-27 17:29:53 +0200980
Tomas Winklerae48d742015-09-10 10:18:06 +0300981 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300982 if (!cldev)
Tomas Winkler60095952015-07-23 15:08:47 +0300983 return;
Samuel Ortize5354102013-03-27 17:29:53 +0200984
Tomas Winkler60095952015-07-23 15:08:47 +0300985 me_cl->bus_added = true;
986 list_add_tail(&cldev->bus_list, &bus->device_list);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300987
Samuel Ortize5354102013-03-27 17:29:53 +0200988}
Samuel Ortize5354102013-03-27 17:29:53 +0200989
Tomas Winkler60095952015-07-23 15:08:47 +0300990/**
991 * mei_cl_bus_rescan - scan me clients list and add create
992 * devices for eligible clients
993 *
994 * @bus: mei device
995 */
996void mei_cl_bus_rescan(struct mei_device *bus)
Samuel Ortize5354102013-03-27 17:29:53 +0200997{
Tomas Winkler60095952015-07-23 15:08:47 +0300998 struct mei_cl_device *cldev, *n;
999 struct mei_me_client *me_cl;
1000
Tomas Winkler2da55cf2015-10-28 14:34:34 +02001001 mutex_lock(&bus->cl_bus_lock);
1002
Tomas Winkler60095952015-07-23 15:08:47 +03001003 down_read(&bus->me_clients_rwsem);
1004 list_for_each_entry(me_cl, &bus->me_clients, list)
Tomas Winklerae48d742015-09-10 10:18:06 +03001005 mei_cl_bus_dev_init(bus, me_cl);
Tomas Winkler60095952015-07-23 15:08:47 +03001006 up_read(&bus->me_clients_rwsem);
1007
Tomas Winkler60095952015-07-23 15:08:47 +03001008 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1009
1010 if (!mei_me_cl_is_active(cldev->me_cl)) {
1011 mei_cl_bus_remove_device(cldev);
1012 continue;
1013 }
1014
1015 if (cldev->is_added)
1016 continue;
1017
Tomas Winklerae48d742015-09-10 10:18:06 +03001018 if (mei_cl_bus_dev_setup(bus, cldev))
Tomas Winkler60095952015-07-23 15:08:47 +03001019 mei_cl_bus_dev_add(cldev);
1020 else {
1021 list_del_init(&cldev->bus_list);
1022 put_device(&cldev->dev);
1023 }
1024 }
1025 mutex_unlock(&bus->cl_bus_lock);
1026
1027 dev_dbg(bus->dev, "rescan end");
Samuel Ortize5354102013-03-27 17:29:53 +02001028}
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001029
Alexander Usyskina816a002016-02-07 23:35:41 +02001030void mei_cl_bus_rescan_work(struct work_struct *work)
1031{
1032 struct mei_device *bus =
1033 container_of(work, struct mei_device, bus_rescan_work);
Alexander Usyskin025fb792016-02-07 23:35:43 +02001034 struct mei_me_client *me_cl;
1035
Alexander Usyskin025fb792016-02-07 23:35:43 +02001036 me_cl = mei_me_cl_by_uuid(bus, &mei_amthif_guid);
1037 if (me_cl)
1038 mei_amthif_host_init(bus, me_cl);
1039 mei_me_cl_put(me_cl);
Alexander Usyskina816a002016-02-07 23:35:41 +02001040
1041 mei_cl_bus_rescan(bus);
1042}
1043
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001044int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1045 struct module *owner)
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001046{
1047 int err;
1048
Tomas Winklerb37719c32015-07-23 15:08:33 +03001049 cldrv->driver.name = cldrv->name;
1050 cldrv->driver.owner = owner;
1051 cldrv->driver.bus = &mei_cl_bus_type;
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001052
Tomas Winklerb37719c32015-07-23 15:08:33 +03001053 err = driver_register(&cldrv->driver);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001054 if (err)
1055 return err;
1056
Tomas Winklerb37719c32015-07-23 15:08:33 +03001057 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001058
1059 return 0;
1060}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001061EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001062
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001063void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001064{
Tomas Winklerb37719c32015-07-23 15:08:33 +03001065 driver_unregister(&cldrv->driver);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001066
Tomas Winklerb37719c32015-07-23 15:08:33 +03001067 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001068}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001069EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
Samuel Ortiz3e833292013-03-27 17:29:55 +02001070
Tomas Winkler60095952015-07-23 15:08:47 +03001071
Samuel Ortizcf3baef2013-03-27 17:29:57 +02001072int __init mei_cl_bus_init(void)
1073{
1074 return bus_register(&mei_cl_bus_type);
1075}
1076
1077void __exit mei_cl_bus_exit(void)
1078{
1079 bus_unregister(&mei_cl_bus_type);
1080}