bus.c 24 KB

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