blob: 68a93443583c2ef6a8b32b079f04f93bee697e41 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * 32bit -> 64bit ioctl wrapper for raw MIDI API
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
7/* This file included from rawmidi.c */
8
9#include <linux/compat.h>
10
Takashi Iwai48c9d412005-11-17 13:56:51 +010011struct snd_rawmidi_params32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 s32 stream;
13 u32 buffer_size;
14 u32 avail_min;
15 unsigned int no_active_sensing; /* avoid bit-field */
David Henningsson08fdced2021-05-15 09:15:33 +020016 unsigned int mode;
17 unsigned char reserved[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -070018} __attribute__((packed));
19
Takashi Iwai48c9d412005-11-17 13:56:51 +010020static int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile,
21 struct snd_rawmidi_params32 __user *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Takashi Iwai48c9d412005-11-17 13:56:51 +010023 struct snd_rawmidi_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 unsigned int val;
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 if (get_user(params.stream, &src->stream) ||
27 get_user(params.buffer_size, &src->buffer_size) ||
28 get_user(params.avail_min, &src->avail_min) ||
David Henningsson08fdced2021-05-15 09:15:33 +020029 get_user(params.mode, &src->mode) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 get_user(val, &src->no_active_sensing))
31 return -EFAULT;
32 params.no_active_sensing = val;
33 switch (params.stream) {
34 case SNDRV_RAWMIDI_STREAM_OUTPUT:
Takashi Iwai8a56ef42018-04-19 18:16:15 +020035 if (!rfile->output)
36 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return snd_rawmidi_output_params(rfile->output, &params);
38 case SNDRV_RAWMIDI_STREAM_INPUT:
Takashi Iwai8a56ef42018-04-19 18:16:15 +020039 if (!rfile->input)
40 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return snd_rawmidi_input_params(rfile->input, &params);
42 }
43 return -EINVAL;
44}
45
Baolin Wangd9e55822018-04-24 20:06:12 +080046struct compat_snd_rawmidi_status64 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 s32 stream;
Baolin Wangd9e55822018-04-24 20:06:12 +080048 u8 rsvd[4]; /* alignment */
49 s64 tstamp_sec;
50 s64 tstamp_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 u32 avail;
52 u32 xruns;
53 unsigned char reserved[16];
54} __attribute__((packed));
55
Baolin Wangd9e55822018-04-24 20:06:12 +080056static int snd_rawmidi_ioctl_status_compat64(struct snd_rawmidi_file *rfile,
57 struct compat_snd_rawmidi_status64 __user *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 int err;
Baolin Wangd9e55822018-04-24 20:06:12 +080060 struct snd_rawmidi_status64 status;
61 struct compat_snd_rawmidi_status64 compat_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (get_user(status.stream, &src->stream))
64 return -EFAULT;
65
66 switch (status.stream) {
67 case SNDRV_RAWMIDI_STREAM_OUTPUT:
Takashi Iwai8a56ef42018-04-19 18:16:15 +020068 if (!rfile->output)
69 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 err = snd_rawmidi_output_status(rfile->output, &status);
71 break;
72 case SNDRV_RAWMIDI_STREAM_INPUT:
Takashi Iwai8a56ef42018-04-19 18:16:15 +020073 if (!rfile->input)
74 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 err = snd_rawmidi_input_status(rfile->input, &status);
76 break;
77 default:
78 return -EINVAL;
79 }
80 if (err < 0)
81 return err;
82
Baolin Wangd9e55822018-04-24 20:06:12 +080083 compat_status = (struct compat_snd_rawmidi_status64) {
84 .stream = status.stream,
85 .tstamp_sec = status.tstamp_sec,
86 .tstamp_nsec = status.tstamp_nsec,
87 .avail = status.avail,
88 .xruns = status.xruns,
89 };
90
91 if (copy_to_user(src, &compat_status, sizeof(*src)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -EFAULT;
93
94 return 0;
95}
96
97enum {
Takashi Iwai48c9d412005-11-17 13:56:51 +010098 SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
Baolin Wangd9e55822018-04-24 20:06:12 +080099 SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
100 SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64 = _IOWR('W', 0x20, struct compat_snd_rawmidi_status64),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
104{
Takashi Iwai48c9d412005-11-17 13:56:51 +0100105 struct snd_rawmidi_file *rfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 void __user *argp = compat_ptr(arg);
107
108 rfile = file->private_data;
109 switch (cmd) {
110 case SNDRV_RAWMIDI_IOCTL_PVERSION:
111 case SNDRV_RAWMIDI_IOCTL_INFO:
112 case SNDRV_RAWMIDI_IOCTL_DROP:
113 case SNDRV_RAWMIDI_IOCTL_DRAIN:
114 return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
115 case SNDRV_RAWMIDI_IOCTL_PARAMS32:
116 return snd_rawmidi_ioctl_params_compat(rfile, argp);
Baolin Wangd9e55822018-04-24 20:06:12 +0800117 case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT32:
118 return snd_rawmidi_ioctl_status32(rfile, argp);
119 case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64:
120 return snd_rawmidi_ioctl_status_compat64(rfile, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 return -ENOIOCTLCMD;
123}