tpm.c 28 KB

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