tpm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. *
  4. * Authors:
  5. * Leendert van Doorn <leendert@watson.ibm.com>
  6. * Dave Safford <safford@watson.ibm.com>
  7. * Reiner Sailer <sailer@watson.ibm.com>
  8. * Kylene Hall <kjhall@us.ibm.com>
  9. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  11. *
  12. * Device driver for TCG/TCPA TPM (trusted platform module).
  13. * Specifications at www.trustedcomputinggroup.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation, version 2 of the
  18. * License.
  19. *
  20. * Note, the TPM chip is not interrupt driven (only polling)
  21. * and can have very long timeouts (minutes!). Hence the unusual
  22. * calls to msleep.
  23. *
  24. */
  25. #include <linux/poll.h>
  26. #include <linux/mutex.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/smp_lock.h>
  29. #include "tpm.h"
  30. enum tpm_const {
  31. TPM_MINOR = 224, /* officially assigned */
  32. TPM_BUFSIZE = 2048,
  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. BUILD_BUG_ON(cmd.header.in.length > READ_PCR_RESULT_SIZE);
  634. rc = transmit_cmd(chip, &cmd, cmd.header.in.length,
  635. "attempting to read a pcr value");
  636. if (rc == 0)
  637. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  638. TPM_DIGEST_SIZE);
  639. return rc;
  640. }
  641. /**
  642. * tpm_pcr_read - read a pcr value
  643. * @chip_num: tpm idx # or ANY
  644. * @pcr_idx: pcr idx to retrieve
  645. * @res_buf: TPM_PCR value
  646. * size of res_buf is 20 bytes (or NULL if you don't care)
  647. *
  648. * The TPM driver should be built-in, but for whatever reason it
  649. * isn't, protect against the chip disappearing, by incrementing
  650. * the module usage count.
  651. */
  652. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  653. {
  654. struct tpm_chip *chip;
  655. int rc;
  656. chip = tpm_chip_find_get(chip_num);
  657. if (chip == NULL)
  658. return -ENODEV;
  659. rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
  660. module_put(chip->dev->driver->owner);
  661. return rc;
  662. }
  663. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  664. /**
  665. * tpm_pcr_extend - extend pcr value with hash
  666. * @chip_num: tpm idx # or AN&
  667. * @pcr_idx: pcr idx to extend
  668. * @hash: hash value used to extend pcr value
  669. *
  670. * The TPM driver should be built-in, but for whatever reason it
  671. * isn't, protect against the chip disappearing, by incrementing
  672. * the module usage count.
  673. */
  674. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  675. #define EXTEND_PCR_SIZE 34
  676. static struct tpm_input_header pcrextend_header = {
  677. .tag = TPM_TAG_RQU_COMMAND,
  678. .length = cpu_to_be32(34),
  679. .ordinal = TPM_ORD_PCR_EXTEND
  680. };
  681. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  682. {
  683. struct tpm_cmd_t cmd;
  684. int rc;
  685. struct tpm_chip *chip;
  686. chip = tpm_chip_find_get(chip_num);
  687. if (chip == NULL)
  688. return -ENODEV;
  689. cmd.header.in = pcrextend_header;
  690. BUILD_BUG_ON(be32_to_cpu(cmd.header.in.length) > EXTEND_PCR_SIZE);
  691. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  692. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  693. rc = transmit_cmd(chip, &cmd, cmd.header.in.length,
  694. "attempting extend a PCR value");
  695. module_put(chip->dev->driver->owner);
  696. return rc;
  697. }
  698. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  699. ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
  700. char *buf)
  701. {
  702. cap_t cap;
  703. u8 digest[TPM_DIGEST_SIZE];
  704. ssize_t rc;
  705. int i, j, num_pcrs;
  706. char *str = buf;
  707. struct tpm_chip *chip = dev_get_drvdata(dev);
  708. rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
  709. "attempting to determine the number of PCRS");
  710. if (rc)
  711. return 0;
  712. num_pcrs = be32_to_cpu(cap.num_pcrs);
  713. for (i = 0; i < num_pcrs; i++) {
  714. rc = __tpm_pcr_read(chip, i, digest);
  715. if (rc)
  716. break;
  717. str += sprintf(str, "PCR-%02d: ", i);
  718. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  719. str += sprintf(str, "%02X ", digest[j]);
  720. str += sprintf(str, "\n");
  721. }
  722. return str - buf;
  723. }
  724. EXPORT_SYMBOL_GPL(tpm_show_pcrs);
  725. #define READ_PUBEK_RESULT_SIZE 314
  726. #define TPM_ORD_READPUBEK cpu_to_be32(124)
  727. struct tpm_input_header tpm_readpubek_header = {
  728. .tag = TPM_TAG_RQU_COMMAND,
  729. .length = cpu_to_be32(30),
  730. .ordinal = TPM_ORD_READPUBEK
  731. };
  732. ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
  733. char *buf)
  734. {
  735. u8 *data;
  736. struct tpm_cmd_t tpm_cmd;
  737. ssize_t err;
  738. int i, rc;
  739. char *str = buf;
  740. struct tpm_chip *chip = dev_get_drvdata(dev);
  741. tpm_cmd.header.in = tpm_readpubek_header;
  742. err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
  743. "attempting to read the PUBEK");
  744. if (err)
  745. goto out;
  746. /*
  747. ignore header 10 bytes
  748. algorithm 32 bits (1 == RSA )
  749. encscheme 16 bits
  750. sigscheme 16 bits
  751. parameters (RSA 12->bytes: keybit, #primes, expbit)
  752. keylenbytes 32 bits
  753. 256 byte modulus
  754. ignore checksum 20 bytes
  755. */
  756. data = tpm_cmd.params.readpubek_out_buffer;
  757. str +=
  758. sprintf(str,
  759. "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
  760. "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
  761. " %02X %02X %02X %02X %02X %02X %02X %02X\n"
  762. "Modulus length: %d\nModulus: \n",
  763. data[10], data[11], data[12], data[13], data[14],
  764. data[15], data[16], data[17], data[22], data[23],
  765. data[24], data[25], data[26], data[27], data[28],
  766. data[29], data[30], data[31], data[32], data[33],
  767. be32_to_cpu(*((__be32 *) (data + 34))));
  768. for (i = 0; i < 256; i++) {
  769. str += sprintf(str, "%02X ", data[i + 38]);
  770. if ((i + 1) % 16 == 0)
  771. str += sprintf(str, "\n");
  772. }
  773. out:
  774. rc = str - buf;
  775. return rc;
  776. }
  777. EXPORT_SYMBOL_GPL(tpm_show_pubek);
  778. ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
  779. char *buf)
  780. {
  781. cap_t cap;
  782. ssize_t rc;
  783. char *str = buf;
  784. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  785. "attempting to determine the manufacturer");
  786. if (rc)
  787. return 0;
  788. str += sprintf(str, "Manufacturer: 0x%x\n",
  789. be32_to_cpu(cap.manufacturer_id));
  790. rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
  791. "attempting to determine the 1.1 version");
  792. if (rc)
  793. return 0;
  794. str += sprintf(str,
  795. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  796. cap.tpm_version.Major, cap.tpm_version.Minor,
  797. cap.tpm_version.revMajor, cap.tpm_version.revMinor);
  798. return str - buf;
  799. }
  800. EXPORT_SYMBOL_GPL(tpm_show_caps);
  801. ssize_t tpm_show_caps_1_2(struct device * dev,
  802. struct device_attribute * attr, char *buf)
  803. {
  804. cap_t cap;
  805. ssize_t rc;
  806. char *str = buf;
  807. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  808. "attempting to determine the manufacturer");
  809. if (rc)
  810. return 0;
  811. str += sprintf(str, "Manufacturer: 0x%x\n",
  812. be32_to_cpu(cap.manufacturer_id));
  813. rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
  814. "attempting to determine the 1.2 version");
  815. if (rc)
  816. return 0;
  817. str += sprintf(str,
  818. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  819. cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
  820. cap.tpm_version_1_2.revMajor,
  821. cap.tpm_version_1_2.revMinor);
  822. return str - buf;
  823. }
  824. EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
  825. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  826. const char *buf, size_t count)
  827. {
  828. struct tpm_chip *chip = dev_get_drvdata(dev);
  829. if (chip == NULL)
  830. return 0;
  831. chip->vendor.cancel(chip);
  832. return count;
  833. }
  834. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  835. /*
  836. * Device file system interface to the TPM
  837. *
  838. * It's assured that the chip will be opened just once,
  839. * by the check of is_open variable, which is protected
  840. * by driver_lock.
  841. */
  842. int tpm_open(struct inode *inode, struct file *file)
  843. {
  844. int minor = iminor(inode);
  845. struct tpm_chip *chip = NULL, *pos;
  846. rcu_read_lock();
  847. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  848. if (pos->vendor.miscdev.minor == minor) {
  849. chip = pos;
  850. get_device(chip->dev);
  851. break;
  852. }
  853. }
  854. rcu_read_unlock();
  855. if (!chip)
  856. return -ENODEV;
  857. if (test_and_set_bit(0, &chip->is_open)) {
  858. dev_dbg(chip->dev, "Another process owns this TPM\n");
  859. put_device(chip->dev);
  860. return -EBUSY;
  861. }
  862. chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  863. if (chip->data_buffer == NULL) {
  864. clear_bit(0, &chip->is_open);
  865. put_device(chip->dev);
  866. return -ENOMEM;
  867. }
  868. atomic_set(&chip->data_pending, 0);
  869. file->private_data = chip;
  870. return 0;
  871. }
  872. EXPORT_SYMBOL_GPL(tpm_open);
  873. /*
  874. * Called on file close
  875. */
  876. int tpm_release(struct inode *inode, struct file *file)
  877. {
  878. struct tpm_chip *chip = file->private_data;
  879. del_singleshot_timer_sync(&chip->user_read_timer);
  880. flush_scheduled_work();
  881. file->private_data = NULL;
  882. atomic_set(&chip->data_pending, 0);
  883. kfree(chip->data_buffer);
  884. clear_bit(0, &chip->is_open);
  885. put_device(chip->dev);
  886. return 0;
  887. }
  888. EXPORT_SYMBOL_GPL(tpm_release);
  889. ssize_t tpm_write(struct file *file, const char __user *buf,
  890. size_t size, loff_t *off)
  891. {
  892. struct tpm_chip *chip = file->private_data;
  893. size_t in_size = size, out_size;
  894. /* cannot perform a write until the read has cleared
  895. either via tpm_read or a user_read_timer timeout */
  896. while (atomic_read(&chip->data_pending) != 0)
  897. msleep(TPM_TIMEOUT);
  898. mutex_lock(&chip->buffer_mutex);
  899. if (in_size > TPM_BUFSIZE)
  900. in_size = TPM_BUFSIZE;
  901. if (copy_from_user
  902. (chip->data_buffer, (void __user *) buf, in_size)) {
  903. mutex_unlock(&chip->buffer_mutex);
  904. return -EFAULT;
  905. }
  906. /* atomic tpm command send and result receive */
  907. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  908. atomic_set(&chip->data_pending, out_size);
  909. mutex_unlock(&chip->buffer_mutex);
  910. /* Set a timeout by which the reader must come claim the result */
  911. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  912. return in_size;
  913. }
  914. EXPORT_SYMBOL_GPL(tpm_write);
  915. ssize_t tpm_read(struct file *file, char __user *buf,
  916. size_t size, loff_t *off)
  917. {
  918. struct tpm_chip *chip = file->private_data;
  919. ssize_t ret_size;
  920. del_singleshot_timer_sync(&chip->user_read_timer);
  921. flush_scheduled_work();
  922. ret_size = atomic_read(&chip->data_pending);
  923. atomic_set(&chip->data_pending, 0);
  924. if (ret_size > 0) { /* relay data */
  925. if (size < ret_size)
  926. ret_size = size;
  927. mutex_lock(&chip->buffer_mutex);
  928. if (copy_to_user(buf, chip->data_buffer, ret_size))
  929. ret_size = -EFAULT;
  930. mutex_unlock(&chip->buffer_mutex);
  931. }
  932. return ret_size;
  933. }
  934. EXPORT_SYMBOL_GPL(tpm_read);
  935. void tpm_remove_hardware(struct device *dev)
  936. {
  937. struct tpm_chip *chip = dev_get_drvdata(dev);
  938. if (chip == NULL) {
  939. dev_err(dev, "No device data found\n");
  940. return;
  941. }
  942. spin_lock(&driver_lock);
  943. list_del_rcu(&chip->list);
  944. spin_unlock(&driver_lock);
  945. synchronize_rcu();
  946. misc_deregister(&chip->vendor.miscdev);
  947. sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
  948. tpm_bios_log_teardown(chip->bios_dir);
  949. /* write it this way to be explicit (chip->dev == dev) */
  950. put_device(chip->dev);
  951. }
  952. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  953. /*
  954. * We are about to suspend. Save the TPM state
  955. * so that it can be restored.
  956. */
  957. int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
  958. {
  959. struct tpm_chip *chip = dev_get_drvdata(dev);
  960. u8 savestate[] = {
  961. 0, 193, /* TPM_TAG_RQU_COMMAND */
  962. 0, 0, 0, 10, /* blob length (in bytes) */
  963. 0, 0, 0, 152 /* TPM_ORD_SaveState */
  964. };
  965. if (chip == NULL)
  966. return -ENODEV;
  967. tpm_transmit(chip, savestate, sizeof(savestate));
  968. return 0;
  969. }
  970. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  971. /*
  972. * Resume from a power safe. The BIOS already restored
  973. * the TPM state.
  974. */
  975. int tpm_pm_resume(struct device *dev)
  976. {
  977. struct tpm_chip *chip = dev_get_drvdata(dev);
  978. if (chip == NULL)
  979. return -ENODEV;
  980. return 0;
  981. }
  982. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  983. /* In case vendor provided release function, call it too.*/
  984. void tpm_dev_vendor_release(struct tpm_chip *chip)
  985. {
  986. if (chip->vendor.release)
  987. chip->vendor.release(chip->dev);
  988. clear_bit(chip->dev_num, dev_mask);
  989. kfree(chip->vendor.miscdev.name);
  990. }
  991. EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
  992. /*
  993. * Once all references to platform device are down to 0,
  994. * release all allocated structures.
  995. */
  996. void tpm_dev_release(struct device *dev)
  997. {
  998. struct tpm_chip *chip = dev_get_drvdata(dev);
  999. tpm_dev_vendor_release(chip);
  1000. chip->release(dev);
  1001. kfree(chip);
  1002. }
  1003. EXPORT_SYMBOL_GPL(tpm_dev_release);
  1004. /*
  1005. * Called from tpm_<specific>.c probe function only for devices
  1006. * the driver has determined it should claim. Prior to calling
  1007. * this function the specific probe function has called pci_enable_device
  1008. * upon errant exit from this function specific probe function should call
  1009. * pci_disable_device
  1010. */
  1011. struct tpm_chip *tpm_register_hardware(struct device *dev,
  1012. const struct tpm_vendor_specific *entry)
  1013. {
  1014. #define DEVNAME_SIZE 7
  1015. char *devname;
  1016. struct tpm_chip *chip;
  1017. /* Driver specific per-device data */
  1018. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1019. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  1020. if (chip == NULL || devname == NULL)
  1021. goto out_free;
  1022. mutex_init(&chip->buffer_mutex);
  1023. mutex_init(&chip->tpm_mutex);
  1024. INIT_LIST_HEAD(&chip->list);
  1025. INIT_WORK(&chip->work, timeout_work);
  1026. setup_timer(&chip->user_read_timer, user_reader_timeout,
  1027. (unsigned long)chip);
  1028. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  1029. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  1030. if (chip->dev_num >= TPM_NUM_DEVICES) {
  1031. dev_err(dev, "No available tpm device numbers\n");
  1032. goto out_free;
  1033. } else if (chip->dev_num == 0)
  1034. chip->vendor.miscdev.minor = TPM_MINOR;
  1035. else
  1036. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  1037. set_bit(chip->dev_num, dev_mask);
  1038. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  1039. chip->vendor.miscdev.name = devname;
  1040. chip->vendor.miscdev.parent = dev;
  1041. chip->dev = get_device(dev);
  1042. chip->release = dev->release;
  1043. dev->release = tpm_dev_release;
  1044. dev_set_drvdata(dev, chip);
  1045. if (misc_register(&chip->vendor.miscdev)) {
  1046. dev_err(chip->dev,
  1047. "unable to misc_register %s, minor %d\n",
  1048. chip->vendor.miscdev.name,
  1049. chip->vendor.miscdev.minor);
  1050. put_device(chip->dev);
  1051. return NULL;
  1052. }
  1053. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1054. misc_deregister(&chip->vendor.miscdev);
  1055. put_device(chip->dev);
  1056. return NULL;
  1057. }
  1058. chip->bios_dir = tpm_bios_log_setup(devname);
  1059. /* Make chip available */
  1060. spin_lock(&driver_lock);
  1061. list_add_rcu(&chip->list, &tpm_chip_list);
  1062. spin_unlock(&driver_lock);
  1063. return chip;
  1064. out_free:
  1065. kfree(chip);
  1066. kfree(devname);
  1067. return NULL;
  1068. }
  1069. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1070. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1071. MODULE_DESCRIPTION("TPM Driver");
  1072. MODULE_VERSION("2.0");
  1073. MODULE_LICENSE("GPL");