2 comedi/comedi_compat32.c
3 32-bit ioctl compatibility for 64-bit comedi kernel module.
5 Author: Ian Abbott, MEV Ltd. <abbotti@mev.co.uk>
6 Copyright (C) 2007 MEV Ltd. <http://www.mev.co.uk/>
8 COMEDI - Linux Control and Measurement Device Interface
9 Copyright (C) 1997-2007 David A. Schleef <ds@schleef.org>
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.
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.
22 #include <linux/uaccess.h>
23 #include <linux/compat.h>
26 #include "comedi_compat32.h"
28 #define COMEDI32_CHANINFO _IOR(CIO, 3, struct comedi32_chaninfo_struct)
29 #define COMEDI32_RANGEINFO _IOR(CIO, 8, struct comedi32_rangeinfo_struct)
30 /* N.B. COMEDI32_CMD and COMEDI_CMD ought to use _IOWR, not _IOR.
31 * It's too late to change it now, but it only affects the command number. */
32 #define COMEDI32_CMD _IOR(CIO, 9, struct comedi32_cmd_struct)
33 /* N.B. COMEDI32_CMDTEST and COMEDI_CMDTEST ought to use _IOWR, not _IOR.
34 * It's too late to change it now, but it only affects the command number. */
35 #define COMEDI32_CMDTEST _IOR(CIO, 10, struct comedi32_cmd_struct)
36 #define COMEDI32_INSNLIST _IOR(CIO, 11, struct comedi32_insnlist_struct)
37 #define COMEDI32_INSN _IOR(CIO, 12, struct comedi32_insn_struct)
39 struct comedi32_chaninfo_struct {
41 compat_uptr_t maxdata_list; /* 32-bit 'unsigned int *' */
42 compat_uptr_t flaglist; /* 32-bit 'unsigned int *' */
43 compat_uptr_t rangelist; /* 32-bit 'unsigned int *' */
44 unsigned int unused[4];
47 struct comedi32_rangeinfo_struct {
48 unsigned int range_type;
49 compat_uptr_t range_ptr; /* 32-bit 'void *' */
52 struct comedi32_cmd_struct {
55 unsigned int start_src;
56 unsigned int start_arg;
57 unsigned int scan_begin_src;
58 unsigned int scan_begin_arg;
59 unsigned int convert_src;
60 unsigned int convert_arg;
61 unsigned int scan_end_src;
62 unsigned int scan_end_arg;
63 unsigned int stop_src;
64 unsigned int stop_arg;
65 compat_uptr_t chanlist; /* 32-bit 'unsigned int *' */
66 unsigned int chanlist_len;
67 compat_uptr_t data; /* 32-bit 'short *' */
68 unsigned int data_len;
71 struct comedi32_insn_struct {
74 compat_uptr_t data; /* 32-bit 'unsigned int *' */
76 unsigned int chanspec;
77 unsigned int unused[3];
80 struct comedi32_insnlist_struct {
82 compat_uptr_t insns; /* 32-bit 'struct comedi_insn *' */
85 /* Handle translated ioctl. */
86 static int translated_ioctl(struct file *file, unsigned int cmd,
89 if (file->f_op->unlocked_ioctl)
90 return file->f_op->unlocked_ioctl(file, cmd, arg);
95 /* Handle 32-bit COMEDI_CHANINFO ioctl. */
96 static int compat_chaninfo(struct file *file, unsigned long arg)
98 struct comedi_chaninfo __user *chaninfo;
99 struct comedi32_chaninfo_struct __user *chaninfo32;
106 chaninfo32 = compat_ptr(arg);
107 chaninfo = compat_alloc_user_space(sizeof(*chaninfo));
109 /* Copy chaninfo structure. Ignore unused members. */
110 if (!access_ok(VERIFY_READ, chaninfo32, sizeof(*chaninfo32)) ||
111 !access_ok(VERIFY_WRITE, chaninfo, sizeof(*chaninfo)))
115 err |= __get_user(temp.uint, &chaninfo32->subdev);
116 err |= __put_user(temp.uint, &chaninfo->subdev);
117 err |= __get_user(temp.uptr, &chaninfo32->maxdata_list);
118 err |= __put_user(compat_ptr(temp.uptr), &chaninfo->maxdata_list);
119 err |= __get_user(temp.uptr, &chaninfo32->flaglist);
120 err |= __put_user(compat_ptr(temp.uptr), &chaninfo->flaglist);
121 err |= __get_user(temp.uptr, &chaninfo32->rangelist);
122 err |= __put_user(compat_ptr(temp.uptr), &chaninfo->rangelist);
126 return translated_ioctl(file, COMEDI_CHANINFO, (unsigned long)chaninfo);
129 /* Handle 32-bit COMEDI_RANGEINFO ioctl. */
130 static int compat_rangeinfo(struct file *file, unsigned long arg)
132 struct comedi_rangeinfo __user *rangeinfo;
133 struct comedi32_rangeinfo_struct __user *rangeinfo32;
140 rangeinfo32 = compat_ptr(arg);
141 rangeinfo = compat_alloc_user_space(sizeof(*rangeinfo));
143 /* Copy rangeinfo structure. */
144 if (!access_ok(VERIFY_READ, rangeinfo32, sizeof(*rangeinfo32)) ||
145 !access_ok(VERIFY_WRITE, rangeinfo, sizeof(*rangeinfo)))
149 err |= __get_user(temp.uint, &rangeinfo32->range_type);
150 err |= __put_user(temp.uint, &rangeinfo->range_type);
151 err |= __get_user(temp.uptr, &rangeinfo32->range_ptr);
152 err |= __put_user(compat_ptr(temp.uptr), &rangeinfo->range_ptr);
156 return translated_ioctl(file, COMEDI_RANGEINFO,
157 (unsigned long)rangeinfo);
160 /* Copy 32-bit cmd structure to native cmd structure. */
161 static int get_compat_cmd(struct comedi_cmd __user *cmd,
162 struct comedi32_cmd_struct __user *cmd32)
170 /* Copy cmd structure. */
171 if (!access_ok(VERIFY_READ, cmd32, sizeof(*cmd32)) ||
172 !access_ok(VERIFY_WRITE, cmd, sizeof(*cmd)))
176 err |= __get_user(temp.uint, &cmd32->subdev);
177 err |= __put_user(temp.uint, &cmd->subdev);
178 err |= __get_user(temp.uint, &cmd32->flags);
179 err |= __put_user(temp.uint, &cmd->flags);
180 err |= __get_user(temp.uint, &cmd32->start_src);
181 err |= __put_user(temp.uint, &cmd->start_src);
182 err |= __get_user(temp.uint, &cmd32->start_arg);
183 err |= __put_user(temp.uint, &cmd->start_arg);
184 err |= __get_user(temp.uint, &cmd32->scan_begin_src);
185 err |= __put_user(temp.uint, &cmd->scan_begin_src);
186 err |= __get_user(temp.uint, &cmd32->scan_begin_arg);
187 err |= __put_user(temp.uint, &cmd->scan_begin_arg);
188 err |= __get_user(temp.uint, &cmd32->convert_src);
189 err |= __put_user(temp.uint, &cmd->convert_src);
190 err |= __get_user(temp.uint, &cmd32->convert_arg);
191 err |= __put_user(temp.uint, &cmd->convert_arg);
192 err |= __get_user(temp.uint, &cmd32->scan_end_src);
193 err |= __put_user(temp.uint, &cmd->scan_end_src);
194 err |= __get_user(temp.uint, &cmd32->scan_end_arg);
195 err |= __put_user(temp.uint, &cmd->scan_end_arg);
196 err |= __get_user(temp.uint, &cmd32->stop_src);
197 err |= __put_user(temp.uint, &cmd->stop_src);
198 err |= __get_user(temp.uint, &cmd32->stop_arg);
199 err |= __put_user(temp.uint, &cmd->stop_arg);
200 err |= __get_user(temp.uptr, &cmd32->chanlist);
201 err |= __put_user(compat_ptr(temp.uptr), &cmd->chanlist);
202 err |= __get_user(temp.uint, &cmd32->chanlist_len);
203 err |= __put_user(temp.uint, &cmd->chanlist_len);
204 err |= __get_user(temp.uptr, &cmd32->data);
205 err |= __put_user(compat_ptr(temp.uptr), &cmd->data);
206 err |= __get_user(temp.uint, &cmd32->data_len);
207 err |= __put_user(temp.uint, &cmd->data_len);
208 return err ? -EFAULT : 0;
211 /* Copy native cmd structure to 32-bit cmd structure. */
212 static int put_compat_cmd(struct comedi32_cmd_struct __user *cmd32,
213 struct comedi_cmd __user *cmd)
218 /* Copy back most of cmd structure. */
219 /* Assume the pointer values are already valid. */
220 /* (Could use ptr_to_compat() to set them, but that wasn't implemented
221 * until kernel version 2.6.11.) */
222 if (!access_ok(VERIFY_READ, cmd, sizeof(*cmd)) ||
223 !access_ok(VERIFY_WRITE, cmd32, sizeof(*cmd32)))
227 err |= __get_user(temp, &cmd->subdev);
228 err |= __put_user(temp, &cmd32->subdev);
229 err |= __get_user(temp, &cmd->flags);
230 err |= __put_user(temp, &cmd32->flags);
231 err |= __get_user(temp, &cmd->start_src);
232 err |= __put_user(temp, &cmd32->start_src);
233 err |= __get_user(temp, &cmd->start_arg);
234 err |= __put_user(temp, &cmd32->start_arg);
235 err |= __get_user(temp, &cmd->scan_begin_src);
236 err |= __put_user(temp, &cmd32->scan_begin_src);
237 err |= __get_user(temp, &cmd->scan_begin_arg);
238 err |= __put_user(temp, &cmd32->scan_begin_arg);
239 err |= __get_user(temp, &cmd->convert_src);
240 err |= __put_user(temp, &cmd32->convert_src);
241 err |= __get_user(temp, &cmd->convert_arg);
242 err |= __put_user(temp, &cmd32->convert_arg);
243 err |= __get_user(temp, &cmd->scan_end_src);
244 err |= __put_user(temp, &cmd32->scan_end_src);
245 err |= __get_user(temp, &cmd->scan_end_arg);
246 err |= __put_user(temp, &cmd32->scan_end_arg);
247 err |= __get_user(temp, &cmd->stop_src);
248 err |= __put_user(temp, &cmd32->stop_src);
249 err |= __get_user(temp, &cmd->stop_arg);
250 err |= __put_user(temp, &cmd32->stop_arg);
251 /* Assume chanlist pointer is unchanged. */
252 err |= __get_user(temp, &cmd->chanlist_len);
253 err |= __put_user(temp, &cmd32->chanlist_len);
254 /* Assume data pointer is unchanged. */
255 err |= __get_user(temp, &cmd->data_len);
256 err |= __put_user(temp, &cmd32->data_len);
257 return err ? -EFAULT : 0;
260 /* Handle 32-bit COMEDI_CMD ioctl. */
261 static int compat_cmd(struct file *file, unsigned long arg)
263 struct comedi_cmd __user *cmd;
264 struct comedi32_cmd_struct __user *cmd32;
267 cmd32 = compat_ptr(arg);
268 cmd = compat_alloc_user_space(sizeof(*cmd));
270 rc = get_compat_cmd(cmd, cmd32);
274 return translated_ioctl(file, COMEDI_CMD, (unsigned long)cmd);
277 /* Handle 32-bit COMEDI_CMDTEST ioctl. */
278 static int compat_cmdtest(struct file *file, unsigned long arg)
280 struct comedi_cmd __user *cmd;
281 struct comedi32_cmd_struct __user *cmd32;
284 cmd32 = compat_ptr(arg);
285 cmd = compat_alloc_user_space(sizeof(*cmd));
287 rc = get_compat_cmd(cmd, cmd32);
291 rc = translated_ioctl(file, COMEDI_CMDTEST, (unsigned long)cmd);
295 err = put_compat_cmd(cmd32, cmd);
302 /* Copy 32-bit insn structure to native insn structure. */
303 static int get_compat_insn(struct comedi_insn __user *insn,
304 struct comedi32_insn_struct __user *insn32)
312 /* Copy insn structure. Ignore the unused members. */
314 if (!access_ok(VERIFY_READ, insn32, sizeof(*insn32)) ||
315 !access_ok(VERIFY_WRITE, insn, sizeof(*insn)))
318 err |= __get_user(temp.uint, &insn32->insn);
319 err |= __put_user(temp.uint, &insn->insn);
320 err |= __get_user(temp.uint, &insn32->n);
321 err |= __put_user(temp.uint, &insn->n);
322 err |= __get_user(temp.uptr, &insn32->data);
323 err |= __put_user(compat_ptr(temp.uptr), &insn->data);
324 err |= __get_user(temp.uint, &insn32->subdev);
325 err |= __put_user(temp.uint, &insn->subdev);
326 err |= __get_user(temp.uint, &insn32->chanspec);
327 err |= __put_user(temp.uint, &insn->chanspec);
328 return err ? -EFAULT : 0;
331 /* Handle 32-bit COMEDI_INSNLIST ioctl. */
332 static int compat_insnlist(struct file *file, unsigned long arg)
334 struct combined_insnlist {
335 struct comedi_insnlist insnlist;
336 struct comedi_insn insn[1];
338 struct comedi32_insnlist_struct __user *insnlist32;
339 struct comedi32_insn_struct __user *insn32;
341 unsigned int n_insns, n;
344 insnlist32 = compat_ptr(arg);
346 /* Get 32-bit insnlist structure. */
347 if (!access_ok(VERIFY_READ, insnlist32, sizeof(*insnlist32)))
351 err |= __get_user(n_insns, &insnlist32->n_insns);
352 err |= __get_user(uptr, &insnlist32->insns);
353 insn32 = compat_ptr(uptr);
357 /* Allocate user memory to copy insnlist and insns into. */
358 s = compat_alloc_user_space(offsetof(struct combined_insnlist,
361 /* Set native insnlist structure. */
362 if (!access_ok(VERIFY_WRITE, &s->insnlist, sizeof(s->insnlist)))
365 err |= __put_user(n_insns, &s->insnlist.n_insns);
366 err |= __put_user(&s->insn[0], &s->insnlist.insns);
370 /* Copy insn structures. */
371 for (n = 0; n < n_insns; n++) {
372 rc = get_compat_insn(&s->insn[n], &insn32[n]);
377 return translated_ioctl(file, COMEDI_INSNLIST,
378 (unsigned long)&s->insnlist);
381 /* Handle 32-bit COMEDI_INSN ioctl. */
382 static int compat_insn(struct file *file, unsigned long arg)
384 struct comedi_insn __user *insn;
385 struct comedi32_insn_struct __user *insn32;
388 insn32 = compat_ptr(arg);
389 insn = compat_alloc_user_space(sizeof(*insn));
391 rc = get_compat_insn(insn, insn32);
395 return translated_ioctl(file, COMEDI_INSN, (unsigned long)insn);
398 /* Process untranslated ioctl. */
399 /* Returns -ENOIOCTLCMD for unrecognised ioctl codes. */
400 static inline int raw_ioctl(struct file *file, unsigned int cmd,
406 case COMEDI_DEVCONFIG:
408 case COMEDI_SUBDINFO:
409 case COMEDI_BUFCONFIG:
411 /* Just need to translate the pointer argument. */
412 arg = (unsigned long)compat_ptr(arg);
413 rc = translated_ioctl(file, cmd, arg);
419 case COMEDI_SETRSUBD:
420 case COMEDI_SETWSUBD:
421 /* No translation needed. */
422 rc = translated_ioctl(file, cmd, arg);
424 case COMEDI32_CHANINFO:
425 rc = compat_chaninfo(file, arg);
427 case COMEDI32_RANGEINFO:
428 rc = compat_rangeinfo(file, arg);
431 rc = compat_cmd(file, arg);
433 case COMEDI32_CMDTEST:
434 rc = compat_cmdtest(file, arg);
436 case COMEDI32_INSNLIST:
437 rc = compat_insnlist(file, arg);
440 rc = compat_insn(file, arg);
449 /* compat_ioctl file operation. */
450 /* Returns -ENOIOCTLCMD for unrecognised ioctl codes. */
451 long comedi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
453 return raw_ioctl(file, cmd, arg);