tpm-interface.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. * Authors:
  6. * Leendert van Doorn <leendert@watson.ibm.com>
  7. * Dave Safford <safford@watson.ibm.com>
  8. * Reiner Sailer <sailer@watson.ibm.com>
  9. * Kylene Hall <kjhall@us.ibm.com>
  10. *
  11. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  12. *
  13. * Device driver for TCG/TCPA TPM (trusted platform module).
  14. * Specifications at www.trustedcomputinggroup.org
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation, version 2 of the
  19. * License.
  20. *
  21. * Note, the TPM chip is not interrupt driven (only polling)
  22. * and can have very long timeouts (minutes!). Hence the unusual
  23. * calls to msleep.
  24. *
  25. */
  26. #include <linux/poll.h>
  27. #include <linux/slab.h>
  28. #include <linux/mutex.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/freezer.h>
  31. #include "tpm.h"
  32. #include "tpm_eventlog.h"
  33. #define TPM_MAX_ORDINAL 243
  34. #define TSC_MAX_ORDINAL 12
  35. #define TPM_PROTECTED_COMMAND 0x00
  36. #define TPM_CONNECTION_COMMAND 0x40
  37. /*
  38. * Bug workaround - some TPM's don't flush the most
  39. * recently changed pcr on suspend, so force the flush
  40. * with an extend to the selected _unused_ non-volatile pcr.
  41. */
  42. static int tpm_suspend_pcr;
  43. module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
  44. MODULE_PARM_DESC(suspend_pcr,
  45. "PCR to use for dummy writes to faciltate flush on suspend.");
  46. /*
  47. * Array with one entry per ordinal defining the maximum amount
  48. * of time the chip could take to return the result. The ordinal
  49. * designation of short, medium or long is defined in a table in
  50. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  51. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  52. * from the chip during initialization with a call to tpm_get_timeouts.
  53. */
  54. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  55. TPM_UNDEFINED, /* 0 */
  56. TPM_UNDEFINED,
  57. TPM_UNDEFINED,
  58. TPM_UNDEFINED,
  59. TPM_UNDEFINED,
  60. TPM_UNDEFINED, /* 5 */
  61. TPM_UNDEFINED,
  62. TPM_UNDEFINED,
  63. TPM_UNDEFINED,
  64. TPM_UNDEFINED,
  65. TPM_SHORT, /* 10 */
  66. TPM_SHORT,
  67. TPM_MEDIUM,
  68. TPM_LONG,
  69. TPM_LONG,
  70. TPM_MEDIUM, /* 15 */
  71. TPM_SHORT,
  72. TPM_SHORT,
  73. TPM_MEDIUM,
  74. TPM_LONG,
  75. TPM_SHORT, /* 20 */
  76. TPM_SHORT,
  77. TPM_MEDIUM,
  78. TPM_MEDIUM,
  79. TPM_MEDIUM,
  80. TPM_SHORT, /* 25 */
  81. TPM_SHORT,
  82. TPM_MEDIUM,
  83. TPM_SHORT,
  84. TPM_SHORT,
  85. TPM_MEDIUM, /* 30 */
  86. TPM_LONG,
  87. TPM_MEDIUM,
  88. TPM_SHORT,
  89. TPM_SHORT,
  90. TPM_SHORT, /* 35 */
  91. TPM_MEDIUM,
  92. TPM_MEDIUM,
  93. TPM_UNDEFINED,
  94. TPM_UNDEFINED,
  95. TPM_MEDIUM, /* 40 */
  96. TPM_LONG,
  97. TPM_MEDIUM,
  98. TPM_SHORT,
  99. TPM_SHORT,
  100. TPM_SHORT, /* 45 */
  101. TPM_SHORT,
  102. TPM_SHORT,
  103. TPM_SHORT,
  104. TPM_LONG,
  105. TPM_MEDIUM, /* 50 */
  106. TPM_MEDIUM,
  107. TPM_UNDEFINED,
  108. TPM_UNDEFINED,
  109. TPM_UNDEFINED,
  110. TPM_UNDEFINED, /* 55 */
  111. TPM_UNDEFINED,
  112. TPM_UNDEFINED,
  113. TPM_UNDEFINED,
  114. TPM_UNDEFINED,
  115. TPM_MEDIUM, /* 60 */
  116. TPM_MEDIUM,
  117. TPM_MEDIUM,
  118. TPM_SHORT,
  119. TPM_SHORT,
  120. TPM_MEDIUM, /* 65 */
  121. TPM_UNDEFINED,
  122. TPM_UNDEFINED,
  123. TPM_UNDEFINED,
  124. TPM_UNDEFINED,
  125. TPM_SHORT, /* 70 */
  126. TPM_SHORT,
  127. TPM_UNDEFINED,
  128. TPM_UNDEFINED,
  129. TPM_UNDEFINED,
  130. TPM_UNDEFINED, /* 75 */
  131. TPM_UNDEFINED,
  132. TPM_UNDEFINED,
  133. TPM_UNDEFINED,
  134. TPM_UNDEFINED,
  135. TPM_LONG, /* 80 */
  136. TPM_UNDEFINED,
  137. TPM_MEDIUM,
  138. TPM_LONG,
  139. TPM_SHORT,
  140. TPM_UNDEFINED, /* 85 */
  141. TPM_UNDEFINED,
  142. TPM_UNDEFINED,
  143. TPM_UNDEFINED,
  144. TPM_UNDEFINED,
  145. TPM_SHORT, /* 90 */
  146. TPM_SHORT,
  147. TPM_SHORT,
  148. TPM_SHORT,
  149. TPM_SHORT,
  150. TPM_UNDEFINED, /* 95 */
  151. TPM_UNDEFINED,
  152. TPM_UNDEFINED,
  153. TPM_UNDEFINED,
  154. TPM_UNDEFINED,
  155. TPM_MEDIUM, /* 100 */
  156. TPM_SHORT,
  157. TPM_SHORT,
  158. TPM_UNDEFINED,
  159. TPM_UNDEFINED,
  160. TPM_UNDEFINED, /* 105 */
  161. TPM_UNDEFINED,
  162. TPM_UNDEFINED,
  163. TPM_UNDEFINED,
  164. TPM_UNDEFINED,
  165. TPM_SHORT, /* 110 */
  166. TPM_SHORT,
  167. TPM_SHORT,
  168. TPM_SHORT,
  169. TPM_SHORT,
  170. TPM_SHORT, /* 115 */
  171. TPM_SHORT,
  172. TPM_SHORT,
  173. TPM_UNDEFINED,
  174. TPM_UNDEFINED,
  175. TPM_LONG, /* 120 */
  176. TPM_LONG,
  177. TPM_MEDIUM,
  178. TPM_UNDEFINED,
  179. TPM_SHORT,
  180. TPM_SHORT, /* 125 */
  181. TPM_SHORT,
  182. TPM_LONG,
  183. TPM_SHORT,
  184. TPM_SHORT,
  185. TPM_SHORT, /* 130 */
  186. TPM_MEDIUM,
  187. TPM_UNDEFINED,
  188. TPM_SHORT,
  189. TPM_MEDIUM,
  190. TPM_UNDEFINED, /* 135 */
  191. TPM_UNDEFINED,
  192. TPM_UNDEFINED,
  193. TPM_UNDEFINED,
  194. TPM_UNDEFINED,
  195. TPM_SHORT, /* 140 */
  196. TPM_SHORT,
  197. TPM_UNDEFINED,
  198. TPM_UNDEFINED,
  199. TPM_UNDEFINED,
  200. TPM_UNDEFINED, /* 145 */
  201. TPM_UNDEFINED,
  202. TPM_UNDEFINED,
  203. TPM_UNDEFINED,
  204. TPM_UNDEFINED,
  205. TPM_SHORT, /* 150 */
  206. TPM_MEDIUM,
  207. TPM_MEDIUM,
  208. TPM_SHORT,
  209. TPM_SHORT,
  210. TPM_UNDEFINED, /* 155 */
  211. TPM_UNDEFINED,
  212. TPM_UNDEFINED,
  213. TPM_UNDEFINED,
  214. TPM_UNDEFINED,
  215. TPM_SHORT, /* 160 */
  216. TPM_SHORT,
  217. TPM_SHORT,
  218. TPM_SHORT,
  219. TPM_UNDEFINED,
  220. TPM_UNDEFINED, /* 165 */
  221. TPM_UNDEFINED,
  222. TPM_UNDEFINED,
  223. TPM_UNDEFINED,
  224. TPM_UNDEFINED,
  225. TPM_LONG, /* 170 */
  226. TPM_UNDEFINED,
  227. TPM_UNDEFINED,
  228. TPM_UNDEFINED,
  229. TPM_UNDEFINED,
  230. TPM_UNDEFINED, /* 175 */
  231. TPM_UNDEFINED,
  232. TPM_UNDEFINED,
  233. TPM_UNDEFINED,
  234. TPM_UNDEFINED,
  235. TPM_MEDIUM, /* 180 */
  236. TPM_SHORT,
  237. TPM_MEDIUM,
  238. TPM_MEDIUM,
  239. TPM_MEDIUM,
  240. TPM_MEDIUM, /* 185 */
  241. TPM_SHORT,
  242. TPM_UNDEFINED,
  243. TPM_UNDEFINED,
  244. TPM_UNDEFINED,
  245. TPM_UNDEFINED, /* 190 */
  246. TPM_UNDEFINED,
  247. TPM_UNDEFINED,
  248. TPM_UNDEFINED,
  249. TPM_UNDEFINED,
  250. TPM_UNDEFINED, /* 195 */
  251. TPM_UNDEFINED,
  252. TPM_UNDEFINED,
  253. TPM_UNDEFINED,
  254. TPM_UNDEFINED,
  255. TPM_SHORT, /* 200 */
  256. TPM_UNDEFINED,
  257. TPM_UNDEFINED,
  258. TPM_UNDEFINED,
  259. TPM_SHORT,
  260. TPM_SHORT, /* 205 */
  261. TPM_SHORT,
  262. TPM_SHORT,
  263. TPM_SHORT,
  264. TPM_SHORT,
  265. TPM_MEDIUM, /* 210 */
  266. TPM_UNDEFINED,
  267. TPM_MEDIUM,
  268. TPM_MEDIUM,
  269. TPM_MEDIUM,
  270. TPM_UNDEFINED, /* 215 */
  271. TPM_MEDIUM,
  272. TPM_UNDEFINED,
  273. TPM_UNDEFINED,
  274. TPM_SHORT,
  275. TPM_SHORT, /* 220 */
  276. TPM_SHORT,
  277. TPM_SHORT,
  278. TPM_SHORT,
  279. TPM_SHORT,
  280. TPM_UNDEFINED, /* 225 */
  281. TPM_UNDEFINED,
  282. TPM_UNDEFINED,
  283. TPM_UNDEFINED,
  284. TPM_UNDEFINED,
  285. TPM_SHORT, /* 230 */
  286. TPM_LONG,
  287. TPM_MEDIUM,
  288. TPM_UNDEFINED,
  289. TPM_UNDEFINED,
  290. TPM_UNDEFINED, /* 235 */
  291. TPM_UNDEFINED,
  292. TPM_UNDEFINED,
  293. TPM_UNDEFINED,
  294. TPM_UNDEFINED,
  295. TPM_SHORT, /* 240 */
  296. TPM_UNDEFINED,
  297. TPM_MEDIUM,
  298. };
  299. /*
  300. * Returns max number of jiffies to wait
  301. */
  302. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  303. u32 ordinal)
  304. {
  305. int duration_idx = TPM_UNDEFINED;
  306. int duration = 0;
  307. /*
  308. * We only have a duration table for protected commands, where the upper
  309. * 16 bits are 0. For the few other ordinals the fallback will be used.
  310. */
  311. if (ordinal < TPM_MAX_ORDINAL)
  312. duration_idx = tpm_ordinal_duration[ordinal];
  313. if (duration_idx != TPM_UNDEFINED)
  314. duration = chip->duration[duration_idx];
  315. if (duration <= 0)
  316. return 2 * 60 * HZ;
  317. else
  318. return duration;
  319. }
  320. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  321. /*
  322. * Internal kernel interface to transmit TPM commands
  323. */
  324. ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
  325. unsigned int flags)
  326. {
  327. ssize_t rc;
  328. u32 count, ordinal;
  329. unsigned long stop;
  330. if (bufsiz < TPM_HEADER_SIZE)
  331. return -EINVAL;
  332. if (bufsiz > TPM_BUFSIZE)
  333. bufsiz = TPM_BUFSIZE;
  334. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  335. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  336. if (count == 0)
  337. return -ENODATA;
  338. if (count > bufsiz) {
  339. dev_err(&chip->dev,
  340. "invalid count value %x %zx\n", count, bufsiz);
  341. return -E2BIG;
  342. }
  343. if (!(flags & TPM_TRANSMIT_UNLOCKED))
  344. mutex_lock(&chip->tpm_mutex);
  345. rc = chip->ops->send(chip, (u8 *) buf, count);
  346. if (rc < 0) {
  347. dev_err(&chip->dev,
  348. "tpm_transmit: tpm_send: error %zd\n", rc);
  349. goto out;
  350. }
  351. if (chip->flags & TPM_CHIP_FLAG_IRQ)
  352. goto out_recv;
  353. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  354. stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
  355. else
  356. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  357. do {
  358. u8 status = chip->ops->status(chip);
  359. if ((status & chip->ops->req_complete_mask) ==
  360. chip->ops->req_complete_val)
  361. goto out_recv;
  362. if (chip->ops->req_canceled(chip, status)) {
  363. dev_err(&chip->dev, "Operation Canceled\n");
  364. rc = -ECANCELED;
  365. goto out;
  366. }
  367. msleep(TPM_TIMEOUT); /* CHECK */
  368. rmb();
  369. } while (time_before(jiffies, stop));
  370. chip->ops->cancel(chip);
  371. dev_err(&chip->dev, "Operation Timed out\n");
  372. rc = -ETIME;
  373. goto out;
  374. out_recv:
  375. rc = chip->ops->recv(chip, (u8 *) buf, bufsiz);
  376. if (rc < 0)
  377. dev_err(&chip->dev,
  378. "tpm_transmit: tpm_recv: error %zd\n", rc);
  379. out:
  380. if (!(flags & TPM_TRANSMIT_UNLOCKED))
  381. mutex_unlock(&chip->tpm_mutex);
  382. return rc;
  383. }
  384. #define TPM_DIGEST_SIZE 20
  385. #define TPM_RET_CODE_IDX 6
  386. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, const void *cmd,
  387. int len, unsigned int flags, const char *desc)
  388. {
  389. const struct tpm_output_header *header;
  390. int err;
  391. len = tpm_transmit(chip, (const u8 *)cmd, len, flags);
  392. if (len < 0)
  393. return len;
  394. else if (len < TPM_HEADER_SIZE)
  395. return -EFAULT;
  396. header = cmd;
  397. err = be32_to_cpu(header->return_code);
  398. if (err != 0 && desc)
  399. dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
  400. desc);
  401. return err;
  402. }
  403. #define TPM_INTERNAL_RESULT_SIZE 200
  404. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  405. #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
  406. static const struct tpm_input_header tpm_getcap_header = {
  407. .tag = TPM_TAG_RQU_COMMAND,
  408. .length = cpu_to_be32(22),
  409. .ordinal = TPM_ORD_GET_CAP
  410. };
  411. ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap,
  412. const char *desc)
  413. {
  414. struct tpm_cmd_t tpm_cmd;
  415. int rc;
  416. tpm_cmd.header.in = tpm_getcap_header;
  417. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  418. tpm_cmd.params.getcap_in.cap = subcap_id;
  419. /*subcap field not necessary */
  420. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  421. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  422. } else {
  423. if (subcap_id == TPM_CAP_FLAG_PERM ||
  424. subcap_id == TPM_CAP_FLAG_VOL)
  425. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  426. else
  427. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  428. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  429. tpm_cmd.params.getcap_in.subcap = subcap_id;
  430. }
  431. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  432. desc);
  433. if (!rc)
  434. *cap = tpm_cmd.params.getcap_out.cap;
  435. return rc;
  436. }
  437. EXPORT_SYMBOL_GPL(tpm_getcap);
  438. #define TPM_ORD_STARTUP cpu_to_be32(153)
  439. #define TPM_ST_CLEAR cpu_to_be16(1)
  440. #define TPM_ST_STATE cpu_to_be16(2)
  441. #define TPM_ST_DEACTIVATED cpu_to_be16(3)
  442. static const struct tpm_input_header tpm_startup_header = {
  443. .tag = TPM_TAG_RQU_COMMAND,
  444. .length = cpu_to_be32(12),
  445. .ordinal = TPM_ORD_STARTUP
  446. };
  447. static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
  448. {
  449. struct tpm_cmd_t start_cmd;
  450. start_cmd.header.in = tpm_startup_header;
  451. start_cmd.params.startup_in.startup_type = startup_type;
  452. return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  453. "attempting to start the TPM");
  454. }
  455. int tpm_get_timeouts(struct tpm_chip *chip)
  456. {
  457. struct tpm_cmd_t tpm_cmd;
  458. unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4];
  459. struct duration_t *duration_cap;
  460. ssize_t rc;
  461. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  462. /* Fixed timeouts for TPM2 */
  463. chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
  464. chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
  465. chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
  466. chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
  467. chip->duration[TPM_SHORT] =
  468. msecs_to_jiffies(TPM2_DURATION_SHORT);
  469. chip->duration[TPM_MEDIUM] =
  470. msecs_to_jiffies(TPM2_DURATION_MEDIUM);
  471. chip->duration[TPM_LONG] =
  472. msecs_to_jiffies(TPM2_DURATION_LONG);
  473. return 0;
  474. }
  475. tpm_cmd.header.in = tpm_getcap_header;
  476. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  477. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  478. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  479. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  480. NULL);
  481. if (rc == TPM_ERR_INVALID_POSTINIT) {
  482. /* The TPM is not started, we are the first to talk to it.
  483. Execute a startup command. */
  484. dev_info(&chip->dev, "Issuing TPM_STARTUP");
  485. if (tpm_startup(chip, TPM_ST_CLEAR))
  486. return rc;
  487. tpm_cmd.header.in = tpm_getcap_header;
  488. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  489. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  490. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  491. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  492. 0, NULL);
  493. }
  494. if (rc) {
  495. dev_err(&chip->dev,
  496. "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  497. rc);
  498. goto duration;
  499. }
  500. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  501. be32_to_cpu(tpm_cmd.header.out.length)
  502. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
  503. return -EINVAL;
  504. timeout_old[0] = jiffies_to_usecs(chip->timeout_a);
  505. timeout_old[1] = jiffies_to_usecs(chip->timeout_b);
  506. timeout_old[2] = jiffies_to_usecs(chip->timeout_c);
  507. timeout_old[3] = jiffies_to_usecs(chip->timeout_d);
  508. timeout_chip[0] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.a);
  509. timeout_chip[1] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.b);
  510. timeout_chip[2] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.c);
  511. timeout_chip[3] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.d);
  512. memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff));
  513. /*
  514. * Provide ability for vendor overrides of timeout values in case
  515. * of misreporting.
  516. */
  517. if (chip->ops->update_timeouts != NULL)
  518. chip->timeout_adjusted =
  519. chip->ops->update_timeouts(chip, timeout_eff);
  520. if (!chip->timeout_adjusted) {
  521. /* Restore default if chip reported 0 */
  522. int i;
  523. for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) {
  524. if (timeout_eff[i])
  525. continue;
  526. timeout_eff[i] = timeout_old[i];
  527. chip->timeout_adjusted = true;
  528. }
  529. if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) {
  530. /* timeouts in msec rather usec */
  531. for (i = 0; i != ARRAY_SIZE(timeout_eff); i++)
  532. timeout_eff[i] *= 1000;
  533. chip->timeout_adjusted = true;
  534. }
  535. }
  536. /* Report adjusted timeouts */
  537. if (chip->timeout_adjusted) {
  538. dev_info(&chip->dev,
  539. HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
  540. timeout_chip[0], timeout_eff[0],
  541. timeout_chip[1], timeout_eff[1],
  542. timeout_chip[2], timeout_eff[2],
  543. timeout_chip[3], timeout_eff[3]);
  544. }
  545. chip->timeout_a = usecs_to_jiffies(timeout_eff[0]);
  546. chip->timeout_b = usecs_to_jiffies(timeout_eff[1]);
  547. chip->timeout_c = usecs_to_jiffies(timeout_eff[2]);
  548. chip->timeout_d = usecs_to_jiffies(timeout_eff[3]);
  549. duration:
  550. tpm_cmd.header.in = tpm_getcap_header;
  551. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  552. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  553. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  554. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  555. "attempting to determine the durations");
  556. if (rc)
  557. return rc;
  558. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  559. be32_to_cpu(tpm_cmd.header.out.length)
  560. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  561. return -EINVAL;
  562. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  563. chip->duration[TPM_SHORT] =
  564. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  565. chip->duration[TPM_MEDIUM] =
  566. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  567. chip->duration[TPM_LONG] =
  568. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  569. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  570. * value wrong and apparently reports msecs rather than usecs. So we
  571. * fix up the resulting too-small TPM_SHORT value to make things work.
  572. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  573. */
  574. if (chip->duration[TPM_SHORT] < (HZ / 100)) {
  575. chip->duration[TPM_SHORT] = HZ;
  576. chip->duration[TPM_MEDIUM] *= 1000;
  577. chip->duration[TPM_LONG] *= 1000;
  578. chip->duration_adjusted = true;
  579. dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
  580. }
  581. return 0;
  582. }
  583. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  584. #define TPM_ORD_CONTINUE_SELFTEST 83
  585. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  586. static const struct tpm_input_header continue_selftest_header = {
  587. .tag = TPM_TAG_RQU_COMMAND,
  588. .length = cpu_to_be32(10),
  589. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  590. };
  591. /**
  592. * tpm_continue_selftest -- run TPM's selftest
  593. * @chip: TPM chip to use
  594. *
  595. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  596. * a TPM error code.
  597. */
  598. static int tpm_continue_selftest(struct tpm_chip *chip)
  599. {
  600. int rc;
  601. struct tpm_cmd_t cmd;
  602. cmd.header.in = continue_selftest_header;
  603. rc = tpm_transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 0,
  604. "continue selftest");
  605. return rc;
  606. }
  607. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  608. #define READ_PCR_RESULT_SIZE 30
  609. static const struct tpm_input_header pcrread_header = {
  610. .tag = TPM_TAG_RQU_COMMAND,
  611. .length = cpu_to_be32(14),
  612. .ordinal = TPM_ORDINAL_PCRREAD
  613. };
  614. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  615. {
  616. int rc;
  617. struct tpm_cmd_t cmd;
  618. cmd.header.in = pcrread_header;
  619. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  620. rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, 0,
  621. "attempting to read a pcr value");
  622. if (rc == 0)
  623. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  624. TPM_DIGEST_SIZE);
  625. return rc;
  626. }
  627. /**
  628. * tpm_is_tpm2 - is the chip a TPM2 chip?
  629. * @chip_num: tpm idx # or ANY
  630. *
  631. * Returns < 0 on error, and 1 or 0 on success depending whether the chip
  632. * is a TPM2 chip.
  633. */
  634. int tpm_is_tpm2(u32 chip_num)
  635. {
  636. struct tpm_chip *chip;
  637. int rc;
  638. chip = tpm_chip_find_get(chip_num);
  639. if (chip == NULL)
  640. return -ENODEV;
  641. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
  642. tpm_put_ops(chip);
  643. return rc;
  644. }
  645. EXPORT_SYMBOL_GPL(tpm_is_tpm2);
  646. /**
  647. * tpm_pcr_read - read a pcr value
  648. * @chip_num: tpm idx # or ANY
  649. * @pcr_idx: pcr idx to retrieve
  650. * @res_buf: TPM_PCR value
  651. * size of res_buf is 20 bytes (or NULL if you don't care)
  652. *
  653. * The TPM driver should be built-in, but for whatever reason it
  654. * isn't, protect against the chip disappearing, by incrementing
  655. * the module usage count.
  656. */
  657. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  658. {
  659. struct tpm_chip *chip;
  660. int rc;
  661. chip = tpm_chip_find_get(chip_num);
  662. if (chip == NULL)
  663. return -ENODEV;
  664. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  665. rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
  666. else
  667. rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
  668. tpm_put_ops(chip);
  669. return rc;
  670. }
  671. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  672. /**
  673. * tpm_pcr_extend - extend pcr value with hash
  674. * @chip_num: tpm idx # or AN&
  675. * @pcr_idx: pcr idx to extend
  676. * @hash: hash value used to extend pcr value
  677. *
  678. * The TPM driver should be built-in, but for whatever reason it
  679. * isn't, protect against the chip disappearing, by incrementing
  680. * the module usage count.
  681. */
  682. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  683. #define EXTEND_PCR_RESULT_SIZE 34
  684. static const struct tpm_input_header pcrextend_header = {
  685. .tag = TPM_TAG_RQU_COMMAND,
  686. .length = cpu_to_be32(34),
  687. .ordinal = TPM_ORD_PCR_EXTEND
  688. };
  689. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  690. {
  691. struct tpm_cmd_t cmd;
  692. int rc;
  693. struct tpm_chip *chip;
  694. chip = tpm_chip_find_get(chip_num);
  695. if (chip == NULL)
  696. return -ENODEV;
  697. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  698. rc = tpm2_pcr_extend(chip, pcr_idx, hash);
  699. tpm_put_ops(chip);
  700. return rc;
  701. }
  702. cmd.header.in = pcrextend_header;
  703. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  704. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  705. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0,
  706. "attempting extend a PCR value");
  707. tpm_put_ops(chip);
  708. return rc;
  709. }
  710. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  711. /**
  712. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  713. * can receive further commands
  714. * @chip: TPM chip to use
  715. *
  716. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  717. * a TPM error code.
  718. */
  719. int tpm_do_selftest(struct tpm_chip *chip)
  720. {
  721. int rc;
  722. unsigned int loops;
  723. unsigned int delay_msec = 100;
  724. unsigned long duration;
  725. u8 dummy[TPM_DIGEST_SIZE];
  726. duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  727. loops = jiffies_to_msecs(duration) / delay_msec;
  728. rc = tpm_continue_selftest(chip);
  729. if (rc == TPM_ERR_INVALID_POSTINIT) {
  730. chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
  731. dev_info(&chip->dev, "TPM not ready (%d)\n", rc);
  732. }
  733. /* This may fail if there was no TPM driver during a suspend/resume
  734. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  735. */
  736. if (rc)
  737. return rc;
  738. do {
  739. /* Attempt to read a PCR value */
  740. rc = tpm_pcr_read_dev(chip, 0, dummy);
  741. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  742. * around 300ms while the self test is ongoing, keep trying
  743. * until the self test duration expires. */
  744. if (rc == -ETIME) {
  745. dev_info(
  746. &chip->dev, HW_ERR
  747. "TPM command timed out during continue self test");
  748. msleep(delay_msec);
  749. continue;
  750. }
  751. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  752. dev_info(&chip->dev,
  753. "TPM is disabled/deactivated (0x%X)\n", rc);
  754. /* TPM is disabled and/or deactivated; driver can
  755. * proceed and TPM does handle commands for
  756. * suspend/resume correctly
  757. */
  758. return 0;
  759. }
  760. if (rc != TPM_WARN_DOING_SELFTEST)
  761. return rc;
  762. msleep(delay_msec);
  763. } while (--loops > 0);
  764. return rc;
  765. }
  766. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  767. /**
  768. * tpm1_auto_startup - Perform the standard automatic TPM initialization
  769. * sequence
  770. * @chip: TPM chip to use
  771. *
  772. * Returns 0 on success, < 0 in case of fatal error.
  773. */
  774. int tpm1_auto_startup(struct tpm_chip *chip)
  775. {
  776. int rc;
  777. rc = tpm_get_timeouts(chip);
  778. if (rc)
  779. goto out;
  780. rc = tpm_do_selftest(chip);
  781. if (rc) {
  782. dev_err(&chip->dev, "TPM self test failed\n");
  783. goto out;
  784. }
  785. return rc;
  786. out:
  787. if (rc > 0)
  788. rc = -ENODEV;
  789. return rc;
  790. }
  791. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  792. {
  793. struct tpm_chip *chip;
  794. int rc;
  795. chip = tpm_chip_find_get(chip_num);
  796. if (chip == NULL)
  797. return -ENODEV;
  798. rc = tpm_transmit_cmd(chip, cmd, buflen, 0, "attempting tpm_cmd");
  799. tpm_put_ops(chip);
  800. return rc;
  801. }
  802. EXPORT_SYMBOL_GPL(tpm_send);
  803. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  804. bool check_cancel, bool *canceled)
  805. {
  806. u8 status = chip->ops->status(chip);
  807. *canceled = false;
  808. if ((status & mask) == mask)
  809. return true;
  810. if (check_cancel && chip->ops->req_canceled(chip, status)) {
  811. *canceled = true;
  812. return true;
  813. }
  814. return false;
  815. }
  816. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  817. wait_queue_head_t *queue, bool check_cancel)
  818. {
  819. unsigned long stop;
  820. long rc;
  821. u8 status;
  822. bool canceled = false;
  823. /* check current status */
  824. status = chip->ops->status(chip);
  825. if ((status & mask) == mask)
  826. return 0;
  827. stop = jiffies + timeout;
  828. if (chip->flags & TPM_CHIP_FLAG_IRQ) {
  829. again:
  830. timeout = stop - jiffies;
  831. if ((long)timeout <= 0)
  832. return -ETIME;
  833. rc = wait_event_interruptible_timeout(*queue,
  834. wait_for_tpm_stat_cond(chip, mask, check_cancel,
  835. &canceled),
  836. timeout);
  837. if (rc > 0) {
  838. if (canceled)
  839. return -ECANCELED;
  840. return 0;
  841. }
  842. if (rc == -ERESTARTSYS && freezing(current)) {
  843. clear_thread_flag(TIF_SIGPENDING);
  844. goto again;
  845. }
  846. } else {
  847. do {
  848. msleep(TPM_TIMEOUT);
  849. status = chip->ops->status(chip);
  850. if ((status & mask) == mask)
  851. return 0;
  852. } while (time_before(jiffies, stop));
  853. }
  854. return -ETIME;
  855. }
  856. EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
  857. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  858. #define SAVESTATE_RESULT_SIZE 10
  859. static const struct tpm_input_header savestate_header = {
  860. .tag = TPM_TAG_RQU_COMMAND,
  861. .length = cpu_to_be32(10),
  862. .ordinal = TPM_ORD_SAVESTATE
  863. };
  864. /*
  865. * We are about to suspend. Save the TPM state
  866. * so that it can be restored.
  867. */
  868. int tpm_pm_suspend(struct device *dev)
  869. {
  870. struct tpm_chip *chip = dev_get_drvdata(dev);
  871. struct tpm_cmd_t cmd;
  872. int rc, try;
  873. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  874. if (chip == NULL)
  875. return -ENODEV;
  876. if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
  877. return 0;
  878. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  879. tpm2_shutdown(chip, TPM2_SU_STATE);
  880. return 0;
  881. }
  882. /* for buggy tpm, flush pcrs with extend to selected dummy */
  883. if (tpm_suspend_pcr) {
  884. cmd.header.in = pcrextend_header;
  885. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  886. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  887. TPM_DIGEST_SIZE);
  888. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0,
  889. "extending dummy pcr before suspend");
  890. }
  891. /* now do the actual savestate */
  892. for (try = 0; try < TPM_RETRY; try++) {
  893. cmd.header.in = savestate_header;
  894. rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, 0,
  895. NULL);
  896. /*
  897. * If the TPM indicates that it is too busy to respond to
  898. * this command then retry before giving up. It can take
  899. * several seconds for this TPM to be ready.
  900. *
  901. * This can happen if the TPM has already been sent the
  902. * SaveState command before the driver has loaded. TCG 1.2
  903. * specification states that any communication after SaveState
  904. * may cause the TPM to invalidate previously saved state.
  905. */
  906. if (rc != TPM_WARN_RETRY)
  907. break;
  908. msleep(TPM_TIMEOUT_RETRY);
  909. }
  910. if (rc)
  911. dev_err(&chip->dev,
  912. "Error (%d) sending savestate before suspend\n", rc);
  913. else if (try > 0)
  914. dev_warn(&chip->dev, "TPM savestate took %dms\n",
  915. try * TPM_TIMEOUT_RETRY);
  916. return rc;
  917. }
  918. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  919. /*
  920. * Resume from a power safe. The BIOS already restored
  921. * the TPM state.
  922. */
  923. int tpm_pm_resume(struct device *dev)
  924. {
  925. struct tpm_chip *chip = dev_get_drvdata(dev);
  926. if (chip == NULL)
  927. return -ENODEV;
  928. return 0;
  929. }
  930. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  931. #define TPM_GETRANDOM_RESULT_SIZE 18
  932. static const struct tpm_input_header tpm_getrandom_header = {
  933. .tag = TPM_TAG_RQU_COMMAND,
  934. .length = cpu_to_be32(14),
  935. .ordinal = TPM_ORD_GET_RANDOM
  936. };
  937. /**
  938. * tpm_get_random() - Get random bytes from the tpm's RNG
  939. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  940. * @out: destination buffer for the random bytes
  941. * @max: the max number of bytes to write to @out
  942. *
  943. * Returns < 0 on error and the number of bytes read on success
  944. */
  945. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  946. {
  947. struct tpm_chip *chip;
  948. struct tpm_cmd_t tpm_cmd;
  949. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  950. int err, total = 0, retries = 5;
  951. u8 *dest = out;
  952. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  953. return -EINVAL;
  954. chip = tpm_chip_find_get(chip_num);
  955. if (chip == NULL)
  956. return -ENODEV;
  957. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  958. err = tpm2_get_random(chip, out, max);
  959. tpm_put_ops(chip);
  960. return err;
  961. }
  962. do {
  963. tpm_cmd.header.in = tpm_getrandom_header;
  964. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  965. err = tpm_transmit_cmd(chip, &tpm_cmd,
  966. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  967. 0, "attempting get random");
  968. if (err)
  969. break;
  970. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  971. if (recd > num_bytes) {
  972. total = -EFAULT;
  973. break;
  974. }
  975. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  976. dest += recd;
  977. total += recd;
  978. num_bytes -= recd;
  979. } while (retries-- && total < max);
  980. tpm_put_ops(chip);
  981. return total ? total : -EIO;
  982. }
  983. EXPORT_SYMBOL_GPL(tpm_get_random);
  984. /**
  985. * tpm_seal_trusted() - seal a trusted key
  986. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  987. * @options: authentication values and other options
  988. * @payload: the key data in clear and encrypted form
  989. *
  990. * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
  991. * are supported.
  992. */
  993. int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload,
  994. struct trusted_key_options *options)
  995. {
  996. struct tpm_chip *chip;
  997. int rc;
  998. chip = tpm_chip_find_get(chip_num);
  999. if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  1000. return -ENODEV;
  1001. rc = tpm2_seal_trusted(chip, payload, options);
  1002. tpm_put_ops(chip);
  1003. return rc;
  1004. }
  1005. EXPORT_SYMBOL_GPL(tpm_seal_trusted);
  1006. /**
  1007. * tpm_unseal_trusted() - unseal a trusted key
  1008. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  1009. * @options: authentication values and other options
  1010. * @payload: the key data in clear and encrypted form
  1011. *
  1012. * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
  1013. * are supported.
  1014. */
  1015. int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload,
  1016. struct trusted_key_options *options)
  1017. {
  1018. struct tpm_chip *chip;
  1019. int rc;
  1020. chip = tpm_chip_find_get(chip_num);
  1021. if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  1022. return -ENODEV;
  1023. rc = tpm2_unseal_trusted(chip, payload, options);
  1024. tpm_put_ops(chip);
  1025. return rc;
  1026. }
  1027. EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
  1028. static int __init tpm_init(void)
  1029. {
  1030. int rc;
  1031. tpm_class = class_create(THIS_MODULE, "tpm");
  1032. if (IS_ERR(tpm_class)) {
  1033. pr_err("couldn't create tpm class\n");
  1034. return PTR_ERR(tpm_class);
  1035. }
  1036. rc = alloc_chrdev_region(&tpm_devt, 0, TPM_NUM_DEVICES, "tpm");
  1037. if (rc < 0) {
  1038. pr_err("tpm: failed to allocate char dev region\n");
  1039. class_destroy(tpm_class);
  1040. return rc;
  1041. }
  1042. return 0;
  1043. }
  1044. static void __exit tpm_exit(void)
  1045. {
  1046. idr_destroy(&dev_nums_idr);
  1047. class_destroy(tpm_class);
  1048. unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES);
  1049. }
  1050. subsys_initcall(tpm_init);
  1051. module_exit(tpm_exit);
  1052. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1053. MODULE_DESCRIPTION("TPM Driver");
  1054. MODULE_VERSION("2.0");
  1055. MODULE_LICENSE("GPL");