1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * dice.h - a part of driver for Dice based devices
4 *
5 * Copyright (c) Clemens Ladisch
6 * Copyright (c) 2014 Takashi Sakamoto
7 */
8
9 #ifndef SOUND_DICE_H_INCLUDED
10 #define SOUND_DICE_H_INCLUDED
11
12 #include <linux/compat.h>
13 #include <linux/completion.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/firewire.h>
17 #include <linux/firewire-constants.h>
18 #include <linux/jiffies.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/wait.h>
24 #include <linux/sched/signal.h>
25
26 #include <sound/control.h>
27 #include <sound/core.h>
28 #include <sound/firewire.h>
29 #include <sound/hwdep.h>
30 #include <sound/info.h>
31 #include <sound/initval.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/rawmidi.h>
35
36 #include "../amdtp-am824.h"
37 #include "../iso-resources.h"
38 #include "../lib.h"
39 #include "dice-interface.h"
40
41 /*
42 * This module support maximum 2 pairs of tx/rx isochronous streams for
43 * our convinience.
44 *
45 * In documents for ASICs called with a name of 'DICE':
46 * - ASIC for DICE II:
47 * - Maximum 2 tx and 4 rx are supported.
48 * - A packet supports maximum 16 data channels.
49 * - TCD2210/2210-E (so-called 'Dice Mini'):
50 * - Maximum 2 tx and 2 rx are supported.
51 * - A packet supports maximum 16 data channels.
52 * - TCD2220/2220-E (so-called 'Dice Jr.')
53 * - 2 tx and 2 rx are supported.
54 * - A packet supports maximum 16 data channels.
55 * - TCD3070-CH (so-called 'Dice III')
56 * - Maximum 2 tx and 2 rx are supported.
57 * - A packet supports maximum 32 data channels.
58 *
59 * For the above, MIDI conformant data channel is just on the first isochronous
60 * stream.
61 */
62 #define MAX_STREAMS 2
63
64 enum snd_dice_rate_mode {
65 SND_DICE_RATE_MODE_LOW = 0,
66 SND_DICE_RATE_MODE_MIDDLE,
67 SND_DICE_RATE_MODE_HIGH,
68 SND_DICE_RATE_MODE_COUNT,
69 };
70
71 struct snd_dice;
72 typedef int (*snd_dice_detect_formats_t)(struct snd_dice *dice);
73
74 struct snd_dice {
75 struct snd_card *card;
76 struct fw_unit *unit;
77 spinlock_t lock;
78 struct mutex mutex;
79
80 /* Offsets for sub-addresses */
81 unsigned int global_offset;
82 unsigned int rx_offset;
83 unsigned int tx_offset;
84 unsigned int sync_offset;
85 unsigned int rsrv_offset;
86
87 unsigned int clock_caps;
88 unsigned int tx_pcm_chs[MAX_STREAMS][SND_DICE_RATE_MODE_COUNT];
89 unsigned int rx_pcm_chs[MAX_STREAMS][SND_DICE_RATE_MODE_COUNT];
90 unsigned int tx_midi_ports[MAX_STREAMS];
91 unsigned int rx_midi_ports[MAX_STREAMS];
92
93 struct fw_address_handler notification_handler;
94 int owner_generation;
95 u32 notification_bits;
96
97 /* For uapi */
98 int dev_lock_count; /* > 0 driver, < 0 userspace */
99 bool dev_lock_changed;
100 wait_queue_head_t hwdep_wait;
101
102 /* For streaming */
103 struct fw_iso_resources tx_resources[MAX_STREAMS];
104 struct fw_iso_resources rx_resources[MAX_STREAMS];
105 struct amdtp_stream tx_stream[MAX_STREAMS];
106 struct amdtp_stream rx_stream[MAX_STREAMS];
107 bool global_enabled:1;
108 bool disable_double_pcm_frames:1;
109 struct completion clock_accepted;
110 unsigned int substreams_counter;
111
112 struct amdtp_domain domain;
113 };
114
115 enum snd_dice_addr_type {
116 SND_DICE_ADDR_TYPE_PRIVATE,
117 SND_DICE_ADDR_TYPE_GLOBAL,
118 SND_DICE_ADDR_TYPE_TX,
119 SND_DICE_ADDR_TYPE_RX,
120 SND_DICE_ADDR_TYPE_SYNC,
121 SND_DICE_ADDR_TYPE_RSRV,
122 };
123
124 int snd_dice_transaction_write(struct snd_dice *dice,
125 enum snd_dice_addr_type type,
126 unsigned int offset,
127 void *buf, unsigned int len);
128 int snd_dice_transaction_read(struct snd_dice *dice,
129 enum snd_dice_addr_type type, unsigned int offset,
130 void *buf, unsigned int len);
131
snd_dice_transaction_write_global(struct snd_dice * dice,unsigned int offset,void * buf,unsigned int len)132 static inline int snd_dice_transaction_write_global(struct snd_dice *dice,
133 unsigned int offset,
134 void *buf, unsigned int len)
135 {
136 return snd_dice_transaction_write(dice,
137 SND_DICE_ADDR_TYPE_GLOBAL, offset,
138 buf, len);
139 }
snd_dice_transaction_read_global(struct snd_dice * dice,unsigned int offset,void * buf,unsigned int len)140 static inline int snd_dice_transaction_read_global(struct snd_dice *dice,
141 unsigned int offset,
142 void *buf, unsigned int len)
143 {
144 return snd_dice_transaction_read(dice,
145 SND_DICE_ADDR_TYPE_GLOBAL, offset,
146 buf, len);
147 }
snd_dice_transaction_write_tx(struct snd_dice * dice,unsigned int offset,void * buf,unsigned int len)148 static inline int snd_dice_transaction_write_tx(struct snd_dice *dice,
149 unsigned int offset,
150 void *buf, unsigned int len)
151 {
152 return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_TX, offset,
153 buf, len);
154 }
snd_dice_transaction_read_tx(struct snd_dice * dice,unsigned int offset,void * buf,unsigned int len)155 static inline int snd_dice_transaction_read_tx(struct snd_dice *dice,
156 unsigned int offset,
157 void *buf, unsigned int len)
158 {
159 return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_TX, offset,
160 buf, len);
161 }
snd_dice_transaction_write_rx(struct snd_dice * dice,unsigned int offset,void * buf,unsigned int len)162 static inline int snd_dice_transaction_write_rx(struct snd_dice *dice,
163 unsigned int offset,
164 void *buf, unsigned int len)
165 {
166 return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_RX, offset,
167 buf, len);
168 }
snd_dice_transaction_read_rx(struct snd_dice * dice,unsigned int offset,void * buf,unsigned int len)169 static inline int snd_dice_transaction_read_rx(struct snd_dice *dice,
170 unsigned int offset,
171 void *buf, unsigned int len)
172 {
173 return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_RX, offset,
174 buf, len);
175 }
snd_dice_transaction_write_sync(struct snd_dice * dice,unsigned int offset,void * buf,unsigned int len)176 static inline int snd_dice_transaction_write_sync(struct snd_dice *dice,
177 unsigned int offset,
178 void *buf, unsigned int len)
179 {
180 return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_SYNC, offset,
181 buf, len);
182 }
snd_dice_transaction_read_sync(struct snd_dice * dice,unsigned int offset,void * buf,unsigned int len)183 static inline int snd_dice_transaction_read_sync(struct snd_dice *dice,
184 unsigned int offset,
185 void *buf, unsigned int len)
186 {
187 return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_SYNC, offset,
188 buf, len);
189 }
190
191 int snd_dice_transaction_get_clock_source(struct snd_dice *dice,
192 unsigned int *source);
193 int snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate);
194 int snd_dice_transaction_set_enable(struct snd_dice *dice);
195 void snd_dice_transaction_clear_enable(struct snd_dice *dice);
196 int snd_dice_transaction_init(struct snd_dice *dice);
197 int snd_dice_transaction_reinit(struct snd_dice *dice);
198 void snd_dice_transaction_destroy(struct snd_dice *dice);
199
200 #define SND_DICE_RATES_COUNT 7
201 extern const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT];
202
203 int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate,
204 enum snd_dice_rate_mode *mode);
205 int snd_dice_stream_start_duplex(struct snd_dice *dice);
206 void snd_dice_stream_stop_duplex(struct snd_dice *dice);
207 int snd_dice_stream_init_duplex(struct snd_dice *dice);
208 void snd_dice_stream_destroy_duplex(struct snd_dice *dice);
209 int snd_dice_stream_reserve_duplex(struct snd_dice *dice, unsigned int rate,
210 unsigned int events_per_period,
211 unsigned int events_per_buffer);
212 void snd_dice_stream_update_duplex(struct snd_dice *dice);
213 int snd_dice_stream_detect_current_formats(struct snd_dice *dice);
214
215 int snd_dice_stream_lock_try(struct snd_dice *dice);
216 void snd_dice_stream_lock_release(struct snd_dice *dice);
217
218 int snd_dice_create_pcm(struct snd_dice *dice);
219
220 int snd_dice_create_hwdep(struct snd_dice *dice);
221
222 void snd_dice_create_proc(struct snd_dice *dice);
223
224 int snd_dice_create_midi(struct snd_dice *dice);
225
226 int snd_dice_detect_tcelectronic_formats(struct snd_dice *dice);
227 int snd_dice_detect_alesis_formats(struct snd_dice *dice);
228 int snd_dice_detect_alesis_mastercontrol_formats(struct snd_dice *dice);
229 int snd_dice_detect_extension_formats(struct snd_dice *dice);
230 int snd_dice_detect_mytek_formats(struct snd_dice *dice);
231 int snd_dice_detect_presonus_formats(struct snd_dice *dice);
232 int snd_dice_detect_harman_formats(struct snd_dice *dice);
233 int snd_dice_detect_focusrite_pro40_tcd3070_formats(struct snd_dice *dice);
234 int snd_dice_detect_weiss_formats(struct snd_dice *dice);
235 int snd_dice_detect_teac_formats(struct snd_dice *dice);
236
237 #endif
238