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