tpm_vtpm_proxy.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright (C) 2015, 2016 IBM Corporation
  3. * Copyright (C) 2016 Intel Corporation
  4. *
  5. * Author: Stefan Berger <stefanb@us.ibm.com>
  6. *
  7. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  8. *
  9. * Device driver for vTPM (vTPM proxy driver)
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation, version 2 of the
  14. * License.
  15. *
  16. */
  17. #include <linux/types.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/wait.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/vtpm_proxy.h>
  23. #include <linux/file.h>
  24. #include <linux/anon_inodes.h>
  25. #include <linux/poll.h>
  26. #include <linux/compat.h>
  27. #include "tpm.h"
  28. #define VTPM_PROXY_REQ_COMPLETE_FLAG BIT(0)
  29. struct proxy_dev {
  30. struct tpm_chip *chip;
  31. u32 flags; /* public API flags */
  32. wait_queue_head_t wq;
  33. struct mutex buf_lock; /* protect buffer and flags */
  34. long state; /* internal state */
  35. #define STATE_OPENED_FLAG BIT(0)
  36. #define STATE_WAIT_RESPONSE_FLAG BIT(1) /* waiting for emulator response */
  37. #define STATE_REGISTERED_FLAG BIT(2)
  38. #define STATE_DRIVER_COMMAND BIT(3) /* sending a driver specific command */
  39. size_t req_len; /* length of queued TPM request */
  40. size_t resp_len; /* length of queued TPM response */
  41. u8 buffer[TPM_BUFSIZE]; /* request/response buffer */
  42. struct work_struct work; /* task that retrieves TPM timeouts */
  43. };
  44. /* all supported flags */
  45. #define VTPM_PROXY_FLAGS_ALL (VTPM_PROXY_FLAG_TPM2)
  46. static struct workqueue_struct *workqueue;
  47. static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev);
  48. /*
  49. * Functions related to 'server side'
  50. */
  51. /**
  52. * vtpm_proxy_fops_read - Read TPM commands on 'server side'
  53. *
  54. * @filp: file pointer
  55. * @buf: read buffer
  56. * @count: number of bytes to read
  57. * @off: offset
  58. *
  59. * Return:
  60. * Number of bytes read or negative error code
  61. */
  62. static ssize_t vtpm_proxy_fops_read(struct file *filp, char __user *buf,
  63. size_t count, loff_t *off)
  64. {
  65. struct proxy_dev *proxy_dev = filp->private_data;
  66. size_t len;
  67. int sig, rc;
  68. sig = wait_event_interruptible(proxy_dev->wq,
  69. proxy_dev->req_len != 0 ||
  70. !(proxy_dev->state & STATE_OPENED_FLAG));
  71. if (sig)
  72. return -EINTR;
  73. mutex_lock(&proxy_dev->buf_lock);
  74. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  75. mutex_unlock(&proxy_dev->buf_lock);
  76. return -EPIPE;
  77. }
  78. len = proxy_dev->req_len;
  79. if (count < len) {
  80. mutex_unlock(&proxy_dev->buf_lock);
  81. pr_debug("Invalid size in recv: count=%zd, req_len=%zd\n",
  82. count, len);
  83. return -EIO;
  84. }
  85. rc = copy_to_user(buf, proxy_dev->buffer, len);
  86. memset(proxy_dev->buffer, 0, len);
  87. proxy_dev->req_len = 0;
  88. if (!rc)
  89. proxy_dev->state |= STATE_WAIT_RESPONSE_FLAG;
  90. mutex_unlock(&proxy_dev->buf_lock);
  91. if (rc)
  92. return -EFAULT;
  93. return len;
  94. }
  95. /**
  96. * vtpm_proxy_fops_write - Write TPM responses on 'server side'
  97. *
  98. * @filp: file pointer
  99. * @buf: write buffer
  100. * @count: number of bytes to write
  101. * @off: offset
  102. *
  103. * Return:
  104. * Number of bytes read or negative error value
  105. */
  106. static ssize_t vtpm_proxy_fops_write(struct file *filp, const char __user *buf,
  107. size_t count, loff_t *off)
  108. {
  109. struct proxy_dev *proxy_dev = filp->private_data;
  110. mutex_lock(&proxy_dev->buf_lock);
  111. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  112. mutex_unlock(&proxy_dev->buf_lock);
  113. return -EPIPE;
  114. }
  115. if (count > sizeof(proxy_dev->buffer) ||
  116. !(proxy_dev->state & STATE_WAIT_RESPONSE_FLAG)) {
  117. mutex_unlock(&proxy_dev->buf_lock);
  118. return -EIO;
  119. }
  120. proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
  121. proxy_dev->req_len = 0;
  122. if (copy_from_user(proxy_dev->buffer, buf, count)) {
  123. mutex_unlock(&proxy_dev->buf_lock);
  124. return -EFAULT;
  125. }
  126. proxy_dev->resp_len = count;
  127. mutex_unlock(&proxy_dev->buf_lock);
  128. wake_up_interruptible(&proxy_dev->wq);
  129. return count;
  130. }
  131. /*
  132. * vtpm_proxy_fops_poll - Poll status on 'server side'
  133. *
  134. * @filp: file pointer
  135. * @wait: poll table
  136. *
  137. * Return: Poll flags
  138. */
  139. static __poll_t vtpm_proxy_fops_poll(struct file *filp, poll_table *wait)
  140. {
  141. struct proxy_dev *proxy_dev = filp->private_data;
  142. __poll_t ret;
  143. poll_wait(filp, &proxy_dev->wq, wait);
  144. ret = EPOLLOUT;
  145. mutex_lock(&proxy_dev->buf_lock);
  146. if (proxy_dev->req_len)
  147. ret |= EPOLLIN | EPOLLRDNORM;
  148. if (!(proxy_dev->state & STATE_OPENED_FLAG))
  149. ret |= EPOLLHUP;
  150. mutex_unlock(&proxy_dev->buf_lock);
  151. return ret;
  152. }
  153. /*
  154. * vtpm_proxy_fops_open - Open vTPM device on 'server side'
  155. *
  156. * @filp: file pointer
  157. *
  158. * Called when setting up the anonymous file descriptor
  159. */
  160. static void vtpm_proxy_fops_open(struct file *filp)
  161. {
  162. struct proxy_dev *proxy_dev = filp->private_data;
  163. proxy_dev->state |= STATE_OPENED_FLAG;
  164. }
  165. /**
  166. * vtpm_proxy_fops_undo_open - counter-part to vtpm_fops_open
  167. * Call to undo vtpm_proxy_fops_open
  168. *
  169. *@proxy_dev: tpm proxy device
  170. */
  171. static void vtpm_proxy_fops_undo_open(struct proxy_dev *proxy_dev)
  172. {
  173. mutex_lock(&proxy_dev->buf_lock);
  174. proxy_dev->state &= ~STATE_OPENED_FLAG;
  175. mutex_unlock(&proxy_dev->buf_lock);
  176. /* no more TPM responses -- wake up anyone waiting for them */
  177. wake_up_interruptible(&proxy_dev->wq);
  178. }
  179. /*
  180. * vtpm_proxy_fops_release - Close 'server side'
  181. *
  182. * @inode: inode
  183. * @filp: file pointer
  184. * Return:
  185. * Always returns 0.
  186. */
  187. static int vtpm_proxy_fops_release(struct inode *inode, struct file *filp)
  188. {
  189. struct proxy_dev *proxy_dev = filp->private_data;
  190. filp->private_data = NULL;
  191. vtpm_proxy_delete_device(proxy_dev);
  192. return 0;
  193. }
  194. static const struct file_operations vtpm_proxy_fops = {
  195. .owner = THIS_MODULE,
  196. .llseek = no_llseek,
  197. .read = vtpm_proxy_fops_read,
  198. .write = vtpm_proxy_fops_write,
  199. .poll = vtpm_proxy_fops_poll,
  200. .release = vtpm_proxy_fops_release,
  201. };
  202. /*
  203. * Functions invoked by the core TPM driver to send TPM commands to
  204. * 'server side' and receive responses from there.
  205. */
  206. /*
  207. * Called when core TPM driver reads TPM responses from 'server side'
  208. *
  209. * @chip: tpm chip to use
  210. * @buf: receive buffer
  211. * @count: bytes to read
  212. * Return:
  213. * Number of TPM response bytes read, negative error value otherwise
  214. */
  215. static int vtpm_proxy_tpm_op_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  216. {
  217. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  218. size_t len;
  219. /* process gone ? */
  220. mutex_lock(&proxy_dev->buf_lock);
  221. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  222. mutex_unlock(&proxy_dev->buf_lock);
  223. return -EPIPE;
  224. }
  225. len = proxy_dev->resp_len;
  226. if (count < len) {
  227. dev_err(&chip->dev,
  228. "Invalid size in recv: count=%zd, resp_len=%zd\n",
  229. count, len);
  230. len = -EIO;
  231. goto out;
  232. }
  233. memcpy(buf, proxy_dev->buffer, len);
  234. proxy_dev->resp_len = 0;
  235. out:
  236. mutex_unlock(&proxy_dev->buf_lock);
  237. return len;
  238. }
  239. static int vtpm_proxy_is_driver_command(struct tpm_chip *chip,
  240. u8 *buf, size_t count)
  241. {
  242. struct tpm_input_header *hdr = (struct tpm_input_header *)buf;
  243. if (count < sizeof(struct tpm_input_header))
  244. return 0;
  245. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  246. switch (be32_to_cpu(hdr->ordinal)) {
  247. case TPM2_CC_SET_LOCALITY:
  248. return 1;
  249. }
  250. } else {
  251. switch (be32_to_cpu(hdr->ordinal)) {
  252. case TPM_ORD_SET_LOCALITY:
  253. return 1;
  254. }
  255. }
  256. return 0;
  257. }
  258. /*
  259. * Called when core TPM driver forwards TPM requests to 'server side'.
  260. *
  261. * @chip: tpm chip to use
  262. * @buf: send buffer
  263. * @count: bytes to send
  264. *
  265. * Return:
  266. * 0 in case of success, negative error value otherwise.
  267. */
  268. static int vtpm_proxy_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t count)
  269. {
  270. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  271. int rc = 0;
  272. if (count > sizeof(proxy_dev->buffer)) {
  273. dev_err(&chip->dev,
  274. "Invalid size in send: count=%zd, buffer size=%zd\n",
  275. count, sizeof(proxy_dev->buffer));
  276. return -EIO;
  277. }
  278. if (!(proxy_dev->state & STATE_DRIVER_COMMAND) &&
  279. vtpm_proxy_is_driver_command(chip, buf, count))
  280. return -EFAULT;
  281. mutex_lock(&proxy_dev->buf_lock);
  282. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  283. mutex_unlock(&proxy_dev->buf_lock);
  284. return -EPIPE;
  285. }
  286. proxy_dev->resp_len = 0;
  287. proxy_dev->req_len = count;
  288. memcpy(proxy_dev->buffer, buf, count);
  289. proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
  290. mutex_unlock(&proxy_dev->buf_lock);
  291. wake_up_interruptible(&proxy_dev->wq);
  292. return rc;
  293. }
  294. static void vtpm_proxy_tpm_op_cancel(struct tpm_chip *chip)
  295. {
  296. /* not supported */
  297. }
  298. static u8 vtpm_proxy_tpm_op_status(struct tpm_chip *chip)
  299. {
  300. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  301. if (proxy_dev->resp_len)
  302. return VTPM_PROXY_REQ_COMPLETE_FLAG;
  303. return 0;
  304. }
  305. static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)
  306. {
  307. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  308. bool ret;
  309. mutex_lock(&proxy_dev->buf_lock);
  310. ret = !(proxy_dev->state & STATE_OPENED_FLAG);
  311. mutex_unlock(&proxy_dev->buf_lock);
  312. return ret;
  313. }
  314. static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
  315. {
  316. struct tpm_buf buf;
  317. int rc;
  318. const struct tpm_output_header *header;
  319. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  320. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  321. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS,
  322. TPM2_CC_SET_LOCALITY);
  323. else
  324. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND,
  325. TPM_ORD_SET_LOCALITY);
  326. if (rc)
  327. return rc;
  328. tpm_buf_append_u8(&buf, locality);
  329. proxy_dev->state |= STATE_DRIVER_COMMAND;
  330. rc = tpm_transmit_cmd(chip, NULL, buf.data, tpm_buf_length(&buf), 0,
  331. TPM_TRANSMIT_NESTED,
  332. "attempting to set locality");
  333. proxy_dev->state &= ~STATE_DRIVER_COMMAND;
  334. if (rc < 0) {
  335. locality = rc;
  336. goto out;
  337. }
  338. header = (const struct tpm_output_header *)buf.data;
  339. rc = be32_to_cpu(header->return_code);
  340. if (rc)
  341. locality = -1;
  342. out:
  343. tpm_buf_destroy(&buf);
  344. return locality;
  345. }
  346. static const struct tpm_class_ops vtpm_proxy_tpm_ops = {
  347. .flags = TPM_OPS_AUTO_STARTUP,
  348. .recv = vtpm_proxy_tpm_op_recv,
  349. .send = vtpm_proxy_tpm_op_send,
  350. .cancel = vtpm_proxy_tpm_op_cancel,
  351. .status = vtpm_proxy_tpm_op_status,
  352. .req_complete_mask = VTPM_PROXY_REQ_COMPLETE_FLAG,
  353. .req_complete_val = VTPM_PROXY_REQ_COMPLETE_FLAG,
  354. .req_canceled = vtpm_proxy_tpm_req_canceled,
  355. .request_locality = vtpm_proxy_request_locality,
  356. };
  357. /*
  358. * Code related to the startup of the TPM 2 and startup of TPM 1.2 +
  359. * retrieval of timeouts and durations.
  360. */
  361. static void vtpm_proxy_work(struct work_struct *work)
  362. {
  363. struct proxy_dev *proxy_dev = container_of(work, struct proxy_dev,
  364. work);
  365. int rc;
  366. rc = tpm_chip_register(proxy_dev->chip);
  367. if (rc)
  368. vtpm_proxy_fops_undo_open(proxy_dev);
  369. else
  370. proxy_dev->state |= STATE_REGISTERED_FLAG;
  371. }
  372. /*
  373. * vtpm_proxy_work_stop: make sure the work has finished
  374. *
  375. * This function is useful when user space closed the fd
  376. * while the driver still determines timeouts.
  377. */
  378. static void vtpm_proxy_work_stop(struct proxy_dev *proxy_dev)
  379. {
  380. vtpm_proxy_fops_undo_open(proxy_dev);
  381. flush_work(&proxy_dev->work);
  382. }
  383. /*
  384. * vtpm_proxy_work_start: Schedule the work for TPM 1.2 & 2 initialization
  385. */
  386. static inline void vtpm_proxy_work_start(struct proxy_dev *proxy_dev)
  387. {
  388. queue_work(workqueue, &proxy_dev->work);
  389. }
  390. /*
  391. * Code related to creation and deletion of device pairs
  392. */
  393. static struct proxy_dev *vtpm_proxy_create_proxy_dev(void)
  394. {
  395. struct proxy_dev *proxy_dev;
  396. struct tpm_chip *chip;
  397. int err;
  398. proxy_dev = kzalloc(sizeof(*proxy_dev), GFP_KERNEL);
  399. if (proxy_dev == NULL)
  400. return ERR_PTR(-ENOMEM);
  401. init_waitqueue_head(&proxy_dev->wq);
  402. mutex_init(&proxy_dev->buf_lock);
  403. INIT_WORK(&proxy_dev->work, vtpm_proxy_work);
  404. chip = tpm_chip_alloc(NULL, &vtpm_proxy_tpm_ops);
  405. if (IS_ERR(chip)) {
  406. err = PTR_ERR(chip);
  407. goto err_proxy_dev_free;
  408. }
  409. dev_set_drvdata(&chip->dev, proxy_dev);
  410. proxy_dev->chip = chip;
  411. return proxy_dev;
  412. err_proxy_dev_free:
  413. kfree(proxy_dev);
  414. return ERR_PTR(err);
  415. }
  416. /*
  417. * Undo what has been done in vtpm_create_proxy_dev
  418. */
  419. static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev *proxy_dev)
  420. {
  421. put_device(&proxy_dev->chip->dev); /* frees chip */
  422. kfree(proxy_dev);
  423. }
  424. /*
  425. * Create a /dev/tpm%d and 'server side' file descriptor pair
  426. *
  427. * Return:
  428. * Returns file pointer on success, an error value otherwise
  429. */
  430. static struct file *vtpm_proxy_create_device(
  431. struct vtpm_proxy_new_dev *vtpm_new_dev)
  432. {
  433. struct proxy_dev *proxy_dev;
  434. int rc, fd;
  435. struct file *file;
  436. if (vtpm_new_dev->flags & ~VTPM_PROXY_FLAGS_ALL)
  437. return ERR_PTR(-EOPNOTSUPP);
  438. proxy_dev = vtpm_proxy_create_proxy_dev();
  439. if (IS_ERR(proxy_dev))
  440. return ERR_CAST(proxy_dev);
  441. proxy_dev->flags = vtpm_new_dev->flags;
  442. /* setup an anonymous file for the server-side */
  443. fd = get_unused_fd_flags(O_RDWR);
  444. if (fd < 0) {
  445. rc = fd;
  446. goto err_delete_proxy_dev;
  447. }
  448. file = anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
  449. O_RDWR);
  450. if (IS_ERR(file)) {
  451. rc = PTR_ERR(file);
  452. goto err_put_unused_fd;
  453. }
  454. /* from now on we can unwind with put_unused_fd() + fput() */
  455. /* simulate an open() on the server side */
  456. vtpm_proxy_fops_open(file);
  457. if (proxy_dev->flags & VTPM_PROXY_FLAG_TPM2)
  458. proxy_dev->chip->flags |= TPM_CHIP_FLAG_TPM2;
  459. vtpm_proxy_work_start(proxy_dev);
  460. vtpm_new_dev->fd = fd;
  461. vtpm_new_dev->major = MAJOR(proxy_dev->chip->dev.devt);
  462. vtpm_new_dev->minor = MINOR(proxy_dev->chip->dev.devt);
  463. vtpm_new_dev->tpm_num = proxy_dev->chip->dev_num;
  464. return file;
  465. err_put_unused_fd:
  466. put_unused_fd(fd);
  467. err_delete_proxy_dev:
  468. vtpm_proxy_delete_proxy_dev(proxy_dev);
  469. return ERR_PTR(rc);
  470. }
  471. /*
  472. * Counter part to vtpm_create_device.
  473. */
  474. static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev)
  475. {
  476. vtpm_proxy_work_stop(proxy_dev);
  477. /*
  478. * A client may hold the 'ops' lock, so let it know that the server
  479. * side shuts down before we try to grab the 'ops' lock when
  480. * unregistering the chip.
  481. */
  482. vtpm_proxy_fops_undo_open(proxy_dev);
  483. if (proxy_dev->state & STATE_REGISTERED_FLAG)
  484. tpm_chip_unregister(proxy_dev->chip);
  485. vtpm_proxy_delete_proxy_dev(proxy_dev);
  486. }
  487. /*
  488. * Code related to the control device /dev/vtpmx
  489. */
  490. /**
  491. * vtpmx_ioc_new_dev - handler for the %VTPM_PROXY_IOC_NEW_DEV ioctl
  492. * @file: /dev/vtpmx
  493. * @ioctl: the ioctl number
  494. * @arg: pointer to the struct vtpmx_proxy_new_dev
  495. *
  496. * Creates an anonymous file that is used by the process acting as a TPM to
  497. * communicate with the client processes. The function will also add a new TPM
  498. * device through which data is proxied to this TPM acting process. The caller
  499. * will be provided with a file descriptor to communicate with the clients and
  500. * major and minor numbers for the TPM device.
  501. */
  502. static long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,
  503. unsigned long arg)
  504. {
  505. void __user *argp = (void __user *)arg;
  506. struct vtpm_proxy_new_dev __user *vtpm_new_dev_p;
  507. struct vtpm_proxy_new_dev vtpm_new_dev;
  508. struct file *vtpm_file;
  509. if (!capable(CAP_SYS_ADMIN))
  510. return -EPERM;
  511. vtpm_new_dev_p = argp;
  512. if (copy_from_user(&vtpm_new_dev, vtpm_new_dev_p,
  513. sizeof(vtpm_new_dev)))
  514. return -EFAULT;
  515. vtpm_file = vtpm_proxy_create_device(&vtpm_new_dev);
  516. if (IS_ERR(vtpm_file))
  517. return PTR_ERR(vtpm_file);
  518. if (copy_to_user(vtpm_new_dev_p, &vtpm_new_dev,
  519. sizeof(vtpm_new_dev))) {
  520. put_unused_fd(vtpm_new_dev.fd);
  521. fput(vtpm_file);
  522. return -EFAULT;
  523. }
  524. fd_install(vtpm_new_dev.fd, vtpm_file);
  525. return 0;
  526. }
  527. /*
  528. * vtpmx_fops_ioctl: ioctl on /dev/vtpmx
  529. *
  530. * Return:
  531. * Returns 0 on success, a negative error code otherwise.
  532. */
  533. static long vtpmx_fops_ioctl(struct file *f, unsigned int ioctl,
  534. unsigned long arg)
  535. {
  536. switch (ioctl) {
  537. case VTPM_PROXY_IOC_NEW_DEV:
  538. return vtpmx_ioc_new_dev(f, ioctl, arg);
  539. default:
  540. return -ENOIOCTLCMD;
  541. }
  542. }
  543. #ifdef CONFIG_COMPAT
  544. static long vtpmx_fops_compat_ioctl(struct file *f, unsigned int ioctl,
  545. unsigned long arg)
  546. {
  547. return vtpmx_fops_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  548. }
  549. #endif
  550. static const struct file_operations vtpmx_fops = {
  551. .owner = THIS_MODULE,
  552. .unlocked_ioctl = vtpmx_fops_ioctl,
  553. #ifdef CONFIG_COMPAT
  554. .compat_ioctl = vtpmx_fops_compat_ioctl,
  555. #endif
  556. .llseek = noop_llseek,
  557. };
  558. static struct miscdevice vtpmx_miscdev = {
  559. .minor = MISC_DYNAMIC_MINOR,
  560. .name = "vtpmx",
  561. .fops = &vtpmx_fops,
  562. };
  563. static int vtpmx_init(void)
  564. {
  565. return misc_register(&vtpmx_miscdev);
  566. }
  567. static void vtpmx_cleanup(void)
  568. {
  569. misc_deregister(&vtpmx_miscdev);
  570. }
  571. static int __init vtpm_module_init(void)
  572. {
  573. int rc;
  574. rc = vtpmx_init();
  575. if (rc) {
  576. pr_err("couldn't create vtpmx device\n");
  577. return rc;
  578. }
  579. workqueue = create_workqueue("tpm-vtpm");
  580. if (!workqueue) {
  581. pr_err("couldn't create workqueue\n");
  582. rc = -ENOMEM;
  583. goto err_vtpmx_cleanup;
  584. }
  585. return 0;
  586. err_vtpmx_cleanup:
  587. vtpmx_cleanup();
  588. return rc;
  589. }
  590. static void __exit vtpm_module_exit(void)
  591. {
  592. destroy_workqueue(workqueue);
  593. vtpmx_cleanup();
  594. }
  595. module_init(vtpm_module_init);
  596. module_exit(vtpm_module_exit);
  597. MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
  598. MODULE_DESCRIPTION("vTPM Driver");
  599. MODULE_VERSION("0.1");
  600. MODULE_LICENSE("GPL");