162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * ff.h - a part of driver for RME Fireface series
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2015-2017 Takashi Sakamoto
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef SOUND_FIREFACE_H_INCLUDED
962306a36Sopenharmony_ci#define SOUND_FIREFACE_H_INCLUDED
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/device.h>
1262306a36Sopenharmony_ci#include <linux/firewire.h>
1362306a36Sopenharmony_ci#include <linux/firewire-constants.h>
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
1662306a36Sopenharmony_ci#include <linux/mutex.h>
1762306a36Sopenharmony_ci#include <linux/slab.h>
1862306a36Sopenharmony_ci#include <linux/compat.h>
1962306a36Sopenharmony_ci#include <linux/sched/signal.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#include <sound/core.h>
2262306a36Sopenharmony_ci#include <sound/info.h>
2362306a36Sopenharmony_ci#include <sound/rawmidi.h>
2462306a36Sopenharmony_ci#include <sound/pcm.h>
2562306a36Sopenharmony_ci#include <sound/pcm_params.h>
2662306a36Sopenharmony_ci#include <sound/hwdep.h>
2762306a36Sopenharmony_ci#include <sound/firewire.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include "../lib.h"
3062306a36Sopenharmony_ci#include "../amdtp-stream.h"
3162306a36Sopenharmony_ci#include "../iso-resources.h"
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define SND_FF_MAXIMIM_MIDI_QUADS	9
3462306a36Sopenharmony_ci#define SND_FF_IN_MIDI_PORTS		2
3562306a36Sopenharmony_ci#define SND_FF_OUT_MIDI_PORTS		2
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cienum snd_ff_unit_version {
3862306a36Sopenharmony_ci	SND_FF_UNIT_VERSION_FF800	= 0x000001,
3962306a36Sopenharmony_ci	SND_FF_UNIT_VERSION_FF400	= 0x000002,
4062306a36Sopenharmony_ci	SND_FF_UNIT_VERSION_UFX		= 0x000003,
4162306a36Sopenharmony_ci	SND_FF_UNIT_VERSION_UCX		= 0x000004,
4262306a36Sopenharmony_ci	SND_FF_UNIT_VERSION_802		= 0x000005,
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cienum snd_ff_stream_mode {
4662306a36Sopenharmony_ci	SND_FF_STREAM_MODE_LOW = 0,
4762306a36Sopenharmony_ci	SND_FF_STREAM_MODE_MID,
4862306a36Sopenharmony_ci	SND_FF_STREAM_MODE_HIGH,
4962306a36Sopenharmony_ci	SND_FF_STREAM_MODE_COUNT,
5062306a36Sopenharmony_ci};
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistruct snd_ff_protocol;
5362306a36Sopenharmony_cistruct snd_ff_spec {
5462306a36Sopenharmony_ci	const unsigned int pcm_capture_channels[SND_FF_STREAM_MODE_COUNT];
5562306a36Sopenharmony_ci	const unsigned int pcm_playback_channels[SND_FF_STREAM_MODE_COUNT];
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	unsigned int midi_in_ports;
5862306a36Sopenharmony_ci	unsigned int midi_out_ports;
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	const struct snd_ff_protocol *protocol;
6162306a36Sopenharmony_ci	u64 midi_high_addr;
6262306a36Sopenharmony_ci	u8 midi_addr_range;
6362306a36Sopenharmony_ci	u64 midi_rx_addrs[SND_FF_OUT_MIDI_PORTS];
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistruct snd_ff {
6762306a36Sopenharmony_ci	struct snd_card *card;
6862306a36Sopenharmony_ci	struct fw_unit *unit;
6962306a36Sopenharmony_ci	struct mutex mutex;
7062306a36Sopenharmony_ci	spinlock_t lock;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	enum snd_ff_unit_version unit_version;
7362306a36Sopenharmony_ci	const struct snd_ff_spec *spec;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	/* To handle MIDI tx. */
7662306a36Sopenharmony_ci	struct snd_rawmidi_substream *tx_midi_substreams[SND_FF_IN_MIDI_PORTS];
7762306a36Sopenharmony_ci	struct fw_address_handler async_handler;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/* TO handle MIDI rx. */
8062306a36Sopenharmony_ci	struct snd_rawmidi_substream *rx_midi_substreams[SND_FF_OUT_MIDI_PORTS];
8162306a36Sopenharmony_ci	bool on_sysex[SND_FF_OUT_MIDI_PORTS];
8262306a36Sopenharmony_ci	__le32 msg_buf[SND_FF_OUT_MIDI_PORTS][SND_FF_MAXIMIM_MIDI_QUADS];
8362306a36Sopenharmony_ci	struct work_struct rx_midi_work[SND_FF_OUT_MIDI_PORTS];
8462306a36Sopenharmony_ci	struct fw_transaction transactions[SND_FF_OUT_MIDI_PORTS];
8562306a36Sopenharmony_ci	ktime_t next_ktime[SND_FF_OUT_MIDI_PORTS];
8662306a36Sopenharmony_ci	bool rx_midi_error[SND_FF_OUT_MIDI_PORTS];
8762306a36Sopenharmony_ci	unsigned int rx_bytes[SND_FF_OUT_MIDI_PORTS];
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	unsigned int substreams_counter;
9062306a36Sopenharmony_ci	struct amdtp_stream tx_stream;
9162306a36Sopenharmony_ci	struct amdtp_stream rx_stream;
9262306a36Sopenharmony_ci	struct fw_iso_resources tx_resources;
9362306a36Sopenharmony_ci	struct fw_iso_resources rx_resources;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	int dev_lock_count;
9662306a36Sopenharmony_ci	bool dev_lock_changed;
9762306a36Sopenharmony_ci	wait_queue_head_t hwdep_wait;
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	struct amdtp_domain domain;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	void *msg_parser;
10262306a36Sopenharmony_ci};
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cienum snd_ff_clock_src {
10562306a36Sopenharmony_ci	SND_FF_CLOCK_SRC_INTERNAL,
10662306a36Sopenharmony_ci	SND_FF_CLOCK_SRC_SPDIF,
10762306a36Sopenharmony_ci	SND_FF_CLOCK_SRC_ADAT1,
10862306a36Sopenharmony_ci	SND_FF_CLOCK_SRC_ADAT2,
10962306a36Sopenharmony_ci	SND_FF_CLOCK_SRC_WORD,
11062306a36Sopenharmony_ci	SND_FF_CLOCK_SRC_LTC,
11162306a36Sopenharmony_ci	/* TODO: perhaps TCO exists. */
11262306a36Sopenharmony_ci};
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistruct snd_ff_protocol {
11562306a36Sopenharmony_ci	size_t msg_parser_size;
11662306a36Sopenharmony_ci	bool (*has_msg)(struct snd_ff *ff);
11762306a36Sopenharmony_ci	long (*copy_msg_to_user)(struct snd_ff *ff, char __user *buf, long count);
11862306a36Sopenharmony_ci	void (*handle_msg)(struct snd_ff *ff, unsigned int offset, const __le32 *buf,
11962306a36Sopenharmony_ci			   size_t length, u32 tstamp);
12062306a36Sopenharmony_ci	int (*fill_midi_msg)(struct snd_ff *ff,
12162306a36Sopenharmony_ci			     struct snd_rawmidi_substream *substream,
12262306a36Sopenharmony_ci			     unsigned int port);
12362306a36Sopenharmony_ci	int (*get_clock)(struct snd_ff *ff, unsigned int *rate,
12462306a36Sopenharmony_ci			 enum snd_ff_clock_src *src);
12562306a36Sopenharmony_ci	int (*switch_fetching_mode)(struct snd_ff *ff, bool enable);
12662306a36Sopenharmony_ci	int (*allocate_resources)(struct snd_ff *ff, unsigned int rate);
12762306a36Sopenharmony_ci	int (*begin_session)(struct snd_ff *ff, unsigned int rate);
12862306a36Sopenharmony_ci	void (*finish_session)(struct snd_ff *ff);
12962306a36Sopenharmony_ci	void (*dump_status)(struct snd_ff *ff, struct snd_info_buffer *buffer);
13062306a36Sopenharmony_ci};
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciextern const struct snd_ff_protocol snd_ff_protocol_ff800;
13362306a36Sopenharmony_ciextern const struct snd_ff_protocol snd_ff_protocol_ff400;
13462306a36Sopenharmony_ciextern const struct snd_ff_protocol snd_ff_protocol_latter;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ciint snd_ff_transaction_register(struct snd_ff *ff);
13762306a36Sopenharmony_ciint snd_ff_transaction_reregister(struct snd_ff *ff);
13862306a36Sopenharmony_civoid snd_ff_transaction_unregister(struct snd_ff *ff);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ciint amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate,
14162306a36Sopenharmony_ci			    unsigned int pcm_channels);
14262306a36Sopenharmony_ciint amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s,
14362306a36Sopenharmony_ci				    struct snd_pcm_runtime *runtime);
14462306a36Sopenharmony_ciint amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit,
14562306a36Sopenharmony_ci		  enum amdtp_stream_direction dir);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciint snd_ff_stream_get_multiplier_mode(enum cip_sfc sfc,
14862306a36Sopenharmony_ci				      enum snd_ff_stream_mode *mode);
14962306a36Sopenharmony_ciint snd_ff_stream_init_duplex(struct snd_ff *ff);
15062306a36Sopenharmony_civoid snd_ff_stream_destroy_duplex(struct snd_ff *ff);
15162306a36Sopenharmony_ciint snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate,
15262306a36Sopenharmony_ci				 unsigned int frames_per_period,
15362306a36Sopenharmony_ci				 unsigned int frames_per_buffer);
15462306a36Sopenharmony_ciint snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate);
15562306a36Sopenharmony_civoid snd_ff_stream_stop_duplex(struct snd_ff *ff);
15662306a36Sopenharmony_civoid snd_ff_stream_update_duplex(struct snd_ff *ff);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_civoid snd_ff_stream_lock_changed(struct snd_ff *ff);
15962306a36Sopenharmony_ciint snd_ff_stream_lock_try(struct snd_ff *ff);
16062306a36Sopenharmony_civoid snd_ff_stream_lock_release(struct snd_ff *ff);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_civoid snd_ff_proc_init(struct snd_ff *ff);
16362306a36Sopenharmony_ciconst char *snd_ff_proc_get_clk_label(enum snd_ff_clock_src src);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciint snd_ff_create_midi_devices(struct snd_ff *ff);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciint snd_ff_create_pcm_devices(struct snd_ff *ff);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ciint snd_ff_create_hwdep_devices(struct snd_ff *ff);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci#endif
172