blob: fbe4b083eac5bdb9ddd19f9c8a9eca48a6748193 [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
2 * Line6 Linux USB driver - 0.8.0
3 *
4 * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2.
9 *
10 */
11
12#include "driver.h"
13
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
15
Markus Grabner705ecec2009-02-27 19:43:04 -080016#include <sound/core.h>
17#include <sound/control.h>
18#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20
21#include "audio.h"
22#include "capture.h"
23#include "playback.h"
24#include "pod.h"
25
26
27/* trigger callback */
28int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
29{
30 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
Markus Grabner705ecec2009-02-27 19:43:04 -080031 struct snd_pcm_substream *s;
32 int err;
33 unsigned long flags;
34
35 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
36 clear_bit(BIT_PREPARED, &line6pcm->flags);
37
Markus Grabner705ecec2009-02-27 19:43:04 -080038 snd_pcm_group_for_each_entry(s, substream) {
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -080039 switch (s->stream) {
Markus Grabner705ecec2009-02-27 19:43:04 -080040 case SNDRV_PCM_STREAM_PLAYBACK:
41 err = snd_line6_playback_trigger(s, cmd);
42
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -080043 if (err < 0) {
44 spin_unlock_irqrestore(&line6pcm->lock_trigger,
45 flags);
Markus Grabner705ecec2009-02-27 19:43:04 -080046 return err;
47 }
48
49 break;
50
51 case SNDRV_PCM_STREAM_CAPTURE:
52 err = snd_line6_capture_trigger(s, cmd);
53
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -080054 if (err < 0) {
55 spin_unlock_irqrestore(&line6pcm->lock_trigger,
56 flags);
Markus Grabner705ecec2009-02-27 19:43:04 -080057 return err;
58 }
59
60 break;
61
62 default:
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -080063 dev_err(s2m(substream), "Unknown stream direction %d\n",
64 s->stream);
Markus Grabner705ecec2009-02-27 19:43:04 -080065 }
66 }
67
68 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
69 return 0;
70}
71
72/* control info callback */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -080073static int snd_line6_control_info(struct snd_kcontrol *kcontrol,
74 struct snd_ctl_elem_info *uinfo)
75{
Markus Grabner705ecec2009-02-27 19:43:04 -080076 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
77 uinfo->count = 2;
78 uinfo->value.integer.min = 0;
79 uinfo->value.integer.max = 256;
80 return 0;
81}
82
83/* control get callback */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -080084static int snd_line6_control_get(struct snd_kcontrol *kcontrol,
85 struct snd_ctl_elem_value *ucontrol)
86{
Markus Grabner705ecec2009-02-27 19:43:04 -080087 int i;
88 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
89
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -080090 for (i = 2; i--;)
Markus Grabner705ecec2009-02-27 19:43:04 -080091 ucontrol->value.integer.value[i] = line6pcm->volume[i];
92
93 return 0;
94}
95
96/* control put callback */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -080097static int snd_line6_control_put(struct snd_kcontrol *kcontrol,
98 struct snd_ctl_elem_value *ucontrol)
99{
Markus Grabner705ecec2009-02-27 19:43:04 -0800100 int i, changed = 0;
101 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
102
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800103 for (i = 2; i--;)
104 if (line6pcm->volume[i] != ucontrol->value.integer.value[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800105 line6pcm->volume[i] = ucontrol->value.integer.value[i];
106 changed = 1;
107 }
108
109 return changed;
110}
111
112/* control definition */
113static struct snd_kcontrol_new line6_control = {
114 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
115 .name = "PCM Playback Volume",
116 .index = 0,
117 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
118 .info = snd_line6_control_info,
119 .get = snd_line6_control_get,
120 .put = snd_line6_control_put
121};
122
123/*
124 Cleanup the PCM device.
125*/
126static void line6_cleanup_pcm(struct snd_pcm *pcm)
127{
128 int i;
129 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
130
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800131 for (i = LINE6_ISO_BUFFERS; i--;) {
132 if (line6pcm->urb_audio_out[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800133 usb_kill_urb(line6pcm->urb_audio_out[i]);
134 usb_free_urb(line6pcm->urb_audio_out[i]);
135 }
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800136 if (line6pcm->urb_audio_in[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800137 usb_kill_urb(line6pcm->urb_audio_in[i]);
138 usb_free_urb(line6pcm->urb_audio_in[i]);
139 }
140 }
141}
142
143/* create a PCM device */
144static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
145{
146 struct snd_pcm *pcm;
147 int err;
148
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800149 err = snd_pcm_new(line6pcm->line6->card,
150 (char *)line6pcm->line6->properties->name,
151 0, 1, 1, &pcm);
152 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800153 return err;
154
155 pcm->private_data = line6pcm;
156 pcm->private_free = line6_cleanup_pcm;
157 line6pcm->pcm = pcm;
158 strcpy(pcm->name, line6pcm->line6->properties->name);
159
160 /* set operators */
Shawn Bohrerafb90912009-11-15 22:17:57 -0600161 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
162 &snd_line6_playback_ops);
Markus Grabner705ecec2009-02-27 19:43:04 -0800163 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
164
165 /* pre-allocation of buffers */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800166 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
167 snd_dma_continuous_data(GFP_KERNEL),
168 64 * 1024, 128 * 1024);
Markus Grabner705ecec2009-02-27 19:43:04 -0800169
170 return 0;
171}
172
173/* PCM device destructor */
174static int snd_line6_pcm_free(struct snd_device *device)
175{
176 return 0;
177}
178
179/*
180 Create and register the PCM device and mixer entries.
181 Create URBs for playback and capture.
182*/
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800183int line6_init_pcm(struct usb_line6 *line6,
184 struct line6_pcm_properties *properties)
Markus Grabner705ecec2009-02-27 19:43:04 -0800185{
186 static struct snd_device_ops pcm_ops = {
187 .dev_free = snd_line6_pcm_free,
188 };
189
190 int err;
191 int ep_read = 0, ep_write = 0;
192 struct snd_line6_pcm *line6pcm;
193
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800194 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
Markus Grabner705ecec2009-02-27 19:43:04 -0800195 return 0; /* skip PCM initialization and report success */
196
197 /* initialize PCM subsystem based on product id: */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800198 switch (line6->product) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800199 case LINE6_DEVID_BASSPODXT:
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800200 case LINE6_DEVID_BASSPODXTLIVE:
201 case LINE6_DEVID_BASSPODXTPRO:
202 case LINE6_DEVID_PODXT:
203 case LINE6_DEVID_PODXTLIVE:
204 case LINE6_DEVID_PODXTPRO:
Markus Grabner705ecec2009-02-27 19:43:04 -0800205 ep_read = 0x82;
206 ep_write = 0x01;
207 break;
208
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800209 case LINE6_DEVID_PODX3:
210 case LINE6_DEVID_PODX3LIVE:
Markus Grabner705ecec2009-02-27 19:43:04 -0800211 ep_read = 0x86;
212 ep_write = 0x02;
213 break;
214
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800215 case LINE6_DEVID_POCKETPOD:
Markus Grabner705ecec2009-02-27 19:43:04 -0800216 ep_read = 0x82;
217 ep_write = 0x02;
218 break;
219
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800220 case LINE6_DEVID_GUITARPORT:
Markus Grabner705ecec2009-02-27 19:43:04 -0800221 case LINE6_DEVID_TONEPORT_GX:
222 ep_read = 0x82;
223 ep_write = 0x01;
224 break;
225
226 case LINE6_DEVID_TONEPORT_UX1:
227 ep_read = 0x00;
228 ep_write = 0x00;
229 break;
230
231 case LINE6_DEVID_TONEPORT_UX2:
232 ep_read = 0x87;
233 ep_write = 0x00;
234 break;
235
236 default:
237 MISSING_CASE;
238 }
239
240 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
241
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800242 if (line6pcm == NULL)
Markus Grabner705ecec2009-02-27 19:43:04 -0800243 return -ENOMEM;
244
245 line6pcm->volume[0] = line6pcm->volume[1] = 128;
246 line6pcm->line6 = line6;
247 line6pcm->ep_audio_read = ep_read;
248 line6pcm->ep_audio_write = ep_write;
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800249 line6pcm->max_packet_size = usb_maxpacket(line6->usbdev,
250 usb_rcvintpipe(line6->usbdev,
251 ep_read),
252 0);
Markus Grabner705ecec2009-02-27 19:43:04 -0800253 line6pcm->properties = properties;
254 line6->line6pcm = line6pcm;
255
256 /* PCM device: */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800257 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
258 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800259 return err;
260
261 snd_card_set_dev(line6->card, line6->ifcdev);
262
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800263 err = snd_line6_new_pcm(line6pcm);
264 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800265 return err;
266
267 spin_lock_init(&line6pcm->lock_audio_out);
268 spin_lock_init(&line6pcm->lock_audio_in);
269 spin_lock_init(&line6pcm->lock_trigger);
270
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800271 err = create_audio_out_urbs(line6pcm);
272 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800273 return err;
274
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800275 err = create_audio_in_urbs(line6pcm);
276 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800277 return err;
278
279 /* mixer: */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800280 err = snd_ctl_add(line6->card, snd_ctl_new1(&line6_control, line6pcm));
281 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800282 return err;
283
284 return 0;
285}
286
287/* prepare pcm callback */
288int snd_line6_prepare(struct snd_pcm_substream *substream)
289{
290 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
291
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800292 if (!test_and_set_bit(BIT_PREPARED, &line6pcm->flags)) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800293 unlink_wait_clear_audio_out_urbs(line6pcm);
294 line6pcm->pos_out = 0;
295 line6pcm->pos_out_done = 0;
296
297 unlink_wait_clear_audio_in_urbs(line6pcm);
298 line6pcm->bytes_out = 0;
299 line6pcm->pos_in_done = 0;
300 line6pcm->bytes_in = 0;
301 }
302
303 return 0;
304}