18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *   32bit -> 64bit ioctl wrapper for raw MIDI API
48c2ecf20Sopenharmony_ci *   Copyright (c) by Takashi Iwai <tiwai@suse.de>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/* This file included from rawmidi.c */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/compat.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistruct snd_rawmidi_params32 {
128c2ecf20Sopenharmony_ci	s32 stream;
138c2ecf20Sopenharmony_ci	u32 buffer_size;
148c2ecf20Sopenharmony_ci	u32 avail_min;
158c2ecf20Sopenharmony_ci	unsigned int no_active_sensing; /* avoid bit-field */
168c2ecf20Sopenharmony_ci	unsigned char reserved[16];
178c2ecf20Sopenharmony_ci} __attribute__((packed));
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile,
208c2ecf20Sopenharmony_ci					   struct snd_rawmidi_params32 __user *src)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct snd_rawmidi_params params;
238c2ecf20Sopenharmony_ci	unsigned int val;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	if (get_user(params.stream, &src->stream) ||
268c2ecf20Sopenharmony_ci	    get_user(params.buffer_size, &src->buffer_size) ||
278c2ecf20Sopenharmony_ci	    get_user(params.avail_min, &src->avail_min) ||
288c2ecf20Sopenharmony_ci	    get_user(val, &src->no_active_sensing))
298c2ecf20Sopenharmony_ci		return -EFAULT;
308c2ecf20Sopenharmony_ci	params.no_active_sensing = val;
318c2ecf20Sopenharmony_ci	switch (params.stream) {
328c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_STREAM_OUTPUT:
338c2ecf20Sopenharmony_ci		if (!rfile->output)
348c2ecf20Sopenharmony_ci			return -EINVAL;
358c2ecf20Sopenharmony_ci		return snd_rawmidi_output_params(rfile->output, &params);
368c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_STREAM_INPUT:
378c2ecf20Sopenharmony_ci		if (!rfile->input)
388c2ecf20Sopenharmony_ci			return -EINVAL;
398c2ecf20Sopenharmony_ci		return snd_rawmidi_input_params(rfile->input, &params);
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci	return -EINVAL;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistruct compat_snd_rawmidi_status64 {
458c2ecf20Sopenharmony_ci	s32 stream;
468c2ecf20Sopenharmony_ci	u8 rsvd[4]; /* alignment */
478c2ecf20Sopenharmony_ci	s64 tstamp_sec;
488c2ecf20Sopenharmony_ci	s64 tstamp_nsec;
498c2ecf20Sopenharmony_ci	u32 avail;
508c2ecf20Sopenharmony_ci	u32 xruns;
518c2ecf20Sopenharmony_ci	unsigned char reserved[16];
528c2ecf20Sopenharmony_ci} __attribute__((packed));
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int snd_rawmidi_ioctl_status_compat64(struct snd_rawmidi_file *rfile,
558c2ecf20Sopenharmony_ci					     struct compat_snd_rawmidi_status64 __user *src)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	int err;
588c2ecf20Sopenharmony_ci	struct snd_rawmidi_status64 status;
598c2ecf20Sopenharmony_ci	struct compat_snd_rawmidi_status64 compat_status;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	if (get_user(status.stream, &src->stream))
628c2ecf20Sopenharmony_ci		return -EFAULT;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	switch (status.stream) {
658c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_STREAM_OUTPUT:
668c2ecf20Sopenharmony_ci		if (!rfile->output)
678c2ecf20Sopenharmony_ci			return -EINVAL;
688c2ecf20Sopenharmony_ci		err = snd_rawmidi_output_status(rfile->output, &status);
698c2ecf20Sopenharmony_ci		break;
708c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_STREAM_INPUT:
718c2ecf20Sopenharmony_ci		if (!rfile->input)
728c2ecf20Sopenharmony_ci			return -EINVAL;
738c2ecf20Sopenharmony_ci		err = snd_rawmidi_input_status(rfile->input, &status);
748c2ecf20Sopenharmony_ci		break;
758c2ecf20Sopenharmony_ci	default:
768c2ecf20Sopenharmony_ci		return -EINVAL;
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci	if (err < 0)
798c2ecf20Sopenharmony_ci		return err;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	compat_status = (struct compat_snd_rawmidi_status64) {
828c2ecf20Sopenharmony_ci		.stream = status.stream,
838c2ecf20Sopenharmony_ci		.tstamp_sec = status.tstamp_sec,
848c2ecf20Sopenharmony_ci		.tstamp_nsec = status.tstamp_nsec,
858c2ecf20Sopenharmony_ci		.avail = status.avail,
868c2ecf20Sopenharmony_ci		.xruns = status.xruns,
878c2ecf20Sopenharmony_ci	};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (copy_to_user(src, &compat_status, sizeof(*src)))
908c2ecf20Sopenharmony_ci		return -EFAULT;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return 0;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cienum {
968c2ecf20Sopenharmony_ci	SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32),
978c2ecf20Sopenharmony_ci	SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT32 = _IOWR('W', 0x20, struct snd_rawmidi_status32),
988c2ecf20Sopenharmony_ci	SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64 = _IOWR('W', 0x20, struct compat_snd_rawmidi_status64),
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct snd_rawmidi_file *rfile;
1048c2ecf20Sopenharmony_ci	void __user *argp = compat_ptr(arg);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	rfile = file->private_data;
1078c2ecf20Sopenharmony_ci	switch (cmd) {
1088c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_IOCTL_PVERSION:
1098c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_IOCTL_INFO:
1108c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_IOCTL_DROP:
1118c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_IOCTL_DRAIN:
1128c2ecf20Sopenharmony_ci		return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp);
1138c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_IOCTL_PARAMS32:
1148c2ecf20Sopenharmony_ci		return snd_rawmidi_ioctl_params_compat(rfile, argp);
1158c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT32:
1168c2ecf20Sopenharmony_ci		return snd_rawmidi_ioctl_status32(rfile, argp);
1178c2ecf20Sopenharmony_ci	case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64:
1188c2ecf20Sopenharmony_ci		return snd_rawmidi_ioctl_status_compat64(rfile, argp);
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci	return -ENOIOCTLCMD;
1218c2ecf20Sopenharmony_ci}
122