1 /*
2  * Line6 Linux USB driver - 0.9.1beta
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License as
8  *	published by the Free Software Foundation, version 2.
9  *
10  */
11 
12 /*
13 	PCM interface to POD series devices.
14 */
15 
16 #ifndef PCM_H
17 #define PCM_H
18 
19 #include <sound/pcm.h>
20 
21 #include "driver.h"
22 #include "usbdefs.h"
23 
24 /* number of URBs */
25 #define LINE6_ISO_BUFFERS	2
26 
27 /*
28 	number of USB frames per URB
29 	The Line6 Windows driver always transmits two frames per packet, but
30 	the Linux driver performs significantly better (i.e., lower latency)
31 	with only one frame per packet.
32 */
33 #define LINE6_ISO_PACKETS	1
34 
35 /* in a "full speed" device (such as the PODxt Pro) this means 1ms */
36 #define LINE6_ISO_INTERVAL	1
37 
38 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
39 #define LINE6_IMPULSE_DEFAULT_PERIOD 100
40 #endif
41 
42 /*
43 	Get substream from Line6 PCM data structure
44 */
45 #define get_substream(line6pcm, stream)	\
46 		(line6pcm->pcm->streams[stream].substream)
47 
48 /*
49 	PCM mode bits and masks.
50 	"ALSA": operations triggered by applications via ALSA
51 	"MONITOR": software monitoring
52 	"IMPULSE": optional impulse response operation
53 */
54 enum {
55 	/* individual bits: */
56 	BIT_PCM_ALSA_PLAYBACK,
57 	BIT_PCM_ALSA_CAPTURE,
58 	BIT_PCM_MONITOR_PLAYBACK,
59 	BIT_PCM_MONITOR_CAPTURE,
60 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
61 	BIT_PCM_IMPULSE_PLAYBACK,
62 	BIT_PCM_IMPULSE_CAPTURE,
63 #endif
64 	BIT_PAUSE_PLAYBACK,
65 	BIT_PREPARED,
66 
67 	/* individual masks: */
68 /* *INDENT-OFF* */
69 	MASK_PCM_ALSA_PLAYBACK    = 1 << BIT_PCM_ALSA_PLAYBACK,
70 	MASK_PCM_ALSA_CAPTURE     = 1 << BIT_PCM_ALSA_CAPTURE,
71 	MASK_PCM_MONITOR_PLAYBACK = 1 << BIT_PCM_MONITOR_PLAYBACK,
72 	MASK_PCM_MONITOR_CAPTURE  = 1 << BIT_PCM_MONITOR_CAPTURE,
73 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
74 	MASK_PCM_IMPULSE_PLAYBACK = 1 << BIT_PCM_IMPULSE_PLAYBACK,
75 	MASK_PCM_IMPULSE_CAPTURE  = 1 << BIT_PCM_IMPULSE_CAPTURE,
76 #endif
77 	MASK_PAUSE_PLAYBACK       = 1 << BIT_PAUSE_PLAYBACK,
78 	MASK_PREPARED             = 1 << BIT_PREPARED,
79 /* *INDENT-ON* */
80 
81 	/* combined masks (by operation): */
82 	MASK_PCM_ALSA = MASK_PCM_ALSA_PLAYBACK | MASK_PCM_ALSA_CAPTURE,
83 	MASK_PCM_MONITOR = MASK_PCM_MONITOR_PLAYBACK | MASK_PCM_MONITOR_CAPTURE,
84 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
85 	MASK_PCM_IMPULSE = MASK_PCM_IMPULSE_PLAYBACK | MASK_PCM_IMPULSE_CAPTURE,
86 #endif
87 
88 	/* combined masks (by direction): */
89 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
90 	MASK_PLAYBACK =
91 	    MASK_PCM_ALSA_PLAYBACK | MASK_PCM_MONITOR_PLAYBACK |
92 	    MASK_PCM_IMPULSE_PLAYBACK,
93 	MASK_CAPTURE =
94 	    MASK_PCM_ALSA_CAPTURE | MASK_PCM_MONITOR_CAPTURE |
95 	    MASK_PCM_IMPULSE_CAPTURE
96 #else
97 	MASK_PLAYBACK = MASK_PCM_ALSA_PLAYBACK | MASK_PCM_MONITOR_PLAYBACK,
98 	MASK_CAPTURE = MASK_PCM_ALSA_CAPTURE | MASK_PCM_MONITOR_CAPTURE
99 #endif
100 };
101 
102 struct line6_pcm_properties {
103 	struct snd_pcm_hardware snd_line6_playback_hw, snd_line6_capture_hw;
104 	struct snd_pcm_hw_constraint_ratdens snd_line6_rates;
105 	int bytes_per_frame;
106 };
107 
108 struct snd_line6_pcm {
109 	/**
110 		 Pointer back to the Line6 driver data structure.
111 	*/
112 	struct usb_line6 *line6;
113 
114 	/**
115 		 Properties.
116 	*/
117 	struct line6_pcm_properties *properties;
118 
119 	/**
120 		 ALSA pcm stream
121 	*/
122 	struct snd_pcm *pcm;
123 
124 	/**
125 		 URBs for audio playback.
126 	*/
127 	struct urb *urb_audio_out[LINE6_ISO_BUFFERS];
128 
129 	/**
130 		 URBs for audio capture.
131 	*/
132 	struct urb *urb_audio_in[LINE6_ISO_BUFFERS];
133 
134 	/**
135 		 Temporary buffer for playback.
136 		 Since the packet size is not known in advance, this buffer is
137 		 large enough to store maximum size packets.
138 	*/
139 	unsigned char *buffer_out;
140 
141 	/**
142 		 Temporary buffer for capture.
143 		 Since the packet size is not known in advance, this buffer is
144 		 large enough to store maximum size packets.
145 	*/
146 	unsigned char *buffer_in;
147 
148 	/**
149 		 Previously captured frame (for software monitoring).
150 	*/
151 	unsigned char *prev_fbuf;
152 
153 	/**
154 		 Size of previously captured frame (for software monitoring).
155 	*/
156 	int prev_fsize;
157 
158 	/**
159 		 Free frame position in the playback buffer.
160 	*/
161 	snd_pcm_uframes_t pos_out;
162 
163 	/**
164 		 Count processed bytes for playback.
165 		 This is modulo period size (to determine when a period is
166 		 finished).
167 	*/
168 	unsigned bytes_out;
169 
170 	/**
171 		 Counter to create desired playback sample rate.
172 	*/
173 	unsigned count_out;
174 
175 	/**
176 		 Playback period size in bytes
177 	*/
178 	unsigned period_out;
179 
180 	/**
181 		 Processed frame position in the playback buffer.
182 		 The contents of the output ring buffer have been consumed by
183 		 the USB subsystem (i.e., sent to the USB device) up to this
184 		 position.
185 	*/
186 	snd_pcm_uframes_t pos_out_done;
187 
188 	/**
189 		 Count processed bytes for capture.
190 		 This is modulo period size (to determine when a period is
191 		 finished).
192 	*/
193 	unsigned bytes_in;
194 
195 	/**
196 		 Counter to create desired capture sample rate.
197 	*/
198 	unsigned count_in;
199 
200 	/**
201 		 Capture period size in bytes
202 	*/
203 	unsigned period_in;
204 
205 	/**
206 		 Processed frame position in the capture buffer.
207 		 The contents of the output ring buffer have been consumed by
208 		 the USB subsystem (i.e., sent to the USB device) up to this
209 		 position.
210 	*/
211 	snd_pcm_uframes_t pos_in_done;
212 
213 	/**
214 		 Bit mask of active playback URBs.
215 	*/
216 	unsigned long active_urb_out;
217 
218 	/**
219 		 Maximum size of USB packet.
220 	*/
221 	int max_packet_size;
222 
223 	/**
224 		 USB endpoint for listening to audio data.
225 	*/
226 	int ep_audio_read;
227 
228 	/**
229 		 USB endpoint for writing audio data.
230 	*/
231 	int ep_audio_write;
232 
233 	/**
234 		 Bit mask of active capture URBs.
235 	*/
236 	unsigned long active_urb_in;
237 
238 	/**
239 		 Bit mask of playback URBs currently being unlinked.
240 	*/
241 	unsigned long unlink_urb_out;
242 
243 	/**
244 		 Bit mask of capture URBs currently being unlinked.
245 	*/
246 	unsigned long unlink_urb_in;
247 
248 	/**
249 		 Spin lock to protect updates of the playback buffer positions (not
250 		 contents!)
251 	*/
252 	spinlock_t lock_audio_out;
253 
254 	/**
255 		 Spin lock to protect updates of the capture buffer positions (not
256 		 contents!)
257 	*/
258 	spinlock_t lock_audio_in;
259 
260 	/**
261 		 Spin lock to protect trigger.
262 	*/
263 	spinlock_t lock_trigger;
264 
265 	/**
266 		 PCM playback volume (left and right).
267 	*/
268 	int volume_playback[2];
269 
270 	/**
271 		 PCM monitor volume.
272 	*/
273 	int volume_monitor;
274 
275 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
276 	/**
277 		 Volume of impulse response test signal (if zero, test is disabled).
278 	*/
279 	int impulse_volume;
280 
281 	/**
282 		 Period of impulse response test signal.
283 	*/
284 	int impulse_period;
285 
286 	/**
287 		 Counter for impulse response test signal.
288 	*/
289 	int impulse_count;
290 #endif
291 
292 	/**
293 		 Several status bits (see BIT_*).
294 	*/
295 	unsigned long flags;
296 
297 	int last_frame_in, last_frame_out;
298 };
299 
300 extern int line6_init_pcm(struct usb_line6 *line6,
301 			  struct line6_pcm_properties *properties);
302 extern int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd);
303 extern int snd_line6_prepare(struct snd_pcm_substream *substream);
304 extern void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm);
305 extern int line6_pcm_start(struct snd_line6_pcm *line6pcm, int channels);
306 extern int line6_pcm_stop(struct snd_line6_pcm *line6pcm, int channels);
307 
308 #define PRINT_FRAME_DIFF(op) {						\
309 	static int diff_prev = 1000;					\
310 	int diff = line6pcm->last_frame_out - line6pcm->last_frame_in;	\
311 	if ((diff != diff_prev) && (abs(diff) < 100)) {			\
312 		printk(KERN_INFO "%s frame diff = %d\n", op, diff);	\
313 		diff_prev = diff;					\
314 	}								\
315 }
316 
317 #endif
318