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