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