tpm: Pull everything related to /dev/tpmX into tpm-dev.c
[cascardo/linux.git] / drivers / char / tpm / tpm-dev.c
1 /*
2  * Copyright (C) 2004 IBM Corporation
3  * Authors:
4  * Leendert van Doorn <leendert@watson.ibm.com>
5  * Dave Safford <safford@watson.ibm.com>
6  * Reiner Sailer <sailer@watson.ibm.com>
7  * Kylene Hall <kjhall@us.ibm.com>
8  *
9  * Copyright (C) 2013 Obsidian Research Corp
10  * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
11  *
12  * Device file system interface to the TPM
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation, version 2 of the
17  * License.
18  *
19  */
20 #include <linux/miscdevice.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include "tpm.h"
24
25 static void user_reader_timeout(unsigned long ptr)
26 {
27         struct tpm_chip *chip = (struct tpm_chip *) ptr;
28
29         schedule_work(&chip->work);
30 }
31
32 static void timeout_work(struct work_struct *work)
33 {
34         struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
35
36         mutex_lock(&chip->buffer_mutex);
37         atomic_set(&chip->data_pending, 0);
38         memset(chip->data_buffer, 0, TPM_BUFSIZE);
39         mutex_unlock(&chip->buffer_mutex);
40 }
41
42 static int tpm_open(struct inode *inode, struct file *file)
43 {
44         struct miscdevice *misc = file->private_data;
45         struct tpm_chip *chip = container_of(misc, struct tpm_chip,
46                                              vendor.miscdev);
47
48         /* It's assured that the chip will be opened just once,
49          * by the check of is_open variable, which is protected
50          * by driver_lock. */
51         if (test_and_set_bit(0, &chip->is_open)) {
52                 dev_dbg(chip->dev, "Another process owns this TPM\n");
53                 return -EBUSY;
54         }
55
56         chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
57         if (chip->data_buffer == NULL) {
58                 clear_bit(0, &chip->is_open);
59                 return -ENOMEM;
60         }
61
62         atomic_set(&chip->data_pending, 0);
63
64         file->private_data = chip;
65         get_device(chip->dev);
66         return 0;
67 }
68
69 static ssize_t tpm_read(struct file *file, char __user *buf,
70                         size_t size, loff_t *off)
71 {
72         struct tpm_chip *chip = file->private_data;
73         ssize_t ret_size;
74         int rc;
75
76         del_singleshot_timer_sync(&chip->user_read_timer);
77         flush_work(&chip->work);
78         ret_size = atomic_read(&chip->data_pending);
79         if (ret_size > 0) {     /* relay data */
80                 ssize_t orig_ret_size = ret_size;
81                 if (size < ret_size)
82                         ret_size = size;
83
84                 mutex_lock(&chip->buffer_mutex);
85                 rc = copy_to_user(buf, chip->data_buffer, ret_size);
86                 memset(chip->data_buffer, 0, orig_ret_size);
87                 if (rc)
88                         ret_size = -EFAULT;
89
90                 mutex_unlock(&chip->buffer_mutex);
91         }
92
93         atomic_set(&chip->data_pending, 0);
94
95         return ret_size;
96 }
97
98 static ssize_t tpm_write(struct file *file, const char __user *buf,
99                          size_t size, loff_t *off)
100 {
101         struct tpm_chip *chip = file->private_data;
102         size_t in_size = size;
103         ssize_t out_size;
104
105         /* cannot perform a write until the read has cleared
106            either via tpm_read or a user_read_timer timeout.
107            This also prevents splitted buffered writes from blocking here.
108         */
109         if (atomic_read(&chip->data_pending) != 0)
110                 return -EBUSY;
111
112         if (in_size > TPM_BUFSIZE)
113                 return -E2BIG;
114
115         mutex_lock(&chip->buffer_mutex);
116
117         if (copy_from_user
118             (chip->data_buffer, (void __user *) buf, in_size)) {
119                 mutex_unlock(&chip->buffer_mutex);
120                 return -EFAULT;
121         }
122
123         /* atomic tpm command send and result receive */
124         out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
125         if (out_size < 0) {
126                 mutex_unlock(&chip->buffer_mutex);
127                 return out_size;
128         }
129
130         atomic_set(&chip->data_pending, out_size);
131         mutex_unlock(&chip->buffer_mutex);
132
133         /* Set a timeout by which the reader must come claim the result */
134         mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
135
136         return in_size;
137 }
138
139 /*
140  * Called on file close
141  */
142 static int tpm_release(struct inode *inode, struct file *file)
143 {
144         struct tpm_chip *chip = file->private_data;
145
146         del_singleshot_timer_sync(&chip->user_read_timer);
147         flush_work(&chip->work);
148         file->private_data = NULL;
149         atomic_set(&chip->data_pending, 0);
150         kzfree(chip->data_buffer);
151         clear_bit(0, &chip->is_open);
152         put_device(chip->dev);
153         return 0;
154 }
155
156 static const struct file_operations tpm_fops = {
157         .owner = THIS_MODULE,
158         .llseek = no_llseek,
159         .open = tpm_open,
160         .read = tpm_read,
161         .write = tpm_write,
162         .release = tpm_release,
163 };
164
165 int tpm_dev_add_device(struct tpm_chip *chip)
166 {
167         int rc;
168
169         mutex_init(&chip->buffer_mutex);
170         INIT_WORK(&chip->work, timeout_work);
171
172         setup_timer(&chip->user_read_timer, user_reader_timeout,
173                         (unsigned long)chip);
174
175         chip->vendor.miscdev.fops = &tpm_fops;
176         if (chip->dev_num == 0)
177                 chip->vendor.miscdev.minor = TPM_MINOR;
178         else
179                 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
180
181         chip->vendor.miscdev.name = chip->devname;
182         chip->vendor.miscdev.parent = chip->dev;
183
184         rc = misc_register(&chip->vendor.miscdev);
185         if (rc) {
186                 chip->vendor.miscdev.name = NULL;
187                 dev_err(chip->dev,
188                         "unable to misc_register %s, minor %d err=%d\n",
189                         chip->vendor.miscdev.name,
190                         chip->vendor.miscdev.minor, rc);
191         }
192         return rc;
193 }
194
195 void tpm_dev_del_device(struct tpm_chip *chip)
196 {
197         if (chip->vendor.miscdev.name)
198                 misc_deregister(&chip->vendor.miscdev);
199 }