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