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