blob: 7c075e9b25e5ee837364750bab05133827e4bfc2 [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
101 *
102 * Return: read size in bytes of < 0 on error
103 */
104ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
105{
106 struct mei_device *bus;
107 struct mei_cl_cb *cb;
108 size_t r_length;
109 ssize_t rets;
110
111 if (WARN_ON(!cl || !cl->dev))
112 return -ENODEV;
113
114 bus = cl->dev;
115
116 mutex_lock(&bus->device_lock);
Alexander Usyskin15c13df2016-02-07 23:35:32 +0200117 if (bus->dev_state != MEI_DEV_ENABLED) {
118 rets = -ENODEV;
119 goto out;
120 }
Tomas Winkler62382992015-07-23 15:08:35 +0300121
122 cb = mei_cl_read_cb(cl, NULL);
123 if (cb)
124 goto copy;
125
126 rets = mei_cl_read_start(cl, length, NULL);
127 if (rets && rets != -EBUSY)
128 goto out;
129
130 /* wait on event only if there is no other waiter */
Alexander Usyskin1eb5bd42016-06-16 17:58:54 +0300131 /* synchronized under device mutex */
132 if (!waitqueue_active(&cl->rx_wait)) {
Tomas Winkler62382992015-07-23 15:08:35 +0300133
134 mutex_unlock(&bus->device_lock);
135
136 if (wait_event_interruptible(cl->rx_wait,
137 (!list_empty(&cl->rd_completed)) ||
138 (!mei_cl_is_connected(cl)))) {
139
140 if (signal_pending(current))
141 return -EINTR;
142 return -ERESTARTSYS;
143 }
144
145 mutex_lock(&bus->device_lock);
146
147 if (!mei_cl_is_connected(cl)) {
Tomas Winkler2d4d5482016-06-23 00:25:31 +0300148 rets = -ENODEV;
Tomas Winkler62382992015-07-23 15:08:35 +0300149 goto out;
150 }
151 }
152
153 cb = mei_cl_read_cb(cl, NULL);
154 if (!cb) {
155 rets = 0;
156 goto out;
157 }
158
159copy:
160 if (cb->status) {
161 rets = cb->status;
162 goto free;
163 }
164
165 r_length = min_t(size_t, length, cb->buf_idx);
166 memcpy(buf, cb->buf.data, r_length);
167 rets = r_length;
168
169free:
170 mei_io_cb_free(cb);
171out:
172 mutex_unlock(&bus->device_lock);
173
174 return rets;
175}
176
177/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300178 * mei_cldev_send - me device send (write)
Tomas Winkler62382992015-07-23 15:08:35 +0300179 *
180 * @cldev: me client device
181 * @buf: buffer to send
182 * @length: buffer length
183 *
184 * Return: written size in bytes or < 0 on error
185 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300186ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
Tomas Winkler62382992015-07-23 15:08:35 +0300187{
188 struct mei_cl *cl = cldev->cl;
189
190 if (cl == NULL)
191 return -ENODEV;
192
Alexander Usyskine0cb6b22016-11-08 18:26:08 +0200193 return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING);
Tomas Winkler62382992015-07-23 15:08:35 +0300194}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300195EXPORT_SYMBOL_GPL(mei_cldev_send);
Tomas Winkler62382992015-07-23 15:08:35 +0300196
197/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300198 * mei_cldev_recv - client receive (read)
Tomas Winkler62382992015-07-23 15:08:35 +0300199 *
200 * @cldev: me client device
Tomas Winklerdf7f5442015-10-13 15:02:40 +0300201 * @buf: buffer to receive
Tomas Winkler62382992015-07-23 15:08:35 +0300202 * @length: buffer length
203 *
204 * Return: read size in bytes of < 0 on error
205 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300206ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
Tomas Winkler62382992015-07-23 15:08:35 +0300207{
208 struct mei_cl *cl = cldev->cl;
209
210 if (cl == NULL)
211 return -ENODEV;
212
213 return __mei_cl_recv(cl, buf, length);
214}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300215EXPORT_SYMBOL_GPL(mei_cldev_recv);
Tomas Winkler62382992015-07-23 15:08:35 +0300216
217/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300218 * mei_cl_bus_event_work - dispatch rx event for a bus device
Tomas Winkler62382992015-07-23 15:08:35 +0300219 * and schedule new work
220 *
221 * @work: work
222 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300223static void mei_cl_bus_event_work(struct work_struct *work)
Tomas Winkler62382992015-07-23 15:08:35 +0300224{
225 struct mei_cl_device *cldev;
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400226 struct mei_device *bus;
Tomas Winkler62382992015-07-23 15:08:35 +0300227
228 cldev = container_of(work, struct mei_cl_device, event_work);
229
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400230 bus = cldev->bus;
231
Tomas Winkler62382992015-07-23 15:08:35 +0300232 if (cldev->event_cb)
Tomas Winkler1e4edb32016-10-19 16:33:31 +0300233 cldev->event_cb(cldev, cldev->events);
Tomas Winkler62382992015-07-23 15:08:35 +0300234
235 cldev->events = 0;
236
237 /* Prepare for the next read */
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400238 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
239 mutex_lock(&bus->device_lock);
Tomas Winkler3030dc02016-07-26 01:06:05 +0300240 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400241 mutex_unlock(&bus->device_lock);
242 }
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300243}
244
245/**
246 * mei_cl_bus_notify_event - schedule notify cb on bus client
247 *
248 * @cl: host client
Tomas Winkler850f8942016-02-07 23:35:31 +0200249 *
250 * Return: true if event was scheduled
251 * false if the client is not waiting for event
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300252 */
Tomas Winkler850f8942016-02-07 23:35:31 +0200253bool mei_cl_bus_notify_event(struct mei_cl *cl)
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300254{
255 struct mei_cl_device *cldev = cl->cldev;
256
257 if (!cldev || !cldev->event_cb)
Tomas Winkler850f8942016-02-07 23:35:31 +0200258 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300259
260 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)))
Tomas Winkler850f8942016-02-07 23:35:31 +0200261 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300262
263 if (!cl->notify_ev)
Tomas Winkler850f8942016-02-07 23:35:31 +0200264 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300265
266 set_bit(MEI_CL_EVENT_NOTIF, &cldev->events);
267
268 schedule_work(&cldev->event_work);
269
270 cl->notify_ev = false;
Tomas Winkler850f8942016-02-07 23:35:31 +0200271
272 return true;
Tomas Winkler62382992015-07-23 15:08:35 +0300273}
274
275/**
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200276 * mei_cl_bus_rx_event - schedule rx event
Tomas Winkler62382992015-07-23 15:08:35 +0300277 *
278 * @cl: host client
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200279 *
280 * Return: true if event was scheduled
281 * false if the client is not waiting for event
Tomas Winkler62382992015-07-23 15:08:35 +0300282 */
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200283bool mei_cl_bus_rx_event(struct mei_cl *cl)
Tomas Winkler62382992015-07-23 15:08:35 +0300284{
285 struct mei_cl_device *cldev = cl->cldev;
286
287 if (!cldev || !cldev->event_cb)
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200288 return false;
Tomas Winkler62382992015-07-23 15:08:35 +0300289
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300290 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX)))
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200291 return false;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300292
Tomas Winkler62382992015-07-23 15:08:35 +0300293 set_bit(MEI_CL_EVENT_RX, &cldev->events);
294
295 schedule_work(&cldev->event_work);
Tomas Winklera1f9ae22016-02-07 23:35:30 +0200296
297 return true;
Tomas Winkler62382992015-07-23 15:08:35 +0300298}
299
300/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300301 * mei_cldev_register_event_cb - register event callback
Tomas Winkler62382992015-07-23 15:08:35 +0300302 *
303 * @cldev: me client devices
304 * @event_cb: callback function
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300305 * @events_mask: requested events bitmask
Tomas Winkler62382992015-07-23 15:08:35 +0300306 *
307 * Return: 0 on success
308 * -EALREADY if an callback is already registered
309 * <0 on other errors
310 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300311int mei_cldev_register_event_cb(struct mei_cl_device *cldev,
312 unsigned long events_mask,
Tomas Winkler1e4edb32016-10-19 16:33:31 +0300313 mei_cldev_event_cb_t event_cb)
Tomas Winkler62382992015-07-23 15:08:35 +0300314{
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400315 struct mei_device *bus = cldev->bus;
Tomas Winkler48168f42015-07-23 15:08:38 +0300316 int ret;
317
Tomas Winkler62382992015-07-23 15:08:35 +0300318 if (cldev->event_cb)
319 return -EALREADY;
320
321 cldev->events = 0;
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300322 cldev->events_mask = events_mask;
Tomas Winkler62382992015-07-23 15:08:35 +0300323 cldev->event_cb = event_cb;
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300324 INIT_WORK(&cldev->event_work, mei_cl_bus_event_work);
Tomas Winkler62382992015-07-23 15:08:35 +0300325
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300326 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400327 mutex_lock(&bus->device_lock);
Tomas Winkler3030dc02016-07-26 01:06:05 +0300328 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400329 mutex_unlock(&bus->device_lock);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300330 if (ret && ret != -EBUSY)
331 return ret;
332 }
333
334 if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) {
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400335 mutex_lock(&bus->device_lock);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300336 ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0);
Alexander Usyskinbc46b452016-05-03 18:54:21 -0400337 mutex_unlock(&bus->device_lock);
Alexander Usyskinbb2ef9c2015-07-26 09:54:23 +0300338 if (ret)
339 return ret;
340 }
Tomas Winkler62382992015-07-23 15:08:35 +0300341
342 return 0;
343}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300344EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb);
Tomas Winkler62382992015-07-23 15:08:35 +0300345
346/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300347 * mei_cldev_get_drvdata - driver data getter
Tomas Winkler62382992015-07-23 15:08:35 +0300348 *
349 * @cldev: mei client device
350 *
351 * Return: driver private data
352 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300353void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300354{
355 return dev_get_drvdata(&cldev->dev);
356}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300357EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
Tomas Winkler62382992015-07-23 15:08:35 +0300358
359/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300360 * mei_cldev_set_drvdata - driver data setter
Tomas Winkler62382992015-07-23 15:08:35 +0300361 *
362 * @cldev: mei client device
363 * @data: data to store
364 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300365void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
Tomas Winkler62382992015-07-23 15:08:35 +0300366{
367 dev_set_drvdata(&cldev->dev, data);
368}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300369EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
Tomas Winkler62382992015-07-23 15:08:35 +0300370
371/**
Tomas Winklerbaeacd02015-09-10 10:18:02 +0300372 * mei_cldev_uuid - return uuid of the underlying me client
373 *
374 * @cldev: mei client device
375 *
376 * Return: me client uuid
377 */
378const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
379{
380 return mei_me_cl_uuid(cldev->me_cl);
381}
382EXPORT_SYMBOL_GPL(mei_cldev_uuid);
383
384/**
385 * mei_cldev_ver - return protocol version of the underlying me client
386 *
387 * @cldev: mei client device
388 *
389 * Return: me client protocol version
390 */
391u8 mei_cldev_ver(const struct mei_cl_device *cldev)
392{
393 return mei_me_cl_ver(cldev->me_cl);
394}
395EXPORT_SYMBOL_GPL(mei_cldev_ver);
396
397/**
Tomas Winkler01a14ed2015-09-10 10:18:03 +0300398 * mei_cldev_enabled - check whether the device is enabled
399 *
400 * @cldev: mei client device
401 *
402 * Return: true if me client is initialized and connected
403 */
404bool mei_cldev_enabled(struct mei_cl_device *cldev)
405{
406 return cldev->cl && mei_cl_is_connected(cldev->cl);
407}
408EXPORT_SYMBOL_GPL(mei_cldev_enabled);
409
410/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300411 * mei_cldev_enable_device - enable me client device
Tomas Winkler62382992015-07-23 15:08:35 +0300412 * create connection with me client
413 *
414 * @cldev: me client device
415 *
416 * Return: 0 on success and < 0 on error
417 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300418int mei_cldev_enable(struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300419{
Tomas Winkler60095952015-07-23 15:08:47 +0300420 struct mei_device *bus = cldev->bus;
421 struct mei_cl *cl;
422 int ret;
Tomas Winkler62382992015-07-23 15:08:35 +0300423
Tomas Winkler60095952015-07-23 15:08:47 +0300424 cl = cldev->cl;
Tomas Winkler62382992015-07-23 15:08:35 +0300425
Tomas Winkler60095952015-07-23 15:08:47 +0300426 if (!cl) {
427 mutex_lock(&bus->device_lock);
Alexander Usyskin7851e002016-02-07 23:35:40 +0200428 cl = mei_cl_alloc_linked(bus);
Tomas Winkler60095952015-07-23 15:08:47 +0300429 mutex_unlock(&bus->device_lock);
430 if (IS_ERR(cl))
431 return PTR_ERR(cl);
432 /* update pointers */
433 cldev->cl = cl;
434 cl->cldev = cldev;
435 }
Tomas Winkler62382992015-07-23 15:08:35 +0300436
437 mutex_lock(&bus->device_lock);
Tomas Winkler62382992015-07-23 15:08:35 +0300438 if (mei_cl_is_connected(cl)) {
Tomas Winkler60095952015-07-23 15:08:47 +0300439 ret = 0;
440 goto out;
Tomas Winkler62382992015-07-23 15:08:35 +0300441 }
442
Tomas Winkler60095952015-07-23 15:08:47 +0300443 if (!mei_me_cl_is_active(cldev->me_cl)) {
444 dev_err(&cldev->dev, "me client is not active\n");
445 ret = -ENOTTY;
446 goto out;
Tomas Winkler62382992015-07-23 15:08:35 +0300447 }
448
Tomas Winkler60095952015-07-23 15:08:47 +0300449 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
450 if (ret < 0)
451 dev_err(&cldev->dev, "cannot connect\n");
452
453out:
Tomas Winkler62382992015-07-23 15:08:35 +0300454 mutex_unlock(&bus->device_lock);
455
Tomas Winkler60095952015-07-23 15:08:47 +0300456 return ret;
Tomas Winkler62382992015-07-23 15:08:35 +0300457}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300458EXPORT_SYMBOL_GPL(mei_cldev_enable);
Tomas Winkler62382992015-07-23 15:08:35 +0300459
460/**
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300461 * mei_cldev_disable - disable me client device
Tomas Winkler62382992015-07-23 15:08:35 +0300462 * disconnect form the me client
463 *
464 * @cldev: me client device
465 *
466 * Return: 0 on success and < 0 on error
467 */
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300468int mei_cldev_disable(struct mei_cl_device *cldev)
Tomas Winkler62382992015-07-23 15:08:35 +0300469{
Tomas Winkler62382992015-07-23 15:08:35 +0300470 struct mei_device *bus;
Tomas Winkler60095952015-07-23 15:08:47 +0300471 struct mei_cl *cl;
472 int err;
Tomas Winkler62382992015-07-23 15:08:35 +0300473
Tomas Winkler60095952015-07-23 15:08:47 +0300474 if (!cldev || !cldev->cl)
Tomas Winkler62382992015-07-23 15:08:35 +0300475 return -ENODEV;
476
Tomas Winkler60095952015-07-23 15:08:47 +0300477 cl = cldev->cl;
478
479 bus = cldev->bus;
Tomas Winkler62382992015-07-23 15:08:35 +0300480
481 cldev->event_cb = NULL;
482
483 mutex_lock(&bus->device_lock);
484
485 if (!mei_cl_is_connected(cl)) {
Alexander Usyskind8820392016-10-30 01:42:19 +0200486 dev_dbg(bus->dev, "Already disconnected");
Tomas Winkler62382992015-07-23 15:08:35 +0300487 err = 0;
488 goto out;
489 }
490
491 err = mei_cl_disconnect(cl);
Tomas Winkler60095952015-07-23 15:08:47 +0300492 if (err < 0)
Tomas Winkler62382992015-07-23 15:08:35 +0300493 dev_err(bus->dev, "Could not disconnect from the ME client");
Tomas Winkler62382992015-07-23 15:08:35 +0300494
495out:
Tomas Winkler60095952015-07-23 15:08:47 +0300496 /* Flush queues and remove any pending read */
497 mei_cl_flush_queues(cl, NULL);
498 mei_cl_unlink(cl);
499
500 kfree(cl);
501 cldev->cl = NULL;
502
Tomas Winkler62382992015-07-23 15:08:35 +0300503 mutex_unlock(&bus->device_lock);
504 return err;
Tomas Winkler62382992015-07-23 15:08:35 +0300505}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300506EXPORT_SYMBOL_GPL(mei_cldev_disable);
Tomas Winkler62382992015-07-23 15:08:35 +0300507
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300508/**
509 * mei_cl_device_find - find matching entry in the driver id table
510 *
511 * @cldev: me client device
512 * @cldrv: me client driver
513 *
514 * Return: id on success; NULL if no id is matching
515 */
516static const
517struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
518 struct mei_cl_driver *cldrv)
Samuel Ortize5354102013-03-27 17:29:53 +0200519{
Samuel Ortize5354102013-03-27 17:29:53 +0200520 const struct mei_cl_device_id *id;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300521 const uuid_le *uuid;
Tomas Winklerb26864c2015-09-10 10:18:01 +0300522 u8 version;
523 bool match;
Samuel Ortize5354102013-03-27 17:29:53 +0200524
Tomas Winklerb37719c32015-07-23 15:08:33 +0300525 uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winklerb26864c2015-09-10 10:18:01 +0300526 version = mei_me_cl_ver(cldev->me_cl);
Samuel Ortize5354102013-03-27 17:29:53 +0200527
Tomas Winklerb37719c32015-07-23 15:08:33 +0300528 id = cldrv->id_table;
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700529 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700530 if (!uuid_le_cmp(*uuid, id->uuid)) {
Tomas Winklerb26864c2015-09-10 10:18:01 +0300531 match = true;
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300532
Tomas Winklerb26864c2015-09-10 10:18:01 +0300533 if (cldev->name[0])
534 if (strncmp(cldev->name, id->name,
535 sizeof(id->name)))
536 match = false;
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300537
Tomas Winklerb26864c2015-09-10 10:18:01 +0300538 if (id->version != MEI_CL_VERSION_ANY)
539 if (id->version != version)
540 match = false;
541 if (match)
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300542 return id;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300543 }
Samuel Ortize5354102013-03-27 17:29:53 +0200544
545 id++;
546 }
547
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300548 return NULL;
549}
550
551/**
552 * mei_cl_device_match - device match function
553 *
554 * @dev: device
555 * @drv: driver
556 *
557 * Return: 1 if matching device was found 0 otherwise
558 */
559static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
560{
561 struct mei_cl_device *cldev = to_mei_cl_device(dev);
562 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
563 const struct mei_cl_device_id *found_id;
564
565 if (!cldev)
566 return 0;
567
Tomas Winkler71ce7892015-07-23 15:08:43 +0300568 if (!cldev->do_match)
569 return 0;
570
Tomas Winkler688a9cc2015-07-23 15:08:39 +0300571 if (!cldrv || !cldrv->id_table)
572 return 0;
573
574 found_id = mei_cl_device_find(cldev, cldrv);
575 if (found_id)
576 return 1;
577
Samuel Ortize5354102013-03-27 17:29:53 +0200578 return 0;
579}
580
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300581/**
582 * mei_cl_device_probe - bus probe function
583 *
584 * @dev: device
585 *
586 * Return: 0 on success; < 0 otherwise
587 */
Samuel Ortize5354102013-03-27 17:29:53 +0200588static int mei_cl_device_probe(struct device *dev)
589{
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300590 struct mei_cl_device *cldev;
Tomas Winklerb37719c32015-07-23 15:08:33 +0300591 struct mei_cl_driver *cldrv;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300592 const struct mei_cl_device_id *id;
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300593 int ret;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300594
595 cldev = to_mei_cl_device(dev);
596 cldrv = to_mei_cl_driver(dev->driver);
Samuel Ortize5354102013-03-27 17:29:53 +0200597
Tomas Winklerb37719c32015-07-23 15:08:33 +0300598 if (!cldev)
Samuel Ortize5354102013-03-27 17:29:53 +0200599 return 0;
600
Tomas Winklerb37719c32015-07-23 15:08:33 +0300601 if (!cldrv || !cldrv->probe)
Samuel Ortize5354102013-03-27 17:29:53 +0200602 return -ENODEV;
603
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300604 id = mei_cl_device_find(cldev, cldrv);
605 if (!id)
606 return -ENODEV;
Samuel Ortize5354102013-03-27 17:29:53 +0200607
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300608 ret = cldrv->probe(cldev, id);
609 if (ret)
610 return ret;
Samuel Ortize5354102013-03-27 17:29:53 +0200611
Alexey Khoroshilovb9c79542016-04-01 23:53:01 +0300612 __module_get(THIS_MODULE);
613 return 0;
Samuel Ortize5354102013-03-27 17:29:53 +0200614}
615
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300616/**
617 * mei_cl_device_remove - remove device from the bus
618 *
619 * @dev: device
620 *
621 * Return: 0 on success; < 0 otherwise
622 */
Samuel Ortize5354102013-03-27 17:29:53 +0200623static int mei_cl_device_remove(struct device *dev)
624{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300625 struct mei_cl_device *cldev = to_mei_cl_device(dev);
626 struct mei_cl_driver *cldrv;
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300627 int ret = 0;
Samuel Ortize5354102013-03-27 17:29:53 +0200628
Tomas Winklerb37719c32015-07-23 15:08:33 +0300629 if (!cldev || !dev->driver)
Samuel Ortize5354102013-03-27 17:29:53 +0200630 return 0;
631
Tomas Winklerb37719c32015-07-23 15:08:33 +0300632 if (cldev->event_cb) {
633 cldev->event_cb = NULL;
634 cancel_work_sync(&cldev->event_work);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200635 }
636
Tomas Winklerb37719c32015-07-23 15:08:33 +0300637 cldrv = to_mei_cl_driver(dev->driver);
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300638 if (cldrv->remove)
639 ret = cldrv->remove(cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200640
Tomas Winklerfeb8cd02015-07-23 15:08:40 +0300641 module_put(THIS_MODULE);
642 dev->driver = NULL;
643 return ret;
Samuel Ortize5354102013-03-27 17:29:53 +0200644
Samuel Ortize5354102013-03-27 17:29:53 +0200645}
646
Tomas Winkler007d64e2015-05-07 15:54:03 +0300647static ssize_t name_show(struct device *dev, struct device_attribute *a,
648 char *buf)
649{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300650 struct mei_cl_device *cldev = to_mei_cl_device(dev);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300651
Rasmus Villemoes423de922016-03-24 10:38:04 +0200652 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300653}
654static DEVICE_ATTR_RO(name);
655
656static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
657 char *buf)
658{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300659 struct mei_cl_device *cldev = to_mei_cl_device(dev);
660 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300661
Rasmus Villemoes423de922016-03-24 10:38:04 +0200662 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
Tomas Winkler007d64e2015-05-07 15:54:03 +0300663}
664static DEVICE_ATTR_RO(uuid);
665
Tomas Winkler40b73202015-09-10 10:18:00 +0300666static ssize_t version_show(struct device *dev, struct device_attribute *a,
667 char *buf)
668{
669 struct mei_cl_device *cldev = to_mei_cl_device(dev);
670 u8 version = mei_me_cl_ver(cldev->me_cl);
Tomas Winkler40b73202015-09-10 10:18:00 +0300671
Rasmus Villemoes423de922016-03-24 10:38:04 +0200672 return scnprintf(buf, PAGE_SIZE, "%02X", version);
Tomas Winkler40b73202015-09-10 10:18:00 +0300673}
674static DEVICE_ATTR_RO(version);
675
Samuel Ortize5354102013-03-27 17:29:53 +0200676static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
677 char *buf)
678{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300679 struct mei_cl_device *cldev = to_mei_cl_device(dev);
680 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Samuel Ortize5354102013-03-27 17:29:53 +0200681
Rasmus Villemoes423de922016-03-24 10:38:04 +0200682 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
Samuel Ortize5354102013-03-27 17:29:53 +0200683}
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700684static DEVICE_ATTR_RO(modalias);
Samuel Ortize5354102013-03-27 17:29:53 +0200685
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300686static struct attribute *mei_cldev_attrs[] = {
Tomas Winkler007d64e2015-05-07 15:54:03 +0300687 &dev_attr_name.attr,
688 &dev_attr_uuid.attr,
Tomas Winkler40b73202015-09-10 10:18:00 +0300689 &dev_attr_version.attr,
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700690 &dev_attr_modalias.attr,
691 NULL,
Samuel Ortize5354102013-03-27 17:29:53 +0200692};
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300693ATTRIBUTE_GROUPS(mei_cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200694
Tomas Winkler38d3c002015-07-23 15:08:36 +0300695/**
696 * mei_cl_device_uevent - me client bus uevent handler
697 *
698 * @dev: device
699 * @env: uevent kobject
700 *
701 * Return: 0 on success -ENOMEM on when add_uevent_var fails
702 */
703static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Samuel Ortize5354102013-03-27 17:29:53 +0200704{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300705 struct mei_cl_device *cldev = to_mei_cl_device(dev);
706 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
Tomas Winkler40b73202015-09-10 10:18:00 +0300707 u8 version = mei_me_cl_ver(cldev->me_cl);
708
709 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
710 return -ENOMEM;
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300711
Tomas Winkler007d64e2015-05-07 15:54:03 +0300712 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
713 return -ENOMEM;
714
Tomas Winklerb37719c32015-07-23 15:08:33 +0300715 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
Tomas Winkler007d64e2015-05-07 15:54:03 +0300716 return -ENOMEM;
717
Tomas Winklerb26864c2015-09-10 10:18:01 +0300718 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
719 cldev->name, uuid, version))
Samuel Ortize5354102013-03-27 17:29:53 +0200720 return -ENOMEM;
721
722 return 0;
723}
724
725static struct bus_type mei_cl_bus_type = {
726 .name = "mei",
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300727 .dev_groups = mei_cldev_groups,
Samuel Ortize5354102013-03-27 17:29:53 +0200728 .match = mei_cl_device_match,
729 .probe = mei_cl_device_probe,
730 .remove = mei_cl_device_remove,
Tomas Winkler38d3c002015-07-23 15:08:36 +0300731 .uevent = mei_cl_device_uevent,
Samuel Ortize5354102013-03-27 17:29:53 +0200732};
733
Tomas Winkler512f64d2015-07-23 15:08:41 +0300734static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
735{
736 if (bus)
737 get_device(bus->dev);
738
739 return bus;
740}
741
742static void mei_dev_bus_put(struct mei_device *bus)
743{
744 if (bus)
745 put_device(bus->dev);
746}
747
Tomas Winklerae48d742015-09-10 10:18:06 +0300748static void mei_cl_bus_dev_release(struct device *dev)
Samuel Ortize5354102013-03-27 17:29:53 +0200749{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300750 struct mei_cl_device *cldev = to_mei_cl_device(dev);
Alexander Usyskind49ed642015-05-04 09:43:54 +0300751
Tomas Winklerb37719c32015-07-23 15:08:33 +0300752 if (!cldev)
Alexander Usyskind49ed642015-05-04 09:43:54 +0300753 return;
754
Tomas Winklerb37719c32015-07-23 15:08:33 +0300755 mei_me_cl_put(cldev->me_cl);
Tomas Winkler512f64d2015-07-23 15:08:41 +0300756 mei_dev_bus_put(cldev->bus);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300757 kfree(cldev);
Samuel Ortize5354102013-03-27 17:29:53 +0200758}
759
760static struct device_type mei_cl_device_type = {
Tomas Winklerae48d742015-09-10 10:18:06 +0300761 .release = mei_cl_bus_dev_release,
Samuel Ortize5354102013-03-27 17:29:53 +0200762};
763
Tomas Winkler71ce7892015-07-23 15:08:43 +0300764/**
Tomas Winkler213dd192015-10-28 14:34:35 +0200765 * mei_cl_bus_set_name - set device name for me client device
766 *
767 * @cldev: me client device
768 */
769static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
770{
771 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
772 cldev->name,
773 mei_me_cl_uuid(cldev->me_cl),
774 mei_me_cl_ver(cldev->me_cl));
775}
776
777/**
Tomas Winklerae48d742015-09-10 10:18:06 +0300778 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
Tomas Winkler71ce7892015-07-23 15:08:43 +0300779 *
780 * @bus: mei device
781 * @me_cl: me client
782 *
783 * Return: allocated device structur or NULL on allocation failure
784 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300785static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
786 struct mei_me_client *me_cl)
Tomas Winkler71ce7892015-07-23 15:08:43 +0300787{
788 struct mei_cl_device *cldev;
789
790 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
791 if (!cldev)
792 return NULL;
793
794 device_initialize(&cldev->dev);
795 cldev->dev.parent = bus->dev;
796 cldev->dev.bus = &mei_cl_bus_type;
797 cldev->dev.type = &mei_cl_device_type;
798 cldev->bus = mei_dev_bus_get(bus);
799 cldev->me_cl = mei_me_cl_get(me_cl);
Tomas Winkler213dd192015-10-28 14:34:35 +0200800 mei_cl_bus_set_name(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300801 cldev->is_added = 0;
802 INIT_LIST_HEAD(&cldev->bus_list);
803
804 return cldev;
805}
806
807/**
808 * mei_cl_dev_setup - setup me client device
809 * run fix up routines and set the device name
810 *
811 * @bus: mei device
812 * @cldev: me client device
813 *
814 * Return: true if the device is eligible for enumeration
815 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300816static bool mei_cl_bus_dev_setup(struct mei_device *bus,
817 struct mei_cl_device *cldev)
Tomas Winkler71ce7892015-07-23 15:08:43 +0300818{
819 cldev->do_match = 1;
Tomas Winklerae48d742015-09-10 10:18:06 +0300820 mei_cl_bus_dev_fixup(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300821
Tomas Winkler213dd192015-10-28 14:34:35 +0200822 /* the device name can change during fix up */
Tomas Winkler71ce7892015-07-23 15:08:43 +0300823 if (cldev->do_match)
Tomas Winkler213dd192015-10-28 14:34:35 +0200824 mei_cl_bus_set_name(cldev);
Tomas Winkler71ce7892015-07-23 15:08:43 +0300825
826 return cldev->do_match == 1;
827}
828
829/**
830 * mei_cl_bus_dev_add - add me client devices
831 *
832 * @cldev: me client device
833 *
834 * Return: 0 on success; < 0 on failre
835 */
836static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
837{
838 int ret;
839
Tomas Winklerb26864c2015-09-10 10:18:01 +0300840 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
841 mei_me_cl_uuid(cldev->me_cl),
842 mei_me_cl_ver(cldev->me_cl));
Tomas Winkler71ce7892015-07-23 15:08:43 +0300843 ret = device_add(&cldev->dev);
844 if (!ret)
845 cldev->is_added = 1;
846
847 return ret;
848}
849
Tomas Winkler60095952015-07-23 15:08:47 +0300850/**
851 * mei_cl_bus_dev_stop - stop the driver
852 *
853 * @cldev: me client device
854 */
855static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
856{
857 if (cldev->is_added)
858 device_release_driver(&cldev->dev);
859}
860
861/**
862 * mei_cl_bus_dev_destroy - destroy me client devices object
863 *
864 * @cldev: me client device
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200865 *
866 * Locking: called under "dev->cl_bus_lock" lock
Tomas Winkler60095952015-07-23 15:08:47 +0300867 */
868static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
869{
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200870
871 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
872
Tomas Winkler60095952015-07-23 15:08:47 +0300873 if (!cldev->is_added)
874 return;
875
876 device_del(&cldev->dev);
877
Tomas Winkler60095952015-07-23 15:08:47 +0300878 list_del_init(&cldev->bus_list);
Tomas Winkler60095952015-07-23 15:08:47 +0300879
880 cldev->is_added = 0;
881 put_device(&cldev->dev);
882}
883
884/**
885 * mei_cl_bus_remove_device - remove a devices form the bus
886 *
887 * @cldev: me client device
888 */
889static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
890{
891 mei_cl_bus_dev_stop(cldev);
892 mei_cl_bus_dev_destroy(cldev);
893}
894
895/**
896 * mei_cl_bus_remove_devices - remove all devices form the bus
897 *
898 * @bus: mei device
899 */
900void mei_cl_bus_remove_devices(struct mei_device *bus)
901{
902 struct mei_cl_device *cldev, *next;
903
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200904 mutex_lock(&bus->cl_bus_lock);
Tomas Winkler60095952015-07-23 15:08:47 +0300905 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
906 mei_cl_bus_remove_device(cldev);
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200907 mutex_unlock(&bus->cl_bus_lock);
Tomas Winkler60095952015-07-23 15:08:47 +0300908}
909
910
911/**
Tomas Winklerae48d742015-09-10 10:18:06 +0300912 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
Tomas Winkler60095952015-07-23 15:08:47 +0300913 * based on me client
914 *
915 * @bus: mei device
916 * @me_cl: me client
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200917 *
918 * Locking: called under "dev->cl_bus_lock" lock
Tomas Winkler60095952015-07-23 15:08:47 +0300919 */
Tomas Winklerae48d742015-09-10 10:18:06 +0300920static void mei_cl_bus_dev_init(struct mei_device *bus,
921 struct mei_me_client *me_cl)
Samuel Ortize5354102013-03-27 17:29:53 +0200922{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300923 struct mei_cl_device *cldev;
Tomas Winkler60095952015-07-23 15:08:47 +0300924
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200925 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
926
Tomas Winkler60095952015-07-23 15:08:47 +0300927 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
928
929 if (me_cl->bus_added)
930 return;
Samuel Ortize5354102013-03-27 17:29:53 +0200931
Tomas Winklerae48d742015-09-10 10:18:06 +0300932 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300933 if (!cldev)
Tomas Winkler60095952015-07-23 15:08:47 +0300934 return;
Samuel Ortize5354102013-03-27 17:29:53 +0200935
Tomas Winkler60095952015-07-23 15:08:47 +0300936 me_cl->bus_added = true;
937 list_add_tail(&cldev->bus_list, &bus->device_list);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300938
Samuel Ortize5354102013-03-27 17:29:53 +0200939}
Samuel Ortize5354102013-03-27 17:29:53 +0200940
Tomas Winkler60095952015-07-23 15:08:47 +0300941/**
942 * mei_cl_bus_rescan - scan me clients list and add create
943 * devices for eligible clients
944 *
945 * @bus: mei device
946 */
947void mei_cl_bus_rescan(struct mei_device *bus)
Samuel Ortize5354102013-03-27 17:29:53 +0200948{
Tomas Winkler60095952015-07-23 15:08:47 +0300949 struct mei_cl_device *cldev, *n;
950 struct mei_me_client *me_cl;
951
Tomas Winkler2da55cf2015-10-28 14:34:34 +0200952 mutex_lock(&bus->cl_bus_lock);
953
Tomas Winkler60095952015-07-23 15:08:47 +0300954 down_read(&bus->me_clients_rwsem);
955 list_for_each_entry(me_cl, &bus->me_clients, list)
Tomas Winklerae48d742015-09-10 10:18:06 +0300956 mei_cl_bus_dev_init(bus, me_cl);
Tomas Winkler60095952015-07-23 15:08:47 +0300957 up_read(&bus->me_clients_rwsem);
958
Tomas Winkler60095952015-07-23 15:08:47 +0300959 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
960
961 if (!mei_me_cl_is_active(cldev->me_cl)) {
962 mei_cl_bus_remove_device(cldev);
963 continue;
964 }
965
966 if (cldev->is_added)
967 continue;
968
Tomas Winklerae48d742015-09-10 10:18:06 +0300969 if (mei_cl_bus_dev_setup(bus, cldev))
Tomas Winkler60095952015-07-23 15:08:47 +0300970 mei_cl_bus_dev_add(cldev);
971 else {
972 list_del_init(&cldev->bus_list);
973 put_device(&cldev->dev);
974 }
975 }
976 mutex_unlock(&bus->cl_bus_lock);
977
978 dev_dbg(bus->dev, "rescan end");
Samuel Ortize5354102013-03-27 17:29:53 +0200979}
Samuel Ortiz333e4ee2013-03-27 17:29:54 +0200980
Alexander Usyskina816a002016-02-07 23:35:41 +0200981void mei_cl_bus_rescan_work(struct work_struct *work)
982{
983 struct mei_device *bus =
984 container_of(work, struct mei_device, bus_rescan_work);
Alexander Usyskin025fb792016-02-07 23:35:43 +0200985 struct mei_me_client *me_cl;
986
Alexander Usyskin025fb792016-02-07 23:35:43 +0200987 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);
Alexander Usyskina816a002016-02-07 23:35:41 +0200991
992 mei_cl_bus_rescan(bus);
993}
994
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300995int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
996 struct module *owner)
Samuel Ortiz333e4ee2013-03-27 17:29:54 +0200997{
998 int err;
999
Tomas Winklerb37719c32015-07-23 15:08:33 +03001000 cldrv->driver.name = cldrv->name;
1001 cldrv->driver.owner = owner;
1002 cldrv->driver.bus = &mei_cl_bus_type;
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001003
Tomas Winklerb37719c32015-07-23 15:08:33 +03001004 err = driver_register(&cldrv->driver);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001005 if (err)
1006 return err;
1007
Tomas Winklerb37719c32015-07-23 15:08:33 +03001008 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001009
1010 return 0;
1011}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001012EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001013
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001014void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001015{
Tomas Winklerb37719c32015-07-23 15:08:33 +03001016 driver_unregister(&cldrv->driver);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001017
Tomas Winklerb37719c32015-07-23 15:08:33 +03001018 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +02001019}
Tomas Winklerd49dc5e2015-09-10 10:18:05 +03001020EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
Samuel Ortiz3e833292013-03-27 17:29:55 +02001021
Tomas Winkler60095952015-07-23 15:08:47 +03001022
Samuel Ortizcf3baef2013-03-27 17:29:57 +02001023int __init mei_cl_bus_init(void)
1024{
1025 return bus_register(&mei_cl_bus_type);
1026}
1027
1028void __exit mei_cl_bus_exit(void)
1029{
1030 bus_unregister(&mei_cl_bus_type);
1031}