blob: acb06126a6a5bb9a27f1548f30cb12d917e3a9b4 [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
14#include <sound/core.h>
15#include <sound/pcm.h>
16#include <sound/pcm_params.h>
17
18#include "audio.h"
19#include "pcm.h"
20#include "pod.h"
Greg Kroah-Hartmanb702ed252009-02-27 20:45:03 -080021#include "playback.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080022
23
24/*
25 Software stereo volume control.
26*/
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -080027static void change_volume(struct urb *urb_out, int volume[],
28 int bytes_per_frame)
Markus Grabner705ecec2009-02-27 19:43:04 -080029{
30 int chn = 0;
31
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -080032 if (volume[0] == 256 && volume[1] == 256)
Markus Grabner705ecec2009-02-27 19:43:04 -080033 return; /* maximum volume - no change */
34
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -080035 if (bytes_per_frame == 4) {
Markus Grabner705ecec2009-02-27 19:43:04 -080036 short *p, *buf_end;
37 p = (short *)urb_out->transfer_buffer;
38 buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);
39
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -080040 for (; p < buf_end; ++p) {
Markus Grabner705ecec2009-02-27 19:43:04 -080041 *p = (*p * volume[chn & 1]) >> 8;
42 ++chn;
43 }
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -080044 } else if (bytes_per_frame == 6) {
Markus Grabner705ecec2009-02-27 19:43:04 -080045 unsigned char *p, *buf_end;
46 p = (unsigned char *)urb_out->transfer_buffer;
47 buf_end = p + urb_out->transfer_buffer_length;
48
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -080049 for (; p < buf_end; p += 3) {
50 int val;
51 val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
Markus Grabner705ecec2009-02-27 19:43:04 -080052 val = (val * volume[chn & 1]) >> 8;
53 p[0] = val;
54 p[1] = val >> 8;
55 p[2] = val >> 16;
56 ++chn;
57 }
58 }
59}
60
61/*
62 Find a free URB, prepare audio data, and submit URB.
63*/
64static int submit_audio_out_urb(struct snd_pcm_substream *substream)
65{
66 int index;
67 unsigned long flags;
68 int i, urb_size, urb_frames;
69 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
70 const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
71 const int frame_increment = line6pcm->properties->snd_line6_rates.rats[0].num_min;
72 const int frame_factor = line6pcm->properties->snd_line6_rates.rats[0].den * (USB_INTERVALS_PER_SECOND / LINE6_ISO_INTERVAL);
73 struct snd_pcm_runtime *runtime = substream->runtime;
74 struct urb *urb_out;
75
76 spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
77 index = find_first_zero_bit(&line6pcm->active_urb_out, LINE6_ISO_BUFFERS);
78
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -080079 if (index < 0 || index >= LINE6_ISO_BUFFERS) {
Markus Grabner705ecec2009-02-27 19:43:04 -080080 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
81 dev_err(s2m(substream), "no free URB found\n");
82 return -EINVAL;
83 }
84
85 urb_out = line6pcm->urb_audio_out[index];
86 urb_size = 0;
87
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -080088 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
Markus Grabner705ecec2009-02-27 19:43:04 -080089 /* compute frame size for given sampling rate */
90 int n, fs;
91 struct usb_iso_packet_descriptor *fout = &urb_out->iso_frame_desc[i];
92 line6pcm->count_out += frame_increment;
93 n = line6pcm->count_out / frame_factor;
94 line6pcm->count_out -= n * frame_factor;
95 fs = n * bytes_per_frame;
96 fout->offset = urb_size;
97 fout->length = fs;
98 urb_size += fs;
99 }
100
101 urb_frames = urb_size / bytes_per_frame;
102
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800103 if (test_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags)) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800104 urb_out->transfer_buffer = line6pcm->wrap_out;
105 memset(line6pcm->wrap_out, 0, urb_size);
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800106 } else {
107 if (line6pcm->pos_out + urb_frames > runtime->buffer_size) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800108 /*
109 The transferred area goes over buffer boundary,
110 copy the data to the temp buffer.
111 */
112 int len;
113 len = runtime->buffer_size - line6pcm->pos_out;
114 urb_out->transfer_buffer = line6pcm->wrap_out;
115
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800116 if (len > 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800117 memcpy(line6pcm->wrap_out, runtime->dma_area + line6pcm->pos_out * bytes_per_frame, len * bytes_per_frame);
118 memcpy(line6pcm->wrap_out + len * bytes_per_frame, runtime->dma_area, (urb_frames - len) * bytes_per_frame);
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800119 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800120 dev_err(s2m(substream), "driver bug: len = %d\n", len); /* this is somewhat paranoid */
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800121 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800122 /* set the buffer pointer */
123 urb_out->transfer_buffer = runtime->dma_area + line6pcm->pos_out * bytes_per_frame;
124 }
125 }
126
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800127 if ((line6pcm->pos_out += urb_frames) >= runtime->buffer_size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800128 line6pcm->pos_out -= runtime->buffer_size;
129
130 urb_out->transfer_buffer_length = urb_size;
131 urb_out->context = substream;
132 change_volume(urb_out, line6pcm->volume, bytes_per_frame);
133
134#if DO_DUMP_PCM_SEND
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800135 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800136 struct usb_iso_packet_descriptor *fout = &urb_out->iso_frame_desc[i];
137 line6_write_hexdump(line6pcm->line6, 'P', urb_out->transfer_buffer + fout->offset, fout->length);
138 }
139#endif
140
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800141 if (usb_submit_urb(urb_out, GFP_ATOMIC) == 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800142 set_bit(index, &line6pcm->active_urb_out);
143 else
144 dev_err(s2m(substream), "URB out #%d submission failed\n", index);
145
146 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
147 return 0;
148}
149
150/*
151 Submit all currently available playback URBs.
152*/
153static int submit_audio_out_all_urbs(struct snd_pcm_substream *substream)
154{
155 int ret, i;
156
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800157 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
158 ret = submit_audio_out_urb(substream);
159 if (ret < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800160 return ret;
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800161 }
Markus Grabner705ecec2009-02-27 19:43:04 -0800162
163 return 0;
164}
165
166/*
167 Unlink all currently active playback URBs.
168*/
169static void unlink_audio_out_urbs(struct snd_line6_pcm *line6pcm)
170{
171 unsigned int i;
172
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800173 for (i = LINE6_ISO_BUFFERS; i--;) {
174 if (test_bit(i, &line6pcm->active_urb_out)) {
175 if (!test_and_set_bit(i, &line6pcm->unlink_urb_out)) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800176 struct urb *u = line6pcm->urb_audio_out[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800177 usb_unlink_urb(u);
178 }
179 }
180 }
181}
182
183/*
184 Wait until unlinking of all currently active playback URBs has been finished.
185*/
186static void wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
187{
188 int timeout = HZ;
189 unsigned int i;
190 int alive;
191
192 do {
193 alive = 0;
194 for (i = LINE6_ISO_BUFFERS; i--;) {
195 if (test_bit(i, &line6pcm->active_urb_out))
196 alive++;
197 }
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800198 if (!alive)
Markus Grabner705ecec2009-02-27 19:43:04 -0800199 break;
200 set_current_state(TASK_UNINTERRUPTIBLE);
201 schedule_timeout(1);
202 } while (--timeout > 0);
203 if (alive)
204 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
205
206 line6pcm->active_urb_out = 0;
207 line6pcm->unlink_urb_out = 0;
208}
209
210/*
211 Unlink all currently active playback URBs, and wait for finishing.
212*/
213void unlink_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
214{
215 unlink_audio_out_urbs(line6pcm);
216 wait_clear_audio_out_urbs(line6pcm);
217}
218
219/*
220 Callback for completed playback URB.
221*/
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -0800222static void audio_out_callback(struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800223{
224 int i, index, length = 0, shutdown = 0;
225 unsigned long flags;
226
227 struct snd_pcm_substream *substream = (struct snd_pcm_substream *)urb->context;
228 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
229 struct snd_pcm_runtime *runtime = substream->runtime;
230
231 /* find index of URB */
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800232 for (index = LINE6_ISO_BUFFERS; index--;)
233 if (urb == line6pcm->urb_audio_out[index])
Markus Grabner705ecec2009-02-27 19:43:04 -0800234 break;
235
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800236 if (index < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800237 return; /* URB has been unlinked asynchronously */
238
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800239 for (i = LINE6_ISO_PACKETS; i--;)
Markus Grabner705ecec2009-02-27 19:43:04 -0800240 length += urb->iso_frame_desc[i].length;
241
242 spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
243 line6pcm->pos_out_done += length / line6pcm->properties->bytes_per_frame;
244
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800245 if (line6pcm->pos_out_done >= runtime->buffer_size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800246 line6pcm->pos_out_done -= runtime->buffer_size;
247
248 clear_bit(index, &line6pcm->active_urb_out);
249
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800250 for (i = LINE6_ISO_PACKETS; i--;)
251 if (urb->iso_frame_desc[i].status == -ESHUTDOWN) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800252 shutdown = 1;
253 break;
254 }
255
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800256 if (test_bit(index, &line6pcm->unlink_urb_out))
Markus Grabner705ecec2009-02-27 19:43:04 -0800257 shutdown = 1;
258
259 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
260
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800261 if (!shutdown) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800262 submit_audio_out_urb(substream);
263
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800264 if ((line6pcm->bytes_out += length) >= line6pcm->period_out) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800265 line6pcm->bytes_out -= line6pcm->period_out;
266 snd_pcm_period_elapsed(substream);
267 }
268 }
269}
270
271/* open playback callback */
272static int snd_line6_playback_open(struct snd_pcm_substream *substream)
273{
274 int err;
275 struct snd_pcm_runtime *runtime = substream->runtime;
276 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
277
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800278 err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
279 (&line6pcm->properties->snd_line6_rates));
280 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800281 return err;
282
283 runtime->hw = line6pcm->properties->snd_line6_playback_hw;
284 return 0;
285}
286
287/* close playback callback */
288static int snd_line6_playback_close(struct snd_pcm_substream *substream)
289{
290 return 0;
291}
292
293/* hw_params playback callback */
294static int snd_line6_playback_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params)
295{
296 int ret;
297 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
298
299 /* -- Florian Demski [FD] */
300 /* don't ask me why, but this fixes the bug on my machine */
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800301 if (line6pcm == NULL) {
302 if (substream->pcm == NULL)
Markus Grabner705ecec2009-02-27 19:43:04 -0800303 return -ENOMEM;
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800304 if (substream->pcm->private_data == NULL)
Markus Grabner705ecec2009-02-27 19:43:04 -0800305 return -ENOMEM;
306 substream->private_data = substream->pcm->private_data;
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800307 line6pcm = snd_pcm_substream_chip(substream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800308 }
309 /* -- [FD] end */
310
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800311 ret = snd_pcm_lib_malloc_pages(substream,
312 params_buffer_bytes(hw_params));
313 if (ret < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800314 return ret;
315
316 line6pcm->period_out = params_period_bytes(hw_params);
317 line6pcm->wrap_out = kmalloc(2 * LINE6_ISO_PACKET_SIZE_MAX, GFP_KERNEL);
318
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800319 if (!line6pcm->wrap_out) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800320 dev_err(s2m(substream), "cannot malloc wrap_out\n");
321 return -ENOMEM;
322 }
323
324 return 0;
325}
326
327/* hw_free playback callback */
328static int snd_line6_playback_hw_free(struct snd_pcm_substream *substream)
329{
330 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
331 unlink_wait_clear_audio_out_urbs(line6pcm);
332
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800333 kfree(line6pcm->wrap_out);
334 line6pcm->wrap_out = NULL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800335
336 return snd_pcm_lib_free_pages(substream);
337}
338
339/* trigger playback callback */
340int snd_line6_playback_trigger(struct snd_pcm_substream *substream, int cmd)
341{
342 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
343 int err;
344 line6pcm->count_out = 0;
345
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800346 switch (cmd) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800347 case SNDRV_PCM_TRIGGER_START:
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800348 if (!test_and_set_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags)) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800349 err = submit_audio_out_all_urbs(substream);
350
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800351 if (err < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800352 clear_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags);
353 return err;
354 }
355 }
356
357 break;
358
359 case SNDRV_PCM_TRIGGER_STOP:
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800360 if (test_and_clear_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags))
Markus Grabner705ecec2009-02-27 19:43:04 -0800361 unlink_audio_out_urbs(line6pcm);
362
363 break;
364
365 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
366 set_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags);
367 break;
368
369 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
370 clear_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags);
371 break;
372
373 default:
374 return -EINVAL;
375 }
376
377 return 0;
378}
379
380/* playback pointer callback */
381static snd_pcm_uframes_t
382snd_line6_playback_pointer(struct snd_pcm_substream *substream)
383{
384 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
385 return line6pcm->pos_out_done;
386}
387
388/* playback operators */
389struct snd_pcm_ops snd_line6_playback_ops = {
390 .open = snd_line6_playback_open,
391 .close = snd_line6_playback_close,
392 .ioctl = snd_pcm_lib_ioctl,
393 .hw_params = snd_line6_playback_hw_params,
394 .hw_free = snd_line6_playback_hw_free,
395 .prepare = snd_line6_prepare,
396 .trigger = snd_line6_trigger,
397 .pointer = snd_line6_playback_pointer,
398};
399
400int create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
401{
402 int i;
403
404 /* create audio URBs and fill in constant values: */
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800405 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800406 struct urb *urb;
407
408 /* URB for audio out: */
409 urb = line6pcm->urb_audio_out[i] = usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
410
Greg Kroah-Hartman010f3782009-02-27 22:41:12 -0800411 if (urb == NULL) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800412 dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
413 return -ENOMEM;
414 }
415
416 urb->dev = line6pcm->line6->usbdev;
417 urb->pipe = usb_sndisocpipe(line6pcm->line6->usbdev, line6pcm->ep_audio_write & USB_ENDPOINT_NUMBER_MASK);
418 urb->transfer_flags = URB_ISO_ASAP;
419 urb->start_frame = -1;
420 urb->number_of_packets = LINE6_ISO_PACKETS;
421 urb->interval = LINE6_ISO_INTERVAL;
422 urb->error_count = 0;
423 urb->complete = audio_out_callback;
424 }
425
426 return 0;
427}