]>
Commit | Line | Data |
---|---|---|
523f1dce | 1 | /* |
8d048841 | 2 | * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese |
523f1dce DM |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
18 | ||
e431cf45 | 19 | #include <linux/spinlock.h> |
523f1dce | 20 | #include <linux/init.h> |
523f1dce | 21 | #include <linux/usb.h> |
523f1dce | 22 | #include <sound/core.h> |
523f1dce | 23 | #include <sound/pcm.h> |
523f1dce | 24 | |
936e7d03 DM |
25 | #include "device.h" |
26 | #include "audio.h" | |
523f1dce DM |
27 | |
28 | #define N_URBS 32 | |
29 | #define CLOCK_DRIFT_TOLERANCE 5 | |
30 | #define FRAMES_PER_URB 8 | |
31 | #define BYTES_PER_FRAME 512 | |
32 | #define CHANNELS_PER_STREAM 2 | |
33 | #define BYTES_PER_SAMPLE 3 | |
34 | #define BYTES_PER_SAMPLE_USB 4 | |
35 | #define MAX_BUFFER_SIZE (128*1024) | |
6e9fc6bd DM |
36 | #define MAX_ENDPOINT_SIZE 512 |
37 | ||
523f1dce DM |
38 | #define ENDPOINT_CAPTURE 2 |
39 | #define ENDPOINT_PLAYBACK 6 | |
40 | ||
41 | #define MAKE_CHECKBYTE(dev,stream,i) \ | |
42 | (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1) | |
43 | ||
44 | static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = { | |
45 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | | |
46 | SNDRV_PCM_INFO_BLOCK_TRANSFER), | |
47 | .formats = SNDRV_PCM_FMTBIT_S24_3BE, | |
48 | .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | | |
49 | SNDRV_PCM_RATE_96000), | |
50 | .rate_min = 44100, | |
51 | .rate_max = 0, /* will overwrite later */ | |
52 | .channels_min = CHANNELS_PER_STREAM, | |
53 | .channels_max = CHANNELS_PER_STREAM, | |
54 | .buffer_bytes_max = MAX_BUFFER_SIZE, | |
09189ac7 | 55 | .period_bytes_min = 128, |
523f1dce DM |
56 | .period_bytes_max = MAX_BUFFER_SIZE, |
57 | .periods_min = 1, | |
58 | .periods_max = 1024, | |
59 | }; | |
60 | ||
61 | static void | |
62 | activate_substream(struct snd_usb_caiaqdev *dev, | |
63 | struct snd_pcm_substream *sub) | |
64 | { | |
65 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
66 | dev->sub_playback[sub->number] = sub; | |
67 | else | |
68 | dev->sub_capture[sub->number] = sub; | |
69 | } | |
70 | ||
71 | static void | |
72 | deactivate_substream(struct snd_usb_caiaqdev *dev, | |
73 | struct snd_pcm_substream *sub) | |
74 | { | |
8d048841 DM |
75 | unsigned long flags; |
76 | spin_lock_irqsave(&dev->spinlock, flags); | |
77 | ||
523f1dce DM |
78 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
79 | dev->sub_playback[sub->number] = NULL; | |
80 | else | |
81 | dev->sub_capture[sub->number] = NULL; | |
8d048841 DM |
82 | |
83 | spin_unlock_irqrestore(&dev->spinlock, flags); | |
523f1dce DM |
84 | } |
85 | ||
86 | static int | |
87 | all_substreams_zero(struct snd_pcm_substream **subs) | |
88 | { | |
89 | int i; | |
90 | for (i = 0; i < MAX_STREAMS; i++) | |
91 | if (subs[i] != NULL) | |
92 | return 0; | |
93 | return 1; | |
94 | } | |
95 | ||
96 | static int stream_start(struct snd_usb_caiaqdev *dev) | |
97 | { | |
98 | int i, ret; | |
99 | ||
8d048841 DM |
100 | debug("%s(%p)\n", __func__, dev); |
101 | ||
102 | if (dev->streaming) | |
523f1dce | 103 | return -EINVAL; |
523f1dce | 104 | |
8d048841 DM |
105 | memset(dev->sub_playback, 0, sizeof(dev->sub_playback)); |
106 | memset(dev->sub_capture, 0, sizeof(dev->sub_capture)); | |
523f1dce DM |
107 | dev->input_panic = 0; |
108 | dev->output_panic = 0; | |
109 | dev->first_packet = 1; | |
110 | dev->streaming = 1; | |
1313e704 | 111 | dev->warned = 0; |
523f1dce DM |
112 | |
113 | for (i = 0; i < N_URBS; i++) { | |
114 | ret = usb_submit_urb(dev->data_urbs_in[i], GFP_ATOMIC); | |
115 | if (ret) { | |
8d048841 | 116 | log("unable to trigger read #%d! (ret %d)\n", i, ret); |
523f1dce | 117 | dev->streaming = 0; |
523f1dce DM |
118 | return -EPIPE; |
119 | } | |
120 | } | |
121 | ||
523f1dce DM |
122 | return 0; |
123 | } | |
124 | ||
125 | static void stream_stop(struct snd_usb_caiaqdev *dev) | |
126 | { | |
127 | int i; | |
8d048841 DM |
128 | |
129 | debug("%s(%p)\n", __func__, dev); | |
523f1dce DM |
130 | if (!dev->streaming) |
131 | return; | |
132 | ||
133 | dev->streaming = 0; | |
8d048841 | 134 | |
523f1dce | 135 | for (i = 0; i < N_URBS; i++) { |
8d048841 DM |
136 | usb_kill_urb(dev->data_urbs_in[i]); |
137 | usb_kill_urb(dev->data_urbs_out[i]); | |
523f1dce DM |
138 | } |
139 | } | |
140 | ||
141 | static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream) | |
142 | { | |
143 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); | |
8d048841 | 144 | debug("%s(%p)\n", __func__, substream); |
523f1dce DM |
145 | substream->runtime->hw = dev->pcm_info; |
146 | snd_pcm_limit_hw_rates(substream->runtime); | |
147 | return 0; | |
148 | } | |
149 | ||
150 | static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream) | |
151 | { | |
152 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); | |
153 | ||
8d048841 | 154 | debug("%s(%p)\n", __func__, substream); |
523f1dce DM |
155 | if (all_substreams_zero(dev->sub_playback) && |
156 | all_substreams_zero(dev->sub_capture)) { | |
157 | /* when the last client has stopped streaming, | |
158 | * all sample rates are allowed again */ | |
159 | stream_stop(dev); | |
160 | dev->pcm_info.rates = dev->samplerates; | |
161 | } | |
8d048841 | 162 | |
523f1dce DM |
163 | return 0; |
164 | } | |
165 | ||
166 | static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub, | |
167 | struct snd_pcm_hw_params *hw_params) | |
168 | { | |
8d048841 | 169 | debug("%s(%p)\n", __func__, sub); |
523f1dce DM |
170 | return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params)); |
171 | } | |
172 | ||
173 | static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub) | |
174 | { | |
175 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); | |
8d048841 | 176 | debug("%s(%p)\n", __func__, sub); |
523f1dce | 177 | deactivate_substream(dev, sub); |
523f1dce DM |
178 | return snd_pcm_lib_free_pages(sub); |
179 | } | |
180 | ||
181 | /* this should probably go upstream */ | |
182 | #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 | |
183 | #error "Change this table" | |
184 | #endif | |
185 | ||
186 | static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100, | |
187 | 48000, 64000, 88200, 96000, 176400, 192000 }; | |
188 | ||
189 | static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream) | |
190 | { | |
191 | int bytes_per_sample, bpp, ret, i; | |
192 | int index = substream->number; | |
193 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream); | |
194 | struct snd_pcm_runtime *runtime = substream->runtime; | |
195 | ||
8d048841 | 196 | debug("%s(%p)\n", __func__, substream); |
523f1dce DM |
197 | |
198 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
199 | dev->audio_out_buf_pos[index] = BYTES_PER_SAMPLE + 1; | |
200 | else | |
6849d49c | 201 | dev->audio_in_buf_pos[index] = BYTES_PER_SAMPLE; |
523f1dce DM |
202 | |
203 | if (dev->streaming) | |
204 | return 0; | |
205 | ||
206 | /* the first client that opens a stream defines the sample rate | |
207 | * setting for all subsequent calls, until the last client closed. */ | |
208 | for (i=0; i < ARRAY_SIZE(rates); i++) | |
209 | if (runtime->rate == rates[i]) | |
210 | dev->pcm_info.rates = 1 << i; | |
211 | ||
212 | snd_pcm_limit_hw_rates(runtime); | |
213 | ||
214 | bytes_per_sample = BYTES_PER_SAMPLE; | |
215 | if (dev->spec.data_alignment == 2) | |
216 | bytes_per_sample++; | |
217 | ||
218 | bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE) | |
219 | * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams; | |
6e9fc6bd DM |
220 | |
221 | if (bpp > MAX_ENDPOINT_SIZE) | |
222 | bpp = MAX_ENDPOINT_SIZE; | |
223 | ||
523f1dce DM |
224 | ret = snd_usb_caiaq_set_audio_params(dev, runtime->rate, |
225 | runtime->sample_bits, bpp); | |
226 | if (ret) | |
227 | return ret; | |
228 | ||
229 | ret = stream_start(dev); | |
230 | if (ret) | |
231 | return ret; | |
232 | ||
233 | dev->output_running = 0; | |
234 | wait_event_timeout(dev->prepare_wait_queue, dev->output_running, HZ); | |
235 | if (!dev->output_running) { | |
236 | stream_stop(dev); | |
237 | return -EPIPE; | |
238 | } | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
243 | static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd) | |
244 | { | |
245 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); | |
246 | ||
247 | switch (cmd) { | |
248 | case SNDRV_PCM_TRIGGER_START: | |
249 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
523f1dce | 250 | activate_substream(dev, sub); |
523f1dce DM |
251 | break; |
252 | case SNDRV_PCM_TRIGGER_STOP: | |
253 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
523f1dce | 254 | deactivate_substream(dev, sub); |
523f1dce DM |
255 | break; |
256 | default: | |
257 | return -EINVAL; | |
258 | } | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | static snd_pcm_uframes_t | |
264 | snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub) | |
265 | { | |
266 | int index = sub->number; | |
267 | struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub); | |
268 | ||
269 | if (dev->input_panic || dev->output_panic) | |
270 | return SNDRV_PCM_POS_XRUN; | |
271 | ||
272 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
273 | return bytes_to_frames(sub->runtime, | |
274 | dev->audio_out_buf_pos[index]); | |
275 | else | |
276 | return bytes_to_frames(sub->runtime, | |
277 | dev->audio_in_buf_pos[index]); | |
278 | } | |
279 | ||
280 | /* operators for both playback and capture */ | |
281 | static struct snd_pcm_ops snd_usb_caiaq_ops = { | |
282 | .open = snd_usb_caiaq_substream_open, | |
283 | .close = snd_usb_caiaq_substream_close, | |
284 | .ioctl = snd_pcm_lib_ioctl, | |
285 | .hw_params = snd_usb_caiaq_pcm_hw_params, | |
286 | .hw_free = snd_usb_caiaq_pcm_hw_free, | |
287 | .prepare = snd_usb_caiaq_pcm_prepare, | |
288 | .trigger = snd_usb_caiaq_pcm_trigger, | |
289 | .pointer = snd_usb_caiaq_pcm_pointer | |
290 | }; | |
291 | ||
292 | static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev, | |
293 | struct snd_pcm_substream **subs) | |
294 | { | |
295 | int stream, pb, *cnt; | |
296 | struct snd_pcm_substream *sub; | |
297 | ||
298 | for (stream = 0; stream < dev->n_streams; stream++) { | |
299 | sub = subs[stream]; | |
300 | if (!sub) | |
301 | continue; | |
302 | ||
303 | pb = frames_to_bytes(sub->runtime, | |
304 | sub->runtime->period_size); | |
305 | cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ? | |
306 | &dev->period_out_count[stream] : | |
307 | &dev->period_in_count[stream]; | |
308 | ||
309 | if (*cnt >= pb) { | |
310 | snd_pcm_period_elapsed(sub); | |
311 | *cnt %= pb; | |
312 | } | |
313 | } | |
314 | } | |
315 | ||
316 | static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev, | |
317 | const struct urb *urb, | |
318 | const struct usb_iso_packet_descriptor *iso) | |
319 | { | |
320 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; | |
321 | struct snd_pcm_substream *sub; | |
322 | int stream, i; | |
323 | ||
324 | if (all_substreams_zero(dev->sub_capture)) | |
325 | return; | |
326 | ||
523f1dce DM |
327 | for (i = 0; i < iso->actual_length;) { |
328 | for (stream = 0; stream < dev->n_streams; stream++, i++) { | |
329 | sub = dev->sub_capture[stream]; | |
330 | if (sub) { | |
331 | struct snd_pcm_runtime *rt = sub->runtime; | |
332 | char *audio_buf = rt->dma_area; | |
333 | int sz = frames_to_bytes(rt, rt->buffer_size); | |
334 | audio_buf[dev->audio_in_buf_pos[stream]++] | |
335 | = usb_buf[i]; | |
336 | dev->period_in_count[stream]++; | |
337 | if (dev->audio_in_buf_pos[stream] == sz) | |
338 | dev->audio_in_buf_pos[stream] = 0; | |
339 | } | |
340 | } | |
341 | } | |
523f1dce DM |
342 | } |
343 | ||
344 | static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev, | |
345 | const struct urb *urb, | |
346 | const struct usb_iso_packet_descriptor *iso) | |
347 | { | |
348 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; | |
349 | unsigned char check_byte; | |
350 | struct snd_pcm_substream *sub; | |
351 | int stream, i; | |
352 | ||
523f1dce DM |
353 | for (i = 0; i < iso->actual_length;) { |
354 | if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) { | |
355 | for (stream = 0; | |
356 | stream < dev->n_streams; | |
357 | stream++, i++) { | |
358 | if (dev->first_packet) | |
359 | continue; | |
360 | ||
361 | check_byte = MAKE_CHECKBYTE(dev, stream, i); | |
362 | ||
363 | if ((usb_buf[i] & 0x3f) != check_byte) | |
364 | dev->input_panic = 1; | |
365 | ||
366 | if (usb_buf[i] & 0x80) | |
367 | dev->output_panic = 1; | |
368 | } | |
369 | } | |
370 | dev->first_packet = 0; | |
371 | ||
372 | for (stream = 0; stream < dev->n_streams; stream++, i++) { | |
373 | sub = dev->sub_capture[stream]; | |
9311c9b4 DM |
374 | if (dev->input_panic) |
375 | usb_buf[i] = 0; | |
376 | ||
523f1dce DM |
377 | if (sub) { |
378 | struct snd_pcm_runtime *rt = sub->runtime; | |
379 | char *audio_buf = rt->dma_area; | |
380 | int sz = frames_to_bytes(rt, rt->buffer_size); | |
a971c3d4 KW |
381 | audio_buf[dev->audio_in_buf_pos[stream]++] = |
382 | usb_buf[i]; | |
523f1dce DM |
383 | dev->period_in_count[stream]++; |
384 | if (dev->audio_in_buf_pos[stream] == sz) | |
385 | dev->audio_in_buf_pos[stream] = 0; | |
386 | } | |
387 | } | |
388 | } | |
523f1dce DM |
389 | } |
390 | ||
391 | static void read_in_urb(struct snd_usb_caiaqdev *dev, | |
392 | const struct urb *urb, | |
393 | const struct usb_iso_packet_descriptor *iso) | |
394 | { | |
395 | if (!dev->streaming) | |
396 | return; | |
397 | ||
9311c9b4 DM |
398 | if (iso->actual_length < dev->bpp) |
399 | return; | |
400 | ||
523f1dce DM |
401 | switch (dev->spec.data_alignment) { |
402 | case 0: | |
403 | read_in_urb_mode0(dev, urb, iso); | |
404 | break; | |
405 | case 2: | |
406 | read_in_urb_mode2(dev, urb, iso); | |
407 | break; | |
408 | } | |
409 | ||
1313e704 | 410 | if ((dev->input_panic || dev->output_panic) && !dev->warned) { |
523f1dce DM |
411 | debug("streaming error detected %s %s\n", |
412 | dev->input_panic ? "(input)" : "", | |
413 | dev->output_panic ? "(output)" : ""); | |
1313e704 | 414 | dev->warned = 1; |
523f1dce | 415 | } |
523f1dce DM |
416 | } |
417 | ||
418 | static void fill_out_urb(struct snd_usb_caiaqdev *dev, | |
419 | struct urb *urb, | |
420 | const struct usb_iso_packet_descriptor *iso) | |
421 | { | |
422 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; | |
423 | struct snd_pcm_substream *sub; | |
424 | int stream, i; | |
523f1dce DM |
425 | |
426 | for (i = 0; i < iso->length;) { | |
a971c3d4 | 427 | for (stream = 0; stream < dev->n_streams; stream++, i++) { |
523f1dce DM |
428 | sub = dev->sub_playback[stream]; |
429 | if (sub) { | |
430 | struct snd_pcm_runtime *rt = sub->runtime; | |
431 | char *audio_buf = rt->dma_area; | |
432 | int sz = frames_to_bytes(rt, rt->buffer_size); | |
a971c3d4 KW |
433 | usb_buf[i] = |
434 | audio_buf[dev->audio_out_buf_pos[stream]]; | |
435 | dev->period_out_count[stream]++; | |
523f1dce DM |
436 | dev->audio_out_buf_pos[stream]++; |
437 | if (dev->audio_out_buf_pos[stream] == sz) | |
438 | dev->audio_out_buf_pos[stream] = 0; | |
439 | } else | |
a971c3d4 KW |
440 | usb_buf[i] = 0; |
441 | } | |
523f1dce DM |
442 | |
443 | /* fill in the check bytes */ | |
444 | if (dev->spec.data_alignment == 2 && | |
445 | i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == | |
446 | (dev->n_streams * CHANNELS_PER_STREAM)) | |
447 | for (stream = 0; stream < dev->n_streams; stream++, i++) | |
448 | usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i); | |
523f1dce | 449 | } |
523f1dce DM |
450 | } |
451 | ||
452 | static void read_completed(struct urb *urb) | |
453 | { | |
454 | struct snd_usb_caiaq_cb_info *info = urb->context; | |
455 | struct snd_usb_caiaqdev *dev; | |
456 | struct urb *out; | |
457 | int frame, len, send_it = 0, outframe = 0; | |
458 | ||
459 | if (urb->status || !info) | |
460 | return; | |
461 | ||
462 | dev = info->dev; | |
8d048841 | 463 | |
523f1dce DM |
464 | if (!dev->streaming) |
465 | return; | |
466 | ||
467 | out = dev->data_urbs_out[info->index]; | |
468 | ||
469 | /* read the recently received packet and send back one which has | |
470 | * the same layout */ | |
471 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { | |
472 | if (urb->iso_frame_desc[frame].status) | |
473 | continue; | |
474 | ||
475 | len = urb->iso_frame_desc[outframe].actual_length; | |
476 | out->iso_frame_desc[outframe].length = len; | |
477 | out->iso_frame_desc[outframe].actual_length = 0; | |
478 | out->iso_frame_desc[outframe].offset = BYTES_PER_FRAME * frame; | |
479 | ||
480 | if (len > 0) { | |
8d048841 | 481 | spin_lock(&dev->spinlock); |
523f1dce DM |
482 | fill_out_urb(dev, out, &out->iso_frame_desc[outframe]); |
483 | read_in_urb(dev, urb, &urb->iso_frame_desc[frame]); | |
8d048841 DM |
484 | spin_unlock(&dev->spinlock); |
485 | check_for_elapsed_periods(dev, dev->sub_playback); | |
486 | check_for_elapsed_periods(dev, dev->sub_capture); | |
523f1dce DM |
487 | send_it = 1; |
488 | } | |
489 | ||
490 | outframe++; | |
491 | } | |
492 | ||
493 | if (send_it) { | |
494 | out->number_of_packets = FRAMES_PER_URB; | |
495 | out->transfer_flags = URB_ISO_ASAP; | |
496 | usb_submit_urb(out, GFP_ATOMIC); | |
497 | } | |
498 | ||
499 | /* re-submit inbound urb */ | |
500 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { | |
501 | urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame; | |
502 | urb->iso_frame_desc[frame].length = BYTES_PER_FRAME; | |
503 | urb->iso_frame_desc[frame].actual_length = 0; | |
504 | } | |
505 | ||
506 | urb->number_of_packets = FRAMES_PER_URB; | |
507 | urb->transfer_flags = URB_ISO_ASAP; | |
508 | usb_submit_urb(urb, GFP_ATOMIC); | |
509 | } | |
510 | ||
511 | static void write_completed(struct urb *urb) | |
512 | { | |
513 | struct snd_usb_caiaq_cb_info *info = urb->context; | |
514 | struct snd_usb_caiaqdev *dev = info->dev; | |
515 | ||
516 | if (!dev->output_running) { | |
517 | dev->output_running = 1; | |
518 | wake_up(&dev->prepare_wait_queue); | |
519 | } | |
520 | } | |
521 | ||
522 | static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret) | |
523 | { | |
524 | int i, frame; | |
525 | struct urb **urbs; | |
526 | struct usb_device *usb_dev = dev->chip.dev; | |
527 | unsigned int pipe; | |
528 | ||
529 | pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ? | |
530 | usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) : | |
531 | usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE); | |
532 | ||
533 | urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL); | |
534 | if (!urbs) { | |
535 | log("unable to kmalloc() urbs, OOM!?\n"); | |
536 | *ret = -ENOMEM; | |
537 | return NULL; | |
538 | } | |
539 | ||
540 | for (i = 0; i < N_URBS; i++) { | |
541 | urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL); | |
542 | if (!urbs[i]) { | |
543 | log("unable to usb_alloc_urb(), OOM!?\n"); | |
544 | *ret = -ENOMEM; | |
545 | return urbs; | |
546 | } | |
547 | ||
548 | urbs[i]->transfer_buffer = | |
549 | kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL); | |
550 | if (!urbs[i]->transfer_buffer) { | |
551 | log("unable to kmalloc() transfer buffer, OOM!?\n"); | |
552 | *ret = -ENOMEM; | |
553 | return urbs; | |
554 | } | |
555 | ||
556 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { | |
557 | struct usb_iso_packet_descriptor *iso = | |
558 | &urbs[i]->iso_frame_desc[frame]; | |
559 | ||
560 | iso->offset = BYTES_PER_FRAME * frame; | |
561 | iso->length = BYTES_PER_FRAME; | |
562 | } | |
563 | ||
564 | urbs[i]->dev = usb_dev; | |
565 | urbs[i]->pipe = pipe; | |
566 | urbs[i]->transfer_buffer_length = FRAMES_PER_URB | |
567 | * BYTES_PER_FRAME; | |
568 | urbs[i]->context = &dev->data_cb_info[i]; | |
569 | urbs[i]->interval = 1; | |
570 | urbs[i]->transfer_flags = URB_ISO_ASAP; | |
571 | urbs[i]->number_of_packets = FRAMES_PER_URB; | |
572 | urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ? | |
573 | read_completed : write_completed; | |
574 | } | |
575 | ||
576 | *ret = 0; | |
577 | return urbs; | |
578 | } | |
579 | ||
580 | static void free_urbs(struct urb **urbs) | |
581 | { | |
582 | int i; | |
583 | ||
584 | if (!urbs) | |
585 | return; | |
586 | ||
587 | for (i = 0; i < N_URBS; i++) { | |
588 | if (!urbs[i]) | |
589 | continue; | |
590 | ||
591 | usb_kill_urb(urbs[i]); | |
592 | kfree(urbs[i]->transfer_buffer); | |
593 | usb_free_urb(urbs[i]); | |
594 | } | |
595 | ||
596 | kfree(urbs); | |
597 | } | |
598 | ||
599c3e76 | 599 | int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev) |
523f1dce DM |
600 | { |
601 | int i, ret; | |
602 | ||
603 | dev->n_audio_in = max(dev->spec.num_analog_audio_in, | |
604 | dev->spec.num_digital_audio_in) / | |
605 | CHANNELS_PER_STREAM; | |
606 | dev->n_audio_out = max(dev->spec.num_analog_audio_out, | |
607 | dev->spec.num_digital_audio_out) / | |
608 | CHANNELS_PER_STREAM; | |
609 | dev->n_streams = max(dev->n_audio_in, dev->n_audio_out); | |
610 | ||
611 | debug("dev->n_audio_in = %d\n", dev->n_audio_in); | |
612 | debug("dev->n_audio_out = %d\n", dev->n_audio_out); | |
613 | debug("dev->n_streams = %d\n", dev->n_streams); | |
614 | ||
615 | if (dev->n_streams > MAX_STREAMS) { | |
616 | log("unable to initialize device, too many streams.\n"); | |
617 | return -EINVAL; | |
618 | } | |
619 | ||
620 | ret = snd_pcm_new(dev->chip.card, dev->product_name, 0, | |
621 | dev->n_audio_out, dev->n_audio_in, &dev->pcm); | |
622 | ||
623 | if (ret < 0) { | |
624 | log("snd_pcm_new() returned %d\n", ret); | |
625 | return ret; | |
626 | } | |
627 | ||
628 | dev->pcm->private_data = dev; | |
629 | strcpy(dev->pcm->name, dev->product_name); | |
630 | ||
631 | memset(dev->sub_playback, 0, sizeof(dev->sub_playback)); | |
632 | memset(dev->sub_capture, 0, sizeof(dev->sub_capture)); | |
633 | ||
634 | memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware, | |
635 | sizeof(snd_usb_caiaq_pcm_hardware)); | |
636 | ||
637 | /* setup samplerates */ | |
638 | dev->samplerates = dev->pcm_info.rates; | |
639 | switch (dev->chip.usb_id) { | |
640 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): | |
ad1e34b5 | 641 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3): |
f3e9d5d1 | 642 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO): |
2165592b | 643 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE): |
523f1dce | 644 | dev->samplerates |= SNDRV_PCM_RATE_192000; |
2165592b DM |
645 | /* fall thru */ |
646 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ): | |
523f1dce DM |
647 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ): |
648 | dev->samplerates |= SNDRV_PCM_RATE_88200; | |
649 | break; | |
650 | } | |
651 | ||
652 | snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, | |
653 | &snd_usb_caiaq_ops); | |
654 | snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, | |
655 | &snd_usb_caiaq_ops); | |
656 | ||
657 | snd_pcm_lib_preallocate_pages_for_all(dev->pcm, | |
658 | SNDRV_DMA_TYPE_CONTINUOUS, | |
659 | snd_dma_continuous_data(GFP_KERNEL), | |
660 | MAX_BUFFER_SIZE, MAX_BUFFER_SIZE); | |
661 | ||
662 | dev->data_cb_info = | |
663 | kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS, | |
664 | GFP_KERNEL); | |
665 | ||
666 | if (!dev->data_cb_info) | |
667 | return -ENOMEM; | |
668 | ||
669 | for (i = 0; i < N_URBS; i++) { | |
670 | dev->data_cb_info[i].dev = dev; | |
671 | dev->data_cb_info[i].index = i; | |
672 | } | |
673 | ||
674 | dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret); | |
675 | if (ret < 0) { | |
676 | kfree(dev->data_cb_info); | |
677 | free_urbs(dev->data_urbs_in); | |
678 | return ret; | |
679 | } | |
680 | ||
681 | dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret); | |
682 | if (ret < 0) { | |
683 | kfree(dev->data_cb_info); | |
684 | free_urbs(dev->data_urbs_in); | |
685 | free_urbs(dev->data_urbs_out); | |
686 | return ret; | |
687 | } | |
688 | ||
689 | return 0; | |
690 | } | |
691 | ||
692 | void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev) | |
693 | { | |
8d048841 | 694 | debug("%s(%p)\n", __func__, dev); |
523f1dce DM |
695 | stream_stop(dev); |
696 | free_urbs(dev->data_urbs_in); | |
697 | free_urbs(dev->data_urbs_out); | |
698 | kfree(dev->data_cb_info); | |
699 | } | |
700 |