V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / s390 / char / vmcp.c
blob2f419b0ea628bf1dd47ca0385f54f95bbf808235
1 /*
2 * Copyright IBM Corp. 2004,2007
3 * Interface implementation for communication with the z/VM control program
4 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
7 * z/VMs CP offers the possibility to issue commands via the diagnose code 8
8 * this driver implements a character device that issues these commands and
9 * returns the answer of CP.
11 * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS
14 #include <linux/fs.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/miscdevice.h>
18 #include <linux/module.h>
19 #include <asm/cpcmd.h>
20 #include <asm/debug.h>
21 #include <asm/uaccess.h>
22 #include "vmcp.h"
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
26 MODULE_DESCRIPTION("z/VM CP interface");
28 #define PRINTK_HEADER "vmcp: "
30 static debug_info_t *vmcp_debug;
32 static int vmcp_open(struct inode *inode, struct file *file)
34 struct vmcp_session *session;
36 if (!capable(CAP_SYS_ADMIN))
37 return -EPERM;
39 session = kmalloc(sizeof(*session), GFP_KERNEL);
40 if (!session)
41 return -ENOMEM;
42 session->bufsize = PAGE_SIZE;
43 session->response = NULL;
44 session->resp_size = 0;
45 mutex_init(&session->mutex);
46 file->private_data = session;
47 return nonseekable_open(inode, file);
50 static int vmcp_release(struct inode *inode, struct file *file)
52 struct vmcp_session *session;
54 session = (struct vmcp_session *)file->private_data;
55 file->private_data = NULL;
56 free_pages((unsigned long)session->response, get_order(session->bufsize));
57 kfree(session);
58 return 0;
61 static ssize_t
62 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
64 size_t tocopy;
65 struct vmcp_session *session;
67 session = (struct vmcp_session *)file->private_data;
68 if (mutex_lock_interruptible(&session->mutex))
69 return -ERESTARTSYS;
70 if (!session->response) {
71 mutex_unlock(&session->mutex);
72 return 0;
74 if (*ppos > session->resp_size) {
75 mutex_unlock(&session->mutex);
76 return 0;
78 tocopy = min(session->resp_size - (size_t) (*ppos), count);
79 tocopy = min(tocopy, session->bufsize - (size_t) (*ppos));
81 if (copy_to_user(buff, session->response + (*ppos), tocopy)) {
82 mutex_unlock(&session->mutex);
83 return -EFAULT;
85 mutex_unlock(&session->mutex);
86 *ppos += tocopy;
87 return tocopy;
90 static ssize_t
91 vmcp_write(struct file *file, const char __user *buff, size_t count,
92 loff_t *ppos)
94 char *cmd;
95 struct vmcp_session *session;
97 if (count > 240)
98 return -EINVAL;
99 cmd = kmalloc(count + 1, GFP_KERNEL);
100 if (!cmd)
101 return -ENOMEM;
102 if (copy_from_user(cmd, buff, count)) {
103 kfree(cmd);
104 return -EFAULT;
106 cmd[count] = '\0';
107 session = (struct vmcp_session *)file->private_data;
108 if (mutex_lock_interruptible(&session->mutex)) {
109 kfree(cmd);
110 return -ERESTARTSYS;
112 if (!session->response)
113 session->response = (char *)__get_free_pages(GFP_KERNEL
114 | __GFP_REPEAT | GFP_DMA,
115 get_order(session->bufsize));
116 if (!session->response) {
117 mutex_unlock(&session->mutex);
118 kfree(cmd);
119 return -ENOMEM;
121 debug_text_event(vmcp_debug, 1, cmd);
122 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
123 &session->resp_code);
124 mutex_unlock(&session->mutex);
125 kfree(cmd);
126 *ppos = 0; /* reset the file pointer after a command */
127 return count;
132 * These ioctls are available, as the semantics of the diagnose 8 call
133 * does not fit very well into a Linux call. Diagnose X'08' is described in
134 * CP Programming Services SC24-6084-00
136 * VMCP_GETCODE: gives the CP return code back to user space
137 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
138 * expects adjacent pages in real storage and to make matters worse, we
139 * dont know the size of the response. Therefore we default to PAGESIZE and
140 * let userspace to change the response size, if userspace expects a bigger
141 * response
143 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
145 struct vmcp_session *session;
146 int temp;
148 session = (struct vmcp_session *)file->private_data;
149 if (mutex_lock_interruptible(&session->mutex))
150 return -ERESTARTSYS;
151 switch (cmd) {
152 case VMCP_GETCODE:
153 temp = session->resp_code;
154 mutex_unlock(&session->mutex);
155 return put_user(temp, (int __user *)arg);
156 case VMCP_SETBUF:
157 free_pages((unsigned long)session->response,
158 get_order(session->bufsize));
159 session->response=NULL;
160 temp = get_user(session->bufsize, (int __user *)arg);
161 if (get_order(session->bufsize) > 8) {
162 session->bufsize = PAGE_SIZE;
163 temp = -EINVAL;
165 mutex_unlock(&session->mutex);
166 return temp;
167 case VMCP_GETSIZE:
168 temp = session->resp_size;
169 mutex_unlock(&session->mutex);
170 return put_user(temp, (int __user *)arg);
171 default:
172 mutex_unlock(&session->mutex);
173 return -ENOIOCTLCMD;
177 static const struct file_operations vmcp_fops = {
178 .owner = THIS_MODULE,
179 .open = vmcp_open,
180 .release = vmcp_release,
181 .read = vmcp_read,
182 .write = vmcp_write,
183 .unlocked_ioctl = vmcp_ioctl,
184 .compat_ioctl = vmcp_ioctl,
187 static struct miscdevice vmcp_dev = {
188 .name = "vmcp",
189 .minor = MISC_DYNAMIC_MINOR,
190 .fops = &vmcp_fops,
193 static int __init vmcp_init(void)
195 int ret;
197 if (!MACHINE_IS_VM) {
198 PRINT_WARN("z/VM CP interface is only available under z/VM\n");
199 return -ENODEV;
201 vmcp_debug = debug_register("vmcp", 1, 1, 240);
202 if (!vmcp_debug) {
203 PRINT_ERR("z/VM CP interface not loaded. Could not register "
204 "debug feature\n");
205 return -ENOMEM;
207 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
208 if (ret) {
209 PRINT_ERR("z/VM CP interface not loaded. Could not register "
210 "debug feature view. Error code: %d\n", ret);
211 debug_unregister(vmcp_debug);
212 return ret;
214 ret = misc_register(&vmcp_dev);
215 if (ret) {
216 PRINT_ERR("z/VM CP interface not loaded. Could not register "
217 "misc device. Error code: %d\n", ret);
218 debug_unregister(vmcp_debug);
219 return ret;
221 PRINT_INFO("z/VM CP interface loaded\n");
222 return 0;
225 static void __exit vmcp_exit(void)
227 misc_deregister(&vmcp_dev);
228 debug_unregister(vmcp_debug);
229 PRINT_INFO("z/VM CP interface unloaded.\n");
232 module_init(vmcp_init);
233 module_exit(vmcp_exit);