tpm.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. *
  4. * Authors:
  5. * Leendert van Doorn <leendert@watson.ibm.com>
  6. * Dave Safford <safford@watson.ibm.com>
  7. * Reiner Sailer <sailer@watson.ibm.com>
  8. * Kylene Hall <kjhall@us.ibm.com>
  9. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  11. *
  12. * Device driver for TCG/TCPA TPM (trusted platform module).
  13. * Specifications at www.trustedcomputinggroup.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation, version 2 of the
  18. * License.
  19. *
  20. * Note, the TPM chip is not interrupt driven (only polling)
  21. * and can have very long timeouts (minutes!). Hence the unusual
  22. * calls to msleep.
  23. *
  24. */
  25. #include <linux/poll.h>
  26. #include <linux/slab.h>
  27. #include <linux/mutex.h>
  28. #include <linux/spinlock.h>
  29. #include "tpm.h"
  30. enum tpm_const {
  31. TPM_MINOR = 224, /* officially assigned */
  32. TPM_BUFSIZE = 4096,
  33. TPM_NUM_DEVICES = 256,
  34. };
  35. enum tpm_duration {
  36. TPM_SHORT = 0,
  37. TPM_MEDIUM = 1,
  38. TPM_LONG = 2,
  39. TPM_UNDEFINED,
  40. };
  41. #define TPM_MAX_ORDINAL 243
  42. #define TPM_MAX_PROTECTED_ORDINAL 12
  43. #define TPM_PROTECTED_ORDINAL_MASK 0xFF
  44. /*
  45. * Bug workaround - some TPM's don't flush the most
  46. * recently changed pcr on suspend, so force the flush
  47. * with an extend to the selected _unused_ non-volatile pcr.
  48. */
  49. static int tpm_suspend_pcr;
  50. module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
  51. MODULE_PARM_DESC(suspend_pcr,
  52. "PCR to use for dummy writes to faciltate flush on suspend.");
  53. static LIST_HEAD(tpm_chip_list);
  54. static DEFINE_SPINLOCK(driver_lock);
  55. static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
  56. /*
  57. * Array with one entry per ordinal defining the maximum amount
  58. * of time the chip could take to return the result. The ordinal
  59. * designation of short, medium or long is defined in a table in
  60. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  61. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  62. * from the chip during initialization with a call to tpm_get_timeouts.
  63. */
  64. static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
  65. TPM_UNDEFINED, /* 0 */
  66. TPM_UNDEFINED,
  67. TPM_UNDEFINED,
  68. TPM_UNDEFINED,
  69. TPM_UNDEFINED,
  70. TPM_UNDEFINED, /* 5 */
  71. TPM_UNDEFINED,
  72. TPM_UNDEFINED,
  73. TPM_UNDEFINED,
  74. TPM_UNDEFINED,
  75. TPM_SHORT, /* 10 */
  76. TPM_SHORT,
  77. };
  78. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  79. TPM_UNDEFINED, /* 0 */
  80. TPM_UNDEFINED,
  81. TPM_UNDEFINED,
  82. TPM_UNDEFINED,
  83. TPM_UNDEFINED,
  84. TPM_UNDEFINED, /* 5 */
  85. TPM_UNDEFINED,
  86. TPM_UNDEFINED,
  87. TPM_UNDEFINED,
  88. TPM_UNDEFINED,
  89. TPM_SHORT, /* 10 */
  90. TPM_SHORT,
  91. TPM_MEDIUM,
  92. TPM_LONG,
  93. TPM_LONG,
  94. TPM_MEDIUM, /* 15 */
  95. TPM_SHORT,
  96. TPM_SHORT,
  97. TPM_MEDIUM,
  98. TPM_LONG,
  99. TPM_SHORT, /* 20 */
  100. TPM_SHORT,
  101. TPM_MEDIUM,
  102. TPM_MEDIUM,
  103. TPM_MEDIUM,
  104. TPM_SHORT, /* 25 */
  105. TPM_SHORT,
  106. TPM_MEDIUM,
  107. TPM_SHORT,
  108. TPM_SHORT,
  109. TPM_MEDIUM, /* 30 */
  110. TPM_LONG,
  111. TPM_MEDIUM,
  112. TPM_SHORT,
  113. TPM_SHORT,
  114. TPM_SHORT, /* 35 */
  115. TPM_MEDIUM,
  116. TPM_MEDIUM,
  117. TPM_UNDEFINED,
  118. TPM_UNDEFINED,
  119. TPM_MEDIUM, /* 40 */
  120. TPM_LONG,
  121. TPM_MEDIUM,
  122. TPM_SHORT,
  123. TPM_SHORT,
  124. TPM_SHORT, /* 45 */
  125. TPM_SHORT,
  126. TPM_SHORT,
  127. TPM_SHORT,
  128. TPM_LONG,
  129. TPM_MEDIUM, /* 50 */
  130. TPM_MEDIUM,
  131. TPM_UNDEFINED,
  132. TPM_UNDEFINED,
  133. TPM_UNDEFINED,
  134. TPM_UNDEFINED, /* 55 */
  135. TPM_UNDEFINED,
  136. TPM_UNDEFINED,
  137. TPM_UNDEFINED,
  138. TPM_UNDEFINED,
  139. TPM_MEDIUM, /* 60 */
  140. TPM_MEDIUM,
  141. TPM_MEDIUM,
  142. TPM_SHORT,
  143. TPM_SHORT,
  144. TPM_MEDIUM, /* 65 */
  145. TPM_UNDEFINED,
  146. TPM_UNDEFINED,
  147. TPM_UNDEFINED,
  148. TPM_UNDEFINED,
  149. TPM_SHORT, /* 70 */
  150. TPM_SHORT,
  151. TPM_UNDEFINED,
  152. TPM_UNDEFINED,
  153. TPM_UNDEFINED,
  154. TPM_UNDEFINED, /* 75 */
  155. TPM_UNDEFINED,
  156. TPM_UNDEFINED,
  157. TPM_UNDEFINED,
  158. TPM_UNDEFINED,
  159. TPM_LONG, /* 80 */
  160. TPM_UNDEFINED,
  161. TPM_MEDIUM,
  162. TPM_LONG,
  163. TPM_SHORT,
  164. TPM_UNDEFINED, /* 85 */
  165. TPM_UNDEFINED,
  166. TPM_UNDEFINED,
  167. TPM_UNDEFINED,
  168. TPM_UNDEFINED,
  169. TPM_SHORT, /* 90 */
  170. TPM_SHORT,
  171. TPM_SHORT,
  172. TPM_SHORT,
  173. TPM_SHORT,
  174. TPM_UNDEFINED, /* 95 */
  175. TPM_UNDEFINED,
  176. TPM_UNDEFINED,
  177. TPM_UNDEFINED,
  178. TPM_UNDEFINED,
  179. TPM_MEDIUM, /* 100 */
  180. TPM_SHORT,
  181. TPM_SHORT,
  182. TPM_UNDEFINED,
  183. TPM_UNDEFINED,
  184. TPM_UNDEFINED, /* 105 */
  185. TPM_UNDEFINED,
  186. TPM_UNDEFINED,
  187. TPM_UNDEFINED,
  188. TPM_UNDEFINED,
  189. TPM_SHORT, /* 110 */
  190. TPM_SHORT,
  191. TPM_SHORT,
  192. TPM_SHORT,
  193. TPM_SHORT,
  194. TPM_SHORT, /* 115 */
  195. TPM_SHORT,
  196. TPM_SHORT,
  197. TPM_UNDEFINED,
  198. TPM_UNDEFINED,
  199. TPM_LONG, /* 120 */
  200. TPM_LONG,
  201. TPM_MEDIUM,
  202. TPM_UNDEFINED,
  203. TPM_SHORT,
  204. TPM_SHORT, /* 125 */
  205. TPM_SHORT,
  206. TPM_LONG,
  207. TPM_SHORT,
  208. TPM_SHORT,
  209. TPM_SHORT, /* 130 */
  210. TPM_MEDIUM,
  211. TPM_UNDEFINED,
  212. TPM_SHORT,
  213. TPM_MEDIUM,
  214. TPM_UNDEFINED, /* 135 */
  215. TPM_UNDEFINED,
  216. TPM_UNDEFINED,
  217. TPM_UNDEFINED,
  218. TPM_UNDEFINED,
  219. TPM_SHORT, /* 140 */
  220. TPM_SHORT,
  221. TPM_UNDEFINED,
  222. TPM_UNDEFINED,
  223. TPM_UNDEFINED,
  224. TPM_UNDEFINED, /* 145 */
  225. TPM_UNDEFINED,
  226. TPM_UNDEFINED,
  227. TPM_UNDEFINED,
  228. TPM_UNDEFINED,
  229. TPM_SHORT, /* 150 */
  230. TPM_MEDIUM,
  231. TPM_MEDIUM,
  232. TPM_SHORT,
  233. TPM_SHORT,
  234. TPM_UNDEFINED, /* 155 */
  235. TPM_UNDEFINED,
  236. TPM_UNDEFINED,
  237. TPM_UNDEFINED,
  238. TPM_UNDEFINED,
  239. TPM_SHORT, /* 160 */
  240. TPM_SHORT,
  241. TPM_SHORT,
  242. TPM_SHORT,
  243. TPM_UNDEFINED,
  244. TPM_UNDEFINED, /* 165 */
  245. TPM_UNDEFINED,
  246. TPM_UNDEFINED,
  247. TPM_UNDEFINED,
  248. TPM_UNDEFINED,
  249. TPM_LONG, /* 170 */
  250. TPM_UNDEFINED,
  251. TPM_UNDEFINED,
  252. TPM_UNDEFINED,
  253. TPM_UNDEFINED,
  254. TPM_UNDEFINED, /* 175 */
  255. TPM_UNDEFINED,
  256. TPM_UNDEFINED,
  257. TPM_UNDEFINED,
  258. TPM_UNDEFINED,
  259. TPM_MEDIUM, /* 180 */
  260. TPM_SHORT,
  261. TPM_MEDIUM,
  262. TPM_MEDIUM,
  263. TPM_MEDIUM,
  264. TPM_MEDIUM, /* 185 */
  265. TPM_SHORT,
  266. TPM_UNDEFINED,
  267. TPM_UNDEFINED,
  268. TPM_UNDEFINED,
  269. TPM_UNDEFINED, /* 190 */
  270. TPM_UNDEFINED,
  271. TPM_UNDEFINED,
  272. TPM_UNDEFINED,
  273. TPM_UNDEFINED,
  274. TPM_UNDEFINED, /* 195 */
  275. TPM_UNDEFINED,
  276. TPM_UNDEFINED,
  277. TPM_UNDEFINED,
  278. TPM_UNDEFINED,
  279. TPM_SHORT, /* 200 */
  280. TPM_UNDEFINED,
  281. TPM_UNDEFINED,
  282. TPM_UNDEFINED,
  283. TPM_SHORT,
  284. TPM_SHORT, /* 205 */
  285. TPM_SHORT,
  286. TPM_SHORT,
  287. TPM_SHORT,
  288. TPM_SHORT,
  289. TPM_MEDIUM, /* 210 */
  290. TPM_UNDEFINED,
  291. TPM_MEDIUM,
  292. TPM_MEDIUM,
  293. TPM_MEDIUM,
  294. TPM_UNDEFINED, /* 215 */
  295. TPM_MEDIUM,
  296. TPM_UNDEFINED,
  297. TPM_UNDEFINED,
  298. TPM_SHORT,
  299. TPM_SHORT, /* 220 */
  300. TPM_SHORT,
  301. TPM_SHORT,
  302. TPM_SHORT,
  303. TPM_SHORT,
  304. TPM_UNDEFINED, /* 225 */
  305. TPM_UNDEFINED,
  306. TPM_UNDEFINED,
  307. TPM_UNDEFINED,
  308. TPM_UNDEFINED,
  309. TPM_SHORT, /* 230 */
  310. TPM_LONG,
  311. TPM_MEDIUM,
  312. TPM_UNDEFINED,
  313. TPM_UNDEFINED,
  314. TPM_UNDEFINED, /* 235 */
  315. TPM_UNDEFINED,
  316. TPM_UNDEFINED,
  317. TPM_UNDEFINED,
  318. TPM_UNDEFINED,
  319. TPM_SHORT, /* 240 */
  320. TPM_UNDEFINED,
  321. TPM_MEDIUM,
  322. };
  323. static void user_reader_timeout(unsigned long ptr)
  324. {
  325. struct tpm_chip *chip = (struct tpm_chip *) ptr;
  326. schedule_work(&chip->work);
  327. }
  328. static void timeout_work(struct work_struct *work)
  329. {
  330. struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
  331. mutex_lock(&chip->buffer_mutex);
  332. atomic_set(&chip->data_pending, 0);
  333. memset(chip->data_buffer, 0, TPM_BUFSIZE);
  334. mutex_unlock(&chip->buffer_mutex);
  335. }
  336. /*
  337. * Returns max number of jiffies to wait
  338. */
  339. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  340. u32 ordinal)
  341. {
  342. int duration_idx = TPM_UNDEFINED;
  343. int duration = 0;
  344. if (ordinal < TPM_MAX_ORDINAL)
  345. duration_idx = tpm_ordinal_duration[ordinal];
  346. else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
  347. TPM_MAX_PROTECTED_ORDINAL)
  348. duration_idx =
  349. tpm_protected_ordinal_duration[ordinal &
  350. TPM_PROTECTED_ORDINAL_MASK];
  351. if (duration_idx != TPM_UNDEFINED)
  352. duration = chip->vendor.duration[duration_idx];
  353. if (duration <= 0)
  354. return 2 * 60 * HZ;
  355. else
  356. return duration;
  357. }
  358. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  359. /*
  360. * Internal kernel interface to transmit TPM commands
  361. */
  362. static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  363. size_t bufsiz)
  364. {
  365. ssize_t rc;
  366. u32 count, ordinal;
  367. unsigned long stop;
  368. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  369. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  370. if (count == 0)
  371. return -ENODATA;
  372. if (count > bufsiz) {
  373. dev_err(chip->dev,
  374. "invalid count value %x %zx \n", count, bufsiz);
  375. return -E2BIG;
  376. }
  377. mutex_lock(&chip->tpm_mutex);
  378. if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
  379. dev_err(chip->dev,
  380. "tpm_transmit: tpm_send: error %zd\n", rc);
  381. goto out;
  382. }
  383. if (chip->vendor.irq)
  384. goto out_recv;
  385. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  386. do {
  387. u8 status = chip->vendor.status(chip);
  388. if ((status & chip->vendor.req_complete_mask) ==
  389. chip->vendor.req_complete_val)
  390. goto out_recv;
  391. if ((status == chip->vendor.req_canceled)) {
  392. dev_err(chip->dev, "Operation Canceled\n");
  393. rc = -ECANCELED;
  394. goto out;
  395. }
  396. msleep(TPM_TIMEOUT); /* CHECK */
  397. rmb();
  398. } while (time_before(jiffies, stop));
  399. chip->vendor.cancel(chip);
  400. dev_err(chip->dev, "Operation Timed out\n");
  401. rc = -ETIME;
  402. goto out;
  403. out_recv:
  404. rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
  405. if (rc < 0)
  406. dev_err(chip->dev,
  407. "tpm_transmit: tpm_recv: error %zd\n", rc);
  408. out:
  409. mutex_unlock(&chip->tpm_mutex);
  410. return rc;
  411. }
  412. #define TPM_DIGEST_SIZE 20
  413. #define TPM_ERROR_SIZE 10
  414. #define TPM_RET_CODE_IDX 6
  415. enum tpm_capabilities {
  416. TPM_CAP_FLAG = cpu_to_be32(4),
  417. TPM_CAP_PROP = cpu_to_be32(5),
  418. CAP_VERSION_1_1 = cpu_to_be32(0x06),
  419. CAP_VERSION_1_2 = cpu_to_be32(0x1A)
  420. };
  421. enum tpm_sub_capabilities {
  422. TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
  423. TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
  424. TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
  425. TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
  426. TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
  427. TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
  428. TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
  429. };
  430. static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
  431. int len, const char *desc)
  432. {
  433. int err;
  434. len = tpm_transmit(chip,(u8 *) cmd, len);
  435. if (len < 0)
  436. return len;
  437. if (len == TPM_ERROR_SIZE) {
  438. err = be32_to_cpu(cmd->header.out.return_code);
  439. dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
  440. return err;
  441. }
  442. return 0;
  443. }
  444. #define TPM_INTERNAL_RESULT_SIZE 200
  445. #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
  446. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  447. static const struct tpm_input_header tpm_getcap_header = {
  448. .tag = TPM_TAG_RQU_COMMAND,
  449. .length = cpu_to_be32(22),
  450. .ordinal = TPM_ORD_GET_CAP
  451. };
  452. ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
  453. const char *desc)
  454. {
  455. struct tpm_cmd_t tpm_cmd;
  456. int rc;
  457. struct tpm_chip *chip = dev_get_drvdata(dev);
  458. tpm_cmd.header.in = tpm_getcap_header;
  459. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  460. tpm_cmd.params.getcap_in.cap = subcap_id;
  461. /*subcap field not necessary */
  462. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  463. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  464. } else {
  465. if (subcap_id == TPM_CAP_FLAG_PERM ||
  466. subcap_id == TPM_CAP_FLAG_VOL)
  467. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  468. else
  469. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  470. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  471. tpm_cmd.params.getcap_in.subcap = subcap_id;
  472. }
  473. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
  474. if (!rc)
  475. *cap = tpm_cmd.params.getcap_out.cap;
  476. return rc;
  477. }
  478. void tpm_gen_interrupt(struct tpm_chip *chip)
  479. {
  480. struct tpm_cmd_t tpm_cmd;
  481. ssize_t rc;
  482. tpm_cmd.header.in = tpm_getcap_header;
  483. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  484. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  485. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  486. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  487. "attempting to determine the timeouts");
  488. }
  489. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  490. void tpm_get_timeouts(struct tpm_chip *chip)
  491. {
  492. struct tpm_cmd_t tpm_cmd;
  493. struct timeout_t *timeout_cap;
  494. struct duration_t *duration_cap;
  495. ssize_t rc;
  496. u32 timeout;
  497. tpm_cmd.header.in = tpm_getcap_header;
  498. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  499. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  500. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  501. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  502. "attempting to determine the timeouts");
  503. if (rc)
  504. goto duration;
  505. if (be32_to_cpu(tpm_cmd.header.out.length)
  506. != 4 * sizeof(u32))
  507. goto duration;
  508. timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
  509. /* Don't overwrite default if value is 0 */
  510. timeout = be32_to_cpu(timeout_cap->a);
  511. if (timeout)
  512. chip->vendor.timeout_a = usecs_to_jiffies(timeout);
  513. timeout = be32_to_cpu(timeout_cap->b);
  514. if (timeout)
  515. chip->vendor.timeout_b = usecs_to_jiffies(timeout);
  516. timeout = be32_to_cpu(timeout_cap->c);
  517. if (timeout)
  518. chip->vendor.timeout_c = usecs_to_jiffies(timeout);
  519. timeout = be32_to_cpu(timeout_cap->d);
  520. if (timeout)
  521. chip->vendor.timeout_d = usecs_to_jiffies(timeout);
  522. duration:
  523. tpm_cmd.header.in = tpm_getcap_header;
  524. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  525. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  526. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  527. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  528. "attempting to determine the durations");
  529. if (rc)
  530. return;
  531. if (be32_to_cpu(tpm_cmd.header.out.return_code)
  532. != 3 * sizeof(u32))
  533. return;
  534. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  535. chip->vendor.duration[TPM_SHORT] =
  536. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  537. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  538. * value wrong and apparently reports msecs rather than usecs. So we
  539. * fix up the resulting too-small TPM_SHORT value to make things work.
  540. */
  541. if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
  542. chip->vendor.duration[TPM_SHORT] = HZ;
  543. chip->vendor.duration[TPM_MEDIUM] =
  544. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  545. chip->vendor.duration[TPM_LONG] =
  546. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  547. }
  548. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  549. void tpm_continue_selftest(struct tpm_chip *chip)
  550. {
  551. u8 data[] = {
  552. 0, 193, /* TPM_TAG_RQU_COMMAND */
  553. 0, 0, 0, 10, /* length */
  554. 0, 0, 0, 83, /* TPM_ORD_GetCapability */
  555. };
  556. tpm_transmit(chip, data, sizeof(data));
  557. }
  558. EXPORT_SYMBOL_GPL(tpm_continue_selftest);
  559. ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
  560. char *buf)
  561. {
  562. cap_t cap;
  563. ssize_t rc;
  564. rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
  565. "attempting to determine the permanent enabled state");
  566. if (rc)
  567. return 0;
  568. rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
  569. return rc;
  570. }
  571. EXPORT_SYMBOL_GPL(tpm_show_enabled);
  572. ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
  573. char *buf)
  574. {
  575. cap_t cap;
  576. ssize_t rc;
  577. rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
  578. "attempting to determine the permanent active state");
  579. if (rc)
  580. return 0;
  581. rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
  582. return rc;
  583. }
  584. EXPORT_SYMBOL_GPL(tpm_show_active);
  585. ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
  586. char *buf)
  587. {
  588. cap_t cap;
  589. ssize_t rc;
  590. rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
  591. "attempting to determine the owner state");
  592. if (rc)
  593. return 0;
  594. rc = sprintf(buf, "%d\n", cap.owned);
  595. return rc;
  596. }
  597. EXPORT_SYMBOL_GPL(tpm_show_owned);
  598. ssize_t tpm_show_temp_deactivated(struct device * dev,
  599. struct device_attribute * attr, char *buf)
  600. {
  601. cap_t cap;
  602. ssize_t rc;
  603. rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
  604. "attempting to determine the temporary state");
  605. if (rc)
  606. return 0;
  607. rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
  608. return rc;
  609. }
  610. EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
  611. /*
  612. * tpm_chip_find_get - return tpm_chip for given chip number
  613. */
  614. static struct tpm_chip *tpm_chip_find_get(int chip_num)
  615. {
  616. struct tpm_chip *pos, *chip = NULL;
  617. rcu_read_lock();
  618. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  619. if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
  620. continue;
  621. if (try_module_get(pos->dev->driver->owner)) {
  622. chip = pos;
  623. break;
  624. }
  625. }
  626. rcu_read_unlock();
  627. return chip;
  628. }
  629. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  630. #define READ_PCR_RESULT_SIZE 30
  631. static struct tpm_input_header pcrread_header = {
  632. .tag = TPM_TAG_RQU_COMMAND,
  633. .length = cpu_to_be32(14),
  634. .ordinal = TPM_ORDINAL_PCRREAD
  635. };
  636. int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  637. {
  638. int rc;
  639. struct tpm_cmd_t cmd;
  640. cmd.header.in = pcrread_header;
  641. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  642. rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
  643. "attempting to read a pcr value");
  644. if (rc == 0)
  645. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  646. TPM_DIGEST_SIZE);
  647. return rc;
  648. }
  649. /**
  650. * tpm_pcr_read - read a pcr value
  651. * @chip_num: tpm idx # or ANY
  652. * @pcr_idx: pcr idx to retrieve
  653. * @res_buf: TPM_PCR value
  654. * size of res_buf is 20 bytes (or NULL if you don't care)
  655. *
  656. * The TPM driver should be built-in, but for whatever reason it
  657. * isn't, protect against the chip disappearing, by incrementing
  658. * the module usage count.
  659. */
  660. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  661. {
  662. struct tpm_chip *chip;
  663. int rc;
  664. chip = tpm_chip_find_get(chip_num);
  665. if (chip == NULL)
  666. return -ENODEV;
  667. rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
  668. module_put(chip->dev->driver->owner);
  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 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. cmd.header.in = pcrextend_header;
  698. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  699. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  700. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  701. "attempting extend a PCR value");
  702. module_put(chip->dev->driver->owner);
  703. return rc;
  704. }
  705. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  706. ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
  707. char *buf)
  708. {
  709. cap_t cap;
  710. u8 digest[TPM_DIGEST_SIZE];
  711. ssize_t rc;
  712. int i, j, num_pcrs;
  713. char *str = buf;
  714. struct tpm_chip *chip = dev_get_drvdata(dev);
  715. rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
  716. "attempting to determine the number of PCRS");
  717. if (rc)
  718. return 0;
  719. num_pcrs = be32_to_cpu(cap.num_pcrs);
  720. for (i = 0; i < num_pcrs; i++) {
  721. rc = __tpm_pcr_read(chip, i, digest);
  722. if (rc)
  723. break;
  724. str += sprintf(str, "PCR-%02d: ", i);
  725. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  726. str += sprintf(str, "%02X ", digest[j]);
  727. str += sprintf(str, "\n");
  728. }
  729. return str - buf;
  730. }
  731. EXPORT_SYMBOL_GPL(tpm_show_pcrs);
  732. #define READ_PUBEK_RESULT_SIZE 314
  733. #define TPM_ORD_READPUBEK cpu_to_be32(124)
  734. struct tpm_input_header tpm_readpubek_header = {
  735. .tag = TPM_TAG_RQU_COMMAND,
  736. .length = cpu_to_be32(30),
  737. .ordinal = TPM_ORD_READPUBEK
  738. };
  739. ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
  740. char *buf)
  741. {
  742. u8 *data;
  743. struct tpm_cmd_t tpm_cmd;
  744. ssize_t err;
  745. int i, rc;
  746. char *str = buf;
  747. struct tpm_chip *chip = dev_get_drvdata(dev);
  748. tpm_cmd.header.in = tpm_readpubek_header;
  749. err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
  750. "attempting to read the PUBEK");
  751. if (err)
  752. goto out;
  753. /*
  754. ignore header 10 bytes
  755. algorithm 32 bits (1 == RSA )
  756. encscheme 16 bits
  757. sigscheme 16 bits
  758. parameters (RSA 12->bytes: keybit, #primes, expbit)
  759. keylenbytes 32 bits
  760. 256 byte modulus
  761. ignore checksum 20 bytes
  762. */
  763. data = tpm_cmd.params.readpubek_out_buffer;
  764. str +=
  765. sprintf(str,
  766. "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
  767. "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
  768. " %02X %02X %02X %02X %02X %02X %02X %02X\n"
  769. "Modulus length: %d\nModulus: \n",
  770. data[10], data[11], data[12], data[13], data[14],
  771. data[15], data[16], data[17], data[22], data[23],
  772. data[24], data[25], data[26], data[27], data[28],
  773. data[29], data[30], data[31], data[32], data[33],
  774. be32_to_cpu(*((__be32 *) (data + 34))));
  775. for (i = 0; i < 256; i++) {
  776. str += sprintf(str, "%02X ", data[i + 38]);
  777. if ((i + 1) % 16 == 0)
  778. str += sprintf(str, "\n");
  779. }
  780. out:
  781. rc = str - buf;
  782. return rc;
  783. }
  784. EXPORT_SYMBOL_GPL(tpm_show_pubek);
  785. ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
  786. char *buf)
  787. {
  788. cap_t cap;
  789. ssize_t rc;
  790. char *str = buf;
  791. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  792. "attempting to determine the manufacturer");
  793. if (rc)
  794. return 0;
  795. str += sprintf(str, "Manufacturer: 0x%x\n",
  796. be32_to_cpu(cap.manufacturer_id));
  797. rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
  798. "attempting to determine the 1.1 version");
  799. if (rc)
  800. return 0;
  801. str += sprintf(str,
  802. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  803. cap.tpm_version.Major, cap.tpm_version.Minor,
  804. cap.tpm_version.revMajor, cap.tpm_version.revMinor);
  805. return str - buf;
  806. }
  807. EXPORT_SYMBOL_GPL(tpm_show_caps);
  808. ssize_t tpm_show_caps_1_2(struct device * dev,
  809. struct device_attribute * attr, char *buf)
  810. {
  811. cap_t cap;
  812. ssize_t rc;
  813. char *str = buf;
  814. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  815. "attempting to determine the manufacturer");
  816. if (rc)
  817. return 0;
  818. str += sprintf(str, "Manufacturer: 0x%x\n",
  819. be32_to_cpu(cap.manufacturer_id));
  820. rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
  821. "attempting to determine the 1.2 version");
  822. if (rc)
  823. return 0;
  824. str += sprintf(str,
  825. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  826. cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
  827. cap.tpm_version_1_2.revMajor,
  828. cap.tpm_version_1_2.revMinor);
  829. return str - buf;
  830. }
  831. EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
  832. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  833. const char *buf, size_t count)
  834. {
  835. struct tpm_chip *chip = dev_get_drvdata(dev);
  836. if (chip == NULL)
  837. return 0;
  838. chip->vendor.cancel(chip);
  839. return count;
  840. }
  841. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  842. /*
  843. * Device file system interface to the TPM
  844. *
  845. * It's assured that the chip will be opened just once,
  846. * by the check of is_open variable, which is protected
  847. * by driver_lock.
  848. */
  849. int tpm_open(struct inode *inode, struct file *file)
  850. {
  851. int minor = iminor(inode);
  852. struct tpm_chip *chip = NULL, *pos;
  853. rcu_read_lock();
  854. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  855. if (pos->vendor.miscdev.minor == minor) {
  856. chip = pos;
  857. get_device(chip->dev);
  858. break;
  859. }
  860. }
  861. rcu_read_unlock();
  862. if (!chip)
  863. return -ENODEV;
  864. if (test_and_set_bit(0, &chip->is_open)) {
  865. dev_dbg(chip->dev, "Another process owns this TPM\n");
  866. put_device(chip->dev);
  867. return -EBUSY;
  868. }
  869. chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  870. if (chip->data_buffer == NULL) {
  871. clear_bit(0, &chip->is_open);
  872. put_device(chip->dev);
  873. return -ENOMEM;
  874. }
  875. atomic_set(&chip->data_pending, 0);
  876. file->private_data = chip;
  877. return 0;
  878. }
  879. EXPORT_SYMBOL_GPL(tpm_open);
  880. /*
  881. * Called on file close
  882. */
  883. int tpm_release(struct inode *inode, struct file *file)
  884. {
  885. struct tpm_chip *chip = file->private_data;
  886. del_singleshot_timer_sync(&chip->user_read_timer);
  887. flush_scheduled_work();
  888. file->private_data = NULL;
  889. atomic_set(&chip->data_pending, 0);
  890. kfree(chip->data_buffer);
  891. clear_bit(0, &chip->is_open);
  892. put_device(chip->dev);
  893. return 0;
  894. }
  895. EXPORT_SYMBOL_GPL(tpm_release);
  896. ssize_t tpm_write(struct file *file, const char __user *buf,
  897. size_t size, loff_t *off)
  898. {
  899. struct tpm_chip *chip = file->private_data;
  900. size_t in_size = size, out_size;
  901. /* cannot perform a write until the read has cleared
  902. either via tpm_read or a user_read_timer timeout */
  903. while (atomic_read(&chip->data_pending) != 0)
  904. msleep(TPM_TIMEOUT);
  905. mutex_lock(&chip->buffer_mutex);
  906. if (in_size > TPM_BUFSIZE)
  907. in_size = TPM_BUFSIZE;
  908. if (copy_from_user
  909. (chip->data_buffer, (void __user *) buf, in_size)) {
  910. mutex_unlock(&chip->buffer_mutex);
  911. return -EFAULT;
  912. }
  913. /* atomic tpm command send and result receive */
  914. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  915. atomic_set(&chip->data_pending, out_size);
  916. mutex_unlock(&chip->buffer_mutex);
  917. /* Set a timeout by which the reader must come claim the result */
  918. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  919. return in_size;
  920. }
  921. EXPORT_SYMBOL_GPL(tpm_write);
  922. ssize_t tpm_read(struct file *file, char __user *buf,
  923. size_t size, loff_t *off)
  924. {
  925. struct tpm_chip *chip = file->private_data;
  926. ssize_t ret_size;
  927. del_singleshot_timer_sync(&chip->user_read_timer);
  928. flush_scheduled_work();
  929. ret_size = atomic_read(&chip->data_pending);
  930. atomic_set(&chip->data_pending, 0);
  931. if (ret_size > 0) { /* relay data */
  932. if (size < ret_size)
  933. ret_size = size;
  934. mutex_lock(&chip->buffer_mutex);
  935. if (copy_to_user(buf, chip->data_buffer, ret_size))
  936. ret_size = -EFAULT;
  937. mutex_unlock(&chip->buffer_mutex);
  938. }
  939. return ret_size;
  940. }
  941. EXPORT_SYMBOL_GPL(tpm_read);
  942. void tpm_remove_hardware(struct device *dev)
  943. {
  944. struct tpm_chip *chip = dev_get_drvdata(dev);
  945. if (chip == NULL) {
  946. dev_err(dev, "No device data found\n");
  947. return;
  948. }
  949. spin_lock(&driver_lock);
  950. list_del_rcu(&chip->list);
  951. spin_unlock(&driver_lock);
  952. synchronize_rcu();
  953. misc_deregister(&chip->vendor.miscdev);
  954. sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
  955. tpm_bios_log_teardown(chip->bios_dir);
  956. /* write it this way to be explicit (chip->dev == dev) */
  957. put_device(chip->dev);
  958. }
  959. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  960. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  961. #define SAVESTATE_RESULT_SIZE 10
  962. static struct tpm_input_header savestate_header = {
  963. .tag = TPM_TAG_RQU_COMMAND,
  964. .length = cpu_to_be32(10),
  965. .ordinal = TPM_ORD_SAVESTATE
  966. };
  967. /*
  968. * We are about to suspend. Save the TPM state
  969. * so that it can be restored.
  970. */
  971. int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
  972. {
  973. struct tpm_chip *chip = dev_get_drvdata(dev);
  974. struct tpm_cmd_t cmd;
  975. int rc;
  976. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  977. if (chip == NULL)
  978. return -ENODEV;
  979. /* for buggy tpm, flush pcrs with extend to selected dummy */
  980. if (tpm_suspend_pcr) {
  981. cmd.header.in = pcrextend_header;
  982. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  983. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  984. TPM_DIGEST_SIZE);
  985. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  986. "extending dummy pcr before suspend");
  987. }
  988. /* now do the actual savestate */
  989. cmd.header.in = savestate_header;
  990. rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
  991. "sending savestate before suspend");
  992. return rc;
  993. }
  994. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  995. /*
  996. * Resume from a power safe. The BIOS already restored
  997. * the TPM state.
  998. */
  999. int tpm_pm_resume(struct device *dev)
  1000. {
  1001. struct tpm_chip *chip = dev_get_drvdata(dev);
  1002. if (chip == NULL)
  1003. return -ENODEV;
  1004. return 0;
  1005. }
  1006. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  1007. /* In case vendor provided release function, call it too.*/
  1008. void tpm_dev_vendor_release(struct tpm_chip *chip)
  1009. {
  1010. if (chip->vendor.release)
  1011. chip->vendor.release(chip->dev);
  1012. clear_bit(chip->dev_num, dev_mask);
  1013. kfree(chip->vendor.miscdev.name);
  1014. }
  1015. EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
  1016. /*
  1017. * Once all references to platform device are down to 0,
  1018. * release all allocated structures.
  1019. */
  1020. void tpm_dev_release(struct device *dev)
  1021. {
  1022. struct tpm_chip *chip = dev_get_drvdata(dev);
  1023. tpm_dev_vendor_release(chip);
  1024. chip->release(dev);
  1025. kfree(chip);
  1026. }
  1027. EXPORT_SYMBOL_GPL(tpm_dev_release);
  1028. /*
  1029. * Called from tpm_<specific>.c probe function only for devices
  1030. * the driver has determined it should claim. Prior to calling
  1031. * this function the specific probe function has called pci_enable_device
  1032. * upon errant exit from this function specific probe function should call
  1033. * pci_disable_device
  1034. */
  1035. struct tpm_chip *tpm_register_hardware(struct device *dev,
  1036. const struct tpm_vendor_specific *entry)
  1037. {
  1038. #define DEVNAME_SIZE 7
  1039. char *devname;
  1040. struct tpm_chip *chip;
  1041. /* Driver specific per-device data */
  1042. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1043. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  1044. if (chip == NULL || devname == NULL)
  1045. goto out_free;
  1046. mutex_init(&chip->buffer_mutex);
  1047. mutex_init(&chip->tpm_mutex);
  1048. INIT_LIST_HEAD(&chip->list);
  1049. INIT_WORK(&chip->work, timeout_work);
  1050. setup_timer(&chip->user_read_timer, user_reader_timeout,
  1051. (unsigned long)chip);
  1052. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  1053. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  1054. if (chip->dev_num >= TPM_NUM_DEVICES) {
  1055. dev_err(dev, "No available tpm device numbers\n");
  1056. goto out_free;
  1057. } else if (chip->dev_num == 0)
  1058. chip->vendor.miscdev.minor = TPM_MINOR;
  1059. else
  1060. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  1061. set_bit(chip->dev_num, dev_mask);
  1062. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  1063. chip->vendor.miscdev.name = devname;
  1064. chip->vendor.miscdev.parent = dev;
  1065. chip->dev = get_device(dev);
  1066. chip->release = dev->release;
  1067. dev->release = tpm_dev_release;
  1068. dev_set_drvdata(dev, chip);
  1069. if (misc_register(&chip->vendor.miscdev)) {
  1070. dev_err(chip->dev,
  1071. "unable to misc_register %s, minor %d\n",
  1072. chip->vendor.miscdev.name,
  1073. chip->vendor.miscdev.minor);
  1074. put_device(chip->dev);
  1075. return NULL;
  1076. }
  1077. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1078. misc_deregister(&chip->vendor.miscdev);
  1079. put_device(chip->dev);
  1080. return NULL;
  1081. }
  1082. chip->bios_dir = tpm_bios_log_setup(devname);
  1083. /* Make chip available */
  1084. spin_lock(&driver_lock);
  1085. list_add_rcu(&chip->list, &tpm_chip_list);
  1086. spin_unlock(&driver_lock);
  1087. return chip;
  1088. out_free:
  1089. kfree(chip);
  1090. kfree(devname);
  1091. return NULL;
  1092. }
  1093. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1094. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1095. MODULE_DESCRIPTION("TPM Driver");
  1096. MODULE_VERSION("2.0");
  1097. MODULE_LICENSE("GPL");