]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/line6/playback.c
Staging: line6: fix up NULL assignment mistakes
[karo-tx-linux.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 /*
25         Software stereo volume control.
26 */
27 static void change_volume(struct urb *urb_out, int volume[], 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         }
44         else if(bytes_per_frame == 6) {
45                 unsigned char *p, *buf_end;
46                 p = (unsigned char *)urb_out->transfer_buffer;
47                 buf_end = p + urb_out->transfer_buffer_length;
48
49                 for(; p < buf_end; p += 3) {
50                         int 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 = line6pcm->properties->snd_line6_rates.rats[0].num_min;
71         const int frame_factor = line6pcm->properties->snd_line6_rates.rats[0].den * (USB_INTERVALS_PER_SECOND / LINE6_ISO_INTERVAL);
72         struct snd_pcm_runtime *runtime = substream->runtime;
73         struct urb *urb_out;
74
75         spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
76         index = find_first_zero_bit(&line6pcm->active_urb_out, LINE6_ISO_BUFFERS);
77
78         if(index < 0 || index >= LINE6_ISO_BUFFERS) {
79                 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
80                 dev_err(s2m(substream), "no free URB found\n");
81                 return -EINVAL;
82         }
83
84         urb_out = line6pcm->urb_audio_out[index];
85         urb_size = 0;
86
87         for(i = 0; i < LINE6_ISO_PACKETS; ++i) {
88                 /* compute frame size for given sampling rate */
89                 int n, fs;
90                 struct usb_iso_packet_descriptor *fout = &urb_out->iso_frame_desc[i];
91                 line6pcm->count_out += frame_increment;
92                 n = line6pcm->count_out / frame_factor;
93                 line6pcm->count_out -= n * frame_factor;
94                 fs = n * bytes_per_frame;
95                 fout->offset = urb_size;
96                 fout->length = fs;
97                 urb_size += fs;
98         }
99
100         urb_frames = urb_size / bytes_per_frame;
101
102         if(test_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags)) {
103                 urb_out->transfer_buffer = line6pcm->wrap_out;
104                 memset(line6pcm->wrap_out, 0, urb_size);
105         }
106         else {
107                 if(line6pcm->pos_out + urb_frames > runtime->buffer_size) {
108                         /*
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
116                         if(len > 0) {
117                                 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);
119                         }
120                         else
121                                 dev_err(s2m(substream), "driver bug: len = %d\n", len);  /* this is somewhat paranoid */
122                 }
123                 else {
124                         /* set the buffer pointer */
125                         urb_out->transfer_buffer = runtime->dma_area + line6pcm->pos_out * bytes_per_frame;
126                 }
127         }
128
129         if((line6pcm->pos_out += urb_frames) >= runtime->buffer_size)
130                 line6pcm->pos_out -= runtime->buffer_size;
131
132         urb_out->transfer_buffer_length = urb_size;
133         urb_out->context = substream;
134         change_volume(urb_out, line6pcm->volume, bytes_per_frame);
135
136 #if DO_DUMP_PCM_SEND
137         for(i = 0; i < LINE6_ISO_PACKETS; ++i) {
138                 struct usb_iso_packet_descriptor *fout = &urb_out->iso_frame_desc[i];
139                 line6_write_hexdump(line6pcm->line6, 'P', urb_out->transfer_buffer + fout->offset, fout->length);
140         }
141 #endif
142
143         if(usb_submit_urb(urb_out, GFP_ATOMIC) == 0)
144                 set_bit(index, &line6pcm->active_urb_out);
145         else
146                 dev_err(s2m(substream), "URB out #%d submission failed\n", index);
147
148         spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
149         return 0;
150 }
151
152 /*
153         Submit all currently available playback URBs.
154 */
155 static int submit_audio_out_all_urbs(struct snd_pcm_substream *substream)
156 {
157         int ret, i;
158
159         for(i = 0; i < LINE6_ISO_BUFFERS; ++i)
160                 if((ret = submit_audio_out_urb(substream)) < 0)
161                         return ret;
162
163         return 0;
164 }
165
166 /*
167         Unlink all currently active playback URBs.
168 */
169 static void unlink_audio_out_urbs(struct snd_line6_pcm *line6pcm)
170 {
171         unsigned int i;
172
173         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)) {
176                                 struct urb *u = line6pcm->urb_audio_out[i];
177                                 usb_unlink_urb(u);
178                         }
179                 }
180         }
181 }
182
183 /*
184         Wait until unlinking of all currently active playback URBs has been finished.
185 */
186 static 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                 }
198                 if (! alive)
199                         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 */
213 void 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 */
222 static void audio_out_callback(struct urb *urb)
223 {
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 */
232         for(index = LINE6_ISO_BUFFERS; index--;)
233                 if(urb == line6pcm->urb_audio_out[index])
234                         break;
235
236         if(index < 0)
237                 return;  /* URB has been unlinked asynchronously */
238
239         for(i = LINE6_ISO_PACKETS; i--;)
240                 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
245         if(line6pcm->pos_out_done >= runtime->buffer_size)
246                 line6pcm->pos_out_done -= runtime->buffer_size;
247
248         clear_bit(index, &line6pcm->active_urb_out);
249
250         for(i = LINE6_ISO_PACKETS; i--;)
251                 if(urb->iso_frame_desc[i].status == -ESHUTDOWN) {
252                         shutdown = 1;
253                         break;
254                 }
255
256         if(test_bit(index, &line6pcm->unlink_urb_out))
257                 shutdown = 1;
258
259         spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
260
261         if(!shutdown) {
262                 submit_audio_out_urb(substream);
263
264                 if((line6pcm->bytes_out += length) >= line6pcm->period_out) {
265                         line6pcm->bytes_out -= line6pcm->period_out;
266                         snd_pcm_period_elapsed(substream);
267                 }
268         }
269 }
270
271 /* open playback callback */
272 static 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
278         if((err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
279                                                                                                                                                                         (&line6pcm->properties->snd_line6_rates))) < 0)
280                 return err;
281
282         runtime->hw = line6pcm->properties->snd_line6_playback_hw;
283         return 0;
284 }
285
286 /* close playback callback */
287 static int snd_line6_playback_close(struct snd_pcm_substream *substream)
288 {
289         return 0;
290 }
291
292 /* hw_params playback callback */
293 static int snd_line6_playback_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params)
294 {
295         int ret;
296         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
297
298         /* -- Florian Demski [FD] */
299         /* don't ask me why, but this fixes the bug on my machine */
300         if ( line6pcm == NULL ) {
301                 if ( substream->pcm == NULL )
302                         return -ENOMEM;
303                 if ( substream->pcm->private_data == NULL )
304                         return -ENOMEM;
305                 substream->private_data = substream->pcm->private_data;
306                 line6pcm = snd_pcm_substream_chip( substream );
307         }
308         /* -- [FD] end */
309
310         if((ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
311                 return ret;
312
313         line6pcm->period_out = params_period_bytes(hw_params);
314         line6pcm->wrap_out = kmalloc(2 * LINE6_ISO_PACKET_SIZE_MAX, GFP_KERNEL);
315
316         if(!line6pcm->wrap_out) {
317                 dev_err(s2m(substream), "cannot malloc wrap_out\n");
318                 return -ENOMEM;
319         }
320
321         return 0;
322 }
323
324 /* hw_free playback callback */
325 static int snd_line6_playback_hw_free(struct snd_pcm_substream *substream)
326 {
327         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
328         unlink_wait_clear_audio_out_urbs(line6pcm);
329
330         if(line6pcm->wrap_out) {
331                 kfree(line6pcm->wrap_out);
332                 line6pcm->wrap_out = NULL;
333         }
334
335         return snd_pcm_lib_free_pages(substream);
336 }
337
338 /* trigger playback callback */
339 int snd_line6_playback_trigger(struct snd_pcm_substream *substream, int cmd)
340 {
341         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
342         int err;
343         line6pcm->count_out = 0;
344
345         switch(cmd) {
346         case SNDRV_PCM_TRIGGER_START:
347                 if(!test_and_set_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags)) {
348                         err = submit_audio_out_all_urbs(substream);
349
350                         if(err < 0) {
351                                 clear_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags);
352                                 return err;
353                         }
354                 }
355
356                 break;
357
358         case SNDRV_PCM_TRIGGER_STOP:
359                 if(test_and_clear_bit(BIT_RUNNING_PLAYBACK, &line6pcm->flags))
360                         unlink_audio_out_urbs(line6pcm);
361
362                 break;
363
364         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
365                 set_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags);
366                 break;
367
368         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
369                 clear_bit(BIT_PAUSE_PLAYBACK, &line6pcm->flags);
370                 break;
371
372         default:
373                 return -EINVAL;
374         }
375
376         return 0;
377 }
378
379 /* playback pointer callback */
380 static snd_pcm_uframes_t
381 snd_line6_playback_pointer(struct snd_pcm_substream *substream)
382 {
383         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
384         return line6pcm->pos_out_done;
385 }
386
387 /* playback operators */
388 struct snd_pcm_ops snd_line6_playback_ops = {
389         .open =        snd_line6_playback_open,
390         .close =       snd_line6_playback_close,
391         .ioctl =       snd_pcm_lib_ioctl,
392         .hw_params =   snd_line6_playback_hw_params,
393         .hw_free =     snd_line6_playback_hw_free,
394         .prepare =     snd_line6_prepare,
395         .trigger =     snd_line6_trigger,
396         .pointer =     snd_line6_playback_pointer,
397 };
398
399 int create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
400 {
401         int i;
402
403         /* create audio URBs and fill in constant values: */
404         for(i = 0; i < LINE6_ISO_BUFFERS; ++i) {
405                 struct urb *urb;
406
407                 /* URB for audio out: */
408                 urb = line6pcm->urb_audio_out[i] = usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
409
410                 if(urb == NULL) {
411                         dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
412                         return -ENOMEM;
413                 }
414
415                 urb->dev = line6pcm->line6->usbdev;
416                 urb->pipe = usb_sndisocpipe(line6pcm->line6->usbdev, line6pcm->ep_audio_write & USB_ENDPOINT_NUMBER_MASK);
417                 urb->transfer_flags = URB_ISO_ASAP;
418                 urb->start_frame = -1;
419                 urb->number_of_packets = LINE6_ISO_PACKETS;
420                 urb->interval = LINE6_ISO_INTERVAL;
421                 urb->error_count = 0;
422                 urb->complete = audio_out_callback;
423         }
424
425         return 0;
426 }