162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci#ifndef __SOUND_RAWMIDI_H
362306a36Sopenharmony_ci#define __SOUND_RAWMIDI_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci/*
662306a36Sopenharmony_ci *  Abstract layer for MIDI v1.0 stream
762306a36Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <sound/asound.h>
1162306a36Sopenharmony_ci#include <linux/interrupt.h>
1262306a36Sopenharmony_ci#include <linux/spinlock.h>
1362306a36Sopenharmony_ci#include <linux/wait.h>
1462306a36Sopenharmony_ci#include <linux/mutex.h>
1562306a36Sopenharmony_ci#include <linux/workqueue.h>
1662306a36Sopenharmony_ci#include <linux/device.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_SND_SEQUENCER)
1962306a36Sopenharmony_ci#include <sound/seq_device.h>
2062306a36Sopenharmony_ci#endif
2162306a36Sopenharmony_ci#include <sound/info.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/*
2462306a36Sopenharmony_ci *  Raw MIDI interface
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define SNDRV_RAWMIDI_DEVICES		8
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define SNDRV_RAWMIDI_LFLG_OUTPUT	(1<<0)
3062306a36Sopenharmony_ci#define SNDRV_RAWMIDI_LFLG_INPUT	(1<<1)
3162306a36Sopenharmony_ci#define SNDRV_RAWMIDI_LFLG_OPEN		(3<<0)
3262306a36Sopenharmony_ci#define SNDRV_RAWMIDI_LFLG_APPEND	(1<<2)
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistruct snd_rawmidi;
3562306a36Sopenharmony_cistruct snd_rawmidi_substream;
3662306a36Sopenharmony_cistruct snd_seq_port_info;
3762306a36Sopenharmony_cistruct pid;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistruct snd_rawmidi_ops {
4062306a36Sopenharmony_ci	int (*open) (struct snd_rawmidi_substream * substream);
4162306a36Sopenharmony_ci	int (*close) (struct snd_rawmidi_substream * substream);
4262306a36Sopenharmony_ci	void (*trigger) (struct snd_rawmidi_substream * substream, int up);
4362306a36Sopenharmony_ci	void (*drain) (struct snd_rawmidi_substream * substream);
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistruct snd_rawmidi_global_ops {
4762306a36Sopenharmony_ci	int (*dev_register) (struct snd_rawmidi * rmidi);
4862306a36Sopenharmony_ci	int (*dev_unregister) (struct snd_rawmidi * rmidi);
4962306a36Sopenharmony_ci	void (*get_port_info)(struct snd_rawmidi *rmidi, int number,
5062306a36Sopenharmony_ci			      struct snd_seq_port_info *info);
5162306a36Sopenharmony_ci	long (*ioctl)(struct snd_rawmidi *rmidi, unsigned int cmd,
5262306a36Sopenharmony_ci		      void __user *argp);
5362306a36Sopenharmony_ci	void (*proc_read)(struct snd_info_entry *entry,
5462306a36Sopenharmony_ci			  struct snd_info_buffer *buf);
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistruct snd_rawmidi_runtime {
5862306a36Sopenharmony_ci	struct snd_rawmidi_substream *substream;
5962306a36Sopenharmony_ci	unsigned int drain: 1,	/* drain stage */
6062306a36Sopenharmony_ci		     oss: 1;	/* OSS compatible mode */
6162306a36Sopenharmony_ci	/* midi stream buffer */
6262306a36Sopenharmony_ci	unsigned char *buffer;	/* buffer for MIDI data */
6362306a36Sopenharmony_ci	size_t buffer_size;	/* size of buffer */
6462306a36Sopenharmony_ci	size_t appl_ptr;	/* application pointer */
6562306a36Sopenharmony_ci	size_t hw_ptr;		/* hardware pointer */
6662306a36Sopenharmony_ci	size_t avail_min;	/* min avail for wakeup */
6762306a36Sopenharmony_ci	size_t avail;		/* max used buffer for wakeup */
6862306a36Sopenharmony_ci	size_t xruns;		/* over/underruns counter */
6962306a36Sopenharmony_ci	size_t align;		/* alignment (0 = byte stream, 3 = UMP) */
7062306a36Sopenharmony_ci	int buffer_ref;		/* buffer reference count */
7162306a36Sopenharmony_ci	/* misc */
7262306a36Sopenharmony_ci	wait_queue_head_t sleep;
7362306a36Sopenharmony_ci	/* event handler (new bytes, input only) */
7462306a36Sopenharmony_ci	void (*event)(struct snd_rawmidi_substream *substream);
7562306a36Sopenharmony_ci	/* defers calls to event [input] or ops->trigger [output] */
7662306a36Sopenharmony_ci	struct work_struct event_work;
7762306a36Sopenharmony_ci	/* private data */
7862306a36Sopenharmony_ci	void *private_data;
7962306a36Sopenharmony_ci	void (*private_free)(struct snd_rawmidi_substream *substream);
8062306a36Sopenharmony_ci};
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistruct snd_rawmidi_substream {
8362306a36Sopenharmony_ci	struct list_head list;		/* list of all substream for given stream */
8462306a36Sopenharmony_ci	int stream;			/* direction */
8562306a36Sopenharmony_ci	int number;			/* substream number */
8662306a36Sopenharmony_ci	bool opened;			/* open flag */
8762306a36Sopenharmony_ci	bool append;			/* append flag (merge more streams) */
8862306a36Sopenharmony_ci	bool active_sensing;		/* send active sensing when close */
8962306a36Sopenharmony_ci	unsigned int framing;		/* whether to frame input data */
9062306a36Sopenharmony_ci	unsigned int clock_type;	/* clock source to use for input framing */
9162306a36Sopenharmony_ci	int use_count;			/* use counter (for output) */
9262306a36Sopenharmony_ci	size_t bytes;
9362306a36Sopenharmony_ci	spinlock_t lock;
9462306a36Sopenharmony_ci	struct snd_rawmidi *rmidi;
9562306a36Sopenharmony_ci	struct snd_rawmidi_str *pstr;
9662306a36Sopenharmony_ci	char name[32];
9762306a36Sopenharmony_ci	struct snd_rawmidi_runtime *runtime;
9862306a36Sopenharmony_ci	struct pid *pid;
9962306a36Sopenharmony_ci	/* hardware layer */
10062306a36Sopenharmony_ci	const struct snd_rawmidi_ops *ops;
10162306a36Sopenharmony_ci};
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistruct snd_rawmidi_file {
10462306a36Sopenharmony_ci	struct snd_rawmidi *rmidi;
10562306a36Sopenharmony_ci	struct snd_rawmidi_substream *input;
10662306a36Sopenharmony_ci	struct snd_rawmidi_substream *output;
10762306a36Sopenharmony_ci	unsigned int user_pversion;	/* supported protocol version */
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistruct snd_rawmidi_str {
11162306a36Sopenharmony_ci	unsigned int substream_count;
11262306a36Sopenharmony_ci	unsigned int substream_opened;
11362306a36Sopenharmony_ci	struct list_head substreams;
11462306a36Sopenharmony_ci};
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistruct snd_rawmidi {
11762306a36Sopenharmony_ci	struct snd_card *card;
11862306a36Sopenharmony_ci	struct list_head list;
11962306a36Sopenharmony_ci	unsigned int device;		/* device number */
12062306a36Sopenharmony_ci	unsigned int info_flags;	/* SNDRV_RAWMIDI_INFO_XXXX */
12162306a36Sopenharmony_ci	char id[64];
12262306a36Sopenharmony_ci	char name[80];
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci#ifdef CONFIG_SND_OSSEMUL
12562306a36Sopenharmony_ci	int ossreg;
12662306a36Sopenharmony_ci#endif
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	const struct snd_rawmidi_global_ops *ops;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	struct snd_rawmidi_str streams[2];
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	void *private_data;
13362306a36Sopenharmony_ci	void (*private_free) (struct snd_rawmidi *rmidi);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	struct mutex open_mutex;
13662306a36Sopenharmony_ci	wait_queue_head_t open_wait;
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	struct device *dev;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	struct snd_info_entry *proc_entry;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_SND_SEQUENCER)
14362306a36Sopenharmony_ci	struct snd_seq_device *seq_dev;
14462306a36Sopenharmony_ci#endif
14562306a36Sopenharmony_ci};
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci/* main rawmidi functions */
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ciint snd_rawmidi_new(struct snd_card *card, char *id, int device,
15062306a36Sopenharmony_ci		    int output_count, int input_count,
15162306a36Sopenharmony_ci		    struct snd_rawmidi **rmidi);
15262306a36Sopenharmony_civoid snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
15362306a36Sopenharmony_ci			 const struct snd_rawmidi_ops *ops);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci/* internal */
15662306a36Sopenharmony_ciint snd_rawmidi_init(struct snd_rawmidi *rmidi,
15762306a36Sopenharmony_ci		     struct snd_card *card, char *id, int device,
15862306a36Sopenharmony_ci		     int output_count, int input_count,
15962306a36Sopenharmony_ci		     unsigned int info_flags);
16062306a36Sopenharmony_ciint snd_rawmidi_free(struct snd_rawmidi *rmidi);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci/* callbacks */
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ciint snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
16562306a36Sopenharmony_ci			const unsigned char *buffer, int count);
16662306a36Sopenharmony_ciint snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream);
16762306a36Sopenharmony_ciint snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
16862306a36Sopenharmony_ci			      unsigned char *buffer, int count);
16962306a36Sopenharmony_ciint snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count);
17062306a36Sopenharmony_ciint snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
17162306a36Sopenharmony_ci			 unsigned char *buffer, int count);
17262306a36Sopenharmony_ciint snd_rawmidi_proceed(struct snd_rawmidi_substream *substream);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci/* main midi functions */
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ciint snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info);
17762306a36Sopenharmony_ciint snd_rawmidi_kernel_open(struct snd_rawmidi *rmidi, int subdevice,
17862306a36Sopenharmony_ci			    int mode, struct snd_rawmidi_file *rfile);
17962306a36Sopenharmony_ciint snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile);
18062306a36Sopenharmony_ciint snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
18162306a36Sopenharmony_ci			      struct snd_rawmidi_params *params);
18262306a36Sopenharmony_ciint snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
18362306a36Sopenharmony_ci			     struct snd_rawmidi_params *params);
18462306a36Sopenharmony_ciint snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream);
18562306a36Sopenharmony_ciint snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream);
18662306a36Sopenharmony_ciint snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream);
18762306a36Sopenharmony_cilong snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
18862306a36Sopenharmony_ci			     unsigned char *buf, long count);
18962306a36Sopenharmony_cilong snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
19062306a36Sopenharmony_ci			      const unsigned char *buf, long count);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci#endif /* __SOUND_RAWMIDI_H */
193