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>
32 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
33 #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
35 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
37 struct mei_cl_device *device = to_mei_cl_device(dev);
38 struct mei_cl_driver *driver = to_mei_cl_driver(drv);
39 const struct mei_cl_device_id *id;
44 if (!driver || !driver->id_table)
47 id = driver->id_table;
50 if (!strncmp(dev_name(dev), id->name, sizeof(id->name)))
59 static int mei_cl_device_probe(struct device *dev)
61 struct mei_cl_device *device = to_mei_cl_device(dev);
62 struct mei_cl_driver *driver;
63 struct mei_cl_device_id id;
68 driver = to_mei_cl_driver(dev->driver);
69 if (!driver || !driver->probe)
72 dev_dbg(dev, "Device probe\n");
74 strncpy(id.name, dev_name(dev), sizeof(id.name));
76 return driver->probe(device, &id);
79 static int mei_cl_device_remove(struct device *dev)
81 struct mei_cl_device *device = to_mei_cl_device(dev);
82 struct mei_cl_driver *driver;
84 if (!device || !dev->driver)
87 if (device->event_cb) {
88 device->event_cb = NULL;
89 cancel_work_sync(&device->event_work);
92 driver = to_mei_cl_driver(dev->driver);
93 if (!driver->remove) {
99 return driver->remove(device);
102 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
107 len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev));
109 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
111 static DEVICE_ATTR_RO(modalias);
113 static struct attribute *mei_cl_dev_attrs[] = {
114 &dev_attr_modalias.attr,
117 ATTRIBUTE_GROUPS(mei_cl_dev);
119 static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
121 if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
127 static struct bus_type mei_cl_bus_type = {
129 .dev_groups = mei_cl_dev_groups,
130 .match = mei_cl_device_match,
131 .probe = mei_cl_device_probe,
132 .remove = mei_cl_device_remove,
133 .uevent = mei_cl_uevent,
136 static void mei_cl_dev_release(struct device *dev)
138 kfree(to_mei_cl_device(dev));
141 static struct device_type mei_cl_device_type = {
142 .release = mei_cl_dev_release,
145 static struct mei_cl *mei_bus_find_mei_cl_by_uuid(struct mei_device *dev,
148 struct mei_cl *cl, *next;
150 list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
151 if (!uuid_le_cmp(uuid, cl->device_uuid))
157 struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
158 uuid_le uuid, char *name,
159 struct mei_cl_ops *ops)
161 struct mei_cl_device *device;
165 cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
169 device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
176 device->dev.parent = &dev->pdev->dev;
177 device->dev.bus = &mei_cl_bus_type;
178 device->dev.type = &mei_cl_device_type;
180 dev_set_name(&device->dev, "%s", name);
182 status = device_register(&device->dev);
184 dev_err(&dev->pdev->dev, "Failed to register MEI device\n");
191 dev_dbg(&device->dev, "client %s registered\n", name);
195 EXPORT_SYMBOL_GPL(mei_cl_add_device);
197 void mei_cl_remove_device(struct mei_cl_device *device)
199 device_unregister(&device->dev);
201 EXPORT_SYMBOL_GPL(mei_cl_remove_device);
203 int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
207 driver->driver.name = driver->name;
208 driver->driver.owner = owner;
209 driver->driver.bus = &mei_cl_bus_type;
211 err = driver_register(&driver->driver);
215 pr_debug("mei: driver [%s] registered\n", driver->driver.name);
219 EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
221 void mei_cl_driver_unregister(struct mei_cl_driver *driver)
223 driver_unregister(&driver->driver);
225 pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
227 EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
229 static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
232 struct mei_device *dev;
233 struct mei_cl_cb *cb;
237 if (WARN_ON(!cl || !cl->dev))
242 if (cl->state != MEI_FILE_CONNECTED)
245 /* Check if we have an ME client device */
246 id = mei_me_cl_by_id(dev, cl->me_client_id);
250 if (length > dev->me_clients[id].props.max_msg_length)
253 cb = mei_io_cb_init(cl, NULL);
257 rets = mei_io_cb_alloc_req_buf(cb, length);
263 memcpy(cb->request_buffer.data, buf, length);
265 mutex_lock(&dev->device_lock);
267 rets = mei_cl_write(cl, cb, blocking);
269 mutex_unlock(&dev->device_lock);
276 int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
278 struct mei_device *dev;
279 struct mei_cl_cb *cb;
283 if (WARN_ON(!cl || !cl->dev))
288 mutex_lock(&dev->device_lock);
291 err = mei_cl_read_start(cl, length);
293 mutex_unlock(&dev->device_lock);
298 if (cl->reading_state != MEI_READ_COMPLETE &&
299 !waitqueue_active(&cl->rx_wait)) {
300 mutex_unlock(&dev->device_lock);
302 if (wait_event_interruptible(cl->rx_wait,
303 (MEI_READ_COMPLETE == cl->reading_state))) {
304 if (signal_pending(current))
309 mutex_lock(&dev->device_lock);
314 if (cl->reading_state != MEI_READ_COMPLETE) {
319 r_length = min_t(size_t, length, cb->buf_idx);
321 memcpy(buf, cb->response_buffer.data, r_length);
324 cl->reading_state = MEI_IDLE;
328 mutex_unlock(&dev->device_lock);
333 inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
335 return ___mei_cl_send(cl, buf, length, 0);
338 inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
340 return ___mei_cl_send(cl, buf, length, 1);
343 int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
345 struct mei_cl *cl = device->cl;
350 if (device->ops && device->ops->send)
351 return device->ops->send(device, buf, length);
353 return __mei_cl_send(cl, buf, length);
355 EXPORT_SYMBOL_GPL(mei_cl_send);
357 int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
359 struct mei_cl *cl = device->cl;
364 if (device->ops && device->ops->recv)
365 return device->ops->recv(device, buf, length);
367 return __mei_cl_recv(cl, buf, length);
369 EXPORT_SYMBOL_GPL(mei_cl_recv);
371 static void mei_bus_event_work(struct work_struct *work)
373 struct mei_cl_device *device;
375 device = container_of(work, struct mei_cl_device, event_work);
377 if (device->event_cb)
378 device->event_cb(device, device->events, device->event_context);
382 /* Prepare for the next read */
383 mei_cl_read_start(device->cl, 0);
386 int mei_cl_register_event_cb(struct mei_cl_device *device,
387 mei_cl_event_cb_t event_cb, void *context)
389 if (device->event_cb)
393 device->event_cb = event_cb;
394 device->event_context = context;
395 INIT_WORK(&device->event_work, mei_bus_event_work);
397 mei_cl_read_start(device->cl, 0);
401 EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
403 void *mei_cl_get_drvdata(const struct mei_cl_device *device)
405 return dev_get_drvdata(&device->dev);
407 EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
409 void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
411 dev_set_drvdata(&device->dev, data);
413 EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
415 int mei_cl_enable_device(struct mei_cl_device *device)
418 struct mei_device *dev;
419 struct mei_cl *cl = device->cl;
426 mutex_lock(&dev->device_lock);
428 cl->state = MEI_FILE_CONNECTING;
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 int __init mei_cl_bus_init(void)
526 return bus_register(&mei_cl_bus_type);
529 void __exit mei_cl_bus_exit(void)
531 bus_unregister(&mei_cl_bus_type);