blob: 4795f128490636a48909b21816f4985cc66a46c0 [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
Markus Grabnere1a164d2010-08-23 01:08:25 +02002 * Line6 Linux USB driver - 0.9.1beta
Markus Grabner705ecec2009-02-27 19:43:04 -08003 *
Markus Grabner1027f4762010-08-12 01:35:30 +02004 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
Markus Grabner705ecec2009-02-27 19:43:04 -08005 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Markus Grabner705ecec2009-02-27 19:43:04 -080013#include <sound/core.h>
14#include <sound/control.h>
15#include <sound/pcm.h>
16#include <sound/pcm_params.h>
17
18#include "audio.h"
19#include "capture.h"
Markus Grabner1027f4762010-08-12 01:35:30 +020020#include "driver.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080021#include "playback.h"
22#include "pod.h"
23
Markus Grabner1027f4762010-08-12 01:35:30 +020024#ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
25
Markus Grabnere1a164d2010-08-23 01:08:25 +020026static struct snd_line6_pcm *dev2pcm(struct device *dev)
Markus Grabner1027f4762010-08-12 01:35:30 +020027{
28 struct usb_interface *interface = to_usb_interface(dev);
29 struct usb_line6 *line6 = usb_get_intfdata(interface);
30 struct snd_line6_pcm *line6pcm = line6->line6pcm;
31 return line6pcm;
32}
33
34/*
35 "read" request on "impulse_volume" special file.
36*/
37static ssize_t pcm_get_impulse_volume(struct device *dev,
Markus Grabnere1a164d2010-08-23 01:08:25 +020038 struct device_attribute *attr, char *buf)
Markus Grabner1027f4762010-08-12 01:35:30 +020039{
40 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_volume);
41}
42
43/*
44 "write" request on "impulse_volume" special file.
45*/
46static ssize_t pcm_set_impulse_volume(struct device *dev,
47 struct device_attribute *attr,
48 const char *buf, size_t count)
49{
50 struct snd_line6_pcm *line6pcm = dev2pcm(dev);
Johannes Thumshirncdf5e552012-08-06 14:08:50 +020051 int value;
Laurent Naveta3762902012-11-30 11:57:32 +010052 int ret;
Johannes Thumshirncdf5e552012-08-06 14:08:50 +020053
Laurent Naveta3762902012-11-30 11:57:32 +010054 ret = kstrtoint(buf, 10, &value);
55 if (ret < 0)
56 return ret;
Johannes Thumshirncdf5e552012-08-06 14:08:50 +020057
Markus Grabner1027f4762010-08-12 01:35:30 +020058 line6pcm->impulse_volume = value;
59
Markus Grabnere1a164d2010-08-23 01:08:25 +020060 if (value > 0)
Markus Grabner0ca54882012-01-20 00:09:09 +010061 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
Markus Grabner1027f4762010-08-12 01:35:30 +020062 else
Markus Grabner0ca54882012-01-20 00:09:09 +010063 line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
Markus Grabner1027f4762010-08-12 01:35:30 +020064
65 return count;
66}
67
68/*
69 "read" request on "impulse_period" special file.
70*/
71static ssize_t pcm_get_impulse_period(struct device *dev,
Markus Grabnere1a164d2010-08-23 01:08:25 +020072 struct device_attribute *attr, char *buf)
Markus Grabner1027f4762010-08-12 01:35:30 +020073{
74 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_period);
75}
76
77/*
78 "write" request on "impulse_period" special file.
79*/
80static ssize_t pcm_set_impulse_period(struct device *dev,
81 struct device_attribute *attr,
82 const char *buf, size_t count)
83{
Laurent Naveta3762902012-11-30 11:57:32 +010084 int value;
85 int ret;
86
87 ret = kstrtoint(buf, 10, &value);
88 if (ret < 0)
89 return ret;
90
91 dev2pcm(dev)->impulse_period = value;
Markus Grabner1027f4762010-08-12 01:35:30 +020092 return count;
93}
94
Greg Kroah-Hartmana3a972a2010-11-18 11:21:04 -080095static DEVICE_ATTR(impulse_volume, S_IWUSR | S_IRUGO, pcm_get_impulse_volume,
Markus Grabnere1a164d2010-08-23 01:08:25 +020096 pcm_set_impulse_volume);
Greg Kroah-Hartmana3a972a2010-11-18 11:21:04 -080097static DEVICE_ATTR(impulse_period, S_IWUSR | S_IRUGO, pcm_get_impulse_period,
Markus Grabnere1a164d2010-08-23 01:08:25 +020098 pcm_set_impulse_period);
Markus Grabner1027f4762010-08-12 01:35:30 +020099
100#endif
101
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100102static bool test_flags(unsigned long flags0, unsigned long flags1,
103 unsigned long mask)
104{
105 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
106}
107
Markus Grabner0ca54882012-01-20 00:09:09 +0100108int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f4762010-08-12 01:35:30 +0200109{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200110 unsigned long flags_old, flags_new, flags_final;
111 int err;
112
113 do {
114 flags_old = ACCESS_ONCE(line6pcm->flags);
115 flags_new = flags_old | channels;
116 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
117
118 flags_final = flags_old;
Johannes Thumshirn82a74d42012-05-05 16:31:52 +0200119
Markus Grabner1027f4762010-08-12 01:35:30 +0200120 line6pcm->prev_fbuf = NULL;
Markus Grabnere1a164d2010-08-23 01:08:25 +0200121
Markus Grabner0ca54882012-01-20 00:09:09 +0100122 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
Stefan Hajnoczi928f25e2012-11-11 13:24:41 +0100123 /* Invoked multiple times in a row so allocate once only */
Markus Grabner0ca54882012-01-20 00:09:09 +0100124 if (!line6pcm->buffer_in) {
125 line6pcm->buffer_in =
126 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
127 line6pcm->max_packet_size, GFP_KERNEL);
Markus Grabner0ca54882012-01-20 00:09:09 +0100128 if (!line6pcm->buffer_in) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100129 err = -ENOMEM;
130 goto pcm_acquire_error;
131 }
132
133 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
134 }
135 }
136
137 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
Markus Grabner1027f4762010-08-12 01:35:30 +0200138 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +0200139 Waiting for completion of active URBs in the stop handler is
140 a bug, we therefore report an error if capturing is restarted
141 too soon.
142 */
Markus Grabner0ca54882012-01-20 00:09:09 +0100143 if (line6pcm->active_urb_in | line6pcm->unlink_urb_in) {
144 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Markus Grabner1027f4762010-08-12 01:35:30 +0200145 return -EBUSY;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100146 }
147
Markus Grabner1027f4762010-08-12 01:35:30 +0200148 line6pcm->count_in = 0;
149 line6pcm->prev_fsize = 0;
150 err = line6_submit_audio_in_all_urbs(line6pcm);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200151
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100152 if (err < 0)
Markus Grabner0ca54882012-01-20 00:09:09 +0100153 goto pcm_acquire_error;
154
155 flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
Markus Grabner1027f4762010-08-12 01:35:30 +0200156 }
Markus Grabnere1a164d2010-08-23 01:08:25 +0200157
Markus Grabner0ca54882012-01-20 00:09:09 +0100158 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
Stefan Hajnoczi928f25e2012-11-11 13:24:41 +0100159 /* Invoked multiple times in a row so allocate once only */
Markus Grabner0ca54882012-01-20 00:09:09 +0100160 if (!line6pcm->buffer_out) {
161 line6pcm->buffer_out =
162 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
163 line6pcm->max_packet_size, GFP_KERNEL);
Markus Grabner0ca54882012-01-20 00:09:09 +0100164 if (!line6pcm->buffer_out) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100165 err = -ENOMEM;
166 goto pcm_acquire_error;
167 }
168
169 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
170 }
171 }
172
173 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
Markus Grabner1027f4762010-08-12 01:35:30 +0200174 /*
Markus Grabner0ca54882012-01-20 00:09:09 +0100175 See comment above regarding PCM restart.
176 */
177 if (line6pcm->active_urb_out | line6pcm->unlink_urb_out) {
178 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Markus Grabner1027f4762010-08-12 01:35:30 +0200179 return -EBUSY;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100180 }
181
Markus Grabner1027f4762010-08-12 01:35:30 +0200182 line6pcm->count_out = 0;
183 err = line6_submit_audio_out_all_urbs(line6pcm);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200184
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100185 if (err < 0)
Markus Grabner0ca54882012-01-20 00:09:09 +0100186 goto pcm_acquire_error;
187
188 flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
Markus Grabner1027f4762010-08-12 01:35:30 +0200189 }
Markus Grabnere1a164d2010-08-23 01:08:25 +0200190
Markus Grabner1027f4762010-08-12 01:35:30 +0200191 return 0;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100192
Markus Grabner0ca54882012-01-20 00:09:09 +0100193pcm_acquire_error:
194 /*
195 If not all requested resources/streams could be obtained, release
196 those which were successfully obtained (if any).
197 */
198 line6_pcm_release(line6pcm, flags_final & channels);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100199 return err;
Markus Grabner1027f4762010-08-12 01:35:30 +0200200}
201
Markus Grabner0ca54882012-01-20 00:09:09 +0100202int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f4762010-08-12 01:35:30 +0200203{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200204 unsigned long flags_old, flags_new;
205
206 do {
207 flags_old = ACCESS_ONCE(line6pcm->flags);
208 flags_new = flags_old & ~channels;
209 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
Markus Grabner1027f4762010-08-12 01:35:30 +0200210
Markus Grabner0ca54882012-01-20 00:09:09 +0100211 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
Markus Grabner1027f4762010-08-12 01:35:30 +0200212 line6_unlink_audio_in_urbs(line6pcm);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100213
Markus Grabner0ca54882012-01-20 00:09:09 +0100214 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
215 line6_wait_clear_audio_in_urbs(line6pcm);
216 line6_free_capture_buffer(line6pcm);
Markus Grabner1027f4762010-08-12 01:35:30 +0200217 }
218
Markus Grabner0ca54882012-01-20 00:09:09 +0100219 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
Markus Grabner1027f4762010-08-12 01:35:30 +0200220 line6_unlink_audio_out_urbs(line6pcm);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100221
Markus Grabner0ca54882012-01-20 00:09:09 +0100222 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
223 line6_wait_clear_audio_out_urbs(line6pcm);
224 line6_free_playback_buffer(line6pcm);
Markus Grabner1027f4762010-08-12 01:35:30 +0200225 }
Markus Grabner1027f4762010-08-12 01:35:30 +0200226
227 return 0;
228}
229
Markus Grabner705ecec2009-02-27 19:43:04 -0800230/* trigger callback */
231int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
232{
233 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800234 struct snd_pcm_substream *s;
235 int err;
236 unsigned long flags;
237
238 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
Markus Grabner0ca54882012-01-20 00:09:09 +0100239 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800240
Markus Grabner705ecec2009-02-27 19:43:04 -0800241 snd_pcm_group_for_each_entry(s, substream) {
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800242 switch (s->stream) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800243 case SNDRV_PCM_STREAM_PLAYBACK:
Markus Grabner1027f4762010-08-12 01:35:30 +0200244 err = snd_line6_playback_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800245
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800246 if (err < 0) {
247 spin_unlock_irqrestore(&line6pcm->lock_trigger,
248 flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800249 return err;
250 }
251
252 break;
253
254 case SNDRV_PCM_STREAM_CAPTURE:
Markus Grabner1027f4762010-08-12 01:35:30 +0200255 err = snd_line6_capture_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800256
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800257 if (err < 0) {
258 spin_unlock_irqrestore(&line6pcm->lock_trigger,
259 flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800260 return err;
261 }
262
263 break;
264
265 default:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200266 dev_err(line6pcm->line6->ifcdev,
267 "Unknown stream direction %d\n", s->stream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800268 }
269 }
270
271 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
272 return 0;
273}
274
275/* control info callback */
Markus Grabner1027f4762010-08-12 01:35:30 +0200276static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
277 struct snd_ctl_elem_info *uinfo)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800278{
Markus Grabner705ecec2009-02-27 19:43:04 -0800279 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
280 uinfo->count = 2;
281 uinfo->value.integer.min = 0;
282 uinfo->value.integer.max = 256;
283 return 0;
284}
285
286/* control get callback */
Markus Grabner1027f4762010-08-12 01:35:30 +0200287static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
288 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800289{
Markus Grabner705ecec2009-02-27 19:43:04 -0800290 int i;
291 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
292
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800293 for (i = 2; i--;)
Markus Grabner1027f4762010-08-12 01:35:30 +0200294 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800295
296 return 0;
297}
298
299/* control put callback */
Markus Grabner1027f4762010-08-12 01:35:30 +0200300static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
301 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800302{
Markus Grabner705ecec2009-02-27 19:43:04 -0800303 int i, changed = 0;
304 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
305
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800306 for (i = 2; i--;)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200307 if (line6pcm->volume_playback[i] !=
308 ucontrol->value.integer.value[i]) {
309 line6pcm->volume_playback[i] =
310 ucontrol->value.integer.value[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800311 changed = 1;
312 }
313
314 return changed;
315}
316
317/* control definition */
Markus Grabner1027f4762010-08-12 01:35:30 +0200318static struct snd_kcontrol_new line6_control_playback = {
Markus Grabner705ecec2009-02-27 19:43:04 -0800319 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
320 .name = "PCM Playback Volume",
321 .index = 0,
322 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Markus Grabner1027f4762010-08-12 01:35:30 +0200323 .info = snd_line6_control_playback_info,
324 .get = snd_line6_control_playback_get,
325 .put = snd_line6_control_playback_put
Markus Grabner705ecec2009-02-27 19:43:04 -0800326};
327
328/*
329 Cleanup the PCM device.
330*/
331static void line6_cleanup_pcm(struct snd_pcm *pcm)
332{
333 int i;
334 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
335
Markus Grabner1027f4762010-08-12 01:35:30 +0200336#ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
337 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
338 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
339#endif
340
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800341 for (i = LINE6_ISO_BUFFERS; i--;) {
342 if (line6pcm->urb_audio_out[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800343 usb_kill_urb(line6pcm->urb_audio_out[i]);
344 usb_free_urb(line6pcm->urb_audio_out[i]);
345 }
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800346 if (line6pcm->urb_audio_in[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800347 usb_kill_urb(line6pcm->urb_audio_in[i]);
348 usb_free_urb(line6pcm->urb_audio_in[i]);
349 }
350 }
351}
352
353/* create a PCM device */
354static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
355{
356 struct snd_pcm *pcm;
357 int err;
358
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800359 err = snd_pcm_new(line6pcm->line6->card,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200360 (char *)line6pcm->line6->properties->name,
361 0, 1, 1, &pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800362 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800363 return err;
364
365 pcm->private_data = line6pcm;
366 pcm->private_free = line6_cleanup_pcm;
367 line6pcm->pcm = pcm;
368 strcpy(pcm->name, line6pcm->line6->properties->name);
369
370 /* set operators */
Shawn Bohrerafb90912009-11-15 22:17:57 -0600371 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
372 &snd_line6_playback_ops);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200373 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
Markus Grabner705ecec2009-02-27 19:43:04 -0800374
375 /* pre-allocation of buffers */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800376 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200377 snd_dma_continuous_data
378 (GFP_KERNEL), 64 * 1024,
379 128 * 1024);
Markus Grabner705ecec2009-02-27 19:43:04 -0800380
381 return 0;
382}
383
384/* PCM device destructor */
385static int snd_line6_pcm_free(struct snd_device *device)
386{
387 return 0;
388}
389
390/*
Markus Grabner1027f4762010-08-12 01:35:30 +0200391 Stop substream if still running.
392*/
393static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
394{
Greg Kroah-Hartman027360c2010-09-21 16:58:00 -0700395 if (substream->runtime && snd_pcm_running(substream))
Markus Grabner1027f4762010-08-12 01:35:30 +0200396 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
Markus Grabner1027f4762010-08-12 01:35:30 +0200397}
398
399/*
400 Stop PCM stream.
401*/
402void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
403{
Markus Grabnere1a164d2010-08-23 01:08:25 +0200404 pcm_disconnect_substream(get_substream
405 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
406 pcm_disconnect_substream(get_substream
407 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
Markus Grabner1027f4762010-08-12 01:35:30 +0200408 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
409 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
410}
411
412/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800413 Create and register the PCM device and mixer entries.
414 Create URBs for playback and capture.
415*/
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800416int line6_init_pcm(struct usb_line6 *line6,
417 struct line6_pcm_properties *properties)
Markus Grabner705ecec2009-02-27 19:43:04 -0800418{
419 static struct snd_device_ops pcm_ops = {
420 .dev_free = snd_line6_pcm_free,
421 };
422
423 int err;
424 int ep_read = 0, ep_write = 0;
425 struct snd_line6_pcm *line6pcm;
426
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800427 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
Markus Grabnere1a164d2010-08-23 01:08:25 +0200428 return 0; /* skip PCM initialization and report success */
Markus Grabner705ecec2009-02-27 19:43:04 -0800429
430 /* initialize PCM subsystem based on product id: */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800431 switch (line6->product) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800432 case LINE6_DEVID_BASSPODXT:
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800433 case LINE6_DEVID_BASSPODXTLIVE:
434 case LINE6_DEVID_BASSPODXTPRO:
435 case LINE6_DEVID_PODXT:
436 case LINE6_DEVID_PODXTLIVE:
437 case LINE6_DEVID_PODXTPRO:
Stefan Hajnoczi16dc1042011-11-23 08:20:42 +0000438 case LINE6_DEVID_PODHD300:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200439 ep_read = 0x82;
Markus Grabner705ecec2009-02-27 19:43:04 -0800440 ep_write = 0x01;
441 break;
442
Markus Grabner4c6fb5f2011-12-05 23:51:53 +0100443 case LINE6_DEVID_PODHD500:
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800444 case LINE6_DEVID_PODX3:
445 case LINE6_DEVID_PODX3LIVE:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200446 ep_read = 0x86;
Markus Grabner705ecec2009-02-27 19:43:04 -0800447 ep_write = 0x02;
448 break;
449
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800450 case LINE6_DEVID_POCKETPOD:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200451 ep_read = 0x82;
Markus Grabner705ecec2009-02-27 19:43:04 -0800452 ep_write = 0x02;
453 break;
454
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800455 case LINE6_DEVID_GUITARPORT:
Markus Grabner1027f4762010-08-12 01:35:30 +0200456 case LINE6_DEVID_PODSTUDIO_GX:
457 case LINE6_DEVID_PODSTUDIO_UX1:
458 case LINE6_DEVID_PODSTUDIO_UX2:
Markus Grabner705ecec2009-02-27 19:43:04 -0800459 case LINE6_DEVID_TONEPORT_GX:
Markus Grabner1027f4762010-08-12 01:35:30 +0200460 case LINE6_DEVID_TONEPORT_UX1:
461 case LINE6_DEVID_TONEPORT_UX2:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200462 ep_read = 0x82;
Markus Grabner705ecec2009-02-27 19:43:04 -0800463 ep_write = 0x01;
464 break;
465
Laurent Naveta3762902012-11-30 11:57:32 +0100466 /* this is for interface_number == 1:
467 case LINE6_DEVID_TONEPORT_UX2:
468 case LINE6_DEVID_PODSTUDIO_UX2:
469 ep_read = 0x87;
470 ep_write = 0x00;
471 break; */
Markus Grabner705ecec2009-02-27 19:43:04 -0800472
473 default:
474 MISSING_CASE;
475 }
476
477 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
478
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800479 if (line6pcm == NULL)
Markus Grabner705ecec2009-02-27 19:43:04 -0800480 return -ENOMEM;
481
Markus Grabner1027f4762010-08-12 01:35:30 +0200482 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
483 line6pcm->volume_monitor = 255;
Markus Grabner705ecec2009-02-27 19:43:04 -0800484 line6pcm->line6 = line6;
485 line6pcm->ep_audio_read = ep_read;
486 line6pcm->ep_audio_write = ep_write;
Stefan Hajnoczi3b08db32011-11-23 08:20:44 +0000487
488 /* Read and write buffers are sized identically, so choose minimum */
489 line6pcm->max_packet_size = min(
490 usb_maxpacket(line6->usbdev,
491 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
492 usb_maxpacket(line6->usbdev,
493 usb_sndisocpipe(line6->usbdev, ep_write), 1));
494
Markus Grabner705ecec2009-02-27 19:43:04 -0800495 line6pcm->properties = properties;
496 line6->line6pcm = line6pcm;
497
498 /* PCM device: */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800499 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
500 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800501 return err;
502
503 snd_card_set_dev(line6->card, line6->ifcdev);
504
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800505 err = snd_line6_new_pcm(line6pcm);
506 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800507 return err;
508
509 spin_lock_init(&line6pcm->lock_audio_out);
510 spin_lock_init(&line6pcm->lock_audio_in);
511 spin_lock_init(&line6pcm->lock_trigger);
512
Markus Grabner1027f4762010-08-12 01:35:30 +0200513 err = line6_create_audio_out_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800514 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800515 return err;
516
Markus Grabner1027f4762010-08-12 01:35:30 +0200517 err = line6_create_audio_in_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800518 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800519 return err;
520
521 /* mixer: */
Markus Grabnere1a164d2010-08-23 01:08:25 +0200522 err =
523 snd_ctl_add(line6->card,
524 snd_ctl_new1(&line6_control_playback, line6pcm));
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800525 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800526 return err;
527
Markus Grabner1027f4762010-08-12 01:35:30 +0200528#ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
529 /* impulse response test: */
530 err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
531 if (err < 0)
532 return err;
533
534 err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
535 if (err < 0)
536 return err;
537
538 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
539#endif
540
Markus Grabner705ecec2009-02-27 19:43:04 -0800541 return 0;
542}
543
544/* prepare pcm callback */
545int snd_line6_prepare(struct snd_pcm_substream *substream)
546{
547 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
548
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100549 switch (substream->stream) {
550 case SNDRV_PCM_STREAM_PLAYBACK:
Markus Grabner0ca54882012-01-20 00:09:09 +0100551 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100552 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
553
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100554 break;
555
556 case SNDRV_PCM_STREAM_CAPTURE:
Markus Grabner0ca54882012-01-20 00:09:09 +0100557 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100558 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
559
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100560 break;
561
562 default:
563 MISSING_CASE;
564 }
565
Markus Grabner0ca54882012-01-20 00:09:09 +0100566 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
Markus Grabner1027f4762010-08-12 01:35:30 +0200567 line6pcm->count_out = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800568 line6pcm->pos_out = 0;
569 line6pcm->pos_out_done = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800570 line6pcm->bytes_out = 0;
Markus Grabner1027f4762010-08-12 01:35:30 +0200571 line6pcm->count_in = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800572 line6pcm->pos_in_done = 0;
573 line6pcm->bytes_in = 0;
574 }
575
576 return 0;
577}