rawmidi_compat.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * 32bit -> 64bit ioctl wrapper for raw MIDI API
  3. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /* This file included from rawmidi.c */
  21. #include <linux/compat.h>
  22. struct snd_rawmidi_params32 {
  23. s32 stream;
  24. u32 buffer_size;
  25. u32 avail_min;
  26. unsigned int no_active_sensing; /* avoid bit-field */
  27. unsigned char reserved[16];
  28. } __attribute__((packed));
  29. static int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile,
  30. struct snd_rawmidi_params32 __user *src)
  31. {
  32. struct snd_rawmidi_params params;
  33. unsigned int val;
  34. if (get_user(params.stream, &src->stream) ||
  35. get_user(params.buffer_size, &src->buffer_size) ||
  36. get_user(params.avail_min, &src->avail_min) ||
  37. get_user(val, &src->no_active_sensing))
  38. return -EFAULT;
  39. params.no_active_sensing = val;
  40. switch (params.stream) {
  41. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  42. if (!rfile->output)
  43. return -EINVAL;
  44. return snd_rawmidi_output_params(rfile->output, &params);
  45. case SNDRV_RAWMIDI_STREAM_INPUT:
  46. if (!rfile->input)
  47. return -EINVAL;
  48. return snd_rawmidi_input_params(rfile->input, &params);
  49. }
  50. return -EINVAL;
  51. }
  52. struct snd_rawmidi_status32 {
  53. s32 stream;
  54. struct compat_timespec tstamp;
  55. u32 avail;
  56. u32 xruns;
  57. unsigned char reserved[16];
  58. } __attribute__((packed));
  59. static int snd_rawmidi_ioctl_status_compat(struct snd_rawmidi_file *rfile,
  60. struct snd_rawmidi_status32 __user *src)
  61. {
  62. int err;
  63. struct snd_rawmidi_status status;
  64. if (get_user(status.stream, &src->stream))
  65. return -EFAULT;
  66. switch (status.stream) {
  67. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  68. if (!rfile->output)
  69. return -EINVAL;
  70. err = snd_rawmidi_output_status(rfile->output, &status);
  71. break;
  72. case SNDRV_RAWMIDI_STREAM_INPUT:
  73. if (!rfile->input)
  74. return -EINVAL;
  75. err = snd_rawmidi_input_status(rfile->input, &status);
  76. break;
  77. default:
  78. return -EINVAL;
  79. }
  80. if (err < 0)
  81. return err;
  82. if (compat_put_timespec(&status.tstamp, &src->tstamp) ||
  83. put_user(status.avail, &src->avail) ||
  84. put_user(status.xruns, &src->xruns))
  85. return -EFAULT;
  86. return 0;
  87. }
  88. #ifdef CONFIG_X86_X32
  89. /* X32 ABI has 64bit timespec and 64bit alignment */
  90. struct snd_rawmidi_status_x32 {
  91. s32 stream;
  92. u32 rsvd; /* alignment */
  93. struct timespec tstamp;
  94. u32 avail;
  95. u32 xruns;
  96. unsigned char reserved[16];
  97. } __attribute__((packed));
  98. #define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst))
  99. static int snd_rawmidi_ioctl_status_x32(struct snd_rawmidi_file *rfile,
  100. struct snd_rawmidi_status_x32 __user *src)
  101. {
  102. int err;
  103. struct snd_rawmidi_status status;
  104. if (get_user(status.stream, &src->stream))
  105. return -EFAULT;
  106. switch (status.stream) {
  107. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  108. if (!rfile->output)
  109. return -EINVAL;
  110. err = snd_rawmidi_output_status(rfile->output, &status);
  111. break;
  112. case SNDRV_RAWMIDI_STREAM_INPUT:
  113. if (!rfile->input)
  114. return -EINVAL;
  115. err = snd_rawmidi_input_status(rfile->input, &status);
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. if (err < 0)
  121. return err;
  122. if (put_timespec(&status.tstamp, &src->tstamp) ||
  123. put_user(status.avail, &src->avail) ||
  124. put_user(status.xruns, &src->xruns))
  125. return -EFAULT;
  126. return 0;
  127. }
  128. #endif /* CONFIG_X86_X32 */
  129. enum {
  130. SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
  131. SNDRV_RAWMIDI_IOCTL_STATUS32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
  132. #ifdef CONFIG_X86_X32
  133. SNDRV_RAWMIDI_IOCTL_STATUS_X32 = _IOWR('W', 0x20, struct snd_rawmidi_status_x32),
  134. #endif /* CONFIG_X86_X32 */
  135. };
  136. static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  137. {
  138. struct snd_rawmidi_file *rfile;
  139. void __user *argp = compat_ptr(arg);
  140. rfile = file->private_data;
  141. switch (cmd) {
  142. case SNDRV_RAWMIDI_IOCTL_PVERSION:
  143. case SNDRV_RAWMIDI_IOCTL_INFO:
  144. case SNDRV_RAWMIDI_IOCTL_DROP:
  145. case SNDRV_RAWMIDI_IOCTL_DRAIN:
  146. return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
  147. case SNDRV_RAWMIDI_IOCTL_PARAMS32:
  148. return snd_rawmidi_ioctl_params_compat(rfile, argp);
  149. case SNDRV_RAWMIDI_IOCTL_STATUS32:
  150. return snd_rawmidi_ioctl_status_compat(rfile, argp);
  151. #ifdef CONFIG_X86_X32
  152. case SNDRV_RAWMIDI_IOCTL_STATUS_X32:
  153. return snd_rawmidi_ioctl_status_x32(rfile, argp);
  154. #endif /* CONFIG_X86_X32 */
  155. }
  156. return -ENOIOCTLCMD;
  157. }