bus.c 24 KB

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