rawmidi_compat.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 (rfile->output == NULL)
  35. return -EINVAL;
  36. if (get_user(params.stream, &src->stream) ||
  37. get_user(params.buffer_size, &src->buffer_size) ||
  38. get_user(params.avail_min, &src->avail_min) ||
  39. get_user(val, &src->no_active_sensing))
  40. return -EFAULT;
  41. params.no_active_sensing = val;
  42. switch (params.stream) {
  43. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  44. return snd_rawmidi_output_params(rfile->output, &params);
  45. case SNDRV_RAWMIDI_STREAM_INPUT:
  46. return snd_rawmidi_input_params(rfile->input, &params);
  47. }
  48. return -EINVAL;
  49. }
  50. struct snd_rawmidi_status32 {
  51. s32 stream;
  52. struct compat_timespec tstamp;
  53. u32 avail;
  54. u32 xruns;
  55. unsigned char reserved[16];
  56. } __attribute__((packed));
  57. static int snd_rawmidi_ioctl_status_compat(struct snd_rawmidi_file *rfile,
  58. struct snd_rawmidi_status32 __user *src)
  59. {
  60. int err;
  61. struct snd_rawmidi_status status;
  62. if (rfile->output == NULL)
  63. return -EINVAL;
  64. if (get_user(status.stream, &src->stream))
  65. return -EFAULT;
  66. switch (status.stream) {
  67. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  68. err = snd_rawmidi_output_status(rfile->output, &status);
  69. break;
  70. case SNDRV_RAWMIDI_STREAM_INPUT:
  71. err = snd_rawmidi_input_status(rfile->input, &status);
  72. break;
  73. default:
  74. return -EINVAL;
  75. }
  76. if (err < 0)
  77. return err;
  78. if (compat_put_timespec(&status.tstamp, &src->tstamp) ||
  79. put_user(status.avail, &src->avail) ||
  80. put_user(status.xruns, &src->xruns))
  81. return -EFAULT;
  82. return 0;
  83. }
  84. #ifdef CONFIG_X86_X32
  85. /* X32 ABI has 64bit timespec and 64bit alignment */
  86. struct snd_rawmidi_status_x32 {
  87. s32 stream;
  88. u32 rsvd; /* alignment */
  89. struct timespec tstamp;
  90. u32 avail;
  91. u32 xruns;
  92. unsigned char reserved[16];
  93. } __attribute__((packed));
  94. #define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst))
  95. static int snd_rawmidi_ioctl_status_x32(struct snd_rawmidi_file *rfile,
  96. struct snd_rawmidi_status_x32 __user *src)
  97. {
  98. int err;
  99. struct snd_rawmidi_status status;
  100. if (rfile->output == NULL)
  101. return -EINVAL;
  102. if (get_user(status.stream, &src->stream))
  103. return -EFAULT;
  104. switch (status.stream) {
  105. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  106. err = snd_rawmidi_output_status(rfile->output, &status);
  107. break;
  108. case SNDRV_RAWMIDI_STREAM_INPUT:
  109. err = snd_rawmidi_input_status(rfile->input, &status);
  110. break;
  111. default:
  112. return -EINVAL;
  113. }
  114. if (err < 0)
  115. return err;
  116. if (put_timespec(&status.tstamp, &src->tstamp) ||
  117. put_user(status.avail, &src->avail) ||
  118. put_user(status.xruns, &src->xruns))
  119. return -EFAULT;
  120. return 0;
  121. }
  122. #endif /* CONFIG_X86_X32 */
  123. enum {
  124. SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
  125. SNDRV_RAWMIDI_IOCTL_STATUS32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
  126. #ifdef CONFIG_X86_X32
  127. SNDRV_RAWMIDI_IOCTL_STATUS_X32 = _IOWR('W', 0x20, struct snd_rawmidi_status_x32),
  128. #endif /* CONFIG_X86_X32 */
  129. };
  130. static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  131. {
  132. struct snd_rawmidi_file *rfile;
  133. void __user *argp = compat_ptr(arg);
  134. rfile = file->private_data;
  135. switch (cmd) {
  136. case SNDRV_RAWMIDI_IOCTL_PVERSION:
  137. case SNDRV_RAWMIDI_IOCTL_INFO:
  138. case SNDRV_RAWMIDI_IOCTL_DROP:
  139. case SNDRV_RAWMIDI_IOCTL_DRAIN:
  140. return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
  141. case SNDRV_RAWMIDI_IOCTL_PARAMS32:
  142. return snd_rawmidi_ioctl_params_compat(rfile, argp);
  143. case SNDRV_RAWMIDI_IOCTL_STATUS32:
  144. return snd_rawmidi_ioctl_status_compat(rfile, argp);
  145. #ifdef CONFIG_X86_X32
  146. case SNDRV_RAWMIDI_IOCTL_STATUS_X32:
  147. return snd_rawmidi_ioctl_status_x32(rfile, argp);
  148. #endif /* CONFIG_X86_X32 */
  149. }
  150. return -ENOIOCTLCMD;
  151. }