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 (put_user(status.tstamp.tv_sec, &src->tstamp.tv_sec) ||
  83. put_user(status.tstamp.tv_nsec, &src->tstamp.tv_nsec) ||
  84. put_user(status.avail, &src->avail) ||
  85. put_user(status.xruns, &src->xruns))
  86. return -EFAULT;
  87. return 0;
  88. }
  89. #ifdef CONFIG_X86_X32
  90. /* X32 ABI has 64bit timespec and 64bit alignment */
  91. struct snd_rawmidi_status_x32 {
  92. s32 stream;
  93. u32 rsvd; /* alignment */
  94. struct timespec tstamp;
  95. u32 avail;
  96. u32 xruns;
  97. unsigned char reserved[16];
  98. } __attribute__((packed));
  99. #define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst))
  100. static int snd_rawmidi_ioctl_status_x32(struct snd_rawmidi_file *rfile,
  101. struct snd_rawmidi_status_x32 __user *src)
  102. {
  103. int err;
  104. struct snd_rawmidi_status status;
  105. if (get_user(status.stream, &src->stream))
  106. return -EFAULT;
  107. switch (status.stream) {
  108. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  109. if (!rfile->output)
  110. return -EINVAL;
  111. err = snd_rawmidi_output_status(rfile->output, &status);
  112. break;
  113. case SNDRV_RAWMIDI_STREAM_INPUT:
  114. if (!rfile->input)
  115. return -EINVAL;
  116. err = snd_rawmidi_input_status(rfile->input, &status);
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. if (err < 0)
  122. return err;
  123. if (put_timespec(&status.tstamp, &src->tstamp) ||
  124. put_user(status.avail, &src->avail) ||
  125. put_user(status.xruns, &src->xruns))
  126. return -EFAULT;
  127. return 0;
  128. }
  129. #endif /* CONFIG_X86_X32 */
  130. enum {
  131. SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
  132. SNDRV_RAWMIDI_IOCTL_STATUS32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
  133. #ifdef CONFIG_X86_X32
  134. SNDRV_RAWMIDI_IOCTL_STATUS_X32 = _IOWR('W', 0x20, struct snd_rawmidi_status_x32),
  135. #endif /* CONFIG_X86_X32 */
  136. };
  137. static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  138. {
  139. struct snd_rawmidi_file *rfile;
  140. void __user *argp = compat_ptr(arg);
  141. rfile = file->private_data;
  142. switch (cmd) {
  143. case SNDRV_RAWMIDI_IOCTL_PVERSION:
  144. case SNDRV_RAWMIDI_IOCTL_INFO:
  145. case SNDRV_RAWMIDI_IOCTL_DROP:
  146. case SNDRV_RAWMIDI_IOCTL_DRAIN:
  147. return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
  148. case SNDRV_RAWMIDI_IOCTL_PARAMS32:
  149. return snd_rawmidi_ioctl_params_compat(rfile, argp);
  150. case SNDRV_RAWMIDI_IOCTL_STATUS32:
  151. return snd_rawmidi_ioctl_status_compat(rfile, argp);
  152. #ifdef CONFIG_X86_X32
  153. case SNDRV_RAWMIDI_IOCTL_STATUS_X32:
  154. return snd_rawmidi_ioctl_status_x32(rfile, argp);
  155. #endif /* CONFIG_X86_X32 */
  156. }
  157. return -ENOIOCTLCMD;
  158. }