tpm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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/sched.h>
  26. #include <linux/poll.h>
  27. #include <linux/spinlock.h>
  28. #include "tpm.h"
  29. enum tpm_const {
  30. TPM_MINOR = 224, /* officially assigned */
  31. TPM_BUFSIZE = 2048,
  32. TPM_NUM_DEVICES = 256,
  33. TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
  34. };
  35. /* PCI configuration addresses */
  36. enum tpm_pci_config_addr {
  37. PCI_GEN_PMCON_1 = 0xA0,
  38. PCI_GEN1_DEC = 0xE4,
  39. PCI_LPC_EN = 0xE6,
  40. PCI_GEN2_DEC = 0xEC
  41. };
  42. enum tpm_config {
  43. TPM_LOCK_REG = 0x0D,
  44. TPM_INTERUPT_REG = 0x0A,
  45. TPM_BASE_ADDR_LO = 0x08,
  46. TPM_BASE_ADDR_HI = 0x09,
  47. TPM_UNLOCK_VALUE = 0x55,
  48. TPM_LOCK_VALUE = 0xAA,
  49. TPM_DISABLE_INTERUPT_VALUE = 0x00
  50. };
  51. static LIST_HEAD(tpm_chip_list);
  52. static DEFINE_SPINLOCK(driver_lock);
  53. static int dev_mask[TPM_NUM_MASK_ENTRIES];
  54. static void user_reader_timeout(unsigned long ptr)
  55. {
  56. struct tpm_chip *chip = (struct tpm_chip *) ptr;
  57. down(&chip->buffer_mutex);
  58. atomic_set(&chip->data_pending, 0);
  59. memset(chip->data_buffer, 0, TPM_BUFSIZE);
  60. up(&chip->buffer_mutex);
  61. }
  62. /*
  63. * Initialize the LPC bus and enable the TPM ports
  64. */
  65. int tpm_lpc_bus_init(struct pci_dev *pci_dev, u16 base)
  66. {
  67. u32 lpcenable, tmp;
  68. int is_lpcm = 0;
  69. switch (pci_dev->vendor) {
  70. case PCI_VENDOR_ID_INTEL:
  71. switch (pci_dev->device) {
  72. case PCI_DEVICE_ID_INTEL_82801CA_12:
  73. case PCI_DEVICE_ID_INTEL_82801DB_12:
  74. is_lpcm = 1;
  75. break;
  76. }
  77. /* init ICH (enable LPC) */
  78. pci_read_config_dword(pci_dev, PCI_GEN1_DEC, &lpcenable);
  79. lpcenable |= 0x20000000;
  80. pci_write_config_dword(pci_dev, PCI_GEN1_DEC, lpcenable);
  81. if (is_lpcm) {
  82. pci_read_config_dword(pci_dev, PCI_GEN1_DEC,
  83. &lpcenable);
  84. if ((lpcenable & 0x20000000) == 0) {
  85. dev_err(&pci_dev->dev,
  86. "cannot enable LPC\n");
  87. return -ENODEV;
  88. }
  89. }
  90. /* initialize TPM registers */
  91. pci_read_config_dword(pci_dev, PCI_GEN2_DEC, &tmp);
  92. if (!is_lpcm)
  93. tmp = (tmp & 0xFFFF0000) | (base & 0xFFF0);
  94. else
  95. tmp =
  96. (tmp & 0xFFFF0000) | (base & 0xFFF0) |
  97. 0x00000001;
  98. pci_write_config_dword(pci_dev, PCI_GEN2_DEC, tmp);
  99. if (is_lpcm) {
  100. pci_read_config_dword(pci_dev, PCI_GEN_PMCON_1,
  101. &tmp);
  102. tmp |= 0x00000004; /* enable CLKRUN */
  103. pci_write_config_dword(pci_dev, PCI_GEN_PMCON_1,
  104. tmp);
  105. }
  106. break;
  107. case PCI_VENDOR_ID_AMD:
  108. /* nothing yet */
  109. break;
  110. }
  111. tpm_write_index(TPM_LOCK_REG, TPM_UNLOCK_VALUE);
  112. tpm_write_index(TPM_INTERUPT_REG, TPM_DISABLE_INTERUPT_VALUE);
  113. tpm_write_index(TPM_BASE_ADDR_LO, base);
  114. tpm_write_index(TPM_BASE_ADDR_HI, (base & 0xFF00) >> 8);
  115. tpm_write_index(TPM_LOCK_REG, TPM_LOCK_VALUE);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL_GPL(tpm_lpc_bus_init);
  119. /*
  120. * Internal kernel interface to transmit TPM commands
  121. */
  122. static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  123. size_t bufsiz)
  124. {
  125. ssize_t len;
  126. u32 count;
  127. __be32 *native_size;
  128. unsigned long stop;
  129. native_size = (__force __be32 *) (buf + 2);
  130. count = be32_to_cpu(*native_size);
  131. if (count == 0)
  132. return -ENODATA;
  133. if (count > bufsiz) {
  134. dev_err(&chip->pci_dev->dev,
  135. "invalid count value %x %zx \n", count, bufsiz);
  136. return -E2BIG;
  137. }
  138. down(&chip->tpm_mutex);
  139. if ((len = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
  140. dev_err(&chip->pci_dev->dev,
  141. "tpm_transmit: tpm_send: error %zd\n", len);
  142. return len;
  143. }
  144. stop = jiffies + 2 * 60 * HZ;
  145. do {
  146. u8 status = inb(chip->vendor->base + 1);
  147. if ((status & chip->vendor->req_complete_mask) ==
  148. chip->vendor->req_complete_val) {
  149. goto out_recv;
  150. }
  151. msleep(TPM_TIMEOUT); /* CHECK */
  152. rmb();
  153. } while (time_before(jiffies, stop));
  154. chip->vendor->cancel(chip);
  155. dev_err(&chip->pci_dev->dev, "Time expired\n");
  156. up(&chip->tpm_mutex);
  157. return -EIO;
  158. out_recv:
  159. len = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
  160. if (len < 0)
  161. dev_err(&chip->pci_dev->dev,
  162. "tpm_transmit: tpm_recv: error %zd\n", len);
  163. up(&chip->tpm_mutex);
  164. return len;
  165. }
  166. #define TPM_DIGEST_SIZE 20
  167. #define CAP_PCR_RESULT_SIZE 18
  168. static u8 cap_pcr[] = {
  169. 0, 193, /* TPM_TAG_RQU_COMMAND */
  170. 0, 0, 0, 22, /* length */
  171. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  172. 0, 0, 0, 5,
  173. 0, 0, 0, 4,
  174. 0, 0, 1, 1
  175. };
  176. #define READ_PCR_RESULT_SIZE 30
  177. static u8 pcrread[] = {
  178. 0, 193, /* TPM_TAG_RQU_COMMAND */
  179. 0, 0, 0, 14, /* length */
  180. 0, 0, 0, 21, /* TPM_ORD_PcrRead */
  181. 0, 0, 0, 0 /* PCR index */
  182. };
  183. static ssize_t show_pcrs(struct device *dev, struct device_attribute *attr, char *buf)
  184. {
  185. u8 data[READ_PCR_RESULT_SIZE];
  186. ssize_t len;
  187. int i, j, index, num_pcrs;
  188. char *str = buf;
  189. struct tpm_chip *chip =
  190. pci_get_drvdata(container_of(dev, struct pci_dev, dev));
  191. if (chip == NULL)
  192. return -ENODEV;
  193. memcpy(data, cap_pcr, sizeof(cap_pcr));
  194. if ((len = tpm_transmit(chip, data, sizeof(data)))
  195. < CAP_PCR_RESULT_SIZE)
  196. return len;
  197. num_pcrs = be32_to_cpu(*((__force __be32 *) (data + 14)));
  198. for (i = 0; i < num_pcrs; i++) {
  199. memcpy(data, pcrread, sizeof(pcrread));
  200. index = cpu_to_be32(i);
  201. memcpy(data + 10, &index, 4);
  202. if ((len = tpm_transmit(chip, data, sizeof(data)))
  203. < READ_PCR_RESULT_SIZE)
  204. return len;
  205. str += sprintf(str, "PCR-%02d: ", i);
  206. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  207. str += sprintf(str, "%02X ", *(data + 10 + j));
  208. str += sprintf(str, "\n");
  209. }
  210. return str - buf;
  211. }
  212. static DEVICE_ATTR(pcrs, S_IRUGO, show_pcrs, NULL);
  213. #define READ_PUBEK_RESULT_SIZE 314
  214. static u8 readpubek[] = {
  215. 0, 193, /* TPM_TAG_RQU_COMMAND */
  216. 0, 0, 0, 30, /* length */
  217. 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
  218. };
  219. static ssize_t show_pubek(struct device *dev, struct device_attribute *attr, char *buf)
  220. {
  221. u8 data[READ_PUBEK_RESULT_SIZE];
  222. ssize_t len;
  223. __be32 *native_val;
  224. int i;
  225. char *str = buf;
  226. struct tpm_chip *chip =
  227. pci_get_drvdata(container_of(dev, struct pci_dev, dev));
  228. if (chip == NULL)
  229. return -ENODEV;
  230. memcpy(data, readpubek, sizeof(readpubek));
  231. memset(data + sizeof(readpubek), 0, 20); /* zero nonce */
  232. if ((len = tpm_transmit(chip, data, sizeof(data))) <
  233. READ_PUBEK_RESULT_SIZE)
  234. return len;
  235. /*
  236. ignore header 10 bytes
  237. algorithm 32 bits (1 == RSA )
  238. encscheme 16 bits
  239. sigscheme 16 bits
  240. parameters (RSA 12->bytes: keybit, #primes, expbit)
  241. keylenbytes 32 bits
  242. 256 byte modulus
  243. ignore checksum 20 bytes
  244. */
  245. native_val = (__force __be32 *) (data + 34);
  246. str +=
  247. sprintf(str,
  248. "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
  249. "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
  250. " %02X %02X %02X %02X %02X %02X %02X %02X\n"
  251. "Modulus length: %d\nModulus: \n",
  252. data[10], data[11], data[12], data[13], data[14],
  253. data[15], data[16], data[17], data[22], data[23],
  254. data[24], data[25], data[26], data[27], data[28],
  255. data[29], data[30], data[31], data[32], data[33],
  256. be32_to_cpu(*native_val)
  257. );
  258. for (i = 0; i < 256; i++) {
  259. str += sprintf(str, "%02X ", data[i + 39]);
  260. if ((i + 1) % 16 == 0)
  261. str += sprintf(str, "\n");
  262. }
  263. return str - buf;
  264. }
  265. static DEVICE_ATTR(pubek, S_IRUGO, show_pubek, NULL);
  266. #define CAP_VER_RESULT_SIZE 18
  267. static u8 cap_version[] = {
  268. 0, 193, /* TPM_TAG_RQU_COMMAND */
  269. 0, 0, 0, 18, /* length */
  270. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  271. 0, 0, 0, 6,
  272. 0, 0, 0, 0
  273. };
  274. #define CAP_MANUFACTURER_RESULT_SIZE 18
  275. static u8 cap_manufacturer[] = {
  276. 0, 193, /* TPM_TAG_RQU_COMMAND */
  277. 0, 0, 0, 22, /* length */
  278. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  279. 0, 0, 0, 5,
  280. 0, 0, 0, 4,
  281. 0, 0, 1, 3
  282. };
  283. static ssize_t show_caps(struct device *dev, struct device_attribute *attr, char *buf)
  284. {
  285. u8 data[READ_PUBEK_RESULT_SIZE];
  286. ssize_t len;
  287. char *str = buf;
  288. struct tpm_chip *chip =
  289. pci_get_drvdata(container_of(dev, struct pci_dev, dev));
  290. if (chip == NULL)
  291. return -ENODEV;
  292. memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
  293. if ((len = tpm_transmit(chip, data, sizeof(data))) <
  294. CAP_MANUFACTURER_RESULT_SIZE)
  295. return len;
  296. str += sprintf(str, "Manufacturer: 0x%x\n",
  297. be32_to_cpu(*(data + 14)));
  298. memcpy(data, cap_version, sizeof(cap_version));
  299. if ((len = tpm_transmit(chip, data, sizeof(data))) <
  300. CAP_VER_RESULT_SIZE)
  301. return len;
  302. str +=
  303. sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
  304. (int) data[14], (int) data[15], (int) data[16],
  305. (int) data[17]);
  306. return str - buf;
  307. }
  308. static DEVICE_ATTR(caps, S_IRUGO, show_caps, NULL);
  309. /*
  310. * Device file system interface to the TPM
  311. */
  312. int tpm_open(struct inode *inode, struct file *file)
  313. {
  314. int rc = 0, minor = iminor(inode);
  315. struct tpm_chip *chip = NULL, *pos;
  316. spin_lock(&driver_lock);
  317. list_for_each_entry(pos, &tpm_chip_list, list) {
  318. if (pos->vendor->miscdev.minor == minor) {
  319. chip = pos;
  320. break;
  321. }
  322. }
  323. if (chip == NULL) {
  324. rc = -ENODEV;
  325. goto err_out;
  326. }
  327. if (chip->num_opens) {
  328. dev_dbg(&chip->pci_dev->dev,
  329. "Another process owns this TPM\n");
  330. rc = -EBUSY;
  331. goto err_out;
  332. }
  333. chip->num_opens++;
  334. pci_dev_get(chip->pci_dev);
  335. spin_unlock(&driver_lock);
  336. chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  337. if (chip->data_buffer == NULL) {
  338. chip->num_opens--;
  339. pci_dev_put(chip->pci_dev);
  340. return -ENOMEM;
  341. }
  342. atomic_set(&chip->data_pending, 0);
  343. file->private_data = chip;
  344. return 0;
  345. err_out:
  346. spin_unlock(&driver_lock);
  347. return rc;
  348. }
  349. EXPORT_SYMBOL_GPL(tpm_open);
  350. int tpm_release(struct inode *inode, struct file *file)
  351. {
  352. struct tpm_chip *chip = file->private_data;
  353. file->private_data = NULL;
  354. spin_lock(&driver_lock);
  355. chip->num_opens--;
  356. spin_unlock(&driver_lock);
  357. down(&chip->timer_manipulation_mutex);
  358. if (timer_pending(&chip->user_read_timer))
  359. del_singleshot_timer_sync(&chip->user_read_timer);
  360. else if (timer_pending(&chip->device_timer))
  361. del_singleshot_timer_sync(&chip->device_timer);
  362. up(&chip->timer_manipulation_mutex);
  363. kfree(chip->data_buffer);
  364. atomic_set(&chip->data_pending, 0);
  365. pci_dev_put(chip->pci_dev);
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(tpm_release);
  369. ssize_t tpm_write(struct file * file, const char __user * buf,
  370. size_t size, loff_t * off)
  371. {
  372. struct tpm_chip *chip = file->private_data;
  373. int in_size = size, out_size;
  374. /* cannot perform a write until the read has cleared
  375. either via tpm_read or a user_read_timer timeout */
  376. while (atomic_read(&chip->data_pending) != 0)
  377. msleep(TPM_TIMEOUT);
  378. down(&chip->buffer_mutex);
  379. if (in_size > TPM_BUFSIZE)
  380. in_size = TPM_BUFSIZE;
  381. if (copy_from_user
  382. (chip->data_buffer, (void __user *) buf, in_size)) {
  383. up(&chip->buffer_mutex);
  384. return -EFAULT;
  385. }
  386. /* atomic tpm command send and result receive */
  387. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  388. atomic_set(&chip->data_pending, out_size);
  389. up(&chip->buffer_mutex);
  390. /* Set a timeout by which the reader must come claim the result */
  391. down(&chip->timer_manipulation_mutex);
  392. init_timer(&chip->user_read_timer);
  393. chip->user_read_timer.function = user_reader_timeout;
  394. chip->user_read_timer.data = (unsigned long) chip;
  395. chip->user_read_timer.expires = jiffies + (60 * HZ);
  396. add_timer(&chip->user_read_timer);
  397. up(&chip->timer_manipulation_mutex);
  398. return in_size;
  399. }
  400. EXPORT_SYMBOL_GPL(tpm_write);
  401. ssize_t tpm_read(struct file * file, char __user * buf,
  402. size_t size, loff_t * off)
  403. {
  404. struct tpm_chip *chip = file->private_data;
  405. int ret_size = -ENODATA;
  406. if (atomic_read(&chip->data_pending) != 0) { /* Result available */
  407. down(&chip->timer_manipulation_mutex);
  408. del_singleshot_timer_sync(&chip->user_read_timer);
  409. up(&chip->timer_manipulation_mutex);
  410. down(&chip->buffer_mutex);
  411. ret_size = atomic_read(&chip->data_pending);
  412. atomic_set(&chip->data_pending, 0);
  413. if (ret_size == 0) /* timeout just occurred */
  414. ret_size = -ETIME;
  415. else if (ret_size > 0) { /* relay data */
  416. if (size < ret_size)
  417. ret_size = size;
  418. if (copy_to_user((void __user *) buf,
  419. chip->data_buffer, ret_size)) {
  420. ret_size = -EFAULT;
  421. }
  422. }
  423. up(&chip->buffer_mutex);
  424. }
  425. return ret_size;
  426. }
  427. EXPORT_SYMBOL_GPL(tpm_read);
  428. void __devexit tpm_remove(struct pci_dev *pci_dev)
  429. {
  430. struct tpm_chip *chip = pci_get_drvdata(pci_dev);
  431. if (chip == NULL) {
  432. dev_err(&pci_dev->dev, "No device data found\n");
  433. return;
  434. }
  435. spin_lock(&driver_lock);
  436. list_del(&chip->list);
  437. spin_unlock(&driver_lock);
  438. pci_set_drvdata(pci_dev, NULL);
  439. misc_deregister(&chip->vendor->miscdev);
  440. device_remove_file(&pci_dev->dev, &dev_attr_pubek);
  441. device_remove_file(&pci_dev->dev, &dev_attr_pcrs);
  442. device_remove_file(&pci_dev->dev, &dev_attr_caps);
  443. pci_disable_device(pci_dev);
  444. dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &= !(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
  445. kfree(chip);
  446. pci_dev_put(pci_dev);
  447. }
  448. EXPORT_SYMBOL_GPL(tpm_remove);
  449. static u8 savestate[] = {
  450. 0, 193, /* TPM_TAG_RQU_COMMAND */
  451. 0, 0, 0, 10, /* blob length (in bytes) */
  452. 0, 0, 0, 152 /* TPM_ORD_SaveState */
  453. };
  454. /*
  455. * We are about to suspend. Save the TPM state
  456. * so that it can be restored.
  457. */
  458. int tpm_pm_suspend(struct pci_dev *pci_dev, pm_message_t pm_state)
  459. {
  460. struct tpm_chip *chip = pci_get_drvdata(pci_dev);
  461. if (chip == NULL)
  462. return -ENODEV;
  463. tpm_transmit(chip, savestate, sizeof(savestate));
  464. return 0;
  465. }
  466. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  467. /*
  468. * Resume from a power safe. The BIOS already restored
  469. * the TPM state.
  470. */
  471. int tpm_pm_resume(struct pci_dev *pci_dev)
  472. {
  473. struct tpm_chip *chip = pci_get_drvdata(pci_dev);
  474. if (chip == NULL)
  475. return -ENODEV;
  476. spin_lock(&driver_lock);
  477. tpm_lpc_bus_init(pci_dev, chip->vendor->base);
  478. spin_unlock(&driver_lock);
  479. return 0;
  480. }
  481. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  482. /*
  483. * Called from tpm_<specific>.c probe function only for devices
  484. * the driver has determined it should claim. Prior to calling
  485. * this function the specific probe function has called pci_enable_device
  486. * upon errant exit from this function specific probe function should call
  487. * pci_disable_device
  488. */
  489. int tpm_register_hardware(struct pci_dev *pci_dev,
  490. struct tpm_vendor_specific *entry)
  491. {
  492. char devname[7];
  493. struct tpm_chip *chip;
  494. int i, j;
  495. /* Driver specific per-device data */
  496. chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  497. if (chip == NULL)
  498. return -ENOMEM;
  499. memset(chip, 0, sizeof(struct tpm_chip));
  500. init_MUTEX(&chip->buffer_mutex);
  501. init_MUTEX(&chip->tpm_mutex);
  502. init_MUTEX(&chip->timer_manipulation_mutex);
  503. INIT_LIST_HEAD(&chip->list);
  504. chip->vendor = entry;
  505. chip->dev_num = -1;
  506. for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
  507. for (j = 0; j < 8 * sizeof(int); j++)
  508. if ((dev_mask[i] & (1 << j)) == 0) {
  509. chip->dev_num =
  510. i * TPM_NUM_MASK_ENTRIES + j;
  511. dev_mask[i] |= 1 << j;
  512. goto dev_num_search_complete;
  513. }
  514. dev_num_search_complete:
  515. if (chip->dev_num < 0) {
  516. dev_err(&pci_dev->dev,
  517. "No available tpm device numbers\n");
  518. kfree(chip);
  519. return -ENODEV;
  520. } else if (chip->dev_num == 0)
  521. chip->vendor->miscdev.minor = TPM_MINOR;
  522. else
  523. chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
  524. snprintf(devname, sizeof(devname), "%s%d", "tpm", chip->dev_num);
  525. chip->vendor->miscdev.name = devname;
  526. chip->vendor->miscdev.dev = &(pci_dev->dev);
  527. chip->pci_dev = pci_dev_get(pci_dev);
  528. if (misc_register(&chip->vendor->miscdev)) {
  529. dev_err(&chip->pci_dev->dev,
  530. "unable to misc_register %s, minor %d\n",
  531. chip->vendor->miscdev.name,
  532. chip->vendor->miscdev.minor);
  533. pci_dev_put(pci_dev);
  534. kfree(chip);
  535. dev_mask[i] &= !(1 << j);
  536. return -ENODEV;
  537. }
  538. pci_set_drvdata(pci_dev, chip);
  539. list_add(&chip->list, &tpm_chip_list);
  540. device_create_file(&pci_dev->dev, &dev_attr_pubek);
  541. device_create_file(&pci_dev->dev, &dev_attr_pcrs);
  542. device_create_file(&pci_dev->dev, &dev_attr_caps);
  543. return 0;
  544. }
  545. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  546. static int __init init_tpm(void)
  547. {
  548. return 0;
  549. }
  550. static void __exit cleanup_tpm(void)
  551. {
  552. }
  553. module_init(init_tpm);
  554. module_exit(cleanup_tpm);
  555. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  556. MODULE_DESCRIPTION("TPM Driver");
  557. MODULE_VERSION("2.0");
  558. MODULE_LICENSE("GPL");