bus.c 23 KB

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