]>
Commit | Line | Data |
---|---|---|
c6d43ba8 TS |
1 | /* |
2 | * Linux driver for TerraTec DMX 6Fire USB | |
3 | * | |
4 | * PCM driver | |
5 | * | |
6 | * Author: Torsten Schenk <torsten.schenk@zoho.com> | |
7 | * Created: Jan 01, 2011 | |
c6d43ba8 TS |
8 | * Copyright: (C) Torsten Schenk |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | */ | |
15 | ||
16 | #include "pcm.h" | |
17 | #include "chip.h" | |
18 | #include "comm.h" | |
2475b0d4 | 19 | #include "control.h" |
c6d43ba8 TS |
20 | |
21 | enum { | |
22 | OUT_N_CHANNELS = 6, IN_N_CHANNELS = 4 | |
23 | }; | |
24 | ||
25 | /* keep next two synced with | |
2475b0d4 TS |
26 | * FW_EP_W_MAX_PACKET_SIZE[] and RATES_MAX_PACKET_SIZE |
27 | * and CONTROL_RATE_XXX in control.h */ | |
c6d43ba8 TS |
28 | static const int rates_in_packet_size[] = { 228, 228, 420, 420, 404, 404 }; |
29 | static const int rates_out_packet_size[] = { 228, 228, 420, 420, 604, 604 }; | |
30 | static const int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000 }; | |
c6d43ba8 TS |
31 | static const int rates_alsaid[] = { |
32 | SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_48000, | |
33 | SNDRV_PCM_RATE_88200, SNDRV_PCM_RATE_96000, | |
34 | SNDRV_PCM_RATE_176400, SNDRV_PCM_RATE_192000 }; | |
35 | ||
c6d43ba8 TS |
36 | enum { /* settings for pcm */ |
37 | OUT_EP = 6, IN_EP = 2, MAX_BUFSIZE = 128 * 1024 | |
38 | }; | |
39 | ||
40 | enum { /* pcm streaming states */ | |
41 | STREAM_DISABLED, /* no pcm streaming */ | |
42 | STREAM_STARTING, /* pcm streaming requested, waiting to become ready */ | |
43 | STREAM_RUNNING, /* pcm streaming running */ | |
44 | STREAM_STOPPING | |
45 | }; | |
46 | ||
c6d43ba8 TS |
47 | static const struct snd_pcm_hardware pcm_hw = { |
48 | .info = SNDRV_PCM_INFO_MMAP | | |
49 | SNDRV_PCM_INFO_INTERLEAVED | | |
50 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
51 | SNDRV_PCM_INFO_MMAP_VALID | | |
52 | SNDRV_PCM_INFO_BATCH, | |
53 | ||
58c54fa4 | 54 | .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE, |
c6d43ba8 TS |
55 | |
56 | .rates = SNDRV_PCM_RATE_44100 | | |
57 | SNDRV_PCM_RATE_48000 | | |
58 | SNDRV_PCM_RATE_88200 | | |
59 | SNDRV_PCM_RATE_96000 | | |
60 | SNDRV_PCM_RATE_176400 | | |
61 | SNDRV_PCM_RATE_192000, | |
62 | ||
63 | .rate_min = 44100, | |
64 | .rate_max = 192000, | |
65 | .channels_min = 1, | |
66 | .channels_max = 0, /* set in pcm_open, depending on capture/playback */ | |
67 | .buffer_bytes_max = MAX_BUFSIZE, | |
68 | .period_bytes_min = PCM_N_PACKETS_PER_URB * (PCM_MAX_PACKET_SIZE - 4), | |
69 | .period_bytes_max = MAX_BUFSIZE, | |
70 | .periods_min = 2, | |
71 | .periods_max = 1024 | |
72 | }; | |
73 | ||
74 | static int usb6fire_pcm_set_rate(struct pcm_runtime *rt) | |
75 | { | |
76 | int ret; | |
2475b0d4 | 77 | struct control_runtime *ctrl_rt = rt->chip->control; |
c6d43ba8 | 78 | |
2475b0d4 TS |
79 | ctrl_rt->usb_streaming = false; |
80 | ret = ctrl_rt->update_streaming(ctrl_rt); | |
c6d43ba8 TS |
81 | if (ret < 0) { |
82 | snd_printk(KERN_ERR PREFIX "error stopping streaming while " | |
83 | "setting samplerate %d.\n", rates[rt->rate]); | |
84 | return ret; | |
85 | } | |
86 | ||
2475b0d4 | 87 | ret = ctrl_rt->set_rate(ctrl_rt, rt->rate); |
c6d43ba8 TS |
88 | if (ret < 0) { |
89 | snd_printk(KERN_ERR PREFIX "error setting samplerate %d.\n", | |
90 | rates[rt->rate]); | |
91 | return ret; | |
92 | } | |
93 | ||
2475b0d4 TS |
94 | ret = ctrl_rt->set_channels(ctrl_rt, OUT_N_CHANNELS, IN_N_CHANNELS, |
95 | false, false); | |
c6d43ba8 | 96 | if (ret < 0) { |
2475b0d4 | 97 | snd_printk(KERN_ERR PREFIX "error initializing channels " |
c6d43ba8 TS |
98 | "while setting samplerate %d.\n", |
99 | rates[rt->rate]); | |
100 | return ret; | |
101 | } | |
c6d43ba8 | 102 | |
2475b0d4 TS |
103 | ctrl_rt->usb_streaming = true; |
104 | ret = ctrl_rt->update_streaming(ctrl_rt); | |
c6d43ba8 TS |
105 | if (ret < 0) { |
106 | snd_printk(KERN_ERR PREFIX "error starting streaming while " | |
107 | "setting samplerate %d.\n", rates[rt->rate]); | |
108 | return ret; | |
109 | } | |
110 | ||
111 | rt->in_n_analog = IN_N_CHANNELS; | |
112 | rt->out_n_analog = OUT_N_CHANNELS; | |
113 | rt->in_packet_size = rates_in_packet_size[rt->rate]; | |
114 | rt->out_packet_size = rates_out_packet_size[rt->rate]; | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static struct pcm_substream *usb6fire_pcm_get_substream( | |
119 | struct snd_pcm_substream *alsa_sub) | |
120 | { | |
121 | struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub); | |
122 | ||
123 | if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
124 | return &rt->playback; | |
125 | else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE) | |
126 | return &rt->capture; | |
127 | snd_printk(KERN_ERR PREFIX "error getting pcm substream slot.\n"); | |
128 | return NULL; | |
129 | } | |
130 | ||
131 | /* call with stream_mutex locked */ | |
132 | static void usb6fire_pcm_stream_stop(struct pcm_runtime *rt) | |
133 | { | |
134 | int i; | |
2475b0d4 | 135 | struct control_runtime *ctrl_rt = rt->chip->control; |
c6d43ba8 TS |
136 | |
137 | if (rt->stream_state != STREAM_DISABLED) { | |
9621055f JK |
138 | |
139 | rt->stream_state = STREAM_STOPPING; | |
140 | ||
c6d43ba8 TS |
141 | for (i = 0; i < PCM_N_URBS; i++) { |
142 | usb_kill_urb(&rt->in_urbs[i].instance); | |
143 | usb_kill_urb(&rt->out_urbs[i].instance); | |
144 | } | |
2475b0d4 TS |
145 | ctrl_rt->usb_streaming = false; |
146 | ctrl_rt->update_streaming(ctrl_rt); | |
c6d43ba8 TS |
147 | rt->stream_state = STREAM_DISABLED; |
148 | } | |
149 | } | |
150 | ||
151 | /* call with stream_mutex locked */ | |
152 | static int usb6fire_pcm_stream_start(struct pcm_runtime *rt) | |
153 | { | |
154 | int ret; | |
155 | int i; | |
156 | int k; | |
157 | struct usb_iso_packet_descriptor *packet; | |
158 | ||
159 | if (rt->stream_state == STREAM_DISABLED) { | |
160 | /* submit our in urbs */ | |
161 | rt->stream_wait_cond = false; | |
162 | rt->stream_state = STREAM_STARTING; | |
163 | for (i = 0; i < PCM_N_URBS; i++) { | |
164 | for (k = 0; k < PCM_N_PACKETS_PER_URB; k++) { | |
165 | packet = &rt->in_urbs[i].packets[k]; | |
166 | packet->offset = k * rt->in_packet_size; | |
167 | packet->length = rt->in_packet_size; | |
168 | packet->actual_length = 0; | |
169 | packet->status = 0; | |
170 | } | |
171 | ret = usb_submit_urb(&rt->in_urbs[i].instance, | |
172 | GFP_ATOMIC); | |
173 | if (ret) { | |
174 | usb6fire_pcm_stream_stop(rt); | |
175 | return ret; | |
176 | } | |
177 | } | |
178 | ||
179 | /* wait for first out urb to return (sent in in urb handler) */ | |
180 | wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond, | |
181 | HZ); | |
182 | if (rt->stream_wait_cond) | |
183 | rt->stream_state = STREAM_RUNNING; | |
184 | else { | |
185 | usb6fire_pcm_stream_stop(rt); | |
186 | return -EIO; | |
187 | } | |
188 | } | |
189 | return 0; | |
190 | } | |
191 | ||
192 | /* call with substream locked */ | |
193 | static void usb6fire_pcm_capture(struct pcm_substream *sub, struct pcm_urb *urb) | |
194 | { | |
195 | int i; | |
196 | int frame; | |
197 | int frame_count; | |
198 | unsigned int total_length = 0; | |
199 | struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance); | |
200 | struct snd_pcm_runtime *alsa_rt = sub->instance->runtime; | |
58c54fa4 | 201 | u32 *src = NULL; |
c6d43ba8 TS |
202 | u32 *dest = (u32 *) (alsa_rt->dma_area + sub->dma_off |
203 | * (alsa_rt->frame_bits >> 3)); | |
204 | u32 *dest_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size | |
205 | * (alsa_rt->frame_bits >> 3)); | |
206 | int bytes_per_frame = alsa_rt->channels << 2; | |
207 | ||
208 | for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) { | |
209 | /* at least 4 header bytes for valid packet. | |
210 | * after that: 32 bits per sample for analog channels */ | |
211 | if (urb->packets[i].actual_length > 4) | |
212 | frame_count = (urb->packets[i].actual_length - 4) | |
213 | / (rt->in_n_analog << 2); | |
214 | else | |
215 | frame_count = 0; | |
216 | ||
58c54fa4 TS |
217 | if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE) |
218 | src = (u32 *) (urb->buffer + total_length); | |
219 | else if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE) | |
220 | src = (u32 *) (urb->buffer - 1 + total_length); | |
221 | else | |
222 | return; | |
c6d43ba8 TS |
223 | src++; /* skip leading 4 bytes of every packet */ |
224 | total_length += urb->packets[i].length; | |
225 | for (frame = 0; frame < frame_count; frame++) { | |
226 | memcpy(dest, src, bytes_per_frame); | |
227 | dest += alsa_rt->channels; | |
228 | src += rt->in_n_analog; | |
229 | sub->dma_off++; | |
230 | sub->period_off++; | |
231 | if (dest == dest_end) { | |
232 | sub->dma_off = 0; | |
233 | dest = (u32 *) alsa_rt->dma_area; | |
234 | } | |
235 | } | |
236 | } | |
237 | } | |
238 | ||
239 | /* call with substream locked */ | |
240 | static void usb6fire_pcm_playback(struct pcm_substream *sub, | |
241 | struct pcm_urb *urb) | |
242 | { | |
243 | int i; | |
244 | int frame; | |
245 | int frame_count; | |
246 | struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance); | |
247 | struct snd_pcm_runtime *alsa_rt = sub->instance->runtime; | |
248 | u32 *src = (u32 *) (alsa_rt->dma_area + sub->dma_off | |
249 | * (alsa_rt->frame_bits >> 3)); | |
250 | u32 *src_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size | |
251 | * (alsa_rt->frame_bits >> 3)); | |
58c54fa4 | 252 | u32 *dest; |
c6d43ba8 TS |
253 | int bytes_per_frame = alsa_rt->channels << 2; |
254 | ||
58c54fa4 TS |
255 | if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE) |
256 | dest = (u32 *) (urb->buffer - 1); | |
257 | else if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE) | |
258 | dest = (u32 *) (urb->buffer); | |
259 | else { | |
260 | snd_printk(KERN_ERR PREFIX "Unknown sample format."); | |
261 | return; | |
262 | } | |
263 | ||
c6d43ba8 TS |
264 | for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) { |
265 | /* at least 4 header bytes for valid packet. | |
266 | * after that: 32 bits per sample for analog channels */ | |
267 | if (urb->packets[i].length > 4) | |
268 | frame_count = (urb->packets[i].length - 4) | |
269 | / (rt->out_n_analog << 2); | |
270 | else | |
271 | frame_count = 0; | |
272 | dest++; /* skip leading 4 bytes of every frame */ | |
273 | for (frame = 0; frame < frame_count; frame++) { | |
274 | memcpy(dest, src, bytes_per_frame); | |
275 | src += alsa_rt->channels; | |
276 | dest += rt->out_n_analog; | |
277 | sub->dma_off++; | |
278 | sub->period_off++; | |
279 | if (src == src_end) { | |
280 | src = (u32 *) alsa_rt->dma_area; | |
281 | sub->dma_off = 0; | |
282 | } | |
283 | } | |
284 | } | |
285 | } | |
286 | ||
287 | static void usb6fire_pcm_in_urb_handler(struct urb *usb_urb) | |
288 | { | |
289 | struct pcm_urb *in_urb = usb_urb->context; | |
290 | struct pcm_urb *out_urb = in_urb->peer; | |
291 | struct pcm_runtime *rt = in_urb->chip->pcm; | |
292 | struct pcm_substream *sub; | |
293 | unsigned long flags; | |
294 | int total_length = 0; | |
295 | int frame_count; | |
296 | int frame; | |
297 | int channel; | |
298 | int i; | |
299 | u8 *dest; | |
300 | ||
301 | if (usb_urb->status || rt->panic || rt->stream_state == STREAM_STOPPING) | |
302 | return; | |
303 | for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) | |
304 | if (in_urb->packets[i].status) { | |
305 | rt->panic = true; | |
306 | return; | |
307 | } | |
308 | ||
309 | if (rt->stream_state == STREAM_DISABLED) { | |
310 | snd_printk(KERN_ERR PREFIX "internal error: " | |
311 | "stream disabled in in-urb handler.\n"); | |
312 | return; | |
313 | } | |
314 | ||
315 | /* receive our capture data */ | |
316 | sub = &rt->capture; | |
317 | spin_lock_irqsave(&sub->lock, flags); | |
318 | if (sub->active) { | |
319 | usb6fire_pcm_capture(sub, in_urb); | |
320 | if (sub->period_off >= sub->instance->runtime->period_size) { | |
321 | sub->period_off %= sub->instance->runtime->period_size; | |
322 | spin_unlock_irqrestore(&sub->lock, flags); | |
323 | snd_pcm_period_elapsed(sub->instance); | |
324 | } else | |
325 | spin_unlock_irqrestore(&sub->lock, flags); | |
326 | } else | |
327 | spin_unlock_irqrestore(&sub->lock, flags); | |
328 | ||
329 | /* setup out urb structure */ | |
330 | for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) { | |
331 | out_urb->packets[i].offset = total_length; | |
332 | out_urb->packets[i].length = (in_urb->packets[i].actual_length | |
333 | - 4) / (rt->in_n_analog << 2) | |
334 | * (rt->out_n_analog << 2) + 4; | |
335 | out_urb->packets[i].status = 0; | |
336 | total_length += out_urb->packets[i].length; | |
337 | } | |
338 | memset(out_urb->buffer, 0, total_length); | |
339 | ||
340 | /* now send our playback data (if a free out urb was found) */ | |
341 | sub = &rt->playback; | |
342 | spin_lock_irqsave(&sub->lock, flags); | |
343 | if (sub->active) { | |
344 | usb6fire_pcm_playback(sub, out_urb); | |
345 | if (sub->period_off >= sub->instance->runtime->period_size) { | |
346 | sub->period_off %= sub->instance->runtime->period_size; | |
347 | spin_unlock_irqrestore(&sub->lock, flags); | |
348 | snd_pcm_period_elapsed(sub->instance); | |
349 | } else | |
350 | spin_unlock_irqrestore(&sub->lock, flags); | |
351 | } else | |
352 | spin_unlock_irqrestore(&sub->lock, flags); | |
353 | ||
354 | /* setup the 4th byte of each sample (0x40 for analog channels) */ | |
355 | dest = out_urb->buffer; | |
356 | for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) | |
357 | if (out_urb->packets[i].length >= 4) { | |
358 | frame_count = (out_urb->packets[i].length - 4) | |
359 | / (rt->out_n_analog << 2); | |
360 | *(dest++) = 0xaa; | |
361 | *(dest++) = 0xaa; | |
362 | *(dest++) = frame_count; | |
363 | *(dest++) = 0x00; | |
364 | for (frame = 0; frame < frame_count; frame++) | |
365 | for (channel = 0; | |
366 | channel < rt->out_n_analog; | |
367 | channel++) { | |
368 | dest += 3; /* skip sample data */ | |
369 | *(dest++) = 0x40; | |
370 | } | |
371 | } | |
372 | usb_submit_urb(&out_urb->instance, GFP_ATOMIC); | |
373 | usb_submit_urb(&in_urb->instance, GFP_ATOMIC); | |
374 | } | |
375 | ||
376 | static void usb6fire_pcm_out_urb_handler(struct urb *usb_urb) | |
377 | { | |
378 | struct pcm_urb *urb = usb_urb->context; | |
379 | struct pcm_runtime *rt = urb->chip->pcm; | |
380 | ||
381 | if (rt->stream_state == STREAM_STARTING) { | |
382 | rt->stream_wait_cond = true; | |
383 | wake_up(&rt->stream_wait_queue); | |
384 | } | |
385 | } | |
386 | ||
387 | static int usb6fire_pcm_open(struct snd_pcm_substream *alsa_sub) | |
388 | { | |
389 | struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub); | |
390 | struct pcm_substream *sub = NULL; | |
391 | struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime; | |
392 | ||
393 | if (rt->panic) | |
394 | return -EPIPE; | |
395 | ||
396 | mutex_lock(&rt->stream_mutex); | |
397 | alsa_rt->hw = pcm_hw; | |
398 | ||
399 | if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
0ec5258d | 400 | if (rt->rate < ARRAY_SIZE(rates)) |
c6d43ba8 TS |
401 | alsa_rt->hw.rates = rates_alsaid[rt->rate]; |
402 | alsa_rt->hw.channels_max = OUT_N_CHANNELS; | |
403 | sub = &rt->playback; | |
404 | } else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE) { | |
0ec5258d | 405 | if (rt->rate < ARRAY_SIZE(rates)) |
c6d43ba8 TS |
406 | alsa_rt->hw.rates = rates_alsaid[rt->rate]; |
407 | alsa_rt->hw.channels_max = IN_N_CHANNELS; | |
408 | sub = &rt->capture; | |
409 | } | |
410 | ||
411 | if (!sub) { | |
412 | mutex_unlock(&rt->stream_mutex); | |
413 | snd_printk(KERN_ERR PREFIX "invalid stream type.\n"); | |
414 | return -EINVAL; | |
415 | } | |
416 | ||
417 | sub->instance = alsa_sub; | |
418 | sub->active = false; | |
419 | mutex_unlock(&rt->stream_mutex); | |
420 | return 0; | |
421 | } | |
422 | ||
423 | static int usb6fire_pcm_close(struct snd_pcm_substream *alsa_sub) | |
424 | { | |
425 | struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub); | |
426 | struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub); | |
427 | unsigned long flags; | |
428 | ||
429 | if (rt->panic) | |
430 | return 0; | |
431 | ||
432 | mutex_lock(&rt->stream_mutex); | |
433 | if (sub) { | |
434 | /* deactivate substream */ | |
435 | spin_lock_irqsave(&sub->lock, flags); | |
436 | sub->instance = NULL; | |
437 | sub->active = false; | |
438 | spin_unlock_irqrestore(&sub->lock, flags); | |
439 | ||
440 | /* all substreams closed? if so, stop streaming */ | |
441 | if (!rt->playback.instance && !rt->capture.instance) { | |
442 | usb6fire_pcm_stream_stop(rt); | |
e220fa3b | 443 | rt->rate = ARRAY_SIZE(rates); |
c6d43ba8 TS |
444 | } |
445 | } | |
446 | mutex_unlock(&rt->stream_mutex); | |
447 | return 0; | |
448 | } | |
449 | ||
450 | static int usb6fire_pcm_hw_params(struct snd_pcm_substream *alsa_sub, | |
451 | struct snd_pcm_hw_params *hw_params) | |
452 | { | |
4a9f9118 AO |
453 | return snd_pcm_lib_alloc_vmalloc_buffer(alsa_sub, |
454 | params_buffer_bytes(hw_params)); | |
c6d43ba8 TS |
455 | } |
456 | ||
457 | static int usb6fire_pcm_hw_free(struct snd_pcm_substream *alsa_sub) | |
458 | { | |
4a9f9118 | 459 | return snd_pcm_lib_free_vmalloc_buffer(alsa_sub); |
c6d43ba8 TS |
460 | } |
461 | ||
462 | static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub) | |
463 | { | |
464 | struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub); | |
465 | struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub); | |
466 | struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime; | |
c6d43ba8 TS |
467 | int ret; |
468 | ||
469 | if (rt->panic) | |
470 | return -EPIPE; | |
471 | if (!sub) | |
472 | return -ENODEV; | |
473 | ||
474 | mutex_lock(&rt->stream_mutex); | |
475 | sub->dma_off = 0; | |
476 | sub->period_off = 0; | |
477 | ||
478 | if (rt->stream_state == STREAM_DISABLED) { | |
e220fa3b TS |
479 | for (rt->rate = 0; rt->rate < ARRAY_SIZE(rates); rt->rate++) |
480 | if (alsa_rt->rate == rates[rt->rate]) | |
c6d43ba8 | 481 | break; |
e220fa3b | 482 | if (rt->rate == ARRAY_SIZE(rates)) { |
c6d43ba8 TS |
483 | mutex_unlock(&rt->stream_mutex); |
484 | snd_printk("invalid rate %d in prepare.\n", | |
485 | alsa_rt->rate); | |
486 | return -EINVAL; | |
487 | } | |
488 | ||
489 | ret = usb6fire_pcm_set_rate(rt); | |
490 | if (ret) { | |
491 | mutex_unlock(&rt->stream_mutex); | |
492 | return ret; | |
493 | } | |
494 | ret = usb6fire_pcm_stream_start(rt); | |
495 | if (ret) { | |
496 | mutex_unlock(&rt->stream_mutex); | |
497 | snd_printk(KERN_ERR PREFIX | |
498 | "could not start pcm stream.\n"); | |
499 | return ret; | |
500 | } | |
501 | } | |
502 | mutex_unlock(&rt->stream_mutex); | |
503 | return 0; | |
504 | } | |
505 | ||
506 | static int usb6fire_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd) | |
507 | { | |
508 | struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub); | |
509 | struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub); | |
510 | unsigned long flags; | |
511 | ||
512 | if (rt->panic) | |
513 | return -EPIPE; | |
514 | if (!sub) | |
515 | return -ENODEV; | |
516 | ||
517 | switch (cmd) { | |
518 | case SNDRV_PCM_TRIGGER_START: | |
519 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
520 | spin_lock_irqsave(&sub->lock, flags); | |
521 | sub->active = true; | |
522 | spin_unlock_irqrestore(&sub->lock, flags); | |
523 | return 0; | |
524 | ||
525 | case SNDRV_PCM_TRIGGER_STOP: | |
526 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
527 | spin_lock_irqsave(&sub->lock, flags); | |
528 | sub->active = false; | |
529 | spin_unlock_irqrestore(&sub->lock, flags); | |
530 | return 0; | |
531 | ||
532 | default: | |
533 | return -EINVAL; | |
534 | } | |
535 | } | |
536 | ||
537 | static snd_pcm_uframes_t usb6fire_pcm_pointer( | |
538 | struct snd_pcm_substream *alsa_sub) | |
539 | { | |
540 | struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub); | |
541 | struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub); | |
542 | unsigned long flags; | |
543 | snd_pcm_uframes_t ret; | |
544 | ||
545 | if (rt->panic || !sub) | |
546 | return SNDRV_PCM_STATE_XRUN; | |
547 | ||
548 | spin_lock_irqsave(&sub->lock, flags); | |
549 | ret = sub->dma_off; | |
550 | spin_unlock_irqrestore(&sub->lock, flags); | |
551 | return ret; | |
552 | } | |
553 | ||
554 | static struct snd_pcm_ops pcm_ops = { | |
555 | .open = usb6fire_pcm_open, | |
556 | .close = usb6fire_pcm_close, | |
557 | .ioctl = snd_pcm_lib_ioctl, | |
558 | .hw_params = usb6fire_pcm_hw_params, | |
559 | .hw_free = usb6fire_pcm_hw_free, | |
560 | .prepare = usb6fire_pcm_prepare, | |
561 | .trigger = usb6fire_pcm_trigger, | |
562 | .pointer = usb6fire_pcm_pointer, | |
4a9f9118 AO |
563 | .page = snd_pcm_lib_get_vmalloc_page, |
564 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
c6d43ba8 TS |
565 | }; |
566 | ||
87f9796a BP |
567 | static void usb6fire_pcm_init_urb(struct pcm_urb *urb, |
568 | struct sfire_chip *chip, bool in, int ep, | |
569 | void (*handler)(struct urb *)) | |
c6d43ba8 TS |
570 | { |
571 | urb->chip = chip; | |
572 | usb_init_urb(&urb->instance); | |
573 | urb->instance.transfer_buffer = urb->buffer; | |
574 | urb->instance.transfer_buffer_length = | |
575 | PCM_N_PACKETS_PER_URB * PCM_MAX_PACKET_SIZE; | |
576 | urb->instance.dev = chip->dev; | |
577 | urb->instance.pipe = in ? usb_rcvisocpipe(chip->dev, ep) | |
578 | : usb_sndisocpipe(chip->dev, ep); | |
579 | urb->instance.interval = 1; | |
c6d43ba8 TS |
580 | urb->instance.complete = handler; |
581 | urb->instance.context = urb; | |
582 | urb->instance.number_of_packets = PCM_N_PACKETS_PER_URB; | |
583 | } | |
584 | ||
87f9796a | 585 | int usb6fire_pcm_init(struct sfire_chip *chip) |
c6d43ba8 TS |
586 | { |
587 | int i; | |
588 | int ret; | |
589 | struct snd_pcm *pcm; | |
590 | struct pcm_runtime *rt = | |
591 | kzalloc(sizeof(struct pcm_runtime), GFP_KERNEL); | |
592 | ||
593 | if (!rt) | |
594 | return -ENOMEM; | |
595 | ||
596 | rt->chip = chip; | |
597 | rt->stream_state = STREAM_DISABLED; | |
e220fa3b | 598 | rt->rate = ARRAY_SIZE(rates); |
c6d43ba8 TS |
599 | init_waitqueue_head(&rt->stream_wait_queue); |
600 | mutex_init(&rt->stream_mutex); | |
601 | ||
602 | spin_lock_init(&rt->playback.lock); | |
603 | spin_lock_init(&rt->capture.lock); | |
604 | ||
605 | for (i = 0; i < PCM_N_URBS; i++) { | |
606 | usb6fire_pcm_init_urb(&rt->in_urbs[i], chip, true, IN_EP, | |
607 | usb6fire_pcm_in_urb_handler); | |
608 | usb6fire_pcm_init_urb(&rt->out_urbs[i], chip, false, OUT_EP, | |
609 | usb6fire_pcm_out_urb_handler); | |
610 | ||
611 | rt->in_urbs[i].peer = &rt->out_urbs[i]; | |
612 | rt->out_urbs[i].peer = &rt->in_urbs[i]; | |
613 | } | |
614 | ||
615 | ret = snd_pcm_new(chip->card, "DMX6FireUSB", 0, 1, 1, &pcm); | |
616 | if (ret < 0) { | |
617 | kfree(rt); | |
618 | snd_printk(KERN_ERR PREFIX "cannot create pcm instance.\n"); | |
619 | return ret; | |
620 | } | |
621 | ||
622 | pcm->private_data = rt; | |
623 | strcpy(pcm->name, "DMX 6Fire USB"); | |
624 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops); | |
625 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops); | |
626 | ||
c6d43ba8 TS |
627 | if (ret) { |
628 | kfree(rt); | |
629 | snd_printk(KERN_ERR PREFIX | |
630 | "error preallocating pcm buffers.\n"); | |
631 | return ret; | |
632 | } | |
633 | rt->instance = pcm; | |
634 | ||
635 | chip->pcm = rt; | |
636 | return 0; | |
637 | } | |
638 | ||
639 | void usb6fire_pcm_abort(struct sfire_chip *chip) | |
640 | { | |
641 | struct pcm_runtime *rt = chip->pcm; | |
5b9ab3f7 | 642 | unsigned long flags; |
c6d43ba8 TS |
643 | int i; |
644 | ||
645 | if (rt) { | |
646 | rt->panic = true; | |
647 | ||
5b9ab3f7 TI |
648 | if (rt->playback.instance) { |
649 | snd_pcm_stream_lock_irqsave(rt->playback.instance, flags); | |
c6d43ba8 TS |
650 | snd_pcm_stop(rt->playback.instance, |
651 | SNDRV_PCM_STATE_XRUN); | |
5b9ab3f7 TI |
652 | snd_pcm_stream_unlock_irqrestore(rt->playback.instance, flags); |
653 | } | |
654 | ||
655 | if (rt->capture.instance) { | |
656 | snd_pcm_stream_lock_irqsave(rt->capture.instance, flags); | |
c6d43ba8 TS |
657 | snd_pcm_stop(rt->capture.instance, |
658 | SNDRV_PCM_STATE_XRUN); | |
5b9ab3f7 TI |
659 | snd_pcm_stream_unlock_irqrestore(rt->capture.instance, flags); |
660 | } | |
c6d43ba8 TS |
661 | |
662 | for (i = 0; i < PCM_N_URBS; i++) { | |
663 | usb_poison_urb(&rt->in_urbs[i].instance); | |
664 | usb_poison_urb(&rt->out_urbs[i].instance); | |
665 | } | |
666 | ||
667 | } | |
668 | } | |
669 | ||
670 | void usb6fire_pcm_destroy(struct sfire_chip *chip) | |
671 | { | |
672 | kfree(chip->pcm); | |
673 | chip->pcm = NULL; | |
674 | } |