2 * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese
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.
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.
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
19 #include <linux/device.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/usb.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
31 #define CLOCK_DRIFT_TOLERANCE 5
32 #define FRAMES_PER_URB 8
33 #define BYTES_PER_FRAME 512
34 #define CHANNELS_PER_STREAM 2
35 #define BYTES_PER_SAMPLE 3
36 #define BYTES_PER_SAMPLE_USB 4
37 #define MAX_BUFFER_SIZE (128*1024)
38 #define MAX_ENDPOINT_SIZE 512
40 #define ENDPOINT_CAPTURE 2
41 #define ENDPOINT_PLAYBACK 6
43 #define MAKE_CHECKBYTE(cdev,stream,i) \
44 (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
46 static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware
= {
47 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
48 SNDRV_PCM_INFO_BLOCK_TRANSFER
),
49 .formats
= SNDRV_PCM_FMTBIT_S24_3BE
,
50 .rates
= (SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
|
51 SNDRV_PCM_RATE_96000
),
53 .rate_max
= 0, /* will overwrite later */
54 .channels_min
= CHANNELS_PER_STREAM
,
55 .channels_max
= CHANNELS_PER_STREAM
,
56 .buffer_bytes_max
= MAX_BUFFER_SIZE
,
57 .period_bytes_min
= 128,
58 .period_bytes_max
= MAX_BUFFER_SIZE
,
64 activate_substream(struct snd_usb_caiaqdev
*cdev
,
65 struct snd_pcm_substream
*sub
)
67 spin_lock(&cdev
->spinlock
);
69 if (sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
70 cdev
->sub_playback
[sub
->number
] = sub
;
72 cdev
->sub_capture
[sub
->number
] = sub
;
74 spin_unlock(&cdev
->spinlock
);
78 deactivate_substream(struct snd_usb_caiaqdev
*cdev
,
79 struct snd_pcm_substream
*sub
)
82 spin_lock_irqsave(&cdev
->spinlock
, flags
);
84 if (sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
85 cdev
->sub_playback
[sub
->number
] = NULL
;
87 cdev
->sub_capture
[sub
->number
] = NULL
;
89 spin_unlock_irqrestore(&cdev
->spinlock
, flags
);
93 all_substreams_zero(struct snd_pcm_substream
**subs
)
96 for (i
= 0; i
< MAX_STREAMS
; i
++)
102 static int stream_start(struct snd_usb_caiaqdev
*cdev
)
105 struct device
*dev
= caiaqdev_to_dev(cdev
);
107 dev_dbg(dev
, "%s(%p)\n", __func__
, cdev
);
112 memset(cdev
->sub_playback
, 0, sizeof(cdev
->sub_playback
));
113 memset(cdev
->sub_capture
, 0, sizeof(cdev
->sub_capture
));
114 cdev
->input_panic
= 0;
115 cdev
->output_panic
= 0;
116 cdev
->first_packet
= 4;
120 for (i
= 0; i
< N_URBS
; i
++) {
121 ret
= usb_submit_urb(cdev
->data_urbs_in
[i
], GFP_ATOMIC
);
123 dev_err(dev
, "unable to trigger read #%d! (ret %d)\n",
133 static void stream_stop(struct snd_usb_caiaqdev
*cdev
)
136 struct device
*dev
= caiaqdev_to_dev(cdev
);
138 dev_dbg(dev
, "%s(%p)\n", __func__
, cdev
);
139 if (!cdev
->streaming
)
144 for (i
= 0; i
< N_URBS
; i
++) {
145 usb_kill_urb(cdev
->data_urbs_in
[i
]);
147 if (test_bit(i
, &cdev
->outurb_active_mask
))
148 usb_kill_urb(cdev
->data_urbs_out
[i
]);
151 cdev
->outurb_active_mask
= 0;
154 static int snd_usb_caiaq_substream_open(struct snd_pcm_substream
*substream
)
156 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(substream
);
157 struct device
*dev
= caiaqdev_to_dev(cdev
);
159 dev_dbg(dev
, "%s(%p)\n", __func__
, substream
);
160 substream
->runtime
->hw
= cdev
->pcm_info
;
161 snd_pcm_limit_hw_rates(substream
->runtime
);
166 static int snd_usb_caiaq_substream_close(struct snd_pcm_substream
*substream
)
168 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(substream
);
169 struct device
*dev
= caiaqdev_to_dev(cdev
);
171 dev_dbg(dev
, "%s(%p)\n", __func__
, substream
);
172 if (all_substreams_zero(cdev
->sub_playback
) &&
173 all_substreams_zero(cdev
->sub_capture
)) {
174 /* when the last client has stopped streaming,
175 * all sample rates are allowed again */
177 cdev
->pcm_info
.rates
= cdev
->samplerates
;
183 static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream
*sub
,
184 struct snd_pcm_hw_params
*hw_params
)
186 return snd_pcm_lib_alloc_vmalloc_buffer(sub
,
187 params_buffer_bytes(hw_params
));
190 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream
*sub
)
192 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(sub
);
193 deactivate_substream(cdev
, sub
);
194 return snd_pcm_lib_free_vmalloc_buffer(sub
);
197 /* this should probably go upstream */
198 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
199 #error "Change this table"
202 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
203 48000, 64000, 88200, 96000, 176400, 192000 };
205 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream
*substream
)
207 int bytes_per_sample
, bpp
, ret
, i
;
208 int index
= substream
->number
;
209 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(substream
);
210 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
211 struct device
*dev
= caiaqdev_to_dev(cdev
);
213 dev_dbg(dev
, "%s(%p)\n", __func__
, substream
);
215 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
218 switch (cdev
->spec
.data_alignment
) {
221 out_pos
= BYTES_PER_SAMPLE
+ 1;
229 cdev
->period_out_count
[index
] = out_pos
;
230 cdev
->audio_out_buf_pos
[index
] = out_pos
;
234 switch (cdev
->spec
.data_alignment
) {
236 in_pos
= BYTES_PER_SAMPLE
+ 2;
239 in_pos
= BYTES_PER_SAMPLE
;
247 cdev
->period_in_count
[index
] = in_pos
;
248 cdev
->audio_in_buf_pos
[index
] = in_pos
;
254 /* the first client that opens a stream defines the sample rate
255 * setting for all subsequent calls, until the last client closed. */
256 for (i
=0; i
< ARRAY_SIZE(rates
); i
++)
257 if (runtime
->rate
== rates
[i
])
258 cdev
->pcm_info
.rates
= 1 << i
;
260 snd_pcm_limit_hw_rates(runtime
);
262 bytes_per_sample
= BYTES_PER_SAMPLE
;
263 if (cdev
->spec
.data_alignment
>= 2)
266 bpp
= ((runtime
->rate
/ 8000) + CLOCK_DRIFT_TOLERANCE
)
267 * bytes_per_sample
* CHANNELS_PER_STREAM
* cdev
->n_streams
;
269 if (bpp
> MAX_ENDPOINT_SIZE
)
270 bpp
= MAX_ENDPOINT_SIZE
;
272 ret
= snd_usb_caiaq_set_audio_params(cdev
, runtime
->rate
,
273 runtime
->sample_bits
, bpp
);
277 ret
= stream_start(cdev
);
281 cdev
->output_running
= 0;
282 wait_event_timeout(cdev
->prepare_wait_queue
, cdev
->output_running
, HZ
);
283 if (!cdev
->output_running
) {
291 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream
*sub
, int cmd
)
293 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(sub
);
294 struct device
*dev
= caiaqdev_to_dev(cdev
);
296 dev_dbg(dev
, "%s(%p) cmd %d\n", __func__
, sub
, cmd
);
299 case SNDRV_PCM_TRIGGER_START
:
300 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
301 activate_substream(cdev
, sub
);
303 case SNDRV_PCM_TRIGGER_STOP
:
304 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
305 deactivate_substream(cdev
, sub
);
314 static snd_pcm_uframes_t
315 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream
*sub
)
317 int index
= sub
->number
;
318 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(sub
);
319 snd_pcm_uframes_t ptr
;
321 spin_lock(&cdev
->spinlock
);
323 if (cdev
->input_panic
|| cdev
->output_panic
) {
324 ptr
= SNDRV_PCM_POS_XRUN
;
328 if (sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
329 ptr
= bytes_to_frames(sub
->runtime
,
330 cdev
->audio_out_buf_pos
[index
]);
332 ptr
= bytes_to_frames(sub
->runtime
,
333 cdev
->audio_in_buf_pos
[index
]);
336 spin_unlock(&cdev
->spinlock
);
340 /* operators for both playback and capture */
341 static struct snd_pcm_ops snd_usb_caiaq_ops
= {
342 .open
= snd_usb_caiaq_substream_open
,
343 .close
= snd_usb_caiaq_substream_close
,
344 .ioctl
= snd_pcm_lib_ioctl
,
345 .hw_params
= snd_usb_caiaq_pcm_hw_params
,
346 .hw_free
= snd_usb_caiaq_pcm_hw_free
,
347 .prepare
= snd_usb_caiaq_pcm_prepare
,
348 .trigger
= snd_usb_caiaq_pcm_trigger
,
349 .pointer
= snd_usb_caiaq_pcm_pointer
,
350 .page
= snd_pcm_lib_get_vmalloc_page
,
351 .mmap
= snd_pcm_lib_mmap_vmalloc
,
354 static void check_for_elapsed_periods(struct snd_usb_caiaqdev
*cdev
,
355 struct snd_pcm_substream
**subs
)
357 int stream
, pb
, *cnt
;
358 struct snd_pcm_substream
*sub
;
360 for (stream
= 0; stream
< cdev
->n_streams
; stream
++) {
365 pb
= snd_pcm_lib_period_bytes(sub
);
366 cnt
= (sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
367 &cdev
->period_out_count
[stream
] :
368 &cdev
->period_in_count
[stream
];
371 snd_pcm_period_elapsed(sub
);
377 static void read_in_urb_mode0(struct snd_usb_caiaqdev
*cdev
,
378 const struct urb
*urb
,
379 const struct usb_iso_packet_descriptor
*iso
)
381 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
382 struct snd_pcm_substream
*sub
;
385 if (all_substreams_zero(cdev
->sub_capture
))
388 for (i
= 0; i
< iso
->actual_length
;) {
389 for (stream
= 0; stream
< cdev
->n_streams
; stream
++, i
++) {
390 sub
= cdev
->sub_capture
[stream
];
392 struct snd_pcm_runtime
*rt
= sub
->runtime
;
393 char *audio_buf
= rt
->dma_area
;
394 int sz
= frames_to_bytes(rt
, rt
->buffer_size
);
395 audio_buf
[cdev
->audio_in_buf_pos
[stream
]++]
397 cdev
->period_in_count
[stream
]++;
398 if (cdev
->audio_in_buf_pos
[stream
] == sz
)
399 cdev
->audio_in_buf_pos
[stream
] = 0;
405 static void read_in_urb_mode2(struct snd_usb_caiaqdev
*cdev
,
406 const struct urb
*urb
,
407 const struct usb_iso_packet_descriptor
*iso
)
409 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
410 unsigned char check_byte
;
411 struct snd_pcm_substream
*sub
;
414 for (i
= 0; i
< iso
->actual_length
;) {
415 if (i
% (cdev
->n_streams
* BYTES_PER_SAMPLE_USB
) == 0) {
417 stream
< cdev
->n_streams
;
419 if (cdev
->first_packet
)
422 check_byte
= MAKE_CHECKBYTE(cdev
, stream
, i
);
424 if ((usb_buf
[i
] & 0x3f) != check_byte
)
425 cdev
->input_panic
= 1;
427 if (usb_buf
[i
] & 0x80)
428 cdev
->output_panic
= 1;
431 cdev
->first_packet
= 0;
433 for (stream
= 0; stream
< cdev
->n_streams
; stream
++, i
++) {
434 sub
= cdev
->sub_capture
[stream
];
435 if (cdev
->input_panic
)
439 struct snd_pcm_runtime
*rt
= sub
->runtime
;
440 char *audio_buf
= rt
->dma_area
;
441 int sz
= frames_to_bytes(rt
, rt
->buffer_size
);
442 audio_buf
[cdev
->audio_in_buf_pos
[stream
]++] =
444 cdev
->period_in_count
[stream
]++;
445 if (cdev
->audio_in_buf_pos
[stream
] == sz
)
446 cdev
->audio_in_buf_pos
[stream
] = 0;
452 static void read_in_urb_mode3(struct snd_usb_caiaqdev
*cdev
,
453 const struct urb
*urb
,
454 const struct usb_iso_packet_descriptor
*iso
)
456 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
457 struct device
*dev
= caiaqdev_to_dev(cdev
);
461 if (iso
->actual_length
% (BYTES_PER_SAMPLE_USB
* CHANNELS_PER_STREAM
))
464 for (i
= 0; i
< iso
->actual_length
;) {
465 for (stream
= 0; stream
< cdev
->n_streams
; stream
++) {
466 struct snd_pcm_substream
*sub
= cdev
->sub_capture
[stream
];
467 char *audio_buf
= NULL
;
470 if (sub
&& !cdev
->input_panic
) {
471 struct snd_pcm_runtime
*rt
= sub
->runtime
;
472 audio_buf
= rt
->dma_area
;
473 sz
= frames_to_bytes(rt
, rt
->buffer_size
);
476 for (c
= 0; c
< CHANNELS_PER_STREAM
; c
++) {
477 /* 3 audio data bytes, followed by 1 check byte */
479 for (n
= 0; n
< BYTES_PER_SAMPLE
; n
++) {
480 audio_buf
[cdev
->audio_in_buf_pos
[stream
]++] = usb_buf
[i
+n
];
482 if (cdev
->audio_in_buf_pos
[stream
] == sz
)
483 cdev
->audio_in_buf_pos
[stream
] = 0;
486 cdev
->period_in_count
[stream
] += BYTES_PER_SAMPLE
;
489 i
+= BYTES_PER_SAMPLE
;
491 if (usb_buf
[i
] != ((stream
<< 1) | c
) &&
492 !cdev
->first_packet
) {
493 if (!cdev
->input_panic
)
494 dev_warn(dev
, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
495 ((stream
<< 1) | c
), usb_buf
[i
], c
, stream
, i
);
496 cdev
->input_panic
= 1;
504 if (cdev
->first_packet
> 0)
505 cdev
->first_packet
--;
508 static void read_in_urb(struct snd_usb_caiaqdev
*cdev
,
509 const struct urb
*urb
,
510 const struct usb_iso_packet_descriptor
*iso
)
512 struct device
*dev
= caiaqdev_to_dev(cdev
);
514 if (!cdev
->streaming
)
517 if (iso
->actual_length
< cdev
->bpp
)
520 switch (cdev
->spec
.data_alignment
) {
522 read_in_urb_mode0(cdev
, urb
, iso
);
525 read_in_urb_mode2(cdev
, urb
, iso
);
528 read_in_urb_mode3(cdev
, urb
, iso
);
532 if ((cdev
->input_panic
|| cdev
->output_panic
) && !cdev
->warned
) {
533 dev_warn(dev
, "streaming error detected %s %s\n",
534 cdev
->input_panic
? "(input)" : "",
535 cdev
->output_panic
? "(output)" : "");
540 static void fill_out_urb_mode_0(struct snd_usb_caiaqdev
*cdev
,
542 const struct usb_iso_packet_descriptor
*iso
)
544 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
545 struct snd_pcm_substream
*sub
;
548 for (i
= 0; i
< iso
->length
;) {
549 for (stream
= 0; stream
< cdev
->n_streams
; stream
++, i
++) {
550 sub
= cdev
->sub_playback
[stream
];
552 struct snd_pcm_runtime
*rt
= sub
->runtime
;
553 char *audio_buf
= rt
->dma_area
;
554 int sz
= frames_to_bytes(rt
, rt
->buffer_size
);
556 audio_buf
[cdev
->audio_out_buf_pos
[stream
]];
557 cdev
->period_out_count
[stream
]++;
558 cdev
->audio_out_buf_pos
[stream
]++;
559 if (cdev
->audio_out_buf_pos
[stream
] == sz
)
560 cdev
->audio_out_buf_pos
[stream
] = 0;
565 /* fill in the check bytes */
566 if (cdev
->spec
.data_alignment
== 2 &&
567 i
% (cdev
->n_streams
* BYTES_PER_SAMPLE_USB
) ==
568 (cdev
->n_streams
* CHANNELS_PER_STREAM
))
569 for (stream
= 0; stream
< cdev
->n_streams
; stream
++, i
++)
570 usb_buf
[i
] = MAKE_CHECKBYTE(cdev
, stream
, i
);
574 static void fill_out_urb_mode_3(struct snd_usb_caiaqdev
*cdev
,
576 const struct usb_iso_packet_descriptor
*iso
)
578 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
581 for (i
= 0; i
< iso
->length
;) {
582 for (stream
= 0; stream
< cdev
->n_streams
; stream
++) {
583 struct snd_pcm_substream
*sub
= cdev
->sub_playback
[stream
];
584 char *audio_buf
= NULL
;
588 struct snd_pcm_runtime
*rt
= sub
->runtime
;
589 audio_buf
= rt
->dma_area
;
590 sz
= frames_to_bytes(rt
, rt
->buffer_size
);
593 for (c
= 0; c
< CHANNELS_PER_STREAM
; c
++) {
594 for (n
= 0; n
< BYTES_PER_SAMPLE
; n
++) {
596 usb_buf
[i
+n
] = audio_buf
[cdev
->audio_out_buf_pos
[stream
]++];
598 if (cdev
->audio_out_buf_pos
[stream
] == sz
)
599 cdev
->audio_out_buf_pos
[stream
] = 0;
606 cdev
->period_out_count
[stream
] += BYTES_PER_SAMPLE
;
608 i
+= BYTES_PER_SAMPLE
;
610 /* fill in the check byte pattern */
611 usb_buf
[i
++] = (stream
<< 1) | c
;
617 static inline void fill_out_urb(struct snd_usb_caiaqdev
*cdev
,
619 const struct usb_iso_packet_descriptor
*iso
)
621 switch (cdev
->spec
.data_alignment
) {
624 fill_out_urb_mode_0(cdev
, urb
, iso
);
627 fill_out_urb_mode_3(cdev
, urb
, iso
);
632 static void read_completed(struct urb
*urb
)
634 struct snd_usb_caiaq_cb_info
*info
= urb
->context
;
635 struct snd_usb_caiaqdev
*cdev
;
637 struct urb
*out
= NULL
;
638 int i
, frame
, len
, send_it
= 0, outframe
= 0;
641 if (urb
->status
|| !info
)
645 dev
= caiaqdev_to_dev(cdev
);
647 if (!cdev
->streaming
)
650 /* find an unused output urb that is unused */
651 for (i
= 0; i
< N_URBS
; i
++)
652 if (test_and_set_bit(i
, &cdev
->outurb_active_mask
) == 0) {
653 out
= cdev
->data_urbs_out
[i
];
658 dev_err(dev
, "Unable to find an output urb to use\n");
662 /* read the recently received packet and send back one which has
664 for (frame
= 0; frame
< FRAMES_PER_URB
; frame
++) {
665 if (urb
->iso_frame_desc
[frame
].status
)
668 len
= urb
->iso_frame_desc
[outframe
].actual_length
;
669 out
->iso_frame_desc
[outframe
].length
= len
;
670 out
->iso_frame_desc
[outframe
].actual_length
= 0;
671 out
->iso_frame_desc
[outframe
].offset
= offset
;
675 spin_lock(&cdev
->spinlock
);
676 fill_out_urb(cdev
, out
, &out
->iso_frame_desc
[outframe
]);
677 read_in_urb(cdev
, urb
, &urb
->iso_frame_desc
[frame
]);
678 spin_unlock(&cdev
->spinlock
);
679 check_for_elapsed_periods(cdev
, cdev
->sub_playback
);
680 check_for_elapsed_periods(cdev
, cdev
->sub_capture
);
688 out
->number_of_packets
= outframe
;
689 usb_submit_urb(out
, GFP_ATOMIC
);
691 struct snd_usb_caiaq_cb_info
*oinfo
= out
->context
;
692 clear_bit(oinfo
->index
, &cdev
->outurb_active_mask
);
696 /* re-submit inbound urb */
697 for (frame
= 0; frame
< FRAMES_PER_URB
; frame
++) {
698 urb
->iso_frame_desc
[frame
].offset
= BYTES_PER_FRAME
* frame
;
699 urb
->iso_frame_desc
[frame
].length
= BYTES_PER_FRAME
;
700 urb
->iso_frame_desc
[frame
].actual_length
= 0;
703 urb
->number_of_packets
= FRAMES_PER_URB
;
704 usb_submit_urb(urb
, GFP_ATOMIC
);
707 static void write_completed(struct urb
*urb
)
709 struct snd_usb_caiaq_cb_info
*info
= urb
->context
;
710 struct snd_usb_caiaqdev
*cdev
= info
->cdev
;
712 if (!cdev
->output_running
) {
713 cdev
->output_running
= 1;
714 wake_up(&cdev
->prepare_wait_queue
);
717 clear_bit(info
->index
, &cdev
->outurb_active_mask
);
720 static struct urb
**alloc_urbs(struct snd_usb_caiaqdev
*cdev
, int dir
, int *ret
)
724 struct usb_device
*usb_dev
= cdev
->chip
.dev
;
725 struct device
*dev
= caiaqdev_to_dev(cdev
);
728 pipe
= (dir
== SNDRV_PCM_STREAM_PLAYBACK
) ?
729 usb_sndisocpipe(usb_dev
, ENDPOINT_PLAYBACK
) :
730 usb_rcvisocpipe(usb_dev
, ENDPOINT_CAPTURE
);
732 urbs
= kmalloc(N_URBS
* sizeof(*urbs
), GFP_KERNEL
);
734 dev_err(dev
, "unable to kmalloc() urbs, OOM!?\n");
739 for (i
= 0; i
< N_URBS
; i
++) {
740 urbs
[i
] = usb_alloc_urb(FRAMES_PER_URB
, GFP_KERNEL
);
742 dev_err(dev
, "unable to usb_alloc_urb(), OOM!?\n");
747 urbs
[i
]->transfer_buffer
=
748 kmalloc(FRAMES_PER_URB
* BYTES_PER_FRAME
, GFP_KERNEL
);
749 if (!urbs
[i
]->transfer_buffer
) {
750 dev_err(dev
, "unable to kmalloc() transfer buffer, OOM!?\n");
755 for (frame
= 0; frame
< FRAMES_PER_URB
; frame
++) {
756 struct usb_iso_packet_descriptor
*iso
=
757 &urbs
[i
]->iso_frame_desc
[frame
];
759 iso
->offset
= BYTES_PER_FRAME
* frame
;
760 iso
->length
= BYTES_PER_FRAME
;
763 urbs
[i
]->dev
= usb_dev
;
764 urbs
[i
]->pipe
= pipe
;
765 urbs
[i
]->transfer_buffer_length
= FRAMES_PER_URB
767 urbs
[i
]->context
= &cdev
->data_cb_info
[i
];
768 urbs
[i
]->interval
= 1;
769 urbs
[i
]->number_of_packets
= FRAMES_PER_URB
;
770 urbs
[i
]->complete
= (dir
== SNDRV_PCM_STREAM_CAPTURE
) ?
771 read_completed
: write_completed
;
778 static void free_urbs(struct urb
**urbs
)
785 for (i
= 0; i
< N_URBS
; i
++) {
789 usb_kill_urb(urbs
[i
]);
790 kfree(urbs
[i
]->transfer_buffer
);
791 usb_free_urb(urbs
[i
]);
797 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev
*cdev
)
800 struct device
*dev
= caiaqdev_to_dev(cdev
);
802 cdev
->n_audio_in
= max(cdev
->spec
.num_analog_audio_in
,
803 cdev
->spec
.num_digital_audio_in
) /
805 cdev
->n_audio_out
= max(cdev
->spec
.num_analog_audio_out
,
806 cdev
->spec
.num_digital_audio_out
) /
808 cdev
->n_streams
= max(cdev
->n_audio_in
, cdev
->n_audio_out
);
810 dev_dbg(dev
, "cdev->n_audio_in = %d\n", cdev
->n_audio_in
);
811 dev_dbg(dev
, "cdev->n_audio_out = %d\n", cdev
->n_audio_out
);
812 dev_dbg(dev
, "cdev->n_streams = %d\n", cdev
->n_streams
);
814 if (cdev
->n_streams
> MAX_STREAMS
) {
815 dev_err(dev
, "unable to initialize device, too many streams.\n");
819 ret
= snd_pcm_new(cdev
->chip
.card
, cdev
->product_name
, 0,
820 cdev
->n_audio_out
, cdev
->n_audio_in
, &cdev
->pcm
);
823 dev_err(dev
, "snd_pcm_new() returned %d\n", ret
);
827 cdev
->pcm
->private_data
= cdev
;
828 strlcpy(cdev
->pcm
->name
, cdev
->product_name
, sizeof(cdev
->pcm
->name
));
830 memset(cdev
->sub_playback
, 0, sizeof(cdev
->sub_playback
));
831 memset(cdev
->sub_capture
, 0, sizeof(cdev
->sub_capture
));
833 memcpy(&cdev
->pcm_info
, &snd_usb_caiaq_pcm_hardware
,
834 sizeof(snd_usb_caiaq_pcm_hardware
));
836 /* setup samplerates */
837 cdev
->samplerates
= cdev
->pcm_info
.rates
;
838 switch (cdev
->chip
.usb_id
) {
839 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AK1
):
840 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_RIGKONTROL3
):
841 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_SESSIONIO
):
842 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_GUITARRIGMOBILE
):
843 cdev
->samplerates
|= SNDRV_PCM_RATE_192000
;
845 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO2DJ
):
846 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO4DJ
):
847 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO8DJ
):
848 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_TRAKTORAUDIO2
):
849 cdev
->samplerates
|= SNDRV_PCM_RATE_88200
;
853 snd_pcm_set_ops(cdev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
855 snd_pcm_set_ops(cdev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
,
859 kmalloc(sizeof(struct snd_usb_caiaq_cb_info
) * N_URBS
,
862 if (!cdev
->data_cb_info
)
865 cdev
->outurb_active_mask
= 0;
866 BUILD_BUG_ON(N_URBS
> (sizeof(cdev
->outurb_active_mask
) * 8));
868 for (i
= 0; i
< N_URBS
; i
++) {
869 cdev
->data_cb_info
[i
].cdev
= cdev
;
870 cdev
->data_cb_info
[i
].index
= i
;
873 cdev
->data_urbs_in
= alloc_urbs(cdev
, SNDRV_PCM_STREAM_CAPTURE
, &ret
);
875 kfree(cdev
->data_cb_info
);
876 free_urbs(cdev
->data_urbs_in
);
880 cdev
->data_urbs_out
= alloc_urbs(cdev
, SNDRV_PCM_STREAM_PLAYBACK
, &ret
);
882 kfree(cdev
->data_cb_info
);
883 free_urbs(cdev
->data_urbs_in
);
884 free_urbs(cdev
->data_urbs_out
);
891 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev
*cdev
)
893 struct device
*dev
= caiaqdev_to_dev(cdev
);
895 dev_dbg(dev
, "%s(%p)\n", __func__
, cdev
);
897 free_urbs(cdev
->data_urbs_in
);
898 free_urbs(cdev
->data_urbs_out
);
899 kfree(cdev
->data_cb_info
);