Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 2 | /* |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 3 | * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 6 | #include <linux/device.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 7 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Daniel Mack | e431cf4 | 2009-03-28 21:19:49 +0100 | [diff] [blame] | 9 | #include <linux/init.h> |
| 10 | #include <linux/usb.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 11 | #include <sound/core.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 12 | #include <sound/pcm.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 13 | |
Daniel Mack | 936e7d0 | 2009-04-01 19:05:39 +0200 | [diff] [blame] | 14 | #include "device.h" |
| 15 | #include "audio.h" |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 16 | |
| 17 | #define N_URBS 32 |
| 18 | #define CLOCK_DRIFT_TOLERANCE 5 |
| 19 | #define FRAMES_PER_URB 8 |
| 20 | #define BYTES_PER_FRAME 512 |
| 21 | #define CHANNELS_PER_STREAM 2 |
| 22 | #define BYTES_PER_SAMPLE 3 |
| 23 | #define BYTES_PER_SAMPLE_USB 4 |
| 24 | #define MAX_BUFFER_SIZE (128*1024) |
Daniel Mack | 6e9fc6b | 2008-04-14 15:40:31 +0200 | [diff] [blame] | 25 | #define MAX_ENDPOINT_SIZE 512 |
| 26 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 27 | #define ENDPOINT_CAPTURE 2 |
| 28 | #define ENDPOINT_PLAYBACK 6 |
| 29 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 30 | #define MAKE_CHECKBYTE(cdev,stream,i) \ |
| 31 | (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 32 | |
| 33 | static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 34 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 35 | SNDRV_PCM_INFO_BLOCK_TRANSFER), |
| 36 | .formats = SNDRV_PCM_FMTBIT_S24_3BE, |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 37 | .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 38 | SNDRV_PCM_RATE_96000), |
| 39 | .rate_min = 44100, |
| 40 | .rate_max = 0, /* will overwrite later */ |
| 41 | .channels_min = CHANNELS_PER_STREAM, |
| 42 | .channels_max = CHANNELS_PER_STREAM, |
| 43 | .buffer_bytes_max = MAX_BUFFER_SIZE, |
Daniel Mack | 09189ac | 2008-01-24 18:46:42 +0100 | [diff] [blame] | 44 | .period_bytes_min = 128, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 45 | .period_bytes_max = MAX_BUFFER_SIZE, |
| 46 | .periods_min = 1, |
| 47 | .periods_max = 1024, |
| 48 | }; |
| 49 | |
| 50 | static void |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 51 | activate_substream(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 52 | struct snd_pcm_substream *sub) |
| 53 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 54 | spin_lock(&cdev->spinlock); |
Mark Hills | ac9dd9d | 2009-10-24 12:59:36 +0100 | [diff] [blame] | 55 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 56 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 57 | cdev->sub_playback[sub->number] = sub; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 58 | else |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 59 | cdev->sub_capture[sub->number] = sub; |
Mark Hills | ac9dd9d | 2009-10-24 12:59:36 +0100 | [diff] [blame] | 60 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 61 | spin_unlock(&cdev->spinlock); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 64 | static void |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 65 | deactivate_substream(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 66 | struct snd_pcm_substream *sub) |
| 67 | { |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 68 | unsigned long flags; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 69 | spin_lock_irqsave(&cdev->spinlock, flags); |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 70 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 71 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 72 | cdev->sub_playback[sub->number] = NULL; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 73 | else |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 74 | cdev->sub_capture[sub->number] = NULL; |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 75 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 76 | spin_unlock_irqrestore(&cdev->spinlock, flags); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static int |
| 80 | all_substreams_zero(struct snd_pcm_substream **subs) |
| 81 | { |
| 82 | int i; |
| 83 | for (i = 0; i < MAX_STREAMS; i++) |
| 84 | if (subs[i] != NULL) |
| 85 | return 0; |
| 86 | return 1; |
| 87 | } |
| 88 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 89 | static int stream_start(struct snd_usb_caiaqdev *cdev) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 90 | { |
| 91 | int i, ret; |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 92 | struct device *dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 93 | |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 94 | dev_dbg(dev, "%s(%p)\n", __func__, cdev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 95 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 96 | if (cdev->streaming) |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 97 | return -EINVAL; |
| 98 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 99 | memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback)); |
| 100 | memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture)); |
| 101 | cdev->input_panic = 0; |
| 102 | cdev->output_panic = 0; |
| 103 | cdev->first_packet = 4; |
| 104 | cdev->streaming = 1; |
| 105 | cdev->warned = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 106 | |
| 107 | for (i = 0; i < N_URBS; i++) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 108 | ret = usb_submit_urb(cdev->data_urbs_in[i], GFP_ATOMIC); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 109 | if (ret) { |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 110 | dev_err(dev, "unable to trigger read #%d! (ret %d)\n", |
| 111 | i, ret); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 112 | cdev->streaming = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 113 | return -EPIPE; |
| 114 | } |
| 115 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 116 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 120 | static void stream_stop(struct snd_usb_caiaqdev *cdev) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 121 | { |
| 122 | int i; |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 123 | struct device *dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 124 | |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 125 | dev_dbg(dev, "%s(%p)\n", __func__, cdev); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 126 | if (!cdev->streaming) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 127 | return; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 128 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 129 | cdev->streaming = 0; |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 130 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 131 | for (i = 0; i < N_URBS; i++) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 132 | usb_kill_urb(cdev->data_urbs_in[i]); |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 133 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 134 | if (test_bit(i, &cdev->outurb_active_mask)) |
| 135 | usb_kill_urb(cdev->data_urbs_out[i]); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 136 | } |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 137 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 138 | cdev->outurb_active_mask = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream) |
| 142 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 143 | struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream); |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 144 | struct device *dev = caiaqdev_to_dev(cdev); |
| 145 | |
| 146 | dev_dbg(dev, "%s(%p)\n", __func__, substream); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 147 | substream->runtime->hw = cdev->pcm_info; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 148 | snd_pcm_limit_hw_rates(substream->runtime); |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 149 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream) |
| 154 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 155 | struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream); |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 156 | struct device *dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 157 | |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 158 | dev_dbg(dev, "%s(%p)\n", __func__, substream); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 159 | if (all_substreams_zero(cdev->sub_playback) && |
| 160 | all_substreams_zero(cdev->sub_capture)) { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 161 | /* when the last client has stopped streaming, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 162 | * all sample rates are allowed again */ |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 163 | stream_stop(cdev); |
| 164 | cdev->pcm_info.rates = cdev->samplerates; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 165 | } |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 166 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub, |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 171 | struct snd_pcm_hw_params *hw_params) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 172 | { |
Antonio Ospite | fc76f86 | 2013-06-21 13:11:49 +0200 | [diff] [blame] | 173 | return snd_pcm_lib_alloc_vmalloc_buffer(sub, |
| 174 | params_buffer_bytes(hw_params)); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub) |
| 178 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 179 | struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 180 | deactivate_substream(cdev, sub); |
Antonio Ospite | fc76f86 | 2013-06-21 13:11:49 +0200 | [diff] [blame] | 181 | return snd_pcm_lib_free_vmalloc_buffer(sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* this should probably go upstream */ |
| 185 | #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 |
| 186 | #error "Change this table" |
| 187 | #endif |
| 188 | |
| 189 | static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100, |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 190 | 48000, 64000, 88200, 96000, 176400, 192000 }; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 191 | |
| 192 | static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream) |
| 193 | { |
| 194 | int bytes_per_sample, bpp, ret, i; |
| 195 | int index = substream->number; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 196 | struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 197 | struct snd_pcm_runtime *runtime = substream->runtime; |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 198 | struct device *dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 199 | |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 200 | dev_dbg(dev, "%s(%p)\n", __func__, substream); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 201 | |
Daniel Mack | a9b487f | 2009-04-27 12:18:05 +0200 | [diff] [blame] | 202 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 203 | int out_pos; |
| 204 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 205 | switch (cdev->spec.data_alignment) { |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 206 | case 0: |
| 207 | case 2: |
| 208 | out_pos = BYTES_PER_SAMPLE + 1; |
| 209 | break; |
| 210 | case 3: |
| 211 | default: |
| 212 | out_pos = 0; |
| 213 | break; |
| 214 | } |
| 215 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 216 | cdev->period_out_count[index] = out_pos; |
| 217 | cdev->audio_out_buf_pos[index] = out_pos; |
Daniel Mack | a9b487f | 2009-04-27 12:18:05 +0200 | [diff] [blame] | 218 | } else { |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 219 | int in_pos; |
| 220 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 221 | switch (cdev->spec.data_alignment) { |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 222 | case 0: |
| 223 | in_pos = BYTES_PER_SAMPLE + 2; |
| 224 | break; |
| 225 | case 2: |
| 226 | in_pos = BYTES_PER_SAMPLE; |
| 227 | break; |
| 228 | case 3: |
| 229 | default: |
| 230 | in_pos = 0; |
| 231 | break; |
| 232 | } |
| 233 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 234 | cdev->period_in_count[index] = in_pos; |
| 235 | cdev->audio_in_buf_pos[index] = in_pos; |
Daniel Mack | a9b487f | 2009-04-27 12:18:05 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 238 | if (cdev->streaming) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 239 | return 0; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 240 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 241 | /* the first client that opens a stream defines the sample rate |
| 242 | * setting for all subsequent calls, until the last client closed. */ |
| 243 | for (i=0; i < ARRAY_SIZE(rates); i++) |
| 244 | if (runtime->rate == rates[i]) |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 245 | cdev->pcm_info.rates = 1 << i; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 246 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 247 | snd_pcm_limit_hw_rates(runtime); |
| 248 | |
| 249 | bytes_per_sample = BYTES_PER_SAMPLE; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 250 | if (cdev->spec.data_alignment >= 2) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 251 | bytes_per_sample++; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 252 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 253 | bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE) |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 254 | * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams; |
Daniel Mack | 6e9fc6b | 2008-04-14 15:40:31 +0200 | [diff] [blame] | 255 | |
| 256 | if (bpp > MAX_ENDPOINT_SIZE) |
| 257 | bpp = MAX_ENDPOINT_SIZE; |
| 258 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 259 | ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 260 | runtime->sample_bits, bpp); |
| 261 | if (ret) |
| 262 | return ret; |
| 263 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 264 | ret = stream_start(cdev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 265 | if (ret) |
| 266 | return ret; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 267 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 268 | cdev->output_running = 0; |
| 269 | wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ); |
| 270 | if (!cdev->output_running) { |
| 271 | stream_stop(cdev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 272 | return -EPIPE; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd) |
| 279 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 280 | struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub); |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 281 | struct device *dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 282 | |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 283 | dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd); |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 284 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 285 | switch (cmd) { |
| 286 | case SNDRV_PCM_TRIGGER_START: |
| 287 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 288 | activate_substream(cdev, sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 289 | break; |
| 290 | case SNDRV_PCM_TRIGGER_STOP: |
| 291 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 292 | deactivate_substream(cdev, sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 293 | break; |
| 294 | default: |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static snd_pcm_uframes_t |
| 302 | snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub) |
| 303 | { |
| 304 | int index = sub->number; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 305 | struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub); |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame] | 306 | snd_pcm_uframes_t ptr; |
| 307 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 308 | spin_lock(&cdev->spinlock); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 309 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 310 | if (cdev->input_panic || cdev->output_panic) { |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame] | 311 | ptr = SNDRV_PCM_POS_XRUN; |
Mark Hills | cb74eb1 | 2012-02-21 21:26:31 +0000 | [diff] [blame] | 312 | goto unlock; |
| 313 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 314 | |
| 315 | if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame] | 316 | ptr = bytes_to_frames(sub->runtime, |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 317 | cdev->audio_out_buf_pos[index]); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 318 | else |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame] | 319 | ptr = bytes_to_frames(sub->runtime, |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 320 | cdev->audio_in_buf_pos[index]); |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame] | 321 | |
Mark Hills | cb74eb1 | 2012-02-21 21:26:31 +0000 | [diff] [blame] | 322 | unlock: |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 323 | spin_unlock(&cdev->spinlock); |
Mark Hills | 3702b08 | 2009-10-24 12:59:35 +0100 | [diff] [blame] | 324 | return ptr; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* operators for both playback and capture */ |
Arvind Yadav | 31cb1fb | 2017-08-18 13:15:21 +0530 | [diff] [blame] | 328 | static const struct snd_pcm_ops snd_usb_caiaq_ops = { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 329 | .open = snd_usb_caiaq_substream_open, |
| 330 | .close = snd_usb_caiaq_substream_close, |
| 331 | .ioctl = snd_pcm_lib_ioctl, |
| 332 | .hw_params = snd_usb_caiaq_pcm_hw_params, |
| 333 | .hw_free = snd_usb_caiaq_pcm_hw_free, |
| 334 | .prepare = snd_usb_caiaq_pcm_prepare, |
| 335 | .trigger = snd_usb_caiaq_pcm_trigger, |
Antonio Ospite | fc76f86 | 2013-06-21 13:11:49 +0200 | [diff] [blame] | 336 | .pointer = snd_usb_caiaq_pcm_pointer, |
| 337 | .page = snd_pcm_lib_get_vmalloc_page, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 338 | }; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 339 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 340 | static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 341 | struct snd_pcm_substream **subs) |
| 342 | { |
| 343 | int stream, pb, *cnt; |
| 344 | struct snd_pcm_substream *sub; |
| 345 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 346 | for (stream = 0; stream < cdev->n_streams; stream++) { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 347 | sub = subs[stream]; |
| 348 | if (!sub) |
| 349 | continue; |
| 350 | |
Daniel Mack | a9b487f | 2009-04-27 12:18:05 +0200 | [diff] [blame] | 351 | pb = snd_pcm_lib_period_bytes(sub); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 352 | cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ? |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 353 | &cdev->period_out_count[stream] : |
| 354 | &cdev->period_in_count[stream]; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 355 | |
| 356 | if (*cnt >= pb) { |
| 357 | snd_pcm_period_elapsed(sub); |
| 358 | *cnt %= pb; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 363 | static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 364 | const struct urb *urb, |
| 365 | const struct usb_iso_packet_descriptor *iso) |
| 366 | { |
| 367 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; |
| 368 | struct snd_pcm_substream *sub; |
| 369 | int stream, i; |
| 370 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 371 | if (all_substreams_zero(cdev->sub_capture)) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 372 | return; |
| 373 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 374 | for (i = 0; i < iso->actual_length;) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 375 | for (stream = 0; stream < cdev->n_streams; stream++, i++) { |
| 376 | sub = cdev->sub_capture[stream]; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 377 | if (sub) { |
| 378 | struct snd_pcm_runtime *rt = sub->runtime; |
| 379 | char *audio_buf = rt->dma_area; |
| 380 | int sz = frames_to_bytes(rt, rt->buffer_size); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 381 | audio_buf[cdev->audio_in_buf_pos[stream]++] |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 382 | = usb_buf[i]; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 383 | cdev->period_in_count[stream]++; |
| 384 | if (cdev->audio_in_buf_pos[stream] == sz) |
| 385 | cdev->audio_in_buf_pos[stream] = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 391 | static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 392 | const struct urb *urb, |
| 393 | const struct usb_iso_packet_descriptor *iso) |
| 394 | { |
| 395 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; |
| 396 | unsigned char check_byte; |
| 397 | struct snd_pcm_substream *sub; |
| 398 | int stream, i; |
| 399 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 400 | for (i = 0; i < iso->actual_length;) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 401 | if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 402 | for (stream = 0; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 403 | stream < cdev->n_streams; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 404 | stream++, i++) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 405 | if (cdev->first_packet) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 406 | continue; |
| 407 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 408 | check_byte = MAKE_CHECKBYTE(cdev, stream, i); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 409 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 410 | if ((usb_buf[i] & 0x3f) != check_byte) |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 411 | cdev->input_panic = 1; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 412 | |
| 413 | if (usb_buf[i] & 0x80) |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 414 | cdev->output_panic = 1; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 415 | } |
| 416 | } |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 417 | cdev->first_packet = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 418 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 419 | for (stream = 0; stream < cdev->n_streams; stream++, i++) { |
| 420 | sub = cdev->sub_capture[stream]; |
| 421 | if (cdev->input_panic) |
Daniel Mack | 9311c9b | 2009-03-18 11:03:54 +0100 | [diff] [blame] | 422 | usb_buf[i] = 0; |
| 423 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 424 | if (sub) { |
| 425 | struct snd_pcm_runtime *rt = sub->runtime; |
| 426 | char *audio_buf = rt->dma_area; |
| 427 | int sz = frames_to_bytes(rt, rt->buffer_size); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 428 | audio_buf[cdev->audio_in_buf_pos[stream]++] = |
Karsten Wiese | a971c3d4 | 2007-03-29 17:02:45 +0200 | [diff] [blame] | 429 | usb_buf[i]; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 430 | cdev->period_in_count[stream]++; |
| 431 | if (cdev->audio_in_buf_pos[stream] == sz) |
| 432 | cdev->audio_in_buf_pos[stream] = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 438 | static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 439 | const struct urb *urb, |
| 440 | const struct usb_iso_packet_descriptor *iso) |
| 441 | { |
| 442 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 443 | struct device *dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 444 | int stream, i; |
| 445 | |
| 446 | /* paranoia check */ |
| 447 | if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM)) |
| 448 | return; |
| 449 | |
| 450 | for (i = 0; i < iso->actual_length;) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 451 | for (stream = 0; stream < cdev->n_streams; stream++) { |
| 452 | struct snd_pcm_substream *sub = cdev->sub_capture[stream]; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 453 | char *audio_buf = NULL; |
| 454 | int c, n, sz = 0; |
| 455 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 456 | if (sub && !cdev->input_panic) { |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 457 | struct snd_pcm_runtime *rt = sub->runtime; |
| 458 | audio_buf = rt->dma_area; |
| 459 | sz = frames_to_bytes(rt, rt->buffer_size); |
| 460 | } |
| 461 | |
| 462 | for (c = 0; c < CHANNELS_PER_STREAM; c++) { |
| 463 | /* 3 audio data bytes, followed by 1 check byte */ |
| 464 | if (audio_buf) { |
| 465 | for (n = 0; n < BYTES_PER_SAMPLE; n++) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 466 | audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n]; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 467 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 468 | if (cdev->audio_in_buf_pos[stream] == sz) |
| 469 | cdev->audio_in_buf_pos[stream] = 0; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 470 | } |
| 471 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 472 | cdev->period_in_count[stream] += BYTES_PER_SAMPLE; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | i += BYTES_PER_SAMPLE; |
| 476 | |
| 477 | if (usb_buf[i] != ((stream << 1) | c) && |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 478 | !cdev->first_packet) { |
| 479 | if (!cdev->input_panic) |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 480 | dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n", |
| 481 | ((stream << 1) | c), usb_buf[i], c, stream, i); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 482 | cdev->input_panic = 1; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | i++; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 490 | if (cdev->first_packet > 0) |
| 491 | cdev->first_packet--; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 492 | } |
| 493 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 494 | static void read_in_urb(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 495 | const struct urb *urb, |
| 496 | const struct usb_iso_packet_descriptor *iso) |
| 497 | { |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 498 | struct device *dev = caiaqdev_to_dev(cdev); |
| 499 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 500 | if (!cdev->streaming) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 501 | return; |
| 502 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 503 | if (iso->actual_length < cdev->bpp) |
Daniel Mack | 9311c9b | 2009-03-18 11:03:54 +0100 | [diff] [blame] | 504 | return; |
| 505 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 506 | switch (cdev->spec.data_alignment) { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 507 | case 0: |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 508 | read_in_urb_mode0(cdev, urb, iso); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 509 | break; |
| 510 | case 2: |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 511 | read_in_urb_mode2(cdev, urb, iso); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 512 | break; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 513 | case 3: |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 514 | read_in_urb_mode3(cdev, urb, iso); |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 515 | break; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 516 | } |
| 517 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 518 | if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) { |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 519 | dev_warn(dev, "streaming error detected %s %s\n", |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 520 | cdev->input_panic ? "(input)" : "", |
| 521 | cdev->output_panic ? "(output)" : ""); |
| 522 | cdev->warned = 1; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 523 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 526 | static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 527 | struct urb *urb, |
| 528 | const struct usb_iso_packet_descriptor *iso) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 529 | { |
| 530 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; |
| 531 | struct snd_pcm_substream *sub; |
| 532 | int stream, i; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 533 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 534 | for (i = 0; i < iso->length;) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 535 | for (stream = 0; stream < cdev->n_streams; stream++, i++) { |
| 536 | sub = cdev->sub_playback[stream]; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 537 | if (sub) { |
| 538 | struct snd_pcm_runtime *rt = sub->runtime; |
| 539 | char *audio_buf = rt->dma_area; |
| 540 | int sz = frames_to_bytes(rt, rt->buffer_size); |
Karsten Wiese | a971c3d4 | 2007-03-29 17:02:45 +0200 | [diff] [blame] | 541 | usb_buf[i] = |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 542 | audio_buf[cdev->audio_out_buf_pos[stream]]; |
| 543 | cdev->period_out_count[stream]++; |
| 544 | cdev->audio_out_buf_pos[stream]++; |
| 545 | if (cdev->audio_out_buf_pos[stream] == sz) |
| 546 | cdev->audio_out_buf_pos[stream] = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 547 | } else |
Karsten Wiese | a971c3d4 | 2007-03-29 17:02:45 +0200 | [diff] [blame] | 548 | usb_buf[i] = 0; |
| 549 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 550 | |
| 551 | /* fill in the check bytes */ |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 552 | if (cdev->spec.data_alignment == 2 && |
| 553 | i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == |
| 554 | (cdev->n_streams * CHANNELS_PER_STREAM)) |
| 555 | for (stream = 0; stream < cdev->n_streams; stream++, i++) |
| 556 | usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i); |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 560 | static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 561 | struct urb *urb, |
| 562 | const struct usb_iso_packet_descriptor *iso) |
| 563 | { |
| 564 | unsigned char *usb_buf = urb->transfer_buffer + iso->offset; |
| 565 | int stream, i; |
| 566 | |
| 567 | for (i = 0; i < iso->length;) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 568 | for (stream = 0; stream < cdev->n_streams; stream++) { |
| 569 | struct snd_pcm_substream *sub = cdev->sub_playback[stream]; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 570 | char *audio_buf = NULL; |
| 571 | int c, n, sz = 0; |
| 572 | |
| 573 | if (sub) { |
| 574 | struct snd_pcm_runtime *rt = sub->runtime; |
| 575 | audio_buf = rt->dma_area; |
| 576 | sz = frames_to_bytes(rt, rt->buffer_size); |
| 577 | } |
| 578 | |
| 579 | for (c = 0; c < CHANNELS_PER_STREAM; c++) { |
| 580 | for (n = 0; n < BYTES_PER_SAMPLE; n++) { |
| 581 | if (audio_buf) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 582 | usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++]; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 583 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 584 | if (cdev->audio_out_buf_pos[stream] == sz) |
| 585 | cdev->audio_out_buf_pos[stream] = 0; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 586 | } else { |
| 587 | usb_buf[i+n] = 0; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if (audio_buf) |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 592 | cdev->period_out_count[stream] += BYTES_PER_SAMPLE; |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 593 | |
| 594 | i += BYTES_PER_SAMPLE; |
| 595 | |
| 596 | /* fill in the check byte pattern */ |
| 597 | usb_buf[i++] = (stream << 1) | c; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 603 | static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 604 | struct urb *urb, |
| 605 | const struct usb_iso_packet_descriptor *iso) |
| 606 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 607 | switch (cdev->spec.data_alignment) { |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 608 | case 0: |
| 609 | case 2: |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 610 | fill_out_urb_mode_0(cdev, urb, iso); |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 611 | break; |
| 612 | case 3: |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 613 | fill_out_urb_mode_3(cdev, urb, iso); |
Daniel Mack | 15c5ab6 | 2010-09-10 17:04:57 +0800 | [diff] [blame] | 614 | break; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 615 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static void read_completed(struct urb *urb) |
| 619 | { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 620 | struct snd_usb_caiaq_cb_info *info = urb->context; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 621 | struct snd_usb_caiaqdev *cdev; |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 622 | struct device *dev; |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 623 | struct urb *out = NULL; |
| 624 | int i, frame, len, send_it = 0, outframe = 0; |
John Ogness | 9b11233 | 2018-07-01 17:28:07 +0200 | [diff] [blame] | 625 | unsigned long flags; |
Daniel Mack | 15439bd | 2011-08-05 13:49:52 +0200 | [diff] [blame] | 626 | size_t offset = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 627 | |
| 628 | if (urb->status || !info) |
| 629 | return; |
| 630 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 631 | cdev = info->cdev; |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 632 | dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 633 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 634 | if (!cdev->streaming) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 635 | return; |
| 636 | |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 637 | /* find an unused output urb that is unused */ |
| 638 | for (i = 0; i < N_URBS; i++) |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 639 | if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) { |
| 640 | out = cdev->data_urbs_out[i]; |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 641 | break; |
| 642 | } |
| 643 | |
| 644 | if (!out) { |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 645 | dev_err(dev, "Unable to find an output urb to use\n"); |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 646 | goto requeue; |
| 647 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 648 | |
| 649 | /* read the recently received packet and send back one which has |
| 650 | * the same layout */ |
| 651 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { |
| 652 | if (urb->iso_frame_desc[frame].status) |
| 653 | continue; |
| 654 | |
| 655 | len = urb->iso_frame_desc[outframe].actual_length; |
| 656 | out->iso_frame_desc[outframe].length = len; |
| 657 | out->iso_frame_desc[outframe].actual_length = 0; |
Daniel Mack | 15439bd | 2011-08-05 13:49:52 +0200 | [diff] [blame] | 658 | out->iso_frame_desc[outframe].offset = offset; |
| 659 | offset += len; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 660 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 661 | if (len > 0) { |
John Ogness | 9b11233 | 2018-07-01 17:28:07 +0200 | [diff] [blame] | 662 | spin_lock_irqsave(&cdev->spinlock, flags); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 663 | fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]); |
| 664 | read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]); |
John Ogness | 9b11233 | 2018-07-01 17:28:07 +0200 | [diff] [blame] | 665 | spin_unlock_irqrestore(&cdev->spinlock, flags); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 666 | check_for_elapsed_periods(cdev, cdev->sub_playback); |
| 667 | check_for_elapsed_periods(cdev, cdev->sub_capture); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 668 | send_it = 1; |
| 669 | } |
| 670 | |
| 671 | outframe++; |
| 672 | } |
| 673 | |
| 674 | if (send_it) { |
Daniel Mack | 15439bd | 2011-08-05 13:49:52 +0200 | [diff] [blame] | 675 | out->number_of_packets = outframe; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 676 | usb_submit_urb(out, GFP_ATOMIC); |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 677 | } else { |
| 678 | struct snd_usb_caiaq_cb_info *oinfo = out->context; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 679 | clear_bit(oinfo->index, &cdev->outurb_active_mask); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 680 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 681 | |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 682 | requeue: |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 683 | /* re-submit inbound urb */ |
| 684 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { |
| 685 | urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame; |
| 686 | urb->iso_frame_desc[frame].length = BYTES_PER_FRAME; |
| 687 | urb->iso_frame_desc[frame].actual_length = 0; |
| 688 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 689 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 690 | urb->number_of_packets = FRAMES_PER_URB; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 691 | usb_submit_urb(urb, GFP_ATOMIC); |
| 692 | } |
| 693 | |
| 694 | static void write_completed(struct urb *urb) |
| 695 | { |
| 696 | struct snd_usb_caiaq_cb_info *info = urb->context; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 697 | struct snd_usb_caiaqdev *cdev = info->cdev; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 698 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 699 | if (!cdev->output_running) { |
| 700 | cdev->output_running = 1; |
| 701 | wake_up(&cdev->prepare_wait_queue); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 702 | } |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 703 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 704 | clear_bit(info->index, &cdev->outurb_active_mask); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 707 | static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 708 | { |
| 709 | int i, frame; |
| 710 | struct urb **urbs; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 711 | struct usb_device *usb_dev = cdev->chip.dev; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 712 | unsigned int pipe; |
| 713 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 714 | pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ? |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 715 | usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) : |
| 716 | usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE); |
| 717 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 718 | urbs = kmalloc_array(N_URBS, sizeof(*urbs), GFP_KERNEL); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 719 | if (!urbs) { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 720 | *ret = -ENOMEM; |
| 721 | return NULL; |
| 722 | } |
| 723 | |
| 724 | for (i = 0; i < N_URBS; i++) { |
| 725 | urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL); |
| 726 | if (!urbs[i]) { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 727 | *ret = -ENOMEM; |
| 728 | return urbs; |
| 729 | } |
| 730 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 731 | urbs[i]->transfer_buffer = |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 732 | kmalloc_array(BYTES_PER_FRAME, FRAMES_PER_URB, |
| 733 | GFP_KERNEL); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 734 | if (!urbs[i]->transfer_buffer) { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 735 | *ret = -ENOMEM; |
| 736 | return urbs; |
| 737 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 738 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 739 | for (frame = 0; frame < FRAMES_PER_URB; frame++) { |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 740 | struct usb_iso_packet_descriptor *iso = |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 741 | &urbs[i]->iso_frame_desc[frame]; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 742 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 743 | iso->offset = BYTES_PER_FRAME * frame; |
| 744 | iso->length = BYTES_PER_FRAME; |
| 745 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 746 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 747 | urbs[i]->dev = usb_dev; |
| 748 | urbs[i]->pipe = pipe; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 749 | urbs[i]->transfer_buffer_length = FRAMES_PER_URB |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 750 | * BYTES_PER_FRAME; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 751 | urbs[i]->context = &cdev->data_cb_info[i]; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 752 | urbs[i]->interval = 1; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 753 | urbs[i]->number_of_packets = FRAMES_PER_URB; |
| 754 | urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ? |
| 755 | read_completed : write_completed; |
| 756 | } |
| 757 | |
| 758 | *ret = 0; |
| 759 | return urbs; |
| 760 | } |
| 761 | |
| 762 | static void free_urbs(struct urb **urbs) |
| 763 | { |
| 764 | int i; |
| 765 | |
| 766 | if (!urbs) |
| 767 | return; |
| 768 | |
| 769 | for (i = 0; i < N_URBS; i++) { |
| 770 | if (!urbs[i]) |
| 771 | continue; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 772 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 773 | usb_kill_urb(urbs[i]); |
| 774 | kfree(urbs[i]->transfer_buffer); |
| 775 | usb_free_urb(urbs[i]); |
| 776 | } |
| 777 | |
| 778 | kfree(urbs); |
| 779 | } |
| 780 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 781 | int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 782 | { |
| 783 | int i, ret; |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 784 | struct device *dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 785 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 786 | cdev->n_audio_in = max(cdev->spec.num_analog_audio_in, |
| 787 | cdev->spec.num_digital_audio_in) / |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 788 | CHANNELS_PER_STREAM; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 789 | cdev->n_audio_out = max(cdev->spec.num_analog_audio_out, |
| 790 | cdev->spec.num_digital_audio_out) / |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 791 | CHANNELS_PER_STREAM; |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 792 | cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 793 | |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 794 | dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in); |
| 795 | dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out); |
| 796 | dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 797 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 798 | if (cdev->n_streams > MAX_STREAMS) { |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 799 | dev_err(dev, "unable to initialize device, too many streams.\n"); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 800 | return -EINVAL; |
| 801 | } |
| 802 | |
Daniel Mack | 49cdd5b | 2015-01-04 19:59:29 +0100 | [diff] [blame] | 803 | if (cdev->n_streams < 1) { |
Daniel Mack | 897c329 | 2014-10-07 14:25:13 +0200 | [diff] [blame] | 804 | dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams); |
| 805 | return -EINVAL; |
| 806 | } |
| 807 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 808 | ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0, |
| 809 | cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 810 | |
| 811 | if (ret < 0) { |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 812 | dev_err(dev, "snd_pcm_new() returned %d\n", ret); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 813 | return ret; |
| 814 | } |
| 815 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 816 | cdev->pcm->private_data = cdev; |
| 817 | strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name)); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 818 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 819 | memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback)); |
| 820 | memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture)); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 821 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 822 | memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 823 | sizeof(snd_usb_caiaq_pcm_hardware)); |
| 824 | |
| 825 | /* setup samplerates */ |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 826 | cdev->samplerates = cdev->pcm_info.rates; |
| 827 | switch (cdev->chip.usb_id) { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 828 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): |
Daniel Mack | ad1e34b | 2007-09-17 14:45:14 +0200 | [diff] [blame] | 829 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3): |
Daniel Mack | f3e9d5d | 2008-05-08 15:42:15 +0200 | [diff] [blame] | 830 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO): |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 831 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE): |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 832 | cdev->samplerates |= SNDRV_PCM_RATE_192000; |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 833 | /* fall thru */ |
Daniel Mack | b30c494 | 2009-07-22 14:13:35 +0200 | [diff] [blame] | 834 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ): |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 835 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ): |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 836 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ): |
Daniel Mack | df8d81a | 2010-09-01 16:23:46 +0800 | [diff] [blame] | 837 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2): |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 838 | cdev->samplerates |= SNDRV_PCM_RATE_88200; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 839 | break; |
| 840 | } |
| 841 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 842 | snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 843 | &snd_usb_caiaq_ops); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 844 | snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 845 | &snd_usb_caiaq_ops); |
| 846 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 847 | cdev->data_cb_info = |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 848 | kmalloc_array(N_URBS, sizeof(struct snd_usb_caiaq_cb_info), |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 849 | GFP_KERNEL); |
| 850 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 851 | if (!cdev->data_cb_info) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 852 | return -ENOMEM; |
| 853 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 854 | cdev->outurb_active_mask = 0; |
| 855 | BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8)); |
Daniel Mack | da6094e | 2011-08-14 11:31:16 +0200 | [diff] [blame] | 856 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 857 | for (i = 0; i < N_URBS; i++) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 858 | cdev->data_cb_info[i].cdev = cdev; |
| 859 | cdev->data_cb_info[i].index = i; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 860 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 861 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 862 | cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 863 | if (ret < 0) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 864 | kfree(cdev->data_cb_info); |
| 865 | free_urbs(cdev->data_urbs_in); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 866 | return ret; |
| 867 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 868 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 869 | cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 870 | if (ret < 0) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 871 | kfree(cdev->data_cb_info); |
| 872 | free_urbs(cdev->data_urbs_in); |
| 873 | free_urbs(cdev->data_urbs_out); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 880 | void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 881 | { |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 882 | struct device *dev = caiaqdev_to_dev(cdev); |
| 883 | |
| 884 | dev_dbg(dev, "%s(%p)\n", __func__, cdev); |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 885 | stream_stop(cdev); |
| 886 | free_urbs(cdev->data_urbs_in); |
| 887 | free_urbs(cdev->data_urbs_out); |
| 888 | kfree(cdev->data_cb_info); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 889 | } |
| 890 | |