Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * US-X2Y AUDIO |
| 3 | * Copyright (c) 2002-2004 by Karsten Wiese |
| 4 | * |
| 5 | * based on |
| 6 | * |
| 7 | * (Tentative) USB Audio Driver for ALSA |
| 8 | * |
| 9 | * Main and PCM part |
| 10 | * |
| 11 | * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> |
| 12 | * |
| 13 | * Many codes borrowed from audio.c by |
| 14 | * Alan Cox (alan@lxorguk.ukuu.org.uk) |
| 15 | * Thomas Sailer (sailer@ife.ee.ethz.ch) |
| 16 | * |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or modify |
| 19 | * it under the terms of the GNU General Public License as published by |
| 20 | * the Free Software Foundation; either version 2 of the License, or |
| 21 | * (at your option) any later version. |
| 22 | * |
| 23 | * This program is distributed in the hope that it will be useful, |
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | * GNU General Public License for more details. |
| 27 | * |
| 28 | * You should have received a copy of the GNU General Public License |
| 29 | * along with this program; if not, write to the Free Software |
| 30 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 31 | */ |
| 32 | |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/usb.h> |
Paul Gortmaker | 31623ca | 2011-08-31 17:02:47 -0400 | [diff] [blame] | 37 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <sound/core.h> |
| 39 | #include <sound/info.h> |
| 40 | #include <sound/pcm.h> |
| 41 | #include <sound/pcm_params.h> |
| 42 | #include "usx2y.h" |
| 43 | #include "usbusx2y.h" |
| 44 | |
| 45 | #define USX2Y_NRPACKS 4 /* Default value used for nr of packs per urb. |
| 46 | 1 to 4 have been tested ok on uhci. |
| 47 | To use 3 on ohci, you'd need a patch: |
| 48 | look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on |
| 49 | "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425" |
| 50 | . |
| 51 | 1, 2 and 4 work out of the box on ohci, if I recall correctly. |
| 52 | Bigger is safer operation, |
| 53 | smaller gives lower latencies. |
| 54 | */ |
| 55 | #define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter |
| 56 | nrpacks set to 1, you might as well comment |
| 57 | this #define out, and thereby produce smaller, faster code. |
| 58 | You'd also set USX2Y_NRPACKS to 1 then. |
| 59 | */ |
| 60 | |
| 61 | #ifdef USX2Y_NRPACKS_VARIABLE |
| 62 | static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */ |
| 63 | #define nr_of_packs() nrpacks |
| 64 | module_param(nrpacks, int, 0444); |
| 65 | MODULE_PARM_DESC(nrpacks, "Number of packets per URB."); |
| 66 | #else |
| 67 | #define nr_of_packs() USX2Y_NRPACKS |
| 68 | #endif |
| 69 | |
| 70 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 71 | static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | struct urb *urb = subs->completed_urb; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 74 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | unsigned char *cp; |
| 76 | int i, len, lens = 0, hwptr_done = subs->hwptr_done; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 77 | struct usX2Ydev *usX2Y = subs->usX2Y; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | for (i = 0; i < nr_of_packs(); i++) { |
| 80 | cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
| 81 | if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 82 | snd_printk(KERN_ERR "active frame status %i. " |
Masanari Iida | 6e8d5d2 | 2012-02-15 00:38:55 +0900 | [diff] [blame] | 83 | "Most probably some hardware problem.\n", |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 84 | urb->iso_frame_desc[i].status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return urb->iso_frame_desc[i].status; |
| 86 | } |
| 87 | len = urb->iso_frame_desc[i].actual_length / usX2Y->stride; |
| 88 | if (! len) { |
| 89 | snd_printd("0 == len ERROR!\n"); |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | /* copy a data chunk */ |
| 94 | if ((hwptr_done + len) > runtime->buffer_size) { |
| 95 | int cnt = runtime->buffer_size - hwptr_done; |
| 96 | int blen = cnt * usX2Y->stride; |
| 97 | memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen); |
| 98 | memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen); |
| 99 | } else { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 100 | memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, |
| 101 | len * usX2Y->stride); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | lens += len; |
| 104 | if ((hwptr_done += len) >= runtime->buffer_size) |
| 105 | hwptr_done -= runtime->buffer_size; |
| 106 | } |
| 107 | |
| 108 | subs->hwptr_done = hwptr_done; |
| 109 | subs->transfer_done += lens; |
| 110 | /* update the pointer, call callback if necessary */ |
| 111 | if (subs->transfer_done >= runtime->period_size) { |
| 112 | subs->transfer_done -= runtime->period_size; |
| 113 | snd_pcm_period_elapsed(subs->pcm_substream); |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | /* |
| 118 | * prepare urb for playback data pipe |
| 119 | * |
| 120 | * we copy the data directly from the pcm buffer. |
| 121 | * the current position to be copied is held in hwptr field. |
| 122 | * since a urb can handle only a single linear buffer, if the total |
| 123 | * transferred area overflows the buffer boundary, we cannot send |
| 124 | * it directly from the buffer. thus the data is once copied to |
| 125 | * a temporary buffer and urb points to that. |
| 126 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 127 | static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream *subs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | struct urb *cap_urb, |
| 129 | struct urb *urb) |
| 130 | { |
| 131 | int count, counts, pack; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 132 | struct usX2Ydev *usX2Y = subs->usX2Y; |
| 133 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | count = 0; |
| 136 | for (pack = 0; pack < nr_of_packs(); pack++) { |
| 137 | /* calculate the size of a packet */ |
| 138 | counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride; |
| 139 | count += counts; |
| 140 | if (counts < 43 || counts > 50) { |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 141 | snd_printk(KERN_ERR "should not be here with counts=%i\n", counts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return -EPIPE; |
| 143 | } |
| 144 | /* set up descriptor */ |
| 145 | urb->iso_frame_desc[pack].offset = pack ? |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 146 | urb->iso_frame_desc[pack - 1].offset + |
| 147 | urb->iso_frame_desc[pack - 1].length : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 0; |
| 149 | urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length; |
| 150 | } |
| 151 | if (atomic_read(&subs->state) >= state_PRERUNNING) |
| 152 | if (subs->hwptr + count > runtime->buffer_size) { |
| 153 | /* err, the transferred area goes over buffer boundary. |
| 154 | * copy the data to the temp buffer. |
| 155 | */ |
| 156 | int len; |
| 157 | len = runtime->buffer_size - subs->hwptr; |
| 158 | urb->transfer_buffer = subs->tmpbuf; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 159 | memcpy(subs->tmpbuf, runtime->dma_area + |
| 160 | subs->hwptr * usX2Y->stride, len * usX2Y->stride); |
| 161 | memcpy(subs->tmpbuf + len * usX2Y->stride, |
| 162 | runtime->dma_area, (count - len) * usX2Y->stride); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | subs->hwptr += count; |
| 164 | subs->hwptr -= runtime->buffer_size; |
| 165 | } else { |
| 166 | /* set the buffer pointer */ |
| 167 | urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride; |
| 168 | if ((subs->hwptr += count) >= runtime->buffer_size) |
| 169 | subs->hwptr -= runtime->buffer_size; |
| 170 | } |
| 171 | else |
| 172 | urb->transfer_buffer = subs->tmpbuf; |
| 173 | urb->transfer_buffer_length = count * usX2Y->stride; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * process after playback data complete |
| 179 | * |
| 180 | * update the current position and call callback if a period is processed. |
| 181 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 182 | static void usX2Y_urb_play_retire(struct snd_usX2Y_substream *subs, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 184 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | int len = urb->actual_length / subs->usX2Y->stride; |
| 186 | |
| 187 | subs->transfer_done += len; |
| 188 | subs->hwptr_done += len; |
| 189 | if (subs->hwptr_done >= runtime->buffer_size) |
| 190 | subs->hwptr_done -= runtime->buffer_size; |
| 191 | if (subs->transfer_done >= runtime->period_size) { |
| 192 | subs->transfer_done -= runtime->period_size; |
| 193 | snd_pcm_period_elapsed(subs->pcm_substream); |
| 194 | } |
| 195 | } |
| 196 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 197 | static int usX2Y_urb_submit(struct snd_usX2Y_substream *subs, struct urb *urb, int frame) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | int err; |
| 200 | if (!urb) |
| 201 | return -ENODEV; |
| 202 | urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks |
| 203 | urb->hcpriv = NULL; |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 204 | urb->dev = subs->usX2Y->dev; /* we need to set this at each time */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 206 | snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return err; |
| 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 212 | static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream *capsubs, |
| 213 | struct snd_usX2Y_substream *playbacksubs, |
| 214 | int frame) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | int err, state; |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 217 | struct urb *urb = playbacksubs->completed_urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 219 | state = atomic_read(&playbacksubs->state); |
| 220 | if (NULL != urb) { |
| 221 | if (state == state_RUNNING) |
| 222 | usX2Y_urb_play_retire(playbacksubs, urb); |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 223 | else if (state >= state_PRERUNNING) |
| 224 | atomic_inc(&playbacksubs->state); |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 225 | } else { |
| 226 | switch (state) { |
| 227 | case state_STARTING1: |
| 228 | urb = playbacksubs->urb[0]; |
| 229 | atomic_inc(&playbacksubs->state); |
| 230 | break; |
| 231 | case state_STARTING2: |
| 232 | urb = playbacksubs->urb[1]; |
| 233 | atomic_inc(&playbacksubs->state); |
| 234 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 237 | if (urb) { |
| 238 | if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) || |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 239 | (err = usX2Y_urb_submit(playbacksubs, urb, frame))) { |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 240 | return err; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 241 | } |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | playbacksubs->completed_urb = NULL; |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | state = atomic_read(&capsubs->state); |
| 247 | if (state >= state_PREPARED) { |
| 248 | if (state == state_RUNNING) { |
| 249 | if ((err = usX2Y_urb_capt_retire(capsubs))) |
| 250 | return err; |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 251 | } else if (state >= state_PRERUNNING) |
| 252 | atomic_inc(&capsubs->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame))) |
| 254 | return err; |
| 255 | } |
| 256 | capsubs->completed_urb = NULL; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 261 | static void usX2Y_clients_stop(struct usX2Ydev *usX2Y) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
| 263 | int s, u; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | for (s = 0; s < 4; s++) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 266 | struct snd_usX2Y_substream *subs = usX2Y->subs[s]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (subs) { |
| 268 | snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state)); |
| 269 | atomic_set(&subs->state, state_STOPPED); |
| 270 | } |
| 271 | } |
| 272 | for (s = 0; s < 4; s++) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 273 | struct snd_usX2Y_substream *subs = usX2Y->subs[s]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | if (subs) { |
| 275 | if (atomic_read(&subs->state) >= state_PRERUNNING) { |
Takashi Iwai | 5be1efb | 2013-07-11 17:58:47 +0200 | [diff] [blame] | 276 | unsigned long flags; |
| 277 | |
| 278 | snd_pcm_stream_lock_irqsave(subs->pcm_substream, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); |
Takashi Iwai | 5be1efb | 2013-07-11 17:58:47 +0200 | [diff] [blame] | 280 | snd_pcm_stream_unlock_irqrestore(subs->pcm_substream, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | for (u = 0; u < NRURBS; u++) { |
| 283 | struct urb *urb = subs->urb[u]; |
| 284 | if (NULL != urb) |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 285 | snd_printdd("%i status=%i start_frame=%i\n", |
| 286 | u, urb->status, urb->start_frame); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
| 290 | usX2Y->prepare_subs = NULL; |
| 291 | wake_up(&usX2Y->prepare_wait_queue); |
| 292 | } |
| 293 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 294 | static void usX2Y_error_urb_status(struct usX2Ydev *usX2Y, |
| 295 | struct snd_usX2Y_substream *subs, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 297 | snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | urb->status = 0; |
| 299 | usX2Y_clients_stop(usX2Y); |
| 300 | } |
| 301 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 302 | static void usX2Y_error_sequence(struct usX2Ydev *usX2Y, |
| 303 | struct snd_usX2Y_substream *subs, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 305 | snd_printk(KERN_ERR |
| 306 | "Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).\n" |
Masanari Iida | 6e8d5d2 | 2012-02-15 00:38:55 +0900 | [diff] [blame] | 307 | "Most probably some urb of usb-frame %i is still missing.\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 308 | "Cause could be too long delays in usb-hcd interrupt handling.\n", |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 309 | usb_get_current_frame_number(usX2Y->dev), |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 310 | subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", |
| 311 | usX2Y->wait_iso_frame, urb->start_frame, usX2Y->wait_iso_frame); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | usX2Y_clients_stop(usX2Y); |
| 313 | } |
| 314 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 315 | static void i_usX2Y_urb_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 317 | struct snd_usX2Y_substream *subs = urb->context; |
| 318 | struct usX2Ydev *usX2Y = subs->usX2Y; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | if (unlikely(atomic_read(&subs->state) < state_PREPARED)) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 321 | snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 322 | usb_get_current_frame_number(usX2Y->dev), |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 323 | subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", |
| 324 | urb->status, urb->start_frame); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | if (unlikely(urb->status)) { |
| 328 | usX2Y_error_urb_status(usX2Y, subs, urb); |
| 329 | return; |
| 330 | } |
Karsten Wiese | bc6191b | 2007-01-10 19:02:26 +0100 | [diff] [blame] | 331 | if (likely((urb->start_frame & 0xFFFF) == (usX2Y->wait_iso_frame & 0xFFFF))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | subs->completed_urb = urb; |
| 333 | else { |
| 334 | usX2Y_error_sequence(usX2Y, subs, urb); |
| 335 | return; |
| 336 | } |
| 337 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 338 | struct snd_usX2Y_substream *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 340 | if (capsubs->completed_urb && |
| 341 | atomic_read(&capsubs->state) >= state_PREPARED && |
| 342 | (playbacksubs->completed_urb || |
| 343 | atomic_read(&playbacksubs->state) < state_PREPARED)) { |
Karsten Wiese | 635bbb3 | 2006-10-04 17:17:32 +0200 | [diff] [blame] | 344 | if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) |
| 345 | usX2Y->wait_iso_frame += nr_of_packs(); |
| 346 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | snd_printdd("\n"); |
| 348 | usX2Y_clients_stop(usX2Y); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 354 | static void usX2Y_urbs_set_complete(struct usX2Ydev * usX2Y, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 355 | void (*complete)(struct urb *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
| 357 | int s, u; |
| 358 | for (s = 0; s < 4; s++) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 359 | struct snd_usX2Y_substream *subs = usX2Y->subs[s]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | if (NULL != subs) |
| 361 | for (u = 0; u < NRURBS; u++) { |
| 362 | struct urb * urb = subs->urb[u]; |
| 363 | if (NULL != urb) |
| 364 | urb->complete = complete; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 369 | static void usX2Y_subs_startup_finish(struct usX2Ydev * usX2Y) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete); |
| 372 | usX2Y->prepare_subs = NULL; |
| 373 | } |
| 374 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 375 | static void i_usX2Y_subs_startup(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 377 | struct snd_usX2Y_substream *subs = urb->context; |
| 378 | struct usX2Ydev *usX2Y = subs->usX2Y; |
| 379 | struct snd_usX2Y_substream *prepare_subs = usX2Y->prepare_subs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (NULL != prepare_subs) |
| 381 | if (urb->start_frame == prepare_subs->urb[0]->start_frame) { |
| 382 | usX2Y_subs_startup_finish(usX2Y); |
| 383 | atomic_inc(&prepare_subs->state); |
| 384 | wake_up(&usX2Y->prepare_wait_queue); |
| 385 | } |
| 386 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 387 | i_usX2Y_urb_complete(urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 390 | static void usX2Y_subs_prepare(struct snd_usX2Y_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 392 | snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n", |
| 393 | subs, subs->endpoint, subs->urb[0], subs->urb[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | /* reset the pointer */ |
| 395 | subs->hwptr = 0; |
| 396 | subs->hwptr_done = 0; |
| 397 | subs->transfer_done = 0; |
| 398 | } |
| 399 | |
| 400 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 401 | static void usX2Y_urb_release(struct urb **urb, int free_tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
| 403 | if (*urb) { |
| 404 | usb_kill_urb(*urb); |
| 405 | if (free_tb) |
| 406 | kfree((*urb)->transfer_buffer); |
| 407 | usb_free_urb(*urb); |
| 408 | *urb = NULL; |
| 409 | } |
| 410 | } |
| 411 | /* |
| 412 | * release a substreams urbs |
| 413 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 414 | static void usX2Y_urbs_release(struct snd_usX2Y_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
| 416 | int i; |
| 417 | snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint); |
| 418 | for (i = 0; i < NRURBS; i++) |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 419 | usX2Y_urb_release(subs->urb + i, |
| 420 | subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Jesper Juhl | 4d57277 | 2005-05-30 17:30:32 +0200 | [diff] [blame] | 422 | kfree(subs->tmpbuf); |
| 423 | subs->tmpbuf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | /* |
| 426 | * initialize a substream's urbs |
| 427 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 428 | static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
| 430 | int i; |
| 431 | unsigned int pipe; |
| 432 | int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 433 | struct usb_device *dev = subs->usX2Y->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) : |
| 436 | usb_rcvisocpipe(dev, subs->endpoint); |
| 437 | subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback); |
| 438 | if (!subs->maxpacksize) |
| 439 | return -EINVAL; |
| 440 | |
| 441 | if (is_playback && NULL == subs->tmpbuf) { /* allocate a temporary buffer for playback */ |
| 442 | subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL); |
| 443 | if (NULL == subs->tmpbuf) { |
| 444 | snd_printk(KERN_ERR "cannot malloc tmpbuf\n"); |
| 445 | return -ENOMEM; |
| 446 | } |
| 447 | } |
| 448 | /* allocate and initialize data urbs */ |
| 449 | for (i = 0; i < NRURBS; i++) { |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 450 | struct urb **purb = subs->urb + i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | if (*purb) { |
| 452 | usb_kill_urb(*purb); |
| 453 | continue; |
| 454 | } |
| 455 | *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL); |
| 456 | if (NULL == *purb) { |
| 457 | usX2Y_urbs_release(subs); |
| 458 | return -ENOMEM; |
| 459 | } |
| 460 | if (!is_playback && !(*purb)->transfer_buffer) { |
| 461 | /* allocate a capture buffer per urb */ |
| 462 | (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL); |
| 463 | if (NULL == (*purb)->transfer_buffer) { |
| 464 | usX2Y_urbs_release(subs); |
| 465 | return -ENOMEM; |
| 466 | } |
| 467 | } |
| 468 | (*purb)->dev = dev; |
| 469 | (*purb)->pipe = pipe; |
| 470 | (*purb)->number_of_packets = nr_of_packs(); |
| 471 | (*purb)->context = subs; |
| 472 | (*purb)->interval = 1; |
| 473 | (*purb)->complete = i_usX2Y_subs_startup; |
| 474 | } |
| 475 | return 0; |
| 476 | } |
| 477 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 478 | static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 480 | struct usX2Ydev *usX2Y = subs->usX2Y; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | usX2Y->prepare_subs = subs; |
| 482 | subs->urb[0]->start_frame = -1; |
| 483 | wmb(); |
| 484 | usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup); |
| 485 | } |
| 486 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 487 | static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 489 | int i, err; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 490 | struct usX2Ydev *usX2Y = subs->usX2Y; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | if ((err = usX2Y_urbs_allocate(subs)) < 0) |
| 493 | return err; |
| 494 | subs->completed_urb = NULL; |
| 495 | for (i = 0; i < 4; i++) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 496 | struct snd_usX2Y_substream *subs = usX2Y->subs[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED) |
| 498 | goto start; |
| 499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 501 | start: |
| 502 | usX2Y_subs_startup(subs); |
| 503 | for (i = 0; i < NRURBS; i++) { |
| 504 | struct urb *urb = subs->urb[i]; |
| 505 | if (usb_pipein(urb->pipe)) { |
| 506 | unsigned long pack; |
| 507 | if (0 == i) |
| 508 | atomic_set(&subs->state, state_STARTING3); |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 509 | urb->dev = usX2Y->dev; |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 510 | for (pack = 0; pack < nr_of_packs(); pack++) { |
| 511 | urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack; |
| 512 | urb->iso_frame_desc[pack].length = subs->maxpacksize; |
| 513 | } |
| 514 | urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); |
| 515 | if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 516 | snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err); |
| 517 | err = -EPIPE; |
| 518 | goto cleanup; |
Karsten Wiese | 635bbb3 | 2006-10-04 17:17:32 +0200 | [diff] [blame] | 519 | } else |
| 520 | if (i == 0) |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 521 | usX2Y->wait_iso_frame = urb->start_frame; |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 522 | urb->transfer_flags = 0; |
| 523 | } else { |
| 524 | atomic_set(&subs->state, state_STARTING1); |
| 525 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 528 | err = 0; |
| 529 | wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs); |
| 530 | if (atomic_read(&subs->state) != state_PREPARED) |
| 531 | err = -EPIPE; |
| 532 | |
| 533 | cleanup: |
| 534 | if (err) { |
| 535 | usX2Y_subs_startup_finish(usX2Y); |
| 536 | usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything |
| 537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | return err; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * return the current pcm pointer. just return the hwptr_done value. |
| 543 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 544 | static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 546 | struct snd_usX2Y_substream *subs = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return subs->hwptr_done; |
| 548 | } |
| 549 | /* |
| 550 | * start/stop substream |
| 551 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 552 | static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 554 | struct snd_usX2Y_substream *subs = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | switch (cmd) { |
| 557 | case SNDRV_PCM_TRIGGER_START: |
| 558 | snd_printdd("snd_usX2Y_pcm_trigger(START)\n"); |
| 559 | if (atomic_read(&subs->state) == state_PREPARED && |
| 560 | atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) { |
| 561 | atomic_set(&subs->state, state_PRERUNNING); |
| 562 | } else { |
| 563 | snd_printdd("\n"); |
| 564 | return -EPIPE; |
| 565 | } |
| 566 | break; |
| 567 | case SNDRV_PCM_TRIGGER_STOP: |
| 568 | snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n"); |
| 569 | if (atomic_read(&subs->state) >= state_PRERUNNING) |
| 570 | atomic_set(&subs->state, state_PREPARED); |
| 571 | break; |
| 572 | default: |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | /* |
| 580 | * allocate a buffer, setup samplerate |
| 581 | * |
| 582 | * so far we use a physically linear buffer although packetize transfer |
| 583 | * doesn't need a continuous area. |
| 584 | * if sg buffer is supported on the later version of alsa, we'll follow |
| 585 | * that. |
| 586 | */ |
| 587 | static struct s_c2 |
| 588 | { |
| 589 | char c1, c2; |
| 590 | } |
| 591 | SetRate44100[] = |
| 592 | { |
| 593 | { 0x14, 0x08}, // this line sets 44100, well actually a little less |
| 594 | { 0x18, 0x40}, // only tascam / frontier design knows the further lines ....... |
| 595 | { 0x18, 0x42}, |
| 596 | { 0x18, 0x45}, |
| 597 | { 0x18, 0x46}, |
| 598 | { 0x18, 0x48}, |
| 599 | { 0x18, 0x4A}, |
| 600 | { 0x18, 0x4C}, |
| 601 | { 0x18, 0x4E}, |
| 602 | { 0x18, 0x50}, |
| 603 | { 0x18, 0x52}, |
| 604 | { 0x18, 0x54}, |
| 605 | { 0x18, 0x56}, |
| 606 | { 0x18, 0x58}, |
| 607 | { 0x18, 0x5A}, |
| 608 | { 0x18, 0x5C}, |
| 609 | { 0x18, 0x5E}, |
| 610 | { 0x18, 0x60}, |
| 611 | { 0x18, 0x62}, |
| 612 | { 0x18, 0x64}, |
| 613 | { 0x18, 0x66}, |
| 614 | { 0x18, 0x68}, |
| 615 | { 0x18, 0x6A}, |
| 616 | { 0x18, 0x6C}, |
| 617 | { 0x18, 0x6E}, |
| 618 | { 0x18, 0x70}, |
| 619 | { 0x18, 0x72}, |
| 620 | { 0x18, 0x74}, |
| 621 | { 0x18, 0x76}, |
| 622 | { 0x18, 0x78}, |
| 623 | { 0x18, 0x7A}, |
| 624 | { 0x18, 0x7C}, |
| 625 | { 0x18, 0x7E} |
| 626 | }; |
| 627 | static struct s_c2 SetRate48000[] = |
| 628 | { |
| 629 | { 0x14, 0x09}, // this line sets 48000, well actually a little less |
| 630 | { 0x18, 0x40}, // only tascam / frontier design knows the further lines ....... |
| 631 | { 0x18, 0x42}, |
| 632 | { 0x18, 0x45}, |
| 633 | { 0x18, 0x46}, |
| 634 | { 0x18, 0x48}, |
| 635 | { 0x18, 0x4A}, |
| 636 | { 0x18, 0x4C}, |
| 637 | { 0x18, 0x4E}, |
| 638 | { 0x18, 0x50}, |
| 639 | { 0x18, 0x52}, |
| 640 | { 0x18, 0x54}, |
| 641 | { 0x18, 0x56}, |
| 642 | { 0x18, 0x58}, |
| 643 | { 0x18, 0x5A}, |
| 644 | { 0x18, 0x5C}, |
| 645 | { 0x18, 0x5E}, |
| 646 | { 0x18, 0x60}, |
| 647 | { 0x18, 0x62}, |
| 648 | { 0x18, 0x64}, |
| 649 | { 0x18, 0x66}, |
| 650 | { 0x18, 0x68}, |
| 651 | { 0x18, 0x6A}, |
| 652 | { 0x18, 0x6C}, |
| 653 | { 0x18, 0x6E}, |
| 654 | { 0x18, 0x70}, |
| 655 | { 0x18, 0x73}, |
| 656 | { 0x18, 0x74}, |
| 657 | { 0x18, 0x76}, |
| 658 | { 0x18, 0x78}, |
| 659 | { 0x18, 0x7A}, |
| 660 | { 0x18, 0x7C}, |
| 661 | { 0x18, 0x7E} |
| 662 | }; |
| 663 | #define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000) |
| 664 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 665 | static void i_usX2Y_04Int(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 667 | struct usX2Ydev *usX2Y = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 669 | if (urb->status) |
| 670 | snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | if (0 == --usX2Y->US04->len) |
| 672 | wake_up(&usX2Y->In04WaitQueue); |
| 673 | } |
| 674 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 675 | static int usX2Y_rate_set(struct usX2Ydev *usX2Y, int rate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
| 677 | int err = 0, i; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 678 | struct snd_usX2Y_urbSeq *us = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | int *usbdata = NULL; |
| 680 | struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100; |
| 681 | |
| 682 | if (usX2Y->rate != rate) { |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 683 | us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | if (NULL == us) { |
| 685 | err = -ENOMEM; |
| 686 | goto cleanup; |
| 687 | } |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 688 | usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | if (NULL == usbdata) { |
| 690 | err = -ENOMEM; |
| 691 | goto cleanup; |
| 692 | } |
| 693 | for (i = 0; i < NOOF_SETRATE_URBS; ++i) { |
| 694 | if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) { |
| 695 | err = -ENOMEM; |
| 696 | goto cleanup; |
| 697 | } |
| 698 | ((char*)(usbdata + i))[0] = ra[i].c1; |
| 699 | ((char*)(usbdata + i))[1] = ra[i].c2; |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 700 | usb_fill_bulk_urb(us->urb[i], usX2Y->dev, usb_sndbulkpipe(usX2Y->dev, 4), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | usbdata + i, 2, i_usX2Y_04Int, usX2Y); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } |
| 703 | us->submitted = 0; |
| 704 | us->len = NOOF_SETRATE_URBS; |
| 705 | usX2Y->US04 = us; |
| 706 | wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ); |
| 707 | usX2Y->US04 = NULL; |
| 708 | if (us->len) |
| 709 | err = -ENODEV; |
| 710 | cleanup: |
| 711 | if (us) { |
| 712 | us->submitted = 2*NOOF_SETRATE_URBS; |
| 713 | for (i = 0; i < NOOF_SETRATE_URBS; ++i) { |
| 714 | struct urb *urb = us->urb[i]; |
| 715 | if (urb->status) { |
| 716 | if (!err) |
| 717 | err = -ENODEV; |
| 718 | usb_kill_urb(urb); |
| 719 | } |
| 720 | usb_free_urb(urb); |
| 721 | } |
| 722 | usX2Y->US04 = NULL; |
| 723 | kfree(usbdata); |
| 724 | kfree(us); |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 725 | if (!err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | usX2Y->rate = rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | return err; |
| 731 | } |
| 732 | |
| 733 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 734 | static int usX2Y_format_set(struct usX2Ydev *usX2Y, snd_pcm_format_t format) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | { |
| 736 | int alternate, err; |
| 737 | struct list_head* p; |
| 738 | if (format == SNDRV_PCM_FORMAT_S24_3LE) { |
| 739 | alternate = 2; |
| 740 | usX2Y->stride = 6; |
| 741 | } else { |
| 742 | alternate = 1; |
| 743 | usX2Y->stride = 4; |
| 744 | } |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 745 | list_for_each(p, &usX2Y->midi_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | snd_usbmidi_input_stop(p); |
| 747 | } |
| 748 | usb_kill_urb(usX2Y->In04urb); |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 749 | if ((err = usb_set_interface(usX2Y->dev, 0, alternate))) { |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 750 | snd_printk(KERN_ERR "usb_set_interface error \n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | return err; |
| 752 | } |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 753 | usX2Y->In04urb->dev = usX2Y->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL); |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 755 | list_for_each(p, &usX2Y->midi_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | snd_usbmidi_input_start(p); |
| 757 | } |
| 758 | usX2Y->format = format; |
| 759 | usX2Y->rate = 0; |
| 760 | return err; |
| 761 | } |
| 762 | |
| 763 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 764 | static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream *substream, |
| 765 | struct snd_pcm_hw_params *hw_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | { |
| 767 | int err = 0; |
| 768 | unsigned int rate = params_rate(hw_params); |
| 769 | snd_pcm_format_t format = params_format(hw_params); |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 770 | struct snd_card *card = substream->pstr->pcm->card; |
| 771 | struct list_head *list; |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 772 | |
| 773 | snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params); |
| 774 | // all pcm substreams off one usX2Y have to operate at the same rate & format |
| 775 | list_for_each(list, &card->devices) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 776 | struct snd_device *dev; |
| 777 | struct snd_pcm *pcm; |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 778 | int s; |
| 779 | dev = snd_device(list); |
| 780 | if (dev->type != SNDRV_DEV_PCM) |
| 781 | continue; |
| 782 | pcm = dev->device_data; |
| 783 | for (s = 0; s < 2; ++s) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 784 | struct snd_pcm_substream *test_substream; |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 785 | test_substream = pcm->streams[s].substream; |
| 786 | if (test_substream && test_substream != substream && |
| 787 | test_substream->runtime && |
| 788 | ((test_substream->runtime->format && |
| 789 | test_substream->runtime->format != format) || |
| 790 | (test_substream->runtime->rate && |
| 791 | test_substream->runtime->rate != rate))) |
| 792 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 796 | snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n", |
| 797 | substream, params_buffer_bytes(hw_params), err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return err; |
| 799 | } |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * free the buffer |
| 805 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 806 | static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 808 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 809 | struct snd_usX2Y_substream *subs = runtime->private_data; |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 810 | mutex_lock(&subs->usX2Y->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | snd_printdd("snd_usX2Y_hw_free(%p)\n", substream); |
| 812 | |
| 813 | if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 814 | struct snd_usX2Y_substream *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | atomic_set(&subs->state, state_STOPPED); |
| 816 | usX2Y_urbs_release(subs); |
| 817 | if (!cap_subs->pcm_substream || |
| 818 | !cap_subs->pcm_substream->runtime || |
| 819 | !cap_subs->pcm_substream->runtime->status || |
| 820 | cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) { |
| 821 | atomic_set(&cap_subs->state, state_STOPPED); |
| 822 | usX2Y_urbs_release(cap_subs); |
| 823 | } |
| 824 | } else { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 825 | struct snd_usX2Y_substream *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | if (atomic_read(&playback_subs->state) < state_PREPARED) { |
| 827 | atomic_set(&subs->state, state_STOPPED); |
| 828 | usX2Y_urbs_release(subs); |
| 829 | } |
| 830 | } |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 831 | mutex_unlock(&subs->usX2Y->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return snd_pcm_lib_free_pages(substream); |
| 833 | } |
| 834 | /* |
| 835 | * prepare callback |
| 836 | * |
| 837 | * set format and initialize urbs |
| 838 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 839 | static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 841 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 842 | struct snd_usX2Y_substream *subs = runtime->private_data; |
| 843 | struct usX2Ydev *usX2Y = subs->usX2Y; |
| 844 | struct snd_usX2Y_substream *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | int err = 0; |
| 846 | snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream); |
| 847 | |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 848 | mutex_lock(&usX2Y->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | usX2Y_subs_prepare(subs); |
| 850 | // Start hardware streams |
| 851 | // SyncStream first.... |
| 852 | if (atomic_read(&capsubs->state) < state_PREPARED) { |
| 853 | if (usX2Y->format != runtime->format) |
| 854 | if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0) |
| 855 | goto up_prepare_mutex; |
| 856 | if (usX2Y->rate != runtime->rate) |
| 857 | if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0) |
| 858 | goto up_prepare_mutex; |
| 859 | snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe"); |
| 860 | if (0 > (err = usX2Y_urbs_start(capsubs))) |
| 861 | goto up_prepare_mutex; |
| 862 | } |
| 863 | |
| 864 | if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED) |
| 865 | err = usX2Y_urbs_start(subs); |
| 866 | |
| 867 | up_prepare_mutex: |
Ingo Molnar | 12aa757 | 2006-01-16 16:36:05 +0100 | [diff] [blame] | 868 | mutex_unlock(&usX2Y->prepare_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | return err; |
| 870 | } |
| 871 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 872 | static struct snd_pcm_hardware snd_usX2Y_2c = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | { |
| 874 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | |
| 875 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
Takashi Iwai | 2008f13 | 2009-04-28 12:25:59 +0200 | [diff] [blame] | 876 | SNDRV_PCM_INFO_MMAP_VALID | |
| 877 | SNDRV_PCM_INFO_BATCH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE, |
| 879 | .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, |
| 880 | .rate_min = 44100, |
| 881 | .rate_max = 48000, |
| 882 | .channels_min = 2, |
| 883 | .channels_max = 2, |
| 884 | .buffer_bytes_max = (2*128*1024), |
| 885 | .period_bytes_min = 64, |
| 886 | .period_bytes_max = (128*1024), |
| 887 | .periods_min = 2, |
| 888 | .periods_max = 1024, |
| 889 | .fifo_size = 0 |
| 890 | }; |
| 891 | |
| 892 | |
| 893 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 894 | static int snd_usX2Y_pcm_open(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 896 | struct snd_usX2Y_substream *subs = ((struct snd_usX2Y_substream **) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | snd_pcm_substream_chip(substream))[substream->stream]; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 898 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
| 900 | if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS) |
| 901 | return -EBUSY; |
| 902 | |
| 903 | runtime->hw = snd_usX2Y_2c; |
| 904 | runtime->private_data = subs; |
| 905 | subs->pcm_substream = substream; |
| 906 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000); |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | |
| 911 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 912 | static int snd_usX2Y_pcm_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 914 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 915 | struct snd_usX2Y_substream *subs = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
| 917 | subs->pcm_substream = NULL; |
| 918 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 919 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 923 | static struct snd_pcm_ops snd_usX2Y_pcm_ops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | { |
| 925 | .open = snd_usX2Y_pcm_open, |
| 926 | .close = snd_usX2Y_pcm_close, |
| 927 | .ioctl = snd_pcm_lib_ioctl, |
| 928 | .hw_params = snd_usX2Y_pcm_hw_params, |
| 929 | .hw_free = snd_usX2Y_pcm_hw_free, |
| 930 | .prepare = snd_usX2Y_pcm_prepare, |
| 931 | .trigger = snd_usX2Y_pcm_trigger, |
| 932 | .pointer = snd_usX2Y_pcm_pointer, |
| 933 | }; |
| 934 | |
| 935 | |
| 936 | /* |
| 937 | * free a usb stream instance |
| 938 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 939 | static void usX2Y_audio_stream_free(struct snd_usX2Y_substream **usX2Y_substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | { |
Richard Knutsson | c111b8d | 2007-05-14 10:38:57 +0200 | [diff] [blame] | 941 | kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]); |
| 942 | usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL; |
| 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]); |
| 945 | usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL; |
| 946 | } |
| 947 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 948 | static void snd_usX2Y_pcm_private_free(struct snd_pcm *pcm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 950 | struct snd_usX2Y_substream **usX2Y_stream = pcm->private_data; |
Takashi Iwai | c3e6f7d | 2005-11-16 18:43:35 +0100 | [diff] [blame] | 951 | if (usX2Y_stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | usX2Y_audio_stream_free(usX2Y_stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 955 | static int usX2Y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 957 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | int err, i; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 959 | struct snd_usX2Y_substream **usX2Y_substream = |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 960 | usX2Y(card)->subs + 2 * usX2Y(card)->pcm_devs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; |
| 963 | i <= SNDRV_PCM_STREAM_CAPTURE; ++i) { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 964 | usX2Y_substream[i] = kzalloc(sizeof(struct snd_usX2Y_substream), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | if (NULL == usX2Y_substream[i]) { |
| 966 | snd_printk(KERN_ERR "cannot malloc\n"); |
| 967 | return -ENOMEM; |
| 968 | } |
| 969 | usX2Y_substream[i]->usX2Y = usX2Y(card); |
| 970 | } |
| 971 | |
| 972 | if (playback_endpoint) |
| 973 | usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint; |
| 974 | usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint; |
| 975 | |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 976 | err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->pcm_devs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | playback_endpoint ? 1 : 0, 1, |
| 978 | &pcm); |
| 979 | if (err < 0) { |
| 980 | usX2Y_audio_stream_free(usX2Y_substream); |
| 981 | return err; |
| 982 | } |
| 983 | |
| 984 | if (playback_endpoint) |
| 985 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops); |
| 986 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops); |
| 987 | |
| 988 | pcm->private_data = usX2Y_substream; |
| 989 | pcm->private_free = snd_usX2Y_pcm_private_free; |
| 990 | pcm->info_flags = 0; |
| 991 | |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 992 | sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->pcm_devs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | if ((playback_endpoint && |
| 995 | 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, |
| 996 | SNDRV_DMA_TYPE_CONTINUOUS, |
| 997 | snd_dma_continuous_data(GFP_KERNEL), |
| 998 | 64*1024, 128*1024))) || |
| 999 | 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, |
| 1000 | SNDRV_DMA_TYPE_CONTINUOUS, |
| 1001 | snd_dma_continuous_data(GFP_KERNEL), |
| 1002 | 64*1024, 128*1024))) { |
| 1003 | snd_usX2Y_pcm_private_free(pcm); |
| 1004 | return err; |
| 1005 | } |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 1006 | usX2Y(card)->pcm_devs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * create a chip instance and set its names. |
| 1013 | */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 1014 | int usX2Y_audio_create(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | { |
| 1016 | int err = 0; |
| 1017 | |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 1018 | INIT_LIST_HEAD(&usX2Y(card)->pcm_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
| 1020 | if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8))) |
| 1021 | return err; |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 1022 | if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) == USB_ID_US428) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA))) |
| 1024 | return err; |
Clemens Ladisch | a014bba | 2009-11-16 12:26:30 +0100 | [diff] [blame] | 1025 | if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) != USB_ID_US122) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122. |
| 1027 | return err; |
| 1028 | } |