blob: 4d20d60ca38d579d876fdbb24257f23c49c83eea [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
33static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
34{
35 struct mei_cl_device *device = to_mei_cl_device(dev);
36 struct mei_cl_driver *driver = to_mei_cl_driver(drv);
37 const struct mei_cl_device_id *id;
38
39 if (!device)
40 return 0;
41
42 if (!driver || !driver->id_table)
43 return 0;
44
45 id = driver->id_table;
46
47 while (id->name[0]) {
Tomas Winkler8b613bb2013-07-24 16:22:59 +030048 if (!strncmp(dev_name(dev), id->name, sizeof(id->name)))
Samuel Ortize5354102013-03-27 17:29:53 +020049 return 1;
50
51 id++;
52 }
53
54 return 0;
55}
56
57static int mei_cl_device_probe(struct device *dev)
58{
59 struct mei_cl_device *device = to_mei_cl_device(dev);
60 struct mei_cl_driver *driver;
61 struct mei_cl_device_id id;
62
63 if (!device)
64 return 0;
65
66 driver = to_mei_cl_driver(dev->driver);
67 if (!driver || !driver->probe)
68 return -ENODEV;
69
70 dev_dbg(dev, "Device probe\n");
71
Alexander Usyskincfda2792014-08-25 16:46:53 +030072 strlcpy(id.name, dev_name(dev), sizeof(id.name));
Samuel Ortize5354102013-03-27 17:29:53 +020073
74 return driver->probe(device, &id);
75}
76
77static int mei_cl_device_remove(struct device *dev)
78{
79 struct mei_cl_device *device = to_mei_cl_device(dev);
80 struct mei_cl_driver *driver;
81
82 if (!device || !dev->driver)
83 return 0;
84
Samuel Ortiz3e833292013-03-27 17:29:55 +020085 if (device->event_cb) {
86 device->event_cb = NULL;
87 cancel_work_sync(&device->event_work);
88 }
89
Samuel Ortize5354102013-03-27 17:29:53 +020090 driver = to_mei_cl_driver(dev->driver);
91 if (!driver->remove) {
92 dev->driver = NULL;
93
94 return 0;
95 }
96
97 return driver->remove(device);
98}
99
100static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
101 char *buf)
102{
103 int len;
104
105 len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev));
106
107 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
108}
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700109static DEVICE_ATTR_RO(modalias);
Samuel Ortize5354102013-03-27 17:29:53 +0200110
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700111static struct attribute *mei_cl_dev_attrs[] = {
112 &dev_attr_modalias.attr,
113 NULL,
Samuel Ortize5354102013-03-27 17:29:53 +0200114};
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700115ATTRIBUTE_GROUPS(mei_cl_dev);
Samuel Ortize5354102013-03-27 17:29:53 +0200116
117static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
118{
119 if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
120 return -ENOMEM;
121
122 return 0;
123}
124
125static struct bus_type mei_cl_bus_type = {
126 .name = "mei",
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700127 .dev_groups = mei_cl_dev_groups,
Samuel Ortize5354102013-03-27 17:29:53 +0200128 .match = mei_cl_device_match,
129 .probe = mei_cl_device_probe,
130 .remove = mei_cl_device_remove,
131 .uevent = mei_cl_uevent,
132};
133
134static void mei_cl_dev_release(struct device *dev)
135{
136 kfree(to_mei_cl_device(dev));
137}
138
139static struct device_type mei_cl_device_type = {
140 .release = mei_cl_dev_release,
141};
142
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200143static struct mei_cl *mei_bus_find_mei_cl_by_uuid(struct mei_device *dev,
144 uuid_le uuid)
145{
Tomas Winkler31f88f52014-02-17 15:13:25 +0200146 struct mei_cl *cl;
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200147
Tomas Winkler31f88f52014-02-17 15:13:25 +0200148 list_for_each_entry(cl, &dev->device_list, device_link) {
Tomas Winklerd880f322014-08-21 14:29:15 +0300149 if (!uuid_le_cmp(uuid, cl->cl_uuid))
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200150 return cl;
151 }
152
153 return NULL;
154}
155struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
Samuel Ortize46980a2013-04-09 01:51:38 +0300156 uuid_le uuid, char *name,
157 struct mei_cl_ops *ops)
Samuel Ortize5354102013-03-27 17:29:53 +0200158{
159 struct mei_cl_device *device;
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200160 struct mei_cl *cl;
Samuel Ortize5354102013-03-27 17:29:53 +0200161 int status;
162
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200163 cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
164 if (cl == NULL)
165 return NULL;
166
Samuel Ortize5354102013-03-27 17:29:53 +0200167 device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
168 if (!device)
169 return NULL;
170
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200171 device->cl = cl;
Samuel Ortize46980a2013-04-09 01:51:38 +0300172 device->ops = ops;
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200173
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300174 device->dev.parent = dev->dev;
Samuel Ortize5354102013-03-27 17:29:53 +0200175 device->dev.bus = &mei_cl_bus_type;
176 device->dev.type = &mei_cl_device_type;
177
178 dev_set_name(&device->dev, "%s", name);
179
180 status = device_register(&device->dev);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200181 if (status) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300182 dev_err(dev->dev, "Failed to register MEI device\n");
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200183 kfree(device);
184 return NULL;
185 }
186
187 cl->device = device;
Samuel Ortize5354102013-03-27 17:29:53 +0200188
189 dev_dbg(&device->dev, "client %s registered\n", name);
190
191 return device;
Samuel Ortize5354102013-03-27 17:29:53 +0200192}
193EXPORT_SYMBOL_GPL(mei_cl_add_device);
194
195void mei_cl_remove_device(struct mei_cl_device *device)
196{
197 device_unregister(&device->dev);
198}
199EXPORT_SYMBOL_GPL(mei_cl_remove_device);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +0200200
201int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
202{
203 int err;
204
205 driver->driver.name = driver->name;
206 driver->driver.owner = owner;
207 driver->driver.bus = &mei_cl_bus_type;
208
209 err = driver_register(&driver->driver);
210 if (err)
211 return err;
212
213 pr_debug("mei: driver [%s] registered\n", driver->driver.name);
214
215 return 0;
216}
217EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
218
219void mei_cl_driver_unregister(struct mei_cl_driver *driver)
220{
221 driver_unregister(&driver->driver);
222
223 pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
224}
225EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200226
Samuel Ortiz44d88d92013-03-27 17:29:58 +0200227static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
228 bool blocking)
Samuel Ortiz3e833292013-03-27 17:29:55 +0200229{
230 struct mei_device *dev;
Tomas Winklerd3208322014-08-24 12:08:55 +0300231 struct mei_me_client *me_cl;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200232 struct mei_cl_cb *cb;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300233 int rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200234
235 if (WARN_ON(!cl || !cl->dev))
236 return -ENODEV;
237
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300238 dev = cl->dev;
239
Samuel Ortiz3e833292013-03-27 17:29:55 +0200240 if (cl->state != MEI_FILE_CONNECTED)
241 return -ENODEV;
242
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300243 /* Check if we have an ME client device */
Tomas Winklerd880f322014-08-21 14:29:15 +0300244 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300245 if (!me_cl)
246 return -ENOTTY;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300247
Tomas Winklerd3208322014-08-24 12:08:55 +0300248 if (length > me_cl->props.max_msg_length)
Alexander Usyskin86113502014-03-31 17:59:24 +0300249 return -EFBIG;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300250
Samuel Ortiz3e833292013-03-27 17:29:55 +0200251 cb = mei_io_cb_init(cl, NULL);
252 if (!cb)
253 return -ENOMEM;
254
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300255 rets = mei_io_cb_alloc_req_buf(cb, length);
256 if (rets < 0) {
Samuel Ortiz3e833292013-03-27 17:29:55 +0200257 mei_io_cb_free(cb);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300258 return rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200259 }
260
261 memcpy(cb->request_buffer.data, buf, length);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200262
263 mutex_lock(&dev->device_lock);
264
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300265 rets = mei_cl_write(cl, cb, blocking);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200266
267 mutex_unlock(&dev->device_lock);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300268 if (rets < 0)
269 mei_io_cb_free(cb);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200270
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300271 return rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200272}
273
274int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
275{
276 struct mei_device *dev;
277 struct mei_cl_cb *cb;
278 size_t r_length;
279 int err;
280
281 if (WARN_ON(!cl || !cl->dev))
282 return -ENODEV;
283
284 dev = cl->dev;
285
286 mutex_lock(&dev->device_lock);
287
288 if (!cl->read_cb) {
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300289 err = mei_cl_read_start(cl, length);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200290 if (err < 0) {
291 mutex_unlock(&dev->device_lock);
292 return err;
293 }
294 }
295
296 if (cl->reading_state != MEI_READ_COMPLETE &&
297 !waitqueue_active(&cl->rx_wait)) {
Tomas Winklere2b31642013-09-02 13:29:46 +0300298
Samuel Ortiz3e833292013-03-27 17:29:55 +0200299 mutex_unlock(&dev->device_lock);
300
301 if (wait_event_interruptible(cl->rx_wait,
Tomas Winklere2b31642013-09-02 13:29:46 +0300302 cl->reading_state == MEI_READ_COMPLETE ||
303 mei_cl_is_transitioning(cl))) {
304
Samuel Ortiz3e833292013-03-27 17:29:55 +0200305 if (signal_pending(current))
306 return -EINTR;
307 return -ERESTARTSYS;
308 }
309
310 mutex_lock(&dev->device_lock);
311 }
312
313 cb = cl->read_cb;
314
315 if (cl->reading_state != MEI_READ_COMPLETE) {
316 r_length = 0;
317 goto out;
318 }
319
320 r_length = min_t(size_t, length, cb->buf_idx);
321
322 memcpy(buf, cb->response_buffer.data, r_length);
323
324 mei_io_cb_free(cb);
325 cl->reading_state = MEI_IDLE;
326 cl->read_cb = NULL;
327
328out:
329 mutex_unlock(&dev->device_lock);
330
331 return r_length;
332}
333
Samuel Ortiz44d88d92013-03-27 17:29:58 +0200334inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
335{
336 return ___mei_cl_send(cl, buf, length, 0);
337}
338
339inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
340{
341 return ___mei_cl_send(cl, buf, length, 1);
342}
343
Samuel Ortiz3e833292013-03-27 17:29:55 +0200344int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
345{
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200346 struct mei_cl *cl = device->cl;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200347
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200348 if (cl == NULL)
349 return -ENODEV;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200350
351 if (device->ops && device->ops->send)
352 return device->ops->send(device, buf, length);
353
354 return __mei_cl_send(cl, buf, length);
355}
356EXPORT_SYMBOL_GPL(mei_cl_send);
357
358int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
359{
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200360 struct mei_cl *cl = device->cl;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200361
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200362 if (cl == NULL)
363 return -ENODEV;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200364
365 if (device->ops && device->ops->recv)
366 return device->ops->recv(device, buf, length);
367
368 return __mei_cl_recv(cl, buf, length);
369}
370EXPORT_SYMBOL_GPL(mei_cl_recv);
371
372static void mei_bus_event_work(struct work_struct *work)
373{
374 struct mei_cl_device *device;
375
376 device = container_of(work, struct mei_cl_device, event_work);
377
378 if (device->event_cb)
379 device->event_cb(device, device->events, device->event_context);
380
381 device->events = 0;
382
383 /* Prepare for the next read */
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300384 mei_cl_read_start(device->cl, 0);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200385}
386
387int mei_cl_register_event_cb(struct mei_cl_device *device,
388 mei_cl_event_cb_t event_cb, void *context)
389{
390 if (device->event_cb)
391 return -EALREADY;
392
393 device->events = 0;
394 device->event_cb = event_cb;
395 device->event_context = context;
396 INIT_WORK(&device->event_work, mei_bus_event_work);
397
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300398 mei_cl_read_start(device->cl, 0);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200399
400 return 0;
401}
402EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200403
Samuel Ortizaa6aef22013-03-27 17:29:59 +0200404void *mei_cl_get_drvdata(const struct mei_cl_device *device)
405{
406 return dev_get_drvdata(&device->dev);
407}
408EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
409
410void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
411{
412 dev_set_drvdata(&device->dev, data);
413}
414EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
415
Samuel Ortize46980a2013-04-09 01:51:38 +0300416int mei_cl_enable_device(struct mei_cl_device *device)
417{
418 int err;
419 struct mei_device *dev;
420 struct mei_cl *cl = device->cl;
421
422 if (cl == NULL)
423 return -ENODEV;
424
425 dev = cl->dev;
426
427 mutex_lock(&dev->device_lock);
428
Samuel Ortize46980a2013-04-09 01:51:38 +0300429 err = mei_cl_connect(cl, NULL);
430 if (err < 0) {
431 mutex_unlock(&dev->device_lock);
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300432 dev_err(dev->dev, "Could not connect to the ME client");
Samuel Ortize46980a2013-04-09 01:51:38 +0300433
434 return err;
435 }
436
437 mutex_unlock(&dev->device_lock);
438
439 if (device->event_cb && !cl->read_cb)
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300440 mei_cl_read_start(device->cl, 0);
Samuel Ortize46980a2013-04-09 01:51:38 +0300441
442 if (!device->ops || !device->ops->enable)
443 return 0;
444
445 return device->ops->enable(device);
446}
447EXPORT_SYMBOL_GPL(mei_cl_enable_device);
448
449int mei_cl_disable_device(struct mei_cl_device *device)
450{
451 int err;
452 struct mei_device *dev;
453 struct mei_cl *cl = device->cl;
454
455 if (cl == NULL)
456 return -ENODEV;
457
458 dev = cl->dev;
459
460 mutex_lock(&dev->device_lock);
461
462 if (cl->state != MEI_FILE_CONNECTED) {
463 mutex_unlock(&dev->device_lock);
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300464 dev_err(dev->dev, "Already disconnected");
Samuel Ortize46980a2013-04-09 01:51:38 +0300465
466 return 0;
467 }
468
469 cl->state = MEI_FILE_DISCONNECTING;
470
471 err = mei_cl_disconnect(cl);
472 if (err < 0) {
473 mutex_unlock(&dev->device_lock);
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300474 dev_err(dev->dev,
Samuel Ortize46980a2013-04-09 01:51:38 +0300475 "Could not disconnect from the ME client");
476
477 return err;
478 }
479
480 /* Flush queues and remove any pending read */
481 mei_cl_flush_queues(cl);
482
483 if (cl->read_cb) {
484 struct mei_cl_cb *cb = NULL;
485
486 cb = mei_cl_find_read_cb(cl);
487 /* Remove entry from read list */
488 if (cb)
489 list_del(&cb->list);
490
491 cb = cl->read_cb;
492 cl->read_cb = NULL;
493
494 if (cb) {
495 mei_io_cb_free(cb);
496 cb = NULL;
497 }
498 }
499
Samuel Ortizbbedf2f2013-05-21 18:52:09 +0200500 device->event_cb = NULL;
501
Samuel Ortize46980a2013-04-09 01:51:38 +0300502 mutex_unlock(&dev->device_lock);
503
504 if (!device->ops || !device->ops->disable)
505 return 0;
506
507 return device->ops->disable(device);
508}
509EXPORT_SYMBOL_GPL(mei_cl_disable_device);
510
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200511void mei_cl_bus_rx_event(struct mei_cl *cl)
512{
513 struct mei_cl_device *device = cl->device;
514
515 if (!device || !device->event_cb)
516 return;
517
518 set_bit(MEI_CL_EVENT_RX, &device->events);
519
520 schedule_work(&device->event_work);
521}
522
Tomas Winkler48705692014-02-17 15:13:19 +0200523void mei_cl_bus_remove_devices(struct mei_device *dev)
524{
525 struct mei_cl *cl, *next;
526
527 mutex_lock(&dev->device_lock);
528 list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
529 if (cl->device)
530 mei_cl_remove_device(cl->device);
531
532 list_del(&cl->device_link);
533 mei_cl_unlink(cl);
534 kfree(cl);
535 }
536 mutex_unlock(&dev->device_lock);
537}
538
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200539int __init mei_cl_bus_init(void)
540{
541 return bus_register(&mei_cl_bus_type);
542}
543
544void __exit mei_cl_bus_exit(void)
545{
546 bus_unregister(&mei_cl_bus_type);
547}