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