bus.c 21 KB

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