bus.c 21 KB

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