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.
50 
51 	There are several features of the Line6 USB driver which require PCM
52 	data to be exchanged with the device:
53 	*) PCM playback and capture via ALSA
54 	*) software monitoring (for devices without hardware monitoring)
55 	*) optional impulse response measurement
56 	However, from the device's point of view, there is just a single
57 	capture and playback stream, which must be shared between these
58 	subsystems. It is therefore necessary to maintain the state of the
59 	subsystems with respect to PCM usage. We define several constants of
60 	the form LINE6_BIT_PCM_<subsystem>_<direction>_<resource> with the
61 	following meanings:
62 	*) <subsystem> is one of
63 	-) ALSA: PCM playback and capture via ALSA
64 	-) MONITOR: software monitoring
65 	-) IMPULSE: optional impulse response measurement
66 	*) <direction> is one of
67 	-) PLAYBACK: audio output (from host to device)
68 	-) CAPTURE: audio input (from device to host)
69 	*) <resource> is one of
70 	-) BUFFER: buffer required by PCM data stream
71 	-) STREAM: actual PCM data stream
72 
73 	The subsystems call line6_pcm_acquire() to acquire the (shared)
74 	resources needed for a particular operation (e.g., allocate the buffer
75 	for ALSA playback or start the capture stream for software monitoring).
76 	When a resource is no longer needed, it is released by calling
77 	line6_pcm_release(). Buffer allocation and stream startup are handled
78 	separately to allow the ALSA kernel driver to perform them at
79 	appropriate places (since the callback which starts a PCM stream is not
80 	allowed to sleep).
81 */
82 enum {
83 	/* individual bit indices: */
84 	LINE6_INDEX_PCM_ALSA_PLAYBACK_BUFFER,
85 	LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM,
86 	LINE6_INDEX_PCM_ALSA_CAPTURE_BUFFER,
87 	LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
88 	LINE6_INDEX_PCM_MONITOR_PLAYBACK_BUFFER,
89 	LINE6_INDEX_PCM_MONITOR_PLAYBACK_STREAM,
90 	LINE6_INDEX_PCM_MONITOR_CAPTURE_BUFFER,
91 	LINE6_INDEX_PCM_MONITOR_CAPTURE_STREAM,
92 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
93 	LINE6_INDEX_PCM_IMPULSE_PLAYBACK_BUFFER,
94 	LINE6_INDEX_PCM_IMPULSE_PLAYBACK_STREAM,
95 	LINE6_INDEX_PCM_IMPULSE_CAPTURE_BUFFER,
96 	LINE6_INDEX_PCM_IMPULSE_CAPTURE_STREAM,
97 #endif
98 	LINE6_INDEX_PAUSE_PLAYBACK,
99 	LINE6_INDEX_PREPARED,
100 
101 	/* individual bit masks: */
102 	LINE6_BIT(PCM_ALSA_PLAYBACK_BUFFER),
103 	LINE6_BIT(PCM_ALSA_PLAYBACK_STREAM),
104 	LINE6_BIT(PCM_ALSA_CAPTURE_BUFFER),
105 	LINE6_BIT(PCM_ALSA_CAPTURE_STREAM),
106 	LINE6_BIT(PCM_MONITOR_PLAYBACK_BUFFER),
107 	LINE6_BIT(PCM_MONITOR_PLAYBACK_STREAM),
108 	LINE6_BIT(PCM_MONITOR_CAPTURE_BUFFER),
109 	LINE6_BIT(PCM_MONITOR_CAPTURE_STREAM),
110 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
111 	LINE6_BIT(PCM_IMPULSE_PLAYBACK_BUFFER),
112 	LINE6_BIT(PCM_IMPULSE_PLAYBACK_STREAM),
113 	LINE6_BIT(PCM_IMPULSE_CAPTURE_BUFFER),
114 	LINE6_BIT(PCM_IMPULSE_CAPTURE_STREAM),
115 #endif
116 	LINE6_BIT(PAUSE_PLAYBACK),
117 	LINE6_BIT(PREPARED),
118 
119 	/* combined bit masks (by operation): */
120 	LINE6_BITS_PCM_ALSA_BUFFER =
121 	    LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER |
122 	    LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER,
123 
124 	LINE6_BITS_PCM_ALSA_STREAM =
125 	    LINE6_BIT_PCM_ALSA_PLAYBACK_STREAM |
126 	    LINE6_BIT_PCM_ALSA_CAPTURE_STREAM,
127 
128 	LINE6_BITS_PCM_MONITOR =
129 	    LINE6_BIT_PCM_MONITOR_PLAYBACK_BUFFER |
130 	    LINE6_BIT_PCM_MONITOR_PLAYBACK_STREAM |
131 	    LINE6_BIT_PCM_MONITOR_CAPTURE_BUFFER |
132 	    LINE6_BIT_PCM_MONITOR_CAPTURE_STREAM,
133 
134 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
135 	LINE6_BITS_PCM_IMPULSE =
136 	    LINE6_BIT_PCM_IMPULSE_PLAYBACK_BUFFER |
137 	    LINE6_BIT_PCM_IMPULSE_PLAYBACK_STREAM |
138 	    LINE6_BIT_PCM_IMPULSE_CAPTURE_BUFFER |
139 	    LINE6_BIT_PCM_IMPULSE_CAPTURE_STREAM,
140 #endif
141 
142 	/* combined bit masks (by direction): */
143 	LINE6_BITS_PLAYBACK_BUFFER =
144 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
145 	    LINE6_BIT_PCM_IMPULSE_PLAYBACK_BUFFER |
146 #endif
147 	    LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER |
148 	    LINE6_BIT_PCM_MONITOR_PLAYBACK_BUFFER ,
149 
150 	LINE6_BITS_PLAYBACK_STREAM =
151 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
152 	    LINE6_BIT_PCM_IMPULSE_PLAYBACK_STREAM |
153 #endif
154 	    LINE6_BIT_PCM_ALSA_PLAYBACK_STREAM |
155 	    LINE6_BIT_PCM_MONITOR_PLAYBACK_STREAM ,
156 
157 	LINE6_BITS_CAPTURE_BUFFER =
158 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
159 	    LINE6_BIT_PCM_IMPULSE_CAPTURE_BUFFER |
160 #endif
161 	    LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER |
162 	    LINE6_BIT_PCM_MONITOR_CAPTURE_BUFFER ,
163 
164 	LINE6_BITS_CAPTURE_STREAM =
165 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
166 	    LINE6_BIT_PCM_IMPULSE_CAPTURE_STREAM |
167 #endif
168 	    LINE6_BIT_PCM_ALSA_CAPTURE_STREAM |
169 	    LINE6_BIT_PCM_MONITOR_CAPTURE_STREAM,
170 
171 	LINE6_BITS_STREAM =
172 	    LINE6_BITS_PLAYBACK_STREAM |
173 	    LINE6_BITS_CAPTURE_STREAM
174 };
175 
176 struct line6_pcm_properties {
177 	struct snd_pcm_hardware snd_line6_playback_hw, snd_line6_capture_hw;
178 	struct snd_pcm_hw_constraint_ratdens snd_line6_rates;
179 	int bytes_per_frame;
180 };
181 
182 struct snd_line6_pcm {
183 	/**
184 		 Pointer back to the Line6 driver data structure.
185 	*/
186 	struct usb_line6 *line6;
187 
188 	/**
189 		 Properties.
190 	*/
191 	struct line6_pcm_properties *properties;
192 
193 	/**
194 		 ALSA pcm stream
195 	*/
196 	struct snd_pcm *pcm;
197 
198 	/**
199 		 URBs for audio playback.
200 	*/
201 	struct urb *urb_audio_out[LINE6_ISO_BUFFERS];
202 
203 	/**
204 		 URBs for audio capture.
205 	*/
206 	struct urb *urb_audio_in[LINE6_ISO_BUFFERS];
207 
208 	/**
209 		 Temporary buffer for playback.
210 		 Since the packet size is not known in advance, this buffer is
211 		 large enough to store maximum size packets.
212 	*/
213 	unsigned char *buffer_out;
214 
215 	/**
216 		 Temporary buffer for capture.
217 		 Since the packet size is not known in advance, this buffer is
218 		 large enough to store maximum size packets.
219 	*/
220 	unsigned char *buffer_in;
221 
222 	/**
223 		 Previously captured frame (for software monitoring).
224 	*/
225 	unsigned char *prev_fbuf;
226 
227 	/**
228 		 Size of previously captured frame (for software monitoring).
229 	*/
230 	int prev_fsize;
231 
232 	/**
233 		 Free frame position in the playback buffer.
234 	*/
235 	snd_pcm_uframes_t pos_out;
236 
237 	/**
238 		 Count processed bytes for playback.
239 		 This is modulo period size (to determine when a period is
240 		 finished).
241 	*/
242 	unsigned bytes_out;
243 
244 	/**
245 		 Counter to create desired playback sample rate.
246 	*/
247 	unsigned count_out;
248 
249 	/**
250 		 Playback period size in bytes
251 	*/
252 	unsigned period_out;
253 
254 	/**
255 		 Processed frame position in the playback buffer.
256 		 The contents of the output ring buffer have been consumed by
257 		 the USB subsystem (i.e., sent to the USB device) up to this
258 		 position.
259 	*/
260 	snd_pcm_uframes_t pos_out_done;
261 
262 	/**
263 		 Count processed bytes for capture.
264 		 This is modulo period size (to determine when a period is
265 		 finished).
266 	*/
267 	unsigned bytes_in;
268 
269 	/**
270 		 Counter to create desired capture sample rate.
271 	*/
272 	unsigned count_in;
273 
274 	/**
275 		 Capture period size in bytes
276 	*/
277 	unsigned period_in;
278 
279 	/**
280 		 Processed frame position in the capture buffer.
281 		 The contents of the output ring buffer have been consumed by
282 		 the USB subsystem (i.e., sent to the USB device) up to this
283 		 position.
284 	*/
285 	snd_pcm_uframes_t pos_in_done;
286 
287 	/**
288 		 Bit mask of active playback URBs.
289 	*/
290 	unsigned long active_urb_out;
291 
292 	/**
293 		 Maximum size of USB packet.
294 	*/
295 	int max_packet_size;
296 
297 	/**
298 		 USB endpoint for listening to audio data.
299 	*/
300 	int ep_audio_read;
301 
302 	/**
303 		 USB endpoint for writing audio data.
304 	*/
305 	int ep_audio_write;
306 
307 	/**
308 		 Bit mask of active capture URBs.
309 	*/
310 	unsigned long active_urb_in;
311 
312 	/**
313 		 Bit mask of playback URBs currently being unlinked.
314 	*/
315 	unsigned long unlink_urb_out;
316 
317 	/**
318 		 Bit mask of capture URBs currently being unlinked.
319 	*/
320 	unsigned long unlink_urb_in;
321 
322 	/**
323 		 Spin lock to protect updates of the playback buffer positions (not
324 		 contents!)
325 	*/
326 	spinlock_t lock_audio_out;
327 
328 	/**
329 		 Spin lock to protect updates of the capture buffer positions (not
330 		 contents!)
331 	*/
332 	spinlock_t lock_audio_in;
333 
334 	/**
335 		 Spin lock to protect trigger.
336 	*/
337 	spinlock_t lock_trigger;
338 
339 	/**
340 		 PCM playback volume (left and right).
341 	*/
342 	int volume_playback[2];
343 
344 	/**
345 		 PCM monitor volume.
346 	*/
347 	int volume_monitor;
348 
349 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
350 	/**
351 		 Volume of impulse response test signal (if zero, test is disabled).
352 	*/
353 	int impulse_volume;
354 
355 	/**
356 		 Period of impulse response test signal.
357 	*/
358 	int impulse_period;
359 
360 	/**
361 		 Counter for impulse response test signal.
362 	*/
363 	int impulse_count;
364 #endif
365 
366 	/**
367 		 Several status bits (see LINE6_BIT_*).
368 	*/
369 	unsigned long flags;
370 
371 	int last_frame_in, last_frame_out;
372 };
373 
374 extern int line6_init_pcm(struct usb_line6 *line6,
375 			  struct line6_pcm_properties *properties);
376 extern int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd);
377 extern int snd_line6_prepare(struct snd_pcm_substream *substream);
378 extern void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm);
379 extern int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels);
380 extern int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels);
381 
382 #endif
383