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