Merge remote-tracking branch 'regulator/fix/core' into tmp
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / comedi_compat32.c
1 /*
2     comedi/comedi_compat32.c
3     32-bit ioctl compatibility for 64-bit comedi kernel module.
4
5     Author: Ian Abbott, MEV Ltd. <abbotti@mev.co.uk>
6     Copyright (C) 2007 MEV Ltd. <http://www.mev.co.uk/>
7
8     COMEDI - Linux Control and Measurement Device Interface
9     Copyright (C) 1997-2007 David A. Schleef <ds@schleef.org>
10
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation; either version 2 of the License, or
14     (at your option) any later version.
15
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 */
26
27 #include <linux/uaccess.h>
28 #include <linux/compat.h>
29 #include <linux/fs.h>
30 #include "comedi.h"
31 #include "comedi_compat32.h"
32
33 #define COMEDI32_CHANINFO _IOR(CIO, 3, struct comedi32_chaninfo_struct)
34 #define COMEDI32_RANGEINFO _IOR(CIO, 8, struct comedi32_rangeinfo_struct)
35 /* N.B. COMEDI32_CMD and COMEDI_CMD ought to use _IOWR, not _IOR.
36  * It's too late to change it now, but it only affects the command number. */
37 #define COMEDI32_CMD _IOR(CIO, 9, struct comedi32_cmd_struct)
38 /* N.B. COMEDI32_CMDTEST and COMEDI_CMDTEST ought to use _IOWR, not _IOR.
39  * It's too late to change it now, but it only affects the command number. */
40 #define COMEDI32_CMDTEST _IOR(CIO, 10, struct comedi32_cmd_struct)
41 #define COMEDI32_INSNLIST _IOR(CIO, 11, struct comedi32_insnlist_struct)
42 #define COMEDI32_INSN _IOR(CIO, 12, struct comedi32_insn_struct)
43
44 struct comedi32_chaninfo_struct {
45         unsigned int subdev;
46         compat_uptr_t maxdata_list;     /* 32-bit 'unsigned int *' */
47         compat_uptr_t flaglist; /* 32-bit 'unsigned int *' */
48         compat_uptr_t rangelist;        /* 32-bit 'unsigned int *' */
49         unsigned int unused[4];
50 };
51
52 struct comedi32_rangeinfo_struct {
53         unsigned int range_type;
54         compat_uptr_t range_ptr;        /* 32-bit 'void *' */
55 };
56
57 struct comedi32_cmd_struct {
58         unsigned int subdev;
59         unsigned int flags;
60         unsigned int start_src;
61         unsigned int start_arg;
62         unsigned int scan_begin_src;
63         unsigned int scan_begin_arg;
64         unsigned int convert_src;
65         unsigned int convert_arg;
66         unsigned int scan_end_src;
67         unsigned int scan_end_arg;
68         unsigned int stop_src;
69         unsigned int stop_arg;
70         compat_uptr_t chanlist; /* 32-bit 'unsigned int *' */
71         unsigned int chanlist_len;
72         compat_uptr_t data;     /* 32-bit 'short *' */
73         unsigned int data_len;
74 };
75
76 struct comedi32_insn_struct {
77         unsigned int insn;
78         unsigned int n;
79         compat_uptr_t data;     /* 32-bit 'unsigned int *' */
80         unsigned int subdev;
81         unsigned int chanspec;
82         unsigned int unused[3];
83 };
84
85 struct comedi32_insnlist_struct {
86         unsigned int n_insns;
87         compat_uptr_t insns;    /* 32-bit 'struct comedi_insn *' */
88 };
89
90 /* Handle translated ioctl. */
91 static int translated_ioctl(struct file *file, unsigned int cmd,
92                             unsigned long arg)
93 {
94         if (!file->f_op)
95                 return -ENOTTY;
96
97         if (file->f_op->unlocked_ioctl)
98                 return file->f_op->unlocked_ioctl(file, cmd, arg);
99
100         return -ENOTTY;
101 }
102
103 /* Handle 32-bit COMEDI_CHANINFO ioctl. */
104 static int compat_chaninfo(struct file *file, unsigned long arg)
105 {
106         struct comedi_chaninfo __user *chaninfo;
107         struct comedi32_chaninfo_struct __user *chaninfo32;
108         int err;
109         union {
110                 unsigned int uint;
111                 compat_uptr_t uptr;
112         } temp;
113
114         chaninfo32 = compat_ptr(arg);
115         chaninfo = compat_alloc_user_space(sizeof(*chaninfo));
116
117         /* Copy chaninfo structure.  Ignore unused members. */
118         if (!access_ok(VERIFY_READ, chaninfo32, sizeof(*chaninfo32))
119             || !access_ok(VERIFY_WRITE, chaninfo, sizeof(*chaninfo))) {
120                 return -EFAULT;
121         }
122         err = 0;
123         err |= __get_user(temp.uint, &chaninfo32->subdev);
124         err |= __put_user(temp.uint, &chaninfo->subdev);
125         err |= __get_user(temp.uptr, &chaninfo32->maxdata_list);
126         err |= __put_user(compat_ptr(temp.uptr), &chaninfo->maxdata_list);
127         err |= __get_user(temp.uptr, &chaninfo32->flaglist);
128         err |= __put_user(compat_ptr(temp.uptr), &chaninfo->flaglist);
129         err |= __get_user(temp.uptr, &chaninfo32->rangelist);
130         err |= __put_user(compat_ptr(temp.uptr), &chaninfo->rangelist);
131         if (err)
132                 return -EFAULT;
133
134         return translated_ioctl(file, COMEDI_CHANINFO, (unsigned long)chaninfo);
135 }
136
137 /* Handle 32-bit COMEDI_RANGEINFO ioctl. */
138 static int compat_rangeinfo(struct file *file, unsigned long arg)
139 {
140         struct comedi_rangeinfo __user *rangeinfo;
141         struct comedi32_rangeinfo_struct __user *rangeinfo32;
142         int err;
143         union {
144                 unsigned int uint;
145                 compat_uptr_t uptr;
146         } temp;
147
148         rangeinfo32 = compat_ptr(arg);
149         rangeinfo = compat_alloc_user_space(sizeof(*rangeinfo));
150
151         /* Copy rangeinfo structure. */
152         if (!access_ok(VERIFY_READ, rangeinfo32, sizeof(*rangeinfo32))
153             || !access_ok(VERIFY_WRITE, rangeinfo, sizeof(*rangeinfo))) {
154                 return -EFAULT;
155         }
156         err = 0;
157         err |= __get_user(temp.uint, &rangeinfo32->range_type);
158         err |= __put_user(temp.uint, &rangeinfo->range_type);
159         err |= __get_user(temp.uptr, &rangeinfo32->range_ptr);
160         err |= __put_user(compat_ptr(temp.uptr), &rangeinfo->range_ptr);
161         if (err)
162                 return -EFAULT;
163
164         return translated_ioctl(file, COMEDI_RANGEINFO,
165                                 (unsigned long)rangeinfo);
166 }
167
168 /* Copy 32-bit cmd structure to native cmd structure. */
169 static int get_compat_cmd(struct comedi_cmd __user *cmd,
170                           struct comedi32_cmd_struct __user *cmd32)
171 {
172         int err;
173         union {
174                 unsigned int uint;
175                 compat_uptr_t uptr;
176         } temp;
177
178         /* Copy cmd structure. */
179         if (!access_ok(VERIFY_READ, cmd32, sizeof(*cmd32))
180             || !access_ok(VERIFY_WRITE, cmd, sizeof(*cmd))) {
181                 return -EFAULT;
182         }
183         err = 0;
184         err |= __get_user(temp.uint, &cmd32->subdev);
185         err |= __put_user(temp.uint, &cmd->subdev);
186         err |= __get_user(temp.uint, &cmd32->flags);
187         err |= __put_user(temp.uint, &cmd->flags);
188         err |= __get_user(temp.uint, &cmd32->start_src);
189         err |= __put_user(temp.uint, &cmd->start_src);
190         err |= __get_user(temp.uint, &cmd32->start_arg);
191         err |= __put_user(temp.uint, &cmd->start_arg);
192         err |= __get_user(temp.uint, &cmd32->scan_begin_src);
193         err |= __put_user(temp.uint, &cmd->scan_begin_src);
194         err |= __get_user(temp.uint, &cmd32->scan_begin_arg);
195         err |= __put_user(temp.uint, &cmd->scan_begin_arg);
196         err |= __get_user(temp.uint, &cmd32->convert_src);
197         err |= __put_user(temp.uint, &cmd->convert_src);
198         err |= __get_user(temp.uint, &cmd32->convert_arg);
199         err |= __put_user(temp.uint, &cmd->convert_arg);
200         err |= __get_user(temp.uint, &cmd32->scan_end_src);
201         err |= __put_user(temp.uint, &cmd->scan_end_src);
202         err |= __get_user(temp.uint, &cmd32->scan_end_arg);
203         err |= __put_user(temp.uint, &cmd->scan_end_arg);
204         err |= __get_user(temp.uint, &cmd32->stop_src);
205         err |= __put_user(temp.uint, &cmd->stop_src);
206         err |= __get_user(temp.uint, &cmd32->stop_arg);
207         err |= __put_user(temp.uint, &cmd->stop_arg);
208         err |= __get_user(temp.uptr, &cmd32->chanlist);
209         err |= __put_user(compat_ptr(temp.uptr), &cmd->chanlist);
210         err |= __get_user(temp.uint, &cmd32->chanlist_len);
211         err |= __put_user(temp.uint, &cmd->chanlist_len);
212         err |= __get_user(temp.uptr, &cmd32->data);
213         err |= __put_user(compat_ptr(temp.uptr), &cmd->data);
214         err |= __get_user(temp.uint, &cmd32->data_len);
215         err |= __put_user(temp.uint, &cmd->data_len);
216         return err ? -EFAULT : 0;
217 }
218
219 /* Copy native cmd structure to 32-bit cmd structure. */
220 static int put_compat_cmd(struct comedi32_cmd_struct __user *cmd32,
221                           struct comedi_cmd __user *cmd)
222 {
223         int err;
224         unsigned int temp;
225
226         /* Copy back most of cmd structure. */
227         /* Assume the pointer values are already valid. */
228         /* (Could use ptr_to_compat() to set them, but that wasn't implemented
229          * until kernel version 2.6.11.) */
230         if (!access_ok(VERIFY_READ, cmd, sizeof(*cmd))
231             || !access_ok(VERIFY_WRITE, cmd32, sizeof(*cmd32))) {
232                 return -EFAULT;
233         }
234         err = 0;
235         err |= __get_user(temp, &cmd->subdev);
236         err |= __put_user(temp, &cmd32->subdev);
237         err |= __get_user(temp, &cmd->flags);
238         err |= __put_user(temp, &cmd32->flags);
239         err |= __get_user(temp, &cmd->start_src);
240         err |= __put_user(temp, &cmd32->start_src);
241         err |= __get_user(temp, &cmd->start_arg);
242         err |= __put_user(temp, &cmd32->start_arg);
243         err |= __get_user(temp, &cmd->scan_begin_src);
244         err |= __put_user(temp, &cmd32->scan_begin_src);
245         err |= __get_user(temp, &cmd->scan_begin_arg);
246         err |= __put_user(temp, &cmd32->scan_begin_arg);
247         err |= __get_user(temp, &cmd->convert_src);
248         err |= __put_user(temp, &cmd32->convert_src);
249         err |= __get_user(temp, &cmd->convert_arg);
250         err |= __put_user(temp, &cmd32->convert_arg);
251         err |= __get_user(temp, &cmd->scan_end_src);
252         err |= __put_user(temp, &cmd32->scan_end_src);
253         err |= __get_user(temp, &cmd->scan_end_arg);
254         err |= __put_user(temp, &cmd32->scan_end_arg);
255         err |= __get_user(temp, &cmd->stop_src);
256         err |= __put_user(temp, &cmd32->stop_src);
257         err |= __get_user(temp, &cmd->stop_arg);
258         err |= __put_user(temp, &cmd32->stop_arg);
259         /* Assume chanlist pointer is unchanged. */
260         err |= __get_user(temp, &cmd->chanlist_len);
261         err |= __put_user(temp, &cmd32->chanlist_len);
262         /* Assume data pointer is unchanged. */
263         err |= __get_user(temp, &cmd->data_len);
264         err |= __put_user(temp, &cmd32->data_len);
265         return err ? -EFAULT : 0;
266 }
267
268 /* Handle 32-bit COMEDI_CMD ioctl. */
269 static int compat_cmd(struct file *file, unsigned long arg)
270 {
271         struct comedi_cmd __user *cmd;
272         struct comedi32_cmd_struct __user *cmd32;
273         int rc;
274
275         cmd32 = compat_ptr(arg);
276         cmd = compat_alloc_user_space(sizeof(*cmd));
277
278         rc = get_compat_cmd(cmd, cmd32);
279         if (rc)
280                 return rc;
281
282         return translated_ioctl(file, COMEDI_CMD, (unsigned long)cmd);
283 }
284
285 /* Handle 32-bit COMEDI_CMDTEST ioctl. */
286 static int compat_cmdtest(struct file *file, unsigned long arg)
287 {
288         struct comedi_cmd __user *cmd;
289         struct comedi32_cmd_struct __user *cmd32;
290         int rc, err;
291
292         cmd32 = compat_ptr(arg);
293         cmd = compat_alloc_user_space(sizeof(*cmd));
294
295         rc = get_compat_cmd(cmd, cmd32);
296         if (rc)
297                 return rc;
298
299         rc = translated_ioctl(file, COMEDI_CMDTEST, (unsigned long)cmd);
300         if (rc < 0)
301                 return rc;
302
303         err = put_compat_cmd(cmd32, cmd);
304         if (err)
305                 rc = err;
306
307         return rc;
308 }
309
310 /* Copy 32-bit insn structure to native insn structure. */
311 static int get_compat_insn(struct comedi_insn __user *insn,
312                            struct comedi32_insn_struct __user *insn32)
313 {
314         int err;
315         union {
316                 unsigned int uint;
317                 compat_uptr_t uptr;
318         } temp;
319
320         /* Copy insn structure.  Ignore the unused members. */
321         err = 0;
322         if (!access_ok(VERIFY_READ, insn32, sizeof(*insn32))
323             || !access_ok(VERIFY_WRITE, insn, sizeof(*insn)))
324                 return -EFAULT;
325
326         err |= __get_user(temp.uint, &insn32->insn);
327         err |= __put_user(temp.uint, &insn->insn);
328         err |= __get_user(temp.uint, &insn32->n);
329         err |= __put_user(temp.uint, &insn->n);
330         err |= __get_user(temp.uptr, &insn32->data);
331         err |= __put_user(compat_ptr(temp.uptr), &insn->data);
332         err |= __get_user(temp.uint, &insn32->subdev);
333         err |= __put_user(temp.uint, &insn->subdev);
334         err |= __get_user(temp.uint, &insn32->chanspec);
335         err |= __put_user(temp.uint, &insn->chanspec);
336         return err ? -EFAULT : 0;
337 }
338
339 /* Handle 32-bit COMEDI_INSNLIST ioctl. */
340 static int compat_insnlist(struct file *file, unsigned long arg)
341 {
342         struct combined_insnlist {
343                 struct comedi_insnlist insnlist;
344                 struct comedi_insn insn[1];
345         } __user *s;
346         struct comedi32_insnlist_struct __user *insnlist32;
347         struct comedi32_insn_struct __user *insn32;
348         compat_uptr_t uptr;
349         unsigned int n_insns, n;
350         int err, rc;
351
352         insnlist32 = compat_ptr(arg);
353
354         /* Get 32-bit insnlist structure.  */
355         if (!access_ok(VERIFY_READ, insnlist32, sizeof(*insnlist32)))
356                 return -EFAULT;
357
358         err = 0;
359         err |= __get_user(n_insns, &insnlist32->n_insns);
360         err |= __get_user(uptr, &insnlist32->insns);
361         insn32 = compat_ptr(uptr);
362         if (err)
363                 return -EFAULT;
364
365         /* Allocate user memory to copy insnlist and insns into. */
366         s = compat_alloc_user_space(offsetof(struct combined_insnlist,
367                                              insn[n_insns]));
368
369         /* Set native insnlist structure. */
370         if (!access_ok(VERIFY_WRITE, &s->insnlist, sizeof(s->insnlist)))
371                 return -EFAULT;
372
373         err |= __put_user(n_insns, &s->insnlist.n_insns);
374         err |= __put_user(&s->insn[0], &s->insnlist.insns);
375         if (err)
376                 return -EFAULT;
377
378         /* Copy insn structures. */
379         for (n = 0; n < n_insns; n++) {
380                 rc = get_compat_insn(&s->insn[n], &insn32[n]);
381                 if (rc)
382                         return rc;
383         }
384
385         return translated_ioctl(file, COMEDI_INSNLIST,
386                                 (unsigned long)&s->insnlist);
387 }
388
389 /* Handle 32-bit COMEDI_INSN ioctl. */
390 static int compat_insn(struct file *file, unsigned long arg)
391 {
392         struct comedi_insn __user *insn;
393         struct comedi32_insn_struct __user *insn32;
394         int rc;
395
396         insn32 = compat_ptr(arg);
397         insn = compat_alloc_user_space(sizeof(*insn));
398
399         rc = get_compat_insn(insn, insn32);
400         if (rc)
401                 return rc;
402
403         return translated_ioctl(file, COMEDI_INSN, (unsigned long)insn);
404 }
405
406 /* Process untranslated ioctl. */
407 /* Returns -ENOIOCTLCMD for unrecognised ioctl codes. */
408 static inline int raw_ioctl(struct file *file, unsigned int cmd,
409                             unsigned long arg)
410 {
411         int rc;
412
413         switch (cmd) {
414         case COMEDI_DEVCONFIG:
415         case COMEDI_DEVINFO:
416         case COMEDI_SUBDINFO:
417         case COMEDI_BUFCONFIG:
418         case COMEDI_BUFINFO:
419                 /* Just need to translate the pointer argument. */
420                 arg = (unsigned long)compat_ptr(arg);
421                 rc = translated_ioctl(file, cmd, arg);
422                 break;
423         case COMEDI_LOCK:
424         case COMEDI_UNLOCK:
425         case COMEDI_CANCEL:
426         case COMEDI_POLL:
427                 /* No translation needed. */
428                 rc = translated_ioctl(file, cmd, arg);
429                 break;
430         case COMEDI32_CHANINFO:
431                 rc = compat_chaninfo(file, arg);
432                 break;
433         case COMEDI32_RANGEINFO:
434                 rc = compat_rangeinfo(file, arg);
435                 break;
436         case COMEDI32_CMD:
437                 rc = compat_cmd(file, arg);
438                 break;
439         case COMEDI32_CMDTEST:
440                 rc = compat_cmdtest(file, arg);
441                 break;
442         case COMEDI32_INSNLIST:
443                 rc = compat_insnlist(file, arg);
444                 break;
445         case COMEDI32_INSN:
446                 rc = compat_insn(file, arg);
447                 break;
448         default:
449                 rc = -ENOIOCTLCMD;
450                 break;
451         }
452         return rc;
453 }
454
455 /* compat_ioctl file operation. */
456 /* Returns -ENOIOCTLCMD for unrecognised ioctl codes. */
457 long comedi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
458 {
459         return raw_ioctl(file, cmd, arg);
460 }