bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 int ___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;
  180. struct mei_cl_cb *cb;
  181. int rets;
  182. if (WARN_ON(!cl || !cl->dev))
  183. return -ENODEV;
  184. dev = cl->dev;
  185. if (cl->state != MEI_FILE_CONNECTED)
  186. return -ENODEV;
  187. /* Check if we have an ME client device */
  188. me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
  189. if (!me_cl)
  190. return -ENOTTY;
  191. if (length > me_cl->props.max_msg_length)
  192. return -EFBIG;
  193. cb = mei_io_cb_init(cl, NULL);
  194. if (!cb)
  195. return -ENOMEM;
  196. rets = mei_io_cb_alloc_req_buf(cb, length);
  197. if (rets < 0) {
  198. mei_io_cb_free(cb);
  199. return rets;
  200. }
  201. memcpy(cb->request_buffer.data, buf, length);
  202. mutex_lock(&dev->device_lock);
  203. rets = mei_cl_write(cl, cb, blocking);
  204. mutex_unlock(&dev->device_lock);
  205. if (rets < 0)
  206. mei_io_cb_free(cb);
  207. return rets;
  208. }
  209. int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
  210. {
  211. struct mei_device *dev;
  212. struct mei_cl_cb *cb;
  213. size_t r_length;
  214. int err;
  215. if (WARN_ON(!cl || !cl->dev))
  216. return -ENODEV;
  217. dev = cl->dev;
  218. mutex_lock(&dev->device_lock);
  219. if (!cl->read_cb) {
  220. err = mei_cl_read_start(cl, length);
  221. if (err < 0) {
  222. mutex_unlock(&dev->device_lock);
  223. return err;
  224. }
  225. }
  226. if (cl->reading_state != MEI_READ_COMPLETE &&
  227. !waitqueue_active(&cl->rx_wait)) {
  228. mutex_unlock(&dev->device_lock);
  229. if (wait_event_interruptible(cl->rx_wait,
  230. cl->reading_state == MEI_READ_COMPLETE ||
  231. mei_cl_is_transitioning(cl))) {
  232. if (signal_pending(current))
  233. return -EINTR;
  234. return -ERESTARTSYS;
  235. }
  236. mutex_lock(&dev->device_lock);
  237. }
  238. cb = cl->read_cb;
  239. if (cl->reading_state != MEI_READ_COMPLETE) {
  240. r_length = 0;
  241. goto out;
  242. }
  243. r_length = min_t(size_t, length, cb->buf_idx);
  244. memcpy(buf, cb->response_buffer.data, r_length);
  245. mei_io_cb_free(cb);
  246. cl->reading_state = MEI_IDLE;
  247. cl->read_cb = NULL;
  248. out:
  249. mutex_unlock(&dev->device_lock);
  250. return r_length;
  251. }
  252. inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
  253. {
  254. return ___mei_cl_send(cl, buf, length, 0);
  255. }
  256. inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
  257. {
  258. return ___mei_cl_send(cl, buf, length, 1);
  259. }
  260. int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
  261. {
  262. struct mei_cl *cl = device->cl;
  263. if (cl == NULL)
  264. return -ENODEV;
  265. if (device->ops && device->ops->send)
  266. return device->ops->send(device, buf, length);
  267. return __mei_cl_send(cl, buf, length);
  268. }
  269. EXPORT_SYMBOL_GPL(mei_cl_send);
  270. int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
  271. {
  272. struct mei_cl *cl = device->cl;
  273. if (cl == NULL)
  274. return -ENODEV;
  275. if (device->ops && device->ops->recv)
  276. return device->ops->recv(device, buf, length);
  277. return __mei_cl_recv(cl, buf, length);
  278. }
  279. EXPORT_SYMBOL_GPL(mei_cl_recv);
  280. static void mei_bus_event_work(struct work_struct *work)
  281. {
  282. struct mei_cl_device *device;
  283. device = container_of(work, struct mei_cl_device, event_work);
  284. if (device->event_cb)
  285. device->event_cb(device, device->events, device->event_context);
  286. device->events = 0;
  287. /* Prepare for the next read */
  288. mei_cl_read_start(device->cl, 0);
  289. }
  290. int mei_cl_register_event_cb(struct mei_cl_device *device,
  291. mei_cl_event_cb_t event_cb, void *context)
  292. {
  293. if (device->event_cb)
  294. return -EALREADY;
  295. device->events = 0;
  296. device->event_cb = event_cb;
  297. device->event_context = context;
  298. INIT_WORK(&device->event_work, mei_bus_event_work);
  299. mei_cl_read_start(device->cl, 0);
  300. return 0;
  301. }
  302. EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
  303. void *mei_cl_get_drvdata(const struct mei_cl_device *device)
  304. {
  305. return dev_get_drvdata(&device->dev);
  306. }
  307. EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
  308. void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
  309. {
  310. dev_set_drvdata(&device->dev, data);
  311. }
  312. EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
  313. int mei_cl_enable_device(struct mei_cl_device *device)
  314. {
  315. int err;
  316. struct mei_device *dev;
  317. struct mei_cl *cl = device->cl;
  318. if (cl == NULL)
  319. return -ENODEV;
  320. dev = cl->dev;
  321. mutex_lock(&dev->device_lock);
  322. err = mei_cl_connect(cl, NULL);
  323. if (err < 0) {
  324. mutex_unlock(&dev->device_lock);
  325. dev_err(dev->dev, "Could not connect to the ME client");
  326. return err;
  327. }
  328. mutex_unlock(&dev->device_lock);
  329. if (device->event_cb && !cl->read_cb)
  330. mei_cl_read_start(device->cl, 0);
  331. if (!device->ops || !device->ops->enable)
  332. return 0;
  333. return device->ops->enable(device);
  334. }
  335. EXPORT_SYMBOL_GPL(mei_cl_enable_device);
  336. int mei_cl_disable_device(struct mei_cl_device *device)
  337. {
  338. int err;
  339. struct mei_device *dev;
  340. struct mei_cl *cl = device->cl;
  341. if (cl == NULL)
  342. return -ENODEV;
  343. dev = cl->dev;
  344. mutex_lock(&dev->device_lock);
  345. if (cl->state != MEI_FILE_CONNECTED) {
  346. mutex_unlock(&dev->device_lock);
  347. dev_err(dev->dev, "Already disconnected");
  348. return 0;
  349. }
  350. cl->state = MEI_FILE_DISCONNECTING;
  351. err = mei_cl_disconnect(cl);
  352. if (err < 0) {
  353. mutex_unlock(&dev->device_lock);
  354. dev_err(dev->dev,
  355. "Could not disconnect from the ME client");
  356. return err;
  357. }
  358. /* Flush queues and remove any pending read */
  359. mei_cl_flush_queues(cl);
  360. if (cl->read_cb) {
  361. struct mei_cl_cb *cb = NULL;
  362. cb = mei_cl_find_read_cb(cl);
  363. /* Remove entry from read list */
  364. if (cb)
  365. list_del(&cb->list);
  366. cb = cl->read_cb;
  367. cl->read_cb = NULL;
  368. if (cb) {
  369. mei_io_cb_free(cb);
  370. cb = NULL;
  371. }
  372. }
  373. device->event_cb = NULL;
  374. mutex_unlock(&dev->device_lock);
  375. if (!device->ops || !device->ops->disable)
  376. return 0;
  377. return device->ops->disable(device);
  378. }
  379. EXPORT_SYMBOL_GPL(mei_cl_disable_device);
  380. void mei_cl_bus_rx_event(struct mei_cl *cl)
  381. {
  382. struct mei_cl_device *device = cl->device;
  383. if (!device || !device->event_cb)
  384. return;
  385. set_bit(MEI_CL_EVENT_RX, &device->events);
  386. schedule_work(&device->event_work);
  387. }
  388. void mei_cl_bus_remove_devices(struct mei_device *dev)
  389. {
  390. struct mei_cl *cl, *next;
  391. mutex_lock(&dev->device_lock);
  392. list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
  393. if (cl->device)
  394. mei_cl_remove_device(cl->device);
  395. list_del(&cl->device_link);
  396. mei_cl_unlink(cl);
  397. kfree(cl);
  398. }
  399. mutex_unlock(&dev->device_lock);
  400. }
  401. int __init mei_cl_bus_init(void)
  402. {
  403. return bus_register(&mei_cl_bus_type);
  404. }
  405. void __exit mei_cl_bus_exit(void)
  406. {
  407. bus_unregister(&mei_cl_bus_type);
  408. }