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