Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 1 | /* |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 2 | * Line6 Linux USB driver - 0.9.1beta |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 3 | * |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 4 | * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 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 | |
Stefan Hajnoczi | 140e28b | 2011-11-23 08:20:45 +0000 | [diff] [blame] | 12 | #include <linux/slab.h> |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 13 | #include <sound/core.h> |
| 14 | #include <sound/pcm.h> |
| 15 | #include <sound/pcm_params.h> |
| 16 | |
| 17 | #include "audio.h" |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 18 | #include "capture.h" |
| 19 | #include "driver.h" |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 20 | #include "pcm.h" |
| 21 | #include "pod.h" |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 22 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 23 | /* |
| 24 | Find a free URB and submit it. |
| 25 | */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 26 | static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 27 | { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 28 | int index; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 29 | unsigned long flags; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 30 | int i, urb_size; |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 31 | int ret; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 32 | struct urb *urb_in; |
| 33 | |
| 34 | spin_lock_irqsave(&line6pcm->lock_audio_in, flags); |
Shawn Bohrer | e0645d6 | 2009-11-15 22:17:52 -0600 | [diff] [blame] | 35 | index = |
| 36 | find_first_zero_bit(&line6pcm->active_urb_in, LINE6_ISO_BUFFERS); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 37 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 38 | if (index < 0 || index >= LINE6_ISO_BUFFERS) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 39 | spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 40 | dev_err(line6pcm->line6->ifcdev, "no free URB found\n"); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 41 | return -EINVAL; |
| 42 | } |
| 43 | |
| 44 | urb_in = line6pcm->urb_audio_in[index]; |
| 45 | urb_size = 0; |
| 46 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 47 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
Shawn Bohrer | e0645d6 | 2009-11-15 22:17:52 -0600 | [diff] [blame] | 48 | struct usb_iso_packet_descriptor *fin = |
| 49 | &urb_in->iso_frame_desc[i]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 50 | fin->offset = urb_size; |
| 51 | fin->length = line6pcm->max_packet_size; |
| 52 | urb_size += line6pcm->max_packet_size; |
| 53 | } |
| 54 | |
Shawn Bohrer | e0645d6 | 2009-11-15 22:17:52 -0600 | [diff] [blame] | 55 | urb_in->transfer_buffer = |
| 56 | line6pcm->buffer_in + |
| 57 | index * LINE6_ISO_PACKETS * line6pcm->max_packet_size; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 58 | urb_in->transfer_buffer_length = urb_size; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 59 | urb_in->context = line6pcm; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 60 | |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 61 | ret = usb_submit_urb(urb_in, GFP_ATOMIC); |
| 62 | |
| 63 | if (ret == 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 64 | set_bit(index, &line6pcm->active_urb_in); |
| 65 | else |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 66 | dev_err(line6pcm->line6->ifcdev, |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 67 | "URB in #%d submission failed (%d)\n", index, ret); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 68 | |
| 69 | spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | Submit all currently available capture URBs. |
| 75 | */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 76 | int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 77 | { |
| 78 | int ret, i; |
| 79 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 80 | for (i = 0; i < LINE6_ISO_BUFFERS; ++i) { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 81 | ret = submit_audio_in_urb(line6pcm); |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 82 | if (ret < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 83 | return ret; |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 84 | } |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | Unlink all currently active capture URBs. |
| 91 | */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 92 | void line6_unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 93 | { |
| 94 | unsigned int i; |
| 95 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 96 | for (i = LINE6_ISO_BUFFERS; i--;) { |
| 97 | if (test_bit(i, &line6pcm->active_urb_in)) { |
| 98 | if (!test_and_set_bit(i, &line6pcm->unlink_urb_in)) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 99 | struct urb *u = line6pcm->urb_audio_in[i]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 100 | usb_unlink_urb(u); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 107 | Wait until unlinking of all currently active capture URBs has been |
| 108 | finished. |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 109 | */ |
| 110 | static void wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
| 111 | { |
| 112 | int timeout = HZ; |
| 113 | unsigned int i; |
| 114 | int alive; |
| 115 | |
| 116 | do { |
| 117 | alive = 0; |
| 118 | for (i = LINE6_ISO_BUFFERS; i--;) { |
| 119 | if (test_bit(i, &line6pcm->active_urb_in)) |
| 120 | alive++; |
| 121 | } |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 122 | if (!alive) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 123 | break; |
| 124 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 125 | schedule_timeout(1); |
| 126 | } while (--timeout > 0); |
| 127 | if (alive) |
| 128 | snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | Unlink all currently active capture URBs, and wait for finishing. |
| 133 | */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 134 | void line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 135 | { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 136 | line6_unlink_audio_in_urbs(line6pcm); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 137 | wait_clear_audio_in_urbs(line6pcm); |
| 138 | } |
| 139 | |
| 140 | /* |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 141 | Copy data into ALSA capture buffer. |
| 142 | */ |
| 143 | void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize) |
| 144 | { |
| 145 | struct snd_pcm_substream *substream = |
| 146 | get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE); |
| 147 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 148 | const int bytes_per_frame = line6pcm->properties->bytes_per_frame; |
| 149 | int frames = fsize / bytes_per_frame; |
| 150 | |
Peter Huewe | 597a1e7 | 2010-12-07 23:38:02 +0100 | [diff] [blame] | 151 | if (runtime == NULL) |
Markus Grabner | c7fcf25 | 2010-09-17 23:33:25 +0200 | [diff] [blame] | 152 | return; |
| 153 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 154 | if (line6pcm->pos_in_done + frames > runtime->buffer_size) { |
| 155 | /* |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 156 | The transferred area goes over buffer boundary, |
| 157 | copy two separate chunks. |
| 158 | */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 159 | int len; |
| 160 | len = runtime->buffer_size - line6pcm->pos_in_done; |
| 161 | |
| 162 | if (len > 0) { |
| 163 | memcpy(runtime->dma_area + |
| 164 | line6pcm->pos_in_done * bytes_per_frame, fbuf, |
| 165 | len * bytes_per_frame); |
| 166 | memcpy(runtime->dma_area, fbuf + len * bytes_per_frame, |
| 167 | (frames - len) * bytes_per_frame); |
Greg Kroah-Hartman | 027360c | 2010-09-21 16:58:00 -0700 | [diff] [blame] | 168 | } else { |
| 169 | /* this is somewhat paranoid */ |
| 170 | dev_err(line6pcm->line6->ifcdev, |
| 171 | "driver bug: len = %d\n", len); |
| 172 | } |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 173 | } else { |
| 174 | /* copy single chunk */ |
| 175 | memcpy(runtime->dma_area + |
| 176 | line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize); |
| 177 | } |
| 178 | |
Greg Kroah-Hartman | 027360c | 2010-09-21 16:58:00 -0700 | [diff] [blame] | 179 | line6pcm->pos_in_done += frames; |
| 180 | if (line6pcm->pos_in_done >= runtime->buffer_size) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 181 | line6pcm->pos_in_done -= runtime->buffer_size; |
| 182 | } |
| 183 | |
| 184 | void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length) |
| 185 | { |
| 186 | struct snd_pcm_substream *substream = |
| 187 | get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE); |
| 188 | |
Greg Kroah-Hartman | 027360c | 2010-09-21 16:58:00 -0700 | [diff] [blame] | 189 | line6pcm->bytes_in += length; |
| 190 | if (line6pcm->bytes_in >= line6pcm->period_in) { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 191 | line6pcm->bytes_in %= line6pcm->period_in; |
| 192 | snd_pcm_period_elapsed(substream); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* |
Greg Kroah-Hartman | 027360c | 2010-09-21 16:58:00 -0700 | [diff] [blame] | 197 | * Callback for completed capture URB. |
| 198 | */ |
Greg Kroah-Hartman | 0c7ab15 | 2009-02-27 20:28:04 -0800 | [diff] [blame] | 199 | static void audio_in_callback(struct urb *urb) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 200 | { |
| 201 | int i, index, length = 0, shutdown = 0; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 202 | unsigned long flags; |
| 203 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 204 | struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context; |
| 205 | |
| 206 | line6pcm->last_frame_in = urb->start_frame; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 207 | |
| 208 | /* find index of URB */ |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 209 | for (index = 0; index < LINE6_ISO_BUFFERS; ++index) |
| 210 | if (urb == line6pcm->urb_audio_in[index]) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 211 | break; |
| 212 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 213 | #ifdef CONFIG_LINE6_USB_DUMP_PCM |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 214 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
Shawn Bohrer | e0645d6 | 2009-11-15 22:17:52 -0600 | [diff] [blame] | 215 | struct usb_iso_packet_descriptor *fout = |
| 216 | &urb->iso_frame_desc[i]; |
| 217 | line6_write_hexdump(line6pcm->line6, 'C', |
| 218 | urb->transfer_buffer + fout->offset, |
| 219 | fout->length); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 220 | } |
| 221 | #endif |
| 222 | |
| 223 | spin_lock_irqsave(&line6pcm->lock_audio_in, flags); |
| 224 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 225 | for (i = 0; i < LINE6_ISO_PACKETS; ++i) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 226 | char *fbuf; |
| 227 | int fsize; |
| 228 | struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i]; |
| 229 | |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 230 | if (fin->status == -EXDEV) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 231 | shutdown = 1; |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | fbuf = urb->transfer_buffer + fin->offset; |
| 236 | fsize = fin->actual_length; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 237 | |
| 238 | if (fsize > line6pcm->max_packet_size) { |
| 239 | dev_err(line6pcm->line6->ifcdev, |
| 240 | "driver and/or device bug: packet too large (%d > %d)\n", |
| 241 | fsize, line6pcm->max_packet_size); |
| 242 | } |
| 243 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 244 | length += fsize; |
| 245 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 246 | /* the following assumes LINE6_ISO_PACKETS == 1: */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 247 | line6pcm->prev_fbuf = fbuf; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 248 | line6pcm->prev_fsize = fsize; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 249 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 250 | #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE |
| 251 | if (!(line6pcm->flags & MASK_PCM_IMPULSE)) |
| 252 | #endif |
| 253 | if (test_bit(BIT_PCM_ALSA_CAPTURE, &line6pcm->flags) |
| 254 | && (fsize > 0)) |
| 255 | line6_capture_copy(line6pcm, fbuf, fsize); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | clear_bit(index, &line6pcm->active_urb_in); |
| 259 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 260 | if (test_and_clear_bit(index, &line6pcm->unlink_urb_in)) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 261 | shutdown = 1; |
| 262 | |
| 263 | spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags); |
| 264 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 265 | if (!shutdown) { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 266 | submit_audio_in_urb(line6pcm); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 267 | |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 268 | #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE |
| 269 | if (!(line6pcm->flags & MASK_PCM_IMPULSE)) |
| 270 | #endif |
| 271 | if (test_bit(BIT_PCM_ALSA_CAPTURE, &line6pcm->flags)) |
| 272 | line6_capture_check_period(line6pcm, length); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | /* open capture callback */ |
| 277 | static int snd_line6_capture_open(struct snd_pcm_substream *substream) |
| 278 | { |
| 279 | int err; |
| 280 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 281 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 282 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 283 | err = snd_pcm_hw_constraint_ratdens(runtime, 0, |
| 284 | SNDRV_PCM_HW_PARAM_RATE, |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 285 | (&line6pcm-> |
| 286 | properties->snd_line6_rates)); |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 287 | if (err < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 288 | return err; |
| 289 | |
| 290 | runtime->hw = line6pcm->properties->snd_line6_capture_hw; |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* close capture callback */ |
| 295 | static int snd_line6_capture_close(struct snd_pcm_substream *substream) |
| 296 | { |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /* hw_params capture callback */ |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 301 | static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream, |
| 302 | struct snd_pcm_hw_params *hw_params) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 303 | { |
| 304 | int ret; |
| 305 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 306 | |
| 307 | /* -- Florian Demski [FD] */ |
| 308 | /* don't ask me why, but this fixes the bug on my machine */ |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 309 | if (line6pcm == NULL) { |
| 310 | if (substream->pcm == NULL) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 311 | return -ENOMEM; |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 312 | if (substream->pcm->private_data == NULL) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 313 | return -ENOMEM; |
| 314 | substream->private_data = substream->pcm->private_data; |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 315 | line6pcm = snd_pcm_substream_chip(substream); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 316 | } |
| 317 | /* -- [FD] end */ |
| 318 | |
Stefan Hajnoczi | 60c01a9 | 2011-12-10 02:12:27 +0100 | [diff] [blame^] | 319 | /* We may be invoked multiple times in a row so allocate once only */ |
| 320 | if (!line6pcm->buffer_in) |
| 321 | line6pcm->buffer_in = |
| 322 | kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * |
| 323 | line6pcm->max_packet_size, GFP_KERNEL); |
Stefan Hajnoczi | 140e28b | 2011-11-23 08:20:45 +0000 | [diff] [blame] | 324 | |
| 325 | if (!line6pcm->buffer_in) { |
| 326 | dev_err(line6pcm->line6->ifcdev, |
| 327 | "cannot malloc capture buffer\n"); |
| 328 | return -ENOMEM; |
| 329 | } |
| 330 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 331 | ret = snd_pcm_lib_malloc_pages(substream, |
| 332 | params_buffer_bytes(hw_params)); |
| 333 | if (ret < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 334 | return ret; |
| 335 | |
| 336 | line6pcm->period_in = params_period_bytes(hw_params); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* hw_free capture callback */ |
| 341 | static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream) |
| 342 | { |
Stefan Hajnoczi | 140e28b | 2011-11-23 08:20:45 +0000 | [diff] [blame] | 343 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 344 | |
| 345 | line6_unlink_wait_clear_audio_in_urbs(line6pcm); |
| 346 | kfree(line6pcm->buffer_in); |
| 347 | line6pcm->buffer_in = NULL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 348 | return snd_pcm_lib_free_pages(substream); |
| 349 | } |
| 350 | |
| 351 | /* trigger callback */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 352 | int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 353 | { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 354 | int err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 355 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 356 | switch (cmd) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 357 | case SNDRV_PCM_TRIGGER_START: |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 358 | #ifdef CONFIG_PM |
| 359 | case SNDRV_PCM_TRIGGER_RESUME: |
| 360 | #endif |
| 361 | err = line6_pcm_start(line6pcm, MASK_PCM_ALSA_CAPTURE); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 362 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 363 | if (err < 0) |
| 364 | return err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 365 | |
| 366 | break; |
| 367 | |
| 368 | case SNDRV_PCM_TRIGGER_STOP: |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 369 | #ifdef CONFIG_PM |
| 370 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 371 | #endif |
| 372 | err = line6_pcm_stop(line6pcm, MASK_PCM_ALSA_CAPTURE); |
| 373 | |
| 374 | if (err < 0) |
| 375 | return err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 376 | |
| 377 | break; |
| 378 | |
| 379 | default: |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | /* capture pointer callback */ |
| 387 | static snd_pcm_uframes_t |
| 388 | snd_line6_capture_pointer(struct snd_pcm_substream *substream) |
| 389 | { |
| 390 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 391 | return line6pcm->pos_in_done; |
| 392 | } |
| 393 | |
| 394 | /* capture operators */ |
| 395 | struct snd_pcm_ops snd_line6_capture_ops = { |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 396 | .open = snd_line6_capture_open, |
| 397 | .close = snd_line6_capture_close, |
| 398 | .ioctl = snd_pcm_lib_ioctl, |
| 399 | .hw_params = snd_line6_capture_hw_params, |
| 400 | .hw_free = snd_line6_capture_hw_free, |
| 401 | .prepare = snd_line6_prepare, |
| 402 | .trigger = snd_line6_trigger, |
| 403 | .pointer = snd_line6_capture_pointer, |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 404 | }; |
| 405 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 406 | int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 407 | { |
| 408 | int i; |
| 409 | |
| 410 | /* create audio URBs and fill in constant values: */ |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 411 | for (i = 0; i < LINE6_ISO_BUFFERS; ++i) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 412 | struct urb *urb; |
| 413 | |
| 414 | /* URB for audio in: */ |
Shawn Bohrer | e0645d6 | 2009-11-15 22:17:52 -0600 | [diff] [blame] | 415 | urb = line6pcm->urb_audio_in[i] = |
| 416 | usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 417 | |
Greg Kroah-Hartman | 6efc566 | 2009-02-27 22:39:22 -0800 | [diff] [blame] | 418 | if (urb == NULL) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 419 | dev_err(line6pcm->line6->ifcdev, "Out of memory\n"); |
| 420 | return -ENOMEM; |
| 421 | } |
| 422 | |
| 423 | urb->dev = line6pcm->line6->usbdev; |
Shawn Bohrer | e0645d6 | 2009-11-15 22:17:52 -0600 | [diff] [blame] | 424 | urb->pipe = |
| 425 | usb_rcvisocpipe(line6pcm->line6->usbdev, |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 426 | line6pcm->ep_audio_read & |
| 427 | USB_ENDPOINT_NUMBER_MASK); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 428 | urb->transfer_flags = URB_ISO_ASAP; |
| 429 | urb->start_frame = -1; |
| 430 | urb->number_of_packets = LINE6_ISO_PACKETS; |
| 431 | urb->interval = LINE6_ISO_INTERVAL; |
| 432 | urb->error_count = 0; |
| 433 | urb->complete = audio_in_callback; |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | } |