bus.c 18 KB

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