tpm.c 28 KB

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