]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/char/tpm/tpm-interface.c
tpm: print tpm2_commit_space() error inside tpm2_commit_space()
[linux.git] / drivers / char / tpm / tpm-interface.c
1 /*
2  * Copyright (C) 2004 IBM Corporation
3  * Copyright (C) 2014 Intel Corporation
4  *
5  * Authors:
6  * Leendert van Doorn <leendert@watson.ibm.com>
7  * Dave Safford <safford@watson.ibm.com>
8  * Reiner Sailer <sailer@watson.ibm.com>
9  * Kylene Hall <kjhall@us.ibm.com>
10  *
11  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12  *
13  * Device driver for TCG/TCPA TPM (trusted platform module).
14  * Specifications at www.trustedcomputinggroup.org
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License as
18  * published by the Free Software Foundation, version 2 of the
19  * License.
20  *
21  * Note, the TPM chip is not interrupt driven (only polling)
22  * and can have very long timeouts (minutes!). Hence the unusual
23  * calls to msleep.
24  *
25  */
26
27 #include <linux/poll.h>
28 #include <linux/slab.h>
29 #include <linux/mutex.h>
30 #include <linux/spinlock.h>
31 #include <linux/freezer.h>
32 #include <linux/tpm_eventlog.h>
33
34 #include "tpm.h"
35
36 /*
37  * Bug workaround - some TPM's don't flush the most
38  * recently changed pcr on suspend, so force the flush
39  * with an extend to the selected _unused_ non-volatile pcr.
40  */
41 static u32 tpm_suspend_pcr;
42 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
43 MODULE_PARM_DESC(suspend_pcr,
44                  "PCR to use for dummy writes to facilitate flush on suspend.");
45
46 /**
47  * tpm_calc_ordinal_duration() - calculate the maximum command duration
48  * @chip:    TPM chip to use.
49  * @ordinal: TPM command ordinal.
50  *
51  * The function returns the maximum amount of time the chip could take
52  * to return the result for a particular ordinal in jiffies.
53  *
54  * Return: A maximal duration time for an ordinal in jiffies.
55  */
56 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
57 {
58         if (chip->flags & TPM_CHIP_FLAG_TPM2)
59                 return tpm2_calc_ordinal_duration(chip, ordinal);
60         else
61                 return tpm1_calc_ordinal_duration(chip, ordinal);
62 }
63 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
64
65 static int tpm_validate_command(struct tpm_chip *chip,
66                                  struct tpm_space *space,
67                                  const u8 *cmd,
68                                  size_t len)
69 {
70         const struct tpm_input_header *header = (const void *)cmd;
71         int i;
72         u32 cc;
73         u32 attrs;
74         unsigned int nr_handles;
75
76         if (len < TPM_HEADER_SIZE)
77                 return -EINVAL;
78
79         if (!space)
80                 return 0;
81
82         if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
83                 cc = be32_to_cpu(header->ordinal);
84
85                 i = tpm2_find_cc(chip, cc);
86                 if (i < 0) {
87                         dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
88                                 cc);
89                         return -EOPNOTSUPP;
90                 }
91
92                 attrs = chip->cc_attrs_tbl[i];
93                 nr_handles =
94                         4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
95                 if (len < TPM_HEADER_SIZE + 4 * nr_handles)
96                         goto err_len;
97         }
98
99         return 0;
100 err_len:
101         dev_dbg(&chip->dev,
102                 "%s: insufficient command length %zu", __func__, len);
103         return -EINVAL;
104 }
105
106 static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags)
107 {
108         int rc;
109
110         if (flags & TPM_TRANSMIT_NESTED)
111                 return 0;
112
113         if (!chip->ops->request_locality)
114                 return 0;
115
116         rc = chip->ops->request_locality(chip, 0);
117         if (rc < 0)
118                 return rc;
119
120         chip->locality = rc;
121
122         return 0;
123 }
124
125 static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags)
126 {
127         int rc;
128
129         if (flags & TPM_TRANSMIT_NESTED)
130                 return;
131
132         if (!chip->ops->relinquish_locality)
133                 return;
134
135         rc = chip->ops->relinquish_locality(chip, chip->locality);
136         if (rc)
137                 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
138
139         chip->locality = -1;
140 }
141
142 static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags)
143 {
144         if (flags & TPM_TRANSMIT_NESTED)
145                 return 0;
146
147         if (!chip->ops->cmd_ready)
148                 return 0;
149
150         return chip->ops->cmd_ready(chip);
151 }
152
153 static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags)
154 {
155         if (flags & TPM_TRANSMIT_NESTED)
156                 return 0;
157
158         if (!chip->ops->go_idle)
159                 return 0;
160
161         return chip->ops->go_idle(chip);
162 }
163
164 static ssize_t tpm_try_transmit(struct tpm_chip *chip,
165                                 struct tpm_space *space,
166                                 u8 *buf, size_t bufsiz,
167                                 unsigned int flags)
168 {
169         struct tpm_output_header *header = (void *)buf;
170         int rc;
171         ssize_t len = 0;
172         u32 count, ordinal;
173         unsigned long stop;
174         bool need_locality;
175
176         rc = tpm_validate_command(chip, space, buf, bufsiz);
177         if (rc == -EINVAL)
178                 return rc;
179         /*
180          * If the command is not implemented by the TPM, synthesize a
181          * response with a TPM2_RC_COMMAND_CODE return for user-space.
182          */
183         if (rc == -EOPNOTSUPP) {
184                 header->length = cpu_to_be32(sizeof(*header));
185                 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
186                 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
187                                                   TSS2_RESMGR_TPM_RC_LAYER);
188                 return sizeof(*header);
189         }
190
191         if (bufsiz > TPM_BUFSIZE)
192                 bufsiz = TPM_BUFSIZE;
193
194         count = be32_to_cpu(*((__be32 *) (buf + 2)));
195         ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
196         if (count == 0)
197                 return -ENODATA;
198         if (count > bufsiz) {
199                 dev_err(&chip->dev,
200                         "invalid count value %x %zx\n", count, bufsiz);
201                 return -E2BIG;
202         }
203
204         if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
205                 mutex_lock(&chip->tpm_mutex);
206
207         if (chip->ops->clk_enable != NULL)
208                 chip->ops->clk_enable(chip, true);
209
210         /* Store the decision as chip->locality will be changed. */
211         need_locality = chip->locality == -1;
212
213         if (need_locality) {
214                 rc = tpm_request_locality(chip, flags);
215                 if (rc < 0) {
216                         need_locality = false;
217                         goto out_locality;
218                 }
219         }
220
221         rc = tpm_cmd_ready(chip, flags);
222         if (rc)
223                 goto out_locality;
224
225         rc = tpm2_prepare_space(chip, space, ordinal, buf);
226         if (rc)
227                 goto out;
228
229         rc = chip->ops->send(chip, buf, count);
230         if (rc < 0) {
231                 if (rc != -EPIPE)
232                         dev_err(&chip->dev,
233                                 "%s: send(): error %d\n", __func__, rc);
234                 goto out;
235         }
236
237         /* A sanity check. send() should just return zero on success e.g.
238          * not the command length.
239          */
240         if (rc > 0) {
241                 dev_warn(&chip->dev,
242                          "%s: send(): invalid value %d\n", __func__, rc);
243                 rc = 0;
244         }
245
246         if (chip->flags & TPM_CHIP_FLAG_IRQ)
247                 goto out_recv;
248
249         stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
250         do {
251                 u8 status = chip->ops->status(chip);
252                 if ((status & chip->ops->req_complete_mask) ==
253                     chip->ops->req_complete_val)
254                         goto out_recv;
255
256                 if (chip->ops->req_canceled(chip, status)) {
257                         dev_err(&chip->dev, "Operation Canceled\n");
258                         rc = -ECANCELED;
259                         goto out;
260                 }
261
262                 tpm_msleep(TPM_TIMEOUT_POLL);
263                 rmb();
264         } while (time_before(jiffies, stop));
265
266         chip->ops->cancel(chip);
267         dev_err(&chip->dev, "Operation Timed out\n");
268         rc = -ETIME;
269         goto out;
270
271 out_recv:
272         len = chip->ops->recv(chip, buf, bufsiz);
273         if (len < 0) {
274                 rc = len;
275                 dev_err(&chip->dev,
276                         "tpm_transmit: tpm_recv: error %d\n", rc);
277                 goto out;
278         } else if (len < TPM_HEADER_SIZE) {
279                 rc = -EFAULT;
280                 goto out;
281         }
282
283         if (len != be32_to_cpu(header->length)) {
284                 rc = -EFAULT;
285                 goto out;
286         }
287
288         rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
289
290 out:
291         /* may fail but do not override previous error value in rc */
292         tpm_go_idle(chip, flags);
293
294 out_locality:
295         if (need_locality)
296                 tpm_relinquish_locality(chip, flags);
297
298         if (chip->ops->clk_enable != NULL)
299                 chip->ops->clk_enable(chip, false);
300
301         if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
302                 mutex_unlock(&chip->tpm_mutex);
303         return rc ? rc : len;
304 }
305
306 /**
307  * tpm_transmit - Internal kernel interface to transmit TPM commands.
308  * @chip:       a TPM chip to use
309  * @space:      a TPM space
310  * @buf:        a TPM command buffer
311  * @bufsiz:     length of the TPM command buffer
312  * @flags:      TPM transmit flags
313  *
314  * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
315  * the TPM and retransmits the command after a delay up to a maximum wait of
316  * TPM2_DURATION_LONG.
317  *
318  * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
319  * only.
320  *
321  * Return:
322  * * The response length        - OK
323  * * -errno                     - A system error
324  */
325 ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
326                      u8 *buf, size_t bufsiz, unsigned int flags)
327 {
328         struct tpm_output_header *header = (struct tpm_output_header *)buf;
329         /* space for header and handles */
330         u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
331         unsigned int delay_msec = TPM2_DURATION_SHORT;
332         u32 rc = 0;
333         ssize_t ret;
334         const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
335                                      bufsiz);
336         /* the command code is where the return code will be */
337         u32 cc = be32_to_cpu(header->return_code);
338
339         /*
340          * Subtlety here: if we have a space, the handles will be
341          * transformed, so when we restore the header we also have to
342          * restore the handles.
343          */
344         memcpy(save, buf, save_size);
345
346         for (;;) {
347                 ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
348                 if (ret < 0)
349                         break;
350                 rc = be32_to_cpu(header->return_code);
351                 if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
352                         break;
353                 /*
354                  * return immediately if self test returns test
355                  * still running to shorten boot time.
356                  */
357                 if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
358                         break;
359
360                 if (delay_msec > TPM2_DURATION_LONG) {
361                         if (rc == TPM2_RC_RETRY)
362                                 dev_err(&chip->dev, "in retry loop\n");
363                         else
364                                 dev_err(&chip->dev,
365                                         "self test is still running\n");
366                         break;
367                 }
368                 tpm_msleep(delay_msec);
369                 delay_msec *= 2;
370                 memcpy(buf, save, save_size);
371         }
372         return ret;
373 }
374
375 /**
376  * tpm_transmit_cmd - send a tpm command to the device
377  * @chip:                       a TPM chip to use
378  * @space:                      a TPM space
379  * @buf:                        a TPM command buffer
380  * @min_rsp_body_length:        minimum expected length of response body
381  * @flags:                      TPM transmit flags
382  * @desc:                       command description used in the error message
383  *
384  * Return:
385  * * 0          - OK
386  * * -errno     - A system error
387  * * TPM_RC     - A TPM error
388  */
389 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
390                          struct tpm_buf *buf, size_t min_rsp_body_length,
391                          unsigned int flags, const char *desc)
392 {
393         const struct tpm_output_header *header =
394                 (struct tpm_output_header *)buf->data;
395         int err;
396         ssize_t len;
397
398         len = tpm_transmit(chip, space, buf->data, PAGE_SIZE, flags);
399         if (len <  0)
400                 return len;
401
402         err = be32_to_cpu(header->return_code);
403         if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
404             && err != TPM2_RC_TESTING && desc)
405                 dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
406                         desc);
407         if (err)
408                 return err;
409
410         if (len < min_rsp_body_length + TPM_HEADER_SIZE)
411                 return -EFAULT;
412
413         return 0;
414 }
415 EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
416
417 int tpm_get_timeouts(struct tpm_chip *chip)
418 {
419         if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
420                 return 0;
421
422         if (chip->flags & TPM_CHIP_FLAG_TPM2)
423                 return tpm2_get_timeouts(chip);
424         else
425                 return tpm1_get_timeouts(chip);
426 }
427 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
428
429 /**
430  * tpm_is_tpm2 - do we a have a TPM2 chip?
431  * @chip:       a &struct tpm_chip instance, %NULL for the default chip
432  *
433  * Return:
434  * 1 if we have a TPM2 chip.
435  * 0 if we don't have a TPM2 chip.
436  * A negative number for system errors (errno).
437  */
438 int tpm_is_tpm2(struct tpm_chip *chip)
439 {
440         int rc;
441
442         chip = tpm_find_get_ops(chip);
443         if (!chip)
444                 return -ENODEV;
445
446         rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
447
448         tpm_put_ops(chip);
449
450         return rc;
451 }
452 EXPORT_SYMBOL_GPL(tpm_is_tpm2);
453
454 /**
455  * tpm_pcr_read - read a PCR value from SHA1 bank
456  * @chip:       a &struct tpm_chip instance, %NULL for the default chip
457  * @pcr_idx:    the PCR to be retrieved
458  * @res_buf:    the value of the PCR
459  *
460  * Return: same as with tpm_transmit_cmd()
461  */
462 int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
463 {
464         int rc;
465
466         chip = tpm_find_get_ops(chip);
467         if (!chip)
468                 return -ENODEV;
469
470         if (chip->flags & TPM_CHIP_FLAG_TPM2)
471                 rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
472         else
473                 rc = tpm1_pcr_read(chip, pcr_idx, res_buf);
474
475         tpm_put_ops(chip);
476         return rc;
477 }
478 EXPORT_SYMBOL_GPL(tpm_pcr_read);
479
480 /**
481  * tpm_pcr_extend - extend a PCR value in SHA1 bank.
482  * @chip:       a &struct tpm_chip instance, %NULL for the default chip
483  * @pcr_idx:    the PCR to be retrieved
484  * @hash:       the hash value used to extend the PCR value
485  *
486  * Note: with TPM 2.0 extends also those banks with a known digest size to the
487  * cryto subsystem in order to prevent malicious use of those PCR banks. In the
488  * future we should dynamically determine digest sizes.
489  *
490  * Return: same as with tpm_transmit_cmd()
491  */
492 int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
493 {
494         int rc;
495         struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
496         u32 count = 0;
497         int i;
498
499         chip = tpm_find_get_ops(chip);
500         if (!chip)
501                 return -ENODEV;
502
503         if (chip->flags & TPM_CHIP_FLAG_TPM2) {
504                 memset(digest_list, 0, sizeof(digest_list));
505
506                 for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
507                             chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
508                         digest_list[i].alg_id = chip->active_banks[i];
509                         memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
510                         count++;
511                 }
512
513                 rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
514                 tpm_put_ops(chip);
515                 return rc;
516         }
517
518         rc = tpm1_pcr_extend(chip, pcr_idx, hash,
519                              "attempting extend a PCR value");
520         tpm_put_ops(chip);
521         return rc;
522 }
523 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
524
525 /**
526  * tpm_send - send a TPM command
527  * @chip:       a &struct tpm_chip instance, %NULL for the default chip
528  * @cmd:        a TPM command buffer
529  * @buflen:     the length of the TPM command buffer
530  *
531  * Return: same as with tpm_transmit_cmd()
532  */
533 int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
534 {
535         struct tpm_buf buf;
536         int rc;
537
538         chip = tpm_find_get_ops(chip);
539         if (!chip)
540                 return -ENODEV;
541
542         rc = tpm_buf_init(&buf, 0, 0);
543         if (rc)
544                 goto out;
545
546         memcpy(buf.data, cmd, buflen);
547         rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
548                               "attempting to a send a command");
549         tpm_buf_destroy(&buf);
550 out:
551         tpm_put_ops(chip);
552         return rc;
553 }
554 EXPORT_SYMBOL_GPL(tpm_send);
555
556 int tpm_auto_startup(struct tpm_chip *chip)
557 {
558         int rc;
559
560         if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
561                 return 0;
562
563         if (chip->flags & TPM_CHIP_FLAG_TPM2)
564                 rc = tpm2_auto_startup(chip);
565         else
566                 rc = tpm1_auto_startup(chip);
567
568         return rc;
569 }
570
571 /*
572  * We are about to suspend. Save the TPM state
573  * so that it can be restored.
574  */
575 int tpm_pm_suspend(struct device *dev)
576 {
577         struct tpm_chip *chip = dev_get_drvdata(dev);
578         int rc = 0;
579
580         if (!chip)
581                 return -ENODEV;
582
583         if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
584                 return 0;
585
586         if (chip->flags & TPM_CHIP_FLAG_TPM2)
587                 tpm2_shutdown(chip, TPM2_SU_STATE);
588         else
589                 rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
590
591         return rc;
592 }
593 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
594
595 /*
596  * Resume from a power safe. The BIOS already restored
597  * the TPM state.
598  */
599 int tpm_pm_resume(struct device *dev)
600 {
601         struct tpm_chip *chip = dev_get_drvdata(dev);
602
603         if (chip == NULL)
604                 return -ENODEV;
605
606         return 0;
607 }
608 EXPORT_SYMBOL_GPL(tpm_pm_resume);
609
610 /**
611  * tpm_get_random() - get random bytes from the TPM's RNG
612  * @chip:       a &struct tpm_chip instance, %NULL for the default chip
613  * @out:        destination buffer for the random bytes
614  * @max:        the max number of bytes to write to @out
615  *
616  * Return: number of random bytes read or a negative error value.
617  */
618 int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
619 {
620         int rc;
621
622         if (!out || max > TPM_MAX_RNG_DATA)
623                 return -EINVAL;
624
625         chip = tpm_find_get_ops(chip);
626         if (!chip)
627                 return -ENODEV;
628
629         if (chip->flags & TPM_CHIP_FLAG_TPM2)
630                 rc = tpm2_get_random(chip, out, max);
631         else
632                 rc = tpm1_get_random(chip, out, max);
633
634         tpm_put_ops(chip);
635         return rc;
636 }
637 EXPORT_SYMBOL_GPL(tpm_get_random);
638
639 /**
640  * tpm_seal_trusted() - seal a trusted key payload
641  * @chip:       a &struct tpm_chip instance, %NULL for the default chip
642  * @options:    authentication values and other options
643  * @payload:    the key data in clear and encrypted form
644  *
645  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
646  * the keyring subsystem.
647  *
648  * Return: same as with tpm_transmit_cmd()
649  */
650 int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
651                      struct trusted_key_options *options)
652 {
653         int rc;
654
655         chip = tpm_find_get_ops(chip);
656         if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
657                 return -ENODEV;
658
659         rc = tpm2_seal_trusted(chip, payload, options);
660
661         tpm_put_ops(chip);
662         return rc;
663 }
664 EXPORT_SYMBOL_GPL(tpm_seal_trusted);
665
666 /**
667  * tpm_unseal_trusted() - unseal a trusted key
668  * @chip:       a &struct tpm_chip instance, %NULL for the default chip
669  * @options:    authentication values and other options
670  * @payload:    the key data in clear and encrypted form
671  *
672  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
673  * the keyring subsystem.
674  *
675  * Return: same as with tpm_transmit_cmd()
676  */
677 int tpm_unseal_trusted(struct tpm_chip *chip,
678                        struct trusted_key_payload *payload,
679                        struct trusted_key_options *options)
680 {
681         int rc;
682
683         chip = tpm_find_get_ops(chip);
684         if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
685                 return -ENODEV;
686
687         rc = tpm2_unseal_trusted(chip, payload, options);
688
689         tpm_put_ops(chip);
690
691         return rc;
692 }
693 EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
694
695 static int __init tpm_init(void)
696 {
697         int rc;
698
699         tpm_class = class_create(THIS_MODULE, "tpm");
700         if (IS_ERR(tpm_class)) {
701                 pr_err("couldn't create tpm class\n");
702                 return PTR_ERR(tpm_class);
703         }
704
705         tpmrm_class = class_create(THIS_MODULE, "tpmrm");
706         if (IS_ERR(tpmrm_class)) {
707                 pr_err("couldn't create tpmrm class\n");
708                 rc = PTR_ERR(tpmrm_class);
709                 goto out_destroy_tpm_class;
710         }
711
712         rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
713         if (rc < 0) {
714                 pr_err("tpm: failed to allocate char dev region\n");
715                 goto out_destroy_tpmrm_class;
716         }
717
718         rc = tpm_dev_common_init();
719         if (rc) {
720                 pr_err("tpm: failed to allocate char dev region\n");
721                 goto out_unreg_chrdev;
722         }
723
724         return 0;
725
726 out_unreg_chrdev:
727         unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
728 out_destroy_tpmrm_class:
729         class_destroy(tpmrm_class);
730 out_destroy_tpm_class:
731         class_destroy(tpm_class);
732
733         return rc;
734 }
735
736 static void __exit tpm_exit(void)
737 {
738         idr_destroy(&dev_nums_idr);
739         class_destroy(tpm_class);
740         class_destroy(tpmrm_class);
741         unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
742         tpm_dev_common_exit();
743 }
744
745 subsys_initcall(tpm_init);
746 module_exit(tpm_exit);
747
748 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
749 MODULE_DESCRIPTION("TPM Driver");
750 MODULE_VERSION("2.0");
751 MODULE_LICENSE("GPL");