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.
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.
27 #define __NO_VERSION__
29 #include <linux/uaccess.h>
31 #include "comedi_compat32.h"
35 #define COMEDI32_CHANINFO _IOR(CIO, 3, struct comedi32_chaninfo_struct)
36 #define COMEDI32_RANGEINFO _IOR(CIO, 8, struct comedi32_rangeinfo_struct)
37 /* N.B. COMEDI32_CMD and COMEDI_CMD ought to use _IOWR, not _IOR.
38 * It's too late to change it now, but it only affects the command number. */
39 #define COMEDI32_CMD _IOR(CIO, 9, struct comedi32_cmd_struct)
40 /* N.B. COMEDI32_CMDTEST and COMEDI_CMDTEST ought to use _IOWR, not _IOR.
41 * It's too late to change it now, but it only affects the command number. */
42 #define COMEDI32_CMDTEST _IOR(CIO, 10, struct comedi32_cmd_struct)
43 #define COMEDI32_INSNLIST _IOR(CIO, 11, struct comedi32_insnlist_struct)
44 #define COMEDI32_INSN _IOR(CIO, 12, struct comedi32_insn_struct)
46 struct comedi32_chaninfo_struct {
48 compat_uptr_t maxdata_list; /* 32-bit 'unsigned int *' */
49 compat_uptr_t flaglist; /* 32-bit 'unsigned int *' */
50 compat_uptr_t rangelist; /* 32-bit 'unsigned int *' */
51 unsigned int unused[4];
54 struct comedi32_rangeinfo_struct {
55 unsigned int range_type;
56 compat_uptr_t range_ptr; /* 32-bit 'void *' */
59 struct comedi32_cmd_struct {
62 unsigned int start_src;
63 unsigned int start_arg;
64 unsigned int scan_begin_src;
65 unsigned int scan_begin_arg;
66 unsigned int convert_src;
67 unsigned int convert_arg;
68 unsigned int scan_end_src;
69 unsigned int scan_end_arg;
70 unsigned int stop_src;
71 unsigned int stop_arg;
72 compat_uptr_t chanlist; /* 32-bit 'unsigned int *' */
73 unsigned int chanlist_len;
74 compat_uptr_t data; /* 32-bit 'short *' */
75 unsigned int data_len;
78 struct comedi32_insn_struct {
81 compat_uptr_t data; /* 32-bit 'unsigned int *' */
83 unsigned int chanspec;
84 unsigned int unused[3];
87 struct comedi32_insnlist_struct {
89 compat_uptr_t insns; /* 32-bit 'struct comedi_insn *' */
92 /* Handle translated ioctl. */
93 static int translated_ioctl(struct file *file, unsigned int cmd,
99 if (file->f_op->unlocked_ioctl)
100 return file->f_op->unlocked_ioctl(file, cmd, arg);
105 /* Handle 32-bit COMEDI_CHANINFO ioctl. */
106 static int compat_chaninfo(struct file *file, unsigned long arg)
108 struct comedi_chaninfo __user *chaninfo;
109 struct comedi32_chaninfo_struct __user *chaninfo32;
116 chaninfo32 = compat_ptr(arg);
117 chaninfo = compat_alloc_user_space(sizeof(*chaninfo));
119 /* Copy chaninfo structure. Ignore unused members. */
120 if (!access_ok(VERIFY_READ, chaninfo32, sizeof(*chaninfo32))
121 || !access_ok(VERIFY_WRITE, chaninfo, sizeof(*chaninfo))) {
125 err |= __get_user(temp.uint, &chaninfo32->subdev);
126 err |= __put_user(temp.uint, &chaninfo->subdev);
127 err |= __get_user(temp.uptr, &chaninfo32->maxdata_list);
128 err |= __put_user(compat_ptr(temp.uptr), &chaninfo->maxdata_list);
129 err |= __get_user(temp.uptr, &chaninfo32->flaglist);
130 err |= __put_user(compat_ptr(temp.uptr), &chaninfo->flaglist);
131 err |= __get_user(temp.uptr, &chaninfo32->rangelist);
132 err |= __put_user(compat_ptr(temp.uptr), &chaninfo->rangelist);
136 return translated_ioctl(file, COMEDI_CHANINFO, (unsigned long)chaninfo);
139 /* Handle 32-bit COMEDI_RANGEINFO ioctl. */
140 static int compat_rangeinfo(struct file *file, unsigned long arg)
142 struct comedi_rangeinfo __user *rangeinfo;
143 struct comedi32_rangeinfo_struct __user *rangeinfo32;
150 rangeinfo32 = compat_ptr(arg);
151 rangeinfo = compat_alloc_user_space(sizeof(*rangeinfo));
153 /* Copy rangeinfo structure. */
154 if (!access_ok(VERIFY_READ, rangeinfo32, sizeof(*rangeinfo32))
155 || !access_ok(VERIFY_WRITE, rangeinfo, sizeof(*rangeinfo))) {
159 err |= __get_user(temp.uint, &rangeinfo32->range_type);
160 err |= __put_user(temp.uint, &rangeinfo->range_type);
161 err |= __get_user(temp.uptr, &rangeinfo32->range_ptr);
162 err |= __put_user(compat_ptr(temp.uptr), &rangeinfo->range_ptr);
166 return translated_ioctl(file, COMEDI_RANGEINFO,
167 (unsigned long)rangeinfo);
170 /* Copy 32-bit cmd structure to native cmd structure. */
171 static int get_compat_cmd(struct comedi_cmd __user *cmd,
172 struct comedi32_cmd_struct __user *cmd32)
180 /* Copy cmd structure. */
181 if (!access_ok(VERIFY_READ, cmd32, sizeof(*cmd32))
182 || !access_ok(VERIFY_WRITE, cmd, sizeof(*cmd))) {
186 err |= __get_user(temp.uint, &cmd32->subdev);
187 err |= __put_user(temp.uint, &cmd->subdev);
188 err |= __get_user(temp.uint, &cmd32->flags);
189 err |= __put_user(temp.uint, &cmd->flags);
190 err |= __get_user(temp.uint, &cmd32->start_src);
191 err |= __put_user(temp.uint, &cmd->start_src);
192 err |= __get_user(temp.uint, &cmd32->start_arg);
193 err |= __put_user(temp.uint, &cmd->start_arg);
194 err |= __get_user(temp.uint, &cmd32->scan_begin_src);
195 err |= __put_user(temp.uint, &cmd->scan_begin_src);
196 err |= __get_user(temp.uint, &cmd32->scan_begin_arg);
197 err |= __put_user(temp.uint, &cmd->scan_begin_arg);
198 err |= __get_user(temp.uint, &cmd32->convert_src);
199 err |= __put_user(temp.uint, &cmd->convert_src);
200 err |= __get_user(temp.uint, &cmd32->convert_arg);
201 err |= __put_user(temp.uint, &cmd->convert_arg);
202 err |= __get_user(temp.uint, &cmd32->scan_end_src);
203 err |= __put_user(temp.uint, &cmd->scan_end_src);
204 err |= __get_user(temp.uint, &cmd32->scan_end_arg);
205 err |= __put_user(temp.uint, &cmd->scan_end_arg);
206 err |= __get_user(temp.uint, &cmd32->stop_src);
207 err |= __put_user(temp.uint, &cmd->stop_src);
208 err |= __get_user(temp.uint, &cmd32->stop_arg);
209 err |= __put_user(temp.uint, &cmd->stop_arg);
210 err |= __get_user(temp.uptr, &cmd32->chanlist);
211 err |= __put_user(compat_ptr(temp.uptr), &cmd->chanlist);
212 err |= __get_user(temp.uint, &cmd32->chanlist_len);
213 err |= __put_user(temp.uint, &cmd->chanlist_len);
214 err |= __get_user(temp.uptr, &cmd32->data);
215 err |= __put_user(compat_ptr(temp.uptr), &cmd->data);
216 err |= __get_user(temp.uint, &cmd32->data_len);
217 err |= __put_user(temp.uint, &cmd->data_len);
218 return err ? -EFAULT : 0;
221 /* Copy native cmd structure to 32-bit cmd structure. */
222 static int put_compat_cmd(struct comedi32_cmd_struct __user *cmd32,
223 struct comedi_cmd __user *cmd)
228 /* Copy back most of cmd structure. */
229 /* Assume the pointer values are already valid. */
230 /* (Could use ptr_to_compat() to set them, but that wasn't implemented
231 * until kernel version 2.6.11.) */
232 if (!access_ok(VERIFY_READ, cmd, sizeof(*cmd))
233 || !access_ok(VERIFY_WRITE, cmd32, sizeof(*cmd32))) {
237 err |= __get_user(temp, &cmd->subdev);
238 err |= __put_user(temp, &cmd32->subdev);
239 err |= __get_user(temp, &cmd->flags);
240 err |= __put_user(temp, &cmd32->flags);
241 err |= __get_user(temp, &cmd->start_src);
242 err |= __put_user(temp, &cmd32->start_src);
243 err |= __get_user(temp, &cmd->start_arg);
244 err |= __put_user(temp, &cmd32->start_arg);
245 err |= __get_user(temp, &cmd->scan_begin_src);
246 err |= __put_user(temp, &cmd32->scan_begin_src);
247 err |= __get_user(temp, &cmd->scan_begin_arg);
248 err |= __put_user(temp, &cmd32->scan_begin_arg);
249 err |= __get_user(temp, &cmd->convert_src);
250 err |= __put_user(temp, &cmd32->convert_src);
251 err |= __get_user(temp, &cmd->convert_arg);
252 err |= __put_user(temp, &cmd32->convert_arg);
253 err |= __get_user(temp, &cmd->scan_end_src);
254 err |= __put_user(temp, &cmd32->scan_end_src);
255 err |= __get_user(temp, &cmd->scan_end_arg);
256 err |= __put_user(temp, &cmd32->scan_end_arg);
257 err |= __get_user(temp, &cmd->stop_src);
258 err |= __put_user(temp, &cmd32->stop_src);
259 err |= __get_user(temp, &cmd->stop_arg);
260 err |= __put_user(temp, &cmd32->stop_arg);
261 /* Assume chanlist pointer is unchanged. */
262 err |= __get_user(temp, &cmd->chanlist_len);
263 err |= __put_user(temp, &cmd32->chanlist_len);
264 /* Assume data pointer is unchanged. */
265 err |= __get_user(temp, &cmd->data_len);
266 err |= __put_user(temp, &cmd32->data_len);
267 return err ? -EFAULT : 0;
270 /* Handle 32-bit COMEDI_CMD ioctl. */
271 static int compat_cmd(struct file *file, unsigned long arg)
273 struct comedi_cmd __user *cmd;
274 struct comedi32_cmd_struct __user *cmd32;
277 cmd32 = compat_ptr(arg);
278 cmd = compat_alloc_user_space(sizeof(*cmd));
280 rc = get_compat_cmd(cmd, cmd32);
284 return translated_ioctl(file, COMEDI_CMD, (unsigned long)cmd);
287 /* Handle 32-bit COMEDI_CMDTEST ioctl. */
288 static int compat_cmdtest(struct file *file, unsigned long arg)
290 struct comedi_cmd __user *cmd;
291 struct comedi32_cmd_struct __user *cmd32;
294 cmd32 = compat_ptr(arg);
295 cmd = compat_alloc_user_space(sizeof(*cmd));
297 rc = get_compat_cmd(cmd, cmd32);
301 rc = translated_ioctl(file, COMEDI_CMDTEST, (unsigned long)cmd);
305 err = put_compat_cmd(cmd32, cmd);
312 /* Copy 32-bit insn structure to native insn structure. */
313 static int get_compat_insn(struct comedi_insn __user *insn,
314 struct comedi32_insn_struct __user *insn32)
322 /* Copy insn structure. Ignore the unused members. */
324 if (!access_ok(VERIFY_READ, insn32, sizeof(*insn32))
325 || !access_ok(VERIFY_WRITE, insn, sizeof(*insn)))
328 err |= __get_user(temp.uint, &insn32->insn);
329 err |= __put_user(temp.uint, &insn->insn);
330 err |= __get_user(temp.uint, &insn32->n);
331 err |= __put_user(temp.uint, &insn->n);
332 err |= __get_user(temp.uptr, &insn32->data);
333 err |= __put_user(compat_ptr(temp.uptr), &insn->data);
334 err |= __get_user(temp.uint, &insn32->subdev);
335 err |= __put_user(temp.uint, &insn->subdev);
336 err |= __get_user(temp.uint, &insn32->chanspec);
337 err |= __put_user(temp.uint, &insn->chanspec);
338 return err ? -EFAULT : 0;
341 /* Handle 32-bit COMEDI_INSNLIST ioctl. */
342 static int compat_insnlist(struct file *file, unsigned long arg)
344 struct combined_insnlist {
345 struct comedi_insnlist insnlist;
346 struct comedi_insn insn[1];
348 struct comedi32_insnlist_struct __user *insnlist32;
349 struct comedi32_insn_struct __user *insn32;
351 unsigned int n_insns, n;
354 insnlist32 = compat_ptr(arg);
356 /* Get 32-bit insnlist structure. */
357 if (!access_ok(VERIFY_READ, insnlist32, sizeof(*insnlist32)))
361 err |= __get_user(n_insns, &insnlist32->n_insns);
362 err |= __get_user(uptr, &insnlist32->insns);
363 insn32 = compat_ptr(uptr);
367 /* Allocate user memory to copy insnlist and insns into. */
368 s = compat_alloc_user_space(offsetof(struct combined_insnlist,
371 /* Set native insnlist structure. */
372 if (!access_ok(VERIFY_WRITE, &s->insnlist, sizeof(s->insnlist)))
375 err |= __put_user(n_insns, &s->insnlist.n_insns);
376 err |= __put_user(&s->insn[0], &s->insnlist.insns);
380 /* Copy insn structures. */
381 for (n = 0; n < n_insns; n++) {
382 rc = get_compat_insn(&s->insn[n], &insn32[n]);
387 return translated_ioctl(file, COMEDI_INSNLIST,
388 (unsigned long)&s->insnlist);
391 /* Handle 32-bit COMEDI_INSN ioctl. */
392 static int compat_insn(struct file *file, unsigned long arg)
394 struct comedi_insn __user *insn;
395 struct comedi32_insn_struct __user *insn32;
398 insn32 = compat_ptr(arg);
399 insn = compat_alloc_user_space(sizeof(*insn));
401 rc = get_compat_insn(insn, insn32);
405 return translated_ioctl(file, COMEDI_INSN, (unsigned long)insn);
408 /* Process untranslated ioctl. */
409 /* Returns -ENOIOCTLCMD for unrecognised ioctl codes. */
410 static inline int raw_ioctl(struct file *file, unsigned int cmd,
416 case COMEDI_DEVCONFIG:
418 case COMEDI_SUBDINFO:
419 case COMEDI_BUFCONFIG:
421 /* Just need to translate the pointer argument. */
422 arg = (unsigned long)compat_ptr(arg);
423 rc = translated_ioctl(file, cmd, arg);
429 /* No translation needed. */
430 rc = translated_ioctl(file, cmd, arg);
432 case COMEDI32_CHANINFO:
433 rc = compat_chaninfo(file, arg);
435 case COMEDI32_RANGEINFO:
436 rc = compat_rangeinfo(file, arg);
439 rc = compat_cmd(file, arg);
441 case COMEDI32_CMDTEST:
442 rc = compat_cmdtest(file, arg);
444 case COMEDI32_INSNLIST:
445 rc = compat_insnlist(file, arg);
448 rc = compat_insn(file, arg);
457 /* compat_ioctl file operation. */
458 /* Returns -ENOIOCTLCMD for unrecognised ioctl codes. */
459 long comedi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
461 return raw_ioctl(file, cmd, arg);
464 #endif /* CONFIG_COMPAT */