1 // SPDX-License-Identifier: GPL-2.0-or-later
5 /* USX2Y "rawusb" aka hwdep_pcm implementation
7 Its usb's unableness to atomically handle power of 2 period sized data chuncs
8 at standard samplerates,
9 what led to this part of the usx2y module:
10 It provides the alsa kernel half of the usx2y-alsa-jack driver pair.
11 The pair uses a hardware dependent alsa-device for mmaped pcm transport.
13 The usb_hc moves pcm data from/into memory via DMA.
14 That memory is mmaped by jack's usx2y driver.
15 Jack's usx2y driver is the first/last to read/write pcm data.
16 Read/write is a combination of power of 2 period shaping and
17 float/int conversation.
18 Compared to mainline alsa/jack we leave out power of 2 period shaping inside
19 snd-usb-usx2y which needs memcpy() and additional buffers.
20 As a side effect possible unwanted pcm-data coruption resulting of
21 standard alsa's snd-usb-usx2y period shaping scheme falls away.
22 Result is sane jack operation at buffering schemes down to 128frames,
24 plain usx2y alsa mode is able to achieve 64frames, 4periods, but only at the
25 cost of easier triggered i.e. aeolus xruns (128 or 256frames,
26 2periods works but is useless cause of crackling).
28 This is a first "proof of concept" implementation.
29 Later, functionalities should migrate to more appropriate places:
31 - The jackd could mmap its float-pcm buffers directly from alsa-lib.
32 - alsa-lib could provide power of 2 period sized shaping combined with int/float
34 Currently the usx2y jack driver provides above 2 services.
36 - rawusb dma pcm buffer transport should go to snd-usb-lib, so also snd-usb-audio
38 Currently rawusb dma pcm buffer transport (this file) is only available to snd-usb-usx2y.
41 #include <linux/delay.h>
42 #include <linux/gfp.h>
43 #include "usbusx2yaudio.c"
45 #if defined(USX2Y_NRPACKS_VARIABLE) || USX2Y_NRPACKS == 1
47 #include <sound/hwdep.h>
49 static int usx2y_usbpcm_urb_capt_retire(struct snd_usx2y_substream *subs)
51 struct urb *urb = subs->completed_urb;
52 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
53 int i, lens = 0, hwptr_done = subs->hwptr_done;
54 struct usx2ydev *usx2y = subs->usx2y;
57 if (usx2y->hwdep_pcm_shm->capture_iso_start < 0) { //FIXME
58 head = usx2y->hwdep_pcm_shm->captured_iso_head + 1;
59 if (head >= ARRAY_SIZE(usx2y->hwdep_pcm_shm->captured_iso))
61 usx2y->hwdep_pcm_shm->capture_iso_start = head;
62 snd_printdd("cap start %i\n", head);
64 for (i = 0; i < nr_of_packs(); i++) {
65 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
67 "active frame status %i. Most probably some hardware problem.\n",
68 urb->iso_frame_desc[i].status);
69 return urb->iso_frame_desc[i].status;
71 lens += urb->iso_frame_desc[i].actual_length / usx2y->stride;
74 if (hwptr_done >= runtime->buffer_size)
75 hwptr_done -= runtime->buffer_size;
76 subs->hwptr_done = hwptr_done;
77 subs->transfer_done += lens;
78 /* update the pointer, call callback if necessary */
79 if (subs->transfer_done >= runtime->period_size) {
80 subs->transfer_done -= runtime->period_size;
81 snd_pcm_period_elapsed(subs->pcm_substream);
86 static int usx2y_iso_frames_per_buffer(struct snd_pcm_runtime *runtime,
87 struct usx2ydev *usx2y)
89 return (runtime->buffer_size * 1000) / usx2y->rate + 1; //FIXME: so far only correct period_size == 2^x ?
93 * prepare urb for playback data pipe
95 * we copy the data directly from the pcm buffer.
96 * the current position to be copied is held in hwptr field.
97 * since a urb can handle only a single linear buffer, if the total
98 * transferred area overflows the buffer boundary, we cannot send
99 * it directly from the buffer. thus the data is once copied to
100 * a temporary buffer and urb points to that.
102 static int usx2y_hwdep_urb_play_prepare(struct snd_usx2y_substream *subs,
105 int count, counts, pack;
106 struct usx2ydev *usx2y = subs->usx2y;
107 struct snd_usx2y_hwdep_pcm_shm *shm = usx2y->hwdep_pcm_shm;
108 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
110 if (shm->playback_iso_start < 0) {
111 shm->playback_iso_start = shm->captured_iso_head -
112 usx2y_iso_frames_per_buffer(runtime, usx2y);
113 if (shm->playback_iso_start < 0)
114 shm->playback_iso_start += ARRAY_SIZE(shm->captured_iso);
115 shm->playback_iso_head = shm->playback_iso_start;
119 for (pack = 0; pack < nr_of_packs(); pack++) {
120 /* calculate the size of a packet */
121 counts = shm->captured_iso[shm->playback_iso_head].length / usx2y->stride;
122 if (counts < 43 || counts > 50) {
123 snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
126 /* set up descriptor */
127 urb->iso_frame_desc[pack].offset = shm->captured_iso[shm->playback_iso_head].offset;
128 urb->iso_frame_desc[pack].length = shm->captured_iso[shm->playback_iso_head].length;
129 if (atomic_read(&subs->state) != STATE_RUNNING)
130 memset((char *)urb->transfer_buffer + urb->iso_frame_desc[pack].offset, 0,
131 urb->iso_frame_desc[pack].length);
132 if (++shm->playback_iso_head >= ARRAY_SIZE(shm->captured_iso))
133 shm->playback_iso_head = 0;
136 urb->transfer_buffer_length = count * usx2y->stride;
140 static void usx2y_usbpcm_urb_capt_iso_advance(struct snd_usx2y_substream *subs,
143 struct usb_iso_packet_descriptor *desc;
144 struct snd_usx2y_hwdep_pcm_shm *shm;
147 for (pack = 0; pack < nr_of_packs(); ++pack) {
148 desc = urb->iso_frame_desc + pack;
150 shm = subs->usx2y->hwdep_pcm_shm;
151 head = shm->captured_iso_head + 1;
152 if (head >= ARRAY_SIZE(shm->captured_iso))
154 shm->captured_iso[head].frame = urb->start_frame + pack;
155 shm->captured_iso[head].offset = desc->offset;
156 shm->captured_iso[head].length = desc->actual_length;
157 shm->captured_iso_head = head;
158 shm->captured_iso_frames++;
160 desc->offset += desc->length * NRURBS * nr_of_packs();
161 if (desc->offset + desc->length >= SSS)
162 desc->offset -= (SSS - desc->length);
166 static int usx2y_usbpcm_usbframe_complete(struct snd_usx2y_substream *capsubs,
167 struct snd_usx2y_substream *capsubs2,
168 struct snd_usx2y_substream *playbacksubs,
172 struct urb *urb = playbacksubs->completed_urb;
174 state = atomic_read(&playbacksubs->state);
176 if (state == STATE_RUNNING)
177 usx2y_urb_play_retire(playbacksubs, urb);
178 else if (state >= STATE_PRERUNNING)
179 atomic_inc(&playbacksubs->state);
182 case STATE_STARTING1:
183 urb = playbacksubs->urb[0];
184 atomic_inc(&playbacksubs->state);
186 case STATE_STARTING2:
187 urb = playbacksubs->urb[1];
188 atomic_inc(&playbacksubs->state);
193 err = usx2y_hwdep_urb_play_prepare(playbacksubs, urb);
196 err = usx2y_hwdep_urb_play_prepare(playbacksubs, urb);
201 playbacksubs->completed_urb = NULL;
203 state = atomic_read(&capsubs->state);
204 if (state >= STATE_PREPARED) {
205 if (state == STATE_RUNNING) {
206 err = usx2y_usbpcm_urb_capt_retire(capsubs);
209 } else if (state >= STATE_PRERUNNING) {
210 atomic_inc(&capsubs->state);
212 usx2y_usbpcm_urb_capt_iso_advance(capsubs, capsubs->completed_urb);
214 usx2y_usbpcm_urb_capt_iso_advance(NULL, capsubs2->completed_urb);
215 err = usx2y_urb_submit(capsubs, capsubs->completed_urb, frame);
219 err = usx2y_urb_submit(capsubs2, capsubs2->completed_urb, frame);
224 capsubs->completed_urb = NULL;
226 capsubs2->completed_urb = NULL;
230 static void i_usx2y_usbpcm_urb_complete(struct urb *urb)
232 struct snd_usx2y_substream *subs = urb->context;
233 struct usx2ydev *usx2y = subs->usx2y;
234 struct snd_usx2y_substream *capsubs, *capsubs2, *playbacksubs;
236 if (unlikely(atomic_read(&subs->state) < STATE_PREPARED)) {
237 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
238 usb_get_current_frame_number(usx2y->dev),
239 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
240 urb->status, urb->start_frame);
243 if (unlikely(urb->status)) {
244 usx2y_error_urb_status(usx2y, subs, urb);
248 subs->completed_urb = urb;
249 capsubs = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
250 capsubs2 = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE + 2];
251 playbacksubs = usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
252 if (capsubs->completed_urb && atomic_read(&capsubs->state) >= STATE_PREPARED &&
253 (!capsubs2 || capsubs2->completed_urb) &&
254 (playbacksubs->completed_urb || atomic_read(&playbacksubs->state) < STATE_PREPARED)) {
255 if (!usx2y_usbpcm_usbframe_complete(capsubs, capsubs2, playbacksubs, urb->start_frame)) {
256 usx2y->wait_iso_frame += nr_of_packs();
259 usx2y_clients_stop(usx2y);
264 static void usx2y_hwdep_urb_release(struct urb **urb)
272 * release a substream
274 static void usx2y_usbpcm_urbs_release(struct snd_usx2y_substream *subs)
278 snd_printdd("snd_usx2y_urbs_release() %i\n", subs->endpoint);
279 for (i = 0; i < NRURBS; i++)
280 usx2y_hwdep_urb_release(subs->urb + i);
283 static void usx2y_usbpcm_subs_startup_finish(struct usx2ydev *usx2y)
285 usx2y_urbs_set_complete(usx2y, i_usx2y_usbpcm_urb_complete);
286 usx2y->prepare_subs = NULL;
289 static void i_usx2y_usbpcm_subs_startup(struct urb *urb)
291 struct snd_usx2y_substream *subs = urb->context;
292 struct usx2ydev *usx2y = subs->usx2y;
293 struct snd_usx2y_substream *prepare_subs = usx2y->prepare_subs;
294 struct snd_usx2y_substream *cap_subs2;
297 urb->start_frame == prepare_subs->urb[0]->start_frame) {
298 atomic_inc(&prepare_subs->state);
299 if (prepare_subs == usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]) {
300 cap_subs2 = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE + 2];
302 atomic_inc(&cap_subs2->state);
304 usx2y_usbpcm_subs_startup_finish(usx2y);
305 wake_up(&usx2y->prepare_wait_queue);
308 i_usx2y_usbpcm_urb_complete(urb);
312 * initialize a substream's urbs
314 static int usx2y_usbpcm_urbs_allocate(struct snd_usx2y_substream *subs)
318 int is_playback = subs == subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
319 struct usb_device *dev = subs->usx2y->dev;
322 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
323 usb_rcvisocpipe(dev, subs->endpoint);
324 subs->maxpacksize = usb_maxpacket(dev, pipe);
325 if (!subs->maxpacksize)
328 /* allocate and initialize data urbs */
329 for (i = 0; i < NRURBS; i++) {
330 purb = subs->urb + i;
335 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
337 usx2y_usbpcm_urbs_release(subs);
340 (*purb)->transfer_buffer = is_playback ?
341 subs->usx2y->hwdep_pcm_shm->playback : (
342 subs->endpoint == 0x8 ?
343 subs->usx2y->hwdep_pcm_shm->capture0x8 :
344 subs->usx2y->hwdep_pcm_shm->capture0xA);
347 (*purb)->pipe = pipe;
348 (*purb)->number_of_packets = nr_of_packs();
349 (*purb)->context = subs;
350 (*purb)->interval = 1;
351 (*purb)->complete = i_usx2y_usbpcm_subs_startup;
359 static int snd_usx2y_usbpcm_hw_free(struct snd_pcm_substream *substream)
361 struct snd_pcm_runtime *runtime = substream->runtime;
362 struct snd_usx2y_substream *subs = runtime->private_data;
363 struct snd_usx2y_substream *cap_subs;
364 struct snd_usx2y_substream *playback_subs;
365 struct snd_usx2y_substream *cap_subs2;
367 mutex_lock(&subs->usx2y->pcm_mutex);
368 snd_printdd("%s(%p)\n", __func__, substream);
370 cap_subs2 = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE + 2];
371 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
372 cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
373 atomic_set(&subs->state, STATE_STOPPED);
374 usx2y_usbpcm_urbs_release(subs);
375 if (!cap_subs->pcm_substream ||
376 !cap_subs->pcm_substream->runtime ||
377 cap_subs->pcm_substream->runtime->state < SNDRV_PCM_STATE_PREPARED) {
378 atomic_set(&cap_subs->state, STATE_STOPPED);
380 atomic_set(&cap_subs2->state, STATE_STOPPED);
381 usx2y_usbpcm_urbs_release(cap_subs);
383 usx2y_usbpcm_urbs_release(cap_subs2);
386 playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
387 if (atomic_read(&playback_subs->state) < STATE_PREPARED) {
388 atomic_set(&subs->state, STATE_STOPPED);
390 atomic_set(&cap_subs2->state, STATE_STOPPED);
391 usx2y_usbpcm_urbs_release(subs);
393 usx2y_usbpcm_urbs_release(cap_subs2);
396 mutex_unlock(&subs->usx2y->pcm_mutex);
400 static void usx2y_usbpcm_subs_startup(struct snd_usx2y_substream *subs)
402 struct usx2ydev *usx2y = subs->usx2y;
404 usx2y->prepare_subs = subs;
405 subs->urb[0]->start_frame = -1;
406 smp_wmb(); // Make sure above modifications are seen by i_usx2y_subs_startup()
407 usx2y_urbs_set_complete(usx2y, i_usx2y_usbpcm_subs_startup);
410 static int usx2y_usbpcm_urbs_start(struct snd_usx2y_substream *subs)
412 int p, u, err, stream = subs->pcm_substream->stream;
413 struct usx2ydev *usx2y = subs->usx2y;
417 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
418 usx2y->hwdep_pcm_shm->captured_iso_head = -1;
419 usx2y->hwdep_pcm_shm->captured_iso_frames = 0;
422 for (p = 0; 3 >= (stream + p); p += 2) {
423 struct snd_usx2y_substream *subs = usx2y->subs[stream + p];
425 err = usx2y_usbpcm_urbs_allocate(subs);
428 subs->completed_urb = NULL;
432 for (p = 0; p < 4; p++) {
433 struct snd_usx2y_substream *subs = usx2y->subs[p];
435 if (subs && atomic_read(&subs->state) >= STATE_PREPARED)
440 usx2y_usbpcm_subs_startup(subs);
441 for (u = 0; u < NRURBS; u++) {
442 for (p = 0; 3 >= (stream + p); p += 2) {
443 struct snd_usx2y_substream *subs = usx2y->subs[stream + p];
448 if (usb_pipein(urb->pipe)) {
450 atomic_set(&subs->state, STATE_STARTING3);
451 urb->dev = usx2y->dev;
452 for (pack = 0; pack < nr_of_packs(); pack++) {
453 urb->iso_frame_desc[pack].offset = subs->maxpacksize * (pack + u * nr_of_packs());
454 urb->iso_frame_desc[pack].length = subs->maxpacksize;
456 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
457 err = usb_submit_urb(urb, GFP_KERNEL);
459 snd_printk(KERN_ERR "cannot usb_submit_urb() for urb %d, err = %d\n", u, err);
463 snd_printdd("%i\n", urb->start_frame);
465 usx2y->wait_iso_frame = urb->start_frame;
467 urb->transfer_flags = 0;
469 atomic_set(&subs->state, STATE_STARTING1);
475 wait_event(usx2y->prepare_wait_queue, !usx2y->prepare_subs);
476 if (atomic_read(&subs->state) != STATE_PREPARED)
481 usx2y_subs_startup_finish(usx2y); // Call it now
482 usx2y_clients_stop(usx2y); // something is completely wrong > stop everything
487 #define USX2Y_HWDEP_PCM_PAGES \
488 PAGE_ALIGN(sizeof(struct snd_usx2y_hwdep_pcm_shm))
493 * set format and initialize urbs
495 static int snd_usx2y_usbpcm_prepare(struct snd_pcm_substream *substream)
497 struct snd_pcm_runtime *runtime = substream->runtime;
498 struct snd_usx2y_substream *subs = runtime->private_data;
499 struct usx2ydev *usx2y = subs->usx2y;
500 struct snd_usx2y_substream *capsubs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
503 snd_printdd("snd_usx2y_pcm_prepare(%p)\n", substream);
505 mutex_lock(&usx2y->pcm_mutex);
507 if (!usx2y->hwdep_pcm_shm) {
508 usx2y->hwdep_pcm_shm = alloc_pages_exact(USX2Y_HWDEP_PCM_PAGES,
510 if (!usx2y->hwdep_pcm_shm) {
512 goto up_prepare_mutex;
514 memset(usx2y->hwdep_pcm_shm, 0, USX2Y_HWDEP_PCM_PAGES);
517 usx2y_subs_prepare(subs);
518 // Start hardware streams
519 // SyncStream first....
520 if (atomic_read(&capsubs->state) < STATE_PREPARED) {
521 if (usx2y->format != runtime->format) {
522 err = usx2y_format_set(usx2y, runtime->format);
524 goto up_prepare_mutex;
526 if (usx2y->rate != runtime->rate) {
527 err = usx2y_rate_set(usx2y, runtime->rate);
529 goto up_prepare_mutex;
531 snd_printdd("starting capture pipe for %s\n", subs == capsubs ?
532 "self" : "playpipe");
533 err = usx2y_usbpcm_urbs_start(capsubs);
535 goto up_prepare_mutex;
538 if (subs != capsubs) {
539 usx2y->hwdep_pcm_shm->playback_iso_start = -1;
540 if (atomic_read(&subs->state) < STATE_PREPARED) {
541 while (usx2y_iso_frames_per_buffer(runtime, usx2y) >
542 usx2y->hwdep_pcm_shm->captured_iso_frames) {
543 snd_printdd("Wait: iso_frames_per_buffer=%i,captured_iso_frames=%i\n",
544 usx2y_iso_frames_per_buffer(runtime, usx2y),
545 usx2y->hwdep_pcm_shm->captured_iso_frames);
546 if (msleep_interruptible(10)) {
548 goto up_prepare_mutex;
551 err = usx2y_usbpcm_urbs_start(subs);
553 goto up_prepare_mutex;
555 snd_printdd("Ready: iso_frames_per_buffer=%i,captured_iso_frames=%i\n",
556 usx2y_iso_frames_per_buffer(runtime, usx2y),
557 usx2y->hwdep_pcm_shm->captured_iso_frames);
559 usx2y->hwdep_pcm_shm->capture_iso_start = -1;
563 mutex_unlock(&usx2y->pcm_mutex);
567 static const struct snd_pcm_hardware snd_usx2y_4c = {
568 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
569 SNDRV_PCM_INFO_BLOCK_TRANSFER |
570 SNDRV_PCM_INFO_MMAP_VALID),
571 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
572 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
577 .buffer_bytes_max = (2*128*1024),
578 .period_bytes_min = 64,
579 .period_bytes_max = (128*1024),
585 static int snd_usx2y_usbpcm_open(struct snd_pcm_substream *substream)
587 struct snd_usx2y_substream *subs =
588 ((struct snd_usx2y_substream **)
589 snd_pcm_substream_chip(substream))[substream->stream];
590 struct snd_pcm_runtime *runtime = substream->runtime;
592 if (!(subs->usx2y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS))
595 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
596 runtime->hw = snd_usx2y_2c;
598 runtime->hw = (subs->usx2y->subs[3] ? snd_usx2y_4c : snd_usx2y_2c);
599 runtime->private_data = subs;
600 subs->pcm_substream = substream;
601 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
605 static int snd_usx2y_usbpcm_close(struct snd_pcm_substream *substream)
607 struct snd_pcm_runtime *runtime = substream->runtime;
608 struct snd_usx2y_substream *subs = runtime->private_data;
610 subs->pcm_substream = NULL;
614 static const struct snd_pcm_ops snd_usx2y_usbpcm_ops = {
615 .open = snd_usx2y_usbpcm_open,
616 .close = snd_usx2y_usbpcm_close,
617 .hw_params = snd_usx2y_pcm_hw_params,
618 .hw_free = snd_usx2y_usbpcm_hw_free,
619 .prepare = snd_usx2y_usbpcm_prepare,
620 .trigger = snd_usx2y_pcm_trigger,
621 .pointer = snd_usx2y_pcm_pointer,
624 static int usx2y_pcms_busy_check(struct snd_card *card)
626 struct usx2ydev *dev = usx2y(card);
627 struct snd_usx2y_substream *subs;
630 for (i = 0; i < dev->pcm_devs * 2; i++) {
632 if (subs && subs->pcm_substream &&
633 SUBSTREAM_BUSY(subs->pcm_substream))
639 static int snd_usx2y_hwdep_pcm_open(struct snd_hwdep *hw, struct file *file)
641 struct snd_card *card = hw->card;
644 mutex_lock(&usx2y(card)->pcm_mutex);
645 err = usx2y_pcms_busy_check(card);
647 usx2y(card)->chip_status |= USX2Y_STAT_CHIP_MMAP_PCM_URBS;
648 mutex_unlock(&usx2y(card)->pcm_mutex);
652 static int snd_usx2y_hwdep_pcm_release(struct snd_hwdep *hw, struct file *file)
654 struct snd_card *card = hw->card;
657 mutex_lock(&usx2y(card)->pcm_mutex);
658 err = usx2y_pcms_busy_check(card);
660 usx2y(hw->card)->chip_status &= ~USX2Y_STAT_CHIP_MMAP_PCM_URBS;
661 mutex_unlock(&usx2y(card)->pcm_mutex);
665 static void snd_usx2y_hwdep_pcm_vm_open(struct vm_area_struct *area)
669 static void snd_usx2y_hwdep_pcm_vm_close(struct vm_area_struct *area)
673 static vm_fault_t snd_usx2y_hwdep_pcm_vm_fault(struct vm_fault *vmf)
675 unsigned long offset;
678 offset = vmf->pgoff << PAGE_SHIFT;
679 vaddr = (char *)((struct usx2ydev *)vmf->vma->vm_private_data)->hwdep_pcm_shm + offset;
680 vmf->page = virt_to_page(vaddr);
685 static const struct vm_operations_struct snd_usx2y_hwdep_pcm_vm_ops = {
686 .open = snd_usx2y_hwdep_pcm_vm_open,
687 .close = snd_usx2y_hwdep_pcm_vm_close,
688 .fault = snd_usx2y_hwdep_pcm_vm_fault,
691 static int snd_usx2y_hwdep_pcm_mmap(struct snd_hwdep *hw, struct file *filp, struct vm_area_struct *area)
693 unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
694 struct usx2ydev *usx2y = hw->private_data;
696 if (!(usx2y->chip_status & USX2Y_STAT_CHIP_INIT))
699 /* if userspace tries to mmap beyond end of our buffer, fail */
700 if (size > USX2Y_HWDEP_PCM_PAGES) {
701 snd_printd("%lu > %lu\n", size, (unsigned long)USX2Y_HWDEP_PCM_PAGES);
705 if (!usx2y->hwdep_pcm_shm)
708 area->vm_ops = &snd_usx2y_hwdep_pcm_vm_ops;
709 vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP);
710 area->vm_private_data = hw->private_data;
714 static void snd_usx2y_hwdep_pcm_private_free(struct snd_hwdep *hwdep)
716 struct usx2ydev *usx2y = hwdep->private_data;
718 if (usx2y->hwdep_pcm_shm)
719 free_pages_exact(usx2y->hwdep_pcm_shm, USX2Y_HWDEP_PCM_PAGES);
722 int usx2y_hwdep_pcm_new(struct snd_card *card)
725 struct snd_hwdep *hw;
727 struct usb_device *dev = usx2y(card)->dev;
729 if (nr_of_packs() != 1)
732 err = snd_hwdep_new(card, SND_USX2Y_USBPCM_ID, 1, &hw);
736 hw->iface = SNDRV_HWDEP_IFACE_USX2Y_PCM;
737 hw->private_data = usx2y(card);
738 hw->private_free = snd_usx2y_hwdep_pcm_private_free;
739 hw->ops.open = snd_usx2y_hwdep_pcm_open;
740 hw->ops.release = snd_usx2y_hwdep_pcm_release;
741 hw->ops.mmap = snd_usx2y_hwdep_pcm_mmap;
743 sprintf(hw->name, "/dev/bus/usb/%03d/%03d/hwdeppcm", dev->bus->busnum, dev->devnum);
745 err = snd_pcm_new(card, NAME_ALLCAPS" hwdep Audio", 2, 1, 1, &pcm);
749 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usx2y_usbpcm_ops);
750 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usx2y_usbpcm_ops);
752 pcm->private_data = usx2y(card)->subs;
755 sprintf(pcm->name, NAME_ALLCAPS" hwdep Audio");
756 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
757 SNDRV_DMA_TYPE_CONTINUOUS,
760 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
761 SNDRV_DMA_TYPE_CONTINUOUS,
770 int usx2y_hwdep_pcm_new(struct snd_card *card)