~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/usb/6fire/pcm.c

Version: ~ [ linux-5.10-rc5 ] ~ [ linux-5.9.10 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.79 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.159 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.208 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.245 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.245 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Linux driver for TerraTec DMX 6Fire USB
  3  *
  4  * PCM driver
  5  *
  6  * Author:      Torsten Schenk <torsten.schenk@zoho.com>
  7  * Created:     Jan 01, 2011
  8  * Copyright:   (C) Torsten Schenk
  9  *
 10  * This program is free software; you can redistribute it and/or modify
 11  * it under the terms of the GNU General Public License as published by
 12  * the Free Software Foundation; either version 2 of the License, or
 13  * (at your option) any later version.
 14  */
 15 
 16 #include "pcm.h"
 17 #include "chip.h"
 18 #include "comm.h"
 19 #include "control.h"
 20 
 21 enum {
 22         OUT_N_CHANNELS = 6, IN_N_CHANNELS = 4
 23 };
 24 
 25 /* keep next two synced with
 26  * FW_EP_W_MAX_PACKET_SIZE[] and RATES_MAX_PACKET_SIZE
 27  * and CONTROL_RATE_XXX in control.h */
 28 static const int rates_in_packet_size[] = { 228, 228, 420, 420, 404, 404 };
 29 static const int rates_out_packet_size[] = { 228, 228, 420, 420, 604, 604 };
 30 static const int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000 };
 31 static const int rates_alsaid[] = {
 32         SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_48000,
 33         SNDRV_PCM_RATE_88200, SNDRV_PCM_RATE_96000,
 34         SNDRV_PCM_RATE_176400, SNDRV_PCM_RATE_192000 };
 35 
 36 enum { /* settings for pcm */
 37         OUT_EP = 6, IN_EP = 2, MAX_BUFSIZE = 128 * 1024
 38 };
 39 
 40 enum { /* pcm streaming states */
 41         STREAM_DISABLED, /* no pcm streaming */
 42         STREAM_STARTING, /* pcm streaming requested, waiting to become ready */
 43         STREAM_RUNNING, /* pcm streaming running */
 44         STREAM_STOPPING
 45 };
 46 
 47 static const struct snd_pcm_hardware pcm_hw = {
 48         .info = SNDRV_PCM_INFO_MMAP |
 49                 SNDRV_PCM_INFO_INTERLEAVED |
 50                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 51                 SNDRV_PCM_INFO_MMAP_VALID |
 52                 SNDRV_PCM_INFO_BATCH,
 53 
 54         .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
 55 
 56         .rates = SNDRV_PCM_RATE_44100 |
 57                 SNDRV_PCM_RATE_48000 |
 58                 SNDRV_PCM_RATE_88200 |
 59                 SNDRV_PCM_RATE_96000 |
 60                 SNDRV_PCM_RATE_176400 |
 61                 SNDRV_PCM_RATE_192000,
 62 
 63         .rate_min = 44100,
 64         .rate_max = 192000,
 65         .channels_min = 1,
 66         .channels_max = 0, /* set in pcm_open, depending on capture/playback */
 67         .buffer_bytes_max = MAX_BUFSIZE,
 68         .period_bytes_min = PCM_N_PACKETS_PER_URB * (PCM_MAX_PACKET_SIZE - 4),
 69         .period_bytes_max = MAX_BUFSIZE,
 70         .periods_min = 2,
 71         .periods_max = 1024
 72 };
 73 
 74 static int usb6fire_pcm_set_rate(struct pcm_runtime *rt)
 75 {
 76         int ret;
 77         struct control_runtime *ctrl_rt = rt->chip->control;
 78 
 79         ctrl_rt->usb_streaming = false;
 80         ret = ctrl_rt->update_streaming(ctrl_rt);
 81         if (ret < 0) {
 82                 snd_printk(KERN_ERR PREFIX "error stopping streaming while "
 83                                 "setting samplerate %d.\n", rates[rt->rate]);
 84                 return ret;
 85         }
 86 
 87         ret = ctrl_rt->set_rate(ctrl_rt, rt->rate);
 88         if (ret < 0) {
 89                 snd_printk(KERN_ERR PREFIX "error setting samplerate %d.\n",
 90                                 rates[rt->rate]);
 91                 return ret;
 92         }
 93 
 94         ret = ctrl_rt->set_channels(ctrl_rt, OUT_N_CHANNELS, IN_N_CHANNELS,
 95                         false, false);
 96         if (ret < 0) {
 97                 snd_printk(KERN_ERR PREFIX "error initializing channels "
 98                                 "while setting samplerate %d.\n",
 99                                 rates[rt->rate]);
100                 return ret;
101         }
102 
103         ctrl_rt->usb_streaming = true;
104         ret = ctrl_rt->update_streaming(ctrl_rt);
105         if (ret < 0) {
106                 snd_printk(KERN_ERR PREFIX "error starting streaming while "
107                                 "setting samplerate %d.\n", rates[rt->rate]);
108                 return ret;
109         }
110 
111         rt->in_n_analog = IN_N_CHANNELS;
112         rt->out_n_analog = OUT_N_CHANNELS;
113         rt->in_packet_size = rates_in_packet_size[rt->rate];
114         rt->out_packet_size = rates_out_packet_size[rt->rate];
115         return 0;
116 }
117 
118 static struct pcm_substream *usb6fire_pcm_get_substream(
119                 struct snd_pcm_substream *alsa_sub)
120 {
121         struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
122 
123         if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
124                 return &rt->playback;
125         else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE)
126                 return &rt->capture;
127         snd_printk(KERN_ERR PREFIX "error getting pcm substream slot.\n");
128         return NULL;
129 }
130 
131 /* call with stream_mutex locked */
132 static void usb6fire_pcm_stream_stop(struct pcm_runtime *rt)
133 {
134         int i;
135         struct control_runtime *ctrl_rt = rt->chip->control;
136 
137         if (rt->stream_state != STREAM_DISABLED) {
138 
139                 rt->stream_state = STREAM_STOPPING;
140 
141                 for (i = 0; i < PCM_N_URBS; i++) {
142                         usb_kill_urb(&rt->in_urbs[i].instance);
143                         usb_kill_urb(&rt->out_urbs[i].instance);
144                 }
145                 ctrl_rt->usb_streaming = false;
146                 ctrl_rt->update_streaming(ctrl_rt);
147                 rt->stream_state = STREAM_DISABLED;
148         }
149 }
150 
151 /* call with stream_mutex locked */
152 static int usb6fire_pcm_stream_start(struct pcm_runtime *rt)
153 {
154         int ret;
155         int i;
156         int k;
157         struct usb_iso_packet_descriptor *packet;
158 
159         if (rt->stream_state == STREAM_DISABLED) {
160                 /* submit our in urbs */
161                 rt->stream_wait_cond = false;
162                 rt->stream_state = STREAM_STARTING;
163                 for (i = 0; i < PCM_N_URBS; i++) {
164                         for (k = 0; k < PCM_N_PACKETS_PER_URB; k++) {
165                                 packet = &rt->in_urbs[i].packets[k];
166                                 packet->offset = k * rt->in_packet_size;
167                                 packet->length = rt->in_packet_size;
168                                 packet->actual_length = 0;
169                                 packet->status = 0;
170                         }
171                         ret = usb_submit_urb(&rt->in_urbs[i].instance,
172                                         GFP_ATOMIC);
173                         if (ret) {
174                                 usb6fire_pcm_stream_stop(rt);
175                                 return ret;
176                         }
177                 }
178 
179                 /* wait for first out urb to return (sent in in urb handler) */
180                 wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
181                                 HZ);
182                 if (rt->stream_wait_cond)
183                         rt->stream_state = STREAM_RUNNING;
184                 else {
185                         usb6fire_pcm_stream_stop(rt);
186                         return -EIO;
187                 }
188         }
189         return 0;
190 }
191 
192 /* call with substream locked */
193 static void usb6fire_pcm_capture(struct pcm_substream *sub, struct pcm_urb *urb)
194 {
195         int i;
196         int frame;
197         int frame_count;
198         unsigned int total_length = 0;
199         struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance);
200         struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
201         u32 *src = NULL;
202         u32 *dest = (u32 *) (alsa_rt->dma_area + sub->dma_off
203                         * (alsa_rt->frame_bits >> 3));
204         u32 *dest_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size
205                         * (alsa_rt->frame_bits >> 3));
206         int bytes_per_frame = alsa_rt->channels << 2;
207 
208         for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
209                 /* at least 4 header bytes for valid packet.
210                  * after that: 32 bits per sample for analog channels */
211                 if (urb->packets[i].actual_length > 4)
212                         frame_count = (urb->packets[i].actual_length - 4)
213                                         / (rt->in_n_analog << 2);
214                 else
215                         frame_count = 0;
216 
217                 if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE)
218                         src = (u32 *) (urb->buffer + total_length);
219                 else if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE)
220                         src = (u32 *) (urb->buffer - 1 + total_length);
221                 else
222                         return;
223                 src++; /* skip leading 4 bytes of every packet */
224                 total_length += urb->packets[i].length;
225                 for (frame = 0; frame < frame_count; frame++) {
226                         memcpy(dest, src, bytes_per_frame);
227                         dest += alsa_rt->channels;
228                         src += rt->in_n_analog;
229                         sub->dma_off++;
230                         sub->period_off++;
231                         if (dest == dest_end) {
232                                 sub->dma_off = 0;
233                                 dest = (u32 *) alsa_rt->dma_area;
234                         }
235                 }
236         }
237 }
238 
239 /* call with substream locked */
240 static void usb6fire_pcm_playback(struct pcm_substream *sub,
241                 struct pcm_urb *urb)
242 {
243         int i;
244         int frame;
245         int frame_count;
246         struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance);
247         struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
248         u32 *src = (u32 *) (alsa_rt->dma_area + sub->dma_off
249                         * (alsa_rt->frame_bits >> 3));
250         u32 *src_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size
251                         * (alsa_rt->frame_bits >> 3));
252         u32 *dest;
253         int bytes_per_frame = alsa_rt->channels << 2;
254 
255         if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE)
256                 dest = (u32 *) (urb->buffer - 1);
257         else if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE)
258                 dest = (u32 *) (urb->buffer);
259         else {
260                 snd_printk(KERN_ERR PREFIX "Unknown sample format.");
261                 return;
262         }
263 
264         for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
265                 /* at least 4 header bytes for valid packet.
266                  * after that: 32 bits per sample for analog channels */
267                 if (urb->packets[i].length > 4)
268                         frame_count = (urb->packets[i].length - 4)
269                                         / (rt->out_n_analog << 2);
270                 else
271                         frame_count = 0;
272                 dest++; /* skip leading 4 bytes of every frame */
273                 for (frame = 0; frame < frame_count; frame++) {
274                         memcpy(dest, src, bytes_per_frame);
275                         src += alsa_rt->channels;
276                         dest += rt->out_n_analog;
277                         sub->dma_off++;
278                         sub->period_off++;
279                         if (src == src_end) {
280                                 src = (u32 *) alsa_rt->dma_area;
281                                 sub->dma_off = 0;
282                         }
283                 }
284         }
285 }
286 
287 static void usb6fire_pcm_in_urb_handler(struct urb *usb_urb)
288 {
289         struct pcm_urb *in_urb = usb_urb->context;
290         struct pcm_urb *out_urb = in_urb->peer;
291         struct pcm_runtime *rt = in_urb->chip->pcm;
292         struct pcm_substream *sub;
293         unsigned long flags;
294         int total_length = 0;
295         int frame_count;
296         int frame;
297         int channel;
298         int i;
299         u8 *dest;
300 
301         if (usb_urb->status || rt->panic || rt->stream_state == STREAM_STOPPING)
302                 return;
303         for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
304                 if (in_urb->packets[i].status) {
305                         rt->panic = true;
306                         return;
307                 }
308 
309         if (rt->stream_state == STREAM_DISABLED) {
310                 snd_printk(KERN_ERR PREFIX "internal error: "
311                                 "stream disabled in in-urb handler.\n");
312                 return;
313         }
314 
315         /* receive our capture data */
316         sub = &rt->capture;
317         spin_lock_irqsave(&sub->lock, flags);
318         if (sub->active) {
319                 usb6fire_pcm_capture(sub, in_urb);
320                 if (sub->period_off >= sub->instance->runtime->period_size) {
321                         sub->period_off %= sub->instance->runtime->period_size;
322                         spin_unlock_irqrestore(&sub->lock, flags);
323                         snd_pcm_period_elapsed(sub->instance);
324                 } else
325                         spin_unlock_irqrestore(&sub->lock, flags);
326         } else
327                 spin_unlock_irqrestore(&sub->lock, flags);
328 
329         /* setup out urb structure */
330         for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
331                 out_urb->packets[i].offset = total_length;
332                 out_urb->packets[i].length = (in_urb->packets[i].actual_length
333                                 - 4) / (rt->in_n_analog << 2)
334                                 * (rt->out_n_analog << 2) + 4;
335                 out_urb->packets[i].status = 0;
336                 total_length += out_urb->packets[i].length;
337         }
338         memset(out_urb->buffer, 0, total_length);
339 
340         /* now send our playback data (if a free out urb was found) */
341         sub = &rt->playback;
342         spin_lock_irqsave(&sub->lock, flags);
343         if (sub->active) {
344                 usb6fire_pcm_playback(sub, out_urb);
345                 if (sub->period_off >= sub->instance->runtime->period_size) {
346                         sub->period_off %= sub->instance->runtime->period_size;
347                         spin_unlock_irqrestore(&sub->lock, flags);
348                         snd_pcm_period_elapsed(sub->instance);
349                 } else
350                         spin_unlock_irqrestore(&sub->lock, flags);
351         } else
352                 spin_unlock_irqrestore(&sub->lock, flags);
353 
354         /* setup the 4th byte of each sample (0x40 for analog channels) */
355         dest = out_urb->buffer;
356         for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
357                 if (out_urb->packets[i].length >= 4) {
358                         frame_count = (out_urb->packets[i].length - 4)
359                                         / (rt->out_n_analog << 2);
360                         *(dest++) = 0xaa;
361                         *(dest++) = 0xaa;
362                         *(dest++) = frame_count;
363                         *(dest++) = 0x00;
364                         for (frame = 0; frame < frame_count; frame++)
365                                 for (channel = 0;
366                                                 channel < rt->out_n_analog;
367                                                 channel++) {
368                                         dest += 3; /* skip sample data */
369                                         *(dest++) = 0x40;
370                                 }
371                 }
372         usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
373         usb_submit_urb(&in_urb->instance, GFP_ATOMIC);
374 }
375 
376 static void usb6fire_pcm_out_urb_handler(struct urb *usb_urb)
377 {
378         struct pcm_urb *urb = usb_urb->context;
379         struct pcm_runtime *rt = urb->chip->pcm;
380 
381         if (rt->stream_state == STREAM_STARTING) {
382                 rt->stream_wait_cond = true;
383                 wake_up(&rt->stream_wait_queue);
384         }
385 }
386 
387 static int usb6fire_pcm_open(struct snd_pcm_substream *alsa_sub)
388 {
389         struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
390         struct pcm_substream *sub = NULL;
391         struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
392 
393         if (rt->panic)
394                 return -EPIPE;
395 
396         mutex_lock(&rt->stream_mutex);
397         alsa_rt->hw = pcm_hw;
398 
399         if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK) {
400                 if (rt->rate < ARRAY_SIZE(rates))
401                         alsa_rt->hw.rates = rates_alsaid[rt->rate];
402                 alsa_rt->hw.channels_max = OUT_N_CHANNELS;
403                 sub = &rt->playback;
404         } else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE) {
405                 if (rt->rate < ARRAY_SIZE(rates))
406                         alsa_rt->hw.rates = rates_alsaid[rt->rate];
407                 alsa_rt->hw.channels_max = IN_N_CHANNELS;
408                 sub = &rt->capture;
409         }
410 
411         if (!sub) {
412                 mutex_unlock(&rt->stream_mutex);
413                 snd_printk(KERN_ERR PREFIX "invalid stream type.\n");
414                 return -EINVAL;
415         }
416 
417         sub->instance = alsa_sub;
418         sub->active = false;
419         mutex_unlock(&rt->stream_mutex);
420         return 0;
421 }
422 
423 static int usb6fire_pcm_close(struct snd_pcm_substream *alsa_sub)
424 {
425         struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
426         struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
427         unsigned long flags;
428 
429         if (rt->panic)
430                 return 0;
431 
432         mutex_lock(&rt->stream_mutex);
433         if (sub) {
434                 /* deactivate substream */
435                 spin_lock_irqsave(&sub->lock, flags);
436                 sub->instance = NULL;
437                 sub->active = false;
438                 spin_unlock_irqrestore(&sub->lock, flags);
439 
440                 /* all substreams closed? if so, stop streaming */
441                 if (!rt->playback.instance && !rt->capture.instance) {
442                         usb6fire_pcm_stream_stop(rt);
443                         rt->rate = ARRAY_SIZE(rates);
444                 }
445         }
446         mutex_unlock(&rt->stream_mutex);
447         return 0;
448 }
449 
450 static int usb6fire_pcm_hw_params(struct snd_pcm_substream *alsa_sub,
451                 struct snd_pcm_hw_params *hw_params)
452 {
453         return snd_pcm_lib_alloc_vmalloc_buffer(alsa_sub,
454                                                 params_buffer_bytes(hw_params));
455 }
456 
457 static int usb6fire_pcm_hw_free(struct snd_pcm_substream *alsa_sub)
458 {
459         return snd_pcm_lib_free_vmalloc_buffer(alsa_sub);
460 }
461 
462 static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
463 {
464         struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
465         struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
466         struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
467         int ret;
468 
469         if (rt->panic)
470                 return -EPIPE;
471         if (!sub)
472                 return -ENODEV;
473 
474         mutex_lock(&rt->stream_mutex);
475         sub->dma_off = 0;
476         sub->period_off = 0;
477 
478         if (rt->stream_state == STREAM_DISABLED) {
479                 for (rt->rate = 0; rt->rate < ARRAY_SIZE(rates); rt->rate++)
480                         if (alsa_rt->rate == rates[rt->rate])
481                                 break;
482                 if (rt->rate == ARRAY_SIZE(rates)) {
483                         mutex_unlock(&rt->stream_mutex);
484                         snd_printk("invalid rate %d in prepare.\n",
485                                         alsa_rt->rate);
486                         return -EINVAL;
487                 }
488 
489                 ret = usb6fire_pcm_set_rate(rt);
490                 if (ret) {
491                         mutex_unlock(&rt->stream_mutex);
492                         return ret;
493                 }
494                 ret = usb6fire_pcm_stream_start(rt);
495                 if (ret) {
496                         mutex_unlock(&rt->stream_mutex);
497                         snd_printk(KERN_ERR PREFIX
498                                         "could not start pcm stream.\n");
499                         return ret;
500                 }
501         }
502         mutex_unlock(&rt->stream_mutex);
503         return 0;
504 }
505 
506 static int usb6fire_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
507 {
508         struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
509         struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
510         unsigned long flags;
511 
512         if (rt->panic)
513                 return -EPIPE;
514         if (!sub)
515                 return -ENODEV;
516 
517         switch (cmd) {
518         case SNDRV_PCM_TRIGGER_START:
519         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
520                 spin_lock_irqsave(&sub->lock, flags);
521                 sub->active = true;
522                 spin_unlock_irqrestore(&sub->lock, flags);
523                 return 0;
524 
525         case SNDRV_PCM_TRIGGER_STOP:
526         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
527                 spin_lock_irqsave(&sub->lock, flags);
528                 sub->active = false;
529                 spin_unlock_irqrestore(&sub->lock, flags);
530                 return 0;
531 
532         default:
533                 return -EINVAL;
534         }
535 }
536 
537 static snd_pcm_uframes_t usb6fire_pcm_pointer(
538                 struct snd_pcm_substream *alsa_sub)
539 {
540         struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
541         struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
542         unsigned long flags;
543         snd_pcm_uframes_t ret;
544 
545         if (rt->panic || !sub)
546                 return SNDRV_PCM_POS_XRUN;
547 
548         spin_lock_irqsave(&sub->lock, flags);
549         ret = sub->dma_off;
550         spin_unlock_irqrestore(&sub->lock, flags);
551         return ret;
552 }
553 
554 static struct snd_pcm_ops pcm_ops = {
555         .open = usb6fire_pcm_open,
556         .close = usb6fire_pcm_close,
557         .ioctl = snd_pcm_lib_ioctl,
558         .hw_params = usb6fire_pcm_hw_params,
559         .hw_free = usb6fire_pcm_hw_free,
560         .prepare = usb6fire_pcm_prepare,
561         .trigger = usb6fire_pcm_trigger,
562         .pointer = usb6fire_pcm_pointer,
563         .page = snd_pcm_lib_get_vmalloc_page,
564         .mmap = snd_pcm_lib_mmap_vmalloc,
565 };
566 
567 static void usb6fire_pcm_init_urb(struct pcm_urb *urb,
568                                   struct sfire_chip *chip, bool in, int ep,
569                                   void (*handler)(struct urb *))
570 {
571         urb->chip = chip;
572         usb_init_urb(&urb->instance);
573         urb->instance.transfer_buffer = urb->buffer;
574         urb->instance.transfer_buffer_length =
575                         PCM_N_PACKETS_PER_URB * PCM_MAX_PACKET_SIZE;
576         urb->instance.dev = chip->dev;
577         urb->instance.pipe = in ? usb_rcvisocpipe(chip->dev, ep)
578                         : usb_sndisocpipe(chip->dev, ep);
579         urb->instance.interval = 1;
580         urb->instance.complete = handler;
581         urb->instance.context = urb;
582         urb->instance.number_of_packets = PCM_N_PACKETS_PER_URB;
583 }
584 
585 static int usb6fire_pcm_buffers_init(struct pcm_runtime *rt)
586 {
587         int i;
588 
589         for (i = 0; i < PCM_N_URBS; i++) {
590                 rt->out_urbs[i].buffer = kzalloc(PCM_N_PACKETS_PER_URB
591                                 * PCM_MAX_PACKET_SIZE, GFP_KERNEL);
592                 if (!rt->out_urbs[i].buffer)
593                         return -ENOMEM;
594                 rt->in_urbs[i].buffer = kzalloc(PCM_N_PACKETS_PER_URB
595                                 * PCM_MAX_PACKET_SIZE, GFP_KERNEL);
596                 if (!rt->in_urbs[i].buffer)
597                         return -ENOMEM;
598         }
599         return 0;
600 }
601 
602 static void usb6fire_pcm_buffers_destroy(struct pcm_runtime *rt)
603 {
604         int i;
605 
606         for (i = 0; i < PCM_N_URBS; i++) {
607                 kfree(rt->out_urbs[i].buffer);
608                 kfree(rt->in_urbs[i].buffer);
609         }
610 }
611 
612 int usb6fire_pcm_init(struct sfire_chip *chip)
613 {
614         int i;
615         int ret;
616         struct snd_pcm *pcm;
617         struct pcm_runtime *rt =
618                         kzalloc(sizeof(struct pcm_runtime), GFP_KERNEL);
619 
620         if (!rt)
621                 return -ENOMEM;
622 
623         ret = usb6fire_pcm_buffers_init(rt);
624         if (ret) {
625                 usb6fire_pcm_buffers_destroy(rt);
626                 kfree(rt);
627                 return ret;
628         }
629 
630         rt->chip = chip;
631         rt->stream_state = STREAM_DISABLED;
632         rt->rate = ARRAY_SIZE(rates);
633         init_waitqueue_head(&rt->stream_wait_queue);
634         mutex_init(&rt->stream_mutex);
635 
636         spin_lock_init(&rt->playback.lock);
637         spin_lock_init(&rt->capture.lock);
638 
639         for (i = 0; i < PCM_N_URBS; i++) {
640                 usb6fire_pcm_init_urb(&rt->in_urbs[i], chip, true, IN_EP,
641                                 usb6fire_pcm_in_urb_handler);
642                 usb6fire_pcm_init_urb(&rt->out_urbs[i], chip, false, OUT_EP,
643                                 usb6fire_pcm_out_urb_handler);
644 
645                 rt->in_urbs[i].peer = &rt->out_urbs[i];
646                 rt->out_urbs[i].peer = &rt->in_urbs[i];
647         }
648 
649         ret = snd_pcm_new(chip->card, "DMX6FireUSB", 0, 1, 1, &pcm);
650         if (ret < 0) {
651                 usb6fire_pcm_buffers_destroy(rt);
652                 kfree(rt);
653                 snd_printk(KERN_ERR PREFIX "cannot create pcm instance.\n");
654                 return ret;
655         }
656 
657         pcm->private_data = rt;
658         strcpy(pcm->name, "DMX 6Fire USB");
659         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
660         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops);
661 
662         if (ret) {
663                 usb6fire_pcm_buffers_destroy(rt);
664                 kfree(rt);
665                 snd_printk(KERN_ERR PREFIX
666                                 "error preallocating pcm buffers.\n");
667                 return ret;
668         }
669         rt->instance = pcm;
670 
671         chip->pcm = rt;
672         return 0;
673 }
674 
675 void usb6fire_pcm_abort(struct sfire_chip *chip)
676 {
677         struct pcm_runtime *rt = chip->pcm;
678         unsigned long flags;
679         int i;
680 
681         if (rt) {
682                 rt->panic = true;
683 
684                 if (rt->playback.instance) {
685                         snd_pcm_stream_lock_irqsave(rt->playback.instance, flags);
686                         snd_pcm_stop(rt->playback.instance,
687                                         SNDRV_PCM_STATE_XRUN);
688                         snd_pcm_stream_unlock_irqrestore(rt->playback.instance, flags);
689                 }
690 
691                 if (rt->capture.instance) {
692                         snd_pcm_stream_lock_irqsave(rt->capture.instance, flags);
693                         snd_pcm_stop(rt->capture.instance,
694                                         SNDRV_PCM_STATE_XRUN);
695                         snd_pcm_stream_unlock_irqrestore(rt->capture.instance, flags);
696                 }
697 
698                 for (i = 0; i < PCM_N_URBS; i++) {
699                         usb_poison_urb(&rt->in_urbs[i].instance);
700                         usb_poison_urb(&rt->out_urbs[i].instance);
701                 }
702 
703         }
704 }
705 
706 void usb6fire_pcm_destroy(struct sfire_chip *chip)
707 {
708         struct pcm_runtime *rt = chip->pcm;
709 
710         usb6fire_pcm_buffers_destroy(rt);
711         kfree(rt);
712         chip->pcm = NULL;
713 }
714 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp