bus.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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/signal.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_unregister_callbacks - internal wrapper for unregistering
  421. * callbacks.
  422. *
  423. * @cldev: client device
  424. */
  425. static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
  426. {
  427. if (cldev->rx_cb) {
  428. cancel_work_sync(&cldev->rx_work);
  429. cldev->rx_cb = NULL;
  430. }
  431. if (cldev->notif_cb) {
  432. cancel_work_sync(&cldev->notif_work);
  433. cldev->notif_cb = NULL;
  434. }
  435. }
  436. /**
  437. * mei_cldev_disable - disable me client device
  438. * disconnect form the me client
  439. *
  440. * @cldev: me client device
  441. *
  442. * Return: 0 on success and < 0 on error
  443. */
  444. int mei_cldev_disable(struct mei_cl_device *cldev)
  445. {
  446. struct mei_device *bus;
  447. struct mei_cl *cl;
  448. int err;
  449. if (!cldev)
  450. return -ENODEV;
  451. cl = cldev->cl;
  452. bus = cldev->bus;
  453. mei_cldev_unregister_callbacks(cldev);
  454. mutex_lock(&bus->device_lock);
  455. if (!mei_cl_is_connected(cl)) {
  456. dev_dbg(bus->dev, "Already disconnected");
  457. err = 0;
  458. goto out;
  459. }
  460. err = mei_cl_disconnect(cl);
  461. if (err < 0)
  462. dev_err(bus->dev, "Could not disconnect from the ME client");
  463. out:
  464. /* Flush queues and remove any pending read */
  465. mei_cl_flush_queues(cl, NULL);
  466. mei_cl_unlink(cl);
  467. mutex_unlock(&bus->device_lock);
  468. return err;
  469. }
  470. EXPORT_SYMBOL_GPL(mei_cldev_disable);
  471. /**
  472. * mei_cl_bus_module_get - acquire module of the underlying
  473. * hw module.
  474. *
  475. * @cl: host client
  476. *
  477. * Return: true on success; false if the module was removed.
  478. */
  479. bool mei_cl_bus_module_get(struct mei_cl *cl)
  480. {
  481. struct mei_cl_device *cldev = cl->cldev;
  482. if (!cldev)
  483. return true;
  484. return try_module_get(cldev->bus->dev->driver->owner);
  485. }
  486. /**
  487. * mei_cl_bus_module_put - release the underlying hw module.
  488. *
  489. * @cl: host client
  490. */
  491. void mei_cl_bus_module_put(struct mei_cl *cl)
  492. {
  493. struct mei_cl_device *cldev = cl->cldev;
  494. if (cldev)
  495. module_put(cldev->bus->dev->driver->owner);
  496. }
  497. /**
  498. * mei_cl_device_find - find matching entry in the driver id table
  499. *
  500. * @cldev: me client device
  501. * @cldrv: me client driver
  502. *
  503. * Return: id on success; NULL if no id is matching
  504. */
  505. static const
  506. struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
  507. struct mei_cl_driver *cldrv)
  508. {
  509. const struct mei_cl_device_id *id;
  510. const uuid_le *uuid;
  511. u8 version;
  512. bool match;
  513. uuid = mei_me_cl_uuid(cldev->me_cl);
  514. version = mei_me_cl_ver(cldev->me_cl);
  515. id = cldrv->id_table;
  516. while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
  517. if (!uuid_le_cmp(*uuid, id->uuid)) {
  518. match = true;
  519. if (cldev->name[0])
  520. if (strncmp(cldev->name, id->name,
  521. sizeof(id->name)))
  522. match = false;
  523. if (id->version != MEI_CL_VERSION_ANY)
  524. if (id->version != version)
  525. match = false;
  526. if (match)
  527. return id;
  528. }
  529. id++;
  530. }
  531. return NULL;
  532. }
  533. /**
  534. * mei_cl_device_match - device match function
  535. *
  536. * @dev: device
  537. * @drv: driver
  538. *
  539. * Return: 1 if matching device was found 0 otherwise
  540. */
  541. static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
  542. {
  543. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  544. struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
  545. const struct mei_cl_device_id *found_id;
  546. if (!cldev)
  547. return 0;
  548. if (!cldev->do_match)
  549. return 0;
  550. if (!cldrv || !cldrv->id_table)
  551. return 0;
  552. found_id = mei_cl_device_find(cldev, cldrv);
  553. if (found_id)
  554. return 1;
  555. return 0;
  556. }
  557. /**
  558. * mei_cl_device_probe - bus probe function
  559. *
  560. * @dev: device
  561. *
  562. * Return: 0 on success; < 0 otherwise
  563. */
  564. static int mei_cl_device_probe(struct device *dev)
  565. {
  566. struct mei_cl_device *cldev;
  567. struct mei_cl_driver *cldrv;
  568. const struct mei_cl_device_id *id;
  569. int ret;
  570. cldev = to_mei_cl_device(dev);
  571. cldrv = to_mei_cl_driver(dev->driver);
  572. if (!cldev)
  573. return 0;
  574. if (!cldrv || !cldrv->probe)
  575. return -ENODEV;
  576. id = mei_cl_device_find(cldev, cldrv);
  577. if (!id)
  578. return -ENODEV;
  579. ret = cldrv->probe(cldev, id);
  580. if (ret)
  581. return ret;
  582. __module_get(THIS_MODULE);
  583. return 0;
  584. }
  585. /**
  586. * mei_cl_device_remove - remove device from the bus
  587. *
  588. * @dev: device
  589. *
  590. * Return: 0 on success; < 0 otherwise
  591. */
  592. static int mei_cl_device_remove(struct device *dev)
  593. {
  594. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  595. struct mei_cl_driver *cldrv;
  596. int ret = 0;
  597. if (!cldev || !dev->driver)
  598. return 0;
  599. cldrv = to_mei_cl_driver(dev->driver);
  600. if (cldrv->remove)
  601. ret = cldrv->remove(cldev);
  602. mei_cldev_unregister_callbacks(cldev);
  603. module_put(THIS_MODULE);
  604. dev->driver = NULL;
  605. return ret;
  606. }
  607. static ssize_t name_show(struct device *dev, struct device_attribute *a,
  608. char *buf)
  609. {
  610. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  611. return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
  612. }
  613. static DEVICE_ATTR_RO(name);
  614. static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
  615. char *buf)
  616. {
  617. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  618. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  619. return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
  620. }
  621. static DEVICE_ATTR_RO(uuid);
  622. static ssize_t version_show(struct device *dev, struct device_attribute *a,
  623. char *buf)
  624. {
  625. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  626. u8 version = mei_me_cl_ver(cldev->me_cl);
  627. return scnprintf(buf, PAGE_SIZE, "%02X", version);
  628. }
  629. static DEVICE_ATTR_RO(version);
  630. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  631. char *buf)
  632. {
  633. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  634. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  635. return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
  636. }
  637. static DEVICE_ATTR_RO(modalias);
  638. static struct attribute *mei_cldev_attrs[] = {
  639. &dev_attr_name.attr,
  640. &dev_attr_uuid.attr,
  641. &dev_attr_version.attr,
  642. &dev_attr_modalias.attr,
  643. NULL,
  644. };
  645. ATTRIBUTE_GROUPS(mei_cldev);
  646. /**
  647. * mei_cl_device_uevent - me client bus uevent handler
  648. *
  649. * @dev: device
  650. * @env: uevent kobject
  651. *
  652. * Return: 0 on success -ENOMEM on when add_uevent_var fails
  653. */
  654. static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  655. {
  656. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  657. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  658. u8 version = mei_me_cl_ver(cldev->me_cl);
  659. if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
  660. return -ENOMEM;
  661. if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
  662. return -ENOMEM;
  663. if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
  664. return -ENOMEM;
  665. if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
  666. cldev->name, uuid, version))
  667. return -ENOMEM;
  668. return 0;
  669. }
  670. static struct bus_type mei_cl_bus_type = {
  671. .name = "mei",
  672. .dev_groups = mei_cldev_groups,
  673. .match = mei_cl_device_match,
  674. .probe = mei_cl_device_probe,
  675. .remove = mei_cl_device_remove,
  676. .uevent = mei_cl_device_uevent,
  677. };
  678. static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
  679. {
  680. if (bus)
  681. get_device(bus->dev);
  682. return bus;
  683. }
  684. static void mei_dev_bus_put(struct mei_device *bus)
  685. {
  686. if (bus)
  687. put_device(bus->dev);
  688. }
  689. static void mei_cl_bus_dev_release(struct device *dev)
  690. {
  691. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  692. if (!cldev)
  693. return;
  694. mei_me_cl_put(cldev->me_cl);
  695. mei_dev_bus_put(cldev->bus);
  696. kfree(cldev->cl);
  697. kfree(cldev);
  698. }
  699. static struct device_type mei_cl_device_type = {
  700. .release = mei_cl_bus_dev_release,
  701. };
  702. /**
  703. * mei_cl_bus_set_name - set device name for me client device
  704. *
  705. * @cldev: me client device
  706. */
  707. static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
  708. {
  709. dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
  710. cldev->name,
  711. mei_me_cl_uuid(cldev->me_cl),
  712. mei_me_cl_ver(cldev->me_cl));
  713. }
  714. /**
  715. * mei_cl_bus_dev_alloc - initialize and allocate mei client device
  716. *
  717. * @bus: mei device
  718. * @me_cl: me client
  719. *
  720. * Return: allocated device structur or NULL on allocation failure
  721. */
  722. static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
  723. struct mei_me_client *me_cl)
  724. {
  725. struct mei_cl_device *cldev;
  726. struct mei_cl *cl;
  727. cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
  728. if (!cldev)
  729. return NULL;
  730. cl = mei_cl_allocate(bus);
  731. if (!cl) {
  732. kfree(cldev);
  733. return NULL;
  734. }
  735. device_initialize(&cldev->dev);
  736. cldev->dev.parent = bus->dev;
  737. cldev->dev.bus = &mei_cl_bus_type;
  738. cldev->dev.type = &mei_cl_device_type;
  739. cldev->bus = mei_dev_bus_get(bus);
  740. cldev->me_cl = mei_me_cl_get(me_cl);
  741. cldev->cl = cl;
  742. mei_cl_bus_set_name(cldev);
  743. cldev->is_added = 0;
  744. INIT_LIST_HEAD(&cldev->bus_list);
  745. return cldev;
  746. }
  747. /**
  748. * mei_cl_dev_setup - setup me client device
  749. * run fix up routines and set the device name
  750. *
  751. * @bus: mei device
  752. * @cldev: me client device
  753. *
  754. * Return: true if the device is eligible for enumeration
  755. */
  756. static bool mei_cl_bus_dev_setup(struct mei_device *bus,
  757. struct mei_cl_device *cldev)
  758. {
  759. cldev->do_match = 1;
  760. mei_cl_bus_dev_fixup(cldev);
  761. /* the device name can change during fix up */
  762. if (cldev->do_match)
  763. mei_cl_bus_set_name(cldev);
  764. return cldev->do_match == 1;
  765. }
  766. /**
  767. * mei_cl_bus_dev_add - add me client devices
  768. *
  769. * @cldev: me client device
  770. *
  771. * Return: 0 on success; < 0 on failre
  772. */
  773. static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
  774. {
  775. int ret;
  776. dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
  777. mei_me_cl_uuid(cldev->me_cl),
  778. mei_me_cl_ver(cldev->me_cl));
  779. ret = device_add(&cldev->dev);
  780. if (!ret)
  781. cldev->is_added = 1;
  782. return ret;
  783. }
  784. /**
  785. * mei_cl_bus_dev_stop - stop the driver
  786. *
  787. * @cldev: me client device
  788. */
  789. static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
  790. {
  791. if (cldev->is_added)
  792. device_release_driver(&cldev->dev);
  793. }
  794. /**
  795. * mei_cl_bus_dev_destroy - destroy me client devices object
  796. *
  797. * @cldev: me client device
  798. *
  799. * Locking: called under "dev->cl_bus_lock" lock
  800. */
  801. static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
  802. {
  803. WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
  804. if (!cldev->is_added)
  805. return;
  806. device_del(&cldev->dev);
  807. list_del_init(&cldev->bus_list);
  808. cldev->is_added = 0;
  809. put_device(&cldev->dev);
  810. }
  811. /**
  812. * mei_cl_bus_remove_device - remove a devices form the bus
  813. *
  814. * @cldev: me client device
  815. */
  816. static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
  817. {
  818. mei_cl_bus_dev_stop(cldev);
  819. mei_cl_bus_dev_destroy(cldev);
  820. }
  821. /**
  822. * mei_cl_bus_remove_devices - remove all devices form the bus
  823. *
  824. * @bus: mei device
  825. */
  826. void mei_cl_bus_remove_devices(struct mei_device *bus)
  827. {
  828. struct mei_cl_device *cldev, *next;
  829. mutex_lock(&bus->cl_bus_lock);
  830. list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
  831. mei_cl_bus_remove_device(cldev);
  832. mutex_unlock(&bus->cl_bus_lock);
  833. }
  834. /**
  835. * mei_cl_bus_dev_init - allocate and initializes an mei client devices
  836. * based on me client
  837. *
  838. * @bus: mei device
  839. * @me_cl: me client
  840. *
  841. * Locking: called under "dev->cl_bus_lock" lock
  842. */
  843. static void mei_cl_bus_dev_init(struct mei_device *bus,
  844. struct mei_me_client *me_cl)
  845. {
  846. struct mei_cl_device *cldev;
  847. WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
  848. dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
  849. if (me_cl->bus_added)
  850. return;
  851. cldev = mei_cl_bus_dev_alloc(bus, me_cl);
  852. if (!cldev)
  853. return;
  854. me_cl->bus_added = true;
  855. list_add_tail(&cldev->bus_list, &bus->device_list);
  856. }
  857. /**
  858. * mei_cl_bus_rescan - scan me clients list and add create
  859. * devices for eligible clients
  860. *
  861. * @bus: mei device
  862. */
  863. void mei_cl_bus_rescan(struct mei_device *bus)
  864. {
  865. struct mei_cl_device *cldev, *n;
  866. struct mei_me_client *me_cl;
  867. mutex_lock(&bus->cl_bus_lock);
  868. down_read(&bus->me_clients_rwsem);
  869. list_for_each_entry(me_cl, &bus->me_clients, list)
  870. mei_cl_bus_dev_init(bus, me_cl);
  871. up_read(&bus->me_clients_rwsem);
  872. list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
  873. if (!mei_me_cl_is_active(cldev->me_cl)) {
  874. mei_cl_bus_remove_device(cldev);
  875. continue;
  876. }
  877. if (cldev->is_added)
  878. continue;
  879. if (mei_cl_bus_dev_setup(bus, cldev))
  880. mei_cl_bus_dev_add(cldev);
  881. else {
  882. list_del_init(&cldev->bus_list);
  883. put_device(&cldev->dev);
  884. }
  885. }
  886. mutex_unlock(&bus->cl_bus_lock);
  887. dev_dbg(bus->dev, "rescan end");
  888. }
  889. void mei_cl_bus_rescan_work(struct work_struct *work)
  890. {
  891. struct mei_device *bus =
  892. container_of(work, struct mei_device, bus_rescan_work);
  893. mei_cl_bus_rescan(bus);
  894. }
  895. int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
  896. struct module *owner)
  897. {
  898. int err;
  899. cldrv->driver.name = cldrv->name;
  900. cldrv->driver.owner = owner;
  901. cldrv->driver.bus = &mei_cl_bus_type;
  902. err = driver_register(&cldrv->driver);
  903. if (err)
  904. return err;
  905. pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
  906. return 0;
  907. }
  908. EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
  909. void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
  910. {
  911. driver_unregister(&cldrv->driver);
  912. pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
  913. }
  914. EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
  915. int __init mei_cl_bus_init(void)
  916. {
  917. return bus_register(&mei_cl_bus_type);
  918. }
  919. void __exit mei_cl_bus_exit(void)
  920. {
  921. bus_unregister(&mei_cl_bus_type);
  922. }