bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  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. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/mutex.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/mei_cl_bus.h>
  25. #include "mei_dev.h"
  26. #include "client.h"
  27. #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
  28. #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
  29. static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
  30. {
  31. struct mei_cl_device *device = to_mei_cl_device(dev);
  32. struct mei_cl_driver *driver = to_mei_cl_driver(drv);
  33. const struct mei_cl_device_id *id;
  34. if (!device)
  35. return 0;
  36. if (!driver || !driver->id_table)
  37. return 0;
  38. id = driver->id_table;
  39. while (id->name[0]) {
  40. if (!strncmp(dev_name(dev), id->name, sizeof(id->name)))
  41. return 1;
  42. id++;
  43. }
  44. return 0;
  45. }
  46. static int mei_cl_device_probe(struct device *dev)
  47. {
  48. struct mei_cl_device *device = to_mei_cl_device(dev);
  49. struct mei_cl_driver *driver;
  50. struct mei_cl_device_id id;
  51. if (!device)
  52. return 0;
  53. driver = to_mei_cl_driver(dev->driver);
  54. if (!driver || !driver->probe)
  55. return -ENODEV;
  56. dev_dbg(dev, "Device probe\n");
  57. strlcpy(id.name, dev_name(dev), sizeof(id.name));
  58. return driver->probe(device, &id);
  59. }
  60. static int mei_cl_device_remove(struct device *dev)
  61. {
  62. struct mei_cl_device *device = to_mei_cl_device(dev);
  63. struct mei_cl_driver *driver;
  64. if (!device || !dev->driver)
  65. return 0;
  66. if (device->event_cb) {
  67. device->event_cb = NULL;
  68. cancel_work_sync(&device->event_work);
  69. }
  70. driver = to_mei_cl_driver(dev->driver);
  71. if (!driver->remove) {
  72. dev->driver = NULL;
  73. return 0;
  74. }
  75. return driver->remove(device);
  76. }
  77. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  78. char *buf)
  79. {
  80. int len;
  81. len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev));
  82. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  83. }
  84. static DEVICE_ATTR_RO(modalias);
  85. static struct attribute *mei_cl_dev_attrs[] = {
  86. &dev_attr_modalias.attr,
  87. NULL,
  88. };
  89. ATTRIBUTE_GROUPS(mei_cl_dev);
  90. static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
  91. {
  92. if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
  93. return -ENOMEM;
  94. return 0;
  95. }
  96. static struct bus_type mei_cl_bus_type = {
  97. .name = "mei",
  98. .dev_groups = mei_cl_dev_groups,
  99. .match = mei_cl_device_match,
  100. .probe = mei_cl_device_probe,
  101. .remove = mei_cl_device_remove,
  102. .uevent = mei_cl_uevent,
  103. };
  104. static void mei_cl_dev_release(struct device *dev)
  105. {
  106. kfree(to_mei_cl_device(dev));
  107. }
  108. static struct device_type mei_cl_device_type = {
  109. .release = mei_cl_dev_release,
  110. };
  111. struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *dev,
  112. uuid_le uuid)
  113. {
  114. struct mei_cl *cl;
  115. list_for_each_entry(cl, &dev->device_list, device_link) {
  116. if (!uuid_le_cmp(uuid, cl->cl_uuid))
  117. return cl;
  118. }
  119. return NULL;
  120. }
  121. struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
  122. uuid_le uuid, char *name,
  123. struct mei_cl_ops *ops)
  124. {
  125. struct mei_cl_device *device;
  126. struct mei_cl *cl;
  127. int status;
  128. cl = mei_cl_bus_find_cl_by_uuid(dev, uuid);
  129. if (cl == NULL)
  130. return NULL;
  131. device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
  132. if (!device)
  133. return NULL;
  134. device->cl = cl;
  135. device->ops = ops;
  136. device->dev.parent = dev->dev;
  137. device->dev.bus = &mei_cl_bus_type;
  138. device->dev.type = &mei_cl_device_type;
  139. dev_set_name(&device->dev, "%s", name);
  140. status = device_register(&device->dev);
  141. if (status) {
  142. dev_err(dev->dev, "Failed to register MEI device\n");
  143. kfree(device);
  144. return NULL;
  145. }
  146. cl->device = device;
  147. dev_dbg(&device->dev, "client %s registered\n", name);
  148. return device;
  149. }
  150. EXPORT_SYMBOL_GPL(mei_cl_add_device);
  151. void mei_cl_remove_device(struct mei_cl_device *device)
  152. {
  153. device_unregister(&device->dev);
  154. }
  155. EXPORT_SYMBOL_GPL(mei_cl_remove_device);
  156. int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
  157. {
  158. int err;
  159. driver->driver.name = driver->name;
  160. driver->driver.owner = owner;
  161. driver->driver.bus = &mei_cl_bus_type;
  162. err = driver_register(&driver->driver);
  163. if (err)
  164. return err;
  165. pr_debug("mei: driver [%s] registered\n", driver->driver.name);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
  169. void mei_cl_driver_unregister(struct mei_cl_driver *driver)
  170. {
  171. driver_unregister(&driver->driver);
  172. pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
  173. }
  174. EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
  175. static ssize_t ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
  176. bool blocking)
  177. {
  178. struct mei_device *dev;
  179. struct mei_me_client *me_cl = NULL;
  180. struct mei_cl_cb *cb = NULL;
  181. ssize_t rets;
  182. if (WARN_ON(!cl || !cl->dev))
  183. return -ENODEV;
  184. dev = cl->dev;
  185. mutex_lock(&dev->device_lock);
  186. if (!mei_cl_is_connected(cl)) {
  187. rets = -ENODEV;
  188. goto out;
  189. }
  190. /* Check if we have an ME client device */
  191. me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
  192. if (!me_cl) {
  193. rets = -ENOTTY;
  194. goto out;
  195. }
  196. if (length > me_cl->props.max_msg_length) {
  197. rets = -EFBIG;
  198. goto out;
  199. }
  200. cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
  201. if (!cb) {
  202. rets = -ENOMEM;
  203. goto out;
  204. }
  205. memcpy(cb->buf.data, buf, length);
  206. rets = mei_cl_write(cl, cb, blocking);
  207. out:
  208. mei_me_cl_put(me_cl);
  209. mutex_unlock(&dev->device_lock);
  210. if (rets < 0)
  211. mei_io_cb_free(cb);
  212. return rets;
  213. }
  214. ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
  215. {
  216. struct mei_device *dev;
  217. struct mei_cl_cb *cb;
  218. size_t r_length;
  219. ssize_t rets;
  220. if (WARN_ON(!cl || !cl->dev))
  221. return -ENODEV;
  222. dev = cl->dev;
  223. mutex_lock(&dev->device_lock);
  224. cb = mei_cl_read_cb(cl, NULL);
  225. if (cb)
  226. goto copy;
  227. rets = mei_cl_read_start(cl, length, NULL);
  228. if (rets && rets != -EBUSY)
  229. goto out;
  230. if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
  231. mutex_unlock(&dev->device_lock);
  232. if (wait_event_interruptible(cl->rx_wait,
  233. (!list_empty(&cl->rd_completed)) ||
  234. (!mei_cl_is_connected(cl)))) {
  235. if (signal_pending(current))
  236. return -EINTR;
  237. return -ERESTARTSYS;
  238. }
  239. mutex_lock(&dev->device_lock);
  240. if (!mei_cl_is_connected(cl)) {
  241. rets = -EBUSY;
  242. goto out;
  243. }
  244. }
  245. cb = mei_cl_read_cb(cl, NULL);
  246. if (!cb) {
  247. rets = 0;
  248. goto out;
  249. }
  250. copy:
  251. if (cb->status) {
  252. rets = cb->status;
  253. goto free;
  254. }
  255. r_length = min_t(size_t, length, cb->buf_idx);
  256. memcpy(buf, cb->buf.data, r_length);
  257. rets = r_length;
  258. free:
  259. mei_io_cb_free(cb);
  260. out:
  261. mutex_unlock(&dev->device_lock);
  262. return rets;
  263. }
  264. inline ssize_t __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
  265. {
  266. return ___mei_cl_send(cl, buf, length, 0);
  267. }
  268. inline ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
  269. {
  270. return ___mei_cl_send(cl, buf, length, 1);
  271. }
  272. ssize_t mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
  273. {
  274. struct mei_cl *cl = device->cl;
  275. if (cl == NULL)
  276. return -ENODEV;
  277. if (device->ops && device->ops->send)
  278. return device->ops->send(device, buf, length);
  279. return __mei_cl_send(cl, buf, length);
  280. }
  281. EXPORT_SYMBOL_GPL(mei_cl_send);
  282. ssize_t mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
  283. {
  284. struct mei_cl *cl = device->cl;
  285. if (cl == NULL)
  286. return -ENODEV;
  287. if (device->ops && device->ops->recv)
  288. return device->ops->recv(device, buf, length);
  289. return __mei_cl_recv(cl, buf, length);
  290. }
  291. EXPORT_SYMBOL_GPL(mei_cl_recv);
  292. static void mei_bus_event_work(struct work_struct *work)
  293. {
  294. struct mei_cl_device *device;
  295. device = container_of(work, struct mei_cl_device, event_work);
  296. if (device->event_cb)
  297. device->event_cb(device, device->events, device->event_context);
  298. device->events = 0;
  299. /* Prepare for the next read */
  300. mei_cl_read_start(device->cl, 0, NULL);
  301. }
  302. int mei_cl_register_event_cb(struct mei_cl_device *device,
  303. mei_cl_event_cb_t event_cb, void *context)
  304. {
  305. if (device->event_cb)
  306. return -EALREADY;
  307. device->events = 0;
  308. device->event_cb = event_cb;
  309. device->event_context = context;
  310. INIT_WORK(&device->event_work, mei_bus_event_work);
  311. mei_cl_read_start(device->cl, 0, NULL);
  312. return 0;
  313. }
  314. EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
  315. void *mei_cl_get_drvdata(const struct mei_cl_device *device)
  316. {
  317. return dev_get_drvdata(&device->dev);
  318. }
  319. EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
  320. void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
  321. {
  322. dev_set_drvdata(&device->dev, data);
  323. }
  324. EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
  325. int mei_cl_enable_device(struct mei_cl_device *device)
  326. {
  327. int err;
  328. struct mei_device *dev;
  329. struct mei_cl *cl = device->cl;
  330. if (cl == NULL)
  331. return -ENODEV;
  332. dev = cl->dev;
  333. mutex_lock(&dev->device_lock);
  334. err = mei_cl_connect(cl, NULL);
  335. if (err < 0) {
  336. mutex_unlock(&dev->device_lock);
  337. dev_err(dev->dev, "Could not connect to the ME client");
  338. return err;
  339. }
  340. mutex_unlock(&dev->device_lock);
  341. if (device->event_cb)
  342. mei_cl_read_start(device->cl, 0, NULL);
  343. if (!device->ops || !device->ops->enable)
  344. return 0;
  345. return device->ops->enable(device);
  346. }
  347. EXPORT_SYMBOL_GPL(mei_cl_enable_device);
  348. int mei_cl_disable_device(struct mei_cl_device *device)
  349. {
  350. int err;
  351. struct mei_device *dev;
  352. struct mei_cl *cl = device->cl;
  353. if (cl == NULL)
  354. return -ENODEV;
  355. dev = cl->dev;
  356. if (device->ops && device->ops->disable)
  357. device->ops->disable(device);
  358. device->event_cb = NULL;
  359. mutex_lock(&dev->device_lock);
  360. if (!mei_cl_is_connected(cl)) {
  361. dev_err(dev->dev, "Already disconnected");
  362. err = 0;
  363. goto out;
  364. }
  365. cl->state = MEI_FILE_DISCONNECTING;
  366. err = mei_cl_disconnect(cl);
  367. if (err < 0) {
  368. dev_err(dev->dev, "Could not disconnect from the ME client");
  369. goto out;
  370. }
  371. /* Flush queues and remove any pending read */
  372. mei_cl_flush_queues(cl, NULL);
  373. out:
  374. mutex_unlock(&dev->device_lock);
  375. return err;
  376. }
  377. EXPORT_SYMBOL_GPL(mei_cl_disable_device);
  378. void mei_cl_bus_rx_event(struct mei_cl *cl)
  379. {
  380. struct mei_cl_device *device = cl->device;
  381. if (!device || !device->event_cb)
  382. return;
  383. set_bit(MEI_CL_EVENT_RX, &device->events);
  384. schedule_work(&device->event_work);
  385. }
  386. void mei_cl_bus_remove_devices(struct mei_device *dev)
  387. {
  388. struct mei_cl *cl, *next;
  389. mutex_lock(&dev->device_lock);
  390. list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
  391. if (cl->device)
  392. mei_cl_remove_device(cl->device);
  393. list_del(&cl->device_link);
  394. mei_cl_unlink(cl);
  395. kfree(cl);
  396. }
  397. mutex_unlock(&dev->device_lock);
  398. }
  399. int __init mei_cl_bus_init(void)
  400. {
  401. return bus_register(&mei_cl_bus_type);
  402. }
  403. void __exit mei_cl_bus_exit(void)
  404. {
  405. bus_unregister(&mei_cl_bus_type);
  406. }