bus.c 21 KB

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