Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Line6 Linux USB driver - 0.8.0 |
| 3 | * |
| 4 | * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include "driver.h" |
| 13 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 16 | #include <sound/core.h> |
| 17 | #include <sound/pcm.h> |
| 18 | #include <sound/pcm_params.h> |
| 19 | |
| 20 | #include "audio.h" |
| 21 | #include "pcm.h" |
| 22 | #include "pod.h" |
Greg Kroah-Hartman | b702ed25 | 2009-02-27 20:45:03 -0800 | [diff] [blame] | 23 | #include "playback.h" |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 24 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 25 | /* |
| 26 | Software stereo volume control. |
| 27 | */ |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 28 | static void change_volume(struct urb *urb_out, int volume[], |
| 29 | int bytes_per_frame) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 30 | { |
| 31 | int chn = 0; |
| 32 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 33 | if (volume[0] == 256 && volume[1] == 256) |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 34 | return; /* maximum volume - no change */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 35 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 36 | if (bytes_per_frame == 4) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 37 | short *p, *buf_end; |
| 38 | p = (short *)urb_out->transfer_buffer; |
| 39 | buf_end = p + urb_out->transfer_buffer_length / sizeof(*p); |
| 40 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 41 | for (; p < buf_end; ++p) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 42 | *p = (*p * volume[chn & 1]) >> 8; |
| 43 | ++chn; |
| 44 | } |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 45 | } else if (bytes_per_frame == 6) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 46 | unsigned char *p, *buf_end; |
| 47 | p = (unsigned char *)urb_out->transfer_buffer; |
| 48 | buf_end = p + urb_out->transfer_buffer_length; |
| 49 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 50 | for (; p < buf_end; p += 3) { |
| 51 | int val; |
| 52 | val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 53 | val = (val * volume[chn & 1]) >> 8; |
| 54 | p[0] = val; |
| 55 | p[1] = val >> 8; |
| 56 | p[2] = val >> 16; |
| 57 | ++chn; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | Find a free URB, prepare audio data, and submit URB. |
| 64 | */ |
| 65 | static int submit_audio_out_urb(struct snd_pcm_substream *substream) |
| 66 | { |
| 67 | int index; |
| 68 | unsigned long flags; |
| 69 | int i, urb_size, urb_frames; |
| 70 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 71 | const int bytes_per_frame = line6pcm->properties->bytes_per_frame; |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 72 | const int frame_increment = |
| 73 | line6pcm->properties->snd_line6_rates.rats[0].num_min; |
| 74 | const int frame_factor = |
| 75 | line6pcm->properties->snd_line6_rates.rats[0].den * |
| 76 | (USB_INTERVALS_PER_SECOND / LINE6_ISO_INTERVAL); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 77 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 78 | struct urb *urb_out; |
| 79 | |
| 80 | spin_lock_irqsave(&line6pcm->lock_audio_out, flags); |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 81 | index = |
| 82 | find_first_zero_bit(&line6pcm->active_urb_out, LINE6_ISO_BUFFERS); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 83 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 84 | if (index < 0 || index >= LINE6_ISO_BUFFERS) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 85 | spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags); |
| 86 | dev_err(s2m(substream), "no free URB found\n"); |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | |
| 90 | urb_out = line6pcm->urb_audio_out[index]; |
| 91 | urb_size = 0; |
| 92 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 93 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 94 | /* compute frame size for given sampling rate */ |
| 95 | int n, fs; |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 96 | struct usb_iso_packet_descriptor *fout = |
| 97 | &urb_out->iso_frame_desc[i]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 98 | line6pcm->count_out += frame_increment; |
| 99 | n = line6pcm->count_out / frame_factor; |
| 100 | line6pcm->count_out -= n * frame_factor; |
| 101 | fs = n * bytes_per_frame; |
| 102 | fout->offset = urb_size; |
| 103 | fout->length = fs; |
| 104 | urb_size += fs; |
| 105 | } |
| 106 | |
| 107 | urb_frames = urb_size / bytes_per_frame; |
| 108 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 109 | if (test_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags)) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 110 | urb_out->transfer_buffer = line6pcm->wrap_out; |
| 111 | memset(line6pcm->wrap_out, 0, urb_size); |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 112 | } else { |
| 113 | if (line6pcm->pos_out + urb_frames > runtime->buffer_size) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 114 | /* |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 115 | The transferred area goes over buffer boundary, |
| 116 | copy the data to the temp buffer. |
| 117 | */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 118 | int len; |
| 119 | len = runtime->buffer_size - line6pcm->pos_out; |
| 120 | urb_out->transfer_buffer = line6pcm->wrap_out; |
| 121 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 122 | if (len > 0) { |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 123 | memcpy(line6pcm->wrap_out, |
| 124 | runtime->dma_area + |
| 125 | line6pcm->pos_out * bytes_per_frame, |
| 126 | len * bytes_per_frame); |
| 127 | memcpy(line6pcm->wrap_out + |
| 128 | len * bytes_per_frame, runtime->dma_area, |
| 129 | (urb_frames - len) * bytes_per_frame); |
| 130 | } else { |
| 131 | /* this is somewhat paranoid */ |
| 132 | dev_err(s2m(substream), |
| 133 | "driver bug: len = %d\n", len); |
| 134 | } |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 135 | } else { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 136 | /* set the buffer pointer */ |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 137 | urb_out->transfer_buffer = |
| 138 | runtime->dma_area + |
| 139 | line6pcm->pos_out * bytes_per_frame; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Shawn Bohrer | 7f66fe5 | 2009-11-15 22:17:51 -0600 | [diff] [blame] | 143 | line6pcm->pos_out += urb_frames; |
| 144 | if (line6pcm->pos_out >= runtime->buffer_size) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 145 | line6pcm->pos_out -= runtime->buffer_size; |
| 146 | |
| 147 | urb_out->transfer_buffer_length = urb_size; |
| 148 | urb_out->context = substream; |
| 149 | change_volume(urb_out, line6pcm->volume, bytes_per_frame); |
| 150 | |
| 151 | #if DO_DUMP_PCM_SEND |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 152 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 153 | struct usb_iso_packet_descriptor *fout = |
| 154 | &urb_out->iso_frame_desc[i]; |
| 155 | line6_write_hexdump(line6pcm->line6, 'P', |
| 156 | urb_out->transfer_buffer + fout->offset, |
| 157 | fout->length); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 158 | } |
| 159 | #endif |
| 160 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 161 | if (usb_submit_urb(urb_out, GFP_ATOMIC) == 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 162 | set_bit(index, &line6pcm->active_urb_out); |
| 163 | else |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 164 | dev_err(s2m(substream), "URB out #%d submission failed\n", |
| 165 | index); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 166 | |
| 167 | spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | Submit all currently available playback URBs. |
| 173 | */ |
| 174 | static int submit_audio_out_all_urbs(struct snd_pcm_substream *substream) |
| 175 | { |
| 176 | int ret, i; |
| 177 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 178 | for (i = 0; i < LINE6_ISO_BUFFERS; ++i) { |
| 179 | ret = submit_audio_out_urb(substream); |
| 180 | if (ret < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 181 | return ret; |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 182 | } |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | Unlink all currently active playback URBs. |
| 189 | */ |
| 190 | static void unlink_audio_out_urbs(struct snd_line6_pcm *line6pcm) |
| 191 | { |
| 192 | unsigned int i; |
| 193 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 194 | for (i = LINE6_ISO_BUFFERS; i--;) { |
| 195 | if (test_bit(i, &line6pcm->active_urb_out)) { |
| 196 | if (!test_and_set_bit(i, &line6pcm->unlink_urb_out)) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 197 | struct urb *u = line6pcm->urb_audio_out[i]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 198 | usb_unlink_urb(u); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 205 | Wait until unlinking of all currently active playback URBs has been finished. |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 206 | */ |
| 207 | static void wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm) |
| 208 | { |
| 209 | int timeout = HZ; |
| 210 | unsigned int i; |
| 211 | int alive; |
| 212 | |
| 213 | do { |
| 214 | alive = 0; |
| 215 | for (i = LINE6_ISO_BUFFERS; i--;) { |
| 216 | if (test_bit(i, &line6pcm->active_urb_out)) |
| 217 | alive++; |
| 218 | } |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 219 | if (!alive) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 220 | break; |
| 221 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 222 | schedule_timeout(1); |
| 223 | } while (--timeout > 0); |
| 224 | if (alive) |
| 225 | snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive); |
| 226 | |
| 227 | line6pcm->active_urb_out = 0; |
| 228 | line6pcm->unlink_urb_out = 0; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | Unlink all currently active playback URBs, and wait for finishing. |
| 233 | */ |
| 234 | void unlink_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm) |
| 235 | { |
| 236 | unlink_audio_out_urbs(line6pcm); |
| 237 | wait_clear_audio_out_urbs(line6pcm); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | Callback for completed playback URB. |
| 242 | */ |
Greg Kroah-Hartman | 0c7ab15 | 2009-02-27 20:28:04 -0800 | [diff] [blame] | 243 | static void audio_out_callback(struct urb *urb) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 244 | { |
| 245 | int i, index, length = 0, shutdown = 0; |
| 246 | unsigned long flags; |
| 247 | |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 248 | struct snd_pcm_substream *substream = |
| 249 | (struct snd_pcm_substream *)urb->context; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 250 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 251 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 252 | |
| 253 | /* find index of URB */ |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 254 | for (index = LINE6_ISO_BUFFERS; index--;) |
| 255 | if (urb == line6pcm->urb_audio_out[index]) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 256 | break; |
| 257 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 258 | if (index < 0) |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 259 | return; /* URB has been unlinked asynchronously */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 260 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 261 | for (i = LINE6_ISO_PACKETS; i--;) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 262 | length += urb->iso_frame_desc[i].length; |
| 263 | |
| 264 | spin_lock_irqsave(&line6pcm->lock_audio_out, flags); |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 265 | line6pcm->pos_out_done += |
| 266 | length / line6pcm->properties->bytes_per_frame; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 267 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 268 | if (line6pcm->pos_out_done >= runtime->buffer_size) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 269 | line6pcm->pos_out_done -= runtime->buffer_size; |
| 270 | |
| 271 | clear_bit(index, &line6pcm->active_urb_out); |
| 272 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 273 | for (i = LINE6_ISO_PACKETS; i--;) |
| 274 | if (urb->iso_frame_desc[i].status == -ESHUTDOWN) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 275 | shutdown = 1; |
| 276 | break; |
| 277 | } |
| 278 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 279 | if (test_bit(index, &line6pcm->unlink_urb_out)) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 280 | shutdown = 1; |
| 281 | |
| 282 | spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags); |
| 283 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 284 | if (!shutdown) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 285 | submit_audio_out_urb(substream); |
| 286 | |
Shawn Bohrer | 7f66fe5 | 2009-11-15 22:17:51 -0600 | [diff] [blame] | 287 | line6pcm->bytes_out += length; |
| 288 | if (line6pcm->bytes_out >= line6pcm->period_out) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 289 | line6pcm->bytes_out -= line6pcm->period_out; |
| 290 | snd_pcm_period_elapsed(substream); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* open playback callback */ |
| 296 | static int snd_line6_playback_open(struct snd_pcm_substream *substream) |
| 297 | { |
| 298 | int err; |
| 299 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 300 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 301 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 302 | err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 303 | (&line6pcm->properties-> |
| 304 | snd_line6_rates)); |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 305 | if (err < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 306 | return err; |
| 307 | |
| 308 | runtime->hw = line6pcm->properties->snd_line6_playback_hw; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* close playback callback */ |
| 313 | static int snd_line6_playback_close(struct snd_pcm_substream *substream) |
| 314 | { |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | /* hw_params playback callback */ |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 319 | static int snd_line6_playback_hw_params(struct snd_pcm_substream *substream, |
| 320 | struct snd_pcm_hw_params *hw_params) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 321 | { |
| 322 | int ret; |
| 323 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 324 | |
| 325 | /* -- Florian Demski [FD] */ |
| 326 | /* don't ask me why, but this fixes the bug on my machine */ |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 327 | if (line6pcm == NULL) { |
| 328 | if (substream->pcm == NULL) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 329 | return -ENOMEM; |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 330 | if (substream->pcm->private_data == NULL) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 331 | return -ENOMEM; |
| 332 | substream->private_data = substream->pcm->private_data; |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 333 | line6pcm = snd_pcm_substream_chip(substream); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 334 | } |
| 335 | /* -- [FD] end */ |
| 336 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 337 | ret = snd_pcm_lib_malloc_pages(substream, |
| 338 | params_buffer_bytes(hw_params)); |
| 339 | if (ret < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 340 | return ret; |
| 341 | |
| 342 | line6pcm->period_out = params_period_bytes(hw_params); |
| 343 | line6pcm->wrap_out = kmalloc(2 * LINE6_ISO_PACKET_SIZE_MAX, GFP_KERNEL); |
| 344 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 345 | if (!line6pcm->wrap_out) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 346 | dev_err(s2m(substream), "cannot malloc wrap_out\n"); |
| 347 | return -ENOMEM; |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* hw_free playback callback */ |
| 354 | static int snd_line6_playback_hw_free(struct snd_pcm_substream *substream) |
| 355 | { |
| 356 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 357 | unlink_wait_clear_audio_out_urbs(line6pcm); |
| 358 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 359 | kfree(line6pcm->wrap_out); |
| 360 | line6pcm->wrap_out = NULL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 361 | |
| 362 | return snd_pcm_lib_free_pages(substream); |
| 363 | } |
| 364 | |
| 365 | /* trigger playback callback */ |
| 366 | int snd_line6_playback_trigger(struct snd_pcm_substream *substream, int cmd) |
| 367 | { |
| 368 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 369 | int err; |
| 370 | line6pcm->count_out = 0; |
| 371 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 372 | switch (cmd) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 373 | case SNDRV_PCM_TRIGGER_START: |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 374 | if (!test_and_set_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags)) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 375 | err = submit_audio_out_all_urbs(substream); |
| 376 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 377 | if (err < 0) { |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 378 | clear_bit(BIT_RUNNING_PLAYBACK, |
| 379 | &line6pcm->flags); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 380 | return err; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | break; |
| 385 | |
| 386 | case SNDRV_PCM_TRIGGER_STOP: |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 387 | if (test_and_clear_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags)) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 388 | unlink_audio_out_urbs(line6pcm); |
| 389 | |
| 390 | break; |
| 391 | |
| 392 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 393 | set_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags); |
| 394 | break; |
| 395 | |
| 396 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 397 | clear_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags); |
| 398 | break; |
| 399 | |
| 400 | default: |
| 401 | return -EINVAL; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /* playback pointer callback */ |
| 408 | static snd_pcm_uframes_t |
| 409 | snd_line6_playback_pointer(struct snd_pcm_substream *substream) |
| 410 | { |
| 411 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 412 | return line6pcm->pos_out_done; |
| 413 | } |
| 414 | |
| 415 | /* playback operators */ |
| 416 | struct snd_pcm_ops snd_line6_playback_ops = { |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 417 | .open = snd_line6_playback_open, |
| 418 | .close = snd_line6_playback_close, |
| 419 | .ioctl = snd_pcm_lib_ioctl, |
| 420 | .hw_params = snd_line6_playback_hw_params, |
| 421 | .hw_free = snd_line6_playback_hw_free, |
| 422 | .prepare = snd_line6_prepare, |
| 423 | .trigger = snd_line6_trigger, |
| 424 | .pointer = snd_line6_playback_pointer, |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | int create_audio_out_urbs(struct snd_line6_pcm *line6pcm) |
| 428 | { |
| 429 | int i; |
| 430 | |
| 431 | /* create audio URBs and fill in constant values: */ |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 432 | for (i = 0; i < LINE6_ISO_BUFFERS; ++i) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 433 | struct urb *urb; |
| 434 | |
| 435 | /* URB for audio out: */ |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 436 | urb = line6pcm->urb_audio_out[i] = |
| 437 | usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 438 | |
Greg Kroah-Hartman | 010f378 | 2009-02-27 22:41:12 -0800 | [diff] [blame] | 439 | if (urb == NULL) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 440 | dev_err(line6pcm->line6->ifcdev, "Out of memory\n"); |
| 441 | return -ENOMEM; |
| 442 | } |
| 443 | |
| 444 | urb->dev = line6pcm->line6->usbdev; |
Shawn Bohrer | 45af497 | 2009-11-15 22:17:50 -0600 | [diff] [blame] | 445 | urb->pipe = |
| 446 | usb_sndisocpipe(line6pcm->line6->usbdev, |
| 447 | line6pcm-> |
| 448 | ep_audio_write & USB_ENDPOINT_NUMBER_MASK); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 449 | urb->transfer_flags = URB_ISO_ASAP; |
| 450 | urb->start_frame = -1; |
| 451 | urb->number_of_packets = LINE6_ISO_PACKETS; |
| 452 | urb->interval = LINE6_ISO_INTERVAL; |
| 453 | urb->error_count = 0; |
| 454 | urb->complete = audio_out_callback; |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |