Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Copyright IBM Corp. 2004,2010
  3 * Interface implementation for communication with the z/VM control program
  4 *
  5 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
  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/slab.h>
 
 19#include <asm/compat.h>
 20#include <asm/cpcmd.h>
 21#include <asm/debug.h>
 22#include <asm/uaccess.h>
 23#include "vmcp.h"
 24
 25static debug_info_t *vmcp_debug;
 26
 27static int vmcp_open(struct inode *inode, struct file *file)
 28{
 29	struct vmcp_session *session;
 30
 31	if (!capable(CAP_SYS_ADMIN))
 32		return -EPERM;
 33
 34	session = kmalloc(sizeof(*session), GFP_KERNEL);
 35	if (!session)
 36		return -ENOMEM;
 37
 38	session->bufsize = PAGE_SIZE;
 39	session->response = NULL;
 40	session->resp_size = 0;
 41	mutex_init(&session->mutex);
 42	file->private_data = session;
 43	return nonseekable_open(inode, file);
 44}
 45
 46static int vmcp_release(struct inode *inode, struct file *file)
 47{
 48	struct vmcp_session *session;
 49
 50	session = file->private_data;
 51	file->private_data = NULL;
 52	free_pages((unsigned long)session->response, get_order(session->bufsize));
 53	kfree(session);
 54	return 0;
 55}
 56
 57static ssize_t
 58vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
 59{
 60	ssize_t ret;
 61	size_t size;
 62	struct vmcp_session *session;
 63
 64	session = file->private_data;
 65	if (mutex_lock_interruptible(&session->mutex))
 66		return -ERESTARTSYS;
 67	if (!session->response) {
 68		mutex_unlock(&session->mutex);
 69		return 0;
 70	}
 71	size = min_t(size_t, session->resp_size, session->bufsize);
 72	ret = simple_read_from_buffer(buff, count, ppos,
 73					session->response, size);
 74
 75	mutex_unlock(&session->mutex);
 76
 77	return ret;
 78}
 79
 80static ssize_t
 81vmcp_write(struct file *file, const char __user *buff, size_t count,
 82	   loff_t *ppos)
 83{
 84	char *cmd;
 85	struct vmcp_session *session;
 86
 87	if (count > 240)
 88		return -EINVAL;
 89	cmd = kmalloc(count + 1, GFP_KERNEL);
 90	if (!cmd)
 91		return -ENOMEM;
 92	if (copy_from_user(cmd, buff, count)) {
 93		kfree(cmd);
 94		return -EFAULT;
 95	}
 96	cmd[count] = '\0';
 97	session = file->private_data;
 98	if (mutex_lock_interruptible(&session->mutex)) {
 99		kfree(cmd);
100		return -ERESTARTSYS;
101	}
102	if (!session->response)
103		session->response = (char *)__get_free_pages(GFP_KERNEL
104						| __GFP_REPEAT | GFP_DMA,
105						get_order(session->bufsize));
106	if (!session->response) {
107		mutex_unlock(&session->mutex);
108		kfree(cmd);
109		return -ENOMEM;
110	}
111	debug_text_event(vmcp_debug, 1, cmd);
112	session->resp_size = cpcmd(cmd, session->response, session->bufsize,
113				   &session->resp_code);
114	mutex_unlock(&session->mutex);
115	kfree(cmd);
116	*ppos = 0;		/* reset the file pointer after a command */
117	return count;
118}
119
120
121/*
122 * These ioctls are available, as the semantics of the diagnose 8 call
123 * does not fit very well into a Linux call. Diagnose X'08' is described in
124 * CP Programming Services SC24-6084-00
125 *
126 * VMCP_GETCODE: gives the CP return code back to user space
127 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
128 * expects adjacent pages in real storage and to make matters worse, we
129 * dont know the size of the response. Therefore we default to PAGESIZE and
130 * let userspace to change the response size, if userspace expects a bigger
131 * response
132 */
133static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
134{
135	struct vmcp_session *session;
136	int __user *argp;
137	int temp;
138
139	session = file->private_data;
140	if (is_compat_task())
141		argp = compat_ptr(arg);
142	else
143		argp = (int __user *)arg;
144	if (mutex_lock_interruptible(&session->mutex))
145		return -ERESTARTSYS;
146	switch (cmd) {
147	case VMCP_GETCODE:
148		temp = session->resp_code;
149		mutex_unlock(&session->mutex);
150		return put_user(temp, argp);
151	case VMCP_SETBUF:
152		free_pages((unsigned long)session->response,
153				get_order(session->bufsize));
154		session->response=NULL;
155		temp = get_user(session->bufsize, argp);
156		if (get_order(session->bufsize) > 8) {
157			session->bufsize = PAGE_SIZE;
158			temp = -EINVAL;
159		}
160		mutex_unlock(&session->mutex);
161		return temp;
162	case VMCP_GETSIZE:
163		temp = session->resp_size;
164		mutex_unlock(&session->mutex);
165		return put_user(temp, argp);
166	default:
167		mutex_unlock(&session->mutex);
168		return -ENOIOCTLCMD;
169	}
170}
171
172static const struct file_operations vmcp_fops = {
173	.owner		= THIS_MODULE,
174	.open		= vmcp_open,
175	.release	= vmcp_release,
176	.read		= vmcp_read,
177	.write		= vmcp_write,
178	.unlocked_ioctl	= vmcp_ioctl,
179	.compat_ioctl	= vmcp_ioctl,
180	.llseek		= no_llseek,
181};
182
183static struct miscdevice vmcp_dev = {
184	.name	= "vmcp",
185	.minor	= MISC_DYNAMIC_MINOR,
186	.fops	= &vmcp_fops,
187};
188
189static int __init vmcp_init(void)
190{
191	int ret;
192
193	if (!MACHINE_IS_VM)
194		return 0;
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}
211device_initcall(vmcp_init);
v4.6
  1/*
  2 * Copyright IBM Corp. 2004, 2010
  3 * Interface implementation for communication with the z/VM control program
  4 *
  5 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
  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/compat.h>
 17#include <linux/kernel.h>
 18#include <linux/miscdevice.h>
 19#include <linux/slab.h>
 20#include <linux/export.h>
 21#include <asm/compat.h>
 22#include <asm/cpcmd.h>
 23#include <asm/debug.h>
 24#include <asm/uaccess.h>
 25#include "vmcp.h"
 26
 27static debug_info_t *vmcp_debug;
 28
 29static int vmcp_open(struct inode *inode, struct file *file)
 30{
 31	struct vmcp_session *session;
 32
 33	if (!capable(CAP_SYS_ADMIN))
 34		return -EPERM;
 35
 36	session = kmalloc(sizeof(*session), GFP_KERNEL);
 37	if (!session)
 38		return -ENOMEM;
 39
 40	session->bufsize = PAGE_SIZE;
 41	session->response = NULL;
 42	session->resp_size = 0;
 43	mutex_init(&session->mutex);
 44	file->private_data = session;
 45	return nonseekable_open(inode, file);
 46}
 47
 48static int vmcp_release(struct inode *inode, struct file *file)
 49{
 50	struct vmcp_session *session;
 51
 52	session = file->private_data;
 53	file->private_data = NULL;
 54	free_pages((unsigned long)session->response, get_order(session->bufsize));
 55	kfree(session);
 56	return 0;
 57}
 58
 59static ssize_t
 60vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
 61{
 62	ssize_t ret;
 63	size_t size;
 64	struct vmcp_session *session;
 65
 66	session = file->private_data;
 67	if (mutex_lock_interruptible(&session->mutex))
 68		return -ERESTARTSYS;
 69	if (!session->response) {
 70		mutex_unlock(&session->mutex);
 71		return 0;
 72	}
 73	size = min_t(size_t, session->resp_size, session->bufsize);
 74	ret = simple_read_from_buffer(buff, count, ppos,
 75					session->response, size);
 76
 77	mutex_unlock(&session->mutex);
 78
 79	return ret;
 80}
 81
 82static ssize_t
 83vmcp_write(struct file *file, const char __user *buff, size_t count,
 84	   loff_t *ppos)
 85{
 86	char *cmd;
 87	struct vmcp_session *session;
 88
 89	if (count > 240)
 90		return -EINVAL;
 91	cmd = memdup_user_nul(buff, count);
 92	if (IS_ERR(cmd))
 93		return PTR_ERR(cmd);
 
 
 
 
 
 94	session = file->private_data;
 95	if (mutex_lock_interruptible(&session->mutex)) {
 96		kfree(cmd);
 97		return -ERESTARTSYS;
 98	}
 99	if (!session->response)
100		session->response = (char *)__get_free_pages(GFP_KERNEL
101						| __GFP_REPEAT | GFP_DMA,
102						get_order(session->bufsize));
103	if (!session->response) {
104		mutex_unlock(&session->mutex);
105		kfree(cmd);
106		return -ENOMEM;
107	}
108	debug_text_event(vmcp_debug, 1, cmd);
109	session->resp_size = cpcmd(cmd, session->response, session->bufsize,
110				   &session->resp_code);
111	mutex_unlock(&session->mutex);
112	kfree(cmd);
113	*ppos = 0;		/* reset the file pointer after a command */
114	return count;
115}
116
117
118/*
119 * These ioctls are available, as the semantics of the diagnose 8 call
120 * does not fit very well into a Linux call. Diagnose X'08' is described in
121 * CP Programming Services SC24-6084-00
122 *
123 * VMCP_GETCODE: gives the CP return code back to user space
124 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
125 * expects adjacent pages in real storage and to make matters worse, we
126 * dont know the size of the response. Therefore we default to PAGESIZE and
127 * let userspace to change the response size, if userspace expects a bigger
128 * response
129 */
130static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
131{
132	struct vmcp_session *session;
133	int __user *argp;
134	int temp;
135
136	session = file->private_data;
137	if (is_compat_task())
138		argp = compat_ptr(arg);
139	else
140		argp = (int __user *)arg;
141	if (mutex_lock_interruptible(&session->mutex))
142		return -ERESTARTSYS;
143	switch (cmd) {
144	case VMCP_GETCODE:
145		temp = session->resp_code;
146		mutex_unlock(&session->mutex);
147		return put_user(temp, argp);
148	case VMCP_SETBUF:
149		free_pages((unsigned long)session->response,
150				get_order(session->bufsize));
151		session->response=NULL;
152		temp = get_user(session->bufsize, argp);
153		if (get_order(session->bufsize) > 8) {
154			session->bufsize = PAGE_SIZE;
155			temp = -EINVAL;
156		}
157		mutex_unlock(&session->mutex);
158		return temp;
159	case VMCP_GETSIZE:
160		temp = session->resp_size;
161		mutex_unlock(&session->mutex);
162		return put_user(temp, argp);
163	default:
164		mutex_unlock(&session->mutex);
165		return -ENOIOCTLCMD;
166	}
167}
168
169static const struct file_operations vmcp_fops = {
170	.owner		= THIS_MODULE,
171	.open		= vmcp_open,
172	.release	= vmcp_release,
173	.read		= vmcp_read,
174	.write		= vmcp_write,
175	.unlocked_ioctl	= vmcp_ioctl,
176	.compat_ioctl	= vmcp_ioctl,
177	.llseek		= no_llseek,
178};
179
180static struct miscdevice vmcp_dev = {
181	.name	= "vmcp",
182	.minor	= MISC_DYNAMIC_MINOR,
183	.fops	= &vmcp_fops,
184};
185
186static int __init vmcp_init(void)
187{
188	int ret;
189
190	if (!MACHINE_IS_VM)
191		return 0;
192
193	vmcp_debug = debug_register("vmcp", 1, 1, 240);
194	if (!vmcp_debug)
195		return -ENOMEM;
196
197	ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
198	if (ret) {
199		debug_unregister(vmcp_debug);
200		return ret;
201	}
202
203	ret = misc_register(&vmcp_dev);
204	if (ret)
205		debug_unregister(vmcp_debug);
206	return ret;
207}
208device_initcall(vmcp_init);