main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/kernel.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/fs.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/poll.h>
  26. #include <linux/init.h>
  27. #include <linux/ioctl.h>
  28. #include <linux/cdev.h>
  29. #include <linux/sched/signal.h>
  30. #include <linux/uuid.h>
  31. #include <linux/compat.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/mei.h>
  35. #include "mei_dev.h"
  36. #include "client.h"
  37. /**
  38. * mei_open - the open function
  39. *
  40. * @inode: pointer to inode structure
  41. * @file: pointer to file structure
  42. *
  43. * Return: 0 on success, <0 on error
  44. */
  45. static int mei_open(struct inode *inode, struct file *file)
  46. {
  47. struct mei_device *dev;
  48. struct mei_cl *cl;
  49. int err;
  50. dev = container_of(inode->i_cdev, struct mei_device, cdev);
  51. if (!dev)
  52. return -ENODEV;
  53. mutex_lock(&dev->device_lock);
  54. if (dev->dev_state != MEI_DEV_ENABLED) {
  55. dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
  56. mei_dev_state_str(dev->dev_state));
  57. err = -ENODEV;
  58. goto err_unlock;
  59. }
  60. cl = mei_cl_alloc_linked(dev);
  61. if (IS_ERR(cl)) {
  62. err = PTR_ERR(cl);
  63. goto err_unlock;
  64. }
  65. cl->fp = file;
  66. file->private_data = cl;
  67. mutex_unlock(&dev->device_lock);
  68. return nonseekable_open(inode, file);
  69. err_unlock:
  70. mutex_unlock(&dev->device_lock);
  71. return err;
  72. }
  73. /**
  74. * mei_release - the release function
  75. *
  76. * @inode: pointer to inode structure
  77. * @file: pointer to file structure
  78. *
  79. * Return: 0 on success, <0 on error
  80. */
  81. static int mei_release(struct inode *inode, struct file *file)
  82. {
  83. struct mei_cl *cl = file->private_data;
  84. struct mei_device *dev;
  85. int rets;
  86. if (WARN_ON(!cl || !cl->dev))
  87. return -ENODEV;
  88. dev = cl->dev;
  89. mutex_lock(&dev->device_lock);
  90. rets = mei_cl_disconnect(cl);
  91. mei_cl_flush_queues(cl, file);
  92. cl_dbg(dev, cl, "removing\n");
  93. mei_cl_unlink(cl);
  94. file->private_data = NULL;
  95. kfree(cl);
  96. mutex_unlock(&dev->device_lock);
  97. return rets;
  98. }
  99. /**
  100. * mei_read - the read function.
  101. *
  102. * @file: pointer to file structure
  103. * @ubuf: pointer to user buffer
  104. * @length: buffer length
  105. * @offset: data offset in buffer
  106. *
  107. * Return: >=0 data length on success , <0 on error
  108. */
  109. static ssize_t mei_read(struct file *file, char __user *ubuf,
  110. size_t length, loff_t *offset)
  111. {
  112. struct mei_cl *cl = file->private_data;
  113. struct mei_device *dev;
  114. struct mei_cl_cb *cb = NULL;
  115. bool nonblock = !!(file->f_flags & O_NONBLOCK);
  116. int rets;
  117. if (WARN_ON(!cl || !cl->dev))
  118. return -ENODEV;
  119. dev = cl->dev;
  120. mutex_lock(&dev->device_lock);
  121. if (dev->dev_state != MEI_DEV_ENABLED) {
  122. rets = -ENODEV;
  123. goto out;
  124. }
  125. if (length == 0) {
  126. rets = 0;
  127. goto out;
  128. }
  129. if (ubuf == NULL) {
  130. rets = -EMSGSIZE;
  131. goto out;
  132. }
  133. cb = mei_cl_read_cb(cl, file);
  134. if (cb)
  135. goto copy_buffer;
  136. if (*offset > 0)
  137. *offset = 0;
  138. rets = mei_cl_read_start(cl, length, file);
  139. if (rets && rets != -EBUSY) {
  140. cl_dbg(dev, cl, "mei start read failure status = %d\n", rets);
  141. goto out;
  142. }
  143. if (nonblock) {
  144. rets = -EAGAIN;
  145. goto out;
  146. }
  147. mutex_unlock(&dev->device_lock);
  148. if (wait_event_interruptible(cl->rx_wait,
  149. !list_empty(&cl->rd_completed) ||
  150. !mei_cl_is_connected(cl))) {
  151. if (signal_pending(current))
  152. return -EINTR;
  153. return -ERESTARTSYS;
  154. }
  155. mutex_lock(&dev->device_lock);
  156. if (!mei_cl_is_connected(cl)) {
  157. rets = -ENODEV;
  158. goto out;
  159. }
  160. cb = mei_cl_read_cb(cl, file);
  161. if (!cb) {
  162. rets = 0;
  163. goto out;
  164. }
  165. copy_buffer:
  166. /* now copy the data to user space */
  167. if (cb->status) {
  168. rets = cb->status;
  169. cl_dbg(dev, cl, "read operation failed %d\n", rets);
  170. goto free;
  171. }
  172. cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n",
  173. cb->buf.size, cb->buf_idx, *offset);
  174. if (*offset >= cb->buf_idx) {
  175. rets = 0;
  176. goto free;
  177. }
  178. /* length is being truncated to PAGE_SIZE,
  179. * however buf_idx may point beyond that */
  180. length = min_t(size_t, length, cb->buf_idx - *offset);
  181. if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
  182. dev_dbg(dev->dev, "failed to copy data to userland\n");
  183. rets = -EFAULT;
  184. goto free;
  185. }
  186. rets = length;
  187. *offset += length;
  188. /* not all data was read, keep the cb */
  189. if (*offset < cb->buf_idx)
  190. goto out;
  191. free:
  192. mei_io_cb_free(cb);
  193. *offset = 0;
  194. out:
  195. cl_dbg(dev, cl, "end mei read rets = %d\n", rets);
  196. mutex_unlock(&dev->device_lock);
  197. return rets;
  198. }
  199. /**
  200. * mei_write - the write function.
  201. *
  202. * @file: pointer to file structure
  203. * @ubuf: pointer to user buffer
  204. * @length: buffer length
  205. * @offset: data offset in buffer
  206. *
  207. * Return: >=0 data length on success , <0 on error
  208. */
  209. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  210. size_t length, loff_t *offset)
  211. {
  212. struct mei_cl *cl = file->private_data;
  213. struct mei_cl_cb *cb;
  214. struct mei_device *dev;
  215. int rets;
  216. if (WARN_ON(!cl || !cl->dev))
  217. return -ENODEV;
  218. dev = cl->dev;
  219. mutex_lock(&dev->device_lock);
  220. if (dev->dev_state != MEI_DEV_ENABLED) {
  221. rets = -ENODEV;
  222. goto out;
  223. }
  224. if (!mei_cl_is_connected(cl)) {
  225. cl_err(dev, cl, "is not connected");
  226. rets = -ENODEV;
  227. goto out;
  228. }
  229. if (!mei_me_cl_is_active(cl->me_cl)) {
  230. rets = -ENOTTY;
  231. goto out;
  232. }
  233. if (length > mei_cl_mtu(cl)) {
  234. rets = -EFBIG;
  235. goto out;
  236. }
  237. if (length == 0) {
  238. rets = 0;
  239. goto out;
  240. }
  241. *offset = 0;
  242. cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
  243. if (!cb) {
  244. rets = -ENOMEM;
  245. goto out;
  246. }
  247. rets = copy_from_user(cb->buf.data, ubuf, length);
  248. if (rets) {
  249. dev_dbg(dev->dev, "failed to copy data from userland\n");
  250. rets = -EFAULT;
  251. mei_io_cb_free(cb);
  252. goto out;
  253. }
  254. rets = mei_cl_write(cl, cb);
  255. out:
  256. mutex_unlock(&dev->device_lock);
  257. return rets;
  258. }
  259. /**
  260. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  261. *
  262. * @file: private data of the file object
  263. * @data: IOCTL connect data, input and output parameters
  264. *
  265. * Locking: called under "dev->device_lock" lock
  266. *
  267. * Return: 0 on success, <0 on failure.
  268. */
  269. static int mei_ioctl_connect_client(struct file *file,
  270. struct mei_connect_client_data *data)
  271. {
  272. struct mei_device *dev;
  273. struct mei_client *client;
  274. struct mei_me_client *me_cl;
  275. struct mei_cl *cl;
  276. int rets;
  277. cl = file->private_data;
  278. dev = cl->dev;
  279. if (dev->dev_state != MEI_DEV_ENABLED)
  280. return -ENODEV;
  281. if (cl->state != MEI_FILE_INITIALIZING &&
  282. cl->state != MEI_FILE_DISCONNECTED)
  283. return -EBUSY;
  284. /* find ME client we're trying to connect to */
  285. me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
  286. if (!me_cl) {
  287. dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
  288. &data->in_client_uuid);
  289. rets = -ENOTTY;
  290. goto end;
  291. }
  292. if (me_cl->props.fixed_address) {
  293. bool forbidden = dev->override_fixed_address ?
  294. !dev->allow_fixed_address : !dev->hbm_f_fa_supported;
  295. if (forbidden) {
  296. dev_dbg(dev->dev, "Connection forbidden to FW Client UUID = %pUl\n",
  297. &data->in_client_uuid);
  298. rets = -ENOTTY;
  299. goto end;
  300. }
  301. }
  302. dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
  303. me_cl->client_id);
  304. dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
  305. me_cl->props.protocol_version);
  306. dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
  307. me_cl->props.max_msg_length);
  308. /* prepare the output buffer */
  309. client = &data->out_client_properties;
  310. client->max_msg_length = me_cl->props.max_msg_length;
  311. client->protocol_version = me_cl->props.protocol_version;
  312. dev_dbg(dev->dev, "Can connect?\n");
  313. rets = mei_cl_connect(cl, me_cl, file);
  314. end:
  315. mei_me_cl_put(me_cl);
  316. return rets;
  317. }
  318. /**
  319. * mei_ioctl_client_notify_request -
  320. * propagate event notification request to client
  321. *
  322. * @file: pointer to file structure
  323. * @request: 0 - disable, 1 - enable
  324. *
  325. * Return: 0 on success , <0 on error
  326. */
  327. static int mei_ioctl_client_notify_request(const struct file *file, u32 request)
  328. {
  329. struct mei_cl *cl = file->private_data;
  330. if (request != MEI_HBM_NOTIFICATION_START &&
  331. request != MEI_HBM_NOTIFICATION_STOP)
  332. return -EINVAL;
  333. return mei_cl_notify_request(cl, file, (u8)request);
  334. }
  335. /**
  336. * mei_ioctl_client_notify_get - wait for notification request
  337. *
  338. * @file: pointer to file structure
  339. * @notify_get: 0 - disable, 1 - enable
  340. *
  341. * Return: 0 on success , <0 on error
  342. */
  343. static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get)
  344. {
  345. struct mei_cl *cl = file->private_data;
  346. bool notify_ev;
  347. bool block = (file->f_flags & O_NONBLOCK) == 0;
  348. int rets;
  349. rets = mei_cl_notify_get(cl, block, &notify_ev);
  350. if (rets)
  351. return rets;
  352. *notify_get = notify_ev ? 1 : 0;
  353. return 0;
  354. }
  355. /**
  356. * mei_ioctl - the IOCTL function
  357. *
  358. * @file: pointer to file structure
  359. * @cmd: ioctl command
  360. * @data: pointer to mei message structure
  361. *
  362. * Return: 0 on success , <0 on error
  363. */
  364. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  365. {
  366. struct mei_device *dev;
  367. struct mei_cl *cl = file->private_data;
  368. struct mei_connect_client_data connect_data;
  369. u32 notify_get, notify_req;
  370. int rets;
  371. if (WARN_ON(!cl || !cl->dev))
  372. return -ENODEV;
  373. dev = cl->dev;
  374. dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
  375. mutex_lock(&dev->device_lock);
  376. if (dev->dev_state != MEI_DEV_ENABLED) {
  377. rets = -ENODEV;
  378. goto out;
  379. }
  380. switch (cmd) {
  381. case IOCTL_MEI_CONNECT_CLIENT:
  382. dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  383. if (copy_from_user(&connect_data, (char __user *)data,
  384. sizeof(struct mei_connect_client_data))) {
  385. dev_dbg(dev->dev, "failed to copy data from userland\n");
  386. rets = -EFAULT;
  387. goto out;
  388. }
  389. rets = mei_ioctl_connect_client(file, &connect_data);
  390. if (rets)
  391. goto out;
  392. /* if all is ok, copying the data back to user. */
  393. if (copy_to_user((char __user *)data, &connect_data,
  394. sizeof(struct mei_connect_client_data))) {
  395. dev_dbg(dev->dev, "failed to copy data to userland\n");
  396. rets = -EFAULT;
  397. goto out;
  398. }
  399. break;
  400. case IOCTL_MEI_NOTIFY_SET:
  401. dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
  402. if (copy_from_user(&notify_req,
  403. (char __user *)data, sizeof(notify_req))) {
  404. dev_dbg(dev->dev, "failed to copy data from userland\n");
  405. rets = -EFAULT;
  406. goto out;
  407. }
  408. rets = mei_ioctl_client_notify_request(file, notify_req);
  409. break;
  410. case IOCTL_MEI_NOTIFY_GET:
  411. dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
  412. rets = mei_ioctl_client_notify_get(file, &notify_get);
  413. if (rets)
  414. goto out;
  415. dev_dbg(dev->dev, "copy connect data to user\n");
  416. if (copy_to_user((char __user *)data,
  417. &notify_get, sizeof(notify_get))) {
  418. dev_dbg(dev->dev, "failed to copy data to userland\n");
  419. rets = -EFAULT;
  420. goto out;
  421. }
  422. break;
  423. default:
  424. dev_err(dev->dev, ": unsupported ioctl %d.\n", cmd);
  425. rets = -ENOIOCTLCMD;
  426. }
  427. out:
  428. mutex_unlock(&dev->device_lock);
  429. return rets;
  430. }
  431. /**
  432. * mei_compat_ioctl - the compat IOCTL function
  433. *
  434. * @file: pointer to file structure
  435. * @cmd: ioctl command
  436. * @data: pointer to mei message structure
  437. *
  438. * Return: 0 on success , <0 on error
  439. */
  440. #ifdef CONFIG_COMPAT
  441. static long mei_compat_ioctl(struct file *file,
  442. unsigned int cmd, unsigned long data)
  443. {
  444. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  445. }
  446. #endif
  447. /**
  448. * mei_poll - the poll function
  449. *
  450. * @file: pointer to file structure
  451. * @wait: pointer to poll_table structure
  452. *
  453. * Return: poll mask
  454. */
  455. static unsigned int mei_poll(struct file *file, poll_table *wait)
  456. {
  457. unsigned long req_events = poll_requested_events(wait);
  458. struct mei_cl *cl = file->private_data;
  459. struct mei_device *dev;
  460. unsigned int mask = 0;
  461. bool notify_en;
  462. if (WARN_ON(!cl || !cl->dev))
  463. return POLLERR;
  464. dev = cl->dev;
  465. mutex_lock(&dev->device_lock);
  466. notify_en = cl->notify_en && (req_events & POLLPRI);
  467. if (dev->dev_state != MEI_DEV_ENABLED ||
  468. !mei_cl_is_connected(cl)) {
  469. mask = POLLERR;
  470. goto out;
  471. }
  472. if (notify_en) {
  473. poll_wait(file, &cl->ev_wait, wait);
  474. if (cl->notify_ev)
  475. mask |= POLLPRI;
  476. }
  477. if (req_events & (POLLIN | POLLRDNORM)) {
  478. poll_wait(file, &cl->rx_wait, wait);
  479. if (!list_empty(&cl->rd_completed))
  480. mask |= POLLIN | POLLRDNORM;
  481. else
  482. mei_cl_read_start(cl, mei_cl_mtu(cl), file);
  483. }
  484. out:
  485. mutex_unlock(&dev->device_lock);
  486. return mask;
  487. }
  488. /**
  489. * mei_cl_is_write_queued - check if the client has pending writes.
  490. *
  491. * @cl: writing host client
  492. *
  493. * Return: true if client is writing, false otherwise.
  494. */
  495. static bool mei_cl_is_write_queued(struct mei_cl *cl)
  496. {
  497. struct mei_device *dev = cl->dev;
  498. struct mei_cl_cb *cb;
  499. list_for_each_entry(cb, &dev->write_list, list)
  500. if (cb->cl == cl)
  501. return true;
  502. list_for_each_entry(cb, &dev->write_waiting_list, list)
  503. if (cb->cl == cl)
  504. return true;
  505. return false;
  506. }
  507. /**
  508. * mei_fsync - the fsync handler
  509. *
  510. * @fp: pointer to file structure
  511. * @start: unused
  512. * @end: unused
  513. * @datasync: unused
  514. *
  515. * Return: 0 on success, -ENODEV if client is not connected
  516. */
  517. static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
  518. {
  519. struct mei_cl *cl = fp->private_data;
  520. struct mei_device *dev;
  521. int rets;
  522. if (WARN_ON(!cl || !cl->dev))
  523. return -ENODEV;
  524. dev = cl->dev;
  525. mutex_lock(&dev->device_lock);
  526. if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) {
  527. rets = -ENODEV;
  528. goto out;
  529. }
  530. while (mei_cl_is_write_queued(cl)) {
  531. mutex_unlock(&dev->device_lock);
  532. rets = wait_event_interruptible(cl->tx_wait,
  533. cl->writing_state == MEI_WRITE_COMPLETE ||
  534. !mei_cl_is_connected(cl));
  535. mutex_lock(&dev->device_lock);
  536. if (rets) {
  537. if (signal_pending(current))
  538. rets = -EINTR;
  539. goto out;
  540. }
  541. if (!mei_cl_is_connected(cl)) {
  542. rets = -ENODEV;
  543. goto out;
  544. }
  545. }
  546. rets = 0;
  547. out:
  548. mutex_unlock(&dev->device_lock);
  549. return rets;
  550. }
  551. /**
  552. * mei_fasync - asynchronous io support
  553. *
  554. * @fd: file descriptor
  555. * @file: pointer to file structure
  556. * @band: band bitmap
  557. *
  558. * Return: negative on error,
  559. * 0 if it did no changes,
  560. * and positive a process was added or deleted
  561. */
  562. static int mei_fasync(int fd, struct file *file, int band)
  563. {
  564. struct mei_cl *cl = file->private_data;
  565. if (!mei_cl_is_connected(cl))
  566. return -ENODEV;
  567. return fasync_helper(fd, file, band, &cl->ev_async);
  568. }
  569. /**
  570. * fw_status_show - mei device fw_status attribute show method
  571. *
  572. * @device: device pointer
  573. * @attr: attribute pointer
  574. * @buf: char out buffer
  575. *
  576. * Return: number of the bytes printed into buf or error
  577. */
  578. static ssize_t fw_status_show(struct device *device,
  579. struct device_attribute *attr, char *buf)
  580. {
  581. struct mei_device *dev = dev_get_drvdata(device);
  582. struct mei_fw_status fw_status;
  583. int err, i;
  584. ssize_t cnt = 0;
  585. mutex_lock(&dev->device_lock);
  586. err = mei_fw_status(dev, &fw_status);
  587. mutex_unlock(&dev->device_lock);
  588. if (err) {
  589. dev_err(device, "read fw_status error = %d\n", err);
  590. return err;
  591. }
  592. for (i = 0; i < fw_status.count; i++)
  593. cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
  594. fw_status.status[i]);
  595. return cnt;
  596. }
  597. static DEVICE_ATTR_RO(fw_status);
  598. /**
  599. * hbm_ver_show - display HBM protocol version negotiated with FW
  600. *
  601. * @device: device pointer
  602. * @attr: attribute pointer
  603. * @buf: char out buffer
  604. *
  605. * Return: number of the bytes printed into buf or error
  606. */
  607. static ssize_t hbm_ver_show(struct device *device,
  608. struct device_attribute *attr, char *buf)
  609. {
  610. struct mei_device *dev = dev_get_drvdata(device);
  611. struct hbm_version ver;
  612. mutex_lock(&dev->device_lock);
  613. ver = dev->version;
  614. mutex_unlock(&dev->device_lock);
  615. return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version);
  616. }
  617. static DEVICE_ATTR_RO(hbm_ver);
  618. /**
  619. * hbm_ver_drv_show - display HBM protocol version advertised by driver
  620. *
  621. * @device: device pointer
  622. * @attr: attribute pointer
  623. * @buf: char out buffer
  624. *
  625. * Return: number of the bytes printed into buf or error
  626. */
  627. static ssize_t hbm_ver_drv_show(struct device *device,
  628. struct device_attribute *attr, char *buf)
  629. {
  630. return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION);
  631. }
  632. static DEVICE_ATTR_RO(hbm_ver_drv);
  633. static struct attribute *mei_attrs[] = {
  634. &dev_attr_fw_status.attr,
  635. &dev_attr_hbm_ver.attr,
  636. &dev_attr_hbm_ver_drv.attr,
  637. NULL
  638. };
  639. ATTRIBUTE_GROUPS(mei);
  640. /*
  641. * file operations structure will be used for mei char device.
  642. */
  643. static const struct file_operations mei_fops = {
  644. .owner = THIS_MODULE,
  645. .read = mei_read,
  646. .unlocked_ioctl = mei_ioctl,
  647. #ifdef CONFIG_COMPAT
  648. .compat_ioctl = mei_compat_ioctl,
  649. #endif
  650. .open = mei_open,
  651. .release = mei_release,
  652. .write = mei_write,
  653. .poll = mei_poll,
  654. .fsync = mei_fsync,
  655. .fasync = mei_fasync,
  656. .llseek = no_llseek
  657. };
  658. static struct class *mei_class;
  659. static dev_t mei_devt;
  660. #define MEI_MAX_DEVS MINORMASK
  661. static DEFINE_MUTEX(mei_minor_lock);
  662. static DEFINE_IDR(mei_idr);
  663. /**
  664. * mei_minor_get - obtain next free device minor number
  665. *
  666. * @dev: device pointer
  667. *
  668. * Return: allocated minor, or -ENOSPC if no free minor left
  669. */
  670. static int mei_minor_get(struct mei_device *dev)
  671. {
  672. int ret;
  673. mutex_lock(&mei_minor_lock);
  674. ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
  675. if (ret >= 0)
  676. dev->minor = ret;
  677. else if (ret == -ENOSPC)
  678. dev_err(dev->dev, "too many mei devices\n");
  679. mutex_unlock(&mei_minor_lock);
  680. return ret;
  681. }
  682. /**
  683. * mei_minor_free - mark device minor number as free
  684. *
  685. * @dev: device pointer
  686. */
  687. static void mei_minor_free(struct mei_device *dev)
  688. {
  689. mutex_lock(&mei_minor_lock);
  690. idr_remove(&mei_idr, dev->minor);
  691. mutex_unlock(&mei_minor_lock);
  692. }
  693. int mei_register(struct mei_device *dev, struct device *parent)
  694. {
  695. struct device *clsdev; /* class device */
  696. int ret, devno;
  697. ret = mei_minor_get(dev);
  698. if (ret < 0)
  699. return ret;
  700. /* Fill in the data structures */
  701. devno = MKDEV(MAJOR(mei_devt), dev->minor);
  702. cdev_init(&dev->cdev, &mei_fops);
  703. dev->cdev.owner = parent->driver->owner;
  704. /* Add the device */
  705. ret = cdev_add(&dev->cdev, devno, 1);
  706. if (ret) {
  707. dev_err(parent, "unable to add device %d:%d\n",
  708. MAJOR(mei_devt), dev->minor);
  709. goto err_dev_add;
  710. }
  711. clsdev = device_create_with_groups(mei_class, parent, devno,
  712. dev, mei_groups,
  713. "mei%d", dev->minor);
  714. if (IS_ERR(clsdev)) {
  715. dev_err(parent, "unable to create device %d:%d\n",
  716. MAJOR(mei_devt), dev->minor);
  717. ret = PTR_ERR(clsdev);
  718. goto err_dev_create;
  719. }
  720. ret = mei_dbgfs_register(dev, dev_name(clsdev));
  721. if (ret) {
  722. dev_err(clsdev, "cannot register debugfs ret = %d\n", ret);
  723. goto err_dev_dbgfs;
  724. }
  725. return 0;
  726. err_dev_dbgfs:
  727. device_destroy(mei_class, devno);
  728. err_dev_create:
  729. cdev_del(&dev->cdev);
  730. err_dev_add:
  731. mei_minor_free(dev);
  732. return ret;
  733. }
  734. EXPORT_SYMBOL_GPL(mei_register);
  735. void mei_deregister(struct mei_device *dev)
  736. {
  737. int devno;
  738. devno = dev->cdev.dev;
  739. cdev_del(&dev->cdev);
  740. mei_dbgfs_deregister(dev);
  741. device_destroy(mei_class, devno);
  742. mei_minor_free(dev);
  743. }
  744. EXPORT_SYMBOL_GPL(mei_deregister);
  745. static int __init mei_init(void)
  746. {
  747. int ret;
  748. mei_class = class_create(THIS_MODULE, "mei");
  749. if (IS_ERR(mei_class)) {
  750. pr_err("couldn't create class\n");
  751. ret = PTR_ERR(mei_class);
  752. goto err;
  753. }
  754. ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
  755. if (ret < 0) {
  756. pr_err("unable to allocate char dev region\n");
  757. goto err_class;
  758. }
  759. ret = mei_cl_bus_init();
  760. if (ret < 0) {
  761. pr_err("unable to initialize bus\n");
  762. goto err_chrdev;
  763. }
  764. return 0;
  765. err_chrdev:
  766. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  767. err_class:
  768. class_destroy(mei_class);
  769. err:
  770. return ret;
  771. }
  772. static void __exit mei_exit(void)
  773. {
  774. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  775. class_destroy(mei_class);
  776. mei_cl_bus_exit();
  777. }
  778. module_init(mei_init);
  779. module_exit(mei_exit);
  780. MODULE_AUTHOR("Intel Corporation");
  781. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  782. MODULE_LICENSE("GPL v2");