Merge branch 'for-linus' of git://git.alsa-project.org/alsa-kernel
[pandora-kernel.git] / drivers / s390 / char / vmcp.c
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>
5  *
6  *
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.
10
11  * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS
12  */
13
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"
23
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
26 MODULE_DESCRIPTION("z/VM CP interface");
27
28 #define PRINTK_HEADER "vmcp: "
29
30 static debug_info_t *vmcp_debug;
31
32 static int vmcp_open(struct inode *inode, struct file *file)
33 {
34         struct vmcp_session *session;
35
36         if (!capable(CAP_SYS_ADMIN))
37                 return -EPERM;
38
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);
48 }
49
50 static int vmcp_release(struct inode *inode, struct file *file)
51 {
52         struct vmcp_session *session;
53
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;
59 }
60
61 static ssize_t
62 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
63 {
64         ssize_t ret;
65         size_t size;
66         struct vmcp_session *session;
67
68         session = file->private_data;
69         if (mutex_lock_interruptible(&session->mutex))
70                 return -ERESTARTSYS;
71         if (!session->response) {
72                 mutex_unlock(&session->mutex);
73                 return 0;
74         }
75         size = min_t(size_t, session->resp_size, session->bufsize);
76         ret = simple_read_from_buffer(buff, count, ppos,
77                                         session->response, size);
78
79         mutex_unlock(&session->mutex);
80
81         return ret;
82 }
83
84 static ssize_t
85 vmcp_write(struct file *file, const char __user *buff, size_t count,
86            loff_t *ppos)
87 {
88         char *cmd;
89         struct vmcp_session *session;
90
91         if (count > 240)
92                 return -EINVAL;
93         cmd = kmalloc(count + 1, GFP_KERNEL);
94         if (!cmd)
95                 return -ENOMEM;
96         if (copy_from_user(cmd, buff, count)) {
97                 kfree(cmd);
98                 return -EFAULT;
99         }
100         cmd[count] = '\0';
101         session = (struct vmcp_session *)file->private_data;
102         if (mutex_lock_interruptible(&session->mutex)) {
103                 kfree(cmd);
104                 return -ERESTARTSYS;
105         }
106         if (!session->response)
107                 session->response = (char *)__get_free_pages(GFP_KERNEL
108                                                 | __GFP_REPEAT | GFP_DMA,
109                                                 get_order(session->bufsize));
110         if (!session->response) {
111                 mutex_unlock(&session->mutex);
112                 kfree(cmd);
113                 return -ENOMEM;
114         }
115         debug_text_event(vmcp_debug, 1, cmd);
116         session->resp_size = cpcmd(cmd, session->response, session->bufsize,
117                                    &session->resp_code);
118         mutex_unlock(&session->mutex);
119         kfree(cmd);
120         *ppos = 0;              /* reset the file pointer after a command */
121         return count;
122 }
123
124
125 /*
126  * These ioctls are available, as the semantics of the diagnose 8 call
127  * does not fit very well into a Linux call. Diagnose X'08' is described in
128  * CP Programming Services SC24-6084-00
129  *
130  * VMCP_GETCODE: gives the CP return code back to user space
131  * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
132  * expects adjacent pages in real storage and to make matters worse, we
133  * dont know the size of the response. Therefore we default to PAGESIZE and
134  * let userspace to change the response size, if userspace expects a bigger
135  * response
136  */
137 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
138 {
139         struct vmcp_session *session;
140         int temp;
141
142         session = (struct vmcp_session *)file->private_data;
143         if (mutex_lock_interruptible(&session->mutex))
144                 return -ERESTARTSYS;
145         switch (cmd) {
146         case VMCP_GETCODE:
147                 temp = session->resp_code;
148                 mutex_unlock(&session->mutex);
149                 return put_user(temp, (int __user *)arg);
150         case VMCP_SETBUF:
151                 free_pages((unsigned long)session->response,
152                                 get_order(session->bufsize));
153                 session->response=NULL;
154                 temp = get_user(session->bufsize, (int __user *)arg);
155                 if (get_order(session->bufsize) > 8) {
156                         session->bufsize = PAGE_SIZE;
157                         temp = -EINVAL;
158                 }
159                 mutex_unlock(&session->mutex);
160                 return temp;
161         case VMCP_GETSIZE:
162                 temp = session->resp_size;
163                 mutex_unlock(&session->mutex);
164                 return put_user(temp, (int __user *)arg);
165         default:
166                 mutex_unlock(&session->mutex);
167                 return -ENOIOCTLCMD;
168         }
169 }
170
171 static const struct file_operations vmcp_fops = {
172         .owner          = THIS_MODULE,
173         .open           = vmcp_open,
174         .release        = vmcp_release,
175         .read           = vmcp_read,
176         .write          = vmcp_write,
177         .unlocked_ioctl = vmcp_ioctl,
178         .compat_ioctl   = vmcp_ioctl,
179 };
180
181 static struct miscdevice vmcp_dev = {
182         .name   = "vmcp",
183         .minor  = MISC_DYNAMIC_MINOR,
184         .fops   = &vmcp_fops,
185 };
186
187 static int __init vmcp_init(void)
188 {
189         int ret;
190
191         if (!MACHINE_IS_VM) {
192                 PRINT_WARN("z/VM CP interface is only available under z/VM\n");
193                 return -ENODEV;
194         }
195
196         vmcp_debug = debug_register("vmcp", 1, 1, 240);
197         if (!vmcp_debug)
198                 return -ENOMEM;
199
200         ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
201         if (ret) {
202                 debug_unregister(vmcp_debug);
203                 return ret;
204         }
205
206         ret = misc_register(&vmcp_dev);
207         if (ret) {
208                 debug_unregister(vmcp_debug);
209                 return ret;
210         }
211
212         return 0;
213 }
214
215 static void __exit vmcp_exit(void)
216 {
217         misc_deregister(&vmcp_dev);
218         debug_unregister(vmcp_debug);
219 }
220
221 module_init(vmcp_init);
222 module_exit(vmcp_exit);