1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Line 6 Linux USB driver 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) 6*4882a593Smuzhiyun */ 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun /* 9*4882a593Smuzhiyun PCM interface to POD series devices. 10*4882a593Smuzhiyun */ 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun #ifndef PCM_H 13*4882a593Smuzhiyun #define PCM_H 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun #include <sound/pcm.h> 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun #include "driver.h" 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun /* 20*4882a593Smuzhiyun number of USB frames per URB 21*4882a593Smuzhiyun The Line 6 Windows driver always transmits two frames per packet, but 22*4882a593Smuzhiyun the Linux driver performs significantly better (i.e., lower latency) 23*4882a593Smuzhiyun with only one frame per packet. 24*4882a593Smuzhiyun */ 25*4882a593Smuzhiyun #define LINE6_ISO_PACKETS 1 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun /* in a "full speed" device (such as the PODxt Pro) this means 1ms, 28*4882a593Smuzhiyun * for "high speed" it's 1/8ms 29*4882a593Smuzhiyun */ 30*4882a593Smuzhiyun #define LINE6_ISO_INTERVAL 1 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun #define LINE6_IMPULSE_DEFAULT_PERIOD 100 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun /* 35*4882a593Smuzhiyun Get substream from Line 6 PCM data structure 36*4882a593Smuzhiyun */ 37*4882a593Smuzhiyun #define get_substream(line6pcm, stream) \ 38*4882a593Smuzhiyun (line6pcm->pcm->streams[stream].substream) 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun /* 41*4882a593Smuzhiyun PCM mode bits. 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun There are several features of the Line 6 USB driver which require PCM 44*4882a593Smuzhiyun data to be exchanged with the device: 45*4882a593Smuzhiyun *) PCM playback and capture via ALSA 46*4882a593Smuzhiyun *) software monitoring (for devices without hardware monitoring) 47*4882a593Smuzhiyun *) optional impulse response measurement 48*4882a593Smuzhiyun However, from the device's point of view, there is just a single 49*4882a593Smuzhiyun capture and playback stream, which must be shared between these 50*4882a593Smuzhiyun subsystems. It is therefore necessary to maintain the state of the 51*4882a593Smuzhiyun subsystems with respect to PCM usage. 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun We define two bit flags, "opened" and "running", for each playback 54*4882a593Smuzhiyun or capture stream. Both can contain the bit flag corresponding to 55*4882a593Smuzhiyun LINE6_STREAM_* type, 56*4882a593Smuzhiyun LINE6_STREAM_PCM = ALSA PCM playback or capture 57*4882a593Smuzhiyun LINE6_STREAM_MONITOR = software monitoring 58*4882a593Smuzhiyun IMPULSE = optional impulse response measurement 59*4882a593Smuzhiyun The opened flag indicates whether the buffer is allocated while 60*4882a593Smuzhiyun the running flag indicates whether the stream is running. 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun For monitor or impulse operations, the driver needs to call 63*4882a593Smuzhiyun line6_pcm_acquire() or line6_pcm_release() with the appropriate 64*4882a593Smuzhiyun LINE6_STREAM_* flag. 65*4882a593Smuzhiyun */ 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun /* stream types */ 68*4882a593Smuzhiyun enum { 69*4882a593Smuzhiyun LINE6_STREAM_PCM, 70*4882a593Smuzhiyun LINE6_STREAM_MONITOR, 71*4882a593Smuzhiyun LINE6_STREAM_IMPULSE, 72*4882a593Smuzhiyun LINE6_STREAM_CAPTURE_HELPER, 73*4882a593Smuzhiyun }; 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun /* misc bit flags for PCM operation */ 76*4882a593Smuzhiyun enum { 77*4882a593Smuzhiyun LINE6_FLAG_PAUSE_PLAYBACK, 78*4882a593Smuzhiyun LINE6_FLAG_PREPARED, 79*4882a593Smuzhiyun }; 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun struct line6_pcm_properties { 82*4882a593Smuzhiyun struct snd_pcm_hardware playback_hw, capture_hw; 83*4882a593Smuzhiyun struct snd_pcm_hw_constraint_ratdens rates; 84*4882a593Smuzhiyun int bytes_per_channel; 85*4882a593Smuzhiyun }; 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun struct line6_pcm_stream { 88*4882a593Smuzhiyun /* allocated URBs */ 89*4882a593Smuzhiyun struct urb **urbs; 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun /* Temporary buffer; 92*4882a593Smuzhiyun * Since the packet size is not known in advance, this buffer is 93*4882a593Smuzhiyun * large enough to store maximum size packets. 94*4882a593Smuzhiyun */ 95*4882a593Smuzhiyun unsigned char *buffer; 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun /* Free frame position in the buffer. */ 98*4882a593Smuzhiyun snd_pcm_uframes_t pos; 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun /* Count processed bytes; 101*4882a593Smuzhiyun * This is modulo period size (to determine when a period is finished). 102*4882a593Smuzhiyun */ 103*4882a593Smuzhiyun unsigned bytes; 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun /* Counter to create desired sample rate */ 106*4882a593Smuzhiyun unsigned count; 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun /* period size in bytes */ 109*4882a593Smuzhiyun unsigned period; 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun /* Processed frame position in the buffer; 112*4882a593Smuzhiyun * The contents of the ring buffer have been consumed by the USB 113*4882a593Smuzhiyun * subsystem (i.e., sent to the USB device) up to this position. 114*4882a593Smuzhiyun */ 115*4882a593Smuzhiyun snd_pcm_uframes_t pos_done; 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun /* Bit mask of active URBs */ 118*4882a593Smuzhiyun unsigned long active_urbs; 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun /* Bit mask of URBs currently being unlinked */ 121*4882a593Smuzhiyun unsigned long unlink_urbs; 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun /* Spin lock to protect updates of the buffer positions (not contents) 124*4882a593Smuzhiyun */ 125*4882a593Smuzhiyun spinlock_t lock; 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun /* Bit flags for operational stream types */ 128*4882a593Smuzhiyun unsigned long opened; 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun /* Bit flags for running stream types */ 131*4882a593Smuzhiyun unsigned long running; 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun int last_frame; 134*4882a593Smuzhiyun }; 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun struct snd_line6_pcm { 137*4882a593Smuzhiyun /* Pointer back to the Line 6 driver data structure */ 138*4882a593Smuzhiyun struct usb_line6 *line6; 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun /* Properties. */ 141*4882a593Smuzhiyun struct line6_pcm_properties *properties; 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun /* ALSA pcm stream */ 144*4882a593Smuzhiyun struct snd_pcm *pcm; 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun /* protection to state changes of in/out streams */ 147*4882a593Smuzhiyun struct mutex state_mutex; 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun /* Capture and playback streams */ 150*4882a593Smuzhiyun struct line6_pcm_stream in; 151*4882a593Smuzhiyun struct line6_pcm_stream out; 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun /* Previously captured frame (for software monitoring) */ 154*4882a593Smuzhiyun unsigned char *prev_fbuf; 155*4882a593Smuzhiyun 156*4882a593Smuzhiyun /* Size of previously captured frame (for software monitoring/sync) */ 157*4882a593Smuzhiyun int prev_fsize; 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun /* Maximum size of USB packet */ 160*4882a593Smuzhiyun int max_packet_size_in; 161*4882a593Smuzhiyun int max_packet_size_out; 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun /* PCM playback volume (left and right) */ 164*4882a593Smuzhiyun int volume_playback[2]; 165*4882a593Smuzhiyun 166*4882a593Smuzhiyun /* PCM monitor volume */ 167*4882a593Smuzhiyun int volume_monitor; 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun /* Volume of impulse response test signal (if zero, test is disabled) */ 170*4882a593Smuzhiyun int impulse_volume; 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun /* Period of impulse response test signal */ 173*4882a593Smuzhiyun int impulse_period; 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun /* Counter for impulse response test signal */ 176*4882a593Smuzhiyun int impulse_count; 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun /* Several status bits (see LINE6_FLAG_*) */ 179*4882a593Smuzhiyun unsigned long flags; 180*4882a593Smuzhiyun }; 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun extern int line6_init_pcm(struct usb_line6 *line6, 183*4882a593Smuzhiyun struct line6_pcm_properties *properties); 184*4882a593Smuzhiyun extern int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd); 185*4882a593Smuzhiyun extern int snd_line6_prepare(struct snd_pcm_substream *substream); 186*4882a593Smuzhiyun extern int snd_line6_hw_params(struct snd_pcm_substream *substream, 187*4882a593Smuzhiyun struct snd_pcm_hw_params *hw_params); 188*4882a593Smuzhiyun extern int snd_line6_hw_free(struct snd_pcm_substream *substream); 189*4882a593Smuzhiyun extern snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream); 190*4882a593Smuzhiyun extern void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm); 191*4882a593Smuzhiyun extern int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, 192*4882a593Smuzhiyun bool start); 193*4882a593Smuzhiyun extern void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type); 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun #endif 196