1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/slab.h>
18#include <sound/pcm.h>
19
20#include "pcm.h"
21#include "chip.h"
22
23#define OUT_EP 0x2
24#define PCM_N_URBS 8
25#define PCM_PACKET_SIZE 4096
26#define PCM_BUFFER_SIZE (2 * PCM_N_URBS * PCM_PACKET_SIZE)
27
28struct pcm_urb {
29 struct hiface_chip *chip;
30
31 struct urb instance;
32 struct usb_anchor submitted;
33 u8 *buffer;
34};
35
36struct pcm_substream {
37 spinlock_t lock;
38 struct snd_pcm_substream *instance;
39
40 bool active;
41 snd_pcm_uframes_t dma_off;
42 snd_pcm_uframes_t period_off;
43};
44
45enum {
46 STREAM_DISABLED,
47 STREAM_STARTING,
48 STREAM_RUNNING,
49 STREAM_STOPPING
50};
51
52struct pcm_runtime {
53 struct hiface_chip *chip;
54 struct snd_pcm *instance;
55
56 struct pcm_substream playback;
57 bool panic;
58
59 struct pcm_urb out_urbs[PCM_N_URBS];
60
61 struct mutex stream_mutex;
62 u8 stream_state;
63 u8 extra_freq;
64 wait_queue_head_t stream_wait_queue;
65 bool stream_wait_cond;
66};
67
68static const unsigned int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000,
69 352800, 384000 };
70static const struct snd_pcm_hw_constraint_list constraints_extra_rates = {
71 .count = ARRAY_SIZE(rates),
72 .list = rates,
73 .mask = 0,
74};
75
76static const struct snd_pcm_hardware pcm_hw = {
77 .info = SNDRV_PCM_INFO_MMAP |
78 SNDRV_PCM_INFO_INTERLEAVED |
79 SNDRV_PCM_INFO_BLOCK_TRANSFER |
80 SNDRV_PCM_INFO_PAUSE |
81 SNDRV_PCM_INFO_MMAP_VALID |
82 SNDRV_PCM_INFO_BATCH,
83
84 .formats = SNDRV_PCM_FMTBIT_S32_LE,
85
86 .rates = SNDRV_PCM_RATE_44100 |
87 SNDRV_PCM_RATE_48000 |
88 SNDRV_PCM_RATE_88200 |
89 SNDRV_PCM_RATE_96000 |
90 SNDRV_PCM_RATE_176400 |
91 SNDRV_PCM_RATE_192000,
92
93 .rate_min = 44100,
94 .rate_max = 192000,
95 .channels_min = 2,
96 .channels_max = 2,
97 .buffer_bytes_max = PCM_BUFFER_SIZE,
98 .period_bytes_min = PCM_PACKET_SIZE,
99 .period_bytes_max = PCM_BUFFER_SIZE,
100 .periods_min = 2,
101 .periods_max = 1024
102};
103
104
105#define HIFACE_SET_RATE_REQUEST 0xb0
106
107#define HIFACE_RATE_44100 0x43
108#define HIFACE_RATE_48000 0x4b
109#define HIFACE_RATE_88200 0x42
110#define HIFACE_RATE_96000 0x4a
111#define HIFACE_RATE_176400 0x40
112#define HIFACE_RATE_192000 0x48
113#define HIFACE_RATE_352800 0x58
114#define HIFACE_RATE_384000 0x68
115
116static int hiface_pcm_set_rate(struct pcm_runtime *rt, unsigned int rate)
117{
118 struct usb_device *device = rt->chip->dev;
119 u16 rate_value;
120 int ret;
121
122
123
124
125 switch (rate) {
126 case 44100:
127 rate_value = HIFACE_RATE_44100;
128 break;
129 case 48000:
130 rate_value = HIFACE_RATE_48000;
131 break;
132 case 88200:
133 rate_value = HIFACE_RATE_88200;
134 break;
135 case 96000:
136 rate_value = HIFACE_RATE_96000;
137 break;
138 case 176400:
139 rate_value = HIFACE_RATE_176400;
140 break;
141 case 192000:
142 rate_value = HIFACE_RATE_192000;
143 break;
144 case 352800:
145 rate_value = HIFACE_RATE_352800;
146 break;
147 case 384000:
148 rate_value = HIFACE_RATE_384000;
149 break;
150 default:
151 dev_err(&device->dev, "Unsupported rate %d\n", rate);
152 return -EINVAL;
153 }
154
155
156
157
158
159
160
161
162
163 ret = usb_control_msg(device, usb_sndctrlpipe(device, 0),
164 HIFACE_SET_RATE_REQUEST,
165 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
166 rate_value, 0, NULL, 0, 100);
167 if (ret < 0) {
168 dev_err(&device->dev, "Error setting samplerate %d.\n", rate);
169 return ret;
170 }
171
172 return 0;
173}
174
175static struct pcm_substream *hiface_pcm_get_substream(struct snd_pcm_substream
176 *alsa_sub)
177{
178 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
179 struct device *device = &rt->chip->dev->dev;
180
181 if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
182 return &rt->playback;
183
184 dev_err(device, "Error getting pcm substream slot.\n");
185 return NULL;
186}
187
188
189static void hiface_pcm_stream_stop(struct pcm_runtime *rt)
190{
191 int i, time;
192
193 if (rt->stream_state != STREAM_DISABLED) {
194 rt->stream_state = STREAM_STOPPING;
195
196 for (i = 0; i < PCM_N_URBS; i++) {
197 time = usb_wait_anchor_empty_timeout(
198 &rt->out_urbs[i].submitted, 100);
199 if (!time)
200 usb_kill_anchored_urbs(
201 &rt->out_urbs[i].submitted);
202 usb_kill_urb(&rt->out_urbs[i].instance);
203 }
204
205 rt->stream_state = STREAM_DISABLED;
206 }
207}
208
209
210static int hiface_pcm_stream_start(struct pcm_runtime *rt)
211{
212 int ret = 0;
213 int i;
214
215 if (rt->stream_state == STREAM_DISABLED) {
216
217
218 rt->panic = false;
219
220
221 rt->stream_state = STREAM_STARTING;
222 for (i = 0; i < PCM_N_URBS; i++) {
223 memset(rt->out_urbs[i].buffer, 0, PCM_PACKET_SIZE);
224 usb_anchor_urb(&rt->out_urbs[i].instance,
225 &rt->out_urbs[i].submitted);
226 ret = usb_submit_urb(&rt->out_urbs[i].instance,
227 GFP_ATOMIC);
228 if (ret) {
229 hiface_pcm_stream_stop(rt);
230 return ret;
231 }
232 }
233
234
235 wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
236 HZ);
237 if (rt->stream_wait_cond) {
238 struct device *device = &rt->chip->dev->dev;
239 dev_dbg(device, "%s: Stream is running wakeup event\n",
240 __func__);
241 rt->stream_state = STREAM_RUNNING;
242 } else {
243 hiface_pcm_stream_stop(rt);
244 return -EIO;
245 }
246 }
247 return ret;
248}
249
250
251static void memcpy_swahw32(u8 *dest, u8 *src, unsigned int n)
252{
253 unsigned int i;
254
255 for (i = 0; i < n / 4; i++)
256 ((u32 *)dest)[i] = swahw32(((u32 *)src)[i]);
257}
258
259
260
261static bool hiface_pcm_playback(struct pcm_substream *sub, struct pcm_urb *urb)
262{
263 struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
264 struct device *device = &urb->chip->dev->dev;
265 u8 *source;
266 unsigned int pcm_buffer_size;
267
268 WARN_ON(alsa_rt->format != SNDRV_PCM_FORMAT_S32_LE);
269
270 pcm_buffer_size = snd_pcm_lib_buffer_bytes(sub->instance);
271
272 if (sub->dma_off + PCM_PACKET_SIZE <= pcm_buffer_size) {
273 dev_dbg(device, "%s: (1) buffer_size %#x dma_offset %#x\n", __func__,
274 (unsigned int) pcm_buffer_size,
275 (unsigned int) sub->dma_off);
276
277 source = alsa_rt->dma_area + sub->dma_off;
278 memcpy_swahw32(urb->buffer, source, PCM_PACKET_SIZE);
279 } else {
280
281 unsigned int len;
282
283 dev_dbg(device, "%s: (2) buffer_size %#x dma_offset %#x\n", __func__,
284 (unsigned int) pcm_buffer_size,
285 (unsigned int) sub->dma_off);
286
287 len = pcm_buffer_size - sub->dma_off;
288
289 source = alsa_rt->dma_area + sub->dma_off;
290 memcpy_swahw32(urb->buffer, source, len);
291
292 source = alsa_rt->dma_area;
293 memcpy_swahw32(urb->buffer + len, source,
294 PCM_PACKET_SIZE - len);
295 }
296 sub->dma_off += PCM_PACKET_SIZE;
297 if (sub->dma_off >= pcm_buffer_size)
298 sub->dma_off -= pcm_buffer_size;
299
300 sub->period_off += PCM_PACKET_SIZE;
301 if (sub->period_off >= alsa_rt->period_size) {
302 sub->period_off %= alsa_rt->period_size;
303 return true;
304 }
305 return false;
306}
307
308static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
309{
310 struct pcm_urb *out_urb = usb_urb->context;
311 struct pcm_runtime *rt = out_urb->chip->pcm;
312 struct pcm_substream *sub;
313 bool do_period_elapsed = false;
314 unsigned long flags;
315 int ret;
316
317 if (rt->panic || rt->stream_state == STREAM_STOPPING)
318 return;
319
320 if (unlikely(usb_urb->status == -ENOENT ||
321 usb_urb->status == -ENODEV ||
322 usb_urb->status == -ECONNRESET ||
323 usb_urb->status == -ESHUTDOWN)) {
324 goto out_fail;
325 }
326
327 if (rt->stream_state == STREAM_STARTING) {
328 rt->stream_wait_cond = true;
329 wake_up(&rt->stream_wait_queue);
330 }
331
332
333 sub = &rt->playback;
334 spin_lock_irqsave(&sub->lock, flags);
335 if (sub->active)
336 do_period_elapsed = hiface_pcm_playback(sub, out_urb);
337 else
338 memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
339
340 spin_unlock_irqrestore(&sub->lock, flags);
341
342 if (do_period_elapsed)
343 snd_pcm_period_elapsed(sub->instance);
344
345 ret = usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
346 if (ret < 0)
347 goto out_fail;
348
349 return;
350
351out_fail:
352 rt->panic = true;
353}
354
355static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
356{
357 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
358 struct pcm_substream *sub = NULL;
359 struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
360 int ret;
361
362 if (rt->panic)
363 return -EPIPE;
364
365 mutex_lock(&rt->stream_mutex);
366 alsa_rt->hw = pcm_hw;
367
368 if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
369 sub = &rt->playback;
370
371 if (!sub) {
372 struct device *device = &rt->chip->dev->dev;
373 mutex_unlock(&rt->stream_mutex);
374 dev_err(device, "Invalid stream type\n");
375 return -EINVAL;
376 }
377
378 if (rt->extra_freq) {
379 alsa_rt->hw.rates |= SNDRV_PCM_RATE_KNOT;
380 alsa_rt->hw.rate_max = 384000;
381
382
383 ret = snd_pcm_hw_constraint_list(alsa_sub->runtime, 0,
384 SNDRV_PCM_HW_PARAM_RATE,
385 &constraints_extra_rates);
386 if (ret < 0) {
387 mutex_unlock(&rt->stream_mutex);
388 return ret;
389 }
390 }
391
392 sub->instance = alsa_sub;
393 sub->active = false;
394 mutex_unlock(&rt->stream_mutex);
395 return 0;
396}
397
398static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
399{
400 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
401 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
402 unsigned long flags;
403
404 if (rt->panic)
405 return 0;
406
407 mutex_lock(&rt->stream_mutex);
408 if (sub) {
409 hiface_pcm_stream_stop(rt);
410
411
412 spin_lock_irqsave(&sub->lock, flags);
413 sub->instance = NULL;
414 sub->active = false;
415 spin_unlock_irqrestore(&sub->lock, flags);
416
417 }
418 mutex_unlock(&rt->stream_mutex);
419 return 0;
420}
421
422static int hiface_pcm_hw_params(struct snd_pcm_substream *alsa_sub,
423 struct snd_pcm_hw_params *hw_params)
424{
425 return snd_pcm_lib_alloc_vmalloc_buffer(alsa_sub,
426 params_buffer_bytes(hw_params));
427}
428
429static int hiface_pcm_hw_free(struct snd_pcm_substream *alsa_sub)
430{
431 return snd_pcm_lib_free_vmalloc_buffer(alsa_sub);
432}
433
434static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
435{
436 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
437 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
438 struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
439 int ret;
440
441 if (rt->panic)
442 return -EPIPE;
443 if (!sub)
444 return -ENODEV;
445
446 mutex_lock(&rt->stream_mutex);
447
448 hiface_pcm_stream_stop(rt);
449
450 sub->dma_off = 0;
451 sub->period_off = 0;
452
453 if (rt->stream_state == STREAM_DISABLED) {
454
455 ret = hiface_pcm_set_rate(rt, alsa_rt->rate);
456 if (ret) {
457 mutex_unlock(&rt->stream_mutex);
458 return ret;
459 }
460 ret = hiface_pcm_stream_start(rt);
461 if (ret) {
462 mutex_unlock(&rt->stream_mutex);
463 return ret;
464 }
465 }
466 mutex_unlock(&rt->stream_mutex);
467 return 0;
468}
469
470static int hiface_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
471{
472 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
473 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
474
475 if (rt->panic)
476 return -EPIPE;
477 if (!sub)
478 return -ENODEV;
479
480 switch (cmd) {
481 case SNDRV_PCM_TRIGGER_START:
482 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
483 spin_lock_irq(&sub->lock);
484 sub->active = true;
485 spin_unlock_irq(&sub->lock);
486 return 0;
487
488 case SNDRV_PCM_TRIGGER_STOP:
489 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
490 spin_lock_irq(&sub->lock);
491 sub->active = false;
492 spin_unlock_irq(&sub->lock);
493 return 0;
494
495 default:
496 return -EINVAL;
497 }
498}
499
500static snd_pcm_uframes_t hiface_pcm_pointer(struct snd_pcm_substream *alsa_sub)
501{
502 struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
503 struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
504 unsigned long flags;
505 snd_pcm_uframes_t dma_offset;
506
507 if (rt->panic || !sub)
508 return SNDRV_PCM_POS_XRUN;
509
510 spin_lock_irqsave(&sub->lock, flags);
511 dma_offset = sub->dma_off;
512 spin_unlock_irqrestore(&sub->lock, flags);
513 return bytes_to_frames(alsa_sub->runtime, dma_offset);
514}
515
516static const struct snd_pcm_ops pcm_ops = {
517 .open = hiface_pcm_open,
518 .close = hiface_pcm_close,
519 .ioctl = snd_pcm_lib_ioctl,
520 .hw_params = hiface_pcm_hw_params,
521 .hw_free = hiface_pcm_hw_free,
522 .prepare = hiface_pcm_prepare,
523 .trigger = hiface_pcm_trigger,
524 .pointer = hiface_pcm_pointer,
525 .page = snd_pcm_lib_get_vmalloc_page,
526 .mmap = snd_pcm_lib_mmap_vmalloc,
527};
528
529static int hiface_pcm_init_urb(struct pcm_urb *urb,
530 struct hiface_chip *chip,
531 unsigned int ep,
532 void (*handler)(struct urb *))
533{
534 urb->chip = chip;
535 usb_init_urb(&urb->instance);
536
537 urb->buffer = kzalloc(PCM_PACKET_SIZE, GFP_KERNEL);
538 if (!urb->buffer)
539 return -ENOMEM;
540
541 usb_fill_bulk_urb(&urb->instance, chip->dev,
542 usb_sndbulkpipe(chip->dev, ep), (void *)urb->buffer,
543 PCM_PACKET_SIZE, handler, urb);
544 if (usb_urb_ep_type_check(&urb->instance))
545 return -EINVAL;
546 init_usb_anchor(&urb->submitted);
547
548 return 0;
549}
550
551void hiface_pcm_abort(struct hiface_chip *chip)
552{
553 struct pcm_runtime *rt = chip->pcm;
554
555 if (rt) {
556 rt->panic = true;
557
558 mutex_lock(&rt->stream_mutex);
559 hiface_pcm_stream_stop(rt);
560 mutex_unlock(&rt->stream_mutex);
561 }
562}
563
564static void hiface_pcm_destroy(struct hiface_chip *chip)
565{
566 struct pcm_runtime *rt = chip->pcm;
567 int i;
568
569 for (i = 0; i < PCM_N_URBS; i++)
570 kfree(rt->out_urbs[i].buffer);
571
572 kfree(chip->pcm);
573 chip->pcm = NULL;
574}
575
576static void hiface_pcm_free(struct snd_pcm *pcm)
577{
578 struct pcm_runtime *rt = pcm->private_data;
579
580 if (rt)
581 hiface_pcm_destroy(rt->chip);
582}
583
584int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
585{
586 int i;
587 int ret;
588 struct snd_pcm *pcm;
589 struct pcm_runtime *rt;
590
591 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
592 if (!rt)
593 return -ENOMEM;
594
595 rt->chip = chip;
596 rt->stream_state = STREAM_DISABLED;
597 if (extra_freq)
598 rt->extra_freq = 1;
599
600 init_waitqueue_head(&rt->stream_wait_queue);
601 mutex_init(&rt->stream_mutex);
602 spin_lock_init(&rt->playback.lock);
603
604 for (i = 0; i < PCM_N_URBS; i++) {
605 ret = hiface_pcm_init_urb(&rt->out_urbs[i], chip, OUT_EP,
606 hiface_pcm_out_urb_handler);
607 if (ret < 0)
608 return ret;
609 }
610
611 ret = snd_pcm_new(chip->card, "USB-SPDIF Audio", 0, 1, 0, &pcm);
612 if (ret < 0) {
613 kfree(rt);
614 dev_err(&chip->dev->dev, "Cannot create pcm instance\n");
615 return ret;
616 }
617
618 pcm->private_data = rt;
619 pcm->private_free = hiface_pcm_free;
620
621 strlcpy(pcm->name, "USB-SPDIF Audio", sizeof(pcm->name));
622 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
623
624 rt->instance = pcm;
625
626 chip->pcm = rt;
627 return 0;
628}
629