2 * Copyright (C) 2004 IBM Corporation
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>
9 * Copyright (C) 2013 Obsidian Research Corp
10 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
12 * Device file system interface to the TPM
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
20 #include <linux/miscdevice.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
25 static void user_reader_timeout(unsigned long ptr)
27 struct tpm_chip *chip = (struct tpm_chip *) ptr;
29 schedule_work(&chip->work);
32 static void timeout_work(struct work_struct *work)
34 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
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);
42 static int tpm_open(struct inode *inode, struct file *file)
44 struct miscdevice *misc = file->private_data;
45 struct tpm_chip *chip = container_of(misc, struct tpm_chip,
48 /* It's assured that the chip will be opened just once,
49 * by the check of is_open variable, which is protected
51 if (test_and_set_bit(0, &chip->is_open)) {
52 dev_dbg(chip->dev, "Another process owns this TPM\n");
56 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
57 if (chip->data_buffer == NULL) {
58 clear_bit(0, &chip->is_open);
62 atomic_set(&chip->data_pending, 0);
64 file->private_data = chip;
65 get_device(chip->dev);
69 static ssize_t tpm_read(struct file *file, char __user *buf,
70 size_t size, loff_t *off)
72 struct tpm_chip *chip = file->private_data;
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;
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);
90 mutex_unlock(&chip->buffer_mutex);
93 atomic_set(&chip->data_pending, 0);
98 static ssize_t tpm_write(struct file *file, const char __user *buf,
99 size_t size, loff_t *off)
101 struct tpm_chip *chip = file->private_data;
102 size_t in_size = size;
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.
109 if (atomic_read(&chip->data_pending) != 0)
112 if (in_size > TPM_BUFSIZE)
115 mutex_lock(&chip->buffer_mutex);
118 (chip->data_buffer, (void __user *) buf, in_size)) {
119 mutex_unlock(&chip->buffer_mutex);
123 /* atomic tpm command send and result receive */
124 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
126 mutex_unlock(&chip->buffer_mutex);
130 atomic_set(&chip->data_pending, out_size);
131 mutex_unlock(&chip->buffer_mutex);
133 /* Set a timeout by which the reader must come claim the result */
134 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
140 * Called on file close
142 static int tpm_release(struct inode *inode, struct file *file)
144 struct tpm_chip *chip = file->private_data;
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);
156 static const struct file_operations tpm_fops = {
157 .owner = THIS_MODULE,
162 .release = tpm_release,
165 int tpm_dev_add_device(struct tpm_chip *chip)
169 mutex_init(&chip->buffer_mutex);
170 INIT_WORK(&chip->work, timeout_work);
172 setup_timer(&chip->user_read_timer, user_reader_timeout,
173 (unsigned long)chip);
175 chip->vendor.miscdev.fops = &tpm_fops;
176 if (chip->dev_num == 0)
177 chip->vendor.miscdev.minor = TPM_MINOR;
179 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
181 chip->vendor.miscdev.name = chip->devname;
182 chip->vendor.miscdev.parent = chip->dev;
184 rc = misc_register(&chip->vendor.miscdev);
186 chip->vendor.miscdev.name = NULL;
188 "unable to misc_register %s, minor %d err=%d\n",
189 chip->vendor.miscdev.name,
190 chip->vendor.miscdev.minor, rc);
195 void tpm_dev_del_device(struct tpm_chip *chip)
197 if (chip->vendor.miscdev.name)
198 misc_deregister(&chip->vendor.miscdev);