ff.h 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * ff.h - a part of driver for RME Fireface series
 *
 * Copyright (c) 2015-2017 Takashi Sakamoto
 *
 * Licensed under the terms of the GNU General Public License, version 2.
 */

#ifndef SOUND_FIREFACE_H_INCLUDED
#define SOUND_FIREFACE_H_INCLUDED

#include <linux/device.h>
#include <linux/firewire.h>
#include <linux/firewire-constants.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/compat.h>
20
#include <linux/sched/signal.h>
21 22

#include <sound/core.h>
23
#include <sound/info.h>
24
#include <sound/rawmidi.h>
25 26
#include <sound/pcm.h>
#include <sound/pcm_params.h>
27 28
#include <sound/hwdep.h>
#include <sound/firewire.h>
29

30
#include "../lib.h"
31
#include "../amdtp-stream.h"
32
#include "../iso-resources.h"
33

34 35 36 37
#define SND_FF_MAXIMIM_MIDI_QUADS	9
#define SND_FF_IN_MIDI_PORTS		2
#define SND_FF_OUT_MIDI_PORTS		2

38 39 40 41 42 43 44
enum snd_ff_stream_mode {
	SND_FF_STREAM_MODE_LOW = 0,
	SND_FF_STREAM_MODE_MID,
	SND_FF_STREAM_MODE_HIGH,
	SND_FF_STREAM_MODE_COUNT,
};

45
struct snd_ff_protocol;
46 47 48
struct snd_ff_spec {
	const char *const name;

49 50
	const unsigned int pcm_capture_channels[SND_FF_STREAM_MODE_COUNT];
	const unsigned int pcm_playback_channels[SND_FF_STREAM_MODE_COUNT];
51 52 53

	unsigned int midi_in_ports;
	unsigned int midi_out_ports;
54

55
	const struct snd_ff_protocol *protocol;
56
	u64 midi_high_addr;
57
	u8 midi_addr_range;
58
	u64 midi_rx_addrs[SND_FF_OUT_MIDI_PORTS];
59 60
};

61 62 63 64
struct snd_ff {
	struct snd_card *card;
	struct fw_unit *unit;
	struct mutex mutex;
65
	spinlock_t lock;
66 67 68

	bool registered;
	struct delayed_work dwork;
69 70

	const struct snd_ff_spec *spec;
71 72 73 74 75 76 77

	/* To handle MIDI tx. */
	struct snd_rawmidi_substream *tx_midi_substreams[SND_FF_IN_MIDI_PORTS];
	struct fw_address_handler async_handler;

	/* TO handle MIDI rx. */
	struct snd_rawmidi_substream *rx_midi_substreams[SND_FF_OUT_MIDI_PORTS];
78
	bool on_sysex[SND_FF_OUT_MIDI_PORTS];
79 80 81 82 83 84
	__le32 msg_buf[SND_FF_OUT_MIDI_PORTS][SND_FF_MAXIMIM_MIDI_QUADS];
	struct work_struct rx_midi_work[SND_FF_OUT_MIDI_PORTS];
	struct fw_transaction transactions[SND_FF_OUT_MIDI_PORTS];
	ktime_t next_ktime[SND_FF_OUT_MIDI_PORTS];
	bool rx_midi_error[SND_FF_OUT_MIDI_PORTS];
	unsigned int rx_bytes[SND_FF_OUT_MIDI_PORTS];
85 86 87 88 89 90

	unsigned int substreams_counter;
	struct amdtp_stream tx_stream;
	struct amdtp_stream rx_stream;
	struct fw_iso_resources tx_resources;
	struct fw_iso_resources rx_resources;
91 92 93 94

	int dev_lock_count;
	bool dev_lock_changed;
	wait_queue_head_t hwdep_wait;
95
};
96 97 98 99

enum snd_ff_clock_src {
	SND_FF_CLOCK_SRC_INTERNAL,
	SND_FF_CLOCK_SRC_SPDIF,
100 101
	SND_FF_CLOCK_SRC_ADAT1,
	SND_FF_CLOCK_SRC_ADAT2,
102 103
	SND_FF_CLOCK_SRC_WORD,
	SND_FF_CLOCK_SRC_LTC,
104
	/* TODO: perhaps TCO exists. */
105 106 107
};

struct snd_ff_protocol {
108 109
	void (*handle_midi_msg)(struct snd_ff *ff, unsigned int offset,
				__le32 *buf, size_t length);
110 111 112
	int (*fill_midi_msg)(struct snd_ff *ff,
			     struct snd_rawmidi_substream *substream,
			     unsigned int port);
113 114
	int (*get_clock)(struct snd_ff *ff, unsigned int *rate,
			 enum snd_ff_clock_src *src);
115
	int (*switch_fetching_mode)(struct snd_ff *ff, bool enable);
116
	int (*allocate_resources)(struct snd_ff *ff, unsigned int rate);
117 118
	int (*begin_session)(struct snd_ff *ff, unsigned int rate);
	void (*finish_session)(struct snd_ff *ff);
119
	void (*dump_status)(struct snd_ff *ff, struct snd_info_buffer *buffer);
120 121
};

122
extern const struct snd_ff_protocol snd_ff_protocol_ff800;
123
extern const struct snd_ff_protocol snd_ff_protocol_ff400;
124
extern const struct snd_ff_protocol snd_ff_protocol_latter;
125

126 127 128 129
int snd_ff_transaction_register(struct snd_ff *ff);
int snd_ff_transaction_reregister(struct snd_ff *ff);
void snd_ff_transaction_unregister(struct snd_ff *ff);

130 131 132 133 134 135 136
int amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate,
			    unsigned int pcm_channels);
int amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s,
				    struct snd_pcm_runtime *runtime);
int amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit,
		  enum amdtp_stream_direction dir);

137 138
int snd_ff_stream_get_multiplier_mode(enum cip_sfc sfc,
				      enum snd_ff_stream_mode *mode);
139 140
int snd_ff_stream_init_duplex(struct snd_ff *ff);
void snd_ff_stream_destroy_duplex(struct snd_ff *ff);
141 142
int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate);
void snd_ff_stream_release_duplex(struct snd_ff *ff);
143 144 145 146
int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate);
void snd_ff_stream_stop_duplex(struct snd_ff *ff);
void snd_ff_stream_update_duplex(struct snd_ff *ff);

147 148 149 150
void snd_ff_stream_lock_changed(struct snd_ff *ff);
int snd_ff_stream_lock_try(struct snd_ff *ff);
void snd_ff_stream_lock_release(struct snd_ff *ff);

151
void snd_ff_proc_init(struct snd_ff *ff);
152
const char *snd_ff_proc_get_clk_label(enum snd_ff_clock_src src);
153

154 155
int snd_ff_create_midi_devices(struct snd_ff *ff);

156 157
int snd_ff_create_pcm_devices(struct snd_ff *ff);

158 159
int snd_ff_create_hwdep_devices(struct snd_ff *ff);

160
#endif