sunrpc: Include missing smp_lock.h
[linux-2.6-block.git] / drivers / staging / line6 / playback.c
1 /*
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"
21 #include "playback.h"
22
23 /*
24         Software stereo volume control.
25 */
26 static void change_volume(struct urb *urb_out, int volume[],
27                           int bytes_per_frame)
28 {
29         int chn = 0;
30
31         if (volume[0] == 256 && volume[1] == 256)
32                 return;         /* maximum volume - no change */
33
34         if (bytes_per_frame == 4) {
35                 short *p, *buf_end;
36                 p = (short *)urb_out->transfer_buffer;
37                 buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);
38
39                 for (; p < buf_end; ++p) {
40                         *p = (*p * volume[chn & 1]) >> 8;
41                         ++chn;
42                 }
43         } else if (bytes_per_frame == 6) {
44                 unsigned char *p, *buf_end;
45                 p = (unsigned char *)urb_out->transfer_buffer;
46                 buf_end = p + urb_out->transfer_buffer_length;
47
48                 for (; p < buf_end; p += 3) {
49                         int val;
50                         val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
51                         val = (val * volume[chn & 1]) >> 8;
52                         p[0] = val;
53                         p[1] = val >> 8;
54                         p[2] = val >> 16;
55                         ++chn;
56                 }
57         }
58 }
59
60 /*
61         Find a free URB, prepare audio data, and submit URB.
62 */
63 static int submit_audio_out_urb(struct snd_pcm_substream *substream)
64 {
65         int index;
66         unsigned long flags;
67         int i, urb_size, urb_frames;
68         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
69         const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
70         const int frame_increment =
71             line6pcm->properties->snd_line6_rates.rats[0].num_min;
72         const int frame_factor =
73             line6pcm->properties->snd_line6_rates.rats[0].den *
74             (USB_INTERVALS_PER_SECOND / LINE6_ISO_INTERVAL);
75         struct snd_pcm_runtime *runtime = substream->runtime;
76         struct urb *urb_out;
77
78         spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
79         index =
80             find_first_zero_bit(&line6pcm->active_urb_out, LINE6_ISO_BUFFERS);
81
82         if (index < 0 || index >= LINE6_ISO_BUFFERS) {
83                 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
84                 dev_err(s2m(substream), "no free URB found\n");
85                 return -EINVAL;
86         }
87
88         urb_out = line6pcm->urb_audio_out[index];
89         urb_size = 0;
90
91         for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
92                 /* compute frame size for given sampling rate */
93                 int n, fs;
94                 struct usb_iso_packet_descriptor *fout =
95                     &urb_out->iso_frame_desc[i];
96                 line6pcm->count_out += frame_increment;
97                 n = line6pcm->count_out / frame_factor;
98                 line6pcm->count_out -= n * frame_factor;
99                 fs = n * bytes_per_frame;
100                 fout->offset = urb_size;
101                 fout->length = fs;
102                 urb_size += fs;
103         }
104
105         urb_frames = urb_size / bytes_per_frame;
106
107         if (test_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags)) {
108                 urb_out->transfer_buffer = line6pcm->wrap_out;
109                 memset(line6pcm->wrap_out, 0, urb_size);
110         } else {
111                 if (line6pcm->pos_out + urb_frames > runtime->buffer_size) {
112                         /*
113                            The transferred area goes over buffer boundary,
114                            copy the data to the temp buffer.
115                          */
116                         int len;
117                         len = runtime->buffer_size - line6pcm->pos_out;
118                         urb_out->transfer_buffer = line6pcm->wrap_out;
119
120                         if (len > 0) {
121                                 memcpy(line6pcm->wrap_out,
122                                        runtime->dma_area +
123                                        line6pcm->pos_out * bytes_per_frame,
124                                        len * bytes_per_frame);
125                                 memcpy(line6pcm->wrap_out +
126                                        len * bytes_per_frame, runtime->dma_area,
127                                        (urb_frames - len) * bytes_per_frame);
128                         } else {
129                                 /* this is somewhat paranoid */
130                                 dev_err(s2m(substream),
131                                         "driver bug: len = %d\n", len);
132                         }
133                 } else {
134                         /* set the buffer pointer */
135                         urb_out->transfer_buffer =
136                             runtime->dma_area +
137                             line6pcm->pos_out * bytes_per_frame;
138                 }
139         }
140
141         line6pcm->pos_out += urb_frames;
142         if (line6pcm->pos_out >= runtime->buffer_size)
143                 line6pcm->pos_out -= runtime->buffer_size;
144
145         urb_out->transfer_buffer_length = urb_size;
146         urb_out->context = substream;
147         change_volume(urb_out, line6pcm->volume, bytes_per_frame);
148
149 #if DO_DUMP_PCM_SEND
150         for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
151                 struct usb_iso_packet_descriptor *fout =
152                     &urb_out->iso_frame_desc[i];
153                 line6_write_hexdump(line6pcm->line6, 'P',
154                                     urb_out->transfer_buffer + fout->offset,
155                                     fout->length);
156         }
157 #endif
158
159         if (usb_submit_urb(urb_out, GFP_ATOMIC) == 0)
160                 set_bit(index, &line6pcm->active_urb_out);
161         else
162                 dev_err(s2m(substream), "URB out #%d submission failed\n",
163                         index);
164
165         spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
166         return 0;
167 }
168
169 /*
170         Submit all currently available playback URBs.
171 */
172 static int submit_audio_out_all_urbs(struct snd_pcm_substream *substream)
173 {
174         int ret, i;
175
176         for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
177                 ret = submit_audio_out_urb(substream);
178                 if (ret < 0)
179                         return ret;
180         }
181
182         return 0;
183 }
184
185 /*
186         Unlink all currently active playback URBs.
187 */
188 static void unlink_audio_out_urbs(struct snd_line6_pcm *line6pcm)
189 {
190         unsigned int i;
191
192         for (i = LINE6_ISO_BUFFERS; i--;) {
193                 if (test_bit(i, &line6pcm->active_urb_out)) {
194                         if (!test_and_set_bit(i, &line6pcm->unlink_urb_out)) {
195                                 struct urb *u = line6pcm->urb_audio_out[i];
196                                 usb_unlink_urb(u);
197                         }
198                 }
199         }
200 }
201
202 /*
203    Wait until unlinking of all currently active playback URBs has been finished.
204 */
205 static void wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
206 {
207         int timeout = HZ;
208         unsigned int i;
209         int alive;
210
211         do {
212                 alive = 0;
213                 for (i = LINE6_ISO_BUFFERS; i--;) {
214                         if (test_bit(i, &line6pcm->active_urb_out))
215                                 alive++;
216                 }
217                 if (!alive)
218                         break;
219                 set_current_state(TASK_UNINTERRUPTIBLE);
220                 schedule_timeout(1);
221         } while (--timeout > 0);
222         if (alive)
223                 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
224
225         line6pcm->active_urb_out = 0;
226         line6pcm->unlink_urb_out = 0;
227 }
228
229 /*
230         Unlink all currently active playback URBs, and wait for finishing.
231 */
232 void unlink_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
233 {
234         unlink_audio_out_urbs(line6pcm);
235         wait_clear_audio_out_urbs(line6pcm);
236 }
237
238 /*
239         Callback for completed playback URB.
240 */
241 static void audio_out_callback(struct urb *urb)
242 {
243         int i, index, length = 0, shutdown = 0;
244         unsigned long flags;
245
246         struct snd_pcm_substream *substream =
247             (struct snd_pcm_substream *)urb->context;
248         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
249         struct snd_pcm_runtime *runtime = substream->runtime;
250
251         /* find index of URB */
252         for (index = LINE6_ISO_BUFFERS; index--;)
253                 if (urb == line6pcm->urb_audio_out[index])
254                         break;
255
256         if (index < 0)
257                 return;         /* URB has been unlinked asynchronously */
258
259         for (i = LINE6_ISO_PACKETS; i--;)
260                 length += urb->iso_frame_desc[i].length;
261
262         spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
263         line6pcm->pos_out_done +=
264             length / line6pcm->properties->bytes_per_frame;
265
266         if (line6pcm->pos_out_done >= runtime->buffer_size)
267                 line6pcm->pos_out_done -= runtime->buffer_size;
268
269         clear_bit(index, &line6pcm->active_urb_out);
270
271         for (i = LINE6_ISO_PACKETS; i--;)
272                 if (urb->iso_frame_desc[i].status == -ESHUTDOWN) {
273                         shutdown = 1;
274                         break;
275                 }
276
277         if (test_bit(index, &line6pcm->unlink_urb_out))
278                 shutdown = 1;
279
280         spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
281
282         if (!shutdown) {
283                 submit_audio_out_urb(substream);
284
285                 line6pcm->bytes_out += length;
286                 if (line6pcm->bytes_out >= line6pcm->period_out) {
287                         line6pcm->bytes_out -= line6pcm->period_out;
288                         snd_pcm_period_elapsed(substream);
289                 }
290         }
291 }
292
293 /* open playback callback */
294 static int snd_line6_playback_open(struct snd_pcm_substream *substream)
295 {
296         int err;
297         struct snd_pcm_runtime *runtime = substream->runtime;
298         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
299
300         err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
301                                             (&line6pcm->properties->
302                                              snd_line6_rates));
303         if (err < 0)
304                 return err;
305
306         runtime->hw = line6pcm->properties->snd_line6_playback_hw;
307         return 0;
308 }
309
310 /* close playback callback */
311 static int snd_line6_playback_close(struct snd_pcm_substream *substream)
312 {
313         return 0;
314 }
315
316 /* hw_params playback callback */
317 static int snd_line6_playback_hw_params(struct snd_pcm_substream *substream,
318                                         struct snd_pcm_hw_params *hw_params)
319 {
320         int ret;
321         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
322
323         /* -- Florian Demski [FD] */
324         /* don't ask me why, but this fixes the bug on my machine */
325         if (line6pcm == NULL) {
326                 if (substream->pcm == NULL)
327                         return -ENOMEM;
328                 if (substream->pcm->private_data == NULL)
329                         return -ENOMEM;
330                 substream->private_data = substream->pcm->private_data;
331                 line6pcm = snd_pcm_substream_chip(substream);
332         }
333         /* -- [FD] end */
334
335         ret = snd_pcm_lib_malloc_pages(substream,
336                                        params_buffer_bytes(hw_params));
337         if (ret < 0)
338                 return ret;
339
340         line6pcm->period_out = params_period_bytes(hw_params);
341         line6pcm->wrap_out = kmalloc(2 * LINE6_ISO_PACKET_SIZE_MAX, GFP_KERNEL);
342
343         if (!line6pcm->wrap_out) {
344                 dev_err(s2m(substream), "cannot malloc wrap_out\n");
345                 return -ENOMEM;
346         }
347
348         return 0;
349 }
350
351 /* hw_free playback callback */
352 static int snd_line6_playback_hw_free(struct snd_pcm_substream *substream)
353 {
354         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
355         unlink_wait_clear_audio_out_urbs(line6pcm);
356
357         kfree(line6pcm->wrap_out);
358         line6pcm->wrap_out = NULL;
359
360         return snd_pcm_lib_free_pages(substream);
361 }
362
363 /* trigger playback callback */
364 int snd_line6_playback_trigger(struct snd_pcm_substream *substream, int cmd)
365 {
366         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
367         int err;
368         line6pcm->count_out = 0;
369
370         switch (cmd) {
371         case SNDRV_PCM_TRIGGER_START:
372                 if (!test_and_set_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags)) {
373                         err = submit_audio_out_all_urbs(substream);
374
375                         if (err < 0) {
376                                 clear_bit(BIT_RUNNING_PLAYBACK,
377                                           &line6pcm->flags);
378                                 return err;
379                         }
380                 }
381
382                 break;
383
384         case SNDRV_PCM_TRIGGER_STOP:
385                 if (test_and_clear_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags))
386                         unlink_audio_out_urbs(line6pcm);
387
388                 break;
389
390         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
391                 set_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags);
392                 break;
393
394         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
395                 clear_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags);
396                 break;
397
398         default:
399                 return -EINVAL;
400         }
401
402         return 0;
403 }
404
405 /* playback pointer callback */
406 static snd_pcm_uframes_t
407 snd_line6_playback_pointer(struct snd_pcm_substream *substream)
408 {
409         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
410         return line6pcm->pos_out_done;
411 }
412
413 /* playback operators */
414 struct snd_pcm_ops snd_line6_playback_ops = {
415         .open = snd_line6_playback_open,
416         .close = snd_line6_playback_close,
417         .ioctl = snd_pcm_lib_ioctl,
418         .hw_params = snd_line6_playback_hw_params,
419         .hw_free = snd_line6_playback_hw_free,
420         .prepare = snd_line6_prepare,
421         .trigger = snd_line6_trigger,
422         .pointer = snd_line6_playback_pointer,
423 };
424
425 int create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
426 {
427         int i;
428
429         /* create audio URBs and fill in constant values: */
430         for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
431                 struct urb *urb;
432
433                 /* URB for audio out: */
434                 urb = line6pcm->urb_audio_out[i] =
435                     usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
436
437                 if (urb == NULL) {
438                         dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
439                         return -ENOMEM;
440                 }
441
442                 urb->dev = line6pcm->line6->usbdev;
443                 urb->pipe =
444                     usb_sndisocpipe(line6pcm->line6->usbdev,
445                                     line6pcm->
446                                     ep_audio_write & USB_ENDPOINT_NUMBER_MASK);
447                 urb->transfer_flags = URB_ISO_ASAP;
448                 urb->start_frame = -1;
449                 urb->number_of_packets = LINE6_ISO_PACKETS;
450                 urb->interval = LINE6_ISO_INTERVAL;
451                 urb->error_count = 0;
452                 urb->complete = audio_out_callback;
453         }
454
455         return 0;
456 }