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. if (count > sizeof(proxy_dev->buffer)) {
  272. dev_err(&chip->dev,
  273. "Invalid size in send: count=%zd, buffer size=%zd\n",
  274. count, sizeof(proxy_dev->buffer));
  275. return -EIO;
  276. }
  277. if (!(proxy_dev->state & STATE_DRIVER_COMMAND) &&
  278. vtpm_proxy_is_driver_command(chip, buf, count))
  279. return -EFAULT;
  280. mutex_lock(&proxy_dev->buf_lock);
  281. if (!(proxy_dev->state & STATE_OPENED_FLAG)) {
  282. mutex_unlock(&proxy_dev->buf_lock);
  283. return -EPIPE;
  284. }
  285. proxy_dev->resp_len = 0;
  286. proxy_dev->req_len = count;
  287. memcpy(proxy_dev->buffer, buf, count);
  288. proxy_dev->state &= ~STATE_WAIT_RESPONSE_FLAG;
  289. mutex_unlock(&proxy_dev->buf_lock);
  290. wake_up_interruptible(&proxy_dev->wq);
  291. return 0;
  292. }
  293. static void vtpm_proxy_tpm_op_cancel(struct tpm_chip *chip)
  294. {
  295. /* not supported */
  296. }
  297. static u8 vtpm_proxy_tpm_op_status(struct tpm_chip *chip)
  298. {
  299. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  300. if (proxy_dev->resp_len)
  301. return VTPM_PROXY_REQ_COMPLETE_FLAG;
  302. return 0;
  303. }
  304. static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)
  305. {
  306. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  307. bool ret;
  308. mutex_lock(&proxy_dev->buf_lock);
  309. ret = !(proxy_dev->state & STATE_OPENED_FLAG);
  310. mutex_unlock(&proxy_dev->buf_lock);
  311. return ret;
  312. }
  313. static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
  314. {
  315. struct tpm_buf buf;
  316. int rc;
  317. const struct tpm_output_header *header;
  318. struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
  319. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  320. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS,
  321. TPM2_CC_SET_LOCALITY);
  322. else
  323. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND,
  324. TPM_ORD_SET_LOCALITY);
  325. if (rc)
  326. return rc;
  327. tpm_buf_append_u8(&buf, locality);
  328. proxy_dev->state |= STATE_DRIVER_COMMAND;
  329. rc = tpm_transmit_cmd(chip, NULL, buf.data, tpm_buf_length(&buf), 0,
  330. TPM_TRANSMIT_NESTED,
  331. "attempting to set locality");
  332. proxy_dev->state &= ~STATE_DRIVER_COMMAND;
  333. if (rc < 0) {
  334. locality = rc;
  335. goto out;
  336. }
  337. header = (const struct tpm_output_header *)buf.data;
  338. rc = be32_to_cpu(header->return_code);
  339. if (rc)
  340. locality = -1;
  341. out:
  342. tpm_buf_destroy(&buf);
  343. return locality;
  344. }
  345. static const struct tpm_class_ops vtpm_proxy_tpm_ops = {
  346. .flags = TPM_OPS_AUTO_STARTUP,
  347. .recv = vtpm_proxy_tpm_op_recv,
  348. .send = vtpm_proxy_tpm_op_send,
  349. .cancel = vtpm_proxy_tpm_op_cancel,
  350. .status = vtpm_proxy_tpm_op_status,
  351. .req_complete_mask = VTPM_PROXY_REQ_COMPLETE_FLAG,
  352. .req_complete_val = VTPM_PROXY_REQ_COMPLETE_FLAG,
  353. .req_canceled = vtpm_proxy_tpm_req_canceled,
  354. .request_locality = vtpm_proxy_request_locality,
  355. };
  356. /*
  357. * Code related to the startup of the TPM 2 and startup of TPM 1.2 +
  358. * retrieval of timeouts and durations.
  359. */
  360. static void vtpm_proxy_work(struct work_struct *work)
  361. {
  362. struct proxy_dev *proxy_dev = container_of(work, struct proxy_dev,
  363. work);
  364. int rc;
  365. rc = tpm_chip_register(proxy_dev->chip);
  366. if (rc)
  367. vtpm_proxy_fops_undo_open(proxy_dev);
  368. else
  369. proxy_dev->state |= STATE_REGISTERED_FLAG;
  370. }
  371. /*
  372. * vtpm_proxy_work_stop: make sure the work has finished
  373. *
  374. * This function is useful when user space closed the fd
  375. * while the driver still determines timeouts.
  376. */
  377. static void vtpm_proxy_work_stop(struct proxy_dev *proxy_dev)
  378. {
  379. vtpm_proxy_fops_undo_open(proxy_dev);
  380. flush_work(&proxy_dev->work);
  381. }
  382. /*
  383. * vtpm_proxy_work_start: Schedule the work for TPM 1.2 & 2 initialization
  384. */
  385. static inline void vtpm_proxy_work_start(struct proxy_dev *proxy_dev)
  386. {
  387. queue_work(workqueue, &proxy_dev->work);
  388. }
  389. /*
  390. * Code related to creation and deletion of device pairs
  391. */
  392. static struct proxy_dev *vtpm_proxy_create_proxy_dev(void)
  393. {
  394. struct proxy_dev *proxy_dev;
  395. struct tpm_chip *chip;
  396. int err;
  397. proxy_dev = kzalloc(sizeof(*proxy_dev), GFP_KERNEL);
  398. if (proxy_dev == NULL)
  399. return ERR_PTR(-ENOMEM);
  400. init_waitqueue_head(&proxy_dev->wq);
  401. mutex_init(&proxy_dev->buf_lock);
  402. INIT_WORK(&proxy_dev->work, vtpm_proxy_work);
  403. chip = tpm_chip_alloc(NULL, &vtpm_proxy_tpm_ops);
  404. if (IS_ERR(chip)) {
  405. err = PTR_ERR(chip);
  406. goto err_proxy_dev_free;
  407. }
  408. dev_set_drvdata(&chip->dev, proxy_dev);
  409. proxy_dev->chip = chip;
  410. return proxy_dev;
  411. err_proxy_dev_free:
  412. kfree(proxy_dev);
  413. return ERR_PTR(err);
  414. }
  415. /*
  416. * Undo what has been done in vtpm_create_proxy_dev
  417. */
  418. static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev *proxy_dev)
  419. {
  420. put_device(&proxy_dev->chip->dev); /* frees chip */
  421. kfree(proxy_dev);
  422. }
  423. /*
  424. * Create a /dev/tpm%d and 'server side' file descriptor pair
  425. *
  426. * Return:
  427. * Returns file pointer on success, an error value otherwise
  428. */
  429. static struct file *vtpm_proxy_create_device(
  430. struct vtpm_proxy_new_dev *vtpm_new_dev)
  431. {
  432. struct proxy_dev *proxy_dev;
  433. int rc, fd;
  434. struct file *file;
  435. if (vtpm_new_dev->flags & ~VTPM_PROXY_FLAGS_ALL)
  436. return ERR_PTR(-EOPNOTSUPP);
  437. proxy_dev = vtpm_proxy_create_proxy_dev();
  438. if (IS_ERR(proxy_dev))
  439. return ERR_CAST(proxy_dev);
  440. proxy_dev->flags = vtpm_new_dev->flags;
  441. /* setup an anonymous file for the server-side */
  442. fd = get_unused_fd_flags(O_RDWR);
  443. if (fd < 0) {
  444. rc = fd;
  445. goto err_delete_proxy_dev;
  446. }
  447. file = anon_inode_getfile("[vtpms]", &vtpm_proxy_fops, proxy_dev,
  448. O_RDWR);
  449. if (IS_ERR(file)) {
  450. rc = PTR_ERR(file);
  451. goto err_put_unused_fd;
  452. }
  453. /* from now on we can unwind with put_unused_fd() + fput() */
  454. /* simulate an open() on the server side */
  455. vtpm_proxy_fops_open(file);
  456. if (proxy_dev->flags & VTPM_PROXY_FLAG_TPM2)
  457. proxy_dev->chip->flags |= TPM_CHIP_FLAG_TPM2;
  458. vtpm_proxy_work_start(proxy_dev);
  459. vtpm_new_dev->fd = fd;
  460. vtpm_new_dev->major = MAJOR(proxy_dev->chip->dev.devt);
  461. vtpm_new_dev->minor = MINOR(proxy_dev->chip->dev.devt);
  462. vtpm_new_dev->tpm_num = proxy_dev->chip->dev_num;
  463. return file;
  464. err_put_unused_fd:
  465. put_unused_fd(fd);
  466. err_delete_proxy_dev:
  467. vtpm_proxy_delete_proxy_dev(proxy_dev);
  468. return ERR_PTR(rc);
  469. }
  470. /*
  471. * Counter part to vtpm_create_device.
  472. */
  473. static void vtpm_proxy_delete_device(struct proxy_dev *proxy_dev)
  474. {
  475. vtpm_proxy_work_stop(proxy_dev);
  476. /*
  477. * A client may hold the 'ops' lock, so let it know that the server
  478. * side shuts down before we try to grab the 'ops' lock when
  479. * unregistering the chip.
  480. */
  481. vtpm_proxy_fops_undo_open(proxy_dev);
  482. if (proxy_dev->state & STATE_REGISTERED_FLAG)
  483. tpm_chip_unregister(proxy_dev->chip);
  484. vtpm_proxy_delete_proxy_dev(proxy_dev);
  485. }
  486. /*
  487. * Code related to the control device /dev/vtpmx
  488. */
  489. /**
  490. * vtpmx_ioc_new_dev - handler for the %VTPM_PROXY_IOC_NEW_DEV ioctl
  491. * @file: /dev/vtpmx
  492. * @ioctl: the ioctl number
  493. * @arg: pointer to the struct vtpmx_proxy_new_dev
  494. *
  495. * Creates an anonymous file that is used by the process acting as a TPM to
  496. * communicate with the client processes. The function will also add a new TPM
  497. * device through which data is proxied to this TPM acting process. The caller
  498. * will be provided with a file descriptor to communicate with the clients and
  499. * major and minor numbers for the TPM device.
  500. */
  501. static long vtpmx_ioc_new_dev(struct file *file, unsigned int ioctl,
  502. unsigned long arg)
  503. {
  504. void __user *argp = (void __user *)arg;
  505. struct vtpm_proxy_new_dev __user *vtpm_new_dev_p;
  506. struct vtpm_proxy_new_dev vtpm_new_dev;
  507. struct file *vtpm_file;
  508. if (!capable(CAP_SYS_ADMIN))
  509. return -EPERM;
  510. vtpm_new_dev_p = argp;
  511. if (copy_from_user(&vtpm_new_dev, vtpm_new_dev_p,
  512. sizeof(vtpm_new_dev)))
  513. return -EFAULT;
  514. vtpm_file = vtpm_proxy_create_device(&vtpm_new_dev);
  515. if (IS_ERR(vtpm_file))
  516. return PTR_ERR(vtpm_file);
  517. if (copy_to_user(vtpm_new_dev_p, &vtpm_new_dev,
  518. sizeof(vtpm_new_dev))) {
  519. put_unused_fd(vtpm_new_dev.fd);
  520. fput(vtpm_file);
  521. return -EFAULT;
  522. }
  523. fd_install(vtpm_new_dev.fd, vtpm_file);
  524. return 0;
  525. }
  526. /*
  527. * vtpmx_fops_ioctl: ioctl on /dev/vtpmx
  528. *
  529. * Return:
  530. * Returns 0 on success, a negative error code otherwise.
  531. */
  532. static long vtpmx_fops_ioctl(struct file *f, unsigned int ioctl,
  533. unsigned long arg)
  534. {
  535. switch (ioctl) {
  536. case VTPM_PROXY_IOC_NEW_DEV:
  537. return vtpmx_ioc_new_dev(f, ioctl, arg);
  538. default:
  539. return -ENOIOCTLCMD;
  540. }
  541. }
  542. #ifdef CONFIG_COMPAT
  543. static long vtpmx_fops_compat_ioctl(struct file *f, unsigned int ioctl,
  544. unsigned long arg)
  545. {
  546. return vtpmx_fops_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  547. }
  548. #endif
  549. static const struct file_operations vtpmx_fops = {
  550. .owner = THIS_MODULE,
  551. .unlocked_ioctl = vtpmx_fops_ioctl,
  552. #ifdef CONFIG_COMPAT
  553. .compat_ioctl = vtpmx_fops_compat_ioctl,
  554. #endif
  555. .llseek = noop_llseek,
  556. };
  557. static struct miscdevice vtpmx_miscdev = {
  558. .minor = MISC_DYNAMIC_MINOR,
  559. .name = "vtpmx",
  560. .fops = &vtpmx_fops,
  561. };
  562. static int vtpmx_init(void)
  563. {
  564. return misc_register(&vtpmx_miscdev);
  565. }
  566. static void vtpmx_cleanup(void)
  567. {
  568. misc_deregister(&vtpmx_miscdev);
  569. }
  570. static int __init vtpm_module_init(void)
  571. {
  572. int rc;
  573. rc = vtpmx_init();
  574. if (rc) {
  575. pr_err("couldn't create vtpmx device\n");
  576. return rc;
  577. }
  578. workqueue = create_workqueue("tpm-vtpm");
  579. if (!workqueue) {
  580. pr_err("couldn't create workqueue\n");
  581. rc = -ENOMEM;
  582. goto err_vtpmx_cleanup;
  583. }
  584. return 0;
  585. err_vtpmx_cleanup:
  586. vtpmx_cleanup();
  587. return rc;
  588. }
  589. static void __exit vtpm_module_exit(void)
  590. {
  591. destroy_workqueue(workqueue);
  592. vtpmx_cleanup();
  593. }
  594. module_init(vtpm_module_init);
  595. module_exit(vtpm_module_exit);
  596. MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
  597. MODULE_DESCRIPTION("vTPM Driver");
  598. MODULE_VERSION("0.1");
  599. MODULE_LICENSE("GPL");