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