18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci */ 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci/* USX2Y "rawusb" aka hwdep_pcm implementation 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci Its usb's unableness to atomically handle power of 2 period sized data chuncs 88c2ecf20Sopenharmony_ci at standard samplerates, 98c2ecf20Sopenharmony_ci what led to this part of the usx2y module: 108c2ecf20Sopenharmony_ci It provides the alsa kernel half of the usx2y-alsa-jack driver pair. 118c2ecf20Sopenharmony_ci The pair uses a hardware dependent alsa-device for mmaped pcm transport. 128c2ecf20Sopenharmony_ci Advantage achieved: 138c2ecf20Sopenharmony_ci The usb_hc moves pcm data from/into memory via DMA. 148c2ecf20Sopenharmony_ci That memory is mmaped by jack's usx2y driver. 158c2ecf20Sopenharmony_ci Jack's usx2y driver is the first/last to read/write pcm data. 168c2ecf20Sopenharmony_ci Read/write is a combination of power of 2 period shaping and 178c2ecf20Sopenharmony_ci float/int conversation. 188c2ecf20Sopenharmony_ci Compared to mainline alsa/jack we leave out power of 2 period shaping inside 198c2ecf20Sopenharmony_ci snd-usb-usx2y which needs memcpy() and additional buffers. 208c2ecf20Sopenharmony_ci As a side effect possible unwanted pcm-data coruption resulting of 218c2ecf20Sopenharmony_ci standard alsa's snd-usb-usx2y period shaping scheme falls away. 228c2ecf20Sopenharmony_ci Result is sane jack operation at buffering schemes down to 128frames, 238c2ecf20Sopenharmony_ci 2 periods. 248c2ecf20Sopenharmony_ci plain usx2y alsa mode is able to achieve 64frames, 4periods, but only at the 258c2ecf20Sopenharmony_ci cost of easier triggered i.e. aeolus xruns (128 or 256frames, 268c2ecf20Sopenharmony_ci 2periods works but is useless cause of crackling). 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci This is a first "proof of concept" implementation. 298c2ecf20Sopenharmony_ci Later, functionalities should migrate to more appropriate places: 308c2ecf20Sopenharmony_ci Userland: 318c2ecf20Sopenharmony_ci - The jackd could mmap its float-pcm buffers directly from alsa-lib. 328c2ecf20Sopenharmony_ci - alsa-lib could provide power of 2 period sized shaping combined with int/float 338c2ecf20Sopenharmony_ci conversation. 348c2ecf20Sopenharmony_ci Currently the usx2y jack driver provides above 2 services. 358c2ecf20Sopenharmony_ci Kernel: 368c2ecf20Sopenharmony_ci - rawusb dma pcm buffer transport should go to snd-usb-lib, so also snd-usb-audio 378c2ecf20Sopenharmony_ci devices can use it. 388c2ecf20Sopenharmony_ci Currently rawusb dma pcm buffer transport (this file) is only available to snd-usb-usx2y. 398c2ecf20Sopenharmony_ci*/ 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#include <linux/delay.h> 428c2ecf20Sopenharmony_ci#include <linux/gfp.h> 438c2ecf20Sopenharmony_ci#include "usbusx2yaudio.c" 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#if defined(USX2Y_NRPACKS_VARIABLE) || USX2Y_NRPACKS == 1 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#include <sound/hwdep.h> 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic int usx2y_usbpcm_urb_capt_retire(struct snd_usx2y_substream *subs) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci struct urb *urb = subs->completed_urb; 538c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 548c2ecf20Sopenharmony_ci int i, lens = 0, hwptr_done = subs->hwptr_done; 558c2ecf20Sopenharmony_ci struct usx2ydev *usx2y = subs->usx2y; 568c2ecf20Sopenharmony_ci if (0 > usx2y->hwdep_pcm_shm->capture_iso_start) { //FIXME 578c2ecf20Sopenharmony_ci int head = usx2y->hwdep_pcm_shm->captured_iso_head + 1; 588c2ecf20Sopenharmony_ci if (head >= ARRAY_SIZE(usx2y->hwdep_pcm_shm->captured_iso)) 598c2ecf20Sopenharmony_ci head = 0; 608c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm->capture_iso_start = head; 618c2ecf20Sopenharmony_ci snd_printdd("cap start %i\n", head); 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci for (i = 0; i < nr_of_packs(); i++) { 648c2ecf20Sopenharmony_ci if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */ 658c2ecf20Sopenharmony_ci snd_printk(KERN_ERR "active frame status %i. Most probably some hardware problem.\n", urb->iso_frame_desc[i].status); 668c2ecf20Sopenharmony_ci return urb->iso_frame_desc[i].status; 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci lens += urb->iso_frame_desc[i].actual_length / usx2y->stride; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci if ((hwptr_done += lens) >= runtime->buffer_size) 718c2ecf20Sopenharmony_ci hwptr_done -= runtime->buffer_size; 728c2ecf20Sopenharmony_ci subs->hwptr_done = hwptr_done; 738c2ecf20Sopenharmony_ci subs->transfer_done += lens; 748c2ecf20Sopenharmony_ci /* update the pointer, call callback if necessary */ 758c2ecf20Sopenharmony_ci if (subs->transfer_done >= runtime->period_size) { 768c2ecf20Sopenharmony_ci subs->transfer_done -= runtime->period_size; 778c2ecf20Sopenharmony_ci snd_pcm_period_elapsed(subs->pcm_substream); 788c2ecf20Sopenharmony_ci } 798c2ecf20Sopenharmony_ci return 0; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic inline int usx2y_iso_frames_per_buffer(struct snd_pcm_runtime *runtime, 838c2ecf20Sopenharmony_ci struct usx2ydev * usx2y) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci return (runtime->buffer_size * 1000) / usx2y->rate + 1; //FIXME: so far only correct period_size == 2^x ? 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci/* 898c2ecf20Sopenharmony_ci * prepare urb for playback data pipe 908c2ecf20Sopenharmony_ci * 918c2ecf20Sopenharmony_ci * we copy the data directly from the pcm buffer. 928c2ecf20Sopenharmony_ci * the current position to be copied is held in hwptr field. 938c2ecf20Sopenharmony_ci * since a urb can handle only a single linear buffer, if the total 948c2ecf20Sopenharmony_ci * transferred area overflows the buffer boundary, we cannot send 958c2ecf20Sopenharmony_ci * it directly from the buffer. thus the data is once copied to 968c2ecf20Sopenharmony_ci * a temporary buffer and urb points to that. 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_cistatic int usx2y_hwdep_urb_play_prepare(struct snd_usx2y_substream *subs, 998c2ecf20Sopenharmony_ci struct urb *urb) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci int count, counts, pack; 1028c2ecf20Sopenharmony_ci struct usx2ydev *usx2y = subs->usx2y; 1038c2ecf20Sopenharmony_ci struct snd_usx2y_hwdep_pcm_shm *shm = usx2y->hwdep_pcm_shm; 1048c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci if (0 > shm->playback_iso_start) { 1078c2ecf20Sopenharmony_ci shm->playback_iso_start = shm->captured_iso_head - 1088c2ecf20Sopenharmony_ci usx2y_iso_frames_per_buffer(runtime, usx2y); 1098c2ecf20Sopenharmony_ci if (0 > shm->playback_iso_start) 1108c2ecf20Sopenharmony_ci shm->playback_iso_start += ARRAY_SIZE(shm->captured_iso); 1118c2ecf20Sopenharmony_ci shm->playback_iso_head = shm->playback_iso_start; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci count = 0; 1158c2ecf20Sopenharmony_ci for (pack = 0; pack < nr_of_packs(); pack++) { 1168c2ecf20Sopenharmony_ci /* calculate the size of a packet */ 1178c2ecf20Sopenharmony_ci counts = shm->captured_iso[shm->playback_iso_head].length / usx2y->stride; 1188c2ecf20Sopenharmony_ci if (counts < 43 || counts > 50) { 1198c2ecf20Sopenharmony_ci snd_printk(KERN_ERR "should not be here with counts=%i\n", counts); 1208c2ecf20Sopenharmony_ci return -EPIPE; 1218c2ecf20Sopenharmony_ci } 1228c2ecf20Sopenharmony_ci /* set up descriptor */ 1238c2ecf20Sopenharmony_ci urb->iso_frame_desc[pack].offset = shm->captured_iso[shm->playback_iso_head].offset; 1248c2ecf20Sopenharmony_ci urb->iso_frame_desc[pack].length = shm->captured_iso[shm->playback_iso_head].length; 1258c2ecf20Sopenharmony_ci if (atomic_read(&subs->state) != STATE_RUNNING) 1268c2ecf20Sopenharmony_ci memset((char *)urb->transfer_buffer + urb->iso_frame_desc[pack].offset, 0, 1278c2ecf20Sopenharmony_ci urb->iso_frame_desc[pack].length); 1288c2ecf20Sopenharmony_ci if (++shm->playback_iso_head >= ARRAY_SIZE(shm->captured_iso)) 1298c2ecf20Sopenharmony_ci shm->playback_iso_head = 0; 1308c2ecf20Sopenharmony_ci count += counts; 1318c2ecf20Sopenharmony_ci } 1328c2ecf20Sopenharmony_ci urb->transfer_buffer_length = count * usx2y->stride; 1338c2ecf20Sopenharmony_ci return 0; 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistatic inline void usx2y_usbpcm_urb_capt_iso_advance(struct snd_usx2y_substream *subs, 1388c2ecf20Sopenharmony_ci struct urb *urb) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci int pack; 1418c2ecf20Sopenharmony_ci for (pack = 0; pack < nr_of_packs(); ++pack) { 1428c2ecf20Sopenharmony_ci struct usb_iso_packet_descriptor *desc = urb->iso_frame_desc + pack; 1438c2ecf20Sopenharmony_ci if (NULL != subs) { 1448c2ecf20Sopenharmony_ci struct snd_usx2y_hwdep_pcm_shm *shm = subs->usx2y->hwdep_pcm_shm; 1458c2ecf20Sopenharmony_ci int head = shm->captured_iso_head + 1; 1468c2ecf20Sopenharmony_ci if (head >= ARRAY_SIZE(shm->captured_iso)) 1478c2ecf20Sopenharmony_ci head = 0; 1488c2ecf20Sopenharmony_ci shm->captured_iso[head].frame = urb->start_frame + pack; 1498c2ecf20Sopenharmony_ci shm->captured_iso[head].offset = desc->offset; 1508c2ecf20Sopenharmony_ci shm->captured_iso[head].length = desc->actual_length; 1518c2ecf20Sopenharmony_ci shm->captured_iso_head = head; 1528c2ecf20Sopenharmony_ci shm->captured_iso_frames++; 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci if ((desc->offset += desc->length * NRURBS*nr_of_packs()) + 1558c2ecf20Sopenharmony_ci desc->length >= SSS) 1568c2ecf20Sopenharmony_ci desc->offset -= (SSS - desc->length); 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_cistatic inline int usx2y_usbpcm_usbframe_complete(struct snd_usx2y_substream *capsubs, 1618c2ecf20Sopenharmony_ci struct snd_usx2y_substream *capsubs2, 1628c2ecf20Sopenharmony_ci struct snd_usx2y_substream *playbacksubs, 1638c2ecf20Sopenharmony_ci int frame) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci int err, state; 1668c2ecf20Sopenharmony_ci struct urb *urb = playbacksubs->completed_urb; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci state = atomic_read(&playbacksubs->state); 1698c2ecf20Sopenharmony_ci if (NULL != urb) { 1708c2ecf20Sopenharmony_ci if (state == STATE_RUNNING) 1718c2ecf20Sopenharmony_ci usx2y_urb_play_retire(playbacksubs, urb); 1728c2ecf20Sopenharmony_ci else if (state >= STATE_PRERUNNING) 1738c2ecf20Sopenharmony_ci atomic_inc(&playbacksubs->state); 1748c2ecf20Sopenharmony_ci } else { 1758c2ecf20Sopenharmony_ci switch (state) { 1768c2ecf20Sopenharmony_ci case STATE_STARTING1: 1778c2ecf20Sopenharmony_ci urb = playbacksubs->urb[0]; 1788c2ecf20Sopenharmony_ci atomic_inc(&playbacksubs->state); 1798c2ecf20Sopenharmony_ci break; 1808c2ecf20Sopenharmony_ci case STATE_STARTING2: 1818c2ecf20Sopenharmony_ci urb = playbacksubs->urb[1]; 1828c2ecf20Sopenharmony_ci atomic_inc(&playbacksubs->state); 1838c2ecf20Sopenharmony_ci break; 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci } 1868c2ecf20Sopenharmony_ci if (urb) { 1878c2ecf20Sopenharmony_ci if ((err = usx2y_hwdep_urb_play_prepare(playbacksubs, urb)) || 1888c2ecf20Sopenharmony_ci (err = usx2y_urb_submit(playbacksubs, urb, frame))) { 1898c2ecf20Sopenharmony_ci return err; 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci } 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci playbacksubs->completed_urb = NULL; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci state = atomic_read(&capsubs->state); 1968c2ecf20Sopenharmony_ci if (state >= STATE_PREPARED) { 1978c2ecf20Sopenharmony_ci if (state == STATE_RUNNING) { 1988c2ecf20Sopenharmony_ci if ((err = usx2y_usbpcm_urb_capt_retire(capsubs))) 1998c2ecf20Sopenharmony_ci return err; 2008c2ecf20Sopenharmony_ci } else if (state >= STATE_PRERUNNING) 2018c2ecf20Sopenharmony_ci atomic_inc(&capsubs->state); 2028c2ecf20Sopenharmony_ci usx2y_usbpcm_urb_capt_iso_advance(capsubs, capsubs->completed_urb); 2038c2ecf20Sopenharmony_ci if (NULL != capsubs2) 2048c2ecf20Sopenharmony_ci usx2y_usbpcm_urb_capt_iso_advance(NULL, capsubs2->completed_urb); 2058c2ecf20Sopenharmony_ci if ((err = usx2y_urb_submit(capsubs, capsubs->completed_urb, frame))) 2068c2ecf20Sopenharmony_ci return err; 2078c2ecf20Sopenharmony_ci if (NULL != capsubs2) 2088c2ecf20Sopenharmony_ci if ((err = usx2y_urb_submit(capsubs2, capsubs2->completed_urb, frame))) 2098c2ecf20Sopenharmony_ci return err; 2108c2ecf20Sopenharmony_ci } 2118c2ecf20Sopenharmony_ci capsubs->completed_urb = NULL; 2128c2ecf20Sopenharmony_ci if (NULL != capsubs2) 2138c2ecf20Sopenharmony_ci capsubs2->completed_urb = NULL; 2148c2ecf20Sopenharmony_ci return 0; 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cistatic void i_usx2y_usbpcm_urb_complete(struct urb *urb) 2198c2ecf20Sopenharmony_ci{ 2208c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = urb->context; 2218c2ecf20Sopenharmony_ci struct usx2ydev *usx2y = subs->usx2y; 2228c2ecf20Sopenharmony_ci struct snd_usx2y_substream *capsubs, *capsubs2, *playbacksubs; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci if (unlikely(atomic_read(&subs->state) < STATE_PREPARED)) { 2258c2ecf20Sopenharmony_ci snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", 2268c2ecf20Sopenharmony_ci usb_get_current_frame_number(usx2y->dev), 2278c2ecf20Sopenharmony_ci subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", 2288c2ecf20Sopenharmony_ci urb->status, urb->start_frame); 2298c2ecf20Sopenharmony_ci return; 2308c2ecf20Sopenharmony_ci } 2318c2ecf20Sopenharmony_ci if (unlikely(urb->status)) { 2328c2ecf20Sopenharmony_ci usx2y_error_urb_status(usx2y, subs, urb); 2338c2ecf20Sopenharmony_ci return; 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci subs->completed_urb = urb; 2378c2ecf20Sopenharmony_ci capsubs = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 2388c2ecf20Sopenharmony_ci capsubs2 = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE + 2]; 2398c2ecf20Sopenharmony_ci playbacksubs = usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 2408c2ecf20Sopenharmony_ci if (capsubs->completed_urb && atomic_read(&capsubs->state) >= STATE_PREPARED && 2418c2ecf20Sopenharmony_ci (NULL == capsubs2 || capsubs2->completed_urb) && 2428c2ecf20Sopenharmony_ci (playbacksubs->completed_urb || atomic_read(&playbacksubs->state) < STATE_PREPARED)) { 2438c2ecf20Sopenharmony_ci if (!usx2y_usbpcm_usbframe_complete(capsubs, capsubs2, playbacksubs, urb->start_frame)) 2448c2ecf20Sopenharmony_ci usx2y->wait_iso_frame += nr_of_packs(); 2458c2ecf20Sopenharmony_ci else { 2468c2ecf20Sopenharmony_ci snd_printdd("\n"); 2478c2ecf20Sopenharmony_ci usx2y_clients_stop(usx2y); 2488c2ecf20Sopenharmony_ci } 2498c2ecf20Sopenharmony_ci } 2508c2ecf20Sopenharmony_ci} 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic void usx2y_hwdep_urb_release(struct urb **urb) 2548c2ecf20Sopenharmony_ci{ 2558c2ecf20Sopenharmony_ci usb_kill_urb(*urb); 2568c2ecf20Sopenharmony_ci usb_free_urb(*urb); 2578c2ecf20Sopenharmony_ci *urb = NULL; 2588c2ecf20Sopenharmony_ci} 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci/* 2618c2ecf20Sopenharmony_ci * release a substream 2628c2ecf20Sopenharmony_ci */ 2638c2ecf20Sopenharmony_cistatic void usx2y_usbpcm_urbs_release(struct snd_usx2y_substream *subs) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci int i; 2668c2ecf20Sopenharmony_ci snd_printdd("snd_usx2y_urbs_release() %i\n", subs->endpoint); 2678c2ecf20Sopenharmony_ci for (i = 0; i < NRURBS; i++) 2688c2ecf20Sopenharmony_ci usx2y_hwdep_urb_release(subs->urb + i); 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_cistatic void usx2y_usbpcm_subs_startup_finish(struct usx2ydev * usx2y) 2728c2ecf20Sopenharmony_ci{ 2738c2ecf20Sopenharmony_ci usx2y_urbs_set_complete(usx2y, i_usx2y_usbpcm_urb_complete); 2748c2ecf20Sopenharmony_ci usx2y->prepare_subs = NULL; 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_cistatic void i_usx2y_usbpcm_subs_startup(struct urb *urb) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = urb->context; 2808c2ecf20Sopenharmony_ci struct usx2ydev *usx2y = subs->usx2y; 2818c2ecf20Sopenharmony_ci struct snd_usx2y_substream *prepare_subs = usx2y->prepare_subs; 2828c2ecf20Sopenharmony_ci if (NULL != prepare_subs && 2838c2ecf20Sopenharmony_ci urb->start_frame == prepare_subs->urb[0]->start_frame) { 2848c2ecf20Sopenharmony_ci atomic_inc(&prepare_subs->state); 2858c2ecf20Sopenharmony_ci if (prepare_subs == usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]) { 2868c2ecf20Sopenharmony_ci struct snd_usx2y_substream *cap_subs2 = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE + 2]; 2878c2ecf20Sopenharmony_ci if (cap_subs2 != NULL) 2888c2ecf20Sopenharmony_ci atomic_inc(&cap_subs2->state); 2898c2ecf20Sopenharmony_ci } 2908c2ecf20Sopenharmony_ci usx2y_usbpcm_subs_startup_finish(usx2y); 2918c2ecf20Sopenharmony_ci wake_up(&usx2y->prepare_wait_queue); 2928c2ecf20Sopenharmony_ci } 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci i_usx2y_usbpcm_urb_complete(urb); 2958c2ecf20Sopenharmony_ci} 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci/* 2988c2ecf20Sopenharmony_ci * initialize a substream's urbs 2998c2ecf20Sopenharmony_ci */ 3008c2ecf20Sopenharmony_cistatic int usx2y_usbpcm_urbs_allocate(struct snd_usx2y_substream *subs) 3018c2ecf20Sopenharmony_ci{ 3028c2ecf20Sopenharmony_ci int i; 3038c2ecf20Sopenharmony_ci unsigned int pipe; 3048c2ecf20Sopenharmony_ci int is_playback = subs == subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 3058c2ecf20Sopenharmony_ci struct usb_device *dev = subs->usx2y->dev; 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) : 3088c2ecf20Sopenharmony_ci usb_rcvisocpipe(dev, subs->endpoint); 3098c2ecf20Sopenharmony_ci subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback); 3108c2ecf20Sopenharmony_ci if (!subs->maxpacksize) 3118c2ecf20Sopenharmony_ci return -EINVAL; 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci /* allocate and initialize data urbs */ 3148c2ecf20Sopenharmony_ci for (i = 0; i < NRURBS; i++) { 3158c2ecf20Sopenharmony_ci struct urb **purb = subs->urb + i; 3168c2ecf20Sopenharmony_ci if (*purb) { 3178c2ecf20Sopenharmony_ci usb_kill_urb(*purb); 3188c2ecf20Sopenharmony_ci continue; 3198c2ecf20Sopenharmony_ci } 3208c2ecf20Sopenharmony_ci *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL); 3218c2ecf20Sopenharmony_ci if (NULL == *purb) { 3228c2ecf20Sopenharmony_ci usx2y_usbpcm_urbs_release(subs); 3238c2ecf20Sopenharmony_ci return -ENOMEM; 3248c2ecf20Sopenharmony_ci } 3258c2ecf20Sopenharmony_ci (*purb)->transfer_buffer = is_playback ? 3268c2ecf20Sopenharmony_ci subs->usx2y->hwdep_pcm_shm->playback : ( 3278c2ecf20Sopenharmony_ci subs->endpoint == 0x8 ? 3288c2ecf20Sopenharmony_ci subs->usx2y->hwdep_pcm_shm->capture0x8 : 3298c2ecf20Sopenharmony_ci subs->usx2y->hwdep_pcm_shm->capture0xA); 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci (*purb)->dev = dev; 3328c2ecf20Sopenharmony_ci (*purb)->pipe = pipe; 3338c2ecf20Sopenharmony_ci (*purb)->number_of_packets = nr_of_packs(); 3348c2ecf20Sopenharmony_ci (*purb)->context = subs; 3358c2ecf20Sopenharmony_ci (*purb)->interval = 1; 3368c2ecf20Sopenharmony_ci (*purb)->complete = i_usx2y_usbpcm_subs_startup; 3378c2ecf20Sopenharmony_ci } 3388c2ecf20Sopenharmony_ci return 0; 3398c2ecf20Sopenharmony_ci} 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci/* 3428c2ecf20Sopenharmony_ci * free the buffer 3438c2ecf20Sopenharmony_ci */ 3448c2ecf20Sopenharmony_cistatic int snd_usx2y_usbpcm_hw_free(struct snd_pcm_substream *substream) 3458c2ecf20Sopenharmony_ci{ 3468c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = substream->runtime; 3478c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = runtime->private_data, 3488c2ecf20Sopenharmony_ci *cap_subs2 = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE + 2]; 3498c2ecf20Sopenharmony_ci mutex_lock(&subs->usx2y->pcm_mutex); 3508c2ecf20Sopenharmony_ci snd_printdd("snd_usx2y_usbpcm_hw_free(%p)\n", substream); 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) { 3538c2ecf20Sopenharmony_ci struct snd_usx2y_substream *cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 3548c2ecf20Sopenharmony_ci atomic_set(&subs->state, STATE_STOPPED); 3558c2ecf20Sopenharmony_ci usx2y_usbpcm_urbs_release(subs); 3568c2ecf20Sopenharmony_ci if (!cap_subs->pcm_substream || 3578c2ecf20Sopenharmony_ci !cap_subs->pcm_substream->runtime || 3588c2ecf20Sopenharmony_ci !cap_subs->pcm_substream->runtime->status || 3598c2ecf20Sopenharmony_ci cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) { 3608c2ecf20Sopenharmony_ci atomic_set(&cap_subs->state, STATE_STOPPED); 3618c2ecf20Sopenharmony_ci if (NULL != cap_subs2) 3628c2ecf20Sopenharmony_ci atomic_set(&cap_subs2->state, STATE_STOPPED); 3638c2ecf20Sopenharmony_ci usx2y_usbpcm_urbs_release(cap_subs); 3648c2ecf20Sopenharmony_ci if (NULL != cap_subs2) 3658c2ecf20Sopenharmony_ci usx2y_usbpcm_urbs_release(cap_subs2); 3668c2ecf20Sopenharmony_ci } 3678c2ecf20Sopenharmony_ci } else { 3688c2ecf20Sopenharmony_ci struct snd_usx2y_substream *playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 3698c2ecf20Sopenharmony_ci if (atomic_read(&playback_subs->state) < STATE_PREPARED) { 3708c2ecf20Sopenharmony_ci atomic_set(&subs->state, STATE_STOPPED); 3718c2ecf20Sopenharmony_ci if (NULL != cap_subs2) 3728c2ecf20Sopenharmony_ci atomic_set(&cap_subs2->state, STATE_STOPPED); 3738c2ecf20Sopenharmony_ci usx2y_usbpcm_urbs_release(subs); 3748c2ecf20Sopenharmony_ci if (NULL != cap_subs2) 3758c2ecf20Sopenharmony_ci usx2y_usbpcm_urbs_release(cap_subs2); 3768c2ecf20Sopenharmony_ci } 3778c2ecf20Sopenharmony_ci } 3788c2ecf20Sopenharmony_ci mutex_unlock(&subs->usx2y->pcm_mutex); 3798c2ecf20Sopenharmony_ci return 0; 3808c2ecf20Sopenharmony_ci} 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_cistatic void usx2y_usbpcm_subs_startup(struct snd_usx2y_substream *subs) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci struct usx2ydev * usx2y = subs->usx2y; 3858c2ecf20Sopenharmony_ci usx2y->prepare_subs = subs; 3868c2ecf20Sopenharmony_ci subs->urb[0]->start_frame = -1; 3878c2ecf20Sopenharmony_ci smp_wmb(); // Make sure above modifications are seen by i_usx2y_subs_startup() 3888c2ecf20Sopenharmony_ci usx2y_urbs_set_complete(usx2y, i_usx2y_usbpcm_subs_startup); 3898c2ecf20Sopenharmony_ci} 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_cistatic int usx2y_usbpcm_urbs_start(struct snd_usx2y_substream *subs) 3928c2ecf20Sopenharmony_ci{ 3938c2ecf20Sopenharmony_ci int p, u, err, 3948c2ecf20Sopenharmony_ci stream = subs->pcm_substream->stream; 3958c2ecf20Sopenharmony_ci struct usx2ydev *usx2y = subs->usx2y; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci if (SNDRV_PCM_STREAM_CAPTURE == stream) { 3988c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm->captured_iso_head = -1; 3998c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm->captured_iso_frames = 0; 4008c2ecf20Sopenharmony_ci } 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci for (p = 0; 3 >= (stream + p); p += 2) { 4038c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = usx2y->subs[stream + p]; 4048c2ecf20Sopenharmony_ci if (subs != NULL) { 4058c2ecf20Sopenharmony_ci if ((err = usx2y_usbpcm_urbs_allocate(subs)) < 0) 4068c2ecf20Sopenharmony_ci return err; 4078c2ecf20Sopenharmony_ci subs->completed_urb = NULL; 4088c2ecf20Sopenharmony_ci } 4098c2ecf20Sopenharmony_ci } 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci for (p = 0; p < 4; p++) { 4128c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = usx2y->subs[p]; 4138c2ecf20Sopenharmony_ci if (subs != NULL && atomic_read(&subs->state) >= STATE_PREPARED) 4148c2ecf20Sopenharmony_ci goto start; 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci start: 4188c2ecf20Sopenharmony_ci usx2y_usbpcm_subs_startup(subs); 4198c2ecf20Sopenharmony_ci for (u = 0; u < NRURBS; u++) { 4208c2ecf20Sopenharmony_ci for (p = 0; 3 >= (stream + p); p += 2) { 4218c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = usx2y->subs[stream + p]; 4228c2ecf20Sopenharmony_ci if (subs != NULL) { 4238c2ecf20Sopenharmony_ci struct urb *urb = subs->urb[u]; 4248c2ecf20Sopenharmony_ci if (usb_pipein(urb->pipe)) { 4258c2ecf20Sopenharmony_ci unsigned long pack; 4268c2ecf20Sopenharmony_ci if (0 == u) 4278c2ecf20Sopenharmony_ci atomic_set(&subs->state, STATE_STARTING3); 4288c2ecf20Sopenharmony_ci urb->dev = usx2y->dev; 4298c2ecf20Sopenharmony_ci for (pack = 0; pack < nr_of_packs(); pack++) { 4308c2ecf20Sopenharmony_ci urb->iso_frame_desc[pack].offset = subs->maxpacksize * (pack + u * nr_of_packs()); 4318c2ecf20Sopenharmony_ci urb->iso_frame_desc[pack].length = subs->maxpacksize; 4328c2ecf20Sopenharmony_ci } 4338c2ecf20Sopenharmony_ci urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); 4348c2ecf20Sopenharmony_ci if ((err = usb_submit_urb(urb, GFP_KERNEL)) < 0) { 4358c2ecf20Sopenharmony_ci snd_printk (KERN_ERR "cannot usb_submit_urb() for urb %d, err = %d\n", u, err); 4368c2ecf20Sopenharmony_ci err = -EPIPE; 4378c2ecf20Sopenharmony_ci goto cleanup; 4388c2ecf20Sopenharmony_ci } else { 4398c2ecf20Sopenharmony_ci snd_printdd("%i\n", urb->start_frame); 4408c2ecf20Sopenharmony_ci if (u == 0) 4418c2ecf20Sopenharmony_ci usx2y->wait_iso_frame = urb->start_frame; 4428c2ecf20Sopenharmony_ci } 4438c2ecf20Sopenharmony_ci urb->transfer_flags = 0; 4448c2ecf20Sopenharmony_ci } else { 4458c2ecf20Sopenharmony_ci atomic_set(&subs->state, STATE_STARTING1); 4468c2ecf20Sopenharmony_ci break; 4478c2ecf20Sopenharmony_ci } 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci } 4508c2ecf20Sopenharmony_ci } 4518c2ecf20Sopenharmony_ci err = 0; 4528c2ecf20Sopenharmony_ci wait_event(usx2y->prepare_wait_queue, NULL == usx2y->prepare_subs); 4538c2ecf20Sopenharmony_ci if (atomic_read(&subs->state) != STATE_PREPARED) 4548c2ecf20Sopenharmony_ci err = -EPIPE; 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci cleanup: 4578c2ecf20Sopenharmony_ci if (err) { 4588c2ecf20Sopenharmony_ci usx2y_subs_startup_finish(usx2y); // Call it now 4598c2ecf20Sopenharmony_ci usx2y_clients_stop(usx2y); // something is completely wroong > stop evrything 4608c2ecf20Sopenharmony_ci } 4618c2ecf20Sopenharmony_ci return err; 4628c2ecf20Sopenharmony_ci} 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci/* 4658c2ecf20Sopenharmony_ci * prepare callback 4668c2ecf20Sopenharmony_ci * 4678c2ecf20Sopenharmony_ci * set format and initialize urbs 4688c2ecf20Sopenharmony_ci */ 4698c2ecf20Sopenharmony_cistatic int snd_usx2y_usbpcm_prepare(struct snd_pcm_substream *substream) 4708c2ecf20Sopenharmony_ci{ 4718c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = substream->runtime; 4728c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = runtime->private_data; 4738c2ecf20Sopenharmony_ci struct usx2ydev *usx2y = subs->usx2y; 4748c2ecf20Sopenharmony_ci struct snd_usx2y_substream *capsubs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 4758c2ecf20Sopenharmony_ci int err = 0; 4768c2ecf20Sopenharmony_ci snd_printdd("snd_usx2y_pcm_prepare(%p)\n", substream); 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci if (NULL == usx2y->hwdep_pcm_shm) { 4798c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm = alloc_pages_exact(sizeof(struct snd_usx2y_hwdep_pcm_shm), 4808c2ecf20Sopenharmony_ci GFP_KERNEL); 4818c2ecf20Sopenharmony_ci if (!usx2y->hwdep_pcm_shm) 4828c2ecf20Sopenharmony_ci return -ENOMEM; 4838c2ecf20Sopenharmony_ci memset(usx2y->hwdep_pcm_shm, 0, sizeof(struct snd_usx2y_hwdep_pcm_shm)); 4848c2ecf20Sopenharmony_ci } 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci mutex_lock(&usx2y->pcm_mutex); 4878c2ecf20Sopenharmony_ci usx2y_subs_prepare(subs); 4888c2ecf20Sopenharmony_ci// Start hardware streams 4898c2ecf20Sopenharmony_ci// SyncStream first.... 4908c2ecf20Sopenharmony_ci if (atomic_read(&capsubs->state) < STATE_PREPARED) { 4918c2ecf20Sopenharmony_ci if (usx2y->format != runtime->format) 4928c2ecf20Sopenharmony_ci if ((err = usx2y_format_set(usx2y, runtime->format)) < 0) 4938c2ecf20Sopenharmony_ci goto up_prepare_mutex; 4948c2ecf20Sopenharmony_ci if (usx2y->rate != runtime->rate) 4958c2ecf20Sopenharmony_ci if ((err = usx2y_rate_set(usx2y, runtime->rate)) < 0) 4968c2ecf20Sopenharmony_ci goto up_prepare_mutex; 4978c2ecf20Sopenharmony_ci snd_printdd("starting capture pipe for %s\n", subs == capsubs ? 4988c2ecf20Sopenharmony_ci "self" : "playpipe"); 4998c2ecf20Sopenharmony_ci if (0 > (err = usx2y_usbpcm_urbs_start(capsubs))) 5008c2ecf20Sopenharmony_ci goto up_prepare_mutex; 5018c2ecf20Sopenharmony_ci } 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci if (subs != capsubs) { 5048c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm->playback_iso_start = -1; 5058c2ecf20Sopenharmony_ci if (atomic_read(&subs->state) < STATE_PREPARED) { 5068c2ecf20Sopenharmony_ci while (usx2y_iso_frames_per_buffer(runtime, usx2y) > 5078c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm->captured_iso_frames) { 5088c2ecf20Sopenharmony_ci snd_printdd("Wait: iso_frames_per_buffer=%i," 5098c2ecf20Sopenharmony_ci "captured_iso_frames=%i\n", 5108c2ecf20Sopenharmony_ci usx2y_iso_frames_per_buffer(runtime, usx2y), 5118c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm->captured_iso_frames); 5128c2ecf20Sopenharmony_ci if (msleep_interruptible(10)) { 5138c2ecf20Sopenharmony_ci err = -ERESTARTSYS; 5148c2ecf20Sopenharmony_ci goto up_prepare_mutex; 5158c2ecf20Sopenharmony_ci } 5168c2ecf20Sopenharmony_ci } 5178c2ecf20Sopenharmony_ci if (0 > (err = usx2y_usbpcm_urbs_start(subs))) 5188c2ecf20Sopenharmony_ci goto up_prepare_mutex; 5198c2ecf20Sopenharmony_ci } 5208c2ecf20Sopenharmony_ci snd_printdd("Ready: iso_frames_per_buffer=%i,captured_iso_frames=%i\n", 5218c2ecf20Sopenharmony_ci usx2y_iso_frames_per_buffer(runtime, usx2y), 5228c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm->captured_iso_frames); 5238c2ecf20Sopenharmony_ci } else 5248c2ecf20Sopenharmony_ci usx2y->hwdep_pcm_shm->capture_iso_start = -1; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci up_prepare_mutex: 5278c2ecf20Sopenharmony_ci mutex_unlock(&usx2y->pcm_mutex); 5288c2ecf20Sopenharmony_ci return err; 5298c2ecf20Sopenharmony_ci} 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_usx2y_4c = 5328c2ecf20Sopenharmony_ci{ 5338c2ecf20Sopenharmony_ci .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 5348c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_BLOCK_TRANSFER | 5358c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_MMAP_VALID), 5368c2ecf20Sopenharmony_ci .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE, 5378c2ecf20Sopenharmony_ci .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, 5388c2ecf20Sopenharmony_ci .rate_min = 44100, 5398c2ecf20Sopenharmony_ci .rate_max = 48000, 5408c2ecf20Sopenharmony_ci .channels_min = 2, 5418c2ecf20Sopenharmony_ci .channels_max = 4, 5428c2ecf20Sopenharmony_ci .buffer_bytes_max = (2*128*1024), 5438c2ecf20Sopenharmony_ci .period_bytes_min = 64, 5448c2ecf20Sopenharmony_ci .period_bytes_max = (128*1024), 5458c2ecf20Sopenharmony_ci .periods_min = 2, 5468c2ecf20Sopenharmony_ci .periods_max = 1024, 5478c2ecf20Sopenharmony_ci .fifo_size = 0 5488c2ecf20Sopenharmony_ci}; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_cistatic int snd_usx2y_usbpcm_open(struct snd_pcm_substream *substream) 5538c2ecf20Sopenharmony_ci{ 5548c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = ((struct snd_usx2y_substream **) 5558c2ecf20Sopenharmony_ci snd_pcm_substream_chip(substream))[substream->stream]; 5568c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = substream->runtime; 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci if (!(subs->usx2y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)) 5598c2ecf20Sopenharmony_ci return -EBUSY; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci runtime->hw = SNDRV_PCM_STREAM_PLAYBACK == substream->stream ? snd_usx2y_2c : 5628c2ecf20Sopenharmony_ci (subs->usx2y->subs[3] ? snd_usx2y_4c : snd_usx2y_2c); 5638c2ecf20Sopenharmony_ci runtime->private_data = subs; 5648c2ecf20Sopenharmony_ci subs->pcm_substream = substream; 5658c2ecf20Sopenharmony_ci snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000); 5668c2ecf20Sopenharmony_ci return 0; 5678c2ecf20Sopenharmony_ci} 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_cistatic int snd_usx2y_usbpcm_close(struct snd_pcm_substream *substream) 5718c2ecf20Sopenharmony_ci{ 5728c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = substream->runtime; 5738c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = runtime->private_data; 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci subs->pcm_substream = NULL; 5768c2ecf20Sopenharmony_ci return 0; 5778c2ecf20Sopenharmony_ci} 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_usx2y_usbpcm_ops = 5818c2ecf20Sopenharmony_ci{ 5828c2ecf20Sopenharmony_ci .open = snd_usx2y_usbpcm_open, 5838c2ecf20Sopenharmony_ci .close = snd_usx2y_usbpcm_close, 5848c2ecf20Sopenharmony_ci .hw_params = snd_usx2y_pcm_hw_params, 5858c2ecf20Sopenharmony_ci .hw_free = snd_usx2y_usbpcm_hw_free, 5868c2ecf20Sopenharmony_ci .prepare = snd_usx2y_usbpcm_prepare, 5878c2ecf20Sopenharmony_ci .trigger = snd_usx2y_pcm_trigger, 5888c2ecf20Sopenharmony_ci .pointer = snd_usx2y_pcm_pointer, 5898c2ecf20Sopenharmony_ci}; 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_cistatic int usx2y_pcms_busy_check(struct snd_card *card) 5938c2ecf20Sopenharmony_ci{ 5948c2ecf20Sopenharmony_ci struct usx2ydev *dev = usx2y(card); 5958c2ecf20Sopenharmony_ci int i; 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci for (i = 0; i < dev->pcm_devs * 2; i++) { 5988c2ecf20Sopenharmony_ci struct snd_usx2y_substream *subs = dev->subs[i]; 5998c2ecf20Sopenharmony_ci if (subs && subs->pcm_substream && 6008c2ecf20Sopenharmony_ci SUBSTREAM_BUSY(subs->pcm_substream)) 6018c2ecf20Sopenharmony_ci return -EBUSY; 6028c2ecf20Sopenharmony_ci } 6038c2ecf20Sopenharmony_ci return 0; 6048c2ecf20Sopenharmony_ci} 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_cistatic int snd_usx2y_hwdep_pcm_open(struct snd_hwdep *hw, struct file *file) 6078c2ecf20Sopenharmony_ci{ 6088c2ecf20Sopenharmony_ci struct snd_card *card = hw->card; 6098c2ecf20Sopenharmony_ci int err; 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci mutex_lock(&usx2y(card)->pcm_mutex); 6128c2ecf20Sopenharmony_ci err = usx2y_pcms_busy_check(card); 6138c2ecf20Sopenharmony_ci if (!err) 6148c2ecf20Sopenharmony_ci usx2y(card)->chip_status |= USX2Y_STAT_CHIP_MMAP_PCM_URBS; 6158c2ecf20Sopenharmony_ci mutex_unlock(&usx2y(card)->pcm_mutex); 6168c2ecf20Sopenharmony_ci return err; 6178c2ecf20Sopenharmony_ci} 6188c2ecf20Sopenharmony_ci 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_cistatic int snd_usx2y_hwdep_pcm_release(struct snd_hwdep *hw, struct file *file) 6218c2ecf20Sopenharmony_ci{ 6228c2ecf20Sopenharmony_ci struct snd_card *card = hw->card; 6238c2ecf20Sopenharmony_ci int err; 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_ci mutex_lock(&usx2y(card)->pcm_mutex); 6268c2ecf20Sopenharmony_ci err = usx2y_pcms_busy_check(card); 6278c2ecf20Sopenharmony_ci if (!err) 6288c2ecf20Sopenharmony_ci usx2y(hw->card)->chip_status &= ~USX2Y_STAT_CHIP_MMAP_PCM_URBS; 6298c2ecf20Sopenharmony_ci mutex_unlock(&usx2y(card)->pcm_mutex); 6308c2ecf20Sopenharmony_ci return err; 6318c2ecf20Sopenharmony_ci} 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_cistatic void snd_usx2y_hwdep_pcm_vm_open(struct vm_area_struct *area) 6358c2ecf20Sopenharmony_ci{ 6368c2ecf20Sopenharmony_ci} 6378c2ecf20Sopenharmony_ci 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_cistatic void snd_usx2y_hwdep_pcm_vm_close(struct vm_area_struct *area) 6408c2ecf20Sopenharmony_ci{ 6418c2ecf20Sopenharmony_ci} 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_cistatic vm_fault_t snd_usx2y_hwdep_pcm_vm_fault(struct vm_fault *vmf) 6458c2ecf20Sopenharmony_ci{ 6468c2ecf20Sopenharmony_ci unsigned long offset; 6478c2ecf20Sopenharmony_ci void *vaddr; 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci offset = vmf->pgoff << PAGE_SHIFT; 6508c2ecf20Sopenharmony_ci vaddr = (char *)((struct usx2ydev *)vmf->vma->vm_private_data)->hwdep_pcm_shm + offset; 6518c2ecf20Sopenharmony_ci vmf->page = virt_to_page(vaddr); 6528c2ecf20Sopenharmony_ci get_page(vmf->page); 6538c2ecf20Sopenharmony_ci return 0; 6548c2ecf20Sopenharmony_ci} 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_cistatic const struct vm_operations_struct snd_usx2y_hwdep_pcm_vm_ops = { 6588c2ecf20Sopenharmony_ci .open = snd_usx2y_hwdep_pcm_vm_open, 6598c2ecf20Sopenharmony_ci .close = snd_usx2y_hwdep_pcm_vm_close, 6608c2ecf20Sopenharmony_ci .fault = snd_usx2y_hwdep_pcm_vm_fault, 6618c2ecf20Sopenharmony_ci}; 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_cistatic int snd_usx2y_hwdep_pcm_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area) 6658c2ecf20Sopenharmony_ci{ 6668c2ecf20Sopenharmony_ci unsigned long size = (unsigned long)(area->vm_end - area->vm_start); 6678c2ecf20Sopenharmony_ci struct usx2ydev *usx2y = hw->private_data; 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci if (!(usx2y->chip_status & USX2Y_STAT_CHIP_INIT)) 6708c2ecf20Sopenharmony_ci return -EBUSY; 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci /* if userspace tries to mmap beyond end of our buffer, fail */ 6738c2ecf20Sopenharmony_ci if (size > PAGE_ALIGN(sizeof(struct snd_usx2y_hwdep_pcm_shm))) { 6748c2ecf20Sopenharmony_ci snd_printd("%lu > %lu\n", size, (unsigned long)sizeof(struct snd_usx2y_hwdep_pcm_shm)); 6758c2ecf20Sopenharmony_ci return -EINVAL; 6768c2ecf20Sopenharmony_ci } 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_ci if (!usx2y->hwdep_pcm_shm) { 6798c2ecf20Sopenharmony_ci return -ENODEV; 6808c2ecf20Sopenharmony_ci } 6818c2ecf20Sopenharmony_ci area->vm_ops = &snd_usx2y_hwdep_pcm_vm_ops; 6828c2ecf20Sopenharmony_ci area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 6838c2ecf20Sopenharmony_ci area->vm_private_data = hw->private_data; 6848c2ecf20Sopenharmony_ci return 0; 6858c2ecf20Sopenharmony_ci} 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_cistatic void snd_usx2y_hwdep_pcm_private_free(struct snd_hwdep *hwdep) 6898c2ecf20Sopenharmony_ci{ 6908c2ecf20Sopenharmony_ci struct usx2ydev *usx2y = hwdep->private_data; 6918c2ecf20Sopenharmony_ci if (NULL != usx2y->hwdep_pcm_shm) 6928c2ecf20Sopenharmony_ci free_pages_exact(usx2y->hwdep_pcm_shm, sizeof(struct snd_usx2y_hwdep_pcm_shm)); 6938c2ecf20Sopenharmony_ci} 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_ciint usx2y_hwdep_pcm_new(struct snd_card *card) 6978c2ecf20Sopenharmony_ci{ 6988c2ecf20Sopenharmony_ci int err; 6998c2ecf20Sopenharmony_ci struct snd_hwdep *hw; 7008c2ecf20Sopenharmony_ci struct snd_pcm *pcm; 7018c2ecf20Sopenharmony_ci struct usb_device *dev = usx2y(card)->dev; 7028c2ecf20Sopenharmony_ci if (1 != nr_of_packs()) 7038c2ecf20Sopenharmony_ci return 0; 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci if ((err = snd_hwdep_new(card, SND_USX2Y_USBPCM_ID, 1, &hw)) < 0) 7068c2ecf20Sopenharmony_ci return err; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci hw->iface = SNDRV_HWDEP_IFACE_USX2Y_PCM; 7098c2ecf20Sopenharmony_ci hw->private_data = usx2y(card); 7108c2ecf20Sopenharmony_ci hw->private_free = snd_usx2y_hwdep_pcm_private_free; 7118c2ecf20Sopenharmony_ci hw->ops.open = snd_usx2y_hwdep_pcm_open; 7128c2ecf20Sopenharmony_ci hw->ops.release = snd_usx2y_hwdep_pcm_release; 7138c2ecf20Sopenharmony_ci hw->ops.mmap = snd_usx2y_hwdep_pcm_mmap; 7148c2ecf20Sopenharmony_ci hw->exclusive = 1; 7158c2ecf20Sopenharmony_ci sprintf(hw->name, "/dev/bus/usb/%03d/%03d/hwdeppcm", dev->bus->busnum, dev->devnum); 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci err = snd_pcm_new(card, NAME_ALLCAPS" hwdep Audio", 2, 1, 1, &pcm); 7188c2ecf20Sopenharmony_ci if (err < 0) { 7198c2ecf20Sopenharmony_ci return err; 7208c2ecf20Sopenharmony_ci } 7218c2ecf20Sopenharmony_ci snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usx2y_usbpcm_ops); 7228c2ecf20Sopenharmony_ci snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usx2y_usbpcm_ops); 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci pcm->private_data = usx2y(card)->subs; 7258c2ecf20Sopenharmony_ci pcm->info_flags = 0; 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci sprintf(pcm->name, NAME_ALLCAPS" hwdep Audio"); 7288c2ecf20Sopenharmony_ci snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, 7298c2ecf20Sopenharmony_ci SNDRV_DMA_TYPE_CONTINUOUS, 7308c2ecf20Sopenharmony_ci NULL, 7318c2ecf20Sopenharmony_ci 64*1024, 128*1024); 7328c2ecf20Sopenharmony_ci snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, 7338c2ecf20Sopenharmony_ci SNDRV_DMA_TYPE_CONTINUOUS, 7348c2ecf20Sopenharmony_ci NULL, 7358c2ecf20Sopenharmony_ci 64*1024, 128*1024); 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci return 0; 7388c2ecf20Sopenharmony_ci} 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci#else 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_ciint usx2y_hwdep_pcm_new(struct snd_card *card) 7438c2ecf20Sopenharmony_ci{ 7448c2ecf20Sopenharmony_ci return 0; 7458c2ecf20Sopenharmony_ci} 7468c2ecf20Sopenharmony_ci 7478c2ecf20Sopenharmony_ci#endif 748