blob: 1f33fea9299f61a4b8ceeb127a638ccbf09c312e [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
39 * @blocking: wait for write completion
40 *
41 * Return: written size bytes or < 0 on error
42 */
43ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
44 bool blocking)
45{
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
83 memcpy(cb->buf.data, buf, length);
84
85 rets = mei_cl_write(cl, cb, blocking);
86
87out:
88 mutex_unlock(&bus->device_lock);
Tomas Winkler62382992015-07-23 15:08:35 +030089
90 return rets;
91}
92
93/**
94 * __mei_cl_recv - internal client receive (read)
95 *
96 * @cl: host client
Tomas Winklerdf7f5442015-10-13 15:02:40 +030097 * @buf: buffer to receive
Tomas Winkler62382992015-07-23 15:08:35 +030098 * @length: buffer length
99 *
100 * Return: read size in bytes of < 0 on error
101 */
102ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
103{
104 struct mei_device *bus;
105 struct mei_cl_cb *cb;
106 size_t r_length;
107 ssize_t rets;
108
109 if (WARN_ON(!cl || !cl->dev))
110 return -ENODEV;
111
112 bus = cl->dev;
113
114 mutex_lock(&bus->device_lock);
Alexander Usyskin15c13df2016-02-07 23:35:32 +0200115 if (bus->dev_state != MEI_DEV_ENABLED) {
116 rets = -ENODEV;
117 goto out;
118 }
Tomas Winkler62382992015-07-23 15:08:35 +0300119
120 cb = mei_cl_read_cb(cl, NULL);
121 if (cb)
122 goto copy;
123
124 rets = mei_cl_read_start(cl, length, NULL);
125 if (rets && rets != -EBUSY)
126 goto out;
127
128 /* wait on event only if there is no other waiter */
129 if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
130
131 mutex_unlock(&bus->device_lock);
132
133 if (wait_event_interruptible(cl->rx_wait,
134 (!list_empty(&cl->rd_completed)) ||
135 (!mei_cl_is_connected(cl)))) {
136
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 }
148 }
149
150 cb = mei_cl_read_cb(cl, NULL);
151 if (!cb) {
152 rets = 0;
153 goto out;
154 }
155
156copy:
157 if (cb->status) {
158 rets = cb->status;
159 goto free;
160 }
161
162 r_length = min_t(size_t, length, cb->buf_idx);
163 memcpy(buf, cb->buf.data, r_length);
164 rets = r_length;
165
166free:
167 mei_io_cb_free(cb);
168out:
169 mutex_unlock(&bus->device_lock);
170
171 return rets;
172}
173
174/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300175 * mei_cldev_send - me device send (write)
Tomas Winkler62382992015-07-23 15:08:35 +0300176 *
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 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300183ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
Tomas Winkler62382992015-07-23 15:08:35 +0300184{
185 struct mei_cl *cl = cldev->cl;
186
187 if (cl == NULL)
188 return -ENODEV;
189
190 return __mei_cl_send(cl, buf, length, 1);
191}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300192EXPORT_SYMBOL_GPL(mei_cldev_send);
Tomas Winkler62382992015-07-23 15:08:35 +0300193
194/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300195 * mei_cldev_recv - client receive (read)
Tomas Winkler62382992015-07-23 15:08:35 +0300196 *
197 * @cldev: me client device
Tomas Winklerdf7f5442015-10-13 15:02:40 +0300198 * @buf: buffer to receive
Tomas Winkler62382992015-07-23 15:08:35 +0300199 * @length: buffer length
200 *
201 * Return: read size in bytes of < 0 on error
202 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300203ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
Tomas Winkler62382992015-07-23 15:08:35 +0300204{
205 struct mei_cl *cl = cldev->cl;
206
207 if (cl == NULL)
208 return -ENODEV;
209
210 return __mei_cl_recv(cl, buf, length);
211}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300212EXPORT_SYMBOL_GPL(mei_cldev_recv);
Tomas Winkler62382992015-07-23 15:08:35 +0300213
214/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300215 * mei_cl_bus_event_work - dispatch rx event for a bus device
Tomas Winkler62382992015-07-23 15:08:35 +0300216 * and schedule new work
217 *
218 * @work: work
219 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300220static void mei_cl_bus_event_work(struct work_struct *work)
Tomas Winkler62382992015-07-23 15:08:35 +0300221{
222 struct mei_cl_device *cldev;
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400223 struct mei_device *bus;
Tomas Winkler62382992015-07-23 15:08:35 +0300224
225 cldev = container_of(work, struct mei_cl_device, event_work);
226
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400227 bus = cldev->bus;
228
Tomas Winkler62382992015-07-23 15:08:35 +0300229 if (cldev->event_cb)
230 cldev->event_cb(cldev, cldev->events, cldev->event_context);
231
232 cldev->events = 0;
233
234 /* Prepare for the next read */
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400235 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
236 mutex_lock(&bus->device_lock);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300237 mei_cl_read_start(cldev->cl, 0, NULL);
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400238 mutex_unlock(&bus->device_lock);
239 }
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300240}
241
242/**
243 * mei_cl_bus_notify_event - schedule notify cb on bus client
244 *
245 * @cl: host client
Tomas Winkler850f8942016-02-07 23:35:31 +0200246 *
247 * Return: true if event was scheduled
248 * false if the client is not waiting for event
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300249 */
Tomas Winkler850f8942016-02-07 23:35:31 +0200250bool mei_cl_bus_notify_event(struct mei_cl *cl)
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300251{
252 struct mei_cl_device *cldev = cl->cldev;
253
254 if (!cldev || !cldev->event_cb)
Tomas Winkler850f8942016-02-07 23:35:31 +0200255 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300256
257 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)))
Tomas Winkler850f8942016-02-07 23:35:31 +0200258 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300259
260 if (!cl->notify_ev)
Tomas Winkler850f8942016-02-07 23:35:31 +0200261 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300262
263 set_bit(MEI_CL_EVENT_NOTIF, &cldev->events);
264
265 schedule_work(&cldev->event_work);
266
267 cl->notify_ev = false;
Tomas Winkler850f8942016-02-07 23:35:31 +0200268
269 return true;
Tomas Winkler62382992015-07-23 15:08:35 +0300270}
271
272/**
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200273 * mei_cl_bus_rx_event - schedule rx event
Tomas Winkler62382992015-07-23 15:08:35 +0300274 *
275 * @cl: host client
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200276 *
277 * Return: true if event was scheduled
278 * false if the client is not waiting for event
Tomas Winkler62382992015-07-23 15:08:35 +0300279 */
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200280bool mei_cl_bus_rx_event(struct mei_cl *cl)
Tomas Winkler62382992015-07-23 15:08:35 +0300281{
282 struct mei_cl_device *cldev = cl->cldev;
283
284 if (!cldev || !cldev->event_cb)
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200285 return false;
Tomas Winkler62382992015-07-23 15:08:35 +0300286
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300287 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX)))
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200288 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300289
Tomas Winkler62382992015-07-23 15:08:35 +0300290 set_bit(MEI_CL_EVENT_RX, &cldev->events);
291
292 schedule_work(&cldev->event_work);
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200293
294 return true;
Tomas Winkler62382992015-07-23 15:08:35 +0300295}
296
297/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300298 * mei_cldev_register_event_cb - register event callback
Tomas Winkler62382992015-07-23 15:08:35 +0300299 *
300 * @cldev: me client devices
301 * @event_cb: callback function
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300302 * @events_mask: requested events bitmask
Tomas Winkler62382992015-07-23 15:08:35 +0300303 * @context: driver context data
304 *
305 * Return: 0 on success
306 * -EALREADY if an callback is already registered
307 * <0 on other errors
308 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300309int 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)
Tomas Winkler62382992015-07-23 15:08:35 +0300312{
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400313 struct mei_device *bus = cldev->bus;
Tomas Winkler48168f42015-07-23 15:08:38 +0300314 int ret;
315
Tomas Winkler62382992015-07-23 15:08:35 +0300316 if (cldev->event_cb)
317 return -EALREADY;
318
319 cldev->events = 0;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300320 cldev->events_mask = events_mask;
Tomas Winkler62382992015-07-23 15:08:35 +0300321 cldev->event_cb = event_cb;
322 cldev->event_context = context;
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300323 INIT_WORK(&cldev->event_work, mei_cl_bus_event_work);
Tomas Winkler62382992015-07-23 15:08:35 +0300324
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300325 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400326 mutex_lock(&bus->device_lock);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300327 ret = mei_cl_read_start(cldev->cl, 0, NULL);
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400328 mutex_unlock(&bus->device_lock);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300329 if (ret && ret != -EBUSY)
330 return ret;
331 }
332
333 if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) {
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400334 mutex_lock(&bus->device_lock);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300335 ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0);
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400336 mutex_unlock(&bus->device_lock);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300337 if (ret)
338 return ret;
339 }
Tomas Winkler62382992015-07-23 15:08:35 +0300340
341 return 0;
342}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300343EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb);
Tomas Winkler62382992015-07-23 15:08:35 +0300344
345/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300346 * mei_cldev_get_drvdata - driver data getter
Tomas Winkler62382992015-07-23 15:08:35 +0300347 *
348 * @cldev: mei client device
349 *
350 * Return: driver private data
351 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300352void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300353{
354 return dev_get_drvdata(&cldev->dev);
355}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300356EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
Tomas Winkler62382992015-07-23 15:08:35 +0300357
358/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300359 * mei_cldev_set_drvdata - driver data setter
Tomas Winkler62382992015-07-23 15:08:35 +0300360 *
361 * @cldev: mei client device
362 * @data: data to store
363 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300364void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
Tomas Winkler62382992015-07-23 15:08:35 +0300365{
366 dev_set_drvdata(&cldev->dev, data);
367}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300368EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
Tomas Winkler62382992015-07-23 15:08:35 +0300369
370/**
Tomas Winklerbaeacd02015-09-10 10:18:02 +0300371 * mei_cldev_uuid - return uuid of the underlying me client
372 *
373 * @cldev: mei client device
374 *
375 * Return: me client uuid
376 */
377const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
378{
379 return mei_me_cl_uuid(cldev->me_cl);
380}
381EXPORT_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 */
390u8 mei_cldev_ver(const struct mei_cl_device *cldev)
391{
392 return mei_me_cl_ver(cldev->me_cl);
393}
394EXPORT_SYMBOL_GPL(mei_cldev_ver);
395
396/**
Tomas Winkler01a14ed2015-09-10 10:18:03 +0300397 * 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 */
403bool mei_cldev_enabled(struct mei_cl_device *cldev)
404{
405 return cldev->cl && mei_cl_is_connected(cldev->cl);
406}
407EXPORT_SYMBOL_GPL(mei_cldev_enabled);
408
409/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300410 * mei_cldev_enable_device - enable me client device
Tomas Winkler62382992015-07-23 15:08:35 +0300411 * create connection with me client
412 *
413 * @cldev: me client device
414 *
415 * Return: 0 on success and < 0 on error
416 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300417int mei_cldev_enable(struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300418{
Tomas Winkler60095952015-07-23 15:08:47 +0300419 struct mei_device *bus = cldev->bus;
420 struct mei_cl *cl;
421 int ret;
Tomas Winkler62382992015-07-23 15:08:35 +0300422
Tomas Winkler60095952015-07-23 15:08:47 +0300423 cl = cldev->cl;
Tomas Winkler62382992015-07-23 15:08:35 +0300424
Tomas Winkler60095952015-07-23 15:08:47 +0300425 if (!cl) {
426 mutex_lock(&bus->device_lock);
Alexander Usyskin7851e002016-02-07 23:35:40 +0200427 cl = mei_cl_alloc_linked(bus);
Tomas Winkler60095952015-07-23 15:08:47 +0300428 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 }
Tomas Winkler62382992015-07-23 15:08:35 +0300435
436 mutex_lock(&bus->device_lock);
Tomas Winkler62382992015-07-23 15:08:35 +0300437 if (mei_cl_is_connected(cl)) {
Tomas Winkler60095952015-07-23 15:08:47 +0300438 ret = 0;
439 goto out;
Tomas Winkler62382992015-07-23 15:08:35 +0300440 }
441
Tomas Winkler60095952015-07-23 15:08:47 +0300442 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;
Tomas Winkler62382992015-07-23 15:08:35 +0300446 }
447
Tomas Winkler60095952015-07-23 15:08:47 +0300448 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
449 if (ret < 0)
450 dev_err(&cldev->dev, "cannot connect\n");
451
452out:
Tomas Winkler62382992015-07-23 15:08:35 +0300453 mutex_unlock(&bus->device_lock);
454
Tomas Winkler60095952015-07-23 15:08:47 +0300455 return ret;
Tomas Winkler62382992015-07-23 15:08:35 +0300456}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300457EXPORT_SYMBOL_GPL(mei_cldev_enable);
Tomas Winkler62382992015-07-23 15:08:35 +0300458
459/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300460 * mei_cldev_disable - disable me client device
Tomas Winkler62382992015-07-23 15:08:35 +0300461 * disconnect form the me client
462 *
463 * @cldev: me client device
464 *
465 * Return: 0 on success and < 0 on error
466 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300467int mei_cldev_disable(struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300468{
Tomas Winkler62382992015-07-23 15:08:35 +0300469 struct mei_device *bus;
Tomas Winkler60095952015-07-23 15:08:47 +0300470 struct mei_cl *cl;
471 int err;
Tomas Winkler62382992015-07-23 15:08:35 +0300472
Tomas Winkler60095952015-07-23 15:08:47 +0300473 if (!cldev || !cldev->cl)
Tomas Winkler62382992015-07-23 15:08:35 +0300474 return -ENODEV;
475
Tomas Winkler60095952015-07-23 15:08:47 +0300476 cl = cldev->cl;
477
478 bus = cldev->bus;
Tomas Winkler62382992015-07-23 15:08:35 +0300479
480 cldev->event_cb = NULL;
481
482 mutex_lock(&bus->device_lock);
483
484 if (!mei_cl_is_connected(cl)) {
485 dev_err(bus->dev, "Already disconnected");
486 err = 0;
487 goto out;
488 }
489
490 err = mei_cl_disconnect(cl);
Tomas Winkler60095952015-07-23 15:08:47 +0300491 if (err < 0)
Tomas Winkler62382992015-07-23 15:08:35 +0300492 dev_err(bus->dev, "Could not disconnect from the ME client");
Tomas Winkler62382992015-07-23 15:08:35 +0300493
494out:
Tomas Winkler60095952015-07-23 15:08:47 +0300495 /* Flush queues and remove any pending read */
496 mei_cl_flush_queues(cl, NULL);
497 mei_cl_unlink(cl);
498
499 kfree(cl);
500 cldev->cl = NULL;
501
Tomas Winkler62382992015-07-23 15:08:35 +0300502 mutex_unlock(&bus->device_lock);
503 return err;
Tomas Winkler62382992015-07-23 15:08:35 +0300504}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300505EXPORT_SYMBOL_GPL(mei_cldev_disable);
Tomas Winkler62382992015-07-23 15:08:35 +0300506
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300507/**
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 */
515static const
516struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
517 struct mei_cl_driver *cldrv)
Samuel Ortize5354102013-03-27 17:29:53 +0200518{
Samuel Ortize5354102013-03-27 17:29:53 +0200519 const struct mei_cl_device_id *id;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300520 const uuid_le *uuid;
Tomas Winklerb26864c2015-09-10 10:18:01 +0300521 u8 version;
522 bool match;
Samuel Ortize5354102013-03-27 17:29:53 +0200523
Tomas Winklerb37719c32015-07-23 15:08:33 +0300524 uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winklerb26864c2015-09-10 10:18:01 +0300525 version = mei_me_cl_ver(cldev->me_cl);
Samuel Ortize5354102013-03-27 17:29:53 +0200526
Tomas Winklerb37719c32015-07-23 15:08:33 +0300527 id = cldrv->id_table;
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700528 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700529 if (!uuid_le_cmp(*uuid, id->uuid)) {
Tomas Winklerb26864c2015-09-10 10:18:01 +0300530 match = true;
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300531
Tomas Winklerb26864c2015-09-10 10:18:01 +0300532 if (cldev->name[0])
533 if (strncmp(cldev->name, id->name,
534 sizeof(id->name)))
535 match = false;
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300536
Tomas Winklerb26864c2015-09-10 10:18:01 +0300537 if (id->version != MEI_CL_VERSION_ANY)
538 if (id->version != version)
539 match = false;
540 if (match)
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300541 return id;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300542 }
Samuel Ortize5354102013-03-27 17:29:53 +0200543
544 id++;
545 }
546
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300547 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 */
558static 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
Tomas Winkler71ce7892015-07-23 15:08:43 +0300567 if (!cldev->do_match)
568 return 0;
569
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300570 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
Samuel Ortize5354102013-03-27 17:29:53 +0200577 return 0;
578}
579
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300580/**
581 * mei_cl_device_probe - bus probe function
582 *
583 * @dev: device
584 *
585 * Return: 0 on success; < 0 otherwise
586 */
Samuel Ortize5354102013-03-27 17:29:53 +0200587static int mei_cl_device_probe(struct device *dev)
588{
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300589 struct mei_cl_device *cldev;
Tomas Winklerb37719c32015-07-23 15:08:33 +0300590 struct mei_cl_driver *cldrv;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300591 const struct mei_cl_device_id *id;
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300592 int ret;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300593
594 cldev = to_mei_cl_device(dev);
595 cldrv = to_mei_cl_driver(dev->driver);
Samuel Ortize5354102013-03-27 17:29:53 +0200596
Tomas Winklerb37719c32015-07-23 15:08:33 +0300597 if (!cldev)
Samuel Ortize5354102013-03-27 17:29:53 +0200598 return 0;
599
Tomas Winklerb37719c32015-07-23 15:08:33 +0300600 if (!cldrv || !cldrv->probe)
Samuel Ortize5354102013-03-27 17:29:53 +0200601 return -ENODEV;
602
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300603 id = mei_cl_device_find(cldev, cldrv);
604 if (!id)
605 return -ENODEV;
Samuel Ortize5354102013-03-27 17:29:53 +0200606
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300607 ret = cldrv->probe(cldev, id);
608 if (ret)
609 return ret;
Samuel Ortize5354102013-03-27 17:29:53 +0200610
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300611 __module_get(THIS_MODULE);
612 return 0;
Samuel Ortize5354102013-03-27 17:29:53 +0200613}
614
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300615/**
616 * mei_cl_device_remove - remove device from the bus
617 *
618 * @dev: device
619 *
620 * Return: 0 on success; < 0 otherwise
621 */
Samuel Ortize5354102013-03-27 17:29:53 +0200622static int mei_cl_device_remove(struct device *dev)
623{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300624 struct mei_cl_device *cldev = to_mei_cl_device(dev);
625 struct mei_cl_driver *cldrv;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300626 int ret = 0;
Samuel Ortize5354102013-03-27 17:29:53 +0200627
Tomas Winklerb37719c32015-07-23 15:08:33 +0300628 if (!cldev || !dev->driver)
Samuel Ortize5354102013-03-27 17:29:53 +0200629 return 0;
630
Tomas Winklerb37719c32015-07-23 15:08:33 +0300631 if (cldev->event_cb) {
632 cldev->event_cb = NULL;
633 cancel_work_sync(&cldev->event_work);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200634 }
635
Tomas Winklerb37719c32015-07-23 15:08:33 +0300636 cldrv = to_mei_cl_driver(dev->driver);
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300637 if (cldrv->remove)
638 ret = cldrv->remove(cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200639
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300640 module_put(THIS_MODULE);
641 dev->driver = NULL;
642 return ret;
Samuel Ortize5354102013-03-27 17:29:53 +0200643
Samuel Ortize5354102013-03-27 17:29:53 +0200644}
645
Tomas Winkler007d64e2015-05-07 15:54:03 +0300646static ssize_t name_show(struct device *dev, struct device_attribute *a,
647 char *buf)
648{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300649 struct mei_cl_device *cldev = to_mei_cl_device(dev);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300650
Rasmus Villemoes423de922016-03-24 10:38:04 +0200651 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300652}
653static DEVICE_ATTR_RO(name);
654
655static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
656 char *buf)
657{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300658 struct mei_cl_device *cldev = to_mei_cl_device(dev);
659 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300660
Rasmus Villemoes423de922016-03-24 10:38:04 +0200661 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300662}
663static DEVICE_ATTR_RO(uuid);
664
Tomas Winkler40b73202015-09-10 10:18:00 +0300665static 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);
Tomas Winkler40b73202015-09-10 10:18:00 +0300670
Rasmus Villemoes423de922016-03-24 10:38:04 +0200671 return scnprintf(buf, PAGE_SIZE, "%02X", version);
Tomas Winkler40b73202015-09-10 10:18:00 +0300672}
673static DEVICE_ATTR_RO(version);
674
Samuel Ortize5354102013-03-27 17:29:53 +0200675static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
676 char *buf)
677{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300678 struct mei_cl_device *cldev = to_mei_cl_device(dev);
679 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Samuel Ortize5354102013-03-27 17:29:53 +0200680
Rasmus Villemoes423de922016-03-24 10:38:04 +0200681 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
Samuel Ortize5354102013-03-27 17:29:53 +0200682}
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700683static DEVICE_ATTR_RO(modalias);
Samuel Ortize5354102013-03-27 17:29:53 +0200684
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300685static struct attribute *mei_cldev_attrs[] = {
Tomas Winkler007d64e2015-05-07 15:54:03 +0300686 &dev_attr_name.attr,
687 &dev_attr_uuid.attr,
Tomas Winkler40b73202015-09-10 10:18:00 +0300688 &dev_attr_version.attr,
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700689 &dev_attr_modalias.attr,
690 NULL,
Samuel Ortize5354102013-03-27 17:29:53 +0200691};
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300692ATTRIBUTE_GROUPS(mei_cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200693
Tomas Winkler38d3c002015-07-23 15:08:36 +0300694/**
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 */
702static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Samuel Ortize5354102013-03-27 17:29:53 +0200703{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300704 struct mei_cl_device *cldev = to_mei_cl_device(dev);
705 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winkler40b73202015-09-10 10:18:00 +0300706 u8 version = mei_me_cl_ver(cldev->me_cl);
707
708 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
709 return -ENOMEM;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300710
Tomas Winkler007d64e2015-05-07 15:54:03 +0300711 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
712 return -ENOMEM;
713
Tomas Winklerb37719c32015-07-23 15:08:33 +0300714 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
Tomas Winkler007d64e2015-05-07 15:54:03 +0300715 return -ENOMEM;
716
Tomas Winklerb26864c2015-09-10 10:18:01 +0300717 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
718 cldev->name, uuid, version))
Samuel Ortize5354102013-03-27 17:29:53 +0200719 return -ENOMEM;
720
721 return 0;
722}
723
724static struct bus_type mei_cl_bus_type = {
725 .name = "mei",
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300726 .dev_groups = mei_cldev_groups,
Samuel Ortize5354102013-03-27 17:29:53 +0200727 .match = mei_cl_device_match,
728 .probe = mei_cl_device_probe,
729 .remove = mei_cl_device_remove,
Tomas Winkler38d3c002015-07-23 15:08:36 +0300730 .uevent = mei_cl_device_uevent,
Samuel Ortize5354102013-03-27 17:29:53 +0200731};
732
Tomas Winkler512f64d2015-07-23 15:08:41 +0300733static 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
741static void mei_dev_bus_put(struct mei_device *bus)
742{
743 if (bus)
744 put_device(bus->dev);
745}
746
Tomas Winklerae48d742015-09-10 10:18:06 +0300747static void mei_cl_bus_dev_release(struct device *dev)
Samuel Ortize5354102013-03-27 17:29:53 +0200748{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300749 struct mei_cl_device *cldev = to_mei_cl_device(dev);
Alexander Usyskind49ed642015-05-04 09:43:54 +0300750
Tomas Winklerb37719c32015-07-23 15:08:33 +0300751 if (!cldev)
Alexander Usyskind49ed642015-05-04 09:43:54 +0300752 return;
753
Tomas Winklerb37719c32015-07-23 15:08:33 +0300754 mei_me_cl_put(cldev->me_cl);
Tomas Winkler512f64d2015-07-23 15:08:41 +0300755 mei_dev_bus_put(cldev->bus);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300756 kfree(cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200757}
758
759static struct device_type mei_cl_device_type = {
Tomas Winklerae48d742015-09-10 10:18:06 +0300760 .release = mei_cl_bus_dev_release,
Samuel Ortize5354102013-03-27 17:29:53 +0200761};
762
Tomas Winkler71ce7892015-07-23 15:08:43 +0300763/**
Tomas Winkler213dd192015-10-28 14:34:35 +0200764 * mei_cl_bus_set_name - set device name for me client device
765 *
766 * @cldev: me client device
767 */
768static 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
776/**
Tomas Winklerae48d742015-09-10 10:18:06 +0300777 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
Tomas Winkler71ce7892015-07-23 15:08:43 +0300778 *
779 * @bus: mei device
780 * @me_cl: me client
781 *
782 * Return: allocated device structur or NULL on allocation failure
783 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300784static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
785 struct mei_me_client *me_cl)
Tomas Winkler71ce7892015-07-23 15:08:43 +0300786{
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);
Tomas Winkler213dd192015-10-28 14:34:35 +0200799 mei_cl_bus_set_name(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300800 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 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300815static bool mei_cl_bus_dev_setup(struct mei_device *bus,
816 struct mei_cl_device *cldev)
Tomas Winkler71ce7892015-07-23 15:08:43 +0300817{
818 cldev->do_match = 1;
Tomas Winklerae48d742015-09-10 10:18:06 +0300819 mei_cl_bus_dev_fixup(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300820
Tomas Winkler213dd192015-10-28 14:34:35 +0200821 /* the device name can change during fix up */
Tomas Winkler71ce7892015-07-23 15:08:43 +0300822 if (cldev->do_match)
Tomas Winkler213dd192015-10-28 14:34:35 +0200823 mei_cl_bus_set_name(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300824
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 */
835static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
836{
837 int ret;
838
Tomas Winklerb26864c2015-09-10 10:18:01 +0300839 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));
Tomas Winkler71ce7892015-07-23 15:08:43 +0300842 ret = device_add(&cldev->dev);
843 if (!ret)
844 cldev->is_added = 1;
845
846 return ret;
847}
848
Tomas Winkler60095952015-07-23 15:08:47 +0300849/**
850 * mei_cl_bus_dev_stop - stop the driver
851 *
852 * @cldev: me client device
853 */
854static 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
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200864 *
865 * Locking: called under "dev->cl_bus_lock" lock
Tomas Winkler60095952015-07-23 15:08:47 +0300866 */
867static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
868{
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200869
870 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
871
Tomas Winkler60095952015-07-23 15:08:47 +0300872 if (!cldev->is_added)
873 return;
874
875 device_del(&cldev->dev);
876
Tomas Winkler60095952015-07-23 15:08:47 +0300877 list_del_init(&cldev->bus_list);
Tomas Winkler60095952015-07-23 15:08:47 +0300878
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 */
888static 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 */
899void mei_cl_bus_remove_devices(struct mei_device *bus)
900{
901 struct mei_cl_device *cldev, *next;
902
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200903 mutex_lock(&bus->cl_bus_lock);
Tomas Winkler60095952015-07-23 15:08:47 +0300904 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
905 mei_cl_bus_remove_device(cldev);
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200906 mutex_unlock(&bus->cl_bus_lock);
Tomas Winkler60095952015-07-23 15:08:47 +0300907}
908
909
910/**
Tomas Winklerae48d742015-09-10 10:18:06 +0300911 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
Tomas Winkler60095952015-07-23 15:08:47 +0300912 * based on me client
913 *
914 * @bus: mei device
915 * @me_cl: me client
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200916 *
917 * Locking: called under "dev->cl_bus_lock" lock
Tomas Winkler60095952015-07-23 15:08:47 +0300918 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300919static void mei_cl_bus_dev_init(struct mei_device *bus,
920 struct mei_me_client *me_cl)
Samuel Ortize5354102013-03-27 17:29:53 +0200921{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300922 struct mei_cl_device *cldev;
Tomas Winkler60095952015-07-23 15:08:47 +0300923
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200924 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
925
Tomas Winkler60095952015-07-23 15:08:47 +0300926 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
927
928 if (me_cl->bus_added)
929 return;
Samuel Ortize5354102013-03-27 17:29:53 +0200930
Tomas Winklerae48d742015-09-10 10:18:06 +0300931 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300932 if (!cldev)
Tomas Winkler60095952015-07-23 15:08:47 +0300933 return;
Samuel Ortize5354102013-03-27 17:29:53 +0200934
Tomas Winkler60095952015-07-23 15:08:47 +0300935 me_cl->bus_added = true;
936 list_add_tail(&cldev->bus_list, &bus->device_list);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300937
Samuel Ortize5354102013-03-27 17:29:53 +0200938}
Samuel Ortize5354102013-03-27 17:29:53 +0200939
Tomas Winkler60095952015-07-23 15:08:47 +0300940/**
941 * mei_cl_bus_rescan - scan me clients list and add create
942 * devices for eligible clients
943 *
944 * @bus: mei device
945 */
946void mei_cl_bus_rescan(struct mei_device *bus)
Samuel Ortize5354102013-03-27 17:29:53 +0200947{
Tomas Winkler60095952015-07-23 15:08:47 +0300948 struct mei_cl_device *cldev, *n;
949 struct mei_me_client *me_cl;
950
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200951 mutex_lock(&bus->cl_bus_lock);
952
Tomas Winkler60095952015-07-23 15:08:47 +0300953 down_read(&bus->me_clients_rwsem);
954 list_for_each_entry(me_cl, &bus->me_clients, list)
Tomas Winklerae48d742015-09-10 10:18:06 +0300955 mei_cl_bus_dev_init(bus, me_cl);
Tomas Winkler60095952015-07-23 15:08:47 +0300956 up_read(&bus->me_clients_rwsem);
957
Tomas Winkler60095952015-07-23 15:08:47 +0300958 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
959
960 if (!mei_me_cl_is_active(cldev->me_cl)) {
961 mei_cl_bus_remove_device(cldev);
962 continue;
963 }
964
965 if (cldev->is_added)
966 continue;
967
Tomas Winklerae48d742015-09-10 10:18:06 +0300968 if (mei_cl_bus_dev_setup(bus, cldev))
Tomas Winkler60095952015-07-23 15:08:47 +0300969 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");
Samuel Ortize5354102013-03-27 17:29:53 +0200978}
Samuel Ortiz333e4ee2013-03-27 17:29:54 +0200979
Alexander Usyskina816a002016-02-07 23:35:41 +0200980void 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);
Alexander Usyskin025fb792016-02-07 23:35:43 +0200984 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);
Alexander Usyskina816a002016-02-07 23:35:41 +0200992
993 mei_cl_bus_rescan(bus);
994}
995
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300996int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
997 struct module *owner)
Samuel Ortiz333e4ee2013-03-27 17:29:54 +0200998{
999 int err;
1000
Tomas Winklerb37719c32015-07-23 15:08:33 +03001001 cldrv->driver.name = cldrv->name;
1002 cldrv->driver.owner = owner;
1003 cldrv->driver.bus = &mei_cl_bus_type;
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001004
Tomas Winklerb37719c32015-07-23 15:08:33 +03001005 err = driver_register(&cldrv->driver);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001006 if (err)
1007 return err;
1008
Tomas Winklerb37719c32015-07-23 15:08:33 +03001009 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001010
1011 return 0;
1012}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001013EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001014
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001015void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001016{
Tomas Winklerb37719c32015-07-23 15:08:33 +03001017 driver_unregister(&cldrv->driver);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001018
Tomas Winklerb37719c32015-07-23 15:08:33 +03001019 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001020}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001021EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
Samuel Ortiz3e833292013-03-27 17:29:55 +02001022
Tomas Winkler60095952015-07-23 15:08:47 +03001023
Samuel Ortizcf3baef2013-03-27 17:29:57 +02001024int __init mei_cl_bus_init(void)
1025{
1026 return bus_register(&mei_cl_bus_type);
1027}
1028
1029void __exit mei_cl_bus_exit(void)
1030{
1031 bus_unregister(&mei_cl_bus_type);
1032}