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