[PATCH] tpm: add cancel function
[linux-2.6] / drivers / char / tpm / tpm_atmel.c
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  */
21
22 #include "tpm.h"
23
24 /* Atmel definitions */
25 enum tpm_atmel_addr{
26         TPM_ATML_BASE = 0x400
27 };
28
29 /* write status bits */
30 enum tpm_atmel_write_status {
31         ATML_STATUS_ABORT = 0x01,
32         ATML_STATUS_LASTBYTE = 0x04
33 };
34 /* read status bits */
35 enum tpm_atmel_read_status {
36         ATML_STATUS_BUSY = 0x01,
37         ATML_STATUS_DATA_AVAIL = 0x02,
38         ATML_STATUS_REWRITE = 0x04,
39         ATML_STATUS_READY = 0x08
40 };
41
42 static int tpm_atml_recv(struct tpm_chip *chip, u8 * buf, size_t count)
43 {
44         u8 status, *hdr = buf;
45         u32 size;
46         int i;
47         __be32 *native_size;
48
49         /* start reading header */
50         if (count < 6)
51                 return -EIO;
52
53         for (i = 0; i < 6; i++) {
54                 status = inb(chip->vendor->base + 1);
55                 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
56                         dev_err(&chip->pci_dev->dev,
57                                 "error reading header\n");
58                         return -EIO;
59                 }
60                 *buf++ = inb(chip->vendor->base);
61         }
62
63         /* size of the data received */
64         native_size = (__force __be32 *) (hdr + 2);
65         size = be32_to_cpu(*native_size);
66
67         if (count < size) {
68                 dev_err(&chip->pci_dev->dev,
69                         "Recv size(%d) less than available space\n", size);
70                 for (; i < size; i++) { /* clear the waiting data anyway */
71                         status = inb(chip->vendor->base + 1);
72                         if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
73                                 dev_err(&chip->pci_dev->dev,
74                                         "error reading data\n");
75                                 return -EIO;
76                         }
77                 }
78                 return -EIO;
79         }
80
81         /* read all the data available */
82         for (; i < size; i++) {
83                 status = inb(chip->vendor->base + 1);
84                 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
85                         dev_err(&chip->pci_dev->dev,
86                                 "error reading data\n");
87                         return -EIO;
88                 }
89                 *buf++ = inb(chip->vendor->base);
90         }
91
92         /* make sure data available is gone */
93         status = inb(chip->vendor->base + 1);
94         if (status & ATML_STATUS_DATA_AVAIL) {
95                 dev_err(&chip->pci_dev->dev, "data available is stuck\n");
96                 return -EIO;
97         }
98
99         return size;
100 }
101
102 static int tpm_atml_send(struct tpm_chip *chip, u8 * buf, size_t count)
103 {
104         int i;
105
106         dev_dbg(&chip->pci_dev->dev, "tpm_atml_send: ");
107         for (i = 0; i < count; i++) {
108                 dev_dbg(&chip->pci_dev->dev, "0x%x(%d) ", buf[i], buf[i]);
109                 outb(buf[i], chip->vendor->base);
110         }
111
112         return count;
113 }
114
115 static void tpm_atml_cancel(struct tpm_chip *chip)
116 {
117         outb(ATML_STATUS_ABORT, chip->vendor->base + 1);
118 }
119
120 static struct file_operations atmel_ops = {
121         .owner = THIS_MODULE,
122         .llseek = no_llseek,
123         .open = tpm_open,
124         .read = tpm_read,
125         .write = tpm_write,
126         .release = tpm_release,
127 };
128
129 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
130 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
131 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
132 static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
133
134 static struct attribute* atmel_attrs[] = {
135         &dev_attr_pubek.attr,
136         &dev_attr_pcrs.attr,
137         &dev_attr_caps.attr,
138         &dev_attr_cancel.attr,
139         0,
140 };
141
142 static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs };
143
144 static struct tpm_vendor_specific tpm_atmel = {
145         .recv = tpm_atml_recv,
146         .send = tpm_atml_send,
147         .cancel = tpm_atml_cancel,
148         .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
149         .req_complete_val = ATML_STATUS_DATA_AVAIL,
150         .req_canceled = ATML_STATUS_READY,
151         .base = TPM_ATML_BASE,
152         .attr_group = &atmel_attr_grp,
153         .miscdev = { .fops = &atmel_ops, },
154 };
155
156 static int __devinit tpm_atml_init(struct pci_dev *pci_dev,
157                                    const struct pci_device_id *pci_id)
158 {
159         u8 version[4];
160         int rc = 0;
161
162         if (pci_enable_device(pci_dev))
163                 return -EIO;
164
165         if (tpm_lpc_bus_init(pci_dev, TPM_ATML_BASE)) {
166                 rc = -ENODEV;
167                 goto out_err;
168         }
169
170         /* verify that it is an Atmel part */
171         if (tpm_read_index(4) != 'A' || tpm_read_index(5) != 'T'
172             || tpm_read_index(6) != 'M' || tpm_read_index(7) != 'L') {
173                 rc = -ENODEV;
174                 goto out_err;
175         }
176
177         /* query chip for its version number */
178         if ((version[0] = tpm_read_index(0x00)) != 0xFF) {
179                 version[1] = tpm_read_index(0x01);
180                 version[2] = tpm_read_index(0x02);
181                 version[3] = tpm_read_index(0x03);
182         } else {
183                 dev_info(&pci_dev->dev, "version query failed\n");
184                 rc = -ENODEV;
185                 goto out_err;
186         }
187
188         if ((rc = tpm_register_hardware(pci_dev, &tpm_atmel)) < 0)
189                 goto out_err;
190
191         dev_info(&pci_dev->dev,
192                  "Atmel TPM version %d.%d.%d.%d\n", version[0], version[1],
193                  version[2], version[3]);
194
195         return 0;
196 out_err:
197         pci_disable_device(pci_dev);
198         return rc;
199 }
200
201 static struct pci_device_id tpm_pci_tbl[] __devinitdata = {
202         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0)},
203         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12)},
204         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0)},
205         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12)},
206         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0)},
207         {PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_LPC)},
208         {0,}
209 };
210
211 MODULE_DEVICE_TABLE(pci, tpm_pci_tbl);
212
213 static struct pci_driver atmel_pci_driver = {
214         .name = "tpm_atmel",
215         .id_table = tpm_pci_tbl,
216         .probe = tpm_atml_init,
217         .remove = __devexit_p(tpm_remove),
218         .suspend = tpm_pm_suspend,
219         .resume = tpm_pm_resume,
220 };
221
222 static int __init init_atmel(void)
223 {
224         return pci_register_driver(&atmel_pci_driver);
225 }
226
227 static void __exit cleanup_atmel(void)
228 {
229         pci_unregister_driver(&atmel_pci_driver);
230 }
231
232 module_init(init_atmel);
233 module_exit(cleanup_atmel);
234
235 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
236 MODULE_DESCRIPTION("TPM Driver");
237 MODULE_VERSION("2.0");
238 MODULE_LICENSE("GPL");