xref: /openbmc/linux/sound/firewire/fireface/ff.h (revision 750afb08)
1 /*
2  * ff.h - a part of driver for RME Fireface series
3  *
4  * Copyright (c) 2015-2017 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #ifndef SOUND_FIREFACE_H_INCLUDED
10 #define SOUND_FIREFACE_H_INCLUDED
11 
12 #include <linux/device.h>
13 #include <linux/firewire.h>
14 #include <linux/firewire-constants.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #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 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/hwdep.h>
28 #include <sound/firewire.h>
29 
30 #include "../lib.h"
31 #include "../amdtp-stream.h"
32 #include "../iso-resources.h"
33 
34 #define SND_FF_MAXIMIM_MIDI_QUADS	9
35 #define SND_FF_IN_MIDI_PORTS		2
36 #define SND_FF_OUT_MIDI_PORTS		2
37 
38 #define SND_FF_REG_SYNC_STATUS		0x0000801c0000ull
39 /* For block write request. */
40 #define SND_FF_REG_FETCH_PCM_FRAMES	0x0000801c0000ull
41 #define SND_FF_REG_CLOCK_CONFIG		0x0000801c0004ull
42 
43 enum snd_ff_stream_mode {
44 	SND_FF_STREAM_MODE_LOW = 0,
45 	SND_FF_STREAM_MODE_MID,
46 	SND_FF_STREAM_MODE_HIGH,
47 	SND_FF_STREAM_MODE_COUNT,
48 };
49 
50 struct snd_ff_protocol;
51 struct snd_ff_spec {
52 	const char *const name;
53 
54 	const unsigned int pcm_capture_channels[SND_FF_STREAM_MODE_COUNT];
55 	const unsigned int pcm_playback_channels[SND_FF_STREAM_MODE_COUNT];
56 
57 	unsigned int midi_in_ports;
58 	unsigned int midi_out_ports;
59 
60 	const struct snd_ff_protocol *protocol;
61 	u64 midi_high_addr;
62 };
63 
64 struct snd_ff {
65 	struct snd_card *card;
66 	struct fw_unit *unit;
67 	struct mutex mutex;
68 	spinlock_t lock;
69 
70 	bool registered;
71 	struct delayed_work dwork;
72 
73 	const struct snd_ff_spec *spec;
74 
75 	/* To handle MIDI tx. */
76 	struct snd_rawmidi_substream *tx_midi_substreams[SND_FF_IN_MIDI_PORTS];
77 	struct fw_address_handler async_handler;
78 
79 	/* TO handle MIDI rx. */
80 	struct snd_rawmidi_substream *rx_midi_substreams[SND_FF_OUT_MIDI_PORTS];
81 	u8 running_status[SND_FF_OUT_MIDI_PORTS];
82 	__le32 msg_buf[SND_FF_OUT_MIDI_PORTS][SND_FF_MAXIMIM_MIDI_QUADS];
83 	struct work_struct rx_midi_work[SND_FF_OUT_MIDI_PORTS];
84 	struct fw_transaction transactions[SND_FF_OUT_MIDI_PORTS];
85 	ktime_t next_ktime[SND_FF_OUT_MIDI_PORTS];
86 	bool rx_midi_error[SND_FF_OUT_MIDI_PORTS];
87 	unsigned int rx_bytes[SND_FF_OUT_MIDI_PORTS];
88 
89 	unsigned int substreams_counter;
90 	struct amdtp_stream tx_stream;
91 	struct amdtp_stream rx_stream;
92 	struct fw_iso_resources tx_resources;
93 	struct fw_iso_resources rx_resources;
94 
95 	int dev_lock_count;
96 	bool dev_lock_changed;
97 	wait_queue_head_t hwdep_wait;
98 };
99 
100 enum snd_ff_clock_src {
101 	SND_FF_CLOCK_SRC_INTERNAL,
102 	SND_FF_CLOCK_SRC_SPDIF,
103 	SND_FF_CLOCK_SRC_ADAT1,
104 	SND_FF_CLOCK_SRC_ADAT2,
105 	SND_FF_CLOCK_SRC_WORD,
106 	SND_FF_CLOCK_SRC_LTC,
107 	/* TODO: perhaps TCO exists. */
108 };
109 
110 struct snd_ff_protocol {
111 	void (*handle_midi_msg)(struct snd_ff *ff, __le32 *buf, size_t length);
112 	int (*begin_session)(struct snd_ff *ff, unsigned int rate);
113 	void (*finish_session)(struct snd_ff *ff);
114 };
115 
116 extern const struct snd_ff_protocol snd_ff_protocol_ff800;
117 extern const struct snd_ff_protocol snd_ff_protocol_ff400;
118 
119 int snd_ff_transaction_get_clock(struct snd_ff *ff, unsigned int *rate,
120 				 enum snd_ff_clock_src *src);
121 int snd_ff_transaction_register(struct snd_ff *ff);
122 int snd_ff_transaction_reregister(struct snd_ff *ff);
123 void snd_ff_transaction_unregister(struct snd_ff *ff);
124 
125 int amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate,
126 			    unsigned int pcm_channels);
127 int amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s,
128 				    struct snd_pcm_runtime *runtime);
129 int amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit,
130 		  enum amdtp_stream_direction dir);
131 
132 int snd_ff_stream_get_multiplier_mode(enum cip_sfc sfc,
133 				      enum snd_ff_stream_mode *mode);
134 int snd_ff_stream_init_duplex(struct snd_ff *ff);
135 void snd_ff_stream_destroy_duplex(struct snd_ff *ff);
136 int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate);
137 void snd_ff_stream_stop_duplex(struct snd_ff *ff);
138 void snd_ff_stream_update_duplex(struct snd_ff *ff);
139 
140 void snd_ff_stream_lock_changed(struct snd_ff *ff);
141 int snd_ff_stream_lock_try(struct snd_ff *ff);
142 void snd_ff_stream_lock_release(struct snd_ff *ff);
143 
144 void snd_ff_proc_init(struct snd_ff *ff);
145 
146 int snd_ff_create_midi_devices(struct snd_ff *ff);
147 
148 int snd_ff_create_pcm_devices(struct snd_ff *ff);
149 
150 int snd_ff_create_hwdep_devices(struct snd_ff *ff);
151 
152 #endif
153