s390/sclp: Add SCLP character device driver
[linux-2.6-block.git] / drivers / s390 / char / sclp_ctl.c
CommitLineData
d475f942
MH
1/*
2 * IOCTL interface for SCLP
3 *
4 * Copyright IBM Corp. 2012
5 *
6 * Author: Michael Holzheu <holzheu@linux.vnet.ibm.com>
7 */
8
9#include <linux/compat.h>
10#include <linux/uaccess.h>
11#include <linux/miscdevice.h>
12#include <linux/gfp.h>
13#include <linux/module.h>
14#include <linux/ioctl.h>
15#include <linux/fs.h>
16#include <linux/compat.h>
17#include <asm/compat.h>
18#include <asm/sclp_ctl.h>
19#include <asm/sclp.h>
20
21#include "sclp.h"
22
23/*
24 * Supported command words
25 */
26static unsigned int sclp_ctl_sccb_wlist[] = {
27 0x00400002,
28 0x00410002,
29};
30
31/*
32 * Check if command word is supported
33 */
34static int sclp_ctl_cmdw_supported(unsigned int cmdw)
35{
36 int i;
37
38 for (i = 0; i < ARRAY_SIZE(sclp_ctl_sccb_wlist); i++) {
39 if (cmdw == sclp_ctl_sccb_wlist[i])
40 return 1;
41 }
42 return 0;
43}
44
45static void __user *u64_to_uptr(u64 value)
46{
47 if (is_compat_task())
48 return compat_ptr(value);
49 else
50 return (void __user *)(unsigned long)value;
51}
52
53/*
54 * Start SCLP request
55 */
56static int sclp_ctl_ioctl_sccb(void __user *user_area)
57{
58 struct sclp_ctl_sccb ctl_sccb;
59 struct sccb_header *sccb;
60 int rc;
61
62 if (copy_from_user(&ctl_sccb, user_area, sizeof(ctl_sccb)))
63 return -EFAULT;
64 if (!sclp_ctl_cmdw_supported(ctl_sccb.cmdw))
65 return -EOPNOTSUPP;
66 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
67 if (!sccb)
68 return -ENOMEM;
69 if (copy_from_user(sccb, u64_to_uptr(ctl_sccb.sccb), sizeof(*sccb))) {
70 rc = -EFAULT;
71 goto out_free;
72 }
73 if (sccb->length > PAGE_SIZE || sccb->length < 8)
74 return -EINVAL;
75 if (copy_from_user(sccb, u64_to_uptr(ctl_sccb.sccb), sccb->length)) {
76 rc = -EFAULT;
77 goto out_free;
78 }
79 rc = sclp_sync_request(ctl_sccb.cmdw, sccb);
80 if (rc)
81 goto out_free;
82 if (copy_to_user(u64_to_uptr(ctl_sccb.sccb), sccb, sccb->length))
83 rc = -EFAULT;
84out_free:
85 free_page((unsigned long) sccb);
86 return rc;
87}
88
89/*
90 * SCLP SCCB ioctl function
91 */
92static long sclp_ctl_ioctl(struct file *filp, unsigned int cmd,
93 unsigned long arg)
94{
95 void __user *argp;
96
97 if (is_compat_task())
98 argp = compat_ptr(arg);
99 else
100 argp = (void __user *) arg;
101 switch (cmd) {
102 case SCLP_CTL_SCCB:
103 return sclp_ctl_ioctl_sccb(argp);
104 default: /* unknown ioctl number */
105 return -ENOTTY;
106 }
107}
108
109/*
110 * File operations
111 */
112static const struct file_operations sclp_ctl_fops = {
113 .owner = THIS_MODULE,
114 .open = nonseekable_open,
115 .unlocked_ioctl = sclp_ctl_ioctl,
116 .compat_ioctl = sclp_ctl_ioctl,
117 .llseek = no_llseek,
118};
119
120/*
121 * Misc device definition
122 */
123static struct miscdevice sclp_ctl_device = {
124 .minor = MISC_DYNAMIC_MINOR,
125 .name = "sclp",
126 .fops = &sclp_ctl_fops,
127};
128
129/*
130 * Register sclp_ctl misc device
131 */
132static int __init sclp_ctl_init(void)
133{
134 return misc_register(&sclp_ctl_device);
135}
136module_init(sclp_ctl_init);
137
138/*
139 * Deregister sclp_ctl misc device
140 */
141static void __exit sclp_ctl_exit(void)
142{
143 misc_deregister(&sclp_ctl_device);
144}
145module_exit(sclp_ctl_exit);