1 /*
2 * Line6 Linux USB driver - 0.9.1beta
3 *
4 * Copyright (C) 2004-2010 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 <linux/slab.h>
13 #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"
20 #include "driver.h"
21 #include "playback.h"
22 #include "pod.h"
23
24 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
25
dev2pcm(struct device * dev)26 static struct snd_line6_pcm *dev2pcm(struct device *dev)
27 {
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 */
pcm_get_impulse_volume(struct device * dev,struct device_attribute * attr,char * buf)37 static ssize_t pcm_get_impulse_volume(struct device *dev,
38 struct device_attribute *attr, char *buf)
39 {
40 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_volume);
41 }
42
43 /*
44 "write" request on "impulse_volume" special file.
45 */
pcm_set_impulse_volume(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)46 static 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);
51 int value = simple_strtoul(buf, NULL, 10);
52 line6pcm->impulse_volume = value;
53
54 if (value > 0)
55 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
56 else
57 line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
58
59 return count;
60 }
61
62 /*
63 "read" request on "impulse_period" special file.
64 */
pcm_get_impulse_period(struct device * dev,struct device_attribute * attr,char * buf)65 static ssize_t pcm_get_impulse_period(struct device *dev,
66 struct device_attribute *attr, char *buf)
67 {
68 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_period);
69 }
70
71 /*
72 "write" request on "impulse_period" special file.
73 */
pcm_set_impulse_period(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)74 static ssize_t pcm_set_impulse_period(struct device *dev,
75 struct device_attribute *attr,
76 const char *buf, size_t count)
77 {
78 dev2pcm(dev)->impulse_period = simple_strtoul(buf, NULL, 10);
79 return count;
80 }
81
82 static DEVICE_ATTR(impulse_volume, S_IWUSR | S_IRUGO, pcm_get_impulse_volume,
83 pcm_set_impulse_volume);
84 static DEVICE_ATTR(impulse_period, S_IWUSR | S_IRUGO, pcm_get_impulse_period,
85 pcm_set_impulse_period);
86
87 #endif
88
test_flags(unsigned long flags0,unsigned long flags1,unsigned long mask)89 static bool test_flags(unsigned long flags0, unsigned long flags1,
90 unsigned long mask)
91 {
92 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
93 }
94
line6_pcm_acquire(struct snd_line6_pcm * line6pcm,int channels)95 int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
96 {
97 unsigned long flags_old =
98 __sync_fetch_and_or(&line6pcm->flags, channels);
99 unsigned long flags_new = flags_old | channels;
100 unsigned long flags_final = flags_old;
101 int err = 0;
102
103 line6pcm->prev_fbuf = NULL;
104
105 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
106 /* We may be invoked multiple times in a row so allocate once only */
107 if (!line6pcm->buffer_in) {
108 line6pcm->buffer_in =
109 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
110 line6pcm->max_packet_size, GFP_KERNEL);
111
112 if (!line6pcm->buffer_in) {
113 dev_err(line6pcm->line6->ifcdev,
114 "cannot malloc capture buffer\n");
115 err = -ENOMEM;
116 goto pcm_acquire_error;
117 }
118
119 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
120 }
121 }
122
123 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
124 /*
125 Waiting for completion of active URBs in the stop handler is
126 a bug, we therefore report an error if capturing is restarted
127 too soon.
128 */
129 if (line6pcm->active_urb_in | line6pcm->unlink_urb_in) {
130 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
131 return -EBUSY;
132 }
133
134 line6pcm->count_in = 0;
135 line6pcm->prev_fsize = 0;
136 err = line6_submit_audio_in_all_urbs(line6pcm);
137
138 if (err < 0)
139 goto pcm_acquire_error;
140
141 flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
142 }
143
144 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
145 /* We may be invoked multiple times in a row so allocate once only */
146 if (!line6pcm->buffer_out) {
147 line6pcm->buffer_out =
148 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
149 line6pcm->max_packet_size, GFP_KERNEL);
150
151 if (!line6pcm->buffer_out) {
152 dev_err(line6pcm->line6->ifcdev,
153 "cannot malloc playback buffer\n");
154 err = -ENOMEM;
155 goto pcm_acquire_error;
156 }
157
158 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
159 }
160 }
161
162 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
163 /*
164 See comment above regarding PCM restart.
165 */
166 if (line6pcm->active_urb_out | line6pcm->unlink_urb_out) {
167 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
168 return -EBUSY;
169 }
170
171 line6pcm->count_out = 0;
172 err = line6_submit_audio_out_all_urbs(line6pcm);
173
174 if (err < 0)
175 goto pcm_acquire_error;
176
177 flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
178 }
179
180 return 0;
181
182 pcm_acquire_error:
183 /*
184 If not all requested resources/streams could be obtained, release
185 those which were successfully obtained (if any).
186 */
187 line6_pcm_release(line6pcm, flags_final & channels);
188 return err;
189 }
190
line6_pcm_release(struct snd_line6_pcm * line6pcm,int channels)191 int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
192 {
193 unsigned long flags_old =
194 __sync_fetch_and_and(&line6pcm->flags, ~channels);
195 unsigned long flags_new = flags_old & ~channels;
196
197 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
198 line6_unlink_audio_in_urbs(line6pcm);
199
200 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
201 line6_wait_clear_audio_in_urbs(line6pcm);
202 line6_free_capture_buffer(line6pcm);
203 }
204
205 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
206 line6_unlink_audio_out_urbs(line6pcm);
207
208 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
209 line6_wait_clear_audio_out_urbs(line6pcm);
210 line6_free_playback_buffer(line6pcm);
211 }
212
213 return 0;
214 }
215
216 /* trigger callback */
snd_line6_trigger(struct snd_pcm_substream * substream,int cmd)217 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
218 {
219 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
220 struct snd_pcm_substream *s;
221 int err;
222 unsigned long flags;
223
224 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
225 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
226
227 snd_pcm_group_for_each_entry(s, substream) {
228 switch (s->stream) {
229 case SNDRV_PCM_STREAM_PLAYBACK:
230 err = snd_line6_playback_trigger(line6pcm, cmd);
231
232 if (err < 0) {
233 spin_unlock_irqrestore(&line6pcm->lock_trigger,
234 flags);
235 return err;
236 }
237
238 break;
239
240 case SNDRV_PCM_STREAM_CAPTURE:
241 err = snd_line6_capture_trigger(line6pcm, cmd);
242
243 if (err < 0) {
244 spin_unlock_irqrestore(&line6pcm->lock_trigger,
245 flags);
246 return err;
247 }
248
249 break;
250
251 default:
252 dev_err(line6pcm->line6->ifcdev,
253 "Unknown stream direction %d\n", s->stream);
254 }
255 }
256
257 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
258 return 0;
259 }
260
261 /* control info callback */
snd_line6_control_playback_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)262 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
263 struct snd_ctl_elem_info *uinfo)
264 {
265 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
266 uinfo->count = 2;
267 uinfo->value.integer.min = 0;
268 uinfo->value.integer.max = 256;
269 return 0;
270 }
271
272 /* control get callback */
snd_line6_control_playback_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)273 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
274 struct snd_ctl_elem_value *ucontrol)
275 {
276 int i;
277 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
278
279 for (i = 2; i--;)
280 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
281
282 return 0;
283 }
284
285 /* control put callback */
snd_line6_control_playback_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)286 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
287 struct snd_ctl_elem_value *ucontrol)
288 {
289 int i, changed = 0;
290 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
291
292 for (i = 2; i--;)
293 if (line6pcm->volume_playback[i] !=
294 ucontrol->value.integer.value[i]) {
295 line6pcm->volume_playback[i] =
296 ucontrol->value.integer.value[i];
297 changed = 1;
298 }
299
300 return changed;
301 }
302
303 /* control definition */
304 static struct snd_kcontrol_new line6_control_playback = {
305 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
306 .name = "PCM Playback Volume",
307 .index = 0,
308 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
309 .info = snd_line6_control_playback_info,
310 .get = snd_line6_control_playback_get,
311 .put = snd_line6_control_playback_put
312 };
313
314 /*
315 Cleanup the PCM device.
316 */
line6_cleanup_pcm(struct snd_pcm * pcm)317 static void line6_cleanup_pcm(struct snd_pcm *pcm)
318 {
319 int i;
320 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
321
322 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
323 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
324 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
325 #endif
326
327 for (i = LINE6_ISO_BUFFERS; i--;) {
328 if (line6pcm->urb_audio_out[i]) {
329 usb_kill_urb(line6pcm->urb_audio_out[i]);
330 usb_free_urb(line6pcm->urb_audio_out[i]);
331 }
332 if (line6pcm->urb_audio_in[i]) {
333 usb_kill_urb(line6pcm->urb_audio_in[i]);
334 usb_free_urb(line6pcm->urb_audio_in[i]);
335 }
336 }
337 }
338
339 /* create a PCM device */
snd_line6_new_pcm(struct snd_line6_pcm * line6pcm)340 static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
341 {
342 struct snd_pcm *pcm;
343 int err;
344
345 err = snd_pcm_new(line6pcm->line6->card,
346 (char *)line6pcm->line6->properties->name,
347 0, 1, 1, &pcm);
348 if (err < 0)
349 return err;
350
351 pcm->private_data = line6pcm;
352 pcm->private_free = line6_cleanup_pcm;
353 line6pcm->pcm = pcm;
354 strcpy(pcm->name, line6pcm->line6->properties->name);
355
356 /* set operators */
357 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
358 &snd_line6_playback_ops);
359 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
360
361 /* pre-allocation of buffers */
362 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
363 snd_dma_continuous_data
364 (GFP_KERNEL), 64 * 1024,
365 128 * 1024);
366
367 return 0;
368 }
369
370 /* PCM device destructor */
snd_line6_pcm_free(struct snd_device * device)371 static int snd_line6_pcm_free(struct snd_device *device)
372 {
373 return 0;
374 }
375
376 /*
377 Stop substream if still running.
378 */
pcm_disconnect_substream(struct snd_pcm_substream * substream)379 static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
380 {
381 if (substream->runtime && snd_pcm_running(substream)) {
382 snd_pcm_stream_lock_irq(substream);
383 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
384 snd_pcm_stream_unlock_irq(substream);
385 }
386 }
387
388 /*
389 Stop PCM stream.
390 */
line6_pcm_disconnect(struct snd_line6_pcm * line6pcm)391 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
392 {
393 pcm_disconnect_substream(get_substream
394 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
395 pcm_disconnect_substream(get_substream
396 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
397 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
398 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
399 }
400
401 /*
402 Create and register the PCM device and mixer entries.
403 Create URBs for playback and capture.
404 */
line6_init_pcm(struct usb_line6 * line6,struct line6_pcm_properties * properties)405 int line6_init_pcm(struct usb_line6 *line6,
406 struct line6_pcm_properties *properties)
407 {
408 static struct snd_device_ops pcm_ops = {
409 .dev_free = snd_line6_pcm_free,
410 };
411
412 int err;
413 int ep_read = 0, ep_write = 0;
414 struct snd_line6_pcm *line6pcm;
415
416 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
417 return 0; /* skip PCM initialization and report success */
418
419 /* initialize PCM subsystem based on product id: */
420 switch (line6->product) {
421 case LINE6_DEVID_BASSPODXT:
422 case LINE6_DEVID_BASSPODXTLIVE:
423 case LINE6_DEVID_BASSPODXTPRO:
424 case LINE6_DEVID_PODXT:
425 case LINE6_DEVID_PODXTLIVE:
426 case LINE6_DEVID_PODXTPRO:
427 case LINE6_DEVID_PODHD300:
428 ep_read = 0x82;
429 ep_write = 0x01;
430 break;
431
432 case LINE6_DEVID_PODHD500:
433 case LINE6_DEVID_PODX3:
434 case LINE6_DEVID_PODX3LIVE:
435 ep_read = 0x86;
436 ep_write = 0x02;
437 break;
438
439 case LINE6_DEVID_POCKETPOD:
440 ep_read = 0x82;
441 ep_write = 0x02;
442 break;
443
444 case LINE6_DEVID_GUITARPORT:
445 case LINE6_DEVID_PODSTUDIO_GX:
446 case LINE6_DEVID_PODSTUDIO_UX1:
447 case LINE6_DEVID_PODSTUDIO_UX2:
448 case LINE6_DEVID_TONEPORT_GX:
449 case LINE6_DEVID_TONEPORT_UX1:
450 case LINE6_DEVID_TONEPORT_UX2:
451 ep_read = 0x82;
452 ep_write = 0x01;
453 break;
454
455 /* this is for interface_number == 1:
456 case LINE6_DEVID_TONEPORT_UX2:
457 case LINE6_DEVID_PODSTUDIO_UX2:
458 ep_read = 0x87;
459 ep_write = 0x00;
460 break;
461 */
462
463 default:
464 MISSING_CASE;
465 }
466
467 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
468
469 if (line6pcm == NULL)
470 return -ENOMEM;
471
472 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
473 line6pcm->volume_monitor = 255;
474 line6pcm->line6 = line6;
475 line6pcm->ep_audio_read = ep_read;
476 line6pcm->ep_audio_write = ep_write;
477
478 /* Read and write buffers are sized identically, so choose minimum */
479 line6pcm->max_packet_size = min(
480 usb_maxpacket(line6->usbdev,
481 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
482 usb_maxpacket(line6->usbdev,
483 usb_sndisocpipe(line6->usbdev, ep_write), 1));
484
485 line6pcm->properties = properties;
486 line6->line6pcm = line6pcm;
487
488 /* PCM device: */
489 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
490 if (err < 0)
491 return err;
492
493 snd_card_set_dev(line6->card, line6->ifcdev);
494
495 err = snd_line6_new_pcm(line6pcm);
496 if (err < 0)
497 return err;
498
499 spin_lock_init(&line6pcm->lock_audio_out);
500 spin_lock_init(&line6pcm->lock_audio_in);
501 spin_lock_init(&line6pcm->lock_trigger);
502
503 err = line6_create_audio_out_urbs(line6pcm);
504 if (err < 0)
505 return err;
506
507 err = line6_create_audio_in_urbs(line6pcm);
508 if (err < 0)
509 return err;
510
511 /* mixer: */
512 err =
513 snd_ctl_add(line6->card,
514 snd_ctl_new1(&line6_control_playback, line6pcm));
515 if (err < 0)
516 return err;
517
518 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
519 /* impulse response test: */
520 err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
521 if (err < 0)
522 return err;
523
524 err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
525 if (err < 0)
526 return err;
527
528 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
529 #endif
530
531 return 0;
532 }
533
534 /* prepare pcm callback */
snd_line6_prepare(struct snd_pcm_substream * substream)535 int snd_line6_prepare(struct snd_pcm_substream *substream)
536 {
537 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
538
539 switch (substream->stream) {
540 case SNDRV_PCM_STREAM_PLAYBACK:
541 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
542 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
543
544 break;
545
546 case SNDRV_PCM_STREAM_CAPTURE:
547 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
548 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
549
550 break;
551
552 default:
553 MISSING_CASE;
554 }
555
556 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
557 line6pcm->count_out = 0;
558 line6pcm->pos_out = 0;
559 line6pcm->pos_out_done = 0;
560 line6pcm->bytes_out = 0;
561 line6pcm->count_in = 0;
562 line6pcm->pos_in_done = 0;
563 line6pcm->bytes_in = 0;
564 }
565
566 return 0;
567 }
568