bus.c 22 KB

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