main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/kernel.h>
  20. #include <linux/device.h>
  21. #include <linux/fs.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/aio.h>
  26. #include <linux/pci.h>
  27. #include <linux/poll.h>
  28. #include <linux/init.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/cdev.h>
  31. #include <linux/sched.h>
  32. #include <linux/uuid.h>
  33. #include <linux/compat.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/mei.h>
  38. #include "mei_dev.h"
  39. #include "hw-me.h"
  40. #include "client.h"
  41. /**
  42. * mei_open - the open function
  43. *
  44. * @inode: pointer to inode structure
  45. * @file: pointer to file structure
  46. *
  47. * returns 0 on success, <0 on error
  48. */
  49. static int mei_open(struct inode *inode, struct file *file)
  50. {
  51. struct miscdevice *misc = file->private_data;
  52. struct pci_dev *pdev;
  53. struct mei_cl *cl;
  54. struct mei_device *dev;
  55. int err;
  56. err = -ENODEV;
  57. if (!misc->parent)
  58. goto out;
  59. pdev = container_of(misc->parent, struct pci_dev, dev);
  60. dev = pci_get_drvdata(pdev);
  61. if (!dev)
  62. goto out;
  63. mutex_lock(&dev->device_lock);
  64. err = -ENOMEM;
  65. cl = mei_cl_allocate(dev);
  66. if (!cl)
  67. goto out_unlock;
  68. err = -ENODEV;
  69. if (dev->dev_state != MEI_DEV_ENABLED) {
  70. dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
  71. mei_dev_state_str(dev->dev_state));
  72. goto out_unlock;
  73. }
  74. err = -EMFILE;
  75. if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  76. dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
  77. MEI_MAX_OPEN_HANDLE_COUNT);
  78. goto out_unlock;
  79. }
  80. err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
  81. if (err)
  82. goto out_unlock;
  83. file->private_data = cl;
  84. mutex_unlock(&dev->device_lock);
  85. return nonseekable_open(inode, file);
  86. out_unlock:
  87. mutex_unlock(&dev->device_lock);
  88. kfree(cl);
  89. out:
  90. return err;
  91. }
  92. /**
  93. * mei_release - the release function
  94. *
  95. * @inode: pointer to inode structure
  96. * @file: pointer to file structure
  97. *
  98. * returns 0 on success, <0 on error
  99. */
  100. static int mei_release(struct inode *inode, struct file *file)
  101. {
  102. struct mei_cl *cl = file->private_data;
  103. struct mei_cl_cb *cb;
  104. struct mei_device *dev;
  105. int rets = 0;
  106. if (WARN_ON(!cl || !cl->dev))
  107. return -ENODEV;
  108. dev = cl->dev;
  109. mutex_lock(&dev->device_lock);
  110. if (cl == &dev->iamthif_cl) {
  111. rets = mei_amthif_release(dev, file);
  112. goto out;
  113. }
  114. if (cl->state == MEI_FILE_CONNECTED) {
  115. cl->state = MEI_FILE_DISCONNECTING;
  116. dev_dbg(&dev->pdev->dev,
  117. "disconnecting client host client = %d, "
  118. "ME client = %d\n",
  119. cl->host_client_id,
  120. cl->me_client_id);
  121. rets = mei_cl_disconnect(cl);
  122. }
  123. mei_cl_flush_queues(cl);
  124. dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
  125. cl->host_client_id,
  126. cl->me_client_id);
  127. if (dev->open_handle_count > 0) {
  128. clear_bit(cl->host_client_id, dev->host_clients_map);
  129. dev->open_handle_count--;
  130. }
  131. mei_cl_unlink(cl);
  132. /* free read cb */
  133. cb = NULL;
  134. if (cl->read_cb) {
  135. cb = mei_cl_find_read_cb(cl);
  136. /* Remove entry from read list */
  137. if (cb)
  138. list_del(&cb->list);
  139. cb = cl->read_cb;
  140. cl->read_cb = NULL;
  141. }
  142. file->private_data = NULL;
  143. if (cb) {
  144. mei_io_cb_free(cb);
  145. cb = NULL;
  146. }
  147. kfree(cl);
  148. out:
  149. mutex_unlock(&dev->device_lock);
  150. return rets;
  151. }
  152. /**
  153. * mei_read - the read function.
  154. *
  155. * @file: pointer to file structure
  156. * @ubuf: pointer to user buffer
  157. * @length: buffer length
  158. * @offset: data offset in buffer
  159. *
  160. * returns >=0 data length on success , <0 on error
  161. */
  162. static ssize_t mei_read(struct file *file, char __user *ubuf,
  163. size_t length, loff_t *offset)
  164. {
  165. struct mei_cl *cl = file->private_data;
  166. struct mei_cl_cb *cb_pos = NULL;
  167. struct mei_cl_cb *cb = NULL;
  168. struct mei_device *dev;
  169. int i;
  170. int rets;
  171. int err;
  172. if (WARN_ON(!cl || !cl->dev))
  173. return -ENODEV;
  174. dev = cl->dev;
  175. mutex_lock(&dev->device_lock);
  176. if (dev->dev_state != MEI_DEV_ENABLED) {
  177. rets = -ENODEV;
  178. goto out;
  179. }
  180. if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
  181. /* Do not allow to read watchdog client */
  182. i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
  183. if (i >= 0) {
  184. struct mei_me_client *me_client = &dev->me_clients[i];
  185. if (cl->me_client_id == me_client->client_id) {
  186. rets = -EBADF;
  187. goto out;
  188. }
  189. }
  190. } else {
  191. cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
  192. }
  193. if (cl == &dev->iamthif_cl) {
  194. rets = mei_amthif_read(dev, file, ubuf, length, offset);
  195. goto out;
  196. }
  197. if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
  198. cb = cl->read_cb;
  199. goto copy_buffer;
  200. } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
  201. cl->read_cb->buf_idx <= *offset) {
  202. cb = cl->read_cb;
  203. rets = 0;
  204. goto free;
  205. } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
  206. /*Offset needs to be cleaned for contiguous reads*/
  207. *offset = 0;
  208. rets = 0;
  209. goto out;
  210. }
  211. err = mei_cl_read_start(cl);
  212. if (err && err != -EBUSY) {
  213. dev_dbg(&dev->pdev->dev,
  214. "mei start read failure with status = %d\n", err);
  215. rets = err;
  216. goto out;
  217. }
  218. if (MEI_READ_COMPLETE != cl->reading_state &&
  219. !waitqueue_active(&cl->rx_wait)) {
  220. if (file->f_flags & O_NONBLOCK) {
  221. rets = -EAGAIN;
  222. goto out;
  223. }
  224. mutex_unlock(&dev->device_lock);
  225. if (wait_event_interruptible(cl->rx_wait,
  226. (MEI_READ_COMPLETE == cl->reading_state ||
  227. MEI_FILE_INITIALIZING == cl->state ||
  228. MEI_FILE_DISCONNECTED == cl->state ||
  229. MEI_FILE_DISCONNECTING == cl->state))) {
  230. if (signal_pending(current))
  231. return -EINTR;
  232. return -ERESTARTSYS;
  233. }
  234. mutex_lock(&dev->device_lock);
  235. if (MEI_FILE_INITIALIZING == cl->state ||
  236. MEI_FILE_DISCONNECTED == cl->state ||
  237. MEI_FILE_DISCONNECTING == cl->state) {
  238. rets = -EBUSY;
  239. goto out;
  240. }
  241. }
  242. cb = cl->read_cb;
  243. if (!cb) {
  244. rets = -ENODEV;
  245. goto out;
  246. }
  247. if (cl->reading_state != MEI_READ_COMPLETE) {
  248. rets = 0;
  249. goto out;
  250. }
  251. /* now copy the data to user space */
  252. copy_buffer:
  253. dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
  254. cb->response_buffer.size);
  255. dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
  256. if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
  257. rets = -EMSGSIZE;
  258. goto free;
  259. }
  260. /* length is being truncated to PAGE_SIZE,
  261. * however buf_idx may point beyond that */
  262. length = min_t(size_t, length, cb->buf_idx - *offset);
  263. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
  264. rets = -EFAULT;
  265. goto free;
  266. }
  267. rets = length;
  268. *offset += length;
  269. if ((unsigned long)*offset < cb->buf_idx)
  270. goto out;
  271. free:
  272. cb_pos = mei_cl_find_read_cb(cl);
  273. /* Remove entry from read list */
  274. if (cb_pos)
  275. list_del(&cb_pos->list);
  276. mei_io_cb_free(cb);
  277. cl->reading_state = MEI_IDLE;
  278. cl->read_cb = NULL;
  279. out:
  280. dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
  281. mutex_unlock(&dev->device_lock);
  282. return rets;
  283. }
  284. /**
  285. * mei_write - the write function.
  286. *
  287. * @file: pointer to file structure
  288. * @ubuf: pointer to user buffer
  289. * @length: buffer length
  290. * @offset: data offset in buffer
  291. *
  292. * returns >=0 data length on success , <0 on error
  293. */
  294. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  295. size_t length, loff_t *offset)
  296. {
  297. struct mei_cl *cl = file->private_data;
  298. struct mei_cl_cb *write_cb = NULL;
  299. struct mei_msg_hdr mei_hdr;
  300. struct mei_device *dev;
  301. unsigned long timeout = 0;
  302. int rets;
  303. int i;
  304. if (WARN_ON(!cl || !cl->dev))
  305. return -ENODEV;
  306. dev = cl->dev;
  307. mutex_lock(&dev->device_lock);
  308. if (dev->dev_state != MEI_DEV_ENABLED) {
  309. rets = -ENODEV;
  310. goto err;
  311. }
  312. i = mei_me_cl_by_id(dev, cl->me_client_id);
  313. if (i < 0) {
  314. rets = -ENODEV;
  315. goto err;
  316. }
  317. if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
  318. rets = -EMSGSIZE;
  319. goto err;
  320. }
  321. if (cl->state != MEI_FILE_CONNECTED) {
  322. rets = -ENODEV;
  323. dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
  324. cl->host_client_id, cl->me_client_id);
  325. goto err;
  326. }
  327. if (cl == &dev->iamthif_cl) {
  328. write_cb = mei_amthif_find_read_list_entry(dev, file);
  329. if (write_cb) {
  330. timeout = write_cb->read_time +
  331. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  332. if (time_after(jiffies, timeout) ||
  333. cl->reading_state == MEI_READ_COMPLETE) {
  334. *offset = 0;
  335. list_del(&write_cb->list);
  336. mei_io_cb_free(write_cb);
  337. write_cb = NULL;
  338. }
  339. }
  340. }
  341. /* free entry used in read */
  342. if (cl->reading_state == MEI_READ_COMPLETE) {
  343. *offset = 0;
  344. write_cb = mei_cl_find_read_cb(cl);
  345. if (write_cb) {
  346. list_del(&write_cb->list);
  347. mei_io_cb_free(write_cb);
  348. write_cb = NULL;
  349. cl->reading_state = MEI_IDLE;
  350. cl->read_cb = NULL;
  351. }
  352. } else if (cl->reading_state == MEI_IDLE)
  353. *offset = 0;
  354. write_cb = mei_io_cb_init(cl, file);
  355. if (!write_cb) {
  356. dev_err(&dev->pdev->dev, "write cb allocation failed\n");
  357. rets = -ENOMEM;
  358. goto err;
  359. }
  360. rets = mei_io_cb_alloc_req_buf(write_cb, length);
  361. if (rets)
  362. goto err;
  363. dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
  364. rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
  365. if (rets)
  366. goto err;
  367. cl->sm_state = 0;
  368. if (length == 4 &&
  369. ((memcmp(mei_wd_state_independence_msg[0],
  370. write_cb->request_buffer.data, 4) == 0) ||
  371. (memcmp(mei_wd_state_independence_msg[1],
  372. write_cb->request_buffer.data, 4) == 0) ||
  373. (memcmp(mei_wd_state_independence_msg[2],
  374. write_cb->request_buffer.data, 4) == 0)))
  375. cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
  376. if (cl == &dev->iamthif_cl) {
  377. rets = mei_amthif_write(dev, write_cb);
  378. if (rets) {
  379. dev_err(&dev->pdev->dev,
  380. "amthif write failed with status = %d\n", rets);
  381. goto err;
  382. }
  383. mutex_unlock(&dev->device_lock);
  384. return length;
  385. }
  386. write_cb->fop_type = MEI_FOP_WRITE;
  387. dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
  388. cl->host_client_id, cl->me_client_id);
  389. rets = mei_cl_flow_ctrl_creds(cl);
  390. if (rets < 0)
  391. goto err;
  392. if (rets == 0 || !dev->hbuf_is_ready) {
  393. write_cb->buf_idx = 0;
  394. mei_hdr.msg_complete = 0;
  395. cl->writing_state = MEI_WRITING;
  396. goto out;
  397. }
  398. dev->hbuf_is_ready = false;
  399. if (length > mei_hbuf_max_len(dev)) {
  400. mei_hdr.length = mei_hbuf_max_len(dev);
  401. mei_hdr.msg_complete = 0;
  402. } else {
  403. mei_hdr.length = length;
  404. mei_hdr.msg_complete = 1;
  405. }
  406. mei_hdr.host_addr = cl->host_client_id;
  407. mei_hdr.me_addr = cl->me_client_id;
  408. mei_hdr.reserved = 0;
  409. dev_dbg(&dev->pdev->dev, "write " MEI_HDR_FMT "\n",
  410. MEI_HDR_PRM(&mei_hdr));
  411. if (mei_write_message(dev, &mei_hdr, write_cb->request_buffer.data)) {
  412. rets = -ENODEV;
  413. goto err;
  414. }
  415. cl->writing_state = MEI_WRITING;
  416. write_cb->buf_idx = mei_hdr.length;
  417. out:
  418. if (mei_hdr.msg_complete) {
  419. if (mei_cl_flow_ctrl_reduce(cl)) {
  420. rets = -ENODEV;
  421. goto err;
  422. }
  423. list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
  424. } else {
  425. list_add_tail(&write_cb->list, &dev->write_list.list);
  426. }
  427. mutex_unlock(&dev->device_lock);
  428. return length;
  429. err:
  430. mutex_unlock(&dev->device_lock);
  431. mei_io_cb_free(write_cb);
  432. return rets;
  433. }
  434. /**
  435. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  436. *
  437. * @dev: the device structure
  438. * @data: IOCTL connect data, input and output parameters
  439. * @file: private data of the file object
  440. *
  441. * Locking: called under "dev->device_lock" lock
  442. *
  443. * returns 0 on success, <0 on failure.
  444. */
  445. static int mei_ioctl_connect_client(struct file *file,
  446. struct mei_connect_client_data *data)
  447. {
  448. struct mei_device *dev;
  449. struct mei_client *client;
  450. struct mei_cl *cl;
  451. int i;
  452. int rets;
  453. cl = file->private_data;
  454. if (WARN_ON(!cl || !cl->dev))
  455. return -ENODEV;
  456. dev = cl->dev;
  457. if (dev->dev_state != MEI_DEV_ENABLED) {
  458. rets = -ENODEV;
  459. goto end;
  460. }
  461. if (cl->state != MEI_FILE_INITIALIZING &&
  462. cl->state != MEI_FILE_DISCONNECTED) {
  463. rets = -EBUSY;
  464. goto end;
  465. }
  466. /* find ME client we're trying to connect to */
  467. i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
  468. if (i >= 0 && !dev->me_clients[i].props.fixed_address) {
  469. cl->me_client_id = dev->me_clients[i].client_id;
  470. cl->state = MEI_FILE_CONNECTING;
  471. }
  472. dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
  473. cl->me_client_id);
  474. dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
  475. dev->me_clients[i].props.protocol_version);
  476. dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
  477. dev->me_clients[i].props.max_msg_length);
  478. /* if we're connecting to amthif client then we will use the
  479. * existing connection
  480. */
  481. if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
  482. dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
  483. if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
  484. rets = -ENODEV;
  485. goto end;
  486. }
  487. clear_bit(cl->host_client_id, dev->host_clients_map);
  488. mei_cl_unlink(cl);
  489. kfree(cl);
  490. cl = NULL;
  491. file->private_data = &dev->iamthif_cl;
  492. client = &data->out_client_properties;
  493. client->max_msg_length =
  494. dev->me_clients[i].props.max_msg_length;
  495. client->protocol_version =
  496. dev->me_clients[i].props.protocol_version;
  497. rets = dev->iamthif_cl.status;
  498. goto end;
  499. }
  500. if (cl->state != MEI_FILE_CONNECTING) {
  501. rets = -ENODEV;
  502. goto end;
  503. }
  504. /* prepare the output buffer */
  505. client = &data->out_client_properties;
  506. client->max_msg_length = dev->me_clients[i].props.max_msg_length;
  507. client->protocol_version = dev->me_clients[i].props.protocol_version;
  508. dev_dbg(&dev->pdev->dev, "Can connect?\n");
  509. rets = mei_cl_connect(cl, file);
  510. end:
  511. dev_dbg(&dev->pdev->dev, "free connect cb memory.");
  512. return rets;
  513. }
  514. /**
  515. * mei_ioctl - the IOCTL function
  516. *
  517. * @file: pointer to file structure
  518. * @cmd: ioctl command
  519. * @data: pointer to mei message structure
  520. *
  521. * returns 0 on success , <0 on error
  522. */
  523. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  524. {
  525. struct mei_device *dev;
  526. struct mei_cl *cl = file->private_data;
  527. struct mei_connect_client_data *connect_data = NULL;
  528. int rets;
  529. if (cmd != IOCTL_MEI_CONNECT_CLIENT)
  530. return -EINVAL;
  531. if (WARN_ON(!cl || !cl->dev))
  532. return -ENODEV;
  533. dev = cl->dev;
  534. dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
  535. mutex_lock(&dev->device_lock);
  536. if (dev->dev_state != MEI_DEV_ENABLED) {
  537. rets = -ENODEV;
  538. goto out;
  539. }
  540. dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  541. connect_data = kzalloc(sizeof(struct mei_connect_client_data),
  542. GFP_KERNEL);
  543. if (!connect_data) {
  544. rets = -ENOMEM;
  545. goto out;
  546. }
  547. dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
  548. if (copy_from_user(connect_data, (char __user *)data,
  549. sizeof(struct mei_connect_client_data))) {
  550. dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
  551. rets = -EFAULT;
  552. goto out;
  553. }
  554. rets = mei_ioctl_connect_client(file, connect_data);
  555. /* if all is ok, copying the data back to user. */
  556. if (rets)
  557. goto out;
  558. dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
  559. if (copy_to_user((char __user *)data, connect_data,
  560. sizeof(struct mei_connect_client_data))) {
  561. dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
  562. rets = -EFAULT;
  563. goto out;
  564. }
  565. out:
  566. kfree(connect_data);
  567. mutex_unlock(&dev->device_lock);
  568. return rets;
  569. }
  570. /**
  571. * mei_compat_ioctl - the compat IOCTL function
  572. *
  573. * @file: pointer to file structure
  574. * @cmd: ioctl command
  575. * @data: pointer to mei message structure
  576. *
  577. * returns 0 on success , <0 on error
  578. */
  579. #ifdef CONFIG_COMPAT
  580. static long mei_compat_ioctl(struct file *file,
  581. unsigned int cmd, unsigned long data)
  582. {
  583. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  584. }
  585. #endif
  586. /**
  587. * mei_poll - the poll function
  588. *
  589. * @file: pointer to file structure
  590. * @wait: pointer to poll_table structure
  591. *
  592. * returns poll mask
  593. */
  594. static unsigned int mei_poll(struct file *file, poll_table *wait)
  595. {
  596. struct mei_cl *cl = file->private_data;
  597. struct mei_device *dev;
  598. unsigned int mask = 0;
  599. if (WARN_ON(!cl || !cl->dev))
  600. return mask;
  601. dev = cl->dev;
  602. mutex_lock(&dev->device_lock);
  603. if (dev->dev_state != MEI_DEV_ENABLED)
  604. goto out;
  605. if (cl == &dev->iamthif_cl) {
  606. mask = mei_amthif_poll(dev, file, wait);
  607. goto out;
  608. }
  609. mutex_unlock(&dev->device_lock);
  610. poll_wait(file, &cl->tx_wait, wait);
  611. mutex_lock(&dev->device_lock);
  612. if (MEI_WRITE_COMPLETE == cl->writing_state)
  613. mask |= (POLLIN | POLLRDNORM);
  614. out:
  615. mutex_unlock(&dev->device_lock);
  616. return mask;
  617. }
  618. /*
  619. * file operations structure will be used for mei char device.
  620. */
  621. static const struct file_operations mei_fops = {
  622. .owner = THIS_MODULE,
  623. .read = mei_read,
  624. .unlocked_ioctl = mei_ioctl,
  625. #ifdef CONFIG_COMPAT
  626. .compat_ioctl = mei_compat_ioctl,
  627. #endif
  628. .open = mei_open,
  629. .release = mei_release,
  630. .write = mei_write,
  631. .poll = mei_poll,
  632. .llseek = no_llseek
  633. };
  634. /*
  635. * Misc Device Struct
  636. */
  637. static struct miscdevice mei_misc_device = {
  638. .name = "mei",
  639. .fops = &mei_fops,
  640. .minor = MISC_DYNAMIC_MINOR,
  641. };
  642. int mei_register(struct device *dev)
  643. {
  644. mei_misc_device.parent = dev;
  645. return misc_register(&mei_misc_device);
  646. }
  647. void mei_deregister(void)
  648. {
  649. misc_deregister(&mei_misc_device);
  650. mei_misc_device.parent = NULL;
  651. }
  652. MODULE_LICENSE("GPL v2");