tpm.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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) != 0 ||
  532. be32_to_cpu(tpm_cmd.header.out.length)
  533. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  534. return;
  535. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  536. chip->vendor.duration[TPM_SHORT] =
  537. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  538. chip->vendor.duration[TPM_MEDIUM] =
  539. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  540. chip->vendor.duration[TPM_LONG] =
  541. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  542. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  543. * value wrong and apparently reports msecs rather than usecs. So we
  544. * fix up the resulting too-small TPM_SHORT value to make things work.
  545. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  546. */
  547. if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
  548. chip->vendor.duration[TPM_SHORT] = HZ;
  549. chip->vendor.duration[TPM_MEDIUM] *= 1000;
  550. chip->vendor.duration[TPM_LONG] *= 1000;
  551. chip->vendor.duration_adjusted = true;
  552. dev_info(chip->dev, "Adjusting TPM timeout parameters.");
  553. }
  554. }
  555. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  556. void tpm_continue_selftest(struct tpm_chip *chip)
  557. {
  558. u8 data[] = {
  559. 0, 193, /* TPM_TAG_RQU_COMMAND */
  560. 0, 0, 0, 10, /* length */
  561. 0, 0, 0, 83, /* TPM_ORD_GetCapability */
  562. };
  563. tpm_transmit(chip, data, sizeof(data));
  564. }
  565. EXPORT_SYMBOL_GPL(tpm_continue_selftest);
  566. ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
  567. char *buf)
  568. {
  569. cap_t cap;
  570. ssize_t rc;
  571. rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
  572. "attempting to determine the permanent enabled state");
  573. if (rc)
  574. return 0;
  575. rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
  576. return rc;
  577. }
  578. EXPORT_SYMBOL_GPL(tpm_show_enabled);
  579. ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
  580. char *buf)
  581. {
  582. cap_t cap;
  583. ssize_t rc;
  584. rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
  585. "attempting to determine the permanent active state");
  586. if (rc)
  587. return 0;
  588. rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
  589. return rc;
  590. }
  591. EXPORT_SYMBOL_GPL(tpm_show_active);
  592. ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
  593. char *buf)
  594. {
  595. cap_t cap;
  596. ssize_t rc;
  597. rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
  598. "attempting to determine the owner state");
  599. if (rc)
  600. return 0;
  601. rc = sprintf(buf, "%d\n", cap.owned);
  602. return rc;
  603. }
  604. EXPORT_SYMBOL_GPL(tpm_show_owned);
  605. ssize_t tpm_show_temp_deactivated(struct device * dev,
  606. struct device_attribute * attr, char *buf)
  607. {
  608. cap_t cap;
  609. ssize_t rc;
  610. rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
  611. "attempting to determine the temporary state");
  612. if (rc)
  613. return 0;
  614. rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
  615. return rc;
  616. }
  617. EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
  618. /*
  619. * tpm_chip_find_get - return tpm_chip for given chip number
  620. */
  621. static struct tpm_chip *tpm_chip_find_get(int chip_num)
  622. {
  623. struct tpm_chip *pos, *chip = NULL;
  624. rcu_read_lock();
  625. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  626. if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
  627. continue;
  628. if (try_module_get(pos->dev->driver->owner)) {
  629. chip = pos;
  630. break;
  631. }
  632. }
  633. rcu_read_unlock();
  634. return chip;
  635. }
  636. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  637. #define READ_PCR_RESULT_SIZE 30
  638. static struct tpm_input_header pcrread_header = {
  639. .tag = TPM_TAG_RQU_COMMAND,
  640. .length = cpu_to_be32(14),
  641. .ordinal = TPM_ORDINAL_PCRREAD
  642. };
  643. int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  644. {
  645. int rc;
  646. struct tpm_cmd_t cmd;
  647. cmd.header.in = pcrread_header;
  648. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  649. rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
  650. "attempting to read a pcr value");
  651. if (rc == 0)
  652. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  653. TPM_DIGEST_SIZE);
  654. return rc;
  655. }
  656. /**
  657. * tpm_pcr_read - read a pcr value
  658. * @chip_num: tpm idx # or ANY
  659. * @pcr_idx: pcr idx to retrieve
  660. * @res_buf: TPM_PCR value
  661. * size of res_buf is 20 bytes (or NULL if you don't care)
  662. *
  663. * The TPM driver should be built-in, but for whatever reason it
  664. * isn't, protect against the chip disappearing, by incrementing
  665. * the module usage count.
  666. */
  667. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  668. {
  669. struct tpm_chip *chip;
  670. int rc;
  671. chip = tpm_chip_find_get(chip_num);
  672. if (chip == NULL)
  673. return -ENODEV;
  674. rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
  675. tpm_chip_put(chip);
  676. return rc;
  677. }
  678. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  679. /**
  680. * tpm_pcr_extend - extend pcr value with hash
  681. * @chip_num: tpm idx # or AN&
  682. * @pcr_idx: pcr idx to extend
  683. * @hash: hash value used to extend pcr value
  684. *
  685. * The TPM driver should be built-in, but for whatever reason it
  686. * isn't, protect against the chip disappearing, by incrementing
  687. * the module usage count.
  688. */
  689. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  690. #define EXTEND_PCR_RESULT_SIZE 34
  691. static struct tpm_input_header pcrextend_header = {
  692. .tag = TPM_TAG_RQU_COMMAND,
  693. .length = cpu_to_be32(34),
  694. .ordinal = TPM_ORD_PCR_EXTEND
  695. };
  696. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  697. {
  698. struct tpm_cmd_t cmd;
  699. int rc;
  700. struct tpm_chip *chip;
  701. chip = tpm_chip_find_get(chip_num);
  702. if (chip == NULL)
  703. return -ENODEV;
  704. cmd.header.in = pcrextend_header;
  705. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  706. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  707. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  708. "attempting extend a PCR value");
  709. tpm_chip_put(chip);
  710. return rc;
  711. }
  712. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  713. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  714. {
  715. struct tpm_chip *chip;
  716. int rc;
  717. chip = tpm_chip_find_get(chip_num);
  718. if (chip == NULL)
  719. return -ENODEV;
  720. rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
  721. tpm_chip_put(chip);
  722. return rc;
  723. }
  724. EXPORT_SYMBOL_GPL(tpm_send);
  725. ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
  726. char *buf)
  727. {
  728. cap_t cap;
  729. u8 digest[TPM_DIGEST_SIZE];
  730. ssize_t rc;
  731. int i, j, num_pcrs;
  732. char *str = buf;
  733. struct tpm_chip *chip = dev_get_drvdata(dev);
  734. rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
  735. "attempting to determine the number of PCRS");
  736. if (rc)
  737. return 0;
  738. num_pcrs = be32_to_cpu(cap.num_pcrs);
  739. for (i = 0; i < num_pcrs; i++) {
  740. rc = __tpm_pcr_read(chip, i, digest);
  741. if (rc)
  742. break;
  743. str += sprintf(str, "PCR-%02d: ", i);
  744. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  745. str += sprintf(str, "%02X ", digest[j]);
  746. str += sprintf(str, "\n");
  747. }
  748. return str - buf;
  749. }
  750. EXPORT_SYMBOL_GPL(tpm_show_pcrs);
  751. #define READ_PUBEK_RESULT_SIZE 314
  752. #define TPM_ORD_READPUBEK cpu_to_be32(124)
  753. struct tpm_input_header tpm_readpubek_header = {
  754. .tag = TPM_TAG_RQU_COMMAND,
  755. .length = cpu_to_be32(30),
  756. .ordinal = TPM_ORD_READPUBEK
  757. };
  758. ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
  759. char *buf)
  760. {
  761. u8 *data;
  762. struct tpm_cmd_t tpm_cmd;
  763. ssize_t err;
  764. int i, rc;
  765. char *str = buf;
  766. struct tpm_chip *chip = dev_get_drvdata(dev);
  767. tpm_cmd.header.in = tpm_readpubek_header;
  768. err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
  769. "attempting to read the PUBEK");
  770. if (err)
  771. goto out;
  772. /*
  773. ignore header 10 bytes
  774. algorithm 32 bits (1 == RSA )
  775. encscheme 16 bits
  776. sigscheme 16 bits
  777. parameters (RSA 12->bytes: keybit, #primes, expbit)
  778. keylenbytes 32 bits
  779. 256 byte modulus
  780. ignore checksum 20 bytes
  781. */
  782. data = tpm_cmd.params.readpubek_out_buffer;
  783. str +=
  784. sprintf(str,
  785. "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
  786. "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
  787. " %02X %02X %02X %02X %02X %02X %02X %02X\n"
  788. "Modulus length: %d\nModulus: \n",
  789. data[10], data[11], data[12], data[13], data[14],
  790. data[15], data[16], data[17], data[22], data[23],
  791. data[24], data[25], data[26], data[27], data[28],
  792. data[29], data[30], data[31], data[32], data[33],
  793. be32_to_cpu(*((__be32 *) (data + 34))));
  794. for (i = 0; i < 256; i++) {
  795. str += sprintf(str, "%02X ", data[i + 38]);
  796. if ((i + 1) % 16 == 0)
  797. str += sprintf(str, "\n");
  798. }
  799. out:
  800. rc = str - buf;
  801. return rc;
  802. }
  803. EXPORT_SYMBOL_GPL(tpm_show_pubek);
  804. ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
  805. char *buf)
  806. {
  807. cap_t cap;
  808. ssize_t rc;
  809. char *str = buf;
  810. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  811. "attempting to determine the manufacturer");
  812. if (rc)
  813. return 0;
  814. str += sprintf(str, "Manufacturer: 0x%x\n",
  815. be32_to_cpu(cap.manufacturer_id));
  816. rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
  817. "attempting to determine the 1.1 version");
  818. if (rc)
  819. return 0;
  820. str += sprintf(str,
  821. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  822. cap.tpm_version.Major, cap.tpm_version.Minor,
  823. cap.tpm_version.revMajor, cap.tpm_version.revMinor);
  824. return str - buf;
  825. }
  826. EXPORT_SYMBOL_GPL(tpm_show_caps);
  827. ssize_t tpm_show_caps_1_2(struct device * dev,
  828. struct device_attribute * attr, char *buf)
  829. {
  830. cap_t cap;
  831. ssize_t rc;
  832. char *str = buf;
  833. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  834. "attempting to determine the manufacturer");
  835. if (rc)
  836. return 0;
  837. str += sprintf(str, "Manufacturer: 0x%x\n",
  838. be32_to_cpu(cap.manufacturer_id));
  839. rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
  840. "attempting to determine the 1.2 version");
  841. if (rc)
  842. return 0;
  843. str += sprintf(str,
  844. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  845. cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
  846. cap.tpm_version_1_2.revMajor,
  847. cap.tpm_version_1_2.revMinor);
  848. return str - buf;
  849. }
  850. EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
  851. ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
  852. char *buf)
  853. {
  854. struct tpm_chip *chip = dev_get_drvdata(dev);
  855. return sprintf(buf, "%d %d %d [%s]\n",
  856. jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
  857. jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
  858. jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
  859. chip->vendor.duration_adjusted
  860. ? "adjusted" : "original");
  861. }
  862. EXPORT_SYMBOL_GPL(tpm_show_durations);
  863. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  864. const char *buf, size_t count)
  865. {
  866. struct tpm_chip *chip = dev_get_drvdata(dev);
  867. if (chip == NULL)
  868. return 0;
  869. chip->vendor.cancel(chip);
  870. return count;
  871. }
  872. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  873. /*
  874. * Device file system interface to the TPM
  875. *
  876. * It's assured that the chip will be opened just once,
  877. * by the check of is_open variable, which is protected
  878. * by driver_lock.
  879. */
  880. int tpm_open(struct inode *inode, struct file *file)
  881. {
  882. int minor = iminor(inode);
  883. struct tpm_chip *chip = NULL, *pos;
  884. rcu_read_lock();
  885. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  886. if (pos->vendor.miscdev.minor == minor) {
  887. chip = pos;
  888. get_device(chip->dev);
  889. break;
  890. }
  891. }
  892. rcu_read_unlock();
  893. if (!chip)
  894. return -ENODEV;
  895. if (test_and_set_bit(0, &chip->is_open)) {
  896. dev_dbg(chip->dev, "Another process owns this TPM\n");
  897. put_device(chip->dev);
  898. return -EBUSY;
  899. }
  900. chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
  901. if (chip->data_buffer == NULL) {
  902. clear_bit(0, &chip->is_open);
  903. put_device(chip->dev);
  904. return -ENOMEM;
  905. }
  906. atomic_set(&chip->data_pending, 0);
  907. file->private_data = chip;
  908. return 0;
  909. }
  910. EXPORT_SYMBOL_GPL(tpm_open);
  911. /*
  912. * Called on file close
  913. */
  914. int tpm_release(struct inode *inode, struct file *file)
  915. {
  916. struct tpm_chip *chip = file->private_data;
  917. del_singleshot_timer_sync(&chip->user_read_timer);
  918. flush_work_sync(&chip->work);
  919. file->private_data = NULL;
  920. atomic_set(&chip->data_pending, 0);
  921. kfree(chip->data_buffer);
  922. clear_bit(0, &chip->is_open);
  923. put_device(chip->dev);
  924. return 0;
  925. }
  926. EXPORT_SYMBOL_GPL(tpm_release);
  927. ssize_t tpm_write(struct file *file, const char __user *buf,
  928. size_t size, loff_t *off)
  929. {
  930. struct tpm_chip *chip = file->private_data;
  931. size_t in_size = size, out_size;
  932. /* cannot perform a write until the read has cleared
  933. either via tpm_read or a user_read_timer timeout */
  934. while (atomic_read(&chip->data_pending) != 0)
  935. msleep(TPM_TIMEOUT);
  936. mutex_lock(&chip->buffer_mutex);
  937. if (in_size > TPM_BUFSIZE)
  938. in_size = TPM_BUFSIZE;
  939. if (copy_from_user
  940. (chip->data_buffer, (void __user *) buf, in_size)) {
  941. mutex_unlock(&chip->buffer_mutex);
  942. return -EFAULT;
  943. }
  944. /* atomic tpm command send and result receive */
  945. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  946. atomic_set(&chip->data_pending, out_size);
  947. mutex_unlock(&chip->buffer_mutex);
  948. /* Set a timeout by which the reader must come claim the result */
  949. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  950. return in_size;
  951. }
  952. EXPORT_SYMBOL_GPL(tpm_write);
  953. ssize_t tpm_read(struct file *file, char __user *buf,
  954. size_t size, loff_t *off)
  955. {
  956. struct tpm_chip *chip = file->private_data;
  957. ssize_t ret_size;
  958. del_singleshot_timer_sync(&chip->user_read_timer);
  959. flush_work_sync(&chip->work);
  960. ret_size = atomic_read(&chip->data_pending);
  961. atomic_set(&chip->data_pending, 0);
  962. if (ret_size > 0) { /* relay data */
  963. if (size < ret_size)
  964. ret_size = size;
  965. mutex_lock(&chip->buffer_mutex);
  966. if (copy_to_user(buf, chip->data_buffer, ret_size))
  967. ret_size = -EFAULT;
  968. mutex_unlock(&chip->buffer_mutex);
  969. }
  970. return ret_size;
  971. }
  972. EXPORT_SYMBOL_GPL(tpm_read);
  973. void tpm_remove_hardware(struct device *dev)
  974. {
  975. struct tpm_chip *chip = dev_get_drvdata(dev);
  976. if (chip == NULL) {
  977. dev_err(dev, "No device data found\n");
  978. return;
  979. }
  980. spin_lock(&driver_lock);
  981. list_del_rcu(&chip->list);
  982. spin_unlock(&driver_lock);
  983. synchronize_rcu();
  984. misc_deregister(&chip->vendor.miscdev);
  985. sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
  986. tpm_bios_log_teardown(chip->bios_dir);
  987. /* write it this way to be explicit (chip->dev == dev) */
  988. put_device(chip->dev);
  989. }
  990. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  991. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  992. #define SAVESTATE_RESULT_SIZE 10
  993. static struct tpm_input_header savestate_header = {
  994. .tag = TPM_TAG_RQU_COMMAND,
  995. .length = cpu_to_be32(10),
  996. .ordinal = TPM_ORD_SAVESTATE
  997. };
  998. /*
  999. * We are about to suspend. Save the TPM state
  1000. * so that it can be restored.
  1001. */
  1002. int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
  1003. {
  1004. struct tpm_chip *chip = dev_get_drvdata(dev);
  1005. struct tpm_cmd_t cmd;
  1006. int rc;
  1007. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  1008. if (chip == NULL)
  1009. return -ENODEV;
  1010. /* for buggy tpm, flush pcrs with extend to selected dummy */
  1011. if (tpm_suspend_pcr) {
  1012. cmd.header.in = pcrextend_header;
  1013. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  1014. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  1015. TPM_DIGEST_SIZE);
  1016. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  1017. "extending dummy pcr before suspend");
  1018. }
  1019. /* now do the actual savestate */
  1020. cmd.header.in = savestate_header;
  1021. rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
  1022. "sending savestate before suspend");
  1023. return rc;
  1024. }
  1025. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  1026. /*
  1027. * Resume from a power safe. The BIOS already restored
  1028. * the TPM state.
  1029. */
  1030. int tpm_pm_resume(struct device *dev)
  1031. {
  1032. struct tpm_chip *chip = dev_get_drvdata(dev);
  1033. if (chip == NULL)
  1034. return -ENODEV;
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  1038. /* In case vendor provided release function, call it too.*/
  1039. void tpm_dev_vendor_release(struct tpm_chip *chip)
  1040. {
  1041. if (chip->vendor.release)
  1042. chip->vendor.release(chip->dev);
  1043. clear_bit(chip->dev_num, dev_mask);
  1044. kfree(chip->vendor.miscdev.name);
  1045. }
  1046. EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
  1047. /*
  1048. * Once all references to platform device are down to 0,
  1049. * release all allocated structures.
  1050. */
  1051. void tpm_dev_release(struct device *dev)
  1052. {
  1053. struct tpm_chip *chip = dev_get_drvdata(dev);
  1054. tpm_dev_vendor_release(chip);
  1055. chip->release(dev);
  1056. kfree(chip);
  1057. }
  1058. EXPORT_SYMBOL_GPL(tpm_dev_release);
  1059. /*
  1060. * Called from tpm_<specific>.c probe function only for devices
  1061. * the driver has determined it should claim. Prior to calling
  1062. * this function the specific probe function has called pci_enable_device
  1063. * upon errant exit from this function specific probe function should call
  1064. * pci_disable_device
  1065. */
  1066. struct tpm_chip *tpm_register_hardware(struct device *dev,
  1067. const struct tpm_vendor_specific *entry)
  1068. {
  1069. #define DEVNAME_SIZE 7
  1070. char *devname;
  1071. struct tpm_chip *chip;
  1072. /* Driver specific per-device data */
  1073. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1074. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  1075. if (chip == NULL || devname == NULL)
  1076. goto out_free;
  1077. mutex_init(&chip->buffer_mutex);
  1078. mutex_init(&chip->tpm_mutex);
  1079. INIT_LIST_HEAD(&chip->list);
  1080. INIT_WORK(&chip->work, timeout_work);
  1081. setup_timer(&chip->user_read_timer, user_reader_timeout,
  1082. (unsigned long)chip);
  1083. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  1084. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  1085. if (chip->dev_num >= TPM_NUM_DEVICES) {
  1086. dev_err(dev, "No available tpm device numbers\n");
  1087. goto out_free;
  1088. } else if (chip->dev_num == 0)
  1089. chip->vendor.miscdev.minor = TPM_MINOR;
  1090. else
  1091. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  1092. set_bit(chip->dev_num, dev_mask);
  1093. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  1094. chip->vendor.miscdev.name = devname;
  1095. chip->vendor.miscdev.parent = dev;
  1096. chip->dev = get_device(dev);
  1097. chip->release = dev->release;
  1098. dev->release = tpm_dev_release;
  1099. dev_set_drvdata(dev, chip);
  1100. if (misc_register(&chip->vendor.miscdev)) {
  1101. dev_err(chip->dev,
  1102. "unable to misc_register %s, minor %d\n",
  1103. chip->vendor.miscdev.name,
  1104. chip->vendor.miscdev.minor);
  1105. put_device(chip->dev);
  1106. return NULL;
  1107. }
  1108. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1109. misc_deregister(&chip->vendor.miscdev);
  1110. put_device(chip->dev);
  1111. return NULL;
  1112. }
  1113. chip->bios_dir = tpm_bios_log_setup(devname);
  1114. /* Make chip available */
  1115. spin_lock(&driver_lock);
  1116. list_add_rcu(&chip->list, &tpm_chip_list);
  1117. spin_unlock(&driver_lock);
  1118. return chip;
  1119. out_free:
  1120. kfree(chip);
  1121. kfree(devname);
  1122. return NULL;
  1123. }
  1124. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1125. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1126. MODULE_DESCRIPTION("TPM Driver");
  1127. MODULE_VERSION("2.0");
  1128. MODULE_LICENSE("GPL");