tpm.c 29 KB

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