main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
  242. if (!cb) {
  243. rets = -ENOMEM;
  244. goto out;
  245. }
  246. rets = copy_from_user(cb->buf.data, ubuf, length);
  247. if (rets) {
  248. dev_dbg(dev->dev, "failed to copy data from userland\n");
  249. rets = -EFAULT;
  250. mei_io_cb_free(cb);
  251. goto out;
  252. }
  253. rets = mei_cl_write(cl, cb);
  254. out:
  255. mutex_unlock(&dev->device_lock);
  256. return rets;
  257. }
  258. /**
  259. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  260. *
  261. * @file: private data of the file object
  262. * @data: IOCTL connect data, input and output parameters
  263. *
  264. * Locking: called under "dev->device_lock" lock
  265. *
  266. * Return: 0 on success, <0 on failure.
  267. */
  268. static int mei_ioctl_connect_client(struct file *file,
  269. struct mei_connect_client_data *data)
  270. {
  271. struct mei_device *dev;
  272. struct mei_client *client;
  273. struct mei_me_client *me_cl;
  274. struct mei_cl *cl;
  275. int rets;
  276. cl = file->private_data;
  277. dev = cl->dev;
  278. if (dev->dev_state != MEI_DEV_ENABLED)
  279. return -ENODEV;
  280. if (cl->state != MEI_FILE_INITIALIZING &&
  281. cl->state != MEI_FILE_DISCONNECTED)
  282. return -EBUSY;
  283. /* find ME client we're trying to connect to */
  284. me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
  285. if (!me_cl) {
  286. dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
  287. &data->in_client_uuid);
  288. rets = -ENOTTY;
  289. goto end;
  290. }
  291. if (me_cl->props.fixed_address) {
  292. bool forbidden = dev->override_fixed_address ?
  293. !dev->allow_fixed_address : !dev->hbm_f_fa_supported;
  294. if (forbidden) {
  295. dev_dbg(dev->dev, "Connection forbidden to FW Client UUID = %pUl\n",
  296. &data->in_client_uuid);
  297. rets = -ENOTTY;
  298. goto end;
  299. }
  300. }
  301. dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
  302. me_cl->client_id);
  303. dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
  304. me_cl->props.protocol_version);
  305. dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
  306. me_cl->props.max_msg_length);
  307. /* prepare the output buffer */
  308. client = &data->out_client_properties;
  309. client->max_msg_length = me_cl->props.max_msg_length;
  310. client->protocol_version = me_cl->props.protocol_version;
  311. dev_dbg(dev->dev, "Can connect?\n");
  312. rets = mei_cl_connect(cl, me_cl, file);
  313. end:
  314. mei_me_cl_put(me_cl);
  315. return rets;
  316. }
  317. /**
  318. * mei_ioctl_client_notify_request -
  319. * propagate event notification request to client
  320. *
  321. * @file: pointer to file structure
  322. * @request: 0 - disable, 1 - enable
  323. *
  324. * Return: 0 on success , <0 on error
  325. */
  326. static int mei_ioctl_client_notify_request(const struct file *file, u32 request)
  327. {
  328. struct mei_cl *cl = file->private_data;
  329. if (request != MEI_HBM_NOTIFICATION_START &&
  330. request != MEI_HBM_NOTIFICATION_STOP)
  331. return -EINVAL;
  332. return mei_cl_notify_request(cl, file, (u8)request);
  333. }
  334. /**
  335. * mei_ioctl_client_notify_get - wait for notification request
  336. *
  337. * @file: pointer to file structure
  338. * @notify_get: 0 - disable, 1 - enable
  339. *
  340. * Return: 0 on success , <0 on error
  341. */
  342. static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get)
  343. {
  344. struct mei_cl *cl = file->private_data;
  345. bool notify_ev;
  346. bool block = (file->f_flags & O_NONBLOCK) == 0;
  347. int rets;
  348. rets = mei_cl_notify_get(cl, block, &notify_ev);
  349. if (rets)
  350. return rets;
  351. *notify_get = notify_ev ? 1 : 0;
  352. return 0;
  353. }
  354. /**
  355. * mei_ioctl - the IOCTL function
  356. *
  357. * @file: pointer to file structure
  358. * @cmd: ioctl command
  359. * @data: pointer to mei message structure
  360. *
  361. * Return: 0 on success , <0 on error
  362. */
  363. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  364. {
  365. struct mei_device *dev;
  366. struct mei_cl *cl = file->private_data;
  367. struct mei_connect_client_data connect_data;
  368. u32 notify_get, notify_req;
  369. int rets;
  370. if (WARN_ON(!cl || !cl->dev))
  371. return -ENODEV;
  372. dev = cl->dev;
  373. dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
  374. mutex_lock(&dev->device_lock);
  375. if (dev->dev_state != MEI_DEV_ENABLED) {
  376. rets = -ENODEV;
  377. goto out;
  378. }
  379. switch (cmd) {
  380. case IOCTL_MEI_CONNECT_CLIENT:
  381. dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  382. if (copy_from_user(&connect_data, (char __user *)data,
  383. sizeof(struct mei_connect_client_data))) {
  384. dev_dbg(dev->dev, "failed to copy data from userland\n");
  385. rets = -EFAULT;
  386. goto out;
  387. }
  388. rets = mei_ioctl_connect_client(file, &connect_data);
  389. if (rets)
  390. goto out;
  391. /* if all is ok, copying the data back to user. */
  392. if (copy_to_user((char __user *)data, &connect_data,
  393. sizeof(struct mei_connect_client_data))) {
  394. dev_dbg(dev->dev, "failed to copy data to userland\n");
  395. rets = -EFAULT;
  396. goto out;
  397. }
  398. break;
  399. case IOCTL_MEI_NOTIFY_SET:
  400. dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
  401. if (copy_from_user(&notify_req,
  402. (char __user *)data, sizeof(notify_req))) {
  403. dev_dbg(dev->dev, "failed to copy data from userland\n");
  404. rets = -EFAULT;
  405. goto out;
  406. }
  407. rets = mei_ioctl_client_notify_request(file, notify_req);
  408. break;
  409. case IOCTL_MEI_NOTIFY_GET:
  410. dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
  411. rets = mei_ioctl_client_notify_get(file, &notify_get);
  412. if (rets)
  413. goto out;
  414. dev_dbg(dev->dev, "copy connect data to user\n");
  415. if (copy_to_user((char __user *)data,
  416. &notify_get, sizeof(notify_get))) {
  417. dev_dbg(dev->dev, "failed to copy data to userland\n");
  418. rets = -EFAULT;
  419. goto out;
  420. }
  421. break;
  422. default:
  423. rets = -ENOIOCTLCMD;
  424. }
  425. out:
  426. mutex_unlock(&dev->device_lock);
  427. return rets;
  428. }
  429. /**
  430. * mei_compat_ioctl - the compat IOCTL function
  431. *
  432. * @file: pointer to file structure
  433. * @cmd: ioctl command
  434. * @data: pointer to mei message structure
  435. *
  436. * Return: 0 on success , <0 on error
  437. */
  438. #ifdef CONFIG_COMPAT
  439. static long mei_compat_ioctl(struct file *file,
  440. unsigned int cmd, unsigned long data)
  441. {
  442. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  443. }
  444. #endif
  445. /**
  446. * mei_poll - the poll function
  447. *
  448. * @file: pointer to file structure
  449. * @wait: pointer to poll_table structure
  450. *
  451. * Return: poll mask
  452. */
  453. static unsigned int mei_poll(struct file *file, poll_table *wait)
  454. {
  455. unsigned long req_events = poll_requested_events(wait);
  456. struct mei_cl *cl = file->private_data;
  457. struct mei_device *dev;
  458. unsigned int mask = 0;
  459. bool notify_en;
  460. if (WARN_ON(!cl || !cl->dev))
  461. return POLLERR;
  462. dev = cl->dev;
  463. mutex_lock(&dev->device_lock);
  464. notify_en = cl->notify_en && (req_events & POLLPRI);
  465. if (dev->dev_state != MEI_DEV_ENABLED ||
  466. !mei_cl_is_connected(cl)) {
  467. mask = POLLERR;
  468. goto out;
  469. }
  470. if (notify_en) {
  471. poll_wait(file, &cl->ev_wait, wait);
  472. if (cl->notify_ev)
  473. mask |= POLLPRI;
  474. }
  475. if (req_events & (POLLIN | POLLRDNORM)) {
  476. poll_wait(file, &cl->rx_wait, wait);
  477. if (!list_empty(&cl->rd_completed))
  478. mask |= POLLIN | POLLRDNORM;
  479. else
  480. mei_cl_read_start(cl, mei_cl_mtu(cl), file);
  481. }
  482. out:
  483. mutex_unlock(&dev->device_lock);
  484. return mask;
  485. }
  486. /**
  487. * mei_cl_is_write_queued - check if the client has pending writes.
  488. *
  489. * @cl: writing host client
  490. *
  491. * Return: true if client is writing, false otherwise.
  492. */
  493. static bool mei_cl_is_write_queued(struct mei_cl *cl)
  494. {
  495. struct mei_device *dev = cl->dev;
  496. struct mei_cl_cb *cb;
  497. list_for_each_entry(cb, &dev->write_list, list)
  498. if (cb->cl == cl)
  499. return true;
  500. list_for_each_entry(cb, &dev->write_waiting_list, list)
  501. if (cb->cl == cl)
  502. return true;
  503. return false;
  504. }
  505. /**
  506. * mei_fsync - the fsync handler
  507. *
  508. * @fp: pointer to file structure
  509. * @start: unused
  510. * @end: unused
  511. * @datasync: unused
  512. *
  513. * Return: 0 on success, -ENODEV if client is not connected
  514. */
  515. static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
  516. {
  517. struct mei_cl *cl = fp->private_data;
  518. struct mei_device *dev;
  519. int rets;
  520. if (WARN_ON(!cl || !cl->dev))
  521. return -ENODEV;
  522. dev = cl->dev;
  523. mutex_lock(&dev->device_lock);
  524. if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) {
  525. rets = -ENODEV;
  526. goto out;
  527. }
  528. while (mei_cl_is_write_queued(cl)) {
  529. mutex_unlock(&dev->device_lock);
  530. rets = wait_event_interruptible(cl->tx_wait,
  531. cl->writing_state == MEI_WRITE_COMPLETE ||
  532. !mei_cl_is_connected(cl));
  533. mutex_lock(&dev->device_lock);
  534. if (rets) {
  535. if (signal_pending(current))
  536. rets = -EINTR;
  537. goto out;
  538. }
  539. if (!mei_cl_is_connected(cl)) {
  540. rets = -ENODEV;
  541. goto out;
  542. }
  543. }
  544. rets = 0;
  545. out:
  546. mutex_unlock(&dev->device_lock);
  547. return rets;
  548. }
  549. /**
  550. * mei_fasync - asynchronous io support
  551. *
  552. * @fd: file descriptor
  553. * @file: pointer to file structure
  554. * @band: band bitmap
  555. *
  556. * Return: negative on error,
  557. * 0 if it did no changes,
  558. * and positive a process was added or deleted
  559. */
  560. static int mei_fasync(int fd, struct file *file, int band)
  561. {
  562. struct mei_cl *cl = file->private_data;
  563. if (!mei_cl_is_connected(cl))
  564. return -ENODEV;
  565. return fasync_helper(fd, file, band, &cl->ev_async);
  566. }
  567. /**
  568. * fw_status_show - mei device fw_status attribute show method
  569. *
  570. * @device: device pointer
  571. * @attr: attribute pointer
  572. * @buf: char out buffer
  573. *
  574. * Return: number of the bytes printed into buf or error
  575. */
  576. static ssize_t fw_status_show(struct device *device,
  577. struct device_attribute *attr, char *buf)
  578. {
  579. struct mei_device *dev = dev_get_drvdata(device);
  580. struct mei_fw_status fw_status;
  581. int err, i;
  582. ssize_t cnt = 0;
  583. mutex_lock(&dev->device_lock);
  584. err = mei_fw_status(dev, &fw_status);
  585. mutex_unlock(&dev->device_lock);
  586. if (err) {
  587. dev_err(device, "read fw_status error = %d\n", err);
  588. return err;
  589. }
  590. for (i = 0; i < fw_status.count; i++)
  591. cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
  592. fw_status.status[i]);
  593. return cnt;
  594. }
  595. static DEVICE_ATTR_RO(fw_status);
  596. /**
  597. * hbm_ver_show - display HBM protocol version negotiated with FW
  598. *
  599. * @device: device pointer
  600. * @attr: attribute pointer
  601. * @buf: char out buffer
  602. *
  603. * Return: number of the bytes printed into buf or error
  604. */
  605. static ssize_t hbm_ver_show(struct device *device,
  606. struct device_attribute *attr, char *buf)
  607. {
  608. struct mei_device *dev = dev_get_drvdata(device);
  609. struct hbm_version ver;
  610. mutex_lock(&dev->device_lock);
  611. ver = dev->version;
  612. mutex_unlock(&dev->device_lock);
  613. return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version);
  614. }
  615. static DEVICE_ATTR_RO(hbm_ver);
  616. /**
  617. * hbm_ver_drv_show - display HBM protocol version advertised by driver
  618. *
  619. * @device: device pointer
  620. * @attr: attribute pointer
  621. * @buf: char out buffer
  622. *
  623. * Return: number of the bytes printed into buf or error
  624. */
  625. static ssize_t hbm_ver_drv_show(struct device *device,
  626. struct device_attribute *attr, char *buf)
  627. {
  628. return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION);
  629. }
  630. static DEVICE_ATTR_RO(hbm_ver_drv);
  631. static struct attribute *mei_attrs[] = {
  632. &dev_attr_fw_status.attr,
  633. &dev_attr_hbm_ver.attr,
  634. &dev_attr_hbm_ver_drv.attr,
  635. NULL
  636. };
  637. ATTRIBUTE_GROUPS(mei);
  638. /*
  639. * file operations structure will be used for mei char device.
  640. */
  641. static const struct file_operations mei_fops = {
  642. .owner = THIS_MODULE,
  643. .read = mei_read,
  644. .unlocked_ioctl = mei_ioctl,
  645. #ifdef CONFIG_COMPAT
  646. .compat_ioctl = mei_compat_ioctl,
  647. #endif
  648. .open = mei_open,
  649. .release = mei_release,
  650. .write = mei_write,
  651. .poll = mei_poll,
  652. .fsync = mei_fsync,
  653. .fasync = mei_fasync,
  654. .llseek = no_llseek
  655. };
  656. static struct class *mei_class;
  657. static dev_t mei_devt;
  658. #define MEI_MAX_DEVS MINORMASK
  659. static DEFINE_MUTEX(mei_minor_lock);
  660. static DEFINE_IDR(mei_idr);
  661. /**
  662. * mei_minor_get - obtain next free device minor number
  663. *
  664. * @dev: device pointer
  665. *
  666. * Return: allocated minor, or -ENOSPC if no free minor left
  667. */
  668. static int mei_minor_get(struct mei_device *dev)
  669. {
  670. int ret;
  671. mutex_lock(&mei_minor_lock);
  672. ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
  673. if (ret >= 0)
  674. dev->minor = ret;
  675. else if (ret == -ENOSPC)
  676. dev_err(dev->dev, "too many mei devices\n");
  677. mutex_unlock(&mei_minor_lock);
  678. return ret;
  679. }
  680. /**
  681. * mei_minor_free - mark device minor number as free
  682. *
  683. * @dev: device pointer
  684. */
  685. static void mei_minor_free(struct mei_device *dev)
  686. {
  687. mutex_lock(&mei_minor_lock);
  688. idr_remove(&mei_idr, dev->minor);
  689. mutex_unlock(&mei_minor_lock);
  690. }
  691. int mei_register(struct mei_device *dev, struct device *parent)
  692. {
  693. struct device *clsdev; /* class device */
  694. int ret, devno;
  695. ret = mei_minor_get(dev);
  696. if (ret < 0)
  697. return ret;
  698. /* Fill in the data structures */
  699. devno = MKDEV(MAJOR(mei_devt), dev->minor);
  700. cdev_init(&dev->cdev, &mei_fops);
  701. dev->cdev.owner = parent->driver->owner;
  702. /* Add the device */
  703. ret = cdev_add(&dev->cdev, devno, 1);
  704. if (ret) {
  705. dev_err(parent, "unable to add device %d:%d\n",
  706. MAJOR(mei_devt), dev->minor);
  707. goto err_dev_add;
  708. }
  709. clsdev = device_create_with_groups(mei_class, parent, devno,
  710. dev, mei_groups,
  711. "mei%d", dev->minor);
  712. if (IS_ERR(clsdev)) {
  713. dev_err(parent, "unable to create device %d:%d\n",
  714. MAJOR(mei_devt), dev->minor);
  715. ret = PTR_ERR(clsdev);
  716. goto err_dev_create;
  717. }
  718. ret = mei_dbgfs_register(dev, dev_name(clsdev));
  719. if (ret) {
  720. dev_err(clsdev, "cannot register debugfs ret = %d\n", ret);
  721. goto err_dev_dbgfs;
  722. }
  723. return 0;
  724. err_dev_dbgfs:
  725. device_destroy(mei_class, devno);
  726. err_dev_create:
  727. cdev_del(&dev->cdev);
  728. err_dev_add:
  729. mei_minor_free(dev);
  730. return ret;
  731. }
  732. EXPORT_SYMBOL_GPL(mei_register);
  733. void mei_deregister(struct mei_device *dev)
  734. {
  735. int devno;
  736. devno = dev->cdev.dev;
  737. cdev_del(&dev->cdev);
  738. mei_dbgfs_deregister(dev);
  739. device_destroy(mei_class, devno);
  740. mei_minor_free(dev);
  741. }
  742. EXPORT_SYMBOL_GPL(mei_deregister);
  743. static int __init mei_init(void)
  744. {
  745. int ret;
  746. mei_class = class_create(THIS_MODULE, "mei");
  747. if (IS_ERR(mei_class)) {
  748. pr_err("couldn't create class\n");
  749. ret = PTR_ERR(mei_class);
  750. goto err;
  751. }
  752. ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
  753. if (ret < 0) {
  754. pr_err("unable to allocate char dev region\n");
  755. goto err_class;
  756. }
  757. ret = mei_cl_bus_init();
  758. if (ret < 0) {
  759. pr_err("unable to initialize bus\n");
  760. goto err_chrdev;
  761. }
  762. return 0;
  763. err_chrdev:
  764. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  765. err_class:
  766. class_destroy(mei_class);
  767. err:
  768. return ret;
  769. }
  770. static void __exit mei_exit(void)
  771. {
  772. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  773. class_destroy(mei_class);
  774. mei_cl_bus_exit();
  775. }
  776. module_init(mei_init);
  777. module_exit(mei_exit);
  778. MODULE_AUTHOR("Intel Corporation");
  779. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  780. MODULE_LICENSE("GPL v2");