bus.c 12 KB

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