bus.c 23 KB

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