tpm.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 is 0, it's because chip->vendor.duration wasn't */
  354. /* filled yet, so we set the lowest timeout just to give enough */
  355. /* time for tpm_get_timeouts() to succeed */
  356. return (duration <= 0 ? HZ : duration);
  357. } else
  358. return 2 * 60 * HZ;
  359. }
  360. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  361. /*
  362. * Internal kernel interface to transmit TPM commands
  363. */
  364. static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  365. size_t bufsiz)
  366. {
  367. ssize_t rc;
  368. u32 count, ordinal;
  369. unsigned long stop;
  370. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  371. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  372. if (count == 0)
  373. return -ENODATA;
  374. if (count > bufsiz) {
  375. dev_err(chip->dev,
  376. "invalid count value %x %zx \n", count, bufsiz);
  377. return -E2BIG;
  378. }
  379. mutex_lock(&chip->tpm_mutex);
  380. if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
  381. dev_err(chip->dev,
  382. "tpm_transmit: tpm_send: error %zd\n", rc);
  383. goto out;
  384. }
  385. if (chip->vendor.irq)
  386. goto out_recv;
  387. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  388. do {
  389. u8 status = chip->vendor.status(chip);
  390. if ((status & chip->vendor.req_complete_mask) ==
  391. chip->vendor.req_complete_val)
  392. goto out_recv;
  393. if ((status == chip->vendor.req_canceled)) {
  394. dev_err(chip->dev, "Operation Canceled\n");
  395. rc = -ECANCELED;
  396. goto out;
  397. }
  398. msleep(TPM_TIMEOUT); /* CHECK */
  399. rmb();
  400. } while (time_before(jiffies, stop));
  401. chip->vendor.cancel(chip);
  402. dev_err(chip->dev, "Operation Timed out\n");
  403. rc = -ETIME;
  404. goto out;
  405. out_recv:
  406. rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
  407. if (rc < 0)
  408. dev_err(chip->dev,
  409. "tpm_transmit: tpm_recv: error %zd\n", rc);
  410. out:
  411. mutex_unlock(&chip->tpm_mutex);
  412. return rc;
  413. }
  414. #define TPM_DIGEST_SIZE 20
  415. #define TPM_ERROR_SIZE 10
  416. #define TPM_RET_CODE_IDX 6
  417. enum tpm_capabilities {
  418. TPM_CAP_FLAG = cpu_to_be32(4),
  419. TPM_CAP_PROP = cpu_to_be32(5),
  420. CAP_VERSION_1_1 = cpu_to_be32(0x06),
  421. CAP_VERSION_1_2 = cpu_to_be32(0x1A)
  422. };
  423. enum tpm_sub_capabilities {
  424. TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
  425. TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
  426. TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
  427. TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
  428. TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
  429. TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
  430. TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
  431. };
  432. static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
  433. int len, const char *desc)
  434. {
  435. int err;
  436. len = tpm_transmit(chip,(u8 *) cmd, len);
  437. if (len < 0)
  438. return len;
  439. if (len == TPM_ERROR_SIZE) {
  440. err = be32_to_cpu(cmd->header.out.return_code);
  441. dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
  442. return err;
  443. }
  444. return 0;
  445. }
  446. #define TPM_INTERNAL_RESULT_SIZE 200
  447. #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
  448. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  449. static const struct tpm_input_header tpm_getcap_header = {
  450. .tag = TPM_TAG_RQU_COMMAND,
  451. .length = cpu_to_be32(22),
  452. .ordinal = TPM_ORD_GET_CAP
  453. };
  454. ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
  455. const char *desc)
  456. {
  457. struct tpm_cmd_t tpm_cmd;
  458. int rc;
  459. struct tpm_chip *chip = dev_get_drvdata(dev);
  460. tpm_cmd.header.in = tpm_getcap_header;
  461. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  462. tpm_cmd.params.getcap_in.cap = subcap_id;
  463. /*subcap field not necessary */
  464. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  465. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  466. } else {
  467. if (subcap_id == TPM_CAP_FLAG_PERM ||
  468. subcap_id == TPM_CAP_FLAG_VOL)
  469. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  470. else
  471. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  472. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  473. tpm_cmd.params.getcap_in.subcap = subcap_id;
  474. }
  475. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
  476. if (!rc)
  477. *cap = tpm_cmd.params.getcap_out.cap;
  478. return rc;
  479. }
  480. void tpm_gen_interrupt(struct tpm_chip *chip)
  481. {
  482. struct tpm_cmd_t tpm_cmd;
  483. ssize_t rc;
  484. tpm_cmd.header.in = tpm_getcap_header;
  485. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  486. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  487. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  488. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  489. "attempting to determine the timeouts");
  490. }
  491. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  492. void tpm_get_timeouts(struct tpm_chip *chip)
  493. {
  494. struct tpm_cmd_t tpm_cmd;
  495. struct timeout_t *timeout_cap;
  496. struct duration_t *duration_cap;
  497. ssize_t rc;
  498. u32 timeout;
  499. tpm_cmd.header.in = tpm_getcap_header;
  500. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  501. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  502. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  503. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  504. "attempting to determine the timeouts");
  505. if (rc)
  506. goto duration;
  507. if (be32_to_cpu(tpm_cmd.header.out.length)
  508. != 4 * sizeof(u32))
  509. goto duration;
  510. timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
  511. /* Don't overwrite default if value is 0 */
  512. timeout = be32_to_cpu(timeout_cap->a);
  513. if (timeout)
  514. chip->vendor.timeout_a = usecs_to_jiffies(timeout);
  515. timeout = be32_to_cpu(timeout_cap->b);
  516. if (timeout)
  517. chip->vendor.timeout_b = usecs_to_jiffies(timeout);
  518. timeout = be32_to_cpu(timeout_cap->c);
  519. if (timeout)
  520. chip->vendor.timeout_c = usecs_to_jiffies(timeout);
  521. timeout = be32_to_cpu(timeout_cap->d);
  522. if (timeout)
  523. chip->vendor.timeout_d = usecs_to_jiffies(timeout);
  524. duration:
  525. tpm_cmd.header.in = tpm_getcap_header;
  526. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  527. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  528. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  529. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  530. "attempting to determine the durations");
  531. if (rc)
  532. return;
  533. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  534. be32_to_cpu(tpm_cmd.header.out.length)
  535. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  536. return;
  537. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  538. chip->vendor.duration[TPM_SHORT] =
  539. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  540. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  541. * value wrong and apparently reports msecs rather than usecs. So we
  542. * fix up the resulting too-small TPM_SHORT value to make things work.
  543. */
  544. if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
  545. chip->vendor.duration[TPM_SHORT] = HZ;
  546. chip->vendor.duration[TPM_MEDIUM] =
  547. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  548. chip->vendor.duration[TPM_LONG] =
  549. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  550. }
  551. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  552. void tpm_continue_selftest(struct tpm_chip *chip)
  553. {
  554. u8 data[] = {
  555. 0, 193, /* TPM_TAG_RQU_COMMAND */
  556. 0, 0, 0, 10, /* length */
  557. 0, 0, 0, 83, /* TPM_ORD_GetCapability */
  558. };
  559. tpm_transmit(chip, data, sizeof(data));
  560. }
  561. EXPORT_SYMBOL_GPL(tpm_continue_selftest);
  562. ssize_t tpm_show_enabled(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 enabled state");
  569. if (rc)
  570. return 0;
  571. rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
  572. return rc;
  573. }
  574. EXPORT_SYMBOL_GPL(tpm_show_enabled);
  575. ssize_t tpm_show_active(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_FLAG_PERM, &cap,
  581. "attempting to determine the permanent active state");
  582. if (rc)
  583. return 0;
  584. rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
  585. return rc;
  586. }
  587. EXPORT_SYMBOL_GPL(tpm_show_active);
  588. ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
  589. char *buf)
  590. {
  591. cap_t cap;
  592. ssize_t rc;
  593. rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
  594. "attempting to determine the owner state");
  595. if (rc)
  596. return 0;
  597. rc = sprintf(buf, "%d\n", cap.owned);
  598. return rc;
  599. }
  600. EXPORT_SYMBOL_GPL(tpm_show_owned);
  601. ssize_t tpm_show_temp_deactivated(struct device * dev,
  602. struct device_attribute * attr, char *buf)
  603. {
  604. cap_t cap;
  605. ssize_t rc;
  606. rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
  607. "attempting to determine the temporary state");
  608. if (rc)
  609. return 0;
  610. rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
  611. return rc;
  612. }
  613. EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
  614. /*
  615. * tpm_chip_find_get - return tpm_chip for given chip number
  616. */
  617. static struct tpm_chip *tpm_chip_find_get(int chip_num)
  618. {
  619. struct tpm_chip *pos, *chip = NULL;
  620. rcu_read_lock();
  621. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  622. if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
  623. continue;
  624. if (try_module_get(pos->dev->driver->owner)) {
  625. chip = pos;
  626. break;
  627. }
  628. }
  629. rcu_read_unlock();
  630. return chip;
  631. }
  632. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  633. #define READ_PCR_RESULT_SIZE 30
  634. static struct tpm_input_header pcrread_header = {
  635. .tag = TPM_TAG_RQU_COMMAND,
  636. .length = cpu_to_be32(14),
  637. .ordinal = TPM_ORDINAL_PCRREAD
  638. };
  639. int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  640. {
  641. int rc;
  642. struct tpm_cmd_t cmd;
  643. cmd.header.in = pcrread_header;
  644. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  645. rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
  646. "attempting to read a pcr value");
  647. if (rc == 0)
  648. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  649. TPM_DIGEST_SIZE);
  650. return rc;
  651. }
  652. /**
  653. * tpm_pcr_read - read a pcr value
  654. * @chip_num: tpm idx # or ANY
  655. * @pcr_idx: pcr idx to retrieve
  656. * @res_buf: TPM_PCR value
  657. * size of res_buf is 20 bytes (or NULL if you don't care)
  658. *
  659. * The TPM driver should be built-in, but for whatever reason it
  660. * isn't, protect against the chip disappearing, by incrementing
  661. * the module usage count.
  662. */
  663. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  664. {
  665. struct tpm_chip *chip;
  666. int rc;
  667. chip = tpm_chip_find_get(chip_num);
  668. if (chip == NULL)
  669. return -ENODEV;
  670. rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
  671. tpm_chip_put(chip);
  672. return rc;
  673. }
  674. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  675. /**
  676. * tpm_pcr_extend - extend pcr value with hash
  677. * @chip_num: tpm idx # or AN&
  678. * @pcr_idx: pcr idx to extend
  679. * @hash: hash value used to extend pcr value
  680. *
  681. * The TPM driver should be built-in, but for whatever reason it
  682. * isn't, protect against the chip disappearing, by incrementing
  683. * the module usage count.
  684. */
  685. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  686. #define EXTEND_PCR_RESULT_SIZE 34
  687. static struct tpm_input_header pcrextend_header = {
  688. .tag = TPM_TAG_RQU_COMMAND,
  689. .length = cpu_to_be32(34),
  690. .ordinal = TPM_ORD_PCR_EXTEND
  691. };
  692. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  693. {
  694. struct tpm_cmd_t cmd;
  695. int rc;
  696. struct tpm_chip *chip;
  697. chip = tpm_chip_find_get(chip_num);
  698. if (chip == NULL)
  699. return -ENODEV;
  700. cmd.header.in = pcrextend_header;
  701. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  702. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  703. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  704. "attempting extend a PCR value");
  705. tpm_chip_put(chip);
  706. return rc;
  707. }
  708. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  709. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  710. {
  711. struct tpm_chip *chip;
  712. int rc;
  713. chip = tpm_chip_find_get(chip_num);
  714. if (chip == NULL)
  715. return -ENODEV;
  716. rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
  717. tpm_chip_put(chip);
  718. return rc;
  719. }
  720. EXPORT_SYMBOL_GPL(tpm_send);
  721. ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
  722. char *buf)
  723. {
  724. cap_t cap;
  725. u8 digest[TPM_DIGEST_SIZE];
  726. ssize_t rc;
  727. int i, j, num_pcrs;
  728. char *str = buf;
  729. struct tpm_chip *chip = dev_get_drvdata(dev);
  730. rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
  731. "attempting to determine the number of PCRS");
  732. if (rc)
  733. return 0;
  734. num_pcrs = be32_to_cpu(cap.num_pcrs);
  735. for (i = 0; i < num_pcrs; i++) {
  736. rc = __tpm_pcr_read(chip, i, digest);
  737. if (rc)
  738. break;
  739. str += sprintf(str, "PCR-%02d: ", i);
  740. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  741. str += sprintf(str, "%02X ", digest[j]);
  742. str += sprintf(str, "\n");
  743. }
  744. return str - buf;
  745. }
  746. EXPORT_SYMBOL_GPL(tpm_show_pcrs);
  747. #define READ_PUBEK_RESULT_SIZE 314
  748. #define TPM_ORD_READPUBEK cpu_to_be32(124)
  749. struct tpm_input_header tpm_readpubek_header = {
  750. .tag = TPM_TAG_RQU_COMMAND,
  751. .length = cpu_to_be32(30),
  752. .ordinal = TPM_ORD_READPUBEK
  753. };
  754. ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
  755. char *buf)
  756. {
  757. u8 *data;
  758. struct tpm_cmd_t tpm_cmd;
  759. ssize_t err;
  760. int i, rc;
  761. char *str = buf;
  762. struct tpm_chip *chip = dev_get_drvdata(dev);
  763. tpm_cmd.header.in = tpm_readpubek_header;
  764. err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
  765. "attempting to read the PUBEK");
  766. if (err)
  767. goto out;
  768. /*
  769. ignore header 10 bytes
  770. algorithm 32 bits (1 == RSA )
  771. encscheme 16 bits
  772. sigscheme 16 bits
  773. parameters (RSA 12->bytes: keybit, #primes, expbit)
  774. keylenbytes 32 bits
  775. 256 byte modulus
  776. ignore checksum 20 bytes
  777. */
  778. data = tpm_cmd.params.readpubek_out_buffer;
  779. str +=
  780. sprintf(str,
  781. "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
  782. "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
  783. " %02X %02X %02X %02X %02X %02X %02X %02X\n"
  784. "Modulus length: %d\nModulus: \n",
  785. data[10], data[11], data[12], data[13], data[14],
  786. data[15], data[16], data[17], data[22], data[23],
  787. data[24], data[25], data[26], data[27], data[28],
  788. data[29], data[30], data[31], data[32], data[33],
  789. be32_to_cpu(*((__be32 *) (data + 34))));
  790. for (i = 0; i < 256; i++) {
  791. str += sprintf(str, "%02X ", data[i + 38]);
  792. if ((i + 1) % 16 == 0)
  793. str += sprintf(str, "\n");
  794. }
  795. out:
  796. rc = str - buf;
  797. return rc;
  798. }
  799. EXPORT_SYMBOL_GPL(tpm_show_pubek);
  800. ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
  801. 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_1, &cap,
  813. "attempting to determine the 1.1 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.Major, cap.tpm_version.Minor,
  819. cap.tpm_version.revMajor, cap.tpm_version.revMinor);
  820. return str - buf;
  821. }
  822. EXPORT_SYMBOL_GPL(tpm_show_caps);
  823. ssize_t tpm_show_caps_1_2(struct device * dev,
  824. struct device_attribute * attr, char *buf)
  825. {
  826. cap_t cap;
  827. ssize_t rc;
  828. char *str = buf;
  829. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  830. "attempting to determine the manufacturer");
  831. if (rc)
  832. return 0;
  833. str += sprintf(str, "Manufacturer: 0x%x\n",
  834. be32_to_cpu(cap.manufacturer_id));
  835. rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
  836. "attempting to determine the 1.2 version");
  837. if (rc)
  838. return 0;
  839. str += sprintf(str,
  840. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  841. cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
  842. cap.tpm_version_1_2.revMajor,
  843. cap.tpm_version_1_2.revMinor);
  844. return str - buf;
  845. }
  846. EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
  847. ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
  848. char *buf)
  849. {
  850. struct tpm_chip *chip = dev_get_drvdata(dev);
  851. return sprintf(buf, "%d %d %d\n",
  852. jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
  853. jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
  854. jiffies_to_usecs(chip->vendor.duration[TPM_LONG]));
  855. }
  856. EXPORT_SYMBOL_GPL(tpm_show_timeouts);
  857. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  858. const char *buf, size_t count)
  859. {
  860. struct tpm_chip *chip = dev_get_drvdata(dev);
  861. if (chip == NULL)
  862. return 0;
  863. chip->vendor.cancel(chip);
  864. return count;
  865. }
  866. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  867. /*
  868. * Device file system interface to the TPM
  869. *
  870. * It's assured that the chip will be opened just once,
  871. * by the check of is_open variable, which is protected
  872. * by driver_lock.
  873. */
  874. int tpm_open(struct inode *inode, struct file *file)
  875. {
  876. int minor = iminor(inode);
  877. struct tpm_chip *chip = NULL, *pos;
  878. rcu_read_lock();
  879. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  880. if (pos->vendor.miscdev.minor == minor) {
  881. chip = pos;
  882. get_device(chip->dev);
  883. break;
  884. }
  885. }
  886. rcu_read_unlock();
  887. if (!chip)
  888. return -ENODEV;
  889. if (test_and_set_bit(0, &chip->is_open)) {
  890. dev_dbg(chip->dev, "Another process owns this TPM\n");
  891. put_device(chip->dev);
  892. return -EBUSY;
  893. }
  894. chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  895. if (chip->data_buffer == NULL) {
  896. clear_bit(0, &chip->is_open);
  897. put_device(chip->dev);
  898. return -ENOMEM;
  899. }
  900. atomic_set(&chip->data_pending, 0);
  901. file->private_data = chip;
  902. return 0;
  903. }
  904. EXPORT_SYMBOL_GPL(tpm_open);
  905. /*
  906. * Called on file close
  907. */
  908. int tpm_release(struct inode *inode, struct file *file)
  909. {
  910. struct tpm_chip *chip = file->private_data;
  911. del_singleshot_timer_sync(&chip->user_read_timer);
  912. flush_work_sync(&chip->work);
  913. file->private_data = NULL;
  914. atomic_set(&chip->data_pending, 0);
  915. kfree(chip->data_buffer);
  916. clear_bit(0, &chip->is_open);
  917. put_device(chip->dev);
  918. return 0;
  919. }
  920. EXPORT_SYMBOL_GPL(tpm_release);
  921. ssize_t tpm_write(struct file *file, const char __user *buf,
  922. size_t size, loff_t *off)
  923. {
  924. struct tpm_chip *chip = file->private_data;
  925. size_t in_size = size, out_size;
  926. /* cannot perform a write until the read has cleared
  927. either via tpm_read or a user_read_timer timeout */
  928. while (atomic_read(&chip->data_pending) != 0)
  929. msleep(TPM_TIMEOUT);
  930. mutex_lock(&chip->buffer_mutex);
  931. if (in_size > TPM_BUFSIZE)
  932. in_size = TPM_BUFSIZE;
  933. if (copy_from_user
  934. (chip->data_buffer, (void __user *) buf, in_size)) {
  935. mutex_unlock(&chip->buffer_mutex);
  936. return -EFAULT;
  937. }
  938. /* atomic tpm command send and result receive */
  939. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  940. atomic_set(&chip->data_pending, out_size);
  941. mutex_unlock(&chip->buffer_mutex);
  942. /* Set a timeout by which the reader must come claim the result */
  943. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  944. return in_size;
  945. }
  946. EXPORT_SYMBOL_GPL(tpm_write);
  947. ssize_t tpm_read(struct file *file, char __user *buf,
  948. size_t size, loff_t *off)
  949. {
  950. struct tpm_chip *chip = file->private_data;
  951. ssize_t ret_size;
  952. del_singleshot_timer_sync(&chip->user_read_timer);
  953. flush_work_sync(&chip->work);
  954. ret_size = atomic_read(&chip->data_pending);
  955. atomic_set(&chip->data_pending, 0);
  956. if (ret_size > 0) { /* relay data */
  957. if (size < ret_size)
  958. ret_size = size;
  959. mutex_lock(&chip->buffer_mutex);
  960. if (copy_to_user(buf, chip->data_buffer, ret_size))
  961. ret_size = -EFAULT;
  962. mutex_unlock(&chip->buffer_mutex);
  963. }
  964. return ret_size;
  965. }
  966. EXPORT_SYMBOL_GPL(tpm_read);
  967. void tpm_remove_hardware(struct device *dev)
  968. {
  969. struct tpm_chip *chip = dev_get_drvdata(dev);
  970. if (chip == NULL) {
  971. dev_err(dev, "No device data found\n");
  972. return;
  973. }
  974. spin_lock(&driver_lock);
  975. list_del_rcu(&chip->list);
  976. spin_unlock(&driver_lock);
  977. synchronize_rcu();
  978. misc_deregister(&chip->vendor.miscdev);
  979. sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
  980. tpm_bios_log_teardown(chip->bios_dir);
  981. /* write it this way to be explicit (chip->dev == dev) */
  982. put_device(chip->dev);
  983. }
  984. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  985. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  986. #define SAVESTATE_RESULT_SIZE 10
  987. static struct tpm_input_header savestate_header = {
  988. .tag = TPM_TAG_RQU_COMMAND,
  989. .length = cpu_to_be32(10),
  990. .ordinal = TPM_ORD_SAVESTATE
  991. };
  992. /*
  993. * We are about to suspend. Save the TPM state
  994. * so that it can be restored.
  995. */
  996. int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
  997. {
  998. struct tpm_chip *chip = dev_get_drvdata(dev);
  999. struct tpm_cmd_t cmd;
  1000. int rc;
  1001. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  1002. if (chip == NULL)
  1003. return -ENODEV;
  1004. /* for buggy tpm, flush pcrs with extend to selected dummy */
  1005. if (tpm_suspend_pcr) {
  1006. cmd.header.in = pcrextend_header;
  1007. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  1008. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  1009. TPM_DIGEST_SIZE);
  1010. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  1011. "extending dummy pcr before suspend");
  1012. }
  1013. /* now do the actual savestate */
  1014. cmd.header.in = savestate_header;
  1015. rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
  1016. "sending savestate before suspend");
  1017. return rc;
  1018. }
  1019. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  1020. /*
  1021. * Resume from a power safe. The BIOS already restored
  1022. * the TPM state.
  1023. */
  1024. int tpm_pm_resume(struct device *dev)
  1025. {
  1026. struct tpm_chip *chip = dev_get_drvdata(dev);
  1027. if (chip == NULL)
  1028. return -ENODEV;
  1029. return 0;
  1030. }
  1031. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  1032. /* In case vendor provided release function, call it too.*/
  1033. void tpm_dev_vendor_release(struct tpm_chip *chip)
  1034. {
  1035. if (chip->vendor.release)
  1036. chip->vendor.release(chip->dev);
  1037. clear_bit(chip->dev_num, dev_mask);
  1038. kfree(chip->vendor.miscdev.name);
  1039. }
  1040. EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
  1041. /*
  1042. * Once all references to platform device are down to 0,
  1043. * release all allocated structures.
  1044. */
  1045. void tpm_dev_release(struct device *dev)
  1046. {
  1047. struct tpm_chip *chip = dev_get_drvdata(dev);
  1048. tpm_dev_vendor_release(chip);
  1049. chip->release(dev);
  1050. kfree(chip);
  1051. }
  1052. EXPORT_SYMBOL_GPL(tpm_dev_release);
  1053. /*
  1054. * Called from tpm_<specific>.c probe function only for devices
  1055. * the driver has determined it should claim. Prior to calling
  1056. * this function the specific probe function has called pci_enable_device
  1057. * upon errant exit from this function specific probe function should call
  1058. * pci_disable_device
  1059. */
  1060. struct tpm_chip *tpm_register_hardware(struct device *dev,
  1061. const struct tpm_vendor_specific *entry)
  1062. {
  1063. #define DEVNAME_SIZE 7
  1064. char *devname;
  1065. struct tpm_chip *chip;
  1066. /* Driver specific per-device data */
  1067. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1068. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  1069. if (chip == NULL || devname == NULL)
  1070. goto out_free;
  1071. mutex_init(&chip->buffer_mutex);
  1072. mutex_init(&chip->tpm_mutex);
  1073. INIT_LIST_HEAD(&chip->list);
  1074. INIT_WORK(&chip->work, timeout_work);
  1075. setup_timer(&chip->user_read_timer, user_reader_timeout,
  1076. (unsigned long)chip);
  1077. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  1078. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  1079. if (chip->dev_num >= TPM_NUM_DEVICES) {
  1080. dev_err(dev, "No available tpm device numbers\n");
  1081. goto out_free;
  1082. } else if (chip->dev_num == 0)
  1083. chip->vendor.miscdev.minor = TPM_MINOR;
  1084. else
  1085. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  1086. set_bit(chip->dev_num, dev_mask);
  1087. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  1088. chip->vendor.miscdev.name = devname;
  1089. chip->vendor.miscdev.parent = dev;
  1090. chip->dev = get_device(dev);
  1091. chip->release = dev->release;
  1092. dev->release = tpm_dev_release;
  1093. dev_set_drvdata(dev, chip);
  1094. if (misc_register(&chip->vendor.miscdev)) {
  1095. dev_err(chip->dev,
  1096. "unable to misc_register %s, minor %d\n",
  1097. chip->vendor.miscdev.name,
  1098. chip->vendor.miscdev.minor);
  1099. put_device(chip->dev);
  1100. return NULL;
  1101. }
  1102. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1103. misc_deregister(&chip->vendor.miscdev);
  1104. put_device(chip->dev);
  1105. return NULL;
  1106. }
  1107. chip->bios_dir = tpm_bios_log_setup(devname);
  1108. /* Make chip available */
  1109. spin_lock(&driver_lock);
  1110. list_add_rcu(&chip->list, &tpm_chip_list);
  1111. spin_unlock(&driver_lock);
  1112. return chip;
  1113. out_free:
  1114. kfree(chip);
  1115. kfree(devname);
  1116. return NULL;
  1117. }
  1118. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1119. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1120. MODULE_DESCRIPTION("TPM Driver");
  1121. MODULE_VERSION("2.0");
  1122. MODULE_LICENSE("GPL");