1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef AUDIO_USB_LINUX_H
10 #define AUDIO_USB_LINUX_H
11 
12 #include <linux/usb.h>
13 
14 #include "audio_platform_if.h"
15 #include "hdf_dlist.h"
16 
17 struct AudioUsbEndpoint;
18 
19 #define MAX_PACKS    6               /* per URB */
20 #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode, 8 is max packet length */
21 #define MAX_URBS     12
22 
23 #define AUDIO_USB_ENDPOINT_TYPE_DATA 0
24 #define AUDIO_USB_ENDPOINT_TYPE_SYNC 1
25 
26 struct AudioUsbUrbCtx {
27     struct urb *urb;
28     uint32_t bufferSize; /* size of data buffer, if data URB */
29     struct AudioUsbEndpoint *endpoint;
30     int32_t index;                    /* index for urb array */
31     int32_t packets;                  /* number of packets per urb */
32     int32_t packetSize[MAX_PACKS_HS]; /* size of packets for next submission */
33     struct list_head readyList;
34 };
35 
36 struct AudioUsbEndpoint {
37     struct AudioUsbDriver *audioUsbDriver;
38 
39     int32_t useCount;
40     int32_t endpointNum; /* the referenced endpoint number */
41     int32_t type;        /* endpoint type */
42     unsigned long flags;
43 
44     void (*AudioPrepareDataUrb)(struct AudioUsbDriver *audioUsbDriver, struct urb *urb);
45     void (*AudioRetireDataUrb)(struct AudioUsbDriver *audioUsbDriver, struct urb *urb);
46 
47     struct AudioUsbEndpoint *syncMasterEndpoint;
48     struct AudioUsbEndpoint *syncSlaveEndpoint;
49 
50     struct AudioUsbUrbCtx urbContext[MAX_URBS];
51 
52     struct AudioUsbPacketInfo {
53         uint32_t packetSize[MAX_PACKS_HS];
54         int32_t packets;
55     } nextPacket[MAX_URBS];
56     int32_t nextPacketReadPos;
57     int32_t nextPacketWritePos;
58     struct list_head readyPlaybackUrbs;
59 
60     uint32_t nurbs;           /* # urbs */
61     unsigned long activeMask; /* bitmask of active urbs */
62     unsigned long unlinkMask; /* bitmask of unlinked urbs */
63     char *syncbuf;            /* sync buffer for all sync URBs */
64     dma_addr_t syncDma;       /* DMA address of syncbuf */
65 
66     uint32_t pipe;             /* the data i/o pipe */
67     uint32_t packsize[2];      /* small/large packet sizes in samples */
68     uint32_t sampleRem;        /* remainder from division fs/pps */
69     uint32_t sampleAccum;      /* sample accumulator */
70     uint32_t pps;              /* packets per second */
71     uint32_t freqn;            /* nominal sampling rate in fs/fps in Q16.16 format */
72     uint32_t freqm;            /* momentary sampling rate in fs/fps in Q16.16 format */
73     int32_t freqShift;         /* how much to shift the feedback value to get Q16.16 */
74     uint32_t freqMax;          /* maximum sampling rate, used for buffer management */
75     uint32_t phase;            /* phase accumulator */
76     uint32_t maxPackSize;      /* max packet size in bytes */
77     uint32_t maxFrameSize;     /* max packet size in frames */
78     uint32_t maxUrbFrames;     /* max URB size in frames */
79     uint32_t curPackSize;      /* current packet size in bytes (for capture) */
80     uint32_t curframesize;     /* current packet size in frames (for capture) */
81     uint32_t syncMaxSize;      /* sync endpoint packet size */
82     uint32_t fillMax : 1;      /* fill max packet size always */
83     uint32_t tenorFbQuirk : 1; /* corrupted feedback data */
84     uint32_t dataInterval;     /* log_2 of data packet interval */
85     uint32_t syncInterval;     /* P for adaptive mode, 0 otherwise */
86     uint8_t silenceValue;
87     uint32_t stride;
88     int32_t iface, altsetting;
89     int32_t skipPackets;     /* quirks for devices to ignore the first n packets in a stream */
90     bool isImplicitFeedback; /* This endpoint is used as implicit feedback */
91 
92     spinlock_t lock;
93     struct DListHead list; /* list of endpoint */
94 };
95 
96 struct AudioUsbFormat {
97     struct DListHead list;
98     uint64_t formats;          /* format bits */
99     uint32_t channels;         /* # channels */
100     uint32_t fmtType;          /* USB audio format type (1-3) */
101     uint32_t fmtBits;          /* number of significant bits */
102     uint32_t frameSize;        /* samples per frame for non-audio */
103     int32_t iface;             /* interface number */
104     uint8_t altsetting;        /* corresponding alternate setting */
105     uint8_t altsetIdx;         /* array index of altenate setting */
106     int32_t attributes;        /* corresponding attributes of cs endpoint */
107     uint8_t endpoint;          /* endpoint */
108     uint8_t epAttr;            /* endpoint attributes */
109     uint8_t dataInterval;      /* log_2 of data packet interval */
110     uint8_t protocol;          /* UAC_VERSION_1/2/3 */
111     uint32_t maxPackSize;      /* max. packet size */
112     uint32_t rates;            /* rate bitmasks */
113     uint32_t rateMin, rateMax; /* min/max rates */
114     uint32_t nrRates;          /* number of rate table entries */
115     uint32_t *rateTable;       /* rate table */
116     uint8_t clock;             /* associated clock */
117     bool dsdDop;               /* add DOP headers in case of DSD samples */
118     bool dsdBitRev;            /* reverse the bits of each DSD sample */
119     bool dsdRaw;               /* altsetting is raw DSD */
120 };
121 
122 struct AudioUsbDriver {
123     struct usb_device *dev;
124     struct usb_interface *usbIf;
125     const struct usb_device_id *usbDevId;
126     struct AudioCard *audioCard;
127     struct AudioUsbFormat *renderUsbFormat;
128     struct AudioUsbFormat *captureUsbFormat;
129 
130     struct usb_host_interface *ctrlIntf;
131     struct CircleBufInfo renderBufInfo;  /**< Render pcm stream transfer */
132     struct CircleBufInfo captureBufInfo; /**< Capture pcm stream transfer */
133     struct PcmInfo renderPcmInfo;        /**< Render pcm stream info */
134     struct PcmInfo capturePcmInfo;       /**< Capture pcm stream info */
135     int32_t ifNum;
136     uint32_t usbId;
137     void *priv;
138     int8_t pnpFlag;
139     atomic_t active;
140     atomic_t shutdown;
141     atomic_t usageCount;
142     wait_queue_head_t shutdownWait;
143     int32_t sampleRateReadError;
144     bool needSetupEp;
145     int32_t setup;
146 
147     unsigned long renderFlags;
148     unsigned long captureFlags;
149 
150     uint32_t fmtType;
151 
152     uint32_t renderTransferDone;
153     uint32_t captureTransferDone;
154     uint32_t renderHwptr;
155     uint32_t captureHwptr;
156 
157     uint32_t frameLimit;
158     bool triggerTstampPendingUpdate;
159     uint32_t running;
160 
161     uint32_t epNum; /* the endpoint number */
162 
163     struct AudioUsbEndpoint *renderDataEndpoint;
164     struct AudioUsbEndpoint *renderSyncEndpoint;
165     struct AudioUsbEndpoint *captureDataEndpoint;
166     struct AudioUsbEndpoint *captureSyncEndpoint;
167 
168     spinlock_t lock;
169 
170     struct DListHead endpointList; /* list of endpoint */
171 
172     struct DListHead renderUsbFormatList;  /* list of audioUsbFormat */
173     struct DListHead captureUsbFormatList; /* list of audioUsbFormat */
174 
175     struct mutex mutex;
176 };
177 
178 uint32_t AudioUsbGetUsbId(uint32_t vendor, uint32_t product);
179 struct AudioUsbDriver *GetLinuxAudioUsb(void);
180 
181 #endif