bus.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. /**
  30. * __mei_cl_send - internal client send (write)
  31. *
  32. * @cl: host client
  33. * @buf: buffer to send
  34. * @length: buffer length
  35. * @blocking: wait for write completion
  36. *
  37. * Return: written size bytes or < 0 on error
  38. */
  39. ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
  40. bool blocking)
  41. {
  42. struct mei_device *bus;
  43. struct mei_cl_cb *cb;
  44. ssize_t rets;
  45. if (WARN_ON(!cl || !cl->dev))
  46. return -ENODEV;
  47. bus = cl->dev;
  48. mutex_lock(&bus->device_lock);
  49. if (bus->dev_state != MEI_DEV_ENABLED) {
  50. rets = -ENODEV;
  51. goto out;
  52. }
  53. if (!mei_cl_is_connected(cl)) {
  54. rets = -ENODEV;
  55. goto out;
  56. }
  57. /* Check if we have an ME client device */
  58. if (!mei_me_cl_is_active(cl->me_cl)) {
  59. rets = -ENOTTY;
  60. goto out;
  61. }
  62. if (length > mei_cl_mtu(cl)) {
  63. rets = -EFBIG;
  64. goto out;
  65. }
  66. cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
  67. if (!cb) {
  68. rets = -ENOMEM;
  69. goto out;
  70. }
  71. memcpy(cb->buf.data, buf, length);
  72. rets = mei_cl_write(cl, cb, blocking);
  73. out:
  74. mutex_unlock(&bus->device_lock);
  75. return rets;
  76. }
  77. /**
  78. * __mei_cl_recv - internal client receive (read)
  79. *
  80. * @cl: host client
  81. * @buf: buffer to receive
  82. * @length: buffer length
  83. *
  84. * Return: read size in bytes of < 0 on error
  85. */
  86. ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
  87. {
  88. struct mei_device *bus;
  89. struct mei_cl_cb *cb;
  90. size_t r_length;
  91. ssize_t rets;
  92. if (WARN_ON(!cl || !cl->dev))
  93. return -ENODEV;
  94. bus = cl->dev;
  95. mutex_lock(&bus->device_lock);
  96. if (bus->dev_state != MEI_DEV_ENABLED) {
  97. rets = -ENODEV;
  98. goto out;
  99. }
  100. cb = mei_cl_read_cb(cl, NULL);
  101. if (cb)
  102. goto copy;
  103. rets = mei_cl_read_start(cl, length, NULL);
  104. if (rets && rets != -EBUSY)
  105. goto out;
  106. /* wait on event only if there is no other waiter */
  107. if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
  108. mutex_unlock(&bus->device_lock);
  109. if (wait_event_interruptible(cl->rx_wait,
  110. (!list_empty(&cl->rd_completed)) ||
  111. (!mei_cl_is_connected(cl)))) {
  112. if (signal_pending(current))
  113. return -EINTR;
  114. return -ERESTARTSYS;
  115. }
  116. mutex_lock(&bus->device_lock);
  117. if (!mei_cl_is_connected(cl)) {
  118. rets = -EBUSY;
  119. goto out;
  120. }
  121. }
  122. cb = mei_cl_read_cb(cl, NULL);
  123. if (!cb) {
  124. rets = 0;
  125. goto out;
  126. }
  127. copy:
  128. if (cb->status) {
  129. rets = cb->status;
  130. goto free;
  131. }
  132. r_length = min_t(size_t, length, cb->buf_idx);
  133. memcpy(buf, cb->buf.data, r_length);
  134. rets = r_length;
  135. free:
  136. mei_io_cb_free(cb);
  137. out:
  138. mutex_unlock(&bus->device_lock);
  139. return rets;
  140. }
  141. /**
  142. * mei_cldev_send - me device send (write)
  143. *
  144. * @cldev: me client device
  145. * @buf: buffer to send
  146. * @length: buffer length
  147. *
  148. * Return: written size in bytes or < 0 on error
  149. */
  150. ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
  151. {
  152. struct mei_cl *cl = cldev->cl;
  153. if (cl == NULL)
  154. return -ENODEV;
  155. return __mei_cl_send(cl, buf, length, 1);
  156. }
  157. EXPORT_SYMBOL_GPL(mei_cldev_send);
  158. /**
  159. * mei_cldev_recv - client receive (read)
  160. *
  161. * @cldev: me client device
  162. * @buf: buffer to receive
  163. * @length: buffer length
  164. *
  165. * Return: read size in bytes of < 0 on error
  166. */
  167. ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
  168. {
  169. struct mei_cl *cl = cldev->cl;
  170. if (cl == NULL)
  171. return -ENODEV;
  172. return __mei_cl_recv(cl, buf, length);
  173. }
  174. EXPORT_SYMBOL_GPL(mei_cldev_recv);
  175. /**
  176. * mei_cl_bus_event_work - dispatch rx event for a bus device
  177. * and schedule new work
  178. *
  179. * @work: work
  180. */
  181. static void mei_cl_bus_event_work(struct work_struct *work)
  182. {
  183. struct mei_cl_device *cldev;
  184. cldev = container_of(work, struct mei_cl_device, event_work);
  185. if (cldev->event_cb)
  186. cldev->event_cb(cldev, cldev->events, cldev->event_context);
  187. cldev->events = 0;
  188. /* Prepare for the next read */
  189. if (cldev->events_mask & BIT(MEI_CL_EVENT_RX))
  190. mei_cl_read_start(cldev->cl, 0, NULL);
  191. }
  192. /**
  193. * mei_cl_bus_notify_event - schedule notify cb on bus client
  194. *
  195. * @cl: host client
  196. *
  197. * Return: true if event was scheduled
  198. * false if the client is not waiting for event
  199. */
  200. bool mei_cl_bus_notify_event(struct mei_cl *cl)
  201. {
  202. struct mei_cl_device *cldev = cl->cldev;
  203. if (!cldev || !cldev->event_cb)
  204. return false;
  205. if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)))
  206. return false;
  207. if (!cl->notify_ev)
  208. return false;
  209. set_bit(MEI_CL_EVENT_NOTIF, &cldev->events);
  210. schedule_work(&cldev->event_work);
  211. cl->notify_ev = false;
  212. return true;
  213. }
  214. /**
  215. * mei_cl_bus_rx_event - schedule rx event
  216. *
  217. * @cl: host client
  218. *
  219. * Return: true if event was scheduled
  220. * false if the client is not waiting for event
  221. */
  222. bool mei_cl_bus_rx_event(struct mei_cl *cl)
  223. {
  224. struct mei_cl_device *cldev = cl->cldev;
  225. if (!cldev || !cldev->event_cb)
  226. return false;
  227. if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX)))
  228. return false;
  229. set_bit(MEI_CL_EVENT_RX, &cldev->events);
  230. schedule_work(&cldev->event_work);
  231. return true;
  232. }
  233. /**
  234. * mei_cldev_register_event_cb - register event callback
  235. *
  236. * @cldev: me client devices
  237. * @event_cb: callback function
  238. * @events_mask: requested events bitmask
  239. * @context: driver context data
  240. *
  241. * Return: 0 on success
  242. * -EALREADY if an callback is already registered
  243. * <0 on other errors
  244. */
  245. int mei_cldev_register_event_cb(struct mei_cl_device *cldev,
  246. unsigned long events_mask,
  247. mei_cldev_event_cb_t event_cb, void *context)
  248. {
  249. int ret;
  250. if (cldev->event_cb)
  251. return -EALREADY;
  252. cldev->events = 0;
  253. cldev->events_mask = events_mask;
  254. cldev->event_cb = event_cb;
  255. cldev->event_context = context;
  256. INIT_WORK(&cldev->event_work, mei_cl_bus_event_work);
  257. if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
  258. ret = mei_cl_read_start(cldev->cl, 0, NULL);
  259. if (ret && ret != -EBUSY)
  260. return ret;
  261. }
  262. if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) {
  263. mutex_lock(&cldev->cl->dev->device_lock);
  264. ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0);
  265. mutex_unlock(&cldev->cl->dev->device_lock);
  266. if (ret)
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb);
  272. /**
  273. * mei_cldev_get_drvdata - driver data getter
  274. *
  275. * @cldev: mei client device
  276. *
  277. * Return: driver private data
  278. */
  279. void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
  280. {
  281. return dev_get_drvdata(&cldev->dev);
  282. }
  283. EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
  284. /**
  285. * mei_cldev_set_drvdata - driver data setter
  286. *
  287. * @cldev: mei client device
  288. * @data: data to store
  289. */
  290. void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
  291. {
  292. dev_set_drvdata(&cldev->dev, data);
  293. }
  294. EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
  295. /**
  296. * mei_cldev_uuid - return uuid of the underlying me client
  297. *
  298. * @cldev: mei client device
  299. *
  300. * Return: me client uuid
  301. */
  302. const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
  303. {
  304. return mei_me_cl_uuid(cldev->me_cl);
  305. }
  306. EXPORT_SYMBOL_GPL(mei_cldev_uuid);
  307. /**
  308. * mei_cldev_ver - return protocol version of the underlying me client
  309. *
  310. * @cldev: mei client device
  311. *
  312. * Return: me client protocol version
  313. */
  314. u8 mei_cldev_ver(const struct mei_cl_device *cldev)
  315. {
  316. return mei_me_cl_ver(cldev->me_cl);
  317. }
  318. EXPORT_SYMBOL_GPL(mei_cldev_ver);
  319. /**
  320. * mei_cldev_enabled - check whether the device is enabled
  321. *
  322. * @cldev: mei client device
  323. *
  324. * Return: true if me client is initialized and connected
  325. */
  326. bool mei_cldev_enabled(struct mei_cl_device *cldev)
  327. {
  328. return cldev->cl && mei_cl_is_connected(cldev->cl);
  329. }
  330. EXPORT_SYMBOL_GPL(mei_cldev_enabled);
  331. /**
  332. * mei_cldev_enable_device - enable me client device
  333. * create connection with me client
  334. *
  335. * @cldev: me client device
  336. *
  337. * Return: 0 on success and < 0 on error
  338. */
  339. int mei_cldev_enable(struct mei_cl_device *cldev)
  340. {
  341. struct mei_device *bus = cldev->bus;
  342. struct mei_cl *cl;
  343. int ret;
  344. cl = cldev->cl;
  345. if (!cl) {
  346. mutex_lock(&bus->device_lock);
  347. cl = mei_cl_alloc_linked(bus);
  348. mutex_unlock(&bus->device_lock);
  349. if (IS_ERR(cl))
  350. return PTR_ERR(cl);
  351. /* update pointers */
  352. cldev->cl = cl;
  353. cl->cldev = cldev;
  354. }
  355. mutex_lock(&bus->device_lock);
  356. if (mei_cl_is_connected(cl)) {
  357. ret = 0;
  358. goto out;
  359. }
  360. if (!mei_me_cl_is_active(cldev->me_cl)) {
  361. dev_err(&cldev->dev, "me client is not active\n");
  362. ret = -ENOTTY;
  363. goto out;
  364. }
  365. ret = mei_cl_connect(cl, cldev->me_cl, NULL);
  366. if (ret < 0)
  367. dev_err(&cldev->dev, "cannot connect\n");
  368. out:
  369. mutex_unlock(&bus->device_lock);
  370. return ret;
  371. }
  372. EXPORT_SYMBOL_GPL(mei_cldev_enable);
  373. /**
  374. * mei_cldev_disable - disable me client device
  375. * disconnect form the me client
  376. *
  377. * @cldev: me client device
  378. *
  379. * Return: 0 on success and < 0 on error
  380. */
  381. int mei_cldev_disable(struct mei_cl_device *cldev)
  382. {
  383. struct mei_device *bus;
  384. struct mei_cl *cl;
  385. int err;
  386. if (!cldev || !cldev->cl)
  387. return -ENODEV;
  388. cl = cldev->cl;
  389. bus = cldev->bus;
  390. cldev->event_cb = NULL;
  391. mutex_lock(&bus->device_lock);
  392. if (!mei_cl_is_connected(cl)) {
  393. dev_err(bus->dev, "Already disconnected");
  394. err = 0;
  395. goto out;
  396. }
  397. err = mei_cl_disconnect(cl);
  398. if (err < 0)
  399. dev_err(bus->dev, "Could not disconnect from the ME client");
  400. out:
  401. /* Flush queues and remove any pending read */
  402. mei_cl_flush_queues(cl, NULL);
  403. mei_cl_unlink(cl);
  404. kfree(cl);
  405. cldev->cl = NULL;
  406. mutex_unlock(&bus->device_lock);
  407. return err;
  408. }
  409. EXPORT_SYMBOL_GPL(mei_cldev_disable);
  410. /**
  411. * mei_cl_device_find - find matching entry in the driver id table
  412. *
  413. * @cldev: me client device
  414. * @cldrv: me client driver
  415. *
  416. * Return: id on success; NULL if no id is matching
  417. */
  418. static const
  419. struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
  420. struct mei_cl_driver *cldrv)
  421. {
  422. const struct mei_cl_device_id *id;
  423. const uuid_le *uuid;
  424. u8 version;
  425. bool match;
  426. uuid = mei_me_cl_uuid(cldev->me_cl);
  427. version = mei_me_cl_ver(cldev->me_cl);
  428. id = cldrv->id_table;
  429. while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
  430. if (!uuid_le_cmp(*uuid, id->uuid)) {
  431. match = true;
  432. if (cldev->name[0])
  433. if (strncmp(cldev->name, id->name,
  434. sizeof(id->name)))
  435. match = false;
  436. if (id->version != MEI_CL_VERSION_ANY)
  437. if (id->version != version)
  438. match = false;
  439. if (match)
  440. return id;
  441. }
  442. id++;
  443. }
  444. return NULL;
  445. }
  446. /**
  447. * mei_cl_device_match - device match function
  448. *
  449. * @dev: device
  450. * @drv: driver
  451. *
  452. * Return: 1 if matching device was found 0 otherwise
  453. */
  454. static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
  455. {
  456. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  457. struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
  458. const struct mei_cl_device_id *found_id;
  459. if (!cldev)
  460. return 0;
  461. if (!cldev->do_match)
  462. return 0;
  463. if (!cldrv || !cldrv->id_table)
  464. return 0;
  465. found_id = mei_cl_device_find(cldev, cldrv);
  466. if (found_id)
  467. return 1;
  468. return 0;
  469. }
  470. /**
  471. * mei_cl_device_probe - bus probe function
  472. *
  473. * @dev: device
  474. *
  475. * Return: 0 on success; < 0 otherwise
  476. */
  477. static int mei_cl_device_probe(struct device *dev)
  478. {
  479. struct mei_cl_device *cldev;
  480. struct mei_cl_driver *cldrv;
  481. const struct mei_cl_device_id *id;
  482. cldev = to_mei_cl_device(dev);
  483. cldrv = to_mei_cl_driver(dev->driver);
  484. if (!cldev)
  485. return 0;
  486. if (!cldrv || !cldrv->probe)
  487. return -ENODEV;
  488. id = mei_cl_device_find(cldev, cldrv);
  489. if (!id)
  490. return -ENODEV;
  491. __module_get(THIS_MODULE);
  492. return cldrv->probe(cldev, id);
  493. }
  494. /**
  495. * mei_cl_device_remove - remove device from the bus
  496. *
  497. * @dev: device
  498. *
  499. * Return: 0 on success; < 0 otherwise
  500. */
  501. static int mei_cl_device_remove(struct device *dev)
  502. {
  503. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  504. struct mei_cl_driver *cldrv;
  505. int ret = 0;
  506. if (!cldev || !dev->driver)
  507. return 0;
  508. if (cldev->event_cb) {
  509. cldev->event_cb = NULL;
  510. cancel_work_sync(&cldev->event_work);
  511. }
  512. cldrv = to_mei_cl_driver(dev->driver);
  513. if (cldrv->remove)
  514. ret = cldrv->remove(cldev);
  515. module_put(THIS_MODULE);
  516. dev->driver = NULL;
  517. return ret;
  518. }
  519. static ssize_t name_show(struct device *dev, struct device_attribute *a,
  520. char *buf)
  521. {
  522. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  523. size_t len;
  524. len = snprintf(buf, PAGE_SIZE, "%s", cldev->name);
  525. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  526. }
  527. static DEVICE_ATTR_RO(name);
  528. static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
  529. char *buf)
  530. {
  531. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  532. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  533. size_t len;
  534. len = snprintf(buf, PAGE_SIZE, "%pUl", uuid);
  535. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  536. }
  537. static DEVICE_ATTR_RO(uuid);
  538. static ssize_t version_show(struct device *dev, struct device_attribute *a,
  539. char *buf)
  540. {
  541. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  542. u8 version = mei_me_cl_ver(cldev->me_cl);
  543. size_t len;
  544. len = snprintf(buf, PAGE_SIZE, "%02X", version);
  545. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  546. }
  547. static DEVICE_ATTR_RO(version);
  548. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  549. char *buf)
  550. {
  551. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  552. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  553. size_t len;
  554. len = snprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
  555. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  556. }
  557. static DEVICE_ATTR_RO(modalias);
  558. static struct attribute *mei_cldev_attrs[] = {
  559. &dev_attr_name.attr,
  560. &dev_attr_uuid.attr,
  561. &dev_attr_version.attr,
  562. &dev_attr_modalias.attr,
  563. NULL,
  564. };
  565. ATTRIBUTE_GROUPS(mei_cldev);
  566. /**
  567. * mei_cl_device_uevent - me client bus uevent handler
  568. *
  569. * @dev: device
  570. * @env: uevent kobject
  571. *
  572. * Return: 0 on success -ENOMEM on when add_uevent_var fails
  573. */
  574. static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  575. {
  576. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  577. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  578. u8 version = mei_me_cl_ver(cldev->me_cl);
  579. if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
  580. return -ENOMEM;
  581. if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
  582. return -ENOMEM;
  583. if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
  584. return -ENOMEM;
  585. if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
  586. cldev->name, uuid, version))
  587. return -ENOMEM;
  588. return 0;
  589. }
  590. static struct bus_type mei_cl_bus_type = {
  591. .name = "mei",
  592. .dev_groups = mei_cldev_groups,
  593. .match = mei_cl_device_match,
  594. .probe = mei_cl_device_probe,
  595. .remove = mei_cl_device_remove,
  596. .uevent = mei_cl_device_uevent,
  597. };
  598. static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
  599. {
  600. if (bus)
  601. get_device(bus->dev);
  602. return bus;
  603. }
  604. static void mei_dev_bus_put(struct mei_device *bus)
  605. {
  606. if (bus)
  607. put_device(bus->dev);
  608. }
  609. static void mei_cl_bus_dev_release(struct device *dev)
  610. {
  611. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  612. if (!cldev)
  613. return;
  614. mei_me_cl_put(cldev->me_cl);
  615. mei_dev_bus_put(cldev->bus);
  616. kfree(cldev);
  617. }
  618. static struct device_type mei_cl_device_type = {
  619. .release = mei_cl_bus_dev_release,
  620. };
  621. /**
  622. * mei_cl_bus_set_name - set device name for me client device
  623. *
  624. * @cldev: me client device
  625. */
  626. static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
  627. {
  628. dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
  629. cldev->name,
  630. mei_me_cl_uuid(cldev->me_cl),
  631. mei_me_cl_ver(cldev->me_cl));
  632. }
  633. /**
  634. * mei_cl_bus_dev_alloc - initialize and allocate mei client device
  635. *
  636. * @bus: mei device
  637. * @me_cl: me client
  638. *
  639. * Return: allocated device structur or NULL on allocation failure
  640. */
  641. static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
  642. struct mei_me_client *me_cl)
  643. {
  644. struct mei_cl_device *cldev;
  645. cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
  646. if (!cldev)
  647. return NULL;
  648. device_initialize(&cldev->dev);
  649. cldev->dev.parent = bus->dev;
  650. cldev->dev.bus = &mei_cl_bus_type;
  651. cldev->dev.type = &mei_cl_device_type;
  652. cldev->bus = mei_dev_bus_get(bus);
  653. cldev->me_cl = mei_me_cl_get(me_cl);
  654. mei_cl_bus_set_name(cldev);
  655. cldev->is_added = 0;
  656. INIT_LIST_HEAD(&cldev->bus_list);
  657. return cldev;
  658. }
  659. /**
  660. * mei_cl_dev_setup - setup me client device
  661. * run fix up routines and set the device name
  662. *
  663. * @bus: mei device
  664. * @cldev: me client device
  665. *
  666. * Return: true if the device is eligible for enumeration
  667. */
  668. static bool mei_cl_bus_dev_setup(struct mei_device *bus,
  669. struct mei_cl_device *cldev)
  670. {
  671. cldev->do_match = 1;
  672. mei_cl_bus_dev_fixup(cldev);
  673. /* the device name can change during fix up */
  674. if (cldev->do_match)
  675. mei_cl_bus_set_name(cldev);
  676. return cldev->do_match == 1;
  677. }
  678. /**
  679. * mei_cl_bus_dev_add - add me client devices
  680. *
  681. * @cldev: me client device
  682. *
  683. * Return: 0 on success; < 0 on failre
  684. */
  685. static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
  686. {
  687. int ret;
  688. dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
  689. mei_me_cl_uuid(cldev->me_cl),
  690. mei_me_cl_ver(cldev->me_cl));
  691. ret = device_add(&cldev->dev);
  692. if (!ret)
  693. cldev->is_added = 1;
  694. return ret;
  695. }
  696. /**
  697. * mei_cl_bus_dev_stop - stop the driver
  698. *
  699. * @cldev: me client device
  700. */
  701. static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
  702. {
  703. if (cldev->is_added)
  704. device_release_driver(&cldev->dev);
  705. }
  706. /**
  707. * mei_cl_bus_dev_destroy - destroy me client devices object
  708. *
  709. * @cldev: me client device
  710. *
  711. * Locking: called under "dev->cl_bus_lock" lock
  712. */
  713. static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
  714. {
  715. WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
  716. if (!cldev->is_added)
  717. return;
  718. device_del(&cldev->dev);
  719. list_del_init(&cldev->bus_list);
  720. cldev->is_added = 0;
  721. put_device(&cldev->dev);
  722. }
  723. /**
  724. * mei_cl_bus_remove_device - remove a devices form the bus
  725. *
  726. * @cldev: me client device
  727. */
  728. static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
  729. {
  730. mei_cl_bus_dev_stop(cldev);
  731. mei_cl_bus_dev_destroy(cldev);
  732. }
  733. /**
  734. * mei_cl_bus_remove_devices - remove all devices form the bus
  735. *
  736. * @bus: mei device
  737. */
  738. void mei_cl_bus_remove_devices(struct mei_device *bus)
  739. {
  740. struct mei_cl_device *cldev, *next;
  741. mutex_lock(&bus->cl_bus_lock);
  742. list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
  743. mei_cl_bus_remove_device(cldev);
  744. mutex_unlock(&bus->cl_bus_lock);
  745. }
  746. /**
  747. * mei_cl_bus_dev_init - allocate and initializes an mei client devices
  748. * based on me client
  749. *
  750. * @bus: mei device
  751. * @me_cl: me client
  752. *
  753. * Locking: called under "dev->cl_bus_lock" lock
  754. */
  755. static void mei_cl_bus_dev_init(struct mei_device *bus,
  756. struct mei_me_client *me_cl)
  757. {
  758. struct mei_cl_device *cldev;
  759. WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
  760. dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
  761. if (me_cl->bus_added)
  762. return;
  763. cldev = mei_cl_bus_dev_alloc(bus, me_cl);
  764. if (!cldev)
  765. return;
  766. me_cl->bus_added = true;
  767. list_add_tail(&cldev->bus_list, &bus->device_list);
  768. }
  769. /**
  770. * mei_cl_bus_rescan - scan me clients list and add create
  771. * devices for eligible clients
  772. *
  773. * @bus: mei device
  774. */
  775. void mei_cl_bus_rescan(struct mei_device *bus)
  776. {
  777. struct mei_cl_device *cldev, *n;
  778. struct mei_me_client *me_cl;
  779. mutex_lock(&bus->cl_bus_lock);
  780. down_read(&bus->me_clients_rwsem);
  781. list_for_each_entry(me_cl, &bus->me_clients, list)
  782. mei_cl_bus_dev_init(bus, me_cl);
  783. up_read(&bus->me_clients_rwsem);
  784. list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
  785. if (!mei_me_cl_is_active(cldev->me_cl)) {
  786. mei_cl_bus_remove_device(cldev);
  787. continue;
  788. }
  789. if (cldev->is_added)
  790. continue;
  791. if (mei_cl_bus_dev_setup(bus, cldev))
  792. mei_cl_bus_dev_add(cldev);
  793. else {
  794. list_del_init(&cldev->bus_list);
  795. put_device(&cldev->dev);
  796. }
  797. }
  798. mutex_unlock(&bus->cl_bus_lock);
  799. dev_dbg(bus->dev, "rescan end");
  800. }
  801. void mei_cl_bus_rescan_work(struct work_struct *work)
  802. {
  803. struct mei_device *bus =
  804. container_of(work, struct mei_device, bus_rescan_work);
  805. struct mei_me_client *me_cl;
  806. mutex_lock(&bus->device_lock);
  807. me_cl = mei_me_cl_by_uuid(bus, &mei_amthif_guid);
  808. if (me_cl)
  809. mei_amthif_host_init(bus, me_cl);
  810. mei_me_cl_put(me_cl);
  811. mutex_unlock(&bus->device_lock);
  812. mei_cl_bus_rescan(bus);
  813. }
  814. int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
  815. struct module *owner)
  816. {
  817. int err;
  818. cldrv->driver.name = cldrv->name;
  819. cldrv->driver.owner = owner;
  820. cldrv->driver.bus = &mei_cl_bus_type;
  821. err = driver_register(&cldrv->driver);
  822. if (err)
  823. return err;
  824. pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
  825. return 0;
  826. }
  827. EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
  828. void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
  829. {
  830. driver_unregister(&cldrv->driver);
  831. pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
  832. }
  833. EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
  834. int __init mei_cl_bus_init(void)
  835. {
  836. return bus_register(&mei_cl_bus_type);
  837. }
  838. void __exit mei_cl_bus_exit(void)
  839. {
  840. bus_unregister(&mei_cl_bus_type);
  841. }