2 * Intel Management Engine Interface (Intel MEI) Linux driver
3 * Copyright (c) 2012-2013, Intel Corporation.
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.
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
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/interrupt.h>
25 #include <linux/pci.h>
26 #include <linux/mei_cl_bus.h>
31 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
32 #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
34 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
36 struct mei_cl_device *device = to_mei_cl_device(dev);
37 struct mei_cl_driver *driver = to_mei_cl_driver(drv);
38 const struct mei_cl_device_id *id;
43 if (!driver || !driver->id_table)
46 id = driver->id_table;
49 if (!strncmp(dev_name(dev), id->name, sizeof(id->name)))
58 static int mei_cl_device_probe(struct device *dev)
60 struct mei_cl_device *device = to_mei_cl_device(dev);
61 struct mei_cl_driver *driver;
62 struct mei_cl_device_id id;
67 driver = to_mei_cl_driver(dev->driver);
68 if (!driver || !driver->probe)
71 dev_dbg(dev, "Device probe\n");
73 strncpy(id.name, dev_name(dev), sizeof(id.name));
75 return driver->probe(device, &id);
78 static int mei_cl_device_remove(struct device *dev)
80 struct mei_cl_device *device = to_mei_cl_device(dev);
81 struct mei_cl_driver *driver;
83 if (!device || !dev->driver)
86 if (device->event_cb) {
87 device->event_cb = NULL;
88 cancel_work_sync(&device->event_work);
91 driver = to_mei_cl_driver(dev->driver);
92 if (!driver->remove) {
98 return driver->remove(device);
101 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
106 len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev));
108 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
110 static DEVICE_ATTR_RO(modalias);
112 static struct attribute *mei_cl_dev_attrs[] = {
113 &dev_attr_modalias.attr,
116 ATTRIBUTE_GROUPS(mei_cl_dev);
118 static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
120 if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
126 static struct bus_type mei_cl_bus_type = {
128 .dev_groups = mei_cl_dev_groups,
129 .match = mei_cl_device_match,
130 .probe = mei_cl_device_probe,
131 .remove = mei_cl_device_remove,
132 .uevent = mei_cl_uevent,
135 static void mei_cl_dev_release(struct device *dev)
137 kfree(to_mei_cl_device(dev));
140 static struct device_type mei_cl_device_type = {
141 .release = mei_cl_dev_release,
144 static struct mei_cl *mei_bus_find_mei_cl_by_uuid(struct mei_device *dev,
149 list_for_each_entry(cl, &dev->device_list, device_link) {
150 if (!uuid_le_cmp(uuid, cl->cl_uuid))
156 struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
157 uuid_le uuid, char *name,
158 struct mei_cl_ops *ops)
160 struct mei_cl_device *device;
164 cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
168 device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
175 device->dev.parent = &dev->pdev->dev;
176 device->dev.bus = &mei_cl_bus_type;
177 device->dev.type = &mei_cl_device_type;
179 dev_set_name(&device->dev, "%s", name);
181 status = device_register(&device->dev);
183 dev_err(&dev->pdev->dev, "Failed to register MEI device\n");
190 dev_dbg(&device->dev, "client %s registered\n", name);
194 EXPORT_SYMBOL_GPL(mei_cl_add_device);
196 void mei_cl_remove_device(struct mei_cl_device *device)
198 device_unregister(&device->dev);
200 EXPORT_SYMBOL_GPL(mei_cl_remove_device);
202 int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
206 driver->driver.name = driver->name;
207 driver->driver.owner = owner;
208 driver->driver.bus = &mei_cl_bus_type;
210 err = driver_register(&driver->driver);
214 pr_debug("mei: driver [%s] registered\n", driver->driver.name);
218 EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
220 void mei_cl_driver_unregister(struct mei_cl_driver *driver)
222 driver_unregister(&driver->driver);
224 pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
226 EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
228 static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
231 struct mei_device *dev;
232 struct mei_me_client *me_cl;
233 struct mei_cl_cb *cb;
236 if (WARN_ON(!cl || !cl->dev))
241 if (cl->state != MEI_FILE_CONNECTED)
244 /* Check if we have an ME client device */
245 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
249 if (length > me_cl->props.max_msg_length)
252 cb = mei_io_cb_init(cl, NULL);
256 rets = mei_io_cb_alloc_req_buf(cb, length);
262 memcpy(cb->request_buffer.data, buf, length);
264 mutex_lock(&dev->device_lock);
266 rets = mei_cl_write(cl, cb, blocking);
268 mutex_unlock(&dev->device_lock);
275 int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
277 struct mei_device *dev;
278 struct mei_cl_cb *cb;
282 if (WARN_ON(!cl || !cl->dev))
287 mutex_lock(&dev->device_lock);
290 err = mei_cl_read_start(cl, length);
292 mutex_unlock(&dev->device_lock);
297 if (cl->reading_state != MEI_READ_COMPLETE &&
298 !waitqueue_active(&cl->rx_wait)) {
300 mutex_unlock(&dev->device_lock);
302 if (wait_event_interruptible(cl->rx_wait,
303 cl->reading_state == MEI_READ_COMPLETE ||
304 mei_cl_is_transitioning(cl))) {
306 if (signal_pending(current))
311 mutex_lock(&dev->device_lock);
316 if (cl->reading_state != MEI_READ_COMPLETE) {
321 r_length = min_t(size_t, length, cb->buf_idx);
323 memcpy(buf, cb->response_buffer.data, r_length);
326 cl->reading_state = MEI_IDLE;
330 mutex_unlock(&dev->device_lock);
335 inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
337 return ___mei_cl_send(cl, buf, length, 0);
340 inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
342 return ___mei_cl_send(cl, buf, length, 1);
345 int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
347 struct mei_cl *cl = device->cl;
352 if (device->ops && device->ops->send)
353 return device->ops->send(device, buf, length);
355 return __mei_cl_send(cl, buf, length);
357 EXPORT_SYMBOL_GPL(mei_cl_send);
359 int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
361 struct mei_cl *cl = device->cl;
366 if (device->ops && device->ops->recv)
367 return device->ops->recv(device, buf, length);
369 return __mei_cl_recv(cl, buf, length);
371 EXPORT_SYMBOL_GPL(mei_cl_recv);
373 static void mei_bus_event_work(struct work_struct *work)
375 struct mei_cl_device *device;
377 device = container_of(work, struct mei_cl_device, event_work);
379 if (device->event_cb)
380 device->event_cb(device, device->events, device->event_context);
384 /* Prepare for the next read */
385 mei_cl_read_start(device->cl, 0);
388 int mei_cl_register_event_cb(struct mei_cl_device *device,
389 mei_cl_event_cb_t event_cb, void *context)
391 if (device->event_cb)
395 device->event_cb = event_cb;
396 device->event_context = context;
397 INIT_WORK(&device->event_work, mei_bus_event_work);
399 mei_cl_read_start(device->cl, 0);
403 EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
405 void *mei_cl_get_drvdata(const struct mei_cl_device *device)
407 return dev_get_drvdata(&device->dev);
409 EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
411 void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
413 dev_set_drvdata(&device->dev, data);
415 EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
417 int mei_cl_enable_device(struct mei_cl_device *device)
420 struct mei_device *dev;
421 struct mei_cl *cl = device->cl;
428 mutex_lock(&dev->device_lock);
430 err = mei_cl_connect(cl, NULL);
432 mutex_unlock(&dev->device_lock);
433 dev_err(&dev->pdev->dev, "Could not connect to the ME client");
438 mutex_unlock(&dev->device_lock);
440 if (device->event_cb && !cl->read_cb)
441 mei_cl_read_start(device->cl, 0);
443 if (!device->ops || !device->ops->enable)
446 return device->ops->enable(device);
448 EXPORT_SYMBOL_GPL(mei_cl_enable_device);
450 int mei_cl_disable_device(struct mei_cl_device *device)
453 struct mei_device *dev;
454 struct mei_cl *cl = device->cl;
461 mutex_lock(&dev->device_lock);
463 if (cl->state != MEI_FILE_CONNECTED) {
464 mutex_unlock(&dev->device_lock);
465 dev_err(&dev->pdev->dev, "Already disconnected");
470 cl->state = MEI_FILE_DISCONNECTING;
472 err = mei_cl_disconnect(cl);
474 mutex_unlock(&dev->device_lock);
475 dev_err(&dev->pdev->dev,
476 "Could not disconnect from the ME client");
481 /* Flush queues and remove any pending read */
482 mei_cl_flush_queues(cl);
485 struct mei_cl_cb *cb = NULL;
487 cb = mei_cl_find_read_cb(cl);
488 /* Remove entry from read list */
501 device->event_cb = NULL;
503 mutex_unlock(&dev->device_lock);
505 if (!device->ops || !device->ops->disable)
508 return device->ops->disable(device);
510 EXPORT_SYMBOL_GPL(mei_cl_disable_device);
512 void mei_cl_bus_rx_event(struct mei_cl *cl)
514 struct mei_cl_device *device = cl->device;
516 if (!device || !device->event_cb)
519 set_bit(MEI_CL_EVENT_RX, &device->events);
521 schedule_work(&device->event_work);
524 void mei_cl_bus_remove_devices(struct mei_device *dev)
526 struct mei_cl *cl, *next;
528 mutex_lock(&dev->device_lock);
529 list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
531 mei_cl_remove_device(cl->device);
533 list_del(&cl->device_link);
537 mutex_unlock(&dev->device_lock);
540 int __init mei_cl_bus_init(void)
542 return bus_register(&mei_cl_bus_type);
545 void __exit mei_cl_bus_exit(void)
547 bus_unregister(&mei_cl_bus_type);