clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name uaudio.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -target-feature +retpoline-external-thunk -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/legacy-dpm -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu13 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/inc -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc/pmfw_if -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D SUSPEND -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/scan/2024-01-11-110808-61670-1 -x c /usr/src/sys/dev/usb/uaudio.c
| 1 |  | 
| 2 |  | 
| 3 |   | 
| 4 |   | 
| 5 |   | 
| 6 |   | 
| 7 |   | 
| 8 |   | 
| 9 |   | 
| 10 |   | 
| 11 |   | 
| 12 |   | 
| 13 |   | 
| 14 |   | 
| 15 |   | 
| 16 |   | 
| 17 |  | 
| 18 |   | 
| 19 |   | 
| 20 |   | 
| 21 |   | 
| 22 |   | 
| 23 |   | 
| 24 | #include <sys/param.h> | 
| 25 | #include <sys/types.h> | 
| 26 | #include <sys/device.h> | 
| 27 | #include <sys/errno.h> | 
| 28 | #include <sys/fcntl.h> | 
| 29 | #include <sys/malloc.h> | 
| 30 | #include <sys/systm.h> | 
| 31 | #include <sys/time.h> | 
| 32 | #include <sys/audioio.h> | 
| 33 | #include <machine/bus.h> | 
| 34 | #include <dev/audio_if.h> | 
| 35 | #include <dev/usb/usb.h> | 
| 36 | #include <dev/usb/usbdi.h> | 
| 37 | #include <dev/usb/usbdivar.h> | 
| 38 | #include <dev/usb/usb_mem.h> | 
| 39 |   | 
| 40 | #ifdef UAUDIO_DEBUG | 
| 41 | #define DPRINTF(...)				\ | 
| 42 | 	do {					\ | 
| 43 | 		if (uaudio_debug)		\ | 
| 44 | 			printf(__VA_ARGS__);	\ | 
| 45 | 	} while (0) | 
| 46 | #else | 
| 47 | #define DPRINTF(...) do {} while(0) | 
| 48 | #endif | 
| 49 |   | 
| 50 | #define DEVNAME(sc) ((sc)->dev.dv_xname) | 
| 51 |   | 
| 52 |  | 
| 53 |   | 
| 54 |   | 
| 55 | #define UE_ISO_USAGE			0x30 | 
| 56 | #define  UE_ISO_USAGE_DATA		0x00 | 
| 57 | #define  UE_ISO_USAGE_FEEDBACK		0x10 | 
| 58 | #define  UE_ISO_USAGE_IMPL		0x20 | 
| 59 | #define UE_GET_ISO_USAGE(a)		((a) & UE_ISO_USAGE) | 
| 60 |   | 
| 61 |  | 
| 62 |   | 
| 63 |   | 
| 64 | #define UAUDIO_NAMEMAX			MAX_AUDIO_DEV_LEN | 
| 65 |   | 
| 66 |  | 
| 67 |   | 
| 68 |   | 
| 69 | #define UAUDIO_V1			0x100 | 
| 70 | #define UAUDIO_V2			0x200 | 
| 71 |   | 
| 72 |  | 
| 73 |   | 
| 74 |   | 
| 75 | #define UAUDIO_AC_HEADER		0x1 | 
| 76 | #define UAUDIO_AC_INPUT			0x2 | 
| 77 | #define UAUDIO_AC_OUTPUT		0x3 | 
| 78 | #define UAUDIO_AC_MIXER			0x4 | 
| 79 | #define UAUDIO_AC_SELECTOR		0x5 | 
| 80 | #define UAUDIO_AC_FEATURE		0x6 | 
| 81 | #define UAUDIO_AC_EFFECT		0x7 | 
| 82 | #define UAUDIO_AC_PROCESSING		0x8 | 
| 83 | #define UAUDIO_AC_EXTENSION		0x9 | 
| 84 | #define UAUDIO_AC_CLKSRC		0xa | 
| 85 | #define UAUDIO_AC_CLKSEL		0xb | 
| 86 | #define UAUDIO_AC_CLKMULT		0xc | 
| 87 | #define UAUDIO_AC_RATECONV		0xd | 
| 88 |   | 
| 89 |  | 
| 90 |   | 
| 91 |   | 
| 92 | #define UAUDIO_AS_GENERAL		0x1 | 
| 93 | #define UAUDIO_AS_FORMAT		0x2 | 
| 94 |   | 
| 95 |  | 
| 96 |   | 
| 97 |   | 
| 98 | #define UAUDIO_EP_GENERAL		0x1 | 
| 99 |   | 
| 100 |  | 
| 101 |   | 
| 102 |   | 
| 103 | #define UAUDIO_V1_FMT_PCM		0x1 | 
| 104 | #define UAUDIO_V1_FMT_PCM8		0x2 | 
| 105 | #define UAUDIO_V1_FMT_FLOAT		0x3 | 
| 106 | #define UAUDIO_V1_FMT_ALAW		0x4 | 
| 107 | #define UAUDIO_V1_FMT_MULAW		0x5 | 
| 108 |   | 
| 109 |  | 
| 110 |   | 
| 111 |   | 
| 112 | #define UAUDIO_V2_FMT_PCM		0x01 | 
| 113 | #define UAUDIO_V2_FMT_PCM8		0x02 | 
| 114 | #define UAUDIO_V2_FMT_FLOAT		0x04 | 
| 115 | #define UAUDIO_V2_FMT_ALAW		0x08 | 
| 116 | #define UAUDIO_V2_FMT_MULAW		0x10 | 
| 117 |   | 
| 118 |  | 
| 119 |   | 
| 120 |   | 
| 121 | #define UAUDIO_V1_REQ_SET_CUR		0x01 | 
| 122 | #define UAUDIO_V1_REQ_SET_MIN		0x02 | 
| 123 | #define UAUDIO_V1_REQ_SET_MAX		0x03 | 
| 124 | #define UAUDIO_V1_REQ_SET_RES		0x04 | 
| 125 | #define UAUDIO_V1_REQ_GET_CUR		0x81 | 
| 126 | #define UAUDIO_V1_REQ_GET_MIN		0x82 | 
| 127 | #define UAUDIO_V1_REQ_GET_MAX		0x83 | 
| 128 | #define UAUDIO_V1_REQ_GET_RES		0x84 | 
| 129 | #define UAUDIO_V2_REQ_CUR		1 | 
| 130 | #define UAUDIO_V2_REQ_RANGES		2 | 
| 131 |   | 
| 132 |  | 
| 133 |   | 
| 134 |   | 
| 135 | #define UAUDIO_V2_REQSEL_CLKFREQ	1 | 
| 136 | #define UAUDIO_V2_REQSEL_CLKSEL		1 | 
| 137 |   | 
| 138 |  | 
| 139 |   | 
| 140 |   | 
| 141 | #define UAUDIO_EP_FREQCTL		0x01 | 
| 142 |   | 
| 143 |  | 
| 144 |   | 
| 145 |   | 
| 146 | #define UAUDIO_REQSEL_MUTE		0x01 | 
| 147 | #define UAUDIO_REQSEL_VOLUME		0x02 | 
| 148 | #define UAUDIO_REQSEL_BASS		0x03 | 
| 149 | #define UAUDIO_REQSEL_MID		0x04 | 
| 150 | #define UAUDIO_REQSEL_TREBLE		0x05 | 
| 151 | #define UAUDIO_REQSEL_EQ		0x06 | 
| 152 | #define UAUDIO_REQSEL_AGC		0x07 | 
| 153 | #define UAUDIO_REQSEL_DELAY		0x08 | 
| 154 | #define UAUDIO_REQSEL_BASSBOOST		0x09 | 
| 155 | #define UAUDIO_REQSEL_LOUDNESS		0x0a | 
| 156 | #define UAUDIO_REQSEL_GAIN		0x0b | 
| 157 | #define UAUDIO_REQSEL_GAINPAD		0x0c | 
| 158 | #define UAUDIO_REQSEL_PHASEINV		0x0d | 
| 159 |   | 
| 160 |  | 
| 161 |   | 
| 162 |   | 
| 163 | #define UAUDIO_REQSEL_RATE		0x01 | 
| 164 |   | 
| 165 |  | 
| 166 |   | 
| 167 |   | 
| 168 |   | 
| 169 |   | 
| 170 |   | 
| 171 |   | 
| 172 | #define UAUDIO_SPF_DIV			327680 | 
| 173 |   | 
| 174 |  | 
| 175 |   | 
| 176 |   | 
| 177 | #define UAUDIO_NAME_PLAY	"dac" | 
| 178 | #define UAUDIO_NAME_REC		"record" | 
| 179 |   | 
| 180 |  | 
| 181 |   | 
| 182 |   | 
| 183 |   | 
| 184 |   | 
| 185 | struct uaudio_blob { | 
| 186 | 	unsigned char *rptr, *wptr; | 
| 187 | }; | 
| 188 |   | 
| 189 |  | 
| 190 |   | 
| 191 |   | 
| 192 |   | 
| 193 | struct uaudio_ranges { | 
| 194 | 	unsigned int nval; | 
| 195 | 	struct uaudio_ranges_el { | 
| 196 | 		struct uaudio_ranges_el *next; | 
| 197 | 		int min, max, res; | 
| 198 | 	} *el; | 
| 199 | }; | 
| 200 |   | 
| 201 | struct uaudio_softc { | 
| 202 | 	struct device dev; | 
| 203 | 	struct usbd_device *udev; | 
| 204 | 	int version; | 
| 205 |   | 
| 206 | 	 | 
| 207 | 	  | 
| 208 | 	  | 
| 209 | 	  | 
| 210 | 	  | 
| 211 | 	  | 
| 212 | 	  | 
| 213 | 	  | 
| 214 | 	  | 
| 215 | 	  | 
| 216 | 	  | 
| 217 | 	struct uaudio_unit { | 
| 218 | 		struct uaudio_unit *unit_next, *src_next, *dst_next; | 
| 219 | 		struct uaudio_unit *src_list, *dst_list; | 
| 220 | 		char name[UAUDIO_NAMEMAX]; | 
| 221 | 		unsigned int nch; | 
| 222 | 		int type, id; | 
| 223 |   | 
| 224 | 		 | 
| 225 | 		unsigned int term; | 
| 226 |   | 
| 227 | 		 | 
| 228 | 		struct uaudio_unit *clock; | 
| 229 |   | 
| 230 | 		 | 
| 231 | 		struct uaudio_ranges rates; | 
| 232 |   | 
| 233 | 		 | 
| 234 | #define UAUDIO_CLASS_OUT	0 | 
| 235 | #define UAUDIO_CLASS_IN		1 | 
| 236 | #define UAUDIO_CLASS_COUNT	2 | 
| 237 | 		int mixer_class; | 
| 238 | 		struct uaudio_mixent { | 
| 239 | 			struct uaudio_mixent *next; | 
| 240 | 			char *fname; | 
| 241 | #define UAUDIO_MIX_SW	0 | 
| 242 | #define UAUDIO_MIX_NUM	1 | 
| 243 | #define UAUDIO_MIX_ENUM	2 | 
| 244 | 			int type; | 
| 245 | 			int chan; | 
| 246 | 			int req_sel; | 
| 247 | 			struct uaudio_ranges ranges; | 
| 248 | 		} *mixent_list; | 
| 249 | 	} *unit_list; | 
| 250 |   | 
| 251 | 	 | 
| 252 | 	  | 
| 253 | 	  | 
| 254 | 	struct uaudio_unit *clock; | 
| 255 |   | 
| 256 | 	 | 
| 257 | 	  | 
| 258 | 	  | 
| 259 | 	unsigned int nin, nout; | 
| 260 |   | 
| 261 | 	 | 
| 262 | 	  | 
| 263 | 	  | 
| 264 | 	  | 
| 265 | 	  | 
| 266 | 	  | 
| 267 | 	struct uaudio_name { | 
| 268 | 		struct uaudio_name *next; | 
| 269 | 		char *templ; | 
| 270 | 		unsigned int unit; | 
| 271 | 	} *names; | 
| 272 |   | 
| 273 | 	 | 
| 274 | 	  | 
| 275 | 	  | 
| 276 | 	  | 
| 277 | 	struct uaudio_alt { | 
| 278 | 		struct uaudio_alt *next; | 
| 279 | 		int ifnum, altnum; | 
| 280 | 		int mode;		 | 
| 281 | 		int data_addr;		 | 
| 282 | 		int sync_addr;		 | 
| 283 | 		int maxpkt;		 | 
| 284 | 		int fps;		 | 
| 285 | 		int bps, bits, nch;	 | 
| 286 | 		int v1_rates;		 | 
| 287 | 	} *alts; | 
| 288 |   | 
| 289 | 	 | 
| 290 | 	  | 
| 291 | 	  | 
| 292 | 	  | 
| 293 | 	struct uaudio_params { | 
| 294 | 		struct uaudio_params *next; | 
| 295 | 		struct uaudio_alt *palt, *ralt; | 
| 296 | 		int v1_rates; | 
| 297 | 	} *params_list, *params; | 
| 298 |   | 
| 299 | 	 | 
| 300 | 	  | 
| 301 | 	  | 
| 302 | 	struct uaudio_stream { | 
| 303 | #define UAUDIO_NXFERS_MIN	2 | 
| 304 | #define UAUDIO_NXFERS_MAX	8 | 
| 305 | 		struct uaudio_xfer { | 
| 306 | 			struct usbd_xfer *usb_xfer; | 
| 307 | 			unsigned char *buf; | 
| 308 | 			uint16_t *sizes; | 
| 309 | 			unsigned int size;	 | 
| 310 | 			unsigned int nframes;	 | 
| 311 | 		} data_xfers[UAUDIO_NXFERS_MAX], sync_xfers[UAUDIO_NXFERS_MAX]; | 
| 312 |   | 
| 313 | 		 | 
| 314 | 		  | 
| 315 | 		  | 
| 316 | 		  | 
| 317 | 		  | 
| 318 | 		unsigned int nxfers; | 
| 319 |   | 
| 320 | 		unsigned int spf_remain;	 | 
| 321 | 		unsigned int spf;		 | 
| 322 | 		unsigned int spf_min, spf_max;	 | 
| 323 |   | 
| 324 | 		 | 
| 325 | 		  | 
| 326 | 		  | 
| 327 | 		  | 
| 328 | 		unsigned int maxpkt; | 
| 329 |   | 
| 330 | 		 | 
| 331 | 		  | 
| 332 | 		  | 
| 333 | 		unsigned int nframes_max; | 
| 334 |   | 
| 335 | 		 | 
| 336 | 		  | 
| 337 | 		  | 
| 338 | 		  | 
| 339 | 		  | 
| 340 | 		  | 
| 341 | 		  | 
| 342 | 		  | 
| 343 | 		  | 
| 344 | 		  | 
| 345 | 		  | 
| 346 | 		  | 
| 347 | 		unsigned int nframes_mask; | 
| 348 |   | 
| 349 | 		unsigned int data_nextxfer, sync_nextxfer; | 
| 350 | 		struct usbd_pipe *data_pipe; | 
| 351 | 		struct usbd_pipe *sync_pipe; | 
| 352 | 		void (*intr)(void *); | 
| 353 | 		void *arg; | 
| 354 |   | 
| 355 | 		 | 
| 356 | 		unsigned char *ring_start, *ring_end; | 
| 357 |   | 
| 358 | 		 | 
| 359 | 		unsigned char *ring_pos; | 
| 360 | 		 | 
| 361 | 		 | 
| 362 | 		int ring_blksz; | 
| 363 |   | 
| 364 | 		 | 
| 365 | 		int ring_offs; | 
| 366 |   | 
| 367 | 		 | 
| 368 | 		  | 
| 369 | 		  | 
| 370 | 		  | 
| 371 | 		  | 
| 372 | 		  | 
| 373 | 		int safe_blksz; | 
| 374 |   | 
| 375 | 		 | 
| 376 | 		  | 
| 377 | 		  | 
| 378 | 		  | 
| 379 | 		int ring_icnt; | 
| 380 |   | 
| 381 | 		 | 
| 382 | 		  | 
| 383 | 		  | 
| 384 | 		  | 
| 385 | 		  | 
| 386 | 		unsigned int ubuf_xfer;		 | 
| 387 | 		unsigned int ubuf_pos;		 | 
| 388 | 	} pstream, rstream; | 
| 389 |   | 
| 390 | 	int ctl_ifnum;			 | 
| 391 |   | 
| 392 | 	int mode;			 | 
| 393 | 	int trigger_mode;		 | 
| 394 |   | 
| 395 | 	unsigned int rate;		 | 
| 396 | 	unsigned int ufps;		 | 
| 397 | 	unsigned int sync_pktsz;	 | 
| 398 | 	unsigned int host_nframes;	 | 
| 399 |   | 
| 400 | 	int diff_nsamp;			 | 
| 401 | 	int diff_nframes;		 | 
| 402 | 	unsigned int adjspf_age;	 | 
| 403 |   | 
| 404 | 	 | 
| 405 | 	  | 
| 406 | 	  | 
| 407 | 	  | 
| 408 | 	  | 
| 409 | 	size_t copy_todo; | 
| 410 | }; | 
| 411 |   | 
| 412 | int uaudio_match(struct device *, void *, void *); | 
| 413 | void uaudio_attach(struct device *, struct device *, void *); | 
| 414 | int uaudio_detach(struct device *, int); | 
| 415 |   | 
| 416 | int uaudio_open(void *, int); | 
| 417 | void uaudio_close(void *); | 
| 418 | int uaudio_set_params(void *, int, int, struct audio_params *, | 
| 419 |     struct audio_params *); | 
| 420 | unsigned int uaudio_set_blksz(void *, int, | 
| 421 |     struct audio_params *, struct audio_params *, unsigned int); | 
| 422 | int uaudio_trigger_output(void *, void *, void *, int, | 
| 423 |     void (*)(void *), void *, struct audio_params *); | 
| 424 | int uaudio_trigger_input(void *, void *, void *, int, | 
| 425 |     void (*)(void *), void *, struct audio_params *); | 
| 426 | void uaudio_copy_output(void *, size_t); | 
| 427 | void uaudio_underrun(void *); | 
| 428 | int uaudio_halt_output(void *); | 
| 429 | int uaudio_halt_input(void *); | 
| 430 | int uaudio_query_devinfo(void *, struct mixer_devinfo *); | 
| 431 | int uaudio_get_port(void *, struct mixer_ctrl *); | 
| 432 | int uaudio_set_port(void *, struct mixer_ctrl *); | 
| 433 |   | 
| 434 | int uaudio_process_unit(struct uaudio_softc *, | 
| 435 |     struct uaudio_unit *, int, | 
| 436 |     struct uaudio_blob, | 
| 437 |     struct uaudio_unit **); | 
| 438 |   | 
| 439 | void uaudio_pdata_intr(struct usbd_xfer *, void *, usbd_status); | 
| 440 | void uaudio_rdata_intr(struct usbd_xfer *, void *, usbd_status); | 
| 441 | void uaudio_psync_intr(struct usbd_xfer *, void *, usbd_status); | 
| 442 |   | 
| 443 | #ifdef UAUDIO_DEBUG | 
| 444 | char *uaudio_isoname(int isotype); | 
| 445 | char *uaudio_modename(int mode); | 
| 446 | char *uaudio_usagename(int usage); | 
| 447 | void uaudio_rates_print(int rates); | 
| 448 | void uaudio_ranges_print(struct uaudio_ranges *r); | 
| 449 | void uaudio_print_unit(struct uaudio_softc *sc, struct uaudio_unit *u); | 
| 450 | void uaudio_mixer_print(struct uaudio_softc *sc); | 
| 451 | void uaudio_conf_print(struct uaudio_softc *sc); | 
| 452 |   | 
| 453 |  | 
| 454 |   | 
| 455 |   | 
| 456 |   | 
| 457 |   | 
| 458 |   | 
| 459 | int uaudio_debug = 1; | 
| 460 | #endif | 
| 461 |   | 
| 462 | struct cfdriver uaudio_cd = { | 
| 463 | 	NULL, "uaudio", DV_DULL | 
| 464 | }; | 
| 465 |   | 
| 466 | const struct cfattach uaudio_ca = { | 
| 467 | 	sizeof(struct uaudio_softc), uaudio_match, uaudio_attach, uaudio_detach | 
| 468 | }; | 
| 469 |   | 
| 470 | const struct audio_hw_if uaudio_hw_if = { | 
| 471 | 	.open = uaudio_open, | 
| 472 | 	.close = uaudio_close, | 
| 473 | 	.set_params = uaudio_set_params, | 
| 474 | 	.halt_output = uaudio_halt_output, | 
| 475 | 	.halt_input = uaudio_halt_input, | 
| 476 | 	.set_port = uaudio_set_port, | 
| 477 | 	.get_port = uaudio_get_port, | 
| 478 | 	.query_devinfo = uaudio_query_devinfo, | 
| 479 | 	.trigger_output = uaudio_trigger_output, | 
| 480 | 	.trigger_input = uaudio_trigger_input, | 
| 481 | 	.copy_output = uaudio_copy_output, | 
| 482 | 	.underrun = uaudio_underrun, | 
| 483 | 	.set_blksz = uaudio_set_blksz, | 
| 484 | }; | 
| 485 |   | 
| 486 |  | 
| 487 |   | 
| 488 |   | 
| 489 |   | 
| 490 |   | 
| 491 | const int uaudio_rates[] = { | 
| 492 | 	8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, | 
| 493 | 	64000, 88200, 96000, 128000, 176400, 192000 | 
| 494 | }; | 
| 495 |   | 
| 496 |  | 
| 497 |   | 
| 498 |   | 
| 499 |   | 
| 500 | int | 
| 501 | uaudio_sign_expand(unsigned int val, int opsize) | 
| 502 | { | 
| 503 | 	unsigned int s; | 
| 504 |   | 
| 505 | 	s = 1 << (8 * opsize - 1); | 
| 506 | 	return (val ^ s) - s; | 
| 507 | } | 
| 508 |   | 
| 509 | int | 
| 510 | uaudio_req(struct uaudio_softc *sc, | 
| 511 |     unsigned int type, | 
| 512 |     unsigned int req, | 
| 513 |     unsigned int sel, | 
| 514 |     unsigned int chan, | 
| 515 |     unsigned int ifnum, | 
| 516 |     unsigned int id, | 
| 517 |     unsigned char *buf, | 
| 518 |     size_t size) | 
| 519 | { | 
| 520 | 	struct usb_device_request r; | 
| 521 | 	int err; | 
| 522 |   | 
| 523 | 	r.bmRequestType = type; | 
| 524 | 	r.bRequest = req; | 
| 525 | 	USETW(r.wValue, sel << 8 | chan); | 
| 526 | 	USETW(r.wIndex, id << 8 | ifnum); | 
| 527 | 	USETW(r.wLength, size); | 
| 528 |   | 
| 529 | 	DPRINTF("%s: type = 0x%x, req = 0x%x, val = 0x%x, " | 
| 530 | 	    "index = 0x%x, size = %d\n", __func__, | 
| 531 | 	    type, req, UGETW(r.wValue), UGETW(r.wIndex), UGETW(r.wLength)); | 
| 532 |   | 
| 533 | 	err = usbd_do_request(sc->udev, &r, buf); | 
| 534 | 	if (err) { | 
| 535 | 		DPRINTF("%s: failed: %s\n", __func__, usbd_errstr(err)); | 
| 536 | 		return 0; | 
| 537 | 	} | 
| 538 | 	return 1; | 
| 539 | } | 
| 540 |   | 
| 541 |  | 
| 542 |   | 
| 543 |   | 
| 544 |   | 
| 545 | int | 
| 546 | uaudio_getnum(struct uaudio_blob *p, unsigned int size, unsigned int *ret) | 
| 547 | { | 
| 548 | 	unsigned int i, num = 0; | 
| 549 |   | 
| 550 | 	if (p->wptr - p->rptr < size) { | 
| 551 | 		DPRINTF("%s: %d: too small\n", __func__, size); | 
| 552 | 		return 0; | 
| 553 | 	} | 
| 554 |   | 
| 555 | 	for (i = 0; i < size; i++) | 
| 556 | 		num |= *p->rptr++ << (8 * i); | 
| 557 |   | 
| 558 | 	if (ret) | 
| 559 | 		*ret = num; | 
| 560 | 	return 1; | 
| 561 | } | 
| 562 |   | 
| 563 |  | 
| 564 |   | 
| 565 |   | 
| 566 | int | 
| 567 | uaudio_getdesc(struct uaudio_blob *p, struct uaudio_blob *ret) | 
| 568 | { | 
| 569 | 	unsigned int size; | 
| 570 |   | 
| 571 | 	if (!uaudio_getnum(p, 1, &size)) | 
| 572 | 		return 0; | 
| 573 | 	if (size-- == 0) { | 
| 574 | 		DPRINTF("%s: zero sized desc\n", __func__); | 
| 575 | 		return 0; | 
| 576 | 	} | 
| 577 | 	if (p->wptr - p->rptr < size) { | 
| 578 | 		DPRINTF("%s: too small\n", __func__); | 
| 579 | 		return 0; | 
| 580 | 	} | 
| 581 | 	ret->rptr = p->rptr; | 
| 582 | 	ret->wptr = p->rptr + size; | 
| 583 | 	p->rptr += size; | 
| 584 | 	return 1; | 
| 585 | } | 
| 586 |   | 
| 587 |  | 
| 588 |   | 
| 589 |   | 
| 590 | struct uaudio_unit * | 
| 591 | uaudio_unit_byid(struct uaudio_softc *sc, unsigned int id) | 
| 592 | { | 
| 593 | 	struct uaudio_unit *u; | 
| 594 |   | 
| 595 | 	for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 596 | 		if (u->id == id) | 
| 597 | 			break; | 
| 598 | 	} | 
| 599 | 	return u; | 
| 600 | } | 
| 601 |   | 
| 602 |  | 
| 603 |   | 
| 604 |   | 
| 605 | char * | 
| 606 | uaudio_tname(struct uaudio_softc *sc, unsigned int type, int isout) | 
| 607 | { | 
| 608 | 	unsigned int hi, lo; | 
| 609 | 	char *name; | 
| 610 |   | 
| 611 | 	hi = type >> 8; | 
| 612 | 	lo = type & 0xff; | 
| 613 |   | 
| 614 | 	 | 
| 615 | 	if (hi == 1) | 
| 616 | 		return isout ? UAUDIO_NAME_REC : UAUDIO_NAME_PLAY; | 
| 617 |   | 
| 618 | 	 | 
| 619 | 	if (isout) { | 
| 620 | 		if (sc->nout == 1) | 
| 621 | 			return "output"; | 
| 622 | 	} else { | 
| 623 | 		if (sc->nin == 1) | 
| 624 | 			return "input"; | 
| 625 | 	} | 
| 626 |   | 
| 627 | 	 | 
| 628 | 	switch (hi) { | 
| 629 | 	case 2: | 
| 630 | 		 | 
| 631 | 		name = isout ? "mic-out" : "mic"; | 
| 632 | 		break; | 
| 633 | 	case 3: | 
| 634 | 		 | 
| 635 | 		switch (lo) { | 
| 636 | 		case 0x02: | 
| 637 | 			name = isout ? "hp" : "hp-in"; | 
| 638 | 			break; | 
| 639 | 		default: | 
| 640 | 			name = isout ? "spkr" : "spkr-in"; | 
| 641 | 			break; | 
| 642 | 		} | 
| 643 | 		break; | 
| 644 | 	case 4: | 
| 645 | 		 | 
| 646 | 		name = isout ? "spkr" : "mic"; | 
| 647 | 		break; | 
| 648 | 	case 5: | 
| 649 | 		 | 
| 650 | 		name = isout ? "phone-in" : "phone-out"; | 
| 651 | 		break; | 
| 652 | 	case 6: | 
| 653 | 		 | 
| 654 | 		switch (lo) { | 
| 655 | 		case 0x02: | 
| 656 | 		case 0x05: | 
| 657 | 		case 0x06: | 
| 658 | 		case 0x07: | 
| 659 | 		case 0x09: | 
| 660 | 		case 0x0a: | 
| 661 | 			name = isout ? "dig-out" : "dig-in"; | 
| 662 | 			break; | 
| 663 | 		default: | 
| 664 | 			name = isout ? "line-out" : "line-in"; | 
| 665 | 			break; | 
| 666 | 		} | 
| 667 | 		break; | 
| 668 | 	case 7: | 
| 669 | 		 | 
| 670 | 		name = isout ? "int-out" : "int-in"; | 
| 671 | 		break; | 
| 672 | 	default: | 
| 673 | 		name = isout ? "unk-out" : "unk-in"; | 
| 674 | 	} | 
| 675 | 	return name; | 
| 676 | } | 
| 677 |   | 
| 678 |  | 
| 679 |   | 
| 680 |   | 
| 681 | char * | 
| 682 | uaudio_clkname(unsigned int attr) | 
| 683 | { | 
| 684 | 	static char *names[] = {"ext", "fixed", "var", "prog"}; | 
| 685 |   | 
| 686 | 	return names[attr & 3]; | 
| 687 | } | 
| 688 |   | 
| 689 |  | 
| 690 |   | 
| 691 |   | 
| 692 | void | 
| 693 | uaudio_mkname(struct uaudio_softc *sc, char *templ, char *res) | 
| 694 | { | 
| 695 | 	struct uaudio_name *n; | 
| 696 | 	char *sep; | 
| 697 |   | 
| 698 | 	 | 
| 699 | 	  | 
| 700 | 	  | 
| 701 | 	  | 
| 702 | 	  | 
| 703 | 	sep = strchr(templ, '_') != NULL ? "_" : ""; | 
| 704 |   | 
| 705 | 	n = sc->names; | 
| 706 | 	while (1) { | 
| 707 | 		if (n == NULL) { | 
| 708 | 			n = malloc(sizeof(struct uaudio_name), | 
| 709 | 			    M_USBDEV, M_WAITOK); | 
| 710 | 			n->templ = templ; | 
| 711 | 			n->unit = 0; | 
| 712 | 			n->next = sc->names; | 
| 713 | 			sc->names = n; | 
| 714 | 		} | 
| 715 | 		if (strcmp(n->templ, templ) == 0) | 
| 716 | 			break; | 
| 717 | 		n = n->next; | 
| 718 | 	} | 
| 719 | 	if (n->unit == 0) | 
| 720 | 		snprintf(res, UAUDIO_NAMEMAX, "%s", templ); | 
| 721 | 	else | 
| 722 | 		snprintf(res, UAUDIO_NAMEMAX, "%s%s%u", templ, sep, n->unit); | 
| 723 | 	n->unit++; | 
| 724 | } | 
| 725 |   | 
| 726 |  | 
| 727 |   | 
| 728 |   | 
| 729 | unsigned int | 
| 730 | uaudio_feature_fixup(struct uaudio_softc *sc, unsigned int ctl) | 
| 731 | { | 
| 732 | 	int i; | 
| 733 | 	unsigned int bits, n; | 
| 734 |   | 
| 735 | 	switch (sc->version) { | 
| 736 | 	case UAUDIO_V1: | 
| 737 | 		n = 0; | 
| 738 | 		for (i = 0; i < 16; i++) { | 
| 739 | 			bits = (ctl >> i) & 1; | 
| 740 | 			if (bits) | 
| 741 | 				bits |= 2; | 
| 742 | 			n |= bits << (2 * i); | 
| 743 | 		} | 
| 744 | 		return n; | 
| 745 | 	case UAUDIO_V2: | 
| 746 | 		break; | 
| 747 | 	} | 
| 748 | 	return ctl; | 
| 749 | } | 
| 750 |   | 
| 751 |  | 
| 752 |   | 
| 753 |   | 
| 754 | void | 
| 755 | uaudio_ranges_init(struct uaudio_ranges *r) | 
| 756 | { | 
| 757 | 	r->el = NULL; | 
| 758 | 	r->nval = 0; | 
| 759 | } | 
| 760 |   | 
| 761 |  | 
| 762 |   | 
| 763 |   | 
| 764 |   | 
| 765 |   | 
| 766 | void | 
| 767 | uaudio_ranges_add(struct uaudio_ranges *r, int min, int max, int res) | 
| 768 | { | 
| 769 | 	struct uaudio_ranges_el *e, **pe; | 
| 770 |   | 
| 771 | 	if (min > max) { | 
| 772 | 		DPRINTF("%s: [%d:%d]/%d: bad range\n", __func__, | 
| 773 | 		    min, max, res); | 
| 774 | 		return; | 
| 775 | 	} | 
| 776 |   | 
| 777 | 	for (pe = &r->el; (e = *pe) != NULL; pe = &e->next) { | 
| 778 | 		if (min <= e->max && max >= e->min) { | 
| 779 | 			DPRINTF("%s: overlapping ranges\n", __func__); | 
| 780 | 			return; | 
| 781 | 		} | 
| 782 | 		if (min < e->max) | 
| 783 | 			break; | 
| 784 | 	} | 
| 785 |   | 
| 786 | 	 | 
| 787 | 	r->nval += max - min + 1; | 
| 788 |   | 
| 789 | 	e = malloc(sizeof(struct uaudio_ranges_el), M_USBDEV, M_WAITOK); | 
| 790 | 	e->min = min; | 
| 791 | 	e->max = max; | 
| 792 | 	e->res = res; | 
| 793 | 	e->next = *pe; | 
| 794 | 	*pe = e; | 
| 795 | } | 
| 796 |   | 
| 797 |  | 
| 798 |   | 
| 799 |   | 
| 800 | void | 
| 801 | uaudio_ranges_clear(struct uaudio_ranges *r) | 
| 802 | { | 
| 803 | 	struct uaudio_ranges_el *e; | 
| 804 |   | 
| 805 | 	while ((e = r->el) != NULL) { | 
| 806 | 		r->el = e->next; | 
| 807 | 		free(e, M_USBDEV, sizeof(struct uaudio_ranges_el)); | 
| 808 | 	} | 
| 809 | 	r->nval = 0; | 
| 810 | } | 
| 811 |   | 
| 812 |  | 
| 813 |   | 
| 814 |   | 
| 815 |   | 
| 816 | int | 
| 817 | uaudio_ranges_decode(struct uaudio_ranges *r, int val) | 
| 818 | { | 
| 819 | 	struct uaudio_ranges_el *e; | 
| 820 | 	int diff, pos; | 
| 821 |   | 
| 822 | 	pos = 0; | 
| 823 |   | 
| 824 | 	for (e = r->el; e != NULL; e = e->next) { | 
| 825 | 		if (val >= e->min && val <= e->max) { | 
| 826 | 			pos += val - e->min; | 
| 827 | 			return (r->nval == 1) ? 0 : | 
| 828 | 			    (pos * 255 + (r->nval - 1) / 2) / (r->nval - 1); | 
| 829 | 		} | 
| 830 | 		diff = e->max - e->min + 1; | 
| 831 | 		pos += diff; | 
| 832 | 	} | 
| 833 | 	return 0; | 
| 834 | } | 
| 835 |   | 
| 836 |  | 
| 837 |   | 
| 838 |   | 
| 839 |   | 
| 840 | unsigned int | 
| 841 | uaudio_ranges_encode(struct uaudio_ranges *r, int val) | 
| 842 | { | 
| 843 | 	struct uaudio_ranges_el *e; | 
| 844 | 	int diff, pos; | 
| 845 |   | 
| 846 | 	pos = (val * (r->nval - 1) + 127) / 255; | 
| 847 |   | 
| 848 | 	for (e = r->el; e != NULL; e = e->next) { | 
| 849 | 		diff = e->max - e->min + 1; | 
| 850 | 		if (pos < diff) | 
| 851 | 			return e->min + pos; | 
| 852 | 		pos -= diff; | 
| 853 | 	} | 
| 854 | 	return 0; | 
| 855 | } | 
| 856 |   | 
| 857 |  | 
| 858 |   | 
| 859 |   | 
| 860 |   | 
| 861 |   | 
| 862 | int | 
| 863 | uaudio_ranges_getrates(struct uaudio_ranges *r, | 
| 864 |     unsigned int mult, unsigned int div) | 
| 865 | { | 
| 866 | 	struct uaudio_ranges_el *e; | 
| 867 | 	int rates, i, v; | 
| 868 |   | 
| 869 | 	rates = 0; | 
| 870 |   | 
| 871 | 	for (e = r->el; e != NULL; e = e->next) { | 
| 872 | 		for (i = 0; i < nitems(uaudio_rates); i++) { | 
| 873 | 			v = (unsigned long long)uaudio_rates[i] * mult / div; | 
| 874 | 			if (v < e->min || v > e->max) | 
| 875 | 				continue; | 
| 876 | 			if (e->res == 0 || v - e->min % e->res == 0) | 
| 877 | 				rates |= 1 << i; | 
| 878 | 		} | 
| 879 | 	} | 
| 880 |   | 
| 881 | 	return rates; | 
| 882 | } | 
| 883 |   | 
| 884 |  | 
| 885 |   | 
| 886 |   | 
| 887 |   | 
| 888 | int | 
| 889 | uaudio_rates_indexof(int mask, int rate) | 
| 890 | { | 
| 891 | 	int i, diff, best_index, best_diff; | 
| 892 |   | 
| 893 | 	best_index = -1; | 
| 894 | 	best_diff = INT_MAX; | 
| 895 | 	for (i = 0; i < nitems(uaudio_rates); i++) { | 
| 896 | 		if ((mask & (1 << i)) == 0) | 
| 897 | 			continue; | 
| 898 | 		diff = uaudio_rates[i] - rate; | 
| 899 | 		if (diff < 0) | 
| 900 | 			diff = -diff; | 
| 901 | 		if (diff < best_diff) { | 
| 902 | 			best_index = i; | 
| 903 | 			best_diff = diff; | 
| 904 | 		} | 
| 905 | 	} | 
| 906 | 	return best_index; | 
| 907 | } | 
| 908 |   | 
| 909 |  | 
| 910 |   | 
| 911 |   | 
| 912 |   | 
| 913 |   | 
| 914 | int | 
| 915 | uaudio_req_ranges(struct uaudio_softc *sc, | 
| 916 |     unsigned int opsize, | 
| 917 |     unsigned int sel, | 
| 918 |     unsigned int chan, | 
| 919 |     unsigned int ifnum, | 
| 920 |     unsigned int id, | 
| 921 |     struct uaudio_ranges *r) | 
| 922 | { | 
| 923 | 	unsigned char req_buf[16], *req = NULL; | 
| 924 | 	size_t req_size; | 
| 925 | 	struct uaudio_blob p; | 
| 926 | 	unsigned int count, min, max, res; | 
| 927 | 	int i; | 
| 928 |   | 
| 929 | 	switch (sc->version) { | 
| 930 | 	case UAUDIO_V1: | 
| 931 | 		count = 1; | 
| 932 | 		req = req_buf; | 
| 933 | 		p.rptr = p.wptr = req; | 
| 934 | 		if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE, | 
| 935 | 			UAUDIO_V1_REQ_GET_MIN, sel, chan, | 
| 936 | 			ifnum, id, p.wptr, opsize)) | 
| 937 | 			return 0; | 
| 938 | 		p.wptr += opsize; | 
| 939 | 		if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE, | 
| 940 | 			UAUDIO_V1_REQ_GET_MAX, sel, chan, | 
| 941 | 			ifnum, id, p.wptr, opsize)) | 
| 942 | 			return 0; | 
| 943 | 		p.wptr += opsize; | 
| 944 | 		if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE, | 
| 945 | 			UAUDIO_V1_REQ_GET_RES, sel, chan, | 
| 946 | 			ifnum, id, p.wptr, opsize)) | 
| 947 | 			return 0; | 
| 948 | 		p.wptr += opsize; | 
| 949 | 		break; | 
| 950 | 	case UAUDIO_V2: | 
| 951 | 		 | 
| 952 | 		if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE, | 
| 953 | 			UAUDIO_V2_REQ_RANGES, sel, chan, | 
| 954 | 			ifnum, id, req_buf, 2)) | 
| 955 | 			return 0; | 
| 956 |   | 
| 957 | 		 | 
| 958 | 		count = req_buf[0] | req_buf[1] << 8; | 
| 959 |   | 
| 960 | 		 | 
| 961 | 		req_size = 2 + 3 * opsize * count; | 
| 962 | 		if (sizeof(req_buf) >= req_size) | 
| 963 | 			req = req_buf; | 
| 964 | 		else | 
| 965 | 			req = malloc(req_size, M_USBDEV, M_WAITOK); | 
| 966 |   | 
| 967 | 		p.rptr = p.wptr = req; | 
| 968 | 		if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE, | 
| 969 | 			UAUDIO_V2_REQ_RANGES, sel, chan, | 
| 970 | 			ifnum, id, p.wptr, req_size)) | 
| 971 | 			return 0; | 
| 972 | 		p.wptr += req_size; | 
| 973 |   | 
| 974 | 		 | 
| 975 | 		p.rptr += 2; | 
| 976 | 		break; | 
| 977 | 	} | 
| 978 |   | 
| 979 | 	for (i = 0; i < count; i++) { | 
| 980 | 		if (!uaudio_getnum(&p, opsize, &min)) | 
| 981 | 			return 0; | 
| 982 | 		if (!uaudio_getnum(&p, opsize, &max)) | 
| 983 | 			return 0; | 
| 984 | 		if (!uaudio_getnum(&p, opsize, &res)) | 
| 985 | 			return 0; | 
| 986 | 		uaudio_ranges_add(r, | 
| 987 | 		    uaudio_sign_expand(min, opsize), | 
| 988 | 		    uaudio_sign_expand(max, opsize), | 
| 989 | 		    uaudio_sign_expand(res, opsize)); | 
| 990 | 	} | 
| 991 |   | 
| 992 | 	if (req != req_buf) | 
| 993 | 		free(req, M_USBDEV, req_size); | 
| 994 |   | 
| 995 | 	return 1; | 
| 996 | } | 
| 997 |   | 
| 998 |  | 
| 999 |   | 
| 1000 |   | 
| 1001 | int | 
| 1002 | uaudio_alt_getrates(struct uaudio_softc *sc, struct uaudio_alt *p) | 
| 1003 | { | 
| 1004 | 	struct uaudio_unit *u; | 
| 1005 | 	unsigned int mult = 1, div = 1; | 
| 1006 |   | 
| 1007 | 	switch (sc->version) { | 
| 1008 | 	case UAUDIO_V1: | 
| 1009 | 		return p->v1_rates; | 
| 1010 | 	case UAUDIO_V2: | 
| 1011 | 		u = sc->clock; | 
| 1012 | 		while (1) { | 
| 1013 | 			switch (u->type) { | 
| 1014 | 			case UAUDIO_AC_CLKSRC: | 
| 1015 | 				return uaudio_ranges_getrates(&u->rates, | 
| 1016 | 				    mult, div); | 
| 1017 | 			case UAUDIO_AC_CLKSEL: | 
| 1018 | 				u = u->clock; | 
| 1019 | 				break; | 
| 1020 | 			case UAUDIO_AC_CLKMULT: | 
| 1021 | 			case UAUDIO_AC_RATECONV: | 
| 1022 | 				 | 
| 1023 | 				u = u->src_list; | 
| 1024 | 				break; | 
| 1025 | 			default: | 
| 1026 | 				DPRINTF("%s: no clock\n", __func__); | 
| 1027 | 				return 0; | 
| 1028 | 			} | 
| 1029 | 		} | 
| 1030 | 	} | 
| 1031 | 	return 0; | 
| 1032 | } | 
| 1033 |   | 
| 1034 |  | 
| 1035 |   | 
| 1036 |   | 
| 1037 | int | 
| 1038 | uaudio_clock_id(struct uaudio_softc *sc) | 
| 1039 | { | 
| 1040 | 	struct uaudio_unit *u; | 
| 1041 |   | 
| 1042 | 	u = sc->clock; | 
| 1043 | 	while (1) { | 
| 1044 | 		if (u == NULL) { | 
| 1045 | 			DPRINTF("%s: NULL clock pointer\n", __func__); | 
| 1046 | 			return -1; | 
| 1047 | 		} | 
| 1048 | 		switch (u->type) { | 
| 1049 | 		case UAUDIO_AC_CLKSRC: | 
| 1050 | 			return u->id; | 
| 1051 | 		case UAUDIO_AC_CLKSEL: | 
| 1052 | 			u = u->clock; | 
| 1053 | 			break; | 
| 1054 | 		case UAUDIO_AC_CLKMULT: | 
| 1055 | 		case UAUDIO_AC_RATECONV: | 
| 1056 | 			u = u->src_list; | 
| 1057 | 			break; | 
| 1058 | 		default: | 
| 1059 | 			DPRINTF("%s: no clock\n", __func__); | 
| 1060 | 			return -1; | 
| 1061 | 		} | 
| 1062 | 	} | 
| 1063 | } | 
| 1064 |   | 
| 1065 |  | 
| 1066 |   | 
| 1067 |   | 
| 1068 | int | 
| 1069 | uaudio_getrates(struct uaudio_softc *sc, struct uaudio_params *p) | 
| 1070 | { | 
| 1071 | 	switch (sc->version) { | 
| 1072 | 	case UAUDIO_V1: | 
| 1073 | 		return p->v1_rates; | 
| 1074 | 	case UAUDIO_V2: | 
| 1075 | 		return uaudio_alt_getrates(sc, p->palt ? p->palt : p->ralt); | 
| 1076 | 	} | 
| 1077 | 	return 0; | 
| 1078 | } | 
| 1079 |   | 
| 1080 |  | 
| 1081 |   | 
| 1082 |   | 
| 1083 | void | 
| 1084 | uaudio_feature_addent(struct uaudio_softc *sc, | 
| 1085 |     struct uaudio_unit *u, int uac_type, int chan) | 
| 1086 | { | 
| 1087 | 	static struct { | 
| 1088 | 		char *name; | 
| 1089 | 		int mix_type; | 
| 1090 | 		int req_sel; | 
| 1091 | 	} features[] = { | 
| 1092 | 		{"mute", UAUDIO_MIX_SW, UAUDIO_REQSEL_MUTE}, | 
| 1093 | 		{"level", UAUDIO_MIX_NUM, UAUDIO_REQSEL_VOLUME}, | 
| 1094 | 		{"bass", UAUDIO_MIX_NUM, UAUDIO_REQSEL_BASS}, | 
| 1095 | 		{"mid", UAUDIO_MIX_NUM, UAUDIO_REQSEL_MID}, | 
| 1096 | 		{"treble", UAUDIO_MIX_NUM, UAUDIO_REQSEL_TREBLE}, | 
| 1097 | 		{"eq", UAUDIO_MIX_NUM, UAUDIO_REQSEL_EQ}, | 
| 1098 | 		{"agc", UAUDIO_MIX_SW, UAUDIO_REQSEL_AGC}, | 
| 1099 | 		{NULL, -1, -1},			 | 
| 1100 | 		{"bassboost", UAUDIO_MIX_SW, UAUDIO_REQSEL_BASSBOOST}, | 
| 1101 | 		{"loud", UAUDIO_MIX_SW, UAUDIO_REQSEL_LOUDNESS}, | 
| 1102 | 		{"gain", UAUDIO_MIX_NUM, UAUDIO_REQSEL_GAIN}, | 
| 1103 | 		{"gainpad", UAUDIO_MIX_SW, UAUDIO_REQSEL_GAINPAD}, | 
| 1104 | 		{"phase", UAUDIO_MIX_SW, UAUDIO_REQSEL_PHASEINV}, | 
| 1105 | 		{NULL, -1, -1},			 | 
| 1106 | 		{NULL, -1, -1}			 | 
| 1107 | 	}; | 
| 1108 | 	struct uaudio_mixent *m, *i, **pi; | 
| 1109 | 	int cmp; | 
| 1110 |   | 
| 1111 | 	if (uac_type >= sizeof(features) / sizeof(features[0])) { | 
| 1112 | 		printf("%s: skipped unknown feature\n", DEVNAME(sc)); | 
| 1113 | 		return; | 
| 1114 | 	} | 
| 1115 |   | 
| 1116 | 	m = malloc(sizeof(struct uaudio_mixent), M_USBDEV, M_WAITOK); | 
| 1117 | 	m->chan = chan; | 
| 1118 | 	m->fname = features[uac_type].name; | 
| 1119 | 	m->type = features[uac_type].mix_type; | 
| 1120 | 	m->req_sel = features[uac_type].req_sel; | 
| 1121 | 	uaudio_ranges_init(&m->ranges); | 
| 1122 |   | 
| 1123 | 	if (m->type == UAUDIO_MIX_NUM) { | 
| 1124 | 		if (!uaudio_req_ranges(sc, 2, | 
| 1125 | 			m->req_sel, chan < 0 ? 0 : chan + 1, | 
| 1126 | 			sc->ctl_ifnum, u->id, | 
| 1127 | 			&m->ranges)) { | 
| 1128 | 			printf("%s: failed to get ranges for %s control\n", | 
| 1129 | 			    DEVNAME(sc), m->fname); | 
| 1130 | 			free(m, M_USBDEV, sizeof(struct uaudio_mixent)); | 
| 1131 | 			return; | 
| 1132 | 		} | 
| 1133 | 		if (m->ranges.el == NULL) { | 
| 1134 | 			printf("%s: skipped %s control with empty range\n", | 
| 1135 | 			    DEVNAME(sc), m->fname); | 
| 1136 | 			free(m, M_USBDEV, sizeof(struct uaudio_mixent)); | 
| 1137 | 			return; | 
| 1138 | 		} | 
| 1139 | #ifdef UAUDIO_DEBUG | 
| 1140 | 		if (uaudio_debug) | 
| 1141 | 			uaudio_ranges_print(&m->ranges); | 
| 1142 | #endif | 
| 1143 | 	} | 
| 1144 |   | 
| 1145 | 	 | 
| 1146 | 	  | 
| 1147 | 	  | 
| 1148 | 	  | 
| 1149 | 	for (pi = &u->mixent_list; (i = *pi) != NULL; pi = &i->next) { | 
| 1150 | 		cmp = strcmp(i->fname, m->fname); | 
| 1151 | 		if (cmp == 0) | 
| 1152 | 			cmp = i->chan - m->chan; | 
| 1153 | 		if (cmp == 0) { | 
| 1154 | 			DPRINTF("%02u: %s.%s: duplicate feature for chan %d\n", | 
| 1155 | 			    u->id, u->name, m->fname, m->chan); | 
| 1156 | 			free(m, M_USBDEV, sizeof(struct uaudio_mixent)); | 
| 1157 | 			return; | 
| 1158 | 		} | 
| 1159 | 		if (cmp > 0) | 
| 1160 | 			break; | 
| 1161 | 	} | 
| 1162 | 	m->next = *pi; | 
| 1163 | 	*pi = m; | 
| 1164 |   | 
| 1165 | 	DPRINTF("\t%s[%d]\n", m->fname, m->chan); | 
| 1166 | } | 
| 1167 |   | 
| 1168 |  | 
| 1169 |   | 
| 1170 |   | 
| 1171 |   | 
| 1172 | int | 
| 1173 | uaudio_process_srcs(struct uaudio_softc *sc, | 
| 1174 | 	struct uaudio_unit *u, struct uaudio_blob units, | 
| 1175 | 	struct uaudio_blob *p) | 
| 1176 | { | 
| 1177 | 	struct uaudio_unit *s, **ps; | 
| 1178 | 	unsigned int i, npin, sid; | 
| 1179 |   | 
| 1180 | 	if (!uaudio_getnum(p, 1, &npin)) | 
| 1181 | 		return 0; | 
| 1182 | 	ps = &u->src_list; | 
| 1183 | 	for (i = 0; i < npin; i++) { | 
| 1184 | 		if (!uaudio_getnum(p, 1, &sid)) | 
| 1185 | 			return 0; | 
| 1186 | 		if (!uaudio_process_unit(sc, u, sid, units, &s)) | 
| 1187 | 			return 0; | 
| 1188 | 		s->src_next = NULL; | 
| 1189 | 		*ps = s; | 
| 1190 | 		ps = &s->src_next; | 
| 1191 | 	} | 
| 1192 | 	return 1; | 
| 1193 | } | 
| 1194 |   | 
| 1195 |  | 
| 1196 |   | 
| 1197 |   | 
| 1198 | int | 
| 1199 | uaudio_process_nch(struct uaudio_softc *sc, | 
| 1200 | 	struct uaudio_unit *u, struct uaudio_blob *p) | 
| 1201 | { | 
| 1202 | 	if (!uaudio_getnum(p, 1, &u->nch)) | 
| 1203 | 		return 0; | 
| 1204 | 	 | 
| 1205 | 	switch (sc->version) { | 
| 1206 | 	case UAUDIO_V1: | 
| 1207 | 		if (!uaudio_getnum(p, 2, NULL))  | 
| 1208 | 			return 0; | 
| 1209 | 		break; | 
| 1210 | 	case UAUDIO_V2: | 
| 1211 | 		if (!uaudio_getnum(p, 4, NULL))  | 
| 1212 | 			return 0; | 
| 1213 | 		break; | 
| 1214 | 	} | 
| 1215 | 	if (!uaudio_getnum(p, 1, NULL))  | 
| 1216 | 		return 0; | 
| 1217 | 	return 1; | 
| 1218 | } | 
| 1219 |   | 
| 1220 |  | 
| 1221 |   | 
| 1222 |   | 
| 1223 | int | 
| 1224 | uaudio_unit_getdesc(struct uaudio_softc *sc, int id, | 
| 1225 | 	struct uaudio_blob units, | 
| 1226 | 	struct uaudio_blob *p, | 
| 1227 | 	unsigned int *rtype) | 
| 1228 | { | 
| 1229 | 	unsigned int i, type, subtype; | 
| 1230 |   | 
| 1231 | 	 | 
| 1232 | 	  | 
| 1233 | 	  | 
| 1234 | 	while (1) { | 
| 1235 | 		if (units.rptr == units.wptr) { | 
| 1236 | 			DPRINTF("%s: %02u: not found\n", __func__, id); | 
| 1237 | 			return 0; | 
| 1238 | 		} | 
| 1239 | 		if (!uaudio_getdesc(&units, p)) | 
| 1240 | 			return 0; | 
| 1241 | 		if (!uaudio_getnum(p, 1, &type)) | 
| 1242 | 			return 0; | 
| 1243 | 		if (!uaudio_getnum(p, 1, &subtype)) | 
| 1244 | 			return 0; | 
| 1245 | 		if (!uaudio_getnum(p, 1, &i)) | 
| 1246 | 			return 0; | 
| 1247 | 		if (i == id) | 
| 1248 | 			break; | 
| 1249 | 	} | 
| 1250 | 	*rtype = subtype; | 
| 1251 | 	return 1; | 
| 1252 | } | 
| 1253 |   | 
| 1254 |  | 
| 1255 |   | 
| 1256 |   | 
| 1257 |   | 
| 1258 | int | 
| 1259 | uaudio_process_unit(struct uaudio_softc *sc, | 
| 1260 | 	struct uaudio_unit *dest, int id, | 
| 1261 | 	struct uaudio_blob units, | 
| 1262 | 	struct uaudio_unit **rchild) | 
| 1263 | { | 
| 1264 | 	struct uaudio_blob p; | 
| 1265 | 	struct uaudio_unit *u, *s; | 
| 1266 | 	unsigned int i, j, size, ctl, type, subtype, assoc, clk; | 
| 1267 | #ifdef UAUDIO_DEBUG | 
| 1268 | 	unsigned int bit; | 
| 1269 | #endif | 
| 1270 |   | 
| 1271 | 	if (!uaudio_unit_getdesc(sc, id, units, &p, &subtype)) | 
| 1272 | 		return 0; | 
| 1273 |   | 
| 1274 | 	 | 
| 1275 | 	  | 
| 1276 | 	  | 
| 1277 | 	  | 
| 1278 | 	u = uaudio_unit_byid(sc, id); | 
| 1279 | 	if (u == NULL) { | 
| 1280 | 		u = malloc(sizeof(struct uaudio_unit), M_USBDEV, M_WAITOK); | 
| 1281 | 		u->id = id; | 
| 1282 | 		u->type = subtype; | 
| 1283 | 		u->term = 0; | 
| 1284 | 		u->src_list = NULL; | 
| 1285 | 		u->dst_list = NULL; | 
| 1286 | 		u->clock = NULL; | 
| 1287 | 		u->mixent_list = NULL; | 
| 1288 | 		u->nch = 0; | 
| 1289 | 		u->name[0] = 0; | 
| 1290 | 		uaudio_ranges_init(&u->rates); | 
| 1291 | 		u->unit_next = sc->unit_list; | 
| 1292 | 		sc->unit_list = u; | 
| 1293 | 	} else { | 
| 1294 | 		switch (u->type) { | 
| 1295 | 		case UAUDIO_AC_CLKSRC: | 
| 1296 | 		case UAUDIO_AC_CLKSEL: | 
| 1297 | 		case UAUDIO_AC_CLKMULT: | 
| 1298 | 		case UAUDIO_AC_RATECONV: | 
| 1299 | 			 | 
| 1300 | 			*rchild = u; | 
| 1301 | 			return 1; | 
| 1302 | 		} | 
| 1303 | 	} | 
| 1304 |   | 
| 1305 | 	if (dest) { | 
| 1306 | 		dest->dst_next = u->dst_list; | 
| 1307 | 		u->dst_list = dest; | 
| 1308 | 		if (dest->dst_next != NULL) { | 
| 1309 | 			 | 
| 1310 | 			*rchild = u; | 
| 1311 | 			return 1; | 
| 1312 | 		} | 
| 1313 | 	} | 
| 1314 |   | 
| 1315 | 	switch (u->type) { | 
| 1316 | 	case UAUDIO_AC_INPUT: | 
| 1317 | 		if (!uaudio_getnum(&p, 2, &u->term)) | 
| 1318 | 			return 0; | 
| 1319 | 		if (!uaudio_getnum(&p, 1, &assoc)) | 
| 1320 | 			return 0; | 
| 1321 | 		if (u->term >> 8 != 1) | 
| 1322 | 			sc->nin++; | 
| 1323 | 		switch (sc->version) { | 
| 1324 | 		case UAUDIO_V1: | 
| 1325 | 			break; | 
| 1326 | 		case UAUDIO_V2: | 
| 1327 | 			if (!uaudio_getnum(&p, 1, &clk)) | 
| 1328 | 				return 0; | 
| 1329 | 			if (!uaudio_process_unit(sc, NULL, | 
| 1330 | 				clk, units, &u->clock)) | 
| 1331 | 				return 0; | 
| 1332 | 			break; | 
| 1333 | 		} | 
| 1334 | 		if (!uaudio_getnum(&p, 1, &u->nch)) | 
| 1335 | 			return 0; | 
| 1336 | 		DPRINTF("%02u: " | 
| 1337 | 		    "in, nch = %d, term = 0x%x, assoc = %d\n", | 
| 1338 | 		    u->id, u->nch, u->term, assoc); | 
| 1339 | 		break; | 
| 1340 | 	case UAUDIO_AC_OUTPUT: | 
| 1341 | 		if (!uaudio_getnum(&p, 2, &u->term)) | 
| 1342 | 			return 0; | 
| 1343 | 		if (!uaudio_getnum(&p, 1, &assoc)) | 
| 1344 | 			return 0; | 
| 1345 | 		if (!uaudio_getnum(&p, 1, &id)) | 
| 1346 | 			return 0; | 
| 1347 | 		if (!uaudio_process_unit(sc, u, id, units, &s)) | 
| 1348 | 			return 0; | 
| 1349 | 		if (u->term >> 8 != 1) | 
| 1350 | 			sc->nout++; | 
| 1351 | 		switch (sc->version) { | 
| 1352 | 		case UAUDIO_V1: | 
| 1353 | 			break; | 
| 1354 | 		case UAUDIO_V2: | 
| 1355 | 			if (!uaudio_getnum(&p, 1, &clk)) | 
| 1356 | 				return 0; | 
| 1357 | 			if (!uaudio_process_unit(sc, NULL, | 
| 1358 | 				clk, units, &u->clock)) | 
| 1359 | 				return 0; | 
| 1360 | 			break; | 
| 1361 | 		} | 
| 1362 | 		u->src_list = s; | 
| 1363 | 		s->src_next = NULL; | 
| 1364 | 		u->nch = s->nch; | 
| 1365 | 		DPRINTF("%02u: " | 
| 1366 | 		    "out, id = %d, nch = %d, term = 0x%x, assoc = %d\n", | 
| 1367 | 		    u->id, id, u->nch, u->term, assoc); | 
| 1368 | 		break; | 
| 1369 | 	case UAUDIO_AC_MIXER: | 
| 1370 | 		if (!uaudio_process_srcs(sc, u, units, &p)) | 
| 1371 | 			return 0; | 
| 1372 | 		if (!uaudio_process_nch(sc, u, &p)) | 
| 1373 | 			return 0; | 
| 1374 | 		DPRINTF("%02u: mixer, nch = %u:\n", u->id, u->nch); | 
| 1375 |   | 
| 1376 | #ifdef UAUDIO_DEBUG | 
| 1377 | 		 | 
| 1378 | 		  | 
| 1379 | 		  | 
| 1380 | 		  | 
| 1381 | 		  | 
| 1382 | 		  | 
| 1383 | 		  | 
| 1384 | 		  | 
| 1385 | 		  | 
| 1386 | 		bit = 0; | 
| 1387 | 		for (s = u->src_list; s != NULL; s = s->src_next) { | 
| 1388 | 			for (i = 0; i < s->nch; i++) { | 
| 1389 | 				for (j = 0; j < u->nch; j++) { | 
| 1390 | 					if ((bit++ & 7) == 0) { | 
| 1391 | 						if (!uaudio_getnum(&p, 1, &ctl)) | 
| 1392 | 							return 0; | 
| 1393 | 					} | 
| 1394 | 					if (ctl & 0x80) | 
| 1395 | 						DPRINTF("\t%02u[%d] -> [%d]\n", | 
| 1396 | 						    s->id, i, j); | 
| 1397 | 					ctl <<= 1; | 
| 1398 | 				} | 
| 1399 | 			} | 
| 1400 | 		} | 
| 1401 | #endif | 
| 1402 | 		break; | 
| 1403 | 	case UAUDIO_AC_SELECTOR: | 
| 1404 | 		 | 
| 1405 | 		  | 
| 1406 | 		  | 
| 1407 | 		if (!uaudio_process_srcs(sc, u, units, &p)) | 
| 1408 | 			return 0; | 
| 1409 | 		if (u->src_list == NULL) { | 
| 1410 | 			printf("%s: selector %02u has no sources\n", | 
| 1411 | 			    DEVNAME(sc), u->id); | 
| 1412 | 			return 0; | 
| 1413 | 		} | 
| 1414 | 		u->nch = u->src_list->nch; | 
| 1415 | 		DPRINTF("%02u: selector, nch = %u\n", u->id, u->nch); | 
| 1416 | 		break; | 
| 1417 | 	case UAUDIO_AC_FEATURE: | 
| 1418 | 		if (!uaudio_getnum(&p, 1, &id)) | 
| 1419 | 			return 0; | 
| 1420 | 		if (!uaudio_process_unit(sc, u, id, units, &s)) | 
| 1421 | 			return 0; | 
| 1422 | 		s->src_next = u->src_list; | 
| 1423 | 		u->src_list = s; | 
| 1424 | 		u->nch = s->nch; | 
| 1425 | 		switch (sc->version) { | 
| 1426 | 		case UAUDIO_V1: | 
| 1427 | 			if (!uaudio_getnum(&p, 1, &size)) | 
| 1428 | 				return 0; | 
| 1429 | 			break; | 
| 1430 | 		case UAUDIO_V2: | 
| 1431 | 			size = 4; | 
| 1432 | 			break; | 
| 1433 | 		} | 
| 1434 | 		DPRINTF("%02d: feature id = %d, nch = %d, size = %d\n", | 
| 1435 | 		    u->id, id, u->nch, size); | 
| 1436 |   | 
| 1437 | 		if (!uaudio_getnum(&p, size, &ctl)) | 
| 1438 | 			return 0; | 
| 1439 | 		ctl = uaudio_feature_fixup(sc, ctl); | 
| 1440 | 		for (i = 0; i < 16; i++) { | 
| 1441 | 			if ((ctl & 3) == 3) | 
| 1442 | 				uaudio_feature_addent(sc, u, i, -1); | 
| 1443 | 			ctl >>= 2; | 
| 1444 | 		} | 
| 1445 |   | 
| 1446 | 		 | 
| 1447 | 		  | 
| 1448 | 		  | 
| 1449 | 		if (p.wptr - p.rptr == 1) | 
| 1450 | 			break; | 
| 1451 |   | 
| 1452 | 		for (j = 0; j < u->nch; j++) { | 
| 1453 | 			if (!uaudio_getnum(&p, size, &ctl)) | 
| 1454 | 				return 0; | 
| 1455 | 			ctl = uaudio_feature_fixup(sc, ctl); | 
| 1456 | 			for (i = 0; i < 16; i++) { | 
| 1457 | 				if ((ctl & 3) == 3) | 
| 1458 | 					uaudio_feature_addent(sc, u, i, j); | 
| 1459 | 				ctl >>= 2; | 
| 1460 | 			} | 
| 1461 | 		} | 
| 1462 | 		break; | 
| 1463 | 	case UAUDIO_AC_EFFECT: | 
| 1464 | 		if (!uaudio_getnum(&p, 2, &type)) | 
| 1465 | 			return 0; | 
| 1466 | 		if (!uaudio_getnum(&p, 1, &id)) | 
| 1467 | 			return 0; | 
| 1468 | 		if (!uaudio_process_unit(sc, u, id, units, &s)) | 
| 1469 | 			return 0; | 
| 1470 | 		s->src_next = u->src_list; | 
| 1471 | 		u->src_list = s; | 
| 1472 | 		u->nch = s->nch; | 
| 1473 | 		DPRINTF("%02d: effect, type = %u, id = %d, nch = %d\n", | 
| 1474 | 		    u->id, type, id, u->nch); | 
| 1475 | 		break; | 
| 1476 | 	case UAUDIO_AC_PROCESSING: | 
| 1477 | 	case UAUDIO_AC_EXTENSION: | 
| 1478 | 		if (!uaudio_getnum(&p, 2, &type)) | 
| 1479 | 			return 0; | 
| 1480 | 		if (!uaudio_process_srcs(sc, u, units, &p)) | 
| 1481 | 			return 0; | 
| 1482 | 		if (!uaudio_process_nch(sc, u, &p)) | 
| 1483 | 			return 0; | 
| 1484 | 		DPRINTF("%02u: proc/ext, type = 0x%x, nch = %u\n", | 
| 1485 | 		    u->id, type, u->nch); | 
| 1486 | 		for (s = u->src_list; s != NULL; s = s->src_next) { | 
| 1487 | 			DPRINTF("%u:\tpin %u:\n", u->id, s->id); | 
| 1488 | 		} | 
| 1489 | 		break; | 
| 1490 | 	case UAUDIO_AC_CLKSRC: | 
| 1491 | 		if (!uaudio_getnum(&p, 1, &u->term)) | 
| 1492 | 			return 0; | 
| 1493 | 		if (!uaudio_getnum(&p, 1, &ctl)) | 
| 1494 | 			return 0; | 
| 1495 | 		DPRINTF("%02u: clock source, attr = 0x%x, ctl = 0x%x\n", | 
| 1496 | 		    u->id, u->term, ctl); | 
| 1497 | 		break; | 
| 1498 | 	case UAUDIO_AC_CLKSEL: | 
| 1499 | 		DPRINTF("%02u: clock sel\n", u->id); | 
| 1500 | 		if (!uaudio_process_srcs(sc, u, units, &p)) | 
| 1501 | 			return 0; | 
| 1502 | 		if (u->src_list == NULL) { | 
| 1503 | 			printf("%s: clock selector %02u with no srcs\n", | 
| 1504 | 			    DEVNAME(sc), u->id); | 
| 1505 | 			return 0; | 
| 1506 | 		} | 
| 1507 | 		break; | 
| 1508 | 	case UAUDIO_AC_CLKMULT: | 
| 1509 | 		DPRINTF("%02u: clock mult\n", u->id); | 
| 1510 |   | 
| 1511 | 		 | 
| 1512 | 		printf("%s: clock multiplier not supported\n", DEVNAME(sc)); | 
| 1513 | 		break; | 
| 1514 | 	case UAUDIO_AC_RATECONV: | 
| 1515 | 		DPRINTF("%02u: rate conv\n", u->id); | 
| 1516 |   | 
| 1517 | 		 | 
| 1518 | 		printf("%s: rate converter not supported\n", DEVNAME(sc)); | 
| 1519 | 		break; | 
| 1520 | 	} | 
| 1521 | 	if (rchild) | 
| 1522 | 		*rchild = u; | 
| 1523 | 	return 1; | 
| 1524 | } | 
| 1525 |   | 
| 1526 |  | 
| 1527 |   | 
| 1528 |   | 
| 1529 |   | 
| 1530 |   | 
| 1531 | int | 
| 1532 | uaudio_setname_dsts(struct uaudio_softc *sc, struct uaudio_unit *u, char *name) | 
| 1533 | { | 
| 1534 | 	struct uaudio_unit *d = u; | 
| 1535 |   | 
| 1536 | 	while (d != NULL) { | 
| 1537 | 		if (d->dst_list == NULL || d->dst_list->dst_next != NULL) | 
| 1538 | 			break; | 
| 1539 | 		d = d->dst_list; | 
| 1540 | 		if (d->src_list == NULL || d->src_list->src_next != NULL) | 
| 1541 | 			break; | 
| 1542 | 		if (d->name[0] != '\0') { | 
| 1543 | 			if (name != NULL && strcmp(name, d->name) != 0) | 
| 1544 | 				break; | 
| 1545 | 			strlcpy(u->name, d->name, UAUDIO_NAMEMAX); | 
| 1546 | 			return 1; | 
| 1547 | 		} | 
| 1548 | 	} | 
| 1549 | 	return 0; | 
| 1550 | } | 
| 1551 |   | 
| 1552 |  | 
| 1553 |   | 
| 1554 |   | 
| 1555 |   | 
| 1556 |   | 
| 1557 | int | 
| 1558 | uaudio_setname_srcs(struct uaudio_softc *sc, struct uaudio_unit *u, char *name) | 
| 1559 | { | 
| 1560 | 	struct uaudio_unit *s = u; | 
| 1561 |   | 
| 1562 | 	while (s != NULL) { | 
| 1563 | 		if (s->src_list == NULL || s->src_list->src_next != NULL) | 
| 1564 | 			break; | 
| 1565 | 		s = s->src_list; | 
| 1566 | 		if (s->dst_list == NULL || s->dst_list->dst_next != NULL) | 
| 1567 | 			break; | 
| 1568 | 		if (s->name[0] != '\0') { | 
| 1569 | 			if (name != NULL && strcmp(name, s->name) != 0) | 
| 1570 | 				break; | 
| 1571 | 			strlcpy(u->name, s->name, UAUDIO_NAMEMAX); | 
| 1572 | 			return 1; | 
| 1573 | 		} | 
| 1574 | 	} | 
| 1575 | 	return 0; | 
| 1576 | } | 
| 1577 |   | 
| 1578 |  | 
| 1579 |   | 
| 1580 |   | 
| 1581 |   | 
| 1582 |   | 
| 1583 |   | 
| 1584 | void | 
| 1585 | uaudio_setname_middle(struct uaudio_softc *sc, struct uaudio_unit *u) | 
| 1586 | { | 
| 1587 | 	struct uaudio_unit *s, *d; | 
| 1588 | 	char name[UAUDIO_NAMEMAX]; | 
| 1589 |   | 
| 1590 | 	s = u->src_list; | 
| 1591 | 	while (1) { | 
| 1592 | 		if (s == NULL) { | 
| 1593 | 			DPRINTF("%s: %02u: has no srcs\n", | 
| 1594 | 			    __func__, u->id); | 
| 1595 | 			return; | 
| 1596 | 		} | 
| 1597 | 		if (s->name[0] != '\0') | 
| 1598 | 			break; | 
| 1599 | 		s = s->src_list; | 
| 1600 | 	} | 
| 1601 |   | 
| 1602 | 	d = u->dst_list; | 
| 1603 | 	while (1) { | 
| 1604 | 		if (d == NULL) { | 
| 1605 | 			DPRINTF("%s: %02u: has no dests\n", | 
| 1606 | 			    __func__, u->id); | 
| 1607 | 			return; | 
| 1608 | 		} | 
| 1609 | 		if (d->name[0] != '\0') | 
| 1610 | 			break; | 
| 1611 | 		d = d->dst_list; | 
| 1612 | 	} | 
| 1613 |   | 
| 1614 | 	snprintf(name, UAUDIO_NAMEMAX, "%s_%s", d->name, s->name); | 
| 1615 | 	uaudio_mkname(sc, name, u->name); | 
| 1616 | } | 
| 1617 |   | 
| 1618 | #ifdef UAUDIO_DEBUG | 
| 1619 |  | 
| 1620 |   | 
| 1621 |   | 
| 1622 | char * | 
| 1623 | uaudio_isoname(int isotype) | 
| 1624 | { | 
| 1625 | 	switch (isotype) { | 
| 1626 | 	case UE_ISO_ASYNC: | 
| 1627 | 		return "async"; | 
| 1628 | 	case UE_ISO_ADAPT: | 
| 1629 | 		return "adapt"; | 
| 1630 | 	case UE_ISO_SYNC: | 
| 1631 | 		return "sync"; | 
| 1632 | 	default: | 
| 1633 | 		return "unk"; | 
| 1634 | 	} | 
| 1635 | } | 
| 1636 |   | 
| 1637 |  | 
| 1638 |   | 
| 1639 |   | 
| 1640 | char * | 
| 1641 | uaudio_modename(int mode) | 
| 1642 | { | 
| 1643 | 	switch (mode) { | 
| 1644 | 	case 0: | 
| 1645 | 		return "none"; | 
| 1646 | 	case AUMODE_PLAY: | 
| 1647 | 		return "play"; | 
| 1648 | 	case AUMODE_RECORD: | 
| 1649 | 		return "rec"; | 
| 1650 | 	case AUMODE_PLAY | AUMODE_RECORD: | 
| 1651 | 		return "duplex"; | 
| 1652 | 	default: | 
| 1653 | 		return "unk"; | 
| 1654 | 	} | 
| 1655 | } | 
| 1656 |   | 
| 1657 |  | 
| 1658 |   | 
| 1659 |   | 
| 1660 | char * | 
| 1661 | uaudio_usagename(int usage) | 
| 1662 | { | 
| 1663 | 	switch (usage) { | 
| 1664 | 	case UE_ISO_USAGE_DATA: | 
| 1665 | 		return "data"; | 
| 1666 | 	case UE_ISO_USAGE_FEEDBACK: | 
| 1667 | 		return "feed"; | 
| 1668 | 	case UE_ISO_USAGE_IMPL: | 
| 1669 | 		return "impl"; | 
| 1670 | 	default: | 
| 1671 | 		return "unk"; | 
| 1672 | 	} | 
| 1673 | } | 
| 1674 |   | 
| 1675 |  | 
| 1676 |   | 
| 1677 |   | 
| 1678 | void | 
| 1679 | uaudio_rates_print(int rates) | 
| 1680 | { | 
| 1681 | 	unsigned int i; | 
| 1682 |   | 
| 1683 | 	for (i = 0; i < nitems(uaudio_rates); i++) { | 
| 1684 | 		if (rates & (1 << i)) | 
| 1685 | 			printf(" %d", uaudio_rates[i]); | 
| 1686 | 	} | 
| 1687 | 	printf("\n"); | 
| 1688 | } | 
| 1689 |   | 
| 1690 |   | 
| 1691 |  | 
| 1692 |   | 
| 1693 |   | 
| 1694 | void | 
| 1695 | uaudio_ranges_print(struct uaudio_ranges *r) | 
| 1696 | { | 
| 1697 | 	struct uaudio_ranges_el *e; | 
| 1698 | 	int more = 0; | 
| 1699 |   | 
| 1700 | 	for (e = r->el; e != NULL; e = e->next) { | 
| 1701 | 		if (more) | 
| 1702 | 			printf(", "); | 
| 1703 | 		if (e->min == e->max) | 
| 1704 | 			printf("%d", e->min); | 
| 1705 | 		else | 
| 1706 | 			printf("[%d:%d]/%d", e->min, e->max, e->res); | 
| 1707 | 		more = 1; | 
| 1708 | 	} | 
| 1709 | 	printf(" (%d vals)\n", r->nval); | 
| 1710 | } | 
| 1711 |   | 
| 1712 |  | 
| 1713 |   | 
| 1714 |   | 
| 1715 | void | 
| 1716 | uaudio_print_unit(struct uaudio_softc *sc, struct uaudio_unit *u) | 
| 1717 | { | 
| 1718 | 	struct uaudio_unit *s; | 
| 1719 |   | 
| 1720 | 	switch (u->type) { | 
| 1721 | 	case UAUDIO_AC_INPUT: | 
| 1722 | 		printf("%02u: input <%s>, dest = %02u <%s>\n", | 
| 1723 | 		    u->id, u->name, u->dst_list->id, u->dst_list->name); | 
| 1724 | 		break; | 
| 1725 | 	case UAUDIO_AC_OUTPUT: | 
| 1726 | 		printf("%02u: output <%s>, source = %02u <%s>\n", | 
| 1727 | 		    u->id, u->name, u->src_list->id, u->src_list->name); | 
| 1728 | 		break; | 
| 1729 | 	case UAUDIO_AC_MIXER: | 
| 1730 | 		printf("%02u: mixer <%s>:\n", u->id, u->name); | 
| 1731 | 		for (s = u->src_list; s != NULL; s = s->src_next) | 
| 1732 | 			printf("%02u:\tsource %u <%s>:\n", | 
| 1733 | 			    u->id, s->id, s->name); | 
| 1734 | 		break; | 
| 1735 | 	case UAUDIO_AC_SELECTOR: | 
| 1736 | 		printf("%02u: selector <%s>:\n", u->id, u->name); | 
| 1737 | 		for (s = u->src_list; s != NULL; s = s->src_next) | 
| 1738 | 			printf("%02u:\tsource %u <%s>:\n", | 
| 1739 | 			    u->id, s->id, s->name); | 
| 1740 | 		break; | 
| 1741 | 	case UAUDIO_AC_FEATURE: | 
| 1742 | 		printf("%02u: feature <%s>, " | 
| 1743 | 		    "src = %02u <%s>, dst = %02u <%s>, cls = %d\n", | 
| 1744 | 		    u->id, u->name, | 
| 1745 | 		    u->src_list->id, u->src_list->name, | 
| 1746 | 		    u->dst_list->id, u->dst_list->name, u->mixer_class); | 
| 1747 | 		break; | 
| 1748 | 	case UAUDIO_AC_EFFECT: | 
| 1749 | 		printf("%02u: effect <%s>, " | 
| 1750 | 		    "src = %02u <%s>, dst = %02u <%s>\n", | 
| 1751 | 		    u->id, u->name, | 
| 1752 | 		    u->src_list->id, u->src_list->name, | 
| 1753 | 		    u->dst_list->id, u->dst_list->name); | 
| 1754 | 		break; | 
| 1755 | 	case UAUDIO_AC_PROCESSING: | 
| 1756 | 	case UAUDIO_AC_EXTENSION: | 
| 1757 | 		printf("%02u: proc/ext <%s>:\n", u->id, u->name); | 
| 1758 | 		for (s = u->src_list; s != NULL; s = s->src_next) | 
| 1759 | 			printf("%02u:\tsource %u <%s>:\n", | 
| 1760 | 			    u->id, s->id, s->name); | 
| 1761 | 		break; | 
| 1762 | 	case UAUDIO_AC_CLKSRC: | 
| 1763 | 		printf("%02u: clock source <%s>\n", u->id, u->name); | 
| 1764 | 		break; | 
| 1765 | 	case UAUDIO_AC_CLKSEL: | 
| 1766 | 		printf("%02u: clock sel <%s>\n", u->id, u->name); | 
| 1767 | 		break; | 
| 1768 | 	case UAUDIO_AC_CLKMULT: | 
| 1769 | 		printf("%02u: clock mult\n", u->id); | 
| 1770 | 		break; | 
| 1771 | 	case UAUDIO_AC_RATECONV: | 
| 1772 | 		printf("%02u: rate conv\n", u->id); | 
| 1773 | 		break; | 
| 1774 | 	} | 
| 1775 | } | 
| 1776 |   | 
| 1777 |  | 
| 1778 |   | 
| 1779 |   | 
| 1780 | void | 
| 1781 | uaudio_mixer_print(struct uaudio_softc *sc) | 
| 1782 | { | 
| 1783 | 	struct uaudio_mixent *m; | 
| 1784 | 	struct uaudio_unit *u; | 
| 1785 |   | 
| 1786 | 	for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 1787 | 		for (m = u->mixent_list; m != NULL; m = m->next) { | 
| 1788 | 			printf("%02u:\t%s.%s", | 
| 1789 | 			    u->id, u->name, m->fname); | 
| 1790 | 			if (m->chan >= 0) | 
| 1791 | 				printf("[%u]", m->chan); | 
| 1792 | 			printf("\n"); | 
| 1793 | 		} | 
| 1794 | 	} | 
| 1795 | } | 
| 1796 |   | 
| 1797 |  | 
| 1798 |   | 
| 1799 |   | 
| 1800 | void | 
| 1801 | uaudio_conf_print(struct uaudio_softc *sc) | 
| 1802 | { | 
| 1803 | 	struct uaudio_alt *a; | 
| 1804 | 	struct uaudio_params *p; | 
| 1805 | 	struct mixer_devinfo mi; | 
| 1806 | 	struct mixer_ctrl ctl; | 
| 1807 | 	int i, rates; | 
| 1808 |   | 
| 1809 | 	mi.index = 0; | 
| 1810 | 	while (1) { | 
| 1811 | 		if (uaudio_query_devinfo(sc, &mi) != 0) | 
| 1812 | 			break; | 
| 1813 |   | 
| 1814 | 		if (mi.type != AUDIO_MIXER_CLASS) { | 
| 1815 | 			ctl.dev = mi.index; | 
| 1816 | 			if (uaudio_get_port(sc, &ctl) != 0) { | 
| 1817 | 				printf("%02u: failed to get port\n", mi.index); | 
| 1818 | 				memset(&ctl.un, 0, sizeof(ctl.un)); | 
| 1819 | 			} | 
| 1820 | 		} | 
| 1821 |   | 
| 1822 | 		printf("%02u: <%s>, next = %d, prev = %d, class = %d", | 
| 1823 | 		    mi.index, mi.label.name, mi.next, mi.prev, mi.mixer_class); | 
| 1824 |   | 
| 1825 | 		switch (mi.type) { | 
| 1826 | 		case AUDIO_MIXER_CLASS: | 
| 1827 | 			break; | 
| 1828 | 		case AUDIO_MIXER_VALUE: | 
| 1829 | 			printf(", nch = %d, delta = %d", | 
| 1830 | 			    mi.un.v.num_channels, mi.un.v.delta); | 
| 1831 | 			printf(", val ="); | 
| 1832 | 			for (i = 0; i < mi.un.v.num_channels; i++) | 
| 1833 | 				printf(" %d", ctl.un.value.level[i]); | 
| 1834 | 			break; | 
| 1835 | 		case AUDIO_MIXER_ENUM: | 
| 1836 | 			printf(", members:"); | 
| 1837 | 			for (i = 0; i != mi.un.e.num_mem; i++) { | 
| 1838 | 				printf(" %s(=%d)", | 
| 1839 | 				    mi.un.e.member[i].label.name, | 
| 1840 | 				    mi.un.e.member[i].ord); | 
| 1841 | 			} | 
| 1842 | 			printf(", val = %d", ctl.un.ord); | 
| 1843 | 			break; | 
| 1844 | 		} | 
| 1845 |   | 
| 1846 | 		printf("\n"); | 
| 1847 | 		mi.index++; | 
| 1848 | 	} | 
| 1849 |   | 
| 1850 | 	printf("%d controls\n", mi.index); | 
| 1851 |   | 
| 1852 | 	printf("alts:\n"); | 
| 1853 | 	for (a = sc->alts; a != NULL; a = a->next) { | 
| 1854 | 		rates = uaudio_alt_getrates(sc, a); | 
| 1855 | 		printf("mode = %s, ifnum = %d, altnum = %d, " | 
| 1856 | 		    "addr = 0x%x, maxpkt = %d, sync = 0x%x, " | 
| 1857 | 		    "nch = %d, fmt = s%dle%d, rates:", | 
| 1858 | 		    uaudio_modename(a->mode), | 
| 1859 | 		    a->ifnum, a->altnum, | 
| 1860 | 		    a->data_addr, a->maxpkt, | 
| 1861 | 		    a->sync_addr, | 
| 1862 | 		    a->nch, a->bits, a->bps); | 
| 1863 | 		uaudio_rates_print(rates); | 
| 1864 | 	} | 
| 1865 |   | 
| 1866 | 	printf("parameters:\n"); | 
| 1867 | 	for (p = sc->params_list; p != NULL; p = p->next) { | 
| 1868 | 		switch (sc->version) { | 
| 1869 | 		case UAUDIO_V1: | 
| 1870 | 			rates = p->v1_rates; | 
| 1871 | 			break; | 
| 1872 | 		case UAUDIO_V2: | 
| 1873 | 			rates = uaudio_getrates(sc, p); | 
| 1874 | 			break; | 
| 1875 | 		} | 
| 1876 | 		printf("pchan = %d, s%dle%d, rchan = %d, s%dle%d, rates:", | 
| 1877 | 		    p->palt ? p->palt->nch : 0, | 
| 1878 | 		    p->palt ? p->palt->bits : 0, | 
| 1879 | 		    p->palt ? p->palt->bps : 0, | 
| 1880 | 		    p->ralt ? p->ralt->nch : 0, | 
| 1881 | 		    p->ralt ? p->ralt->bits : 0, | 
| 1882 | 		    p->ralt ? p->ralt->bps : 0); | 
| 1883 | 		uaudio_rates_print(rates); | 
| 1884 | 	} | 
| 1885 | } | 
| 1886 | #endif | 
| 1887 |   | 
| 1888 |  | 
| 1889 |   | 
| 1890 |   | 
| 1891 |   | 
| 1892 | int | 
| 1893 | uaudio_mixer_nchan(struct uaudio_mixent *m, struct uaudio_mixent **rnext) | 
| 1894 | { | 
| 1895 | 	char *name; | 
| 1896 | 	int i; | 
| 1897 |   | 
| 1898 | 	i = 0; | 
| 1899 | 	name = m->fname; | 
| 1900 | 	while (m != NULL && strcmp(name, m->fname) == 0) { | 
| 1901 | 		m = m->next; | 
| 1902 | 		i++; | 
| 1903 | 	} | 
| 1904 | 	if (rnext) | 
| 1905 | 		*rnext = m; | 
| 1906 | 	return i; | 
| 1907 | } | 
| 1908 |   | 
| 1909 |  | 
| 1910 |   | 
| 1911 |   | 
| 1912 |   | 
| 1913 |   | 
| 1914 | void | 
| 1915 | uaudio_mixer_skip(struct uaudio_mixent **pm) | 
| 1916 | { | 
| 1917 | 	struct uaudio_mixent *m = *pm; | 
| 1918 |   | 
| 1919 | 	if (m != NULL && | 
| 1920 | 	    m->chan == -1 && | 
| 1921 | 	    m->next != NULL && | 
| 1922 | 	    strcmp(m->fname, m->next->fname) == 0) | 
| 1923 | 		m = m->next; | 
| 1924 |   | 
| 1925 | 	*pm = m; | 
| 1926 | } | 
| 1927 |   | 
| 1928 |  | 
| 1929 |   | 
| 1930 |   | 
| 1931 |   | 
| 1932 | int | 
| 1933 | uaudio_mixer_byindex(struct uaudio_softc *sc, int index, | 
| 1934 |     struct uaudio_unit **ru, struct uaudio_mixent **rm) | 
| 1935 | { | 
| 1936 | 	struct uaudio_unit *u; | 
| 1937 | 	struct uaudio_mixent *m; | 
| 1938 | 	char *name; | 
| 1939 | 	int i; | 
| 1940 |   | 
| 1941 | 	i = UAUDIO_CLASS_COUNT; | 
| 1942 | 	for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 1943 | 		m = u->mixent_list; | 
| 1944 | 		while (1) { | 
| 1945 | 			uaudio_mixer_skip(&m); | 
| 1946 | 			if (m == NULL) | 
| 1947 | 				break; | 
| 1948 | 			if (index == i) { | 
| 1949 | 				*ru = u; | 
| 1950 | 				*rm = m; | 
| 1951 | 				return 1; | 
| 1952 | 			} | 
| 1953 | 			if (m->type == UAUDIO_MIX_NUM) { | 
| 1954 | 				name = m->fname; | 
| 1955 | 				while (m != NULL && | 
| 1956 | 				    strcmp(name, m->fname) == 0) | 
| 1957 | 					m = m->next; | 
| 1958 | 			} else | 
| 1959 | 				m = m->next; | 
| 1960 | 			i++; | 
| 1961 | 		} | 
| 1962 | 	} | 
| 1963 | 	return 0; | 
| 1964 | } | 
| 1965 |   | 
| 1966 |  | 
| 1967 |   | 
| 1968 |   | 
| 1969 |   | 
| 1970 |   | 
| 1971 |   | 
| 1972 | int | 
| 1973 | uaudio_process_header(struct uaudio_softc *sc, struct uaudio_blob *p) | 
| 1974 | { | 
| 1975 | 	struct uaudio_blob ph; | 
| 1976 | 	unsigned int type, subtype; | 
| 1977 |   | 
| 1978 | 	if (!uaudio_getdesc(p, &ph)) | 
| 1979 | 		return 0; | 
| 1980 | 	if (!uaudio_getnum(&ph, 1, &type)) | 
| 1981 | 		return 0; | 
| 1982 | 	if (type != UDESC_CS_INTERFACE) { | 
| 1983 | 		DPRINTF("%s: expected cs iface desc\n", __func__); | 
| 1984 | 		return 0; | 
| 1985 | 	} | 
| 1986 | 	if (!uaudio_getnum(&ph, 1, &subtype)) | 
| 1987 | 		return 0; | 
| 1988 | 	if (subtype != UAUDIO_AC_HEADER) { | 
| 1989 | 		DPRINTF("%s: expected header desc\n", __func__); | 
| 1990 | 		return 0; | 
| 1991 | 	} | 
| 1992 | 	if (!uaudio_getnum(&ph, 2, &sc->version)) | 
| 1993 | 		return 0; | 
| 1994 |   | 
| 1995 | 	DPRINTF("%s: version 0x%x\n", __func__, sc->version); | 
| 1996 | 	return 1; | 
| 1997 | } | 
| 1998 |   | 
| 1999 |  | 
| 2000 |   | 
| 2001 |   | 
| 2002 |   | 
| 2003 |   | 
| 2004 |   | 
| 2005 | int | 
| 2006 | uaudio_process_ac_ep(struct uaudio_softc *sc, struct uaudio_blob *p) | 
| 2007 | { | 
| 2008 | #ifdef UAUDIO_DEBUG | 
| 2009 | 	static const char *xfer[] = { | 
| 2010 | 		"ctl", "iso", "bulk", "intr" | 
| 2011 | 	}; | 
| 2012 | #endif | 
| 2013 | 	struct uaudio_blob dp; | 
| 2014 | 	unsigned int type, addr, attr, maxpkt, ival; | 
| 2015 | 	unsigned char *savepos; | 
| 2016 |   | 
| 2017 | 	 | 
| 2018 | 	  | 
| 2019 | 	  | 
| 2020 | 	if (p->rptr == p->wptr) | 
| 2021 | 		return 1; | 
| 2022 | 	savepos = p->rptr; | 
| 2023 | 	if (!uaudio_getdesc(p, &dp)) | 
| 2024 | 		return 0; | 
| 2025 | 	if (!uaudio_getnum(&dp, 1, &type)) | 
| 2026 | 		return 0; | 
| 2027 | 	if (type != UDESC_ENDPOINT) { | 
| 2028 | 		p->rptr = savepos; | 
| 2029 | 		return 1; | 
| 2030 | 	} | 
| 2031 |   | 
| 2032 | 	if (!uaudio_getnum(&dp, 1, &addr)) | 
| 2033 | 		return 0; | 
| 2034 | 	if (!uaudio_getnum(&dp, 1, &attr)) | 
| 2035 | 		return 0; | 
| 2036 | 	if (!uaudio_getnum(&dp, 2, &maxpkt)) | 
| 2037 | 		return 0; | 
| 2038 | 	if (!uaudio_getnum(&dp, 1, &ival)) | 
| 2039 | 		return 0; | 
| 2040 |   | 
| 2041 | 	DPRINTF("%s: addr = 0x%x, type = %s, maxpkt = %d, ival = %d\n", | 
| 2042 | 	    __func__, addr, xfer[UE_GET_XFERTYPE(attr)], | 
| 2043 | 	    UE_GET_SIZE(maxpkt), ival); | 
| 2044 |   | 
| 2045 | 	return 1; | 
| 2046 | } | 
| 2047 |   | 
| 2048 |  | 
| 2049 |   | 
| 2050 |   | 
| 2051 |   | 
| 2052 |   | 
| 2053 |   | 
| 2054 |   | 
| 2055 |   | 
| 2056 |   | 
| 2057 |   | 
| 2058 |   | 
| 2059 |   | 
| 2060 |   | 
| 2061 |   | 
| 2062 | int | 
| 2063 | uaudio_process_ac(struct uaudio_softc *sc, struct uaudio_blob *p, int ifnum) | 
| 2064 | { | 
| 2065 | 	struct uaudio_blob units, pu; | 
| 2066 | 	struct uaudio_unit *u, *v; | 
| 2067 | 	unsigned char *savepos; | 
| 2068 | 	unsigned int type, subtype, id; | 
| 2069 | 	char *name, val; | 
| 2070 |   | 
| 2071 | 	DPRINTF("%s: ifnum = %d, %zd bytes to process\n", __func__, | 
| 2072 | 	    ifnum, p->wptr - p->rptr); | 
| 2073 |   | 
| 2074 | 	sc->ctl_ifnum = ifnum; | 
| 2075 |   | 
| 2076 | 	 | 
| 2077 | 	if (!uaudio_process_header(sc, p)) | 
| 2078 | 		return 0; | 
| 2079 |   | 
| 2080 | 	 | 
| 2081 | 	  | 
| 2082 | 	  | 
| 2083 | 	  | 
| 2084 | 	  | 
| 2085 | 	savepos = p->rptr; | 
| 2086 | 	units.rptr = units.wptr = p->rptr; | 
| 2087 | 	while (p->rptr != p->wptr) { | 
| 2088 | 		if (!uaudio_getdesc(p, &pu)) | 
| 2089 | 			return 0; | 
| 2090 | 		if (!uaudio_getnum(&pu, 1, &type)) | 
| 2091 | 			return 0; | 
| 2092 | 		if (type != UDESC_CS_INTERFACE) | 
| 2093 | 			break; | 
| 2094 | 		units.wptr = p->rptr; | 
| 2095 | 	} | 
| 2096 | 	p->rptr = savepos; | 
| 2097 |   | 
| 2098 | 	 | 
| 2099 | 	  | 
| 2100 | 	  | 
| 2101 | 	  | 
| 2102 | 	while (p->rptr != units.wptr) { | 
| 2103 | 		if (!uaudio_getdesc(p, &pu)) | 
| 2104 | 			return 0; | 
| 2105 | 		if (!uaudio_getnum(&pu, 1, &type)) | 
| 2106 | 			return 0; | 
| 2107 | 		if (!uaudio_getnum(&pu, 1, &subtype)) | 
| 2108 | 			return 0; | 
| 2109 | 		if (subtype == UAUDIO_AC_OUTPUT) { | 
| 2110 | 			if (!uaudio_getnum(&pu, 1, &id)) | 
| 2111 | 				return 0; | 
| 2112 | 			if (!uaudio_process_unit(sc, NULL, id, units, NULL)) | 
| 2113 | 				return 0; | 
| 2114 | 		} | 
| 2115 | 	} | 
| 2116 |   | 
| 2117 | 	 | 
| 2118 | 	  | 
| 2119 | 	  | 
| 2120 | 	for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 2121 | 		switch (u->type) { | 
| 2122 | 		case UAUDIO_AC_INPUT: | 
| 2123 | 			uaudio_mkname(sc, uaudio_tname(sc, u->term, 0), u->name); | 
| 2124 | 			break; | 
| 2125 | 		case UAUDIO_AC_OUTPUT: | 
| 2126 | 			uaudio_mkname(sc, uaudio_tname(sc, u->term, 1), u->name); | 
| 2127 | 			break; | 
| 2128 | 		case UAUDIO_AC_CLKSRC: | 
| 2129 | 			uaudio_mkname(sc, uaudio_clkname(u->term), u->name); | 
| 2130 | 			break; | 
| 2131 | 		case UAUDIO_AC_CLKSEL: | 
| 2132 | 			uaudio_mkname(sc, "clksel", u->name); | 
| 2133 | 			break; | 
| 2134 | 		case UAUDIO_AC_EFFECT: | 
| 2135 | 			uaudio_mkname(sc, "fx", u->name); | 
| 2136 | 			break; | 
| 2137 | 		case UAUDIO_AC_PROCESSING: | 
| 2138 | 			uaudio_mkname(sc, "proc", u->name); | 
| 2139 | 			break; | 
| 2140 | 		case UAUDIO_AC_EXTENSION: | 
| 2141 | 			uaudio_mkname(sc, "ext", u->name); | 
| 2142 | 			break; | 
| 2143 | 		} | 
| 2144 | 	} | 
| 2145 |   | 
| 2146 | 	 | 
| 2147 | 	  | 
| 2148 | 	  | 
| 2149 | 	for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 2150 | 		if (u->type != UAUDIO_AC_MIXER && | 
| 2151 | 		    u->type != UAUDIO_AC_SELECTOR) | 
| 2152 | 			continue; | 
| 2153 | 		if (!uaudio_setname_dsts(sc, u, NULL)) { | 
| 2154 | 			switch (u->type) { | 
| 2155 | 			case UAUDIO_AC_MIXER: | 
| 2156 | 				name = "mix"; | 
| 2157 | 				break; | 
| 2158 | 			case UAUDIO_AC_SELECTOR: | 
| 2159 | 				name = "sel"; | 
| 2160 | 				break; | 
| 2161 | 			} | 
| 2162 | 			uaudio_mkname(sc, name, u->name); | 
| 2163 | 		} | 
| 2164 | 	} | 
| 2165 |   | 
| 2166 | 	 | 
| 2167 | 	  | 
| 2168 | 	  | 
| 2169 | 	for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 2170 | 		if (u->type != UAUDIO_AC_FEATURE) | 
| 2171 | 			continue; | 
| 2172 | 		if (uaudio_setname_dsts(sc, u, UAUDIO_NAME_REC)) { | 
| 2173 | 			u->mixer_class = UAUDIO_CLASS_IN; | 
| 2174 | 			continue; | 
| 2175 | 		} | 
| 2176 | 		if (uaudio_setname_srcs(sc, u, UAUDIO_NAME_PLAY)) { | 
| 2177 | 			u->mixer_class = UAUDIO_CLASS_OUT; | 
| 2178 | 			continue; | 
| 2179 | 		} | 
| 2180 | 		if (uaudio_setname_dsts(sc, u, NULL)) { | 
| 2181 | 			u->mixer_class = UAUDIO_CLASS_OUT; | 
| 2182 | 			continue; | 
| 2183 | 		} | 
| 2184 | 		if (uaudio_setname_srcs(sc, u, NULL)) { | 
| 2185 | 			u->mixer_class = UAUDIO_CLASS_IN; | 
| 2186 | 			continue; | 
| 2187 | 		} | 
| 2188 | 		uaudio_setname_middle(sc, u); | 
| 2189 | 		u->mixer_class = UAUDIO_CLASS_IN; | 
| 2190 | 	} | 
| 2191 |   | 
| 2192 | #ifdef UAUDIO_DEBUG | 
| 2193 | 	if (uaudio_debug) { | 
| 2194 | 		printf("%s: units list:\n", DEVNAME(sc)); | 
| 2195 | 		for (u = sc->unit_list; u != NULL; u = u->unit_next) | 
| 2196 | 			uaudio_print_unit(sc, u); | 
| 2197 |   | 
| 2198 | 		printf("%s: mixer controls:\n", DEVNAME(sc)); | 
| 2199 | 		uaudio_mixer_print(sc); | 
| 2200 | 	} | 
| 2201 | #endif | 
| 2202 |   | 
| 2203 | 	 | 
| 2204 | 	if (!uaudio_process_ac_ep(sc, p)) | 
| 2205 | 		return 0; | 
| 2206 |   | 
| 2207 | 	 | 
| 2208 | 	for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 2209 | 		switch (u->type) { | 
| 2210 | 		case UAUDIO_AC_CLKSRC: | 
| 2211 | 			if (!uaudio_req_ranges(sc, 4, | 
| 2212 | 				UAUDIO_V2_REQSEL_CLKFREQ, | 
| 2213 | 				0,  | 
| 2214 | 				sc->ctl_ifnum, | 
| 2215 | 				u->id, | 
| 2216 | 				&u->rates)) { | 
| 2217 | 				printf("%s: failed to read clock rates\n", | 
| 2218 | 				    DEVNAME(sc)); | 
| 2219 | 				return 0; | 
| 2220 | 			} | 
| 2221 | #ifdef UAUDIO_DEBUG | 
| 2222 | 			if (uaudio_debug) { | 
| 2223 | 				printf("%02u: clock rates: ", u->id); | 
| 2224 | 				uaudio_ranges_print(&u->rates); | 
| 2225 | 			} | 
| 2226 | #endif | 
| 2227 | 			break; | 
| 2228 | 		case UAUDIO_AC_CLKSEL: | 
| 2229 | 			if (!uaudio_req(sc, UT_READ_CLASS_INTERFACE, | 
| 2230 | 				UAUDIO_V2_REQ_CUR, | 
| 2231 | 				UAUDIO_V2_REQSEL_CLKSEL, 0, | 
| 2232 | 				sc->ctl_ifnum, u->id, | 
| 2233 | 				&val, 1)) { | 
| 2234 | 				printf("%s: failed to read clock selector\n", | 
| 2235 | 				    DEVNAME(sc)); | 
| 2236 | 				return 0; | 
| 2237 | 			} | 
| 2238 | 			for (v = u->src_list; v != NULL; v = v->src_next) { | 
| 2239 | 				if (--val == 0) | 
| 2240 | 					break; | 
| 2241 | 			} | 
| 2242 | 			u->clock = v; | 
| 2243 | 			break; | 
| 2244 | 		} | 
| 2245 | 	} | 
| 2246 |   | 
| 2247 | 	if (sc->version == UAUDIO_V2) { | 
| 2248 | 		 | 
| 2249 | 		  | 
| 2250 | 		  | 
| 2251 | 		  | 
| 2252 | 		  | 
| 2253 | 		sc->clock = NULL; | 
| 2254 | 		for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 2255 | 			if (u->type != UAUDIO_AC_INPUT && | 
| 2256 | 			    u->type != UAUDIO_AC_OUTPUT) | 
| 2257 | 				continue; | 
| 2258 | 			if (sc->clock == NULL) { | 
| 2259 | 				if (u->clock == NULL) { | 
| 2260 | 					printf("%s: terminal with no clock\n", | 
| 2261 | 					    DEVNAME(sc)); | 
| 2262 | 					return 0; | 
| 2263 | 				} | 
| 2264 | 				sc->clock = u->clock; | 
| 2265 | 			} else if (u->clock != sc->clock) { | 
| 2266 | 				printf("%s: only one clock domain supported\n", | 
| 2267 | 				    DEVNAME(sc)); | 
| 2268 | 				return 0; | 
| 2269 | 			} | 
| 2270 | 		} | 
| 2271 |   | 
| 2272 | 		if (sc->clock == NULL) { | 
| 2273 | 			printf("%s: no clock found\n", DEVNAME(sc)); | 
| 2274 | 			return 0; | 
| 2275 | 		} | 
| 2276 | 	} | 
| 2277 | 	return 1; | 
| 2278 | } | 
| 2279 |   | 
| 2280 |  | 
| 2281 |   | 
| 2282 |   | 
| 2283 |   | 
| 2284 |   | 
| 2285 |   | 
| 2286 |   | 
| 2287 |   | 
| 2288 |   | 
| 2289 |   | 
| 2290 |   | 
| 2291 |   | 
| 2292 |   | 
| 2293 |   | 
| 2294 |   | 
| 2295 |   | 
| 2296 |   | 
| 2297 |   | 
| 2298 |   | 
| 2299 |   | 
| 2300 |   | 
| 2301 |   | 
| 2302 |   | 
| 2303 |   | 
| 2304 |   | 
| 2305 |   | 
| 2306 |   | 
| 2307 |   | 
| 2308 |   | 
| 2309 |   | 
| 2310 |   | 
| 2311 |   | 
| 2312 |   | 
| 2313 |   | 
| 2314 |   | 
| 2315 |   | 
| 2316 |   | 
| 2317 |   | 
| 2318 |   | 
| 2319 |   | 
| 2320 |   | 
| 2321 |   | 
| 2322 |   | 
| 2323 |   | 
| 2324 |   | 
| 2325 |   | 
| 2326 |   | 
| 2327 |   | 
| 2328 |   | 
| 2329 |   | 
| 2330 | int | 
| 2331 | uaudio_process_as_ep(struct uaudio_softc *sc, | 
| 2332 | 	struct uaudio_blob *p, struct uaudio_alt *a, int nep) | 
| 2333 | { | 
| 2334 | 	unsigned int addr, attr, maxpkt, isotype, ival; | 
| 2335 |   | 
| 2336 | 	if (!uaudio_getnum(p, 1, &addr)) | 
| 2337 | 		return 0; | 
| 2338 | 	if (!uaudio_getnum(p, 1, &attr)) | 
| 2339 | 		return 0; | 
| 2340 | 	if (!uaudio_getnum(p, 2, &maxpkt)) | 
| 2341 | 		return 0; | 
| 2342 | 	if (!uaudio_getnum(p, 1, &ival))  | 
| 2343 | 		return 0; | 
| 2344 |   | 
| 2345 | 	DPRINTF("%s: addr = 0x%x, %s/%s, " | 
| 2346 | 	    "maxpktsz = %d, ival = %d\n", | 
| 2347 | 	    __func__, addr, | 
| 2348 | 	    uaudio_isoname(UE_GET_ISO_TYPE(attr)), | 
| 2349 | 	    uaudio_usagename(UE_GET_ISO_USAGE(attr)), | 
| 2350 | 	    maxpkt, ival); | 
| 2351 |   | 
| 2352 | 	if (UE_GET_XFERTYPE(attr) != UE_ISOCHRONOUS) { | 
| 2353 | 		printf("%s: skipped non-isoc endpt.\n", DEVNAME(sc)); | 
| 2354 | 		return 1; | 
| 2355 | 	} | 
| 2356 |   | 
| 2357 | 	 | 
| 2358 | 	  | 
| 2359 | 	  | 
| 2360 | 	  | 
| 2361 | 	  | 
| 2362 | 	  | 
| 2363 | 	  | 
| 2364 | 	  | 
| 2365 | 	  | 
| 2366 | 	  | 
| 2367 | 	  | 
| 2368 | 	  | 
| 2369 | 	isotype = UE_GET_ISO_TYPE(attr); | 
| 2370 | 	if (isotype || nep == 1) { | 
| 2371 | 		 | 
| 2372 |   | 
| 2373 | 		if (a->data_addr && addr != a->data_addr) { | 
| 2374 | 			printf("%s: skipped extra data endpt.\n", DEVNAME(sc)); | 
| 2375 | 			return 1; | 
| 2376 | 		} | 
| 2377 |   | 
| 2378 | 		a->mode = (UE_GET_DIR(addr) == UE_DIR_IN) ? | 
| 2379 | 		    AUMODE_RECORD : AUMODE_PLAY; | 
| 2380 | 		a->data_addr = addr; | 
| 2381 | 		a->fps = sc->ufps / (1 << (ival - 1)); | 
| 2382 | 		a->maxpkt = UE_GET_SIZE(maxpkt); | 
| 2383 | 	} else { | 
| 2384 | 		 | 
| 2385 |   | 
| 2386 | 		if (a->sync_addr && addr != a->sync_addr) { | 
| 2387 | 			printf("%s: skipped extra sync endpt.\n", DEVNAME(sc)); | 
| 2388 | 			return 1; | 
| 2389 | 		} | 
| 2390 | 		a->sync_addr = addr; | 
| 2391 | 	} | 
| 2392 |   | 
| 2393 | 	return 1; | 
| 2394 | } | 
| 2395 |   | 
| 2396 |  | 
| 2397 |   | 
| 2398 |   | 
| 2399 |   | 
| 2400 |   | 
| 2401 |   | 
| 2402 | int | 
| 2403 | uaudio_process_as_general(struct uaudio_softc *sc, | 
| 2404 | 	struct uaudio_blob *p, int *rispcm, struct uaudio_alt *a) | 
| 2405 | { | 
| 2406 | 	unsigned int term, fmt, ctl, fmt_type, fmt_map, nch; | 
| 2407 |   | 
| 2408 | 	if (!uaudio_getnum(p, 1, &term)) | 
| 2409 | 		return 0; | 
| 2410 | 	switch (sc->version) { | 
| 2411 | 	case UAUDIO_V1: | 
| 2412 | 		if (!uaudio_getnum(p, 1, NULL))	 | 
| 2413 | 			return 0; | 
| 2414 | 		if (!uaudio_getnum(p, 1, &fmt)) | 
| 2415 | 			return 0; | 
| 2416 | 		*rispcm = (fmt == UAUDIO_V1_FMT_PCM); | 
| 2417 | 		break; | 
| 2418 | 	case UAUDIO_V2: | 
| 2419 | 		 | 
| 2420 | 		if (!uaudio_getnum(p, 1, &ctl)) | 
| 2421 | 			return 0; | 
| 2422 | 		if (!uaudio_getnum(p, 1, &fmt_type)) | 
| 2423 | 			return 0; | 
| 2424 | 		if (!uaudio_getnum(p, 4, &fmt_map)) | 
| 2425 | 			return 0; | 
| 2426 | 		if (!uaudio_getnum(p, 1, &nch)) | 
| 2427 | 			return 0; | 
| 2428 | 		a->nch = nch; | 
| 2429 | 		*rispcm = (fmt_type == 1) && (fmt_map & UAUDIO_V2_FMT_PCM); | 
| 2430 | 	} | 
| 2431 | 	return 1; | 
| 2432 | } | 
| 2433 |   | 
| 2434 |  | 
| 2435 |   | 
| 2436 |   | 
| 2437 |   | 
| 2438 |   | 
| 2439 |   | 
| 2440 |   | 
| 2441 |   | 
| 2442 | int | 
| 2443 | uaudio_process_as_format(struct uaudio_softc *sc, | 
| 2444 | 	struct uaudio_blob *p, struct uaudio_alt *a, int *ispcm) | 
| 2445 | { | 
| 2446 | 	unsigned int type, bps, bits, nch, nrates, rate_min, rate_max, rates; | 
| 2447 | 	int i, j; | 
| 2448 |   | 
| 2449 | 	switch (sc->version) { | 
| 2450 | 	case UAUDIO_V1: | 
| 2451 | 		if (!uaudio_getnum(p, 1, &type)) | 
| 2452 | 			return 0; | 
| 2453 | 		if (type != 1) { | 
| 2454 | 			DPRINTF("%s: class v1: " | 
| 2455 | 			    "skipped unsupported type = %d\n", __func__, type); | 
| 2456 | 			*ispcm = 0; | 
| 2457 | 			return 1; | 
| 2458 | 		} | 
| 2459 | 		if (!uaudio_getnum(p, 1, &nch)) | 
| 2460 | 			return 0; | 
| 2461 | 		if (!uaudio_getnum(p, 1, &bps)) | 
| 2462 | 			return 0; | 
| 2463 | 		if (!uaudio_getnum(p, 1, &bits)) | 
| 2464 | 			return 0; | 
| 2465 | 		if (!uaudio_getnum(p, 1, &nrates)) | 
| 2466 | 			return 0; | 
| 2467 | 		rates = 0; | 
| 2468 | 		if (nrates == 0) { | 
| 2469 | 			if (!uaudio_getnum(p, 3, &rate_min)) | 
| 2470 | 				return 0; | 
| 2471 | 			if (!uaudio_getnum(p, 3, &rate_max)) | 
| 2472 | 				return 0; | 
| 2473 | 			for (i = 0; i < nitems(uaudio_rates); i++) { | 
| 2474 | 				if (uaudio_rates[i] >= rate_min && | 
| 2475 | 				    uaudio_rates[i] <= rate_max) | 
| 2476 | 					rates |= 1 << i; | 
| 2477 | 			} | 
| 2478 | 		} else { | 
| 2479 | 			for (j = 0; j < nrates; j++) { | 
| 2480 | 				if (!uaudio_getnum(p, 3, &rate_min)) | 
| 2481 | 					return 0; | 
| 2482 | 				for (i = 0; i < nitems(uaudio_rates); i++) { | 
| 2483 | 					if (uaudio_rates[i] == rate_min) | 
| 2484 | 						rates |= 1 << i; | 
| 2485 | 				} | 
| 2486 | 			} | 
| 2487 | 		} | 
| 2488 | 		a->v1_rates = rates; | 
| 2489 | 		a->nch = nch; | 
| 2490 | 		break; | 
| 2491 | 	case UAUDIO_V2: | 
| 2492 | 		 | 
| 2493 | 		  | 
| 2494 | 		  | 
| 2495 | 		  | 
| 2496 | 		  | 
| 2497 | 		  | 
| 2498 | 		  | 
| 2499 | 		if (!uaudio_getnum(p, 1, &type)) | 
| 2500 | 			return 0; | 
| 2501 | 		if (type != 1) { | 
| 2502 | 			DPRINTF("%s: class v2: " | 
| 2503 | 			    "skipped unsupported type = %d\n", __func__, type); | 
| 2504 | 			*ispcm = 0; | 
| 2505 | 			return 1; | 
| 2506 | 		} | 
| 2507 | 		if (!uaudio_getnum(p, 1, &bps)) | 
| 2508 | 			return 0; | 
| 2509 | 		if (!uaudio_getnum(p, 1, &bits)) | 
| 2510 | 			return 0; | 
| 2511 |   | 
| 2512 | 		 | 
| 2513 | 		  | 
| 2514 | 		  | 
| 2515 | 		  | 
| 2516 | 		break; | 
| 2517 | 	} | 
| 2518 | 	a->bps = bps; | 
| 2519 | 	a->bits = bits; | 
| 2520 | 	*ispcm = 1; | 
| 2521 | 	return 1; | 
| 2522 | } | 
| 2523 |   | 
| 2524 |  | 
| 2525 |   | 
| 2526 |   | 
| 2527 |   | 
| 2528 |   | 
| 2529 |   | 
| 2530 |   | 
| 2531 |   | 
| 2532 |   | 
| 2533 |   | 
| 2534 |   | 
| 2535 |   | 
| 2536 | int | 
| 2537 | uaudio_process_as(struct uaudio_softc *sc, | 
| 2538 |     struct uaudio_blob *p, int ifnum, int altnum, int nep) | 
| 2539 | { | 
| 2540 | 	struct uaudio_alt *a, *anext, **pa; | 
| 2541 | 	struct uaudio_blob dp; | 
| 2542 | 	unsigned char *savep; | 
| 2543 | 	unsigned int type, subtype; | 
| 2544 | 	int ispcm = 0; | 
| 2545 |   | 
| 2546 | 	a = malloc(sizeof(struct uaudio_alt), M_USBDEV, M_WAITOK); | 
| 2547 | 	a->mode = 0; | 
| 2548 | 	a->nch = 0; | 
| 2549 | 	a->v1_rates = 0; | 
| 2550 | 	a->data_addr = 0; | 
| 2551 | 	a->sync_addr = 0; | 
| 2552 | 	a->ifnum = ifnum; | 
| 2553 | 	a->altnum = altnum; | 
| 2554 |   | 
| 2555 | 	while (p->rptr != p->wptr) { | 
| 2556 | 		savep = p->rptr; | 
| 2557 | 		if (!uaudio_getdesc(p, &dp)) | 
| 2558 | 			goto failed; | 
| 2559 | 		if (!uaudio_getnum(&dp, 1, &type)) | 
| 2560 | 			goto failed; | 
| 2561 | 		if (type != UDESC_CS_INTERFACE) { | 
| 2562 | 			p->rptr = savep; | 
| 2563 | 			break; | 
| 2564 | 		} | 
| 2565 | 		if (!uaudio_getnum(&dp, 1, &subtype)) | 
| 2566 | 			goto failed; | 
| 2567 | 		switch (subtype) { | 
| 2568 | 		case UAUDIO_AS_GENERAL: | 
| 2569 | 			if (!uaudio_process_as_general(sc, &dp, &ispcm, a)) | 
| 2570 | 				goto failed; | 
| 2571 | 			break; | 
| 2572 | 		case UAUDIO_AS_FORMAT: | 
| 2573 | 			if (!uaudio_process_as_format(sc, &dp, a, &ispcm)) | 
| 2574 | 				goto failed; | 
| 2575 | 			break; | 
| 2576 | 		default: | 
| 2577 | 			DPRINTF("%s: unknown desc\n", __func__); | 
| 2578 | 			continue; | 
| 2579 | 		} | 
| 2580 | 		if (!ispcm) { | 
| 2581 | 			DPRINTF("%s: non-pcm iface\n", __func__); | 
| 2582 | 			free(a, M_USBDEV, sizeof(struct uaudio_alt)); | 
| 2583 | 			return 1; | 
| 2584 | 		} | 
| 2585 | 	} | 
| 2586 |   | 
| 2587 | 	while (p->rptr != p->wptr) { | 
| 2588 | 		savep = p->rptr; | 
| 2589 | 		if (!uaudio_getdesc(p, &dp)) | 
| 2590 | 			goto failed; | 
| 2591 | 		if (!uaudio_getnum(&dp, 1, &type)) | 
| 2592 | 			goto failed; | 
| 2593 | 		if (type == UDESC_CS_ENDPOINT) | 
| 2594 | 			continue; | 
| 2595 | 		if (type != UDESC_ENDPOINT) { | 
| 2596 | 			p->rptr = savep; | 
| 2597 | 			break; | 
| 2598 | 		} | 
| 2599 | 		if (!uaudio_process_as_ep(sc, &dp, a, nep)) | 
| 2600 | 			goto failed; | 
| 2601 | 	} | 
| 2602 |   | 
| 2603 | 	if (a->mode == 0) { | 
| 2604 | 		printf("%s: no data endpoints found\n", DEVNAME(sc)); | 
| 2605 | 		free(a, M_USBDEV, sizeof(struct uaudio_alt)); | 
| 2606 | 		return 1; | 
| 2607 | 	} | 
| 2608 |   | 
| 2609 | 	 | 
| 2610 | 	  | 
| 2611 | 	  | 
| 2612 | 	  | 
| 2613 | 	  | 
| 2614 | 	pa = &sc->alts; | 
| 2615 | 	while (1) { | 
| 2616 | 		if ((anext = *pa) == NULL) | 
| 2617 | 			break; | 
| 2618 | 		if (a->nch > anext->nch) | 
| 2619 | 			break; | 
| 2620 | 		else if (a->nch == anext->nch) { | 
| 2621 | 			if (a->bits > anext->bits) | 
| 2622 | 				break; | 
| 2623 | 			else if (sc->version == UAUDIO_V1 && | 
| 2624 | 				a->v1_rates > anext->v1_rates) | 
| 2625 | 				break; | 
| 2626 | 		} | 
| 2627 | 		pa = &anext->next; | 
| 2628 | 	} | 
| 2629 | 	a->next = *pa; | 
| 2630 | 	*pa = a; | 
| 2631 | 	return 1; | 
| 2632 | failed: | 
| 2633 | 	free(a, M_USBDEV, sizeof(struct uaudio_alt)); | 
| 2634 | 	return 0; | 
| 2635 | } | 
| 2636 |   | 
| 2637 |  | 
| 2638 |   | 
| 2639 |   | 
| 2640 |   | 
| 2641 | void | 
| 2642 | uaudio_fixup_params(struct uaudio_softc *sc) | 
| 2643 | { | 
| 2644 | 	struct uaudio_alt *ap, *ar, *a; | 
| 2645 | 	struct uaudio_params *p, **pp; | 
| 2646 | 	int rates; | 
 | 27  |  | 'rates' declared without an initial value |  |  
  | 
| 2647 |   | 
| 2648 | 	 | 
| 2649 | 	  | 
| 2650 | 	  | 
| 2651 | 	pp = &sc->params_list; | 
| 2652 | 	for (ap = sc->alts; ap != NULL; ap = ap->next) { | 
 | 28  |  | Assuming 'ap' is not equal to NULL |  |  
  | 
 | 29  |  | Loop condition is true.  Entering loop body |  |  
  | 
| 2653 | 		if (ap->mode != AUMODE_PLAY) | 
 | 30  |  | Assuming field 'mode' is equal to AUMODE_PLAY |  |  
  | 
 |  | 
| 2654 | 			continue; | 
| 2655 | 		for (ar = sc->alts; ar != NULL; ar = ar->next) { | 
 | 32  |  | Loop condition is true.  Entering loop body |  |  
  | 
 | 35  |  | Assuming 'ar' is not equal to NULL |  |  
  | 
 | 36  |  | Loop condition is true.  Entering loop body |  |  
  | 
| 2656 | 			if (ar->mode != AUMODE_RECORD) | 
 |  | 
 | 37  |  | Assuming field 'mode' is equal to AUMODE_RECORD |  |  
  | 
| 2657 | 				continue; | 
 | 34  |  |  Execution continues on line 2655 |  |  
  | 
| 2658 | 			if (ar->bps != ap->bps || ar->bits != ap->bits) | 
 | 38  |  | Assuming 'ar->bps' is equal to 'ap->bps' |  |  
  | 
 | 39  |  | Assuming 'ar->bits' is equal to 'ap->bits' |  |  
  | 
 |  | 
| 2659 | 				continue; | 
| 2660 | 			switch (sc->version) { | 
| 2661 | 			case UAUDIO_V1: | 
| 2662 | 				rates = ap->v1_rates & ar->v1_rates; | 
| 2663 | 				if (rates == 0) | 
| 2664 | 					continue; | 
| 2665 | 				break; | 
| 2666 | 			case UAUDIO_V2: | 
| 2667 | 				 | 
| 2668 | 				rates = 0; | 
| 2669 | 				break; | 
| 2670 | 			} | 
| 2671 | 			p = malloc(sizeof(struct uaudio_params), | 
 | 41  |  | 'Default' branch taken. Execution continues on line 2671 |  |  
  | 
| 2672 | 			    M_USBDEV, M_WAITOK); | 
| 2673 | 			p->palt = ap; | 
| 2674 | 			p->ralt = ar; | 
| 2675 | 			p->v1_rates = rates; | 
 | 42  |  | Assigned value is garbage or undefined |  
  | 
| 2676 | 			p->next = NULL; | 
| 2677 | 			*pp = p; | 
| 2678 | 			pp = &p->next; | 
| 2679 | 		} | 
| 2680 | 	} | 
| 2681 |   | 
| 2682 | 	 | 
| 2683 | 	  | 
| 2684 | 	  | 
| 2685 | 	  | 
| 2686 | 	if (sc->params_list == NULL) { | 
| 2687 | 		for (a = sc->alts; a != NULL; a = a->next) { | 
| 2688 | 			p = malloc(sizeof(struct uaudio_params), | 
| 2689 | 			    M_USBDEV, M_WAITOK); | 
| 2690 | 			if (a->mode == AUMODE_PLAY) { | 
| 2691 | 				p->palt = a; | 
| 2692 | 				p->ralt = NULL; | 
| 2693 | 			} else { | 
| 2694 | 				p->palt = NULL; | 
| 2695 | 				p->ralt = a; | 
| 2696 | 			} | 
| 2697 | 			p->v1_rates = a->v1_rates; | 
| 2698 | 			p->next = NULL; | 
| 2699 | 			*pp = p; | 
| 2700 | 			pp = &p->next; | 
| 2701 | 		} | 
| 2702 | 	} | 
| 2703 | } | 
| 2704 |   | 
| 2705 |  | 
| 2706 |   | 
| 2707 |   | 
| 2708 | int | 
| 2709 | uaudio_process_conf(struct uaudio_softc *sc, struct uaudio_blob *p) | 
| 2710 | { | 
| 2711 | 	struct uaudio_blob dp; | 
| 2712 | 	struct uaudio_alt *a; | 
| 2713 | 	unsigned int type, ifnum, altnum, nep, class, subclass; | 
| 2714 |   | 
| 2715 | 	while (p->rptr != p->wptr) { | 
 | 6  |  | Assuming field 'rptr' is not equal to field 'wptr' |  |  
  | 
 | 7  |  | Loop condition is true.  Entering loop body |  |  
  | 
 | 25  |  | Loop condition is false. Execution continues on line 2765 |  |  
  | 
| 2716 | 		if (!uaudio_getdesc(p, &dp)) | 
 |  | 
| 2717 | 			return 0; | 
| 2718 | 		if (!uaudio_getnum(&dp, 1, &type)) | 
 |  | 
| 2719 | 			return 0; | 
| 2720 | 		if (type != UDESC_INTERFACE) | 
 | 10  |  | Assuming 'type' is equal to UDESC_INTERFACE |  |  
  | 
 |  | 
| 2721 | 			continue; | 
| 2722 | 		if (!uaudio_getnum(&dp, 1, &ifnum)) | 
 |  | 
| 2723 | 			return 0; | 
| 2724 | 		if (!uaudio_getnum(&dp, 1, &altnum)) | 
 |  | 
| 2725 | 			return 0; | 
| 2726 | 		if (!uaudio_getnum(&dp, 1, &nep)) | 
 |  | 
| 2727 | 			return 0; | 
| 2728 | 		if (!uaudio_getnum(&dp, 1, &class)) | 
 |  | 
| 2729 | 			return 0; | 
| 2730 | 		if (!uaudio_getnum(&dp, 1, &subclass)) | 
 |  | 
| 2731 | 			return 0; | 
| 2732 | 		if (class != UICLASS_AUDIO) { | 
 | 17  |  | Assuming 'class' is equal to UICLASS_AUDIO |  |  
  | 
 |  | 
| 2733 | 			DPRINTF("%s: skipped iface\n", __func__); | 
| 2734 | 			continue; | 
| 2735 | 		} | 
| 2736 |   | 
| 2737 | 		switch (subclass) { | 
 | 19  |  | Control jumps to 'case 2:'  at line 2750 |  |  
  | 
| 2738 | 		case UISUBCLASS_AUDIOCONTROL: | 
| 2739 | 			if (usbd_iface_claimed(sc->udev, ifnum)) { | 
| 2740 | 				DPRINTF("%s: %d: AC already claimed\n", __func__, ifnum); | 
| 2741 | 				break; | 
| 2742 | 			} | 
| 2743 | 			if (sc->unit_list != NULL) { | 
| 2744 | 				DPRINTF("%s: >1 AC ifaces\n", __func__); | 
| 2745 | 				goto done; | 
| 2746 | 			} | 
| 2747 | 			if (!uaudio_process_ac(sc, p, ifnum)) | 
| 2748 | 				return 0; | 
| 2749 | 			break; | 
| 2750 | 		case UISUBCLASS_AUDIOSTREAM: | 
| 2751 | 			if (usbd_iface_claimed(sc->udev, ifnum)) { | 
 | 20  |  | Assuming the condition is false |  |  
  | 
 |  | 
| 2752 | 				DPRINTF("%s: %d: AS already claimed\n", __func__, ifnum); | 
| 2753 | 				break; | 
| 2754 | 			} | 
| 2755 | 			if (nep == 0) { | 
 | 22  |  | Assuming 'nep' is not equal to 0 |  |  
  | 
 |  | 
| 2756 | 				DPRINTF("%s: " | 
| 2757 | 				    "stop altnum %d\n", __func__, altnum); | 
| 2758 | 				break;	 | 
| 2759 | 			} | 
| 2760 | 			if (!uaudio_process_as(sc, p, ifnum, altnum, nep)) | 
 |  | 
| 2761 | 				return 0; | 
| 2762 | 		} | 
| 2763 | 	} | 
| 2764 | done: | 
| 2765 | 	uaudio_fixup_params(sc); | 
 | 26  |  | Calling 'uaudio_fixup_params' |  |  
  | 
| 2766 |   | 
| 2767 | 	 | 
| 2768 | 	  | 
| 2769 | 	  | 
| 2770 | 	  | 
| 2771 | 	for (a = sc->alts; a != NULL; a = a->next) | 
| 2772 | 		usbd_claim_iface(sc->udev, a->ifnum); | 
| 2773 |   | 
| 2774 | 	usbd_claim_iface(sc->udev, sc->ctl_ifnum); | 
| 2775 |   | 
| 2776 | 	return 1; | 
| 2777 | } | 
| 2778 |   | 
| 2779 |  | 
| 2780 |   | 
| 2781 |   | 
| 2782 |   | 
| 2783 | int | 
| 2784 | uaudio_xfer_alloc(struct uaudio_softc *sc, struct uaudio_xfer *xfer, | 
| 2785 | 	unsigned int framesize, unsigned int count) | 
| 2786 | { | 
| 2787 | 	xfer->usb_xfer = usbd_alloc_xfer(sc->udev); | 
| 2788 | 	if (xfer->usb_xfer == NULL) | 
| 2789 | 		return ENOMEM; | 
| 2790 |   | 
| 2791 | 	xfer->buf = usbd_alloc_buffer(xfer->usb_xfer, framesize * count); | 
| 2792 | 	if (xfer->buf == NULL) | 
| 2793 | 		return ENOMEM; | 
| 2794 |   | 
| 2795 | 	xfer->sizes = mallocarray(count, | 
| 2796 | 	    sizeof(xfer->sizes[0]), M_USBDEV, M_WAITOK); | 
| 2797 | 	if (xfer->sizes == NULL) | 
| 2798 | 		return ENOMEM; | 
| 2799 |   | 
| 2800 | 	return 0; | 
| 2801 | } | 
| 2802 |   | 
| 2803 |  | 
| 2804 |   | 
| 2805 |   | 
| 2806 | void | 
| 2807 | uaudio_xfer_free(struct uaudio_softc *sc, struct uaudio_xfer *xfer, | 
| 2808 | 	unsigned int count) | 
| 2809 | { | 
| 2810 | 	if (xfer->usb_xfer != NULL) { | 
| 2811 | 		 | 
| 2812 | 		usbd_free_xfer(xfer->usb_xfer); | 
| 2813 | 		xfer->usb_xfer = NULL; | 
| 2814 | 	} | 
| 2815 | 	if (xfer->sizes != NULL) { | 
| 2816 | 		free(xfer->sizes, M_USBDEV, | 
| 2817 | 		    sizeof(xfer->sizes[0]) * count); | 
| 2818 | 		xfer->sizes = NULL; | 
| 2819 | 	} | 
| 2820 | } | 
| 2821 |   | 
| 2822 |  | 
| 2823 |   | 
| 2824 |   | 
| 2825 | void | 
| 2826 | uaudio_stream_close(struct uaudio_softc *sc, int dir) | 
| 2827 | { | 
| 2828 | 	struct uaudio_stream *s = &sc->pstream; | 
| 2829 | 	struct uaudio_alt *a = sc->params->palt; | 
| 2830 | 	struct usbd_interface *iface; | 
| 2831 | 	int err, i; | 
| 2832 |   | 
| 2833 | 	if (dir == AUMODE_PLAY) { | 
| 2834 | 		s = &sc->pstream; | 
| 2835 | 		a = sc->params->palt; | 
| 2836 | 	} else { | 
| 2837 | 		s = &sc->rstream; | 
| 2838 | 		a = sc->params->ralt; | 
| 2839 | 	} | 
| 2840 |   | 
| 2841 | 	if (s->data_pipe) { | 
| 2842 | 		usbd_close_pipe(s->data_pipe); | 
| 2843 | 		s->data_pipe = NULL; | 
| 2844 | 	} | 
| 2845 |   | 
| 2846 | 	if (s->sync_pipe) { | 
| 2847 | 		usbd_close_pipe(s->sync_pipe); | 
| 2848 | 		s->sync_pipe = NULL; | 
| 2849 | 	} | 
| 2850 |   | 
| 2851 | 	err = usbd_device2interface_handle(sc->udev, a->ifnum, &iface); | 
| 2852 | 	if (err) | 
| 2853 | 		printf("%s: can't get iface handle\n", DEVNAME(sc)); | 
| 2854 | 	else { | 
| 2855 | 		err = usbd_set_interface(iface, 0); | 
| 2856 | 		if (err) | 
| 2857 | 			printf("%s: can't reset interface\n", DEVNAME(sc)); | 
| 2858 | 	} | 
| 2859 |   | 
| 2860 | 	for (i = 0; i < UAUDIO_NXFERS_MAX; i++) { | 
| 2861 | 		uaudio_xfer_free(sc, s->data_xfers + i, s->nframes_max); | 
| 2862 | 		uaudio_xfer_free(sc, s->sync_xfers + i, 1); | 
| 2863 | 	} | 
| 2864 | } | 
| 2865 |   | 
| 2866 |  | 
| 2867 |   | 
| 2868 |   | 
| 2869 |   | 
| 2870 | int | 
| 2871 | uaudio_stream_open(struct uaudio_softc *sc, int dir, | 
| 2872 |     void *start, void *end, size_t blksz, void (*intr)(void *), void *arg) | 
| 2873 | { | 
| 2874 | 	struct uaudio_stream *s; | 
| 2875 | 	struct uaudio_alt *a; | 
| 2876 | 	struct usbd_interface *iface; | 
| 2877 | 	unsigned char req_buf[4]; | 
| 2878 | 	unsigned int bpa, spf_max, min_blksz; | 
| 2879 | 	int err, clock_id, i; | 
| 2880 |   | 
| 2881 | 	if (dir == AUMODE_PLAY) { | 
| 2882 | 		s = &sc->pstream; | 
| 2883 | 		a = sc->params->palt; | 
| 2884 | 	} else { | 
| 2885 | 		s = &sc->rstream; | 
| 2886 | 		a = sc->params->ralt; | 
| 2887 | 	} | 
| 2888 |   | 
| 2889 | 	for (i = 0; i < UAUDIO_NXFERS_MAX; i++) { | 
| 2890 | 		s->data_xfers[i].usb_xfer = NULL; | 
| 2891 | 		s->data_xfers[i].sizes = NULL; | 
| 2892 | 		s->sync_xfers[i].usb_xfer = NULL; | 
| 2893 | 		s->sync_xfers[i].sizes = NULL; | 
| 2894 | 	} | 
| 2895 | 	s->data_pipe = NULL; | 
| 2896 | 	s->sync_pipe = NULL; | 
| 2897 |   | 
| 2898 | 	s->nframes_mask = 0; | 
| 2899 | 	i = a->fps; | 
| 2900 | 	while (i > 1000) { | 
| 2901 | 		s->nframes_mask = (s->nframes_mask << 1) | 1; | 
| 2902 | 		i >>= 1; | 
| 2903 | 	} | 
| 2904 |   | 
| 2905 | 	 | 
| 2906 | 	bpa = a->bps * a->nch; | 
| 2907 |   | 
| 2908 | 	 | 
| 2909 | 	s->spf = (uint64_t)sc->rate * UAUDIO_SPF_DIV / a->fps; | 
| 2910 |   | 
| 2911 | 	 | 
| 2912 | 	  | 
| 2913 | 	  | 
| 2914 | 	  | 
| 2915 | 	  | 
| 2916 | 	s->spf_min = (uint64_t)s->spf * 255 / 256; | 
| 2917 | 	s->spf_max = (uint64_t)s->spf * 257 / 256; | 
| 2918 |   | 
| 2919 | 	 | 
| 2920 | 	spf_max = (a->maxpkt / bpa) * UAUDIO_SPF_DIV; | 
| 2921 | 	if (s->spf > spf_max) { | 
| 2922 | 		printf("%s: samples per frame too large\n", DEVNAME(sc)); | 
| 2923 | 		return EIO; | 
| 2924 | 	} | 
| 2925 | 	if (s->spf_max > spf_max) | 
| 2926 | 		s->spf_max = spf_max; | 
| 2927 |   | 
| 2928 | 	 | 
| 2929 | 	  | 
| 2930 | 	  | 
| 2931 | 	  | 
| 2932 | 	  | 
| 2933 | 	  | 
| 2934 | 	  | 
| 2935 | 	  | 
| 2936 | 	  | 
| 2937 | 	  | 
| 2938 | 	  | 
| 2939 | 	  | 
| 2940 | 	  | 
| 2941 | 	  | 
| 2942 | 	min_blksz = (((uint64_t)blksz << 32) / s->spf_max * s->spf_min) >> 32; | 
| 2943 |   | 
| 2944 | 	 | 
| 2945 | 	min_blksz -= min_blksz % bpa; | 
| 2946 |   | 
| 2947 | 	 | 
| 2948 | 	s->safe_blksz = blksz + UAUDIO_NXFERS_MAX * (blksz - min_blksz); | 
| 2949 |   | 
| 2950 | 	 | 
| 2951 | 	s->nframes_max = (uint64_t)(s->safe_blksz / bpa) * | 
| 2952 | 	    UAUDIO_SPF_DIV / s->spf_min + 1; | 
| 2953 |   | 
| 2954 | 	 | 
| 2955 | 	s->nframes_max = (s->nframes_max + s->nframes_mask) & | 
| 2956 | 	    ~s->nframes_mask; | 
| 2957 |   | 
| 2958 | 	 | 
| 2959 | 	s->maxpkt = bpa * | 
| 2960 | 	    ((s->spf_max + UAUDIO_SPF_DIV - 1) / UAUDIO_SPF_DIV); | 
| 2961 |   | 
| 2962 | 	 | 
| 2963 | 	s->nxfers = sc->host_nframes / s->nframes_max; | 
| 2964 | 	if (s->nxfers > UAUDIO_NXFERS_MAX) | 
| 2965 | 		s->nxfers = UAUDIO_NXFERS_MAX; | 
| 2966 |   | 
| 2967 | 	DPRINTF("%s: %s: blksz = %zu, rate = %u, fps = %u\n", __func__, | 
| 2968 | 	    dir == AUMODE_PLAY ? "play" : "rec", blksz, sc->rate, a->fps); | 
| 2969 | 	DPRINTF("%s: spf = 0x%x in [0x%x:0x%x]\n", __func__, | 
| 2970 | 	    s->spf, s->spf_min, s->spf_max); | 
| 2971 | 	DPRINTF("%s: nframes_max = %u, nframes_mask = %u, maxpkt = %u\n", | 
| 2972 | 	    __func__, s->nframes_max, s->nframes_mask, s->maxpkt); | 
| 2973 | 	DPRINTF("%s: safe_blksz = %d, nxfers = %d\n", __func__, | 
| 2974 | 	    s->safe_blksz, s->nxfers); | 
| 2975 |   | 
| 2976 | 	if (s->nxfers < UAUDIO_NXFERS_MIN) { | 
| 2977 | 		printf("%s: block size too large\n", DEVNAME(sc)); | 
| 2978 | 		return EIO; | 
| 2979 | 	} | 
| 2980 |   | 
| 2981 | 	 | 
| 2982 | 	  | 
| 2983 | 	  | 
| 2984 | 	   | 
| 2985 | 	  | 
| 2986 | 	  | 
| 2987 | 	if (1000 * blksz < 2 * sc->rate * bpa) { | 
| 2988 | 		printf("%s: audio block too small\n", DEVNAME(sc)); | 
| 2989 | 		return EIO; | 
| 2990 | 	} | 
| 2991 |   | 
| 2992 | 	for (i = 0; i < s->nxfers; i++) { | 
| 2993 | 		err = uaudio_xfer_alloc(sc, s->data_xfers + i, | 
| 2994 | 		    s->maxpkt, s->nframes_max); | 
| 2995 | 		if (err) | 
| 2996 | 			goto failed; | 
| 2997 | 		if (a->sync_addr) { | 
| 2998 | 			err = uaudio_xfer_alloc(sc, s->sync_xfers + i, | 
| 2999 | 			    sc->sync_pktsz, 1); | 
| 3000 | 			if (err) | 
| 3001 | 				goto failed; | 
| 3002 | 		} | 
| 3003 | 	} | 
| 3004 |   | 
| 3005 | 	err = usbd_device2interface_handle(sc->udev, a->ifnum, &iface); | 
| 3006 | 	if (err) { | 
| 3007 | 		printf("%s: can't get iface handle\n", DEVNAME(sc)); | 
| 3008 | 		goto failed; | 
| 3009 | 	} | 
| 3010 |   | 
| 3011 | 	err = usbd_set_interface(iface, a->altnum); | 
| 3012 | 	if (err) { | 
| 3013 | 		printf("%s: can't set interface\n", DEVNAME(sc)); | 
| 3014 | 		goto failed; | 
| 3015 | 	} | 
| 3016 |   | 
| 3017 | 	 | 
| 3018 | 	  | 
| 3019 | 	  | 
| 3020 | 	  | 
| 3021 | 	  | 
| 3022 | 	  | 
| 3023 | 	  | 
| 3024 | 	  | 
| 3025 | 	  | 
| 3026 | 	  | 
| 3027 | 	  | 
| 3028 | 	  | 
| 3029 | 	  | 
| 3030 | 	switch (sc->version) { | 
| 3031 | 	case UAUDIO_V1: | 
| 3032 | 		req_buf[0] = sc->rate; | 
| 3033 | 		req_buf[1] = sc->rate >> 8; | 
| 3034 | 		req_buf[2] = sc->rate >> 16; | 
| 3035 | 		if (!uaudio_req(sc, UT_WRITE_CLASS_ENDPOINT, | 
| 3036 | 			UAUDIO_V1_REQ_SET_CUR, UAUDIO_REQSEL_RATE, 0, | 
| 3037 | 			a->data_addr, 0, req_buf, 3)) { | 
| 3038 | 			DPRINTF("%s: not setting endpoint rate\n", __func__); | 
| 3039 | 		} | 
| 3040 | 		break; | 
| 3041 | 	case UAUDIO_V2: | 
| 3042 | 		req_buf[0] = sc->rate; | 
| 3043 | 		req_buf[1] = sc->rate >> 8; | 
| 3044 | 		req_buf[2] = sc->rate >> 16; | 
| 3045 | 		req_buf[3] = sc->rate >> 24; | 
| 3046 | 		clock_id = uaudio_clock_id(sc); | 
| 3047 | 		if (clock_id < 0) { | 
| 3048 | 			printf("%s: can't get clock id\n", DEVNAME(sc)); | 
| 3049 | 			goto failed; | 
| 3050 | 		} | 
| 3051 | 		if (!uaudio_req(sc, UT_WRITE_CLASS_INTERFACE, | 
| 3052 | 			UAUDIO_V2_REQ_CUR, UAUDIO_REQSEL_RATE, 0, | 
| 3053 | 			sc->ctl_ifnum, clock_id, req_buf, 4)) { | 
| 3054 | 			DPRINTF("%s: not setting clock rate\n", __func__); | 
| 3055 | 		} | 
| 3056 | 		break; | 
| 3057 | 	} | 
| 3058 |   | 
| 3059 | 	err = usbd_open_pipe(iface, a->data_addr, 0, &s->data_pipe); | 
| 3060 | 	if (err) { | 
| 3061 | 		printf("%s: can't open data pipe\n", DEVNAME(sc)); | 
| 3062 | 		goto failed; | 
| 3063 | 	} | 
| 3064 |   | 
| 3065 | 	if (a->sync_addr) { | 
| 3066 | 		err = usbd_open_pipe(iface, a->sync_addr, 0, &s->sync_pipe); | 
| 3067 | 		if (err) { | 
| 3068 | 			printf("%s: can't open sync pipe\n", DEVNAME(sc)); | 
| 3069 | 			goto failed; | 
| 3070 | 		} | 
| 3071 | 	} | 
| 3072 |   | 
| 3073 | 	s->data_nextxfer = 0; | 
| 3074 | 	s->sync_nextxfer = 0; | 
| 3075 | 	s->spf_remain = 0; | 
| 3076 |   | 
| 3077 | 	s->intr = intr; | 
| 3078 | 	s->arg = arg; | 
| 3079 | 	s->ring_start = start; | 
| 3080 | 	s->ring_end = end; | 
| 3081 | 	s->ring_blksz = blksz; | 
| 3082 |   | 
| 3083 | 	s->ring_pos = s->ring_start; | 
| 3084 | 	s->ring_offs = 0; | 
| 3085 | 	s->ring_icnt = 0; | 
| 3086 |   | 
| 3087 | 	s->ubuf_xfer = 0; | 
| 3088 | 	s->ubuf_pos = 0; | 
| 3089 | 	return 0; | 
| 3090 |   | 
| 3091 | failed: | 
| 3092 | 	uaudio_stream_close(sc, dir); | 
| 3093 | 	return ENOMEM; | 
| 3094 | } | 
| 3095 |   | 
| 3096 |  | 
| 3097 |   | 
| 3098 |   | 
| 3099 | void | 
| 3100 | uaudio_adjspf(struct uaudio_softc *sc) | 
| 3101 | { | 
| 3102 | 	struct uaudio_stream *s = &sc->pstream; | 
| 3103 | 	int diff; | 
| 3104 |   | 
| 3105 | 	if (sc->mode != (AUMODE_RECORD | AUMODE_PLAY)) | 
| 3106 | 		return; | 
| 3107 | 	if (s->sync_pipe != NULL) | 
| 3108 | 		return; | 
| 3109 |   | 
| 3110 | 	 | 
| 3111 | 	  | 
| 3112 | 	  | 
| 3113 | 	diff = sc->diff_nsamp; | 
| 3114 | 	if (sc->diff_nframes > 0) { | 
| 3115 | 		diff -= (uint64_t)sc->pstream.spf * | 
| 3116 | 		    sc->diff_nframes / UAUDIO_SPF_DIV; | 
| 3117 | 	} else { | 
| 3118 | 		diff += (uint64_t)sc->rstream.spf * | 
| 3119 | 		    -sc->diff_nframes / UAUDIO_SPF_DIV; | 
| 3120 | 	} | 
| 3121 |   | 
| 3122 | 	 | 
| 3123 | 	  | 
| 3124 | 	  | 
| 3125 | 	s->spf = (uint64_t)(sc->rate - diff) * UAUDIO_SPF_DIV / sc->ufps; | 
| 3126 | 	if (s->spf > s->spf_max) | 
| 3127 | 		s->spf = s->spf_max; | 
| 3128 | 	else if (s->spf < s->spf_min) | 
| 3129 | 		s->spf = s->spf_min; | 
| 3130 | #ifdef UAUDIO_DEBUG | 
| 3131 | 	if (uaudio_debug >= 2) | 
| 3132 | 		printf("%s: diff = %d, spf = 0x%x\n", __func__, diff, s->spf); | 
| 3133 | #endif | 
| 3134 | } | 
| 3135 |   | 
| 3136 |  | 
| 3137 |   | 
| 3138 |   | 
| 3139 | void | 
| 3140 | uaudio_pdata_copy(struct uaudio_softc *sc) | 
| 3141 | { | 
| 3142 | 	struct uaudio_stream *s = &sc->pstream; | 
| 3143 | 	struct uaudio_xfer *xfer; | 
| 3144 | 	size_t count, avail; | 
| 3145 | 	int index; | 
| 3146 | #ifdef UAUDIO_DEBUG | 
| 3147 | 	struct timeval tv; | 
| 3148 |   | 
| 3149 | 	getmicrotime(&tv); | 
| 3150 | #endif | 
| 3151 | 	while (sc->copy_todo > 0 && s->ubuf_xfer < s->nxfers) { | 
| 3152 | 		index = s->data_nextxfer + s->ubuf_xfer; | 
| 3153 | 		if (index >= s->nxfers) | 
| 3154 | 			index -= s->nxfers; | 
| 3155 | 		xfer = s->data_xfers + index; | 
| 3156 | 		avail = s->ring_end - s->ring_pos; | 
| 3157 | 		count = xfer->size - s->ubuf_pos; | 
| 3158 | 		if (count > avail) | 
| 3159 | 			count = avail; | 
| 3160 | 		if (count > sc->copy_todo) | 
| 3161 | 			count = sc->copy_todo; | 
| 3162 | #ifdef UAUDIO_DEBUG | 
| 3163 | 		if (uaudio_debug >= 2) { | 
| 3164 | 			printf("%s: %llu.%06lu: %zd..%zd -> %u:%u..%zu\n", | 
| 3165 | 			    __func__, tv.tv_sec, tv.tv_usec, | 
| 3166 | 			    s->ring_pos - s->ring_start, | 
| 3167 | 			    s->ring_pos - s->ring_start + count, | 
| 3168 | 			    s->ubuf_xfer, s->ubuf_pos, s->ubuf_pos + count); | 
| 3169 | 		} | 
| 3170 | #endif | 
| 3171 | 		memcpy(xfer->buf + s->ubuf_pos, s->ring_pos, count); | 
| 3172 | 		sc->copy_todo -= count; | 
| 3173 | 		s->ring_pos += count; | 
| 3174 | 		if (s->ring_pos == s->ring_end) { | 
| 3175 | 			s->ring_pos = s->ring_start; | 
| 3176 | 		} | 
| 3177 | 		s->ubuf_pos += count; | 
| 3178 | 		if (s->ubuf_pos == xfer->size) { | 
| 3179 | 			usb_syncmem(&xfer->usb_xfer->dmabuf, 0, xfer->size, | 
| 3180 | 			    BUS_DMASYNC_PREWRITE); | 
| 3181 | 			s->ubuf_pos = 0; | 
| 3182 | #ifdef DIAGNOSTIC | 
| 3183 | 			if (s->ubuf_xfer == s->nxfers) { | 
| 3184 | 				printf("%s: overflow\n", __func__); | 
| 3185 | 				return; | 
| 3186 | 			} | 
| 3187 | #endif | 
| 3188 | 			s->ubuf_xfer++; | 
| 3189 | 		} | 
| 3190 | 	} | 
| 3191 | } | 
| 3192 |   | 
| 3193 |  | 
| 3194 |   | 
| 3195 |   | 
| 3196 | void | 
| 3197 | uaudio_pdata_calcsizes(struct uaudio_softc *sc, struct uaudio_xfer *xfer) | 
| 3198 | { | 
| 3199 | #ifdef UAUDIO_DEBUG | 
| 3200 | 	struct timeval tv; | 
| 3201 | #endif | 
| 3202 | 	struct uaudio_stream *s = &sc->pstream; | 
| 3203 | 	struct uaudio_alt *a = sc->params->palt; | 
| 3204 | 	unsigned int fsize, bpf; | 
| 3205 | 	int done; | 
| 3206 |   | 
| 3207 | 	bpf = a->bps * a->nch; | 
| 3208 | 	done = s->ring_offs; | 
| 3209 | 	xfer->nframes = 0; | 
| 3210 |   | 
| 3211 | 	while (1) { | 
| 3212 | 		 | 
| 3213 | 		  | 
| 3214 | 		  | 
| 3215 | 		if ((xfer->nframes & s->nframes_mask) == 0 && | 
| 3216 | 		    done > s->safe_blksz) | 
| 3217 | 			break; | 
| 3218 |   | 
| 3219 | 		 | 
| 3220 | 		  | 
| 3221 | 		  | 
| 3222 | 		if (xfer->nframes == s->nframes_max) { | 
| 3223 | 			printf("%s: too many frames for play xfer: " | 
| 3224 | 			    "done = %u, blksz = %d\n", | 
| 3225 | 			    DEVNAME(sc), done, s->ring_blksz); | 
| 3226 | 			break; | 
| 3227 | 		} | 
| 3228 |   | 
| 3229 | 		 | 
| 3230 | 		  | 
| 3231 | 		  | 
| 3232 | 		s->spf_remain += s->spf; | 
| 3233 | 		fsize = s->spf_remain / UAUDIO_SPF_DIV * bpf; | 
| 3234 | 		s->spf_remain %= UAUDIO_SPF_DIV; | 
| 3235 | 		done += fsize; | 
| 3236 | 		xfer->sizes[xfer->nframes] = fsize; | 
| 3237 | 		xfer->nframes++; | 
| 3238 | 	} | 
| 3239 |   | 
| 3240 | 	xfer->size = done - s->ring_offs; | 
| 3241 | 	s->ring_offs = done - s->ring_blksz; | 
| 3242 |   | 
| 3243 | #ifdef UAUDIO_DEBUG | 
| 3244 | 	if (uaudio_debug >= 3) { | 
| 3245 | 		getmicrotime(&tv); | 
| 3246 | 		printf("%s: size = %d, offs -> %d\n", __func__, | 
| 3247 | 		    xfer->size, s->ring_offs); | 
| 3248 | 	} | 
| 3249 | #endif | 
| 3250 | 	memset(xfer->buf, 0, xfer->size); | 
| 3251 | } | 
| 3252 |   | 
| 3253 |  | 
| 3254 |   | 
| 3255 |   | 
| 3256 | void | 
| 3257 | uaudio_pdata_xfer(struct uaudio_softc *sc) | 
| 3258 | { | 
| 3259 | #ifdef UAUDIO_DEBUG | 
| 3260 | 	struct timeval tv; | 
| 3261 | #endif | 
| 3262 | 	struct uaudio_stream *s = &sc->pstream; | 
| 3263 | 	struct uaudio_xfer *xfer; | 
| 3264 | 	int err; | 
| 3265 |   | 
| 3266 | 	xfer = s->data_xfers + s->data_nextxfer; | 
| 3267 |   | 
| 3268 | #ifdef UAUDIO_DEBUG | 
| 3269 | 	if (uaudio_debug >= 3) { | 
| 3270 | 		getmicrotime(&tv); | 
| 3271 | 		printf("%s: %llu.%06lu: " | 
| 3272 | 		    "%d bytes, %u frames, remain = 0x%x, offs = %d\n", | 
| 3273 | 		    __func__, tv.tv_sec, tv.tv_usec, | 
| 3274 | 		    xfer->size, xfer->nframes, | 
| 3275 | 		    s->spf_remain, s->ring_offs); | 
| 3276 | 	} | 
| 3277 | #endif | 
| 3278 |   | 
| 3279 | 	 | 
| 3280 | 	if (xfer->nframes == 0) { | 
| 3281 | 		printf("%s: zero frame play xfer\n", DEVNAME(sc)); | 
| 3282 | 		return; | 
| 3283 | 	} | 
| 3284 |   | 
| 3285 | 	 | 
| 3286 | 	  | 
| 3287 | 	  | 
| 3288 | 	  | 
| 3289 | 	usbd_setup_isoc_xfer(xfer->usb_xfer, s->data_pipe, sc, | 
| 3290 | 	    xfer->sizes, xfer->nframes, | 
| 3291 | 	    USBD_NO_COPY | USBD_SHORT_XFER_OK, | 
| 3292 | 	    uaudio_pdata_intr); | 
| 3293 |   | 
| 3294 | 	err = usbd_transfer(xfer->usb_xfer); | 
| 3295 | 	if (err != 0 && err != USBD_IN_PROGRESS) | 
| 3296 | 		printf("%s: play xfer, err = %d\n", DEVNAME(sc), err); | 
| 3297 |   | 
| 3298 | 	if (++s->data_nextxfer == s->nxfers) | 
| 3299 | 		s->data_nextxfer = 0; | 
| 3300 | } | 
| 3301 |   | 
| 3302 |  | 
| 3303 |   | 
| 3304 |   | 
| 3305 | void | 
| 3306 | uaudio_pdata_intr(struct usbd_xfer *usb_xfer, void *arg, usbd_status status) | 
| 3307 | { | 
| 3308 | #ifdef UAUDIO_DEBUG | 
| 3309 | 	struct timeval tv; | 
| 3310 | #endif | 
| 3311 | 	struct uaudio_softc *sc = arg; | 
| 3312 | 	struct uaudio_stream *s = &sc->pstream; | 
| 3313 | 	struct uaudio_xfer *xfer; | 
| 3314 | 	uint32_t size; | 
| 3315 | 	int nintr; | 
| 3316 |   | 
| 3317 | 	if (status != 0 && status != USBD_IOERROR) { | 
| 3318 | 		DPRINTF("%s: xfer status = %d\n", __func__, status); | 
| 3319 | 		return; | 
| 3320 | 	} | 
| 3321 |   | 
| 3322 | 	xfer = s->data_xfers + s->data_nextxfer; | 
| 3323 | 	if (xfer->usb_xfer != usb_xfer) { | 
| 3324 | 		DPRINTF("%s: wrong xfer\n", __func__); | 
| 3325 | 		return; | 
| 3326 | 	} | 
| 3327 |   | 
| 3328 | 	sc->diff_nsamp += xfer->size / | 
| 3329 | 	    (sc->params->palt->nch * sc->params->palt->bps); | 
| 3330 | 	sc->diff_nframes += xfer->nframes; | 
| 3331 |   | 
| 3332 | #ifdef UAUDIO_DEBUG | 
| 3333 | 	if (uaudio_debug >= 2) { | 
| 3334 | 		getmicrotime(&tv); | 
| 3335 | 		printf("%s: %llu.%06lu: %u: %u bytes\n", | 
| 3336 | 		    __func__, tv.tv_sec, tv.tv_usec, | 
| 3337 | 		    s->data_nextxfer, xfer->size); | 
| 3338 | 	} | 
| 3339 | #endif | 
| 3340 | 	usbd_get_xfer_status(usb_xfer, NULL, NULL, &size, NULL); | 
| 3341 | 	if (size != xfer->size) { | 
| 3342 | 		DPRINTF("%s: %u bytes out of %u: incomplete play xfer\n", | 
| 3343 | 		    DEVNAME(sc), size, xfer->size); | 
| 3344 | 	} | 
| 3345 |   | 
| 3346 | 	 | 
| 3347 | 	  | 
| 3348 | 	  | 
| 3349 | 	  | 
| 3350 | 	  | 
| 3351 | 	s->ring_icnt += xfer->size; | 
| 3352 | 	nintr = 0; | 
| 3353 | 	mtx_enter(&audio_lock); | 
| 3354 | 	while (s->ring_icnt >= s->ring_blksz) { | 
| 3355 | 		s->intr(s->arg); | 
| 3356 | 		s->ring_icnt -= s->ring_blksz; | 
| 3357 | 		nintr++; | 
| 3358 | 	} | 
| 3359 | 	mtx_leave(&audio_lock); | 
| 3360 | 	if (nintr != 1) | 
| 3361 | 		printf("%s: %d: bad play intr count\n", __func__, nintr); | 
| 3362 |   | 
| 3363 | 	uaudio_pdata_calcsizes(sc, xfer); | 
| 3364 | 	uaudio_pdata_xfer(sc); | 
| 3365 | #ifdef DIAGNOSTIC | 
| 3366 | 	if (s->ubuf_xfer == 0) { | 
| 3367 | 		printf("%s: underflow\n", __func__); | 
| 3368 | 		return; | 
| 3369 | 	} | 
| 3370 | #endif | 
| 3371 | 	s->ubuf_xfer--; | 
| 3372 | 	uaudio_pdata_copy(sc); | 
| 3373 | } | 
| 3374 |   | 
| 3375 |  | 
| 3376 |   | 
| 3377 |   | 
| 3378 | void | 
| 3379 | uaudio_psync_xfer(struct uaudio_softc *sc) | 
| 3380 | { | 
| 3381 | #ifdef UAUDIO_DEBUG | 
| 3382 | 	struct timeval tv; | 
| 3383 | #endif | 
| 3384 | 	struct uaudio_stream *s = &sc->pstream; | 
| 3385 | 	struct uaudio_xfer *xfer; | 
| 3386 | 	unsigned int i; | 
| 3387 | 	int err; | 
| 3388 |   | 
| 3389 | 	xfer = s->sync_xfers + s->sync_nextxfer; | 
| 3390 | 	xfer->nframes = 1; | 
| 3391 |   | 
| 3392 | 	for (i = 0; i < xfer->nframes; i++) | 
| 3393 | 		xfer->sizes[i] = sc->sync_pktsz; | 
| 3394 |   | 
| 3395 | 	xfer->size = xfer->nframes * sc->sync_pktsz; | 
| 3396 |   | 
| 3397 | #ifdef UAUDIO_DEBUG | 
| 3398 | 	memset(xfer->buf, 0xd0, sc->sync_pktsz * xfer->nframes); | 
| 3399 | #endif | 
| 3400 |   | 
| 3401 | 	usbd_setup_isoc_xfer(xfer->usb_xfer, s->sync_pipe, sc, | 
| 3402 | 	    xfer->sizes, xfer->nframes, | 
| 3403 | 	    USBD_NO_COPY | USBD_SHORT_XFER_OK, | 
| 3404 | 	    uaudio_psync_intr); | 
| 3405 |   | 
| 3406 | 	err = usbd_transfer(xfer->usb_xfer); | 
| 3407 | 	if (err != 0 && err != USBD_IN_PROGRESS) | 
| 3408 | 		printf("%s: sync play xfer, err = %d\n", DEVNAME(sc), err); | 
| 3409 |   | 
| 3410 | 	if (++s->sync_nextxfer == s->nxfers) | 
| 3411 | 		s->sync_nextxfer = 0; | 
| 3412 |   | 
| 3413 | #ifdef UAUDIO_DEBUG | 
| 3414 | 	if (uaudio_debug >= 3) { | 
| 3415 | 		getmicrotime(&tv); | 
| 3416 | 		printf("%s: %llu.%06lu: %dB, %d fr\n", __func__, | 
| 3417 | 		    tv.tv_sec, tv.tv_usec, sc->sync_pktsz, xfer->nframes); | 
| 3418 | 	} | 
| 3419 | #endif | 
| 3420 | } | 
| 3421 |   | 
| 3422 |  | 
| 3423 |   | 
| 3424 |   | 
| 3425 | void | 
| 3426 | uaudio_psync_intr(struct usbd_xfer *usb_xfer, void *arg, usbd_status status) | 
| 3427 | { | 
| 3428 | #ifdef UAUDIO_DEBUG | 
| 3429 | 	struct timeval tv; | 
| 3430 | #endif | 
| 3431 | 	struct uaudio_softc *sc = arg; | 
| 3432 | 	struct uaudio_stream *s = &sc->pstream; | 
| 3433 | 	struct uaudio_xfer *xfer; | 
| 3434 | 	unsigned char *buf; | 
| 3435 | 	unsigned int i; | 
| 3436 | 	int32_t val; | 
| 3437 |   | 
| 3438 | 	if (status != 0) { | 
| 3439 | 		DPRINTF("%s: xfer status = %d\n", __func__, status); | 
| 3440 | 		return; | 
| 3441 | 	} | 
| 3442 |   | 
| 3443 | 	xfer = s->sync_xfers + s->sync_nextxfer; | 
| 3444 | 	if (xfer->usb_xfer != usb_xfer) { | 
| 3445 | 		DPRINTF("%s: wrong xfer\n", __func__); | 
| 3446 | 		return; | 
| 3447 | 	} | 
| 3448 |   | 
| 3449 | 	 | 
| 3450 |   | 
| 3451 | 	buf = xfer->buf; | 
| 3452 | 	for (i = 0; i < xfer->nframes; i++) { | 
| 3453 | 		if (xfer->sizes[i] == sc->sync_pktsz) { | 
| 3454 | 			val = buf[0] | buf[1] << 8 | buf[2] << 16; | 
| 3455 | 			if (sc->sync_pktsz == 4) | 
| 3456 | 				val |= xfer->buf[3] << 24; | 
| 3457 | 			else | 
| 3458 | 				val <<= 2; | 
| 3459 | 			val *= UAUDIO_SPF_DIV / (1 << 16); | 
| 3460 | #ifdef UAUDIO_DEBUG | 
| 3461 | 			if (uaudio_debug >= 2) { | 
| 3462 | 				getmicrotime(&tv); | 
| 3463 | 				printf("%s: %llu.%06lu: spf: %08x\n", | 
| 3464 | 				    __func__, tv.tv_sec, tv.tv_usec, val); | 
| 3465 | 			} | 
| 3466 | #endif | 
| 3467 | 			if (val > s->spf_max) | 
| 3468 | 				s->spf = s->spf_max; | 
| 3469 | 			else if (val < s->spf_min) | 
| 3470 | 				s->spf = s->spf_min; | 
| 3471 | 			else | 
| 3472 | 				s->spf = val; | 
| 3473 | 		} | 
| 3474 | 		buf += sc->sync_pktsz; | 
| 3475 | 	} | 
| 3476 |   | 
| 3477 | 	uaudio_psync_xfer(sc); | 
| 3478 | } | 
| 3479 |   | 
| 3480 |  | 
| 3481 |   | 
| 3482 |   | 
| 3483 | void | 
| 3484 | uaudio_rdata_xfer(struct uaudio_softc *sc) | 
| 3485 | { | 
| 3486 | #ifdef UAUDIO_DEBUG | 
| 3487 | 	struct timeval tv; | 
| 3488 | #endif | 
| 3489 | 	struct uaudio_stream *s = &sc->rstream; | 
| 3490 | 	struct uaudio_alt *a = sc->params->ralt; | 
| 3491 | 	struct uaudio_xfer *xfer; | 
| 3492 | 	unsigned int fsize, bpf; | 
| 3493 | 	int done; | 
| 3494 | 	int err; | 
| 3495 |   | 
| 3496 | 	xfer = s->data_xfers + s->data_nextxfer; | 
| 3497 | 	bpf = a->bps * a->nch; | 
| 3498 | 	xfer->nframes = 0; | 
| 3499 | 	done = s->ring_offs; | 
| 3500 |   | 
| 3501 | 	while (1) { | 
| 3502 | 		 | 
| 3503 | 		  | 
| 3504 | 		  | 
| 3505 | 		if ((xfer->nframes & s->nframes_mask) == 0 && | 
| 3506 | 		    done > s->safe_blksz) { | 
| 3507 | 		done: | 
| 3508 | 			xfer->size = done - s->ring_offs; | 
| 3509 | 			s->ring_offs = done - s->ring_blksz; | 
| 3510 | 			break; | 
| 3511 | 		} | 
| 3512 |   | 
| 3513 | 		 | 
| 3514 | 		  | 
| 3515 | 		  | 
| 3516 | 		if (xfer->nframes == s->nframes_max) { | 
| 3517 | 			printf("%s: too many frames for rec xfer: " | 
| 3518 | 			    "done = %d, blksz = %d\n", | 
| 3519 | 			    DEVNAME(sc), done, s->ring_blksz); | 
| 3520 | 			goto done; | 
| 3521 | 		} | 
| 3522 |   | 
| 3523 | 		 | 
| 3524 | 		  | 
| 3525 | 		  | 
| 3526 | 		  | 
| 3527 | 		s->spf_remain += s->spf; | 
| 3528 | 		fsize = s->spf_remain / UAUDIO_SPF_DIV * bpf; | 
| 3529 | 		s->spf_remain %= UAUDIO_SPF_DIV; | 
| 3530 | 		done += fsize; | 
| 3531 | 		xfer->sizes[xfer->nframes] = s->maxpkt; | 
| 3532 | 		xfer->nframes++; | 
| 3533 | 	} | 
| 3534 |   | 
| 3535 | #ifdef UAUDIO_DEBUG | 
| 3536 | 	if (uaudio_debug >= 3) { | 
| 3537 | 		getmicrotime(&tv); | 
| 3538 | 		printf("%s: %llu.%06lu: " | 
| 3539 | 		    "%u fr, %d bytes (max %d), offs = %d\n", | 
| 3540 | 		    __func__, tv.tv_sec, tv.tv_usec, | 
| 3541 | 		    xfer->nframes, xfer->size, | 
| 3542 | 		    s->maxpkt * xfer->nframes, s->ring_offs); | 
| 3543 | 	} | 
| 3544 | #endif | 
| 3545 |   | 
| 3546 | 	 | 
| 3547 | 	if (xfer->nframes == 0) { | 
| 3548 | 		printf("%s: zero frame rec xfer\n", DEVNAME(sc)); | 
| 3549 | 		return; | 
| 3550 | 	} | 
| 3551 |   | 
| 3552 | #ifdef UAUDIO_DEBUG | 
| 3553 | 	memset(xfer->buf, 0xd0, s->maxpkt * xfer->nframes); | 
| 3554 | #endif | 
| 3555 | 	usbd_setup_isoc_xfer(xfer->usb_xfer, s->data_pipe, sc, | 
| 3556 | 	    xfer->sizes, xfer->nframes, USBD_NO_COPY | USBD_SHORT_XFER_OK, | 
| 3557 | 	    uaudio_rdata_intr); | 
| 3558 |   | 
| 3559 | 	err = usbd_transfer(xfer->usb_xfer); | 
| 3560 | 	if (err != 0 && err != USBD_IN_PROGRESS) | 
| 3561 | 		printf("%s: rec xfer, err = %d\n", DEVNAME(sc), err); | 
| 3562 |   | 
| 3563 | 	if (++s->data_nextxfer == s->nxfers) | 
| 3564 | 		s->data_nextxfer = 0; | 
| 3565 | } | 
| 3566 |   | 
| 3567 |  | 
| 3568 |   | 
| 3569 |   | 
| 3570 | void | 
| 3571 | uaudio_rdata_intr(struct usbd_xfer *usb_xfer, void *arg, usbd_status status) | 
| 3572 | { | 
| 3573 | #ifdef UAUDIO_DEBUG | 
| 3574 | 	struct timeval tv; | 
| 3575 | #endif | 
| 3576 | 	struct uaudio_softc *sc = arg; | 
| 3577 | 	struct uaudio_stream *s = &sc->rstream; | 
| 3578 | 	struct uaudio_alt *a = sc->params->ralt; | 
| 3579 | 	struct uaudio_xfer *xfer; | 
| 3580 | 	unsigned char *buf, *framebuf; | 
| 3581 | 	unsigned int count, fsize, fsize_min, nframes, bpf; | 
| 3582 | 	unsigned int data_size, null_count; | 
| 3583 | 	unsigned int nintr; | 
| 3584 |   | 
| 3585 | 	if (status != 0) { | 
| 3586 | 		DPRINTF("%s: xfer status = %d\n", __func__, status); | 
| 3587 | 		return; | 
| 3588 | 	} | 
| 3589 |   | 
| 3590 | 	xfer = s->data_xfers + s->data_nextxfer; | 
| 3591 | 	if (xfer->usb_xfer != usb_xfer) { | 
| 3592 | 		DPRINTF("%s: wrong xfer\n", __func__); | 
| 3593 | 		return; | 
| 3594 | 	} | 
| 3595 |   | 
| 3596 | 	bpf = a->bps * a->nch; | 
| 3597 | 	framebuf = xfer->buf; | 
| 3598 | 	nframes = 0; | 
| 3599 | 	null_count = 0; | 
| 3600 | 	data_size = 0; | 
| 3601 | 	fsize_min = s->spf_min / UAUDIO_SPF_DIV; | 
| 3602 | 	for (nframes = 0; nframes < xfer->nframes; nframes++) { | 
| 3603 |   | 
| 3604 | 		 | 
| 3605 | 		  | 
| 3606 | 		  | 
| 3607 | 		  | 
| 3608 | 		  | 
| 3609 | 		fsize = xfer->sizes[nframes]; | 
| 3610 | 		if (fsize < fsize_min) { | 
| 3611 | 			s->spf_remain += s->spf; | 
| 3612 | 			fsize = s->spf_remain / UAUDIO_SPF_DIV * bpf; | 
| 3613 | 			s->spf_remain %= UAUDIO_SPF_DIV; | 
| 3614 | 			memset(framebuf, 0, fsize); | 
| 3615 | 			null_count++; | 
| 3616 | 		} | 
| 3617 | 		data_size += fsize; | 
| 3618 |   | 
| 3619 | 		 | 
| 3620 | 		  | 
| 3621 | 		  | 
| 3622 | 		  | 
| 3623 | 		buf = framebuf; | 
| 3624 | 		while (fsize > 0) { | 
| 3625 | 			count = s->ring_end - s->ring_pos; | 
| 3626 | 			if (count > fsize) | 
| 3627 | 				count = fsize; | 
| 3628 | 			memcpy(s->ring_pos, buf, count); | 
| 3629 | 			s->ring_pos += count; | 
| 3630 | 			if (s->ring_pos == s->ring_end) | 
| 3631 | 				s->ring_pos = s->ring_start; | 
| 3632 | 			buf += count; | 
| 3633 | 			fsize -= count; | 
| 3634 | 		} | 
| 3635 |   | 
| 3636 | 		framebuf += s->maxpkt; | 
| 3637 | 	} | 
| 3638 |   | 
| 3639 | 	s->ring_offs += data_size - xfer->size; | 
| 3640 | 	s->ring_icnt += data_size; | 
| 3641 |   | 
| 3642 | 	sc->diff_nsamp -= data_size / | 
| 3643 | 	    (sc->params->ralt->nch * sc->params->ralt->bps); | 
| 3644 | 	sc->diff_nframes -= xfer->nframes; | 
| 3645 |   | 
| 3646 | 	sc->adjspf_age += xfer->nframes; | 
| 3647 | 	if (sc->adjspf_age >= sc->ufps / 8) { | 
| 3648 | 		sc->adjspf_age -= sc->ufps / 8; | 
| 3649 | 		uaudio_adjspf(sc); | 
| 3650 | 	} | 
| 3651 |   | 
| 3652 | #ifdef UAUDIO_DEBUG | 
| 3653 | 	if (uaudio_debug >= 2) { | 
| 3654 | 		getmicrotime(&tv); | 
| 3655 | 		printf("%s: %llu.%06lu: %u: " | 
| 3656 | 		    "%u bytes of %u, offs -> %d\n", | 
| 3657 | 		    __func__, tv.tv_sec, tv.tv_usec, | 
| 3658 | 		    s->data_nextxfer, data_size, xfer->size, s->ring_offs); | 
| 3659 | 	} | 
| 3660 | 	if (null_count > 0) { | 
| 3661 | 		DPRINTF("%s: %u null frames out of %u: incomplete record xfer\n", | 
| 3662 | 		    DEVNAME(sc), null_count, xfer->nframes); | 
| 3663 | 	} | 
| 3664 | #endif | 
| 3665 | 	uaudio_rdata_xfer(sc); | 
| 3666 |   | 
| 3667 | 	nintr = 0; | 
| 3668 | 	mtx_enter(&audio_lock); | 
| 3669 | 	while (s->ring_icnt >= s->ring_blksz) { | 
| 3670 | 		s->intr(s->arg); | 
| 3671 | 		s->ring_icnt -= s->ring_blksz; | 
| 3672 | 		nintr++; | 
| 3673 | 	} | 
| 3674 | 	mtx_leave(&audio_lock); | 
| 3675 | 	if (nintr != 1) | 
| 3676 | 		printf("%s: %u: bad rec intr count\n", DEVNAME(sc), nintr); | 
| 3677 | } | 
| 3678 |   | 
| 3679 |  | 
| 3680 |   | 
| 3681 |   | 
| 3682 |   | 
| 3683 | void | 
| 3684 | uaudio_trigger(struct uaudio_softc *sc) | 
| 3685 | { | 
| 3686 | 	int i, s; | 
| 3687 |   | 
| 3688 | 	if (sc->mode != sc->trigger_mode) | 
| 3689 | 		return; | 
| 3690 |   | 
| 3691 | 	DPRINTF("%s: preparing\n", __func__); | 
| 3692 | 	if (sc->mode & AUMODE_PLAY) { | 
| 3693 | 		for (i = 0; i < sc->pstream.nxfers; i++) | 
| 3694 | 			uaudio_pdata_calcsizes(sc, sc->pstream.data_xfers + i); | 
| 3695 |   | 
| 3696 | 		uaudio_pdata_copy(sc); | 
| 3697 | 	} | 
| 3698 |   | 
| 3699 | 	sc->diff_nsamp = 0; | 
| 3700 | 	sc->diff_nframes = 0; | 
| 3701 | 	sc->adjspf_age = 0; | 
| 3702 |   | 
| 3703 | 	DPRINTF("%s: starting\n", __func__); | 
| 3704 | 	s = splusb(); | 
| 3705 | 	for (i = 0; i < UAUDIO_NXFERS_MAX; i++) { | 
| 3706 | 		if ((sc->mode & AUMODE_PLAY) && i < sc->pstream.nxfers) { | 
| 3707 | 			if (sc->pstream.sync_pipe) | 
| 3708 | 				uaudio_psync_xfer(sc); | 
| 3709 | 			uaudio_pdata_xfer(sc); | 
| 3710 | 		} | 
| 3711 | 		if ((sc->mode & AUMODE_RECORD) && i < sc->rstream.nxfers) | 
| 3712 | 			uaudio_rdata_xfer(sc); | 
| 3713 | 	} | 
| 3714 | 	splx(s); | 
| 3715 | } | 
| 3716 |   | 
| 3717 | void | 
| 3718 | uaudio_print(struct uaudio_softc *sc) | 
| 3719 | { | 
| 3720 | 	struct uaudio_unit *u; | 
| 3721 | 	struct uaudio_mixent *m; | 
| 3722 | 	struct uaudio_params *p; | 
| 3723 | 	int pchan = 0, rchan = 0, async = 0; | 
| 3724 | 	int nctl = 0; | 
| 3725 |   | 
| 3726 | 	for (u = sc->unit_list; u != NULL; u = u->unit_next) { | 
| 3727 | 		m = u->mixent_list; | 
| 3728 | 		while (1) { | 
| 3729 | 			uaudio_mixer_skip(&m); | 
| 3730 | 			if (m == NULL) | 
| 3731 | 				break; | 
| 3732 | 			m = m->next; | 
| 3733 | 			nctl++; | 
| 3734 | 		} | 
| 3735 | 	} | 
| 3736 |   | 
| 3737 | 	for (p = sc->params_list; p != NULL; p = p->next) { | 
| 3738 | 		if (p->palt && p->palt->nch > pchan) | 
| 3739 | 			pchan = p->palt->nch; | 
| 3740 | 		if (p->ralt && p->ralt->nch > rchan) | 
| 3741 | 			rchan = p->ralt->nch; | 
| 3742 | 		if (p->palt && p->palt->sync_addr) | 
| 3743 | 			async = 1; | 
| 3744 | 		if (p->ralt && p->ralt->sync_addr) | 
| 3745 | 			async = 1; | 
| 3746 | 	} | 
| 3747 |   | 
| 3748 | 	printf("%s: class v%d, %s, %s, channels: %d play, %d rec, %d ctls\n", | 
| 3749 | 	    DEVNAME(sc), | 
| 3750 | 	    sc->version >> 8, | 
| 3751 | 	    sc->ufps == 1000 ? "full-speed" : "high-speed", | 
| 3752 | 	    async ? "async" : "sync", | 
| 3753 | 	    pchan, rchan, nctl); | 
| 3754 | } | 
| 3755 |   | 
| 3756 | int | 
| 3757 | uaudio_match(struct device *parent, void *match, void *aux) | 
| 3758 | { | 
| 3759 | 	struct usb_attach_arg *arg = aux; | 
| 3760 | 	struct usb_interface_descriptor *idesc; | 
| 3761 |   | 
| 3762 | 	if (arg->iface == NULL || arg->device == NULL) | 
| 3763 | 		return UMATCH_NONE; | 
| 3764 |   | 
| 3765 | 	idesc = usbd_get_interface_descriptor(arg->iface); | 
| 3766 | 	if (idesc == NULL) { | 
| 3767 | 		DPRINTF("%s: couldn't get idesc\n", __func__); | 
| 3768 | 		return UMATCH_NONE; | 
| 3769 | 	} | 
| 3770 |   | 
| 3771 | 	if (idesc->bInterfaceClass != UICLASS_AUDIO || | 
| 3772 | 	    idesc->bInterfaceSubClass != UISUBCLASS_AUDIOSTREAM) | 
| 3773 | 		return UMATCH_NONE; | 
| 3774 |   | 
| 3775 | 	return UMATCH_VENDOR_PRODUCT_CONF_IFACE; | 
| 3776 | } | 
| 3777 |   | 
| 3778 | void | 
| 3779 | uaudio_attach(struct device *parent, struct device *self, void *aux) | 
| 3780 | { | 
| 3781 | 	struct uaudio_softc *sc = (struct uaudio_softc *)self; | 
| 3782 | 	struct usb_attach_arg *arg = aux; | 
| 3783 | 	struct usb_config_descriptor *cdesc; | 
| 3784 | 	struct uaudio_blob desc; | 
| 3785 |   | 
| 3786 | 	 | 
| 3787 | 	  | 
| 3788 | 	  | 
| 3789 | 	  | 
| 3790 |   | 
| 3791 | 	cdesc = usbd_get_config_descriptor(arg->device); | 
| 3792 | 	if (cdesc == NULL) | 
 | 1  | Assuming 'cdesc' is not equal to NULL |  |  
  | 
 |  | 
| 3793 | 		return; | 
| 3794 |   | 
| 3795 | 	desc.rptr = (unsigned char *)cdesc; | 
| 3796 | 	desc.wptr = desc.rptr + UGETW(cdesc->wTotalLength); | 
| 3797 |   | 
| 3798 | 	sc->udev = arg->device; | 
| 3799 | 	sc->unit_list = NULL; | 
| 3800 | 	sc->names = NULL; | 
| 3801 | 	sc->alts = NULL; | 
| 3802 | 	sc->params_list = NULL; | 
| 3803 | 	sc->clock = NULL; | 
| 3804 | 	sc->params = NULL; | 
| 3805 | 	sc->rate = 0; | 
| 3806 | 	sc->mode = 0; | 
| 3807 | 	sc->trigger_mode = 0; | 
| 3808 | 	sc->copy_todo = 0; | 
| 3809 |   | 
| 3810 | 	 | 
| 3811 | 	  | 
| 3812 | 	  | 
| 3813 | 	  | 
| 3814 | 	  | 
| 3815 | 	  | 
| 3816 | 	  | 
| 3817 | 	  | 
| 3818 | 	switch (sc->udev->speed) { | 
 | 3  |  | Control jumps to 'case 3:'  at line 3825 |  |  
  | 
| 3819 | 	case USB_SPEED_LOW: | 
| 3820 | 	case USB_SPEED_FULL: | 
| 3821 | 		sc->ufps = 1000; | 
| 3822 | 		sc->sync_pktsz = 3; | 
| 3823 | 		sc->host_nframes = 50; | 
| 3824 | 		break; | 
| 3825 | 	case USB_SPEED_HIGH: | 
| 3826 | 	case USB_SPEED_SUPER: | 
| 3827 | 		sc->ufps = 8000; | 
| 3828 | 		sc->sync_pktsz = 4; | 
| 3829 | 		sc->host_nframes = 240; | 
| 3830 | 		break; | 
| 3831 | 	default: | 
| 3832 | 		printf("%s: unsupported bus speed\n", DEVNAME(sc)); | 
| 3833 | 		return; | 
| 3834 | 	} | 
| 3835 |   | 
| 3836 | 	if (!uaudio_process_conf(sc, &desc)) | 
 | 4  |  |  Execution continues on line 3836 |  |  
  | 
 | 5  |  | Calling 'uaudio_process_conf' |  |  
  | 
| 3837 | 		return; | 
| 3838 |   | 
| 3839 | #ifdef UAUDIO_DEBUG | 
| 3840 | 	if (uaudio_debug) | 
| 3841 | 		uaudio_conf_print(sc); | 
| 3842 | #endif | 
| 3843 | 	 | 
| 3844 | 	uaudio_print(sc); | 
| 3845 |   | 
| 3846 | 	audio_attach_mi(&uaudio_hw_if, sc, arg->cookie, &sc->dev); | 
| 3847 | } | 
| 3848 |   | 
| 3849 | int | 
| 3850 | uaudio_detach(struct device *self, int flags) | 
| 3851 | { | 
| 3852 | 	struct uaudio_softc *sc = (struct uaudio_softc *)self; | 
| 3853 | 	struct uaudio_unit *unit; | 
| 3854 | 	struct uaudio_params *params; | 
| 3855 | 	struct uaudio_alt *alt; | 
| 3856 | 	struct uaudio_name *name; | 
| 3857 | 	struct uaudio_mixent *mixent; | 
| 3858 | 	int rv; | 
| 3859 |   | 
| 3860 | 	rv = config_detach_children(self, flags); | 
| 3861 |   | 
| 3862 | 	usbd_ref_wait(sc->udev); | 
| 3863 |   | 
| 3864 | 	while ((alt = sc->alts) != NULL) { | 
| 3865 | 		sc->alts = alt->next; | 
| 3866 | 		free(alt, M_USBDEV, sizeof(struct uaudio_alt)); | 
| 3867 | 	} | 
| 3868 |   | 
| 3869 | 	while ((params = sc->params_list) != NULL) { | 
| 3870 | 		sc->params_list = params->next; | 
| 3871 | 		free(params, M_USBDEV, sizeof(struct uaudio_params)); | 
| 3872 | 	} | 
| 3873 |   | 
| 3874 | 	while ((unit = sc->unit_list) != NULL) { | 
| 3875 | 		sc->unit_list = unit->unit_next; | 
| 3876 | 		while ((mixent = unit->mixent_list) != NULL) { | 
| 3877 | 			unit->mixent_list = mixent->next; | 
| 3878 | 			uaudio_ranges_clear(&mixent->ranges); | 
| 3879 | 			free(mixent, M_USBDEV, sizeof(struct uaudio_mixent)); | 
| 3880 | 		} | 
| 3881 | 		uaudio_ranges_clear(&unit->rates); | 
| 3882 | 		free(unit, M_USBDEV, sizeof(struct uaudio_unit)); | 
| 3883 | 	} | 
| 3884 |   | 
| 3885 | 	while ((name = sc->names)) { | 
| 3886 | 		sc->names = name->next; | 
| 3887 | 		free(name, M_USBDEV, sizeof(struct uaudio_name)); | 
| 3888 | 	} | 
| 3889 |   | 
| 3890 | 	return rv; | 
| 3891 | } | 
| 3892 |   | 
| 3893 | int | 
| 3894 | uaudio_open(void *self, int flags) | 
| 3895 | { | 
| 3896 | 	struct uaudio_softc *sc = self; | 
| 3897 | 	struct uaudio_params *p; | 
| 3898 |   | 
| 3899 | 	if (usbd_is_dying(sc->udev)) | 
| 3900 | 		return EIO; | 
| 3901 |   | 
| 3902 | 	usbd_ref_incr(sc->udev); | 
| 3903 |   | 
| 3904 | 	flags &= (FREAD | FWRITE); | 
| 3905 |   | 
| 3906 | 	for (p = sc->params_list; p != NULL; p = p->next) { | 
| 3907 | 		switch (flags) { | 
| 3908 | 		case FWRITE: | 
| 3909 | 			if (!p->palt) | 
| 3910 | 				break; | 
| 3911 | 			sc->mode = AUMODE_PLAY; | 
| 3912 | 			return 0; | 
| 3913 | 		case FREAD: | 
| 3914 | 			if (!p->ralt) | 
| 3915 | 				break; | 
| 3916 | 			sc->mode = AUMODE_RECORD; | 
| 3917 | 			return 0; | 
| 3918 | 		case FREAD | FWRITE: | 
| 3919 | 			if (!(p->ralt && p->palt)) | 
| 3920 | 				break; | 
| 3921 | 			sc->mode = AUMODE_RECORD | AUMODE_PLAY; | 
| 3922 | 			return 0; | 
| 3923 | 		} | 
| 3924 | 	} | 
| 3925 |   | 
| 3926 | 	usbd_ref_decr(sc->udev); | 
| 3927 | 	return ENXIO; | 
| 3928 | } | 
| 3929 |   | 
| 3930 | void | 
| 3931 | uaudio_close(void *self) | 
| 3932 | { | 
| 3933 | 	struct uaudio_softc *sc = self; | 
| 3934 |   | 
| 3935 | 	sc->mode = 0; | 
| 3936 | 	usbd_ref_decr(sc->udev); | 
| 3937 | } | 
| 3938 |   | 
| 3939 | int | 
| 3940 | uaudio_set_params(void *self, int setmode, int usemode, | 
| 3941 |     struct audio_params *ap, struct audio_params *ar) | 
| 3942 | { | 
| 3943 | 	struct uaudio_softc *sc = (struct uaudio_softc *)self; | 
| 3944 | 	struct uaudio_params *p, *best_mode, *best_rate, *best_nch; | 
| 3945 | 	int rate, rateindex; | 
| 3946 |   | 
| 3947 | #ifdef DIAGNOSTIC | 
| 3948 | 	if (setmode != usemode || setmode != sc->mode) { | 
| 3949 | 		printf("%s: bad call to uaudio_set_params()\n", DEVNAME(sc)); | 
| 3950 | 		return EINVAL; | 
| 3951 | 	} | 
| 3952 | 	if (sc->mode == 0) { | 
| 3953 | 		printf("%s: uaudio_set_params(): not open\n", DEVNAME(sc)); | 
| 3954 | 		return EINVAL; | 
| 3955 | 	} | 
| 3956 | #endif | 
| 3957 | 	 | 
| 3958 | 	  | 
| 3959 | 	  | 
| 3960 | 	rate = (sc->mode & AUMODE_PLAY) ? ap->sample_rate : ar->sample_rate; | 
| 3961 | 	rateindex = uaudio_rates_indexof(~0, rate); | 
| 3962 |   | 
| 3963 | 	DPRINTF("%s: rate %d -> %d (index %d)\n", __func__, | 
| 3964 | 	    rate, uaudio_rates[rateindex], rateindex); | 
| 3965 |   | 
| 3966 | 	best_mode = best_rate = best_nch = NULL; | 
| 3967 |   | 
| 3968 | 	for (p = sc->params_list; p != NULL; p = p->next) { | 
| 3969 |   | 
| 3970 | 		 | 
| 3971 | 		if (sc->mode & AUMODE_PLAY) { | 
| 3972 | 			if (p->palt == NULL) | 
| 3973 | 				continue; | 
| 3974 | 		} | 
| 3975 | 		if (sc->mode & AUMODE_RECORD) { | 
| 3976 | 			if (p->ralt == NULL) | 
| 3977 | 				continue; | 
| 3978 | 		} | 
| 3979 | 		if (best_mode == NULL) | 
| 3980 | 			best_mode = p; | 
| 3981 |   | 
| 3982 | 		 | 
| 3983 | 		if ((uaudio_getrates(sc, p) & (1 << rateindex)) == 0) | 
| 3984 | 			continue; | 
| 3985 | 		if (best_rate == NULL) | 
| 3986 | 			best_rate = p; | 
| 3987 |   | 
| 3988 | 		 | 
| 3989 | 		if (sc->mode & AUMODE_PLAY) { | 
| 3990 | 			if (p->palt->nch != ap->channels) | 
| 3991 | 				continue; | 
| 3992 | 		} | 
| 3993 | 		if (sc->mode & AUMODE_RECORD) { | 
| 3994 | 			if (p->ralt->nch != ar->channels) | 
| 3995 | 				continue; | 
| 3996 | 		} | 
| 3997 | 		if (best_nch == NULL) | 
| 3998 | 			best_nch = p; | 
| 3999 |   | 
| 4000 | 		 | 
| 4001 | 		if (sc->mode & AUMODE_PLAY) { | 
| 4002 | 			if (p->palt->bits != ap->precision) | 
| 4003 | 				continue; | 
| 4004 | 		} | 
| 4005 | 		if (sc->mode & AUMODE_RECORD) { | 
| 4006 | 			if (p->ralt->bits != ar->precision) | 
| 4007 | 				continue; | 
| 4008 | 		} | 
| 4009 |   | 
| 4010 | 		 | 
| 4011 | 		break; | 
| 4012 | 	} | 
| 4013 |   | 
| 4014 | 	if (p == NULL) { | 
| 4015 | 		if (best_nch) | 
| 4016 | 			p = best_nch; | 
| 4017 | 		else if (best_rate) | 
| 4018 | 			p = best_rate; | 
| 4019 | 		else if (best_mode) | 
| 4020 | 			p = best_mode; | 
| 4021 | 		else | 
| 4022 | 			return ENOTTY; | 
| 4023 | 	} | 
| 4024 |   | 
| 4025 | 	 | 
| 4026 | 	  | 
| 4027 | 	  | 
| 4028 | 	  | 
| 4029 | 	rateindex = uaudio_rates_indexof(uaudio_getrates(sc, p), rate); | 
| 4030 | 	if (rateindex < 0) | 
| 4031 | 		return ENOTTY; | 
| 4032 |   | 
| 4033 | 	sc->params = p; | 
| 4034 | 	sc->rate = uaudio_rates[rateindex]; | 
| 4035 |   | 
| 4036 | 	DPRINTF("%s: rate = %u\n", __func__, sc->rate); | 
| 4037 |   | 
| 4038 | 	if (sc->mode & AUMODE_PLAY) { | 
| 4039 | 		ap->sample_rate = sc->rate; | 
| 4040 | 		ap->precision = p->palt->bits; | 
| 4041 | 		ap->encoding = AUDIO_ENCODING_SLINEAR_LE; | 
| 4042 | 		ap->bps = p->palt->bps; | 
| 4043 | 		ap->msb = 1; | 
| 4044 | 		ap->channels = p->palt->nch; | 
| 4045 | 	} | 
| 4046 | 	if (sc->mode & AUMODE_RECORD) { | 
| 4047 | 		ar->sample_rate = sc->rate; | 
| 4048 | 		ar->precision = p->ralt->bits; | 
| 4049 | 		ar->encoding = AUDIO_ENCODING_SLINEAR_LE; | 
| 4050 | 		ar->bps = p->ralt->bps; | 
| 4051 | 		ar->msb = 1; | 
| 4052 | 		ar->channels = p->ralt->nch; | 
| 4053 | 	} | 
| 4054 |   | 
| 4055 | 	return 0; | 
| 4056 | } | 
| 4057 |   | 
| 4058 | unsigned int | 
| 4059 | uaudio_set_blksz(void *self, int mode, | 
| 4060 |     struct audio_params *p, struct audio_params *r, unsigned int blksz) | 
| 4061 | { | 
| 4062 | 	struct uaudio_softc *sc = self; | 
| 4063 | 	unsigned int fps, fps_min; | 
| 4064 | 	unsigned int blksz_max, blksz_min; | 
| 4065 |   | 
| 4066 | 	 | 
| 4067 | 	  | 
| 4068 | 	  | 
| 4069 | 	fps_min = sc->ufps; | 
| 4070 | 	if (mode & AUMODE_PLAY) { | 
| 4071 | 		fps = sc->params->palt->fps; | 
| 4072 | 		if (fps_min > fps) | 
| 4073 | 			fps_min = fps; | 
| 4074 | 	} | 
| 4075 | 	if (mode & AUMODE_RECORD) { | 
| 4076 | 		fps = sc->params->ralt->fps; | 
| 4077 | 		if (fps_min > fps) | 
| 4078 | 			fps_min = fps; | 
| 4079 | 	} | 
| 4080 | 	blksz_min = (sc->rate * 2 + fps_min - 1) / fps_min; | 
| 4081 |   | 
| 4082 | 	 | 
| 4083 | 	  | 
| 4084 | 	  | 
| 4085 | 	  | 
| 4086 | 	blksz_max = sc->rate * (sc->host_nframes / UAUDIO_NXFERS_MIN) / | 
| 4087 | 	    sc->ufps * 85 / 100; | 
| 4088 |   | 
| 4089 | 	if (blksz > blksz_max) | 
| 4090 | 		blksz = blksz_max; | 
| 4091 | 	else if (blksz < blksz_min) | 
| 4092 | 		blksz = blksz_min; | 
| 4093 |   | 
| 4094 | 	return blksz; | 
| 4095 | } | 
| 4096 |   | 
| 4097 | int | 
| 4098 | uaudio_trigger_output(void *self, void *start, void *end, int blksz, | 
| 4099 |     void (*intr)(void *), void *arg, struct audio_params *param) | 
| 4100 | { | 
| 4101 | 	struct uaudio_softc *sc = self; | 
| 4102 | 	int err; | 
| 4103 |   | 
| 4104 | 	err = uaudio_stream_open(sc, | 
| 4105 | 	    AUMODE_PLAY, start, end, blksz, intr, arg); | 
| 4106 | 	if (err) | 
| 4107 | 		return err; | 
| 4108 |   | 
| 4109 | 	sc->trigger_mode |= AUMODE_PLAY; | 
| 4110 | 	uaudio_trigger(sc); | 
| 4111 | 	return 0; | 
| 4112 | } | 
| 4113 |   | 
| 4114 | int | 
| 4115 | uaudio_trigger_input(void *self, void *start, void *end, int blksz, | 
| 4116 |     void (*intr)(void *), void *arg, struct audio_params *param) | 
| 4117 | { | 
| 4118 | 	struct uaudio_softc *sc = self; | 
| 4119 | 	int err; | 
| 4120 |   | 
| 4121 | 	err = uaudio_stream_open(sc, | 
| 4122 | 	    AUMODE_RECORD, start, end, blksz, intr, arg); | 
| 4123 | 	if (err) | 
| 4124 | 		return err; | 
| 4125 |   | 
| 4126 | 	sc->trigger_mode |= AUMODE_RECORD; | 
| 4127 | 	uaudio_trigger(sc); | 
| 4128 | 	return 0; | 
| 4129 | } | 
| 4130 |   | 
| 4131 | void | 
| 4132 | uaudio_copy_output(void *self, size_t todo) | 
| 4133 | { | 
| 4134 | 	struct uaudio_softc *sc = (struct uaudio_softc *)self; | 
| 4135 | 	int s; | 
| 4136 |   | 
| 4137 | 	s = splusb(); | 
| 4138 | 	sc->copy_todo += todo; | 
| 4139 |   | 
| 4140 | #ifdef UAUDIO_DEBUG | 
| 4141 | 	if (uaudio_debug >= 3) { | 
| 4142 | 		printf("%s: copy_todo -> %zd (+%zd)\n", __func__, | 
| 4143 | 		    sc->copy_todo, todo); | 
| 4144 | 	} | 
| 4145 | #endif | 
| 4146 |   | 
| 4147 | 	if (sc->mode == sc->trigger_mode) | 
| 4148 | 		uaudio_pdata_copy(sc); | 
| 4149 | 	splx(s); | 
| 4150 | } | 
| 4151 |   | 
| 4152 | void | 
| 4153 | uaudio_underrun(void *self) | 
| 4154 | { | 
| 4155 | 	struct uaudio_softc *sc = (struct uaudio_softc *)self; | 
| 4156 | 	struct uaudio_stream *s = &sc->pstream; | 
| 4157 |   | 
| 4158 | 	sc->copy_todo += s->ring_blksz; | 
| 4159 |   | 
| 4160 | #ifdef UAUDIO_DEBUG | 
| 4161 | 	if (uaudio_debug >= 3) | 
| 4162 | 		printf("%s: copy_todo -> %zd\n", __func__, sc->copy_todo); | 
| 4163 | #endif | 
| 4164 |   | 
| 4165 | 	 | 
| 4166 | 	uaudio_pdata_copy(sc); | 
| 4167 | } | 
| 4168 |   | 
| 4169 | int | 
| 4170 | uaudio_halt_output(void *self) | 
| 4171 | { | 
| 4172 | 	struct uaudio_softc *sc = (struct uaudio_softc *)self; | 
| 4173 |   | 
| 4174 | 	uaudio_stream_close(sc, AUMODE_PLAY); | 
| 4175 | 	sc->trigger_mode &= ~AUMODE_PLAY; | 
| 4176 | 	sc->copy_todo = 0; | 
| 4177 | 	return 0; | 
| 4178 | } | 
| 4179 |   | 
| 4180 | int | 
| 4181 | uaudio_halt_input(void *self) | 
| 4182 | { | 
| 4183 | 	struct uaudio_softc *sc = (struct uaudio_softc *)self; | 
| 4184 |   | 
| 4185 | 	uaudio_stream_close(sc, AUMODE_RECORD); | 
| 4186 | 	sc->trigger_mode &= ~AUMODE_RECORD; | 
| 4187 | 	return 0; | 
| 4188 | } | 
| 4189 |   | 
| 4190 | int | 
| 4191 | uaudio_get_port_do(struct uaudio_softc *sc, struct mixer_ctrl *ctl) | 
| 4192 | { | 
| 4193 | 	struct uaudio_unit *u; | 
| 4194 | 	struct uaudio_mixent *m; | 
| 4195 | 	unsigned char req_buf[4]; | 
| 4196 | 	struct uaudio_blob p; | 
| 4197 | 	int i, nch, val, req_num; | 
| 4198 |   | 
| 4199 | 	if (!uaudio_mixer_byindex(sc, ctl->dev, &u, &m)) | 
| 4200 | 		return ENOENT; | 
| 4201 |   | 
| 4202 | 	switch (sc->version) { | 
| 4203 | 	case UAUDIO_V1: | 
| 4204 | 		req_num = UAUDIO_V1_REQ_GET_CUR; | 
| 4205 | 		break; | 
| 4206 | 	case UAUDIO_V2: | 
| 4207 | 		req_num = UAUDIO_V2_REQ_CUR; | 
| 4208 | 	} | 
| 4209 |   | 
| 4210 | 	switch (m->type) { | 
| 4211 | 	case UAUDIO_MIX_SW: | 
| 4212 | 		p.rptr = p.wptr = req_buf; | 
| 4213 | 		if (!uaudio_req(sc, | 
| 4214 | 			UT_READ_CLASS_INTERFACE, | 
| 4215 | 			req_num, | 
| 4216 | 			m->req_sel, | 
| 4217 | 			m->chan < 0 ? 0 : m->chan, | 
| 4218 | 			sc->ctl_ifnum, | 
| 4219 | 			u->id, | 
| 4220 | 			req_buf, | 
| 4221 | 			1)) | 
| 4222 | 			return EIO; | 
| 4223 | 		p.wptr++; | 
| 4224 | 		if (!uaudio_getnum(&p, 1, &val)) | 
| 4225 | 			return EIO; | 
| 4226 | 		ctl->un.ord = !!val; | 
| 4227 | 		break; | 
| 4228 | 	case UAUDIO_MIX_NUM: | 
| 4229 | 		nch = uaudio_mixer_nchan(m, NULL); | 
| 4230 | 		ctl->un.value.num_channels = nch; | 
| 4231 | 		for (i = 0; i < nch; i++) { | 
| 4232 | 			p.rptr = p.wptr = req_buf; | 
| 4233 | 			if (!uaudio_req(sc, | 
| 4234 | 				UT_READ_CLASS_INTERFACE, | 
| 4235 | 				req_num, | 
| 4236 | 				m->req_sel, | 
| 4237 | 				m->chan < 0 ? 0 : i + 1, | 
| 4238 | 				sc->ctl_ifnum, | 
| 4239 | 				u->id, | 
| 4240 | 				req_buf, | 
| 4241 | 				2)) | 
| 4242 | 				return EIO; | 
| 4243 | 			p.wptr += 2; | 
| 4244 | 			if (!uaudio_getnum(&p, 2, &val)) | 
| 4245 | 				return EIO; | 
| 4246 | 			ctl->un.value.level[i] = | 
| 4247 | 			    uaudio_ranges_decode(&m->ranges, | 
| 4248 | 				uaudio_sign_expand(val, 2)); | 
| 4249 | 			m = m->next; | 
| 4250 | 		} | 
| 4251 | 		break; | 
| 4252 | 	case UAUDIO_MIX_ENUM: | 
| 4253 | 		 | 
| 4254 | 		break; | 
| 4255 | 	} | 
| 4256 | 	return 0; | 
| 4257 | } | 
| 4258 |   | 
| 4259 | int | 
| 4260 | uaudio_set_port_do(struct uaudio_softc *sc, struct mixer_ctrl *ctl) | 
| 4261 | { | 
| 4262 | 	struct uaudio_unit *u; | 
| 4263 | 	struct uaudio_mixent *m; | 
| 4264 | 	unsigned char req_buf[4]; | 
| 4265 | 	unsigned int val; | 
| 4266 | 	int i, nch; | 
| 4267 |   | 
| 4268 | 	if (!uaudio_mixer_byindex(sc, ctl->dev, &u, &m)) | 
| 4269 | 		return ENOENT; | 
| 4270 |   | 
| 4271 | 	switch (m->type) { | 
| 4272 | 	case UAUDIO_MIX_SW: | 
| 4273 | 		if (ctl->un.ord < 0 || ctl->un.ord > 1) | 
| 4274 | 			return EINVAL; | 
| 4275 | 		req_buf[0] = ctl->un.ord; | 
| 4276 | 		if (!uaudio_req(sc, | 
| 4277 | 			UT_WRITE_CLASS_INTERFACE, | 
| 4278 | 			UAUDIO_V1_REQ_SET_CUR, | 
| 4279 | 			m->req_sel, | 
| 4280 | 			m->chan < 0 ? 0 : m->chan, | 
| 4281 | 			sc->ctl_ifnum, | 
| 4282 | 			u->id, | 
| 4283 | 			req_buf, | 
| 4284 | 			1)) | 
| 4285 | 			return EIO; | 
| 4286 | 		break; | 
| 4287 | 	case UAUDIO_MIX_NUM: | 
| 4288 | 		nch = uaudio_mixer_nchan(m, NULL); | 
| 4289 | 		ctl->un.value.num_channels = nch; | 
| 4290 | 		for (i = 0; i < nch; i++) { | 
| 4291 | 			val = uaudio_ranges_encode(&m->ranges, | 
| 4292 | 			    ctl->un.value.level[i]); | 
| 4293 | 			DPRINTF("%s: ch %d, ctl %d, num val %d\n", __func__, | 
| 4294 | 			    i, ctl->un.value.level[i], val); | 
| 4295 | 			req_buf[0] = val; | 
| 4296 | 			req_buf[1] = val >> 8; | 
| 4297 | 			if (!uaudio_req(sc, | 
| 4298 | 				UT_WRITE_CLASS_INTERFACE, | 
| 4299 | 				UAUDIO_V1_REQ_SET_CUR, | 
| 4300 | 				m->req_sel, | 
| 4301 | 				m->chan < 0 ? 0 : i + 1, | 
| 4302 | 				sc->ctl_ifnum, | 
| 4303 | 				u->id, | 
| 4304 | 				req_buf, | 
| 4305 | 				2)) | 
| 4306 | 				return EIO; | 
| 4307 | 			m = m->next; | 
| 4308 | 		} | 
| 4309 | 		break; | 
| 4310 | 	case UAUDIO_MIX_ENUM: | 
| 4311 | 		 | 
| 4312 | 		break; | 
| 4313 | 	} | 
| 4314 | 	return 0; | 
| 4315 | } | 
| 4316 |   | 
| 4317 | int | 
| 4318 | uaudio_query_devinfo_do(struct uaudio_softc *sc, struct mixer_devinfo *devinfo) | 
| 4319 | { | 
| 4320 | 	struct uaudio_unit *u; | 
| 4321 | 	struct uaudio_mixent *m; | 
| 4322 |   | 
| 4323 | 	devinfo->next = -1; | 
| 4324 | 	devinfo->prev = -1; | 
| 4325 | 	switch (devinfo->index) { | 
| 4326 | 	case UAUDIO_CLASS_IN: | 
| 4327 | 		strlcpy(devinfo->label.name, AudioCinputs, MAX_AUDIO_DEV_LEN); | 
| 4328 | 		devinfo->type = AUDIO_MIXER_CLASS; | 
| 4329 | 		devinfo->mixer_class = -1; | 
| 4330 | 		return 0; | 
| 4331 | 	case UAUDIO_CLASS_OUT: | 
| 4332 | 		strlcpy(devinfo->label.name, AudioCoutputs, MAX_AUDIO_DEV_LEN); | 
| 4333 | 		devinfo->type = AUDIO_MIXER_CLASS; | 
| 4334 | 		devinfo->mixer_class = -1; | 
| 4335 | 		return 0; | 
| 4336 | 	} | 
| 4337 |   | 
| 4338 | 	 | 
| 4339 | 	  | 
| 4340 | 	  | 
| 4341 | 	if (!uaudio_mixer_byindex(sc, devinfo->index, &u, &m)) | 
| 4342 | 		return ENOENT; | 
| 4343 |   | 
| 4344 | 	if (strcmp(m->fname, "level") == 0) { | 
| 4345 | 		 | 
| 4346 | 		  | 
| 4347 | 		  | 
| 4348 | 		  | 
| 4349 | 		strlcpy(devinfo->label.name, u->name, MAX_AUDIO_DEV_LEN); | 
| 4350 | 	} else { | 
| 4351 | 		if (m->chan == -1) { | 
| 4352 | 			snprintf(devinfo->label.name, MAX_AUDIO_DEV_LEN, | 
| 4353 | 			    "%s_%s", u->name, m->fname); | 
| 4354 | 		} else { | 
| 4355 | 			snprintf(devinfo->label.name, MAX_AUDIO_DEV_LEN, | 
| 4356 | 			    "%s_%s%u", u->name, m->fname, m->chan); | 
| 4357 | 		} | 
| 4358 | 	} | 
| 4359 |   | 
| 4360 | 	devinfo->mixer_class = u->mixer_class; | 
| 4361 | 	switch (m->type) { | 
| 4362 | 	case UAUDIO_MIX_SW: | 
| 4363 | 		devinfo->type = AUDIO_MIXER_ENUM; | 
| 4364 | 		devinfo->un.e.num_mem = 2; | 
| 4365 | 		devinfo->un.e.member[0].ord = 0; | 
| 4366 | 		strlcpy(devinfo->un.e.member[0].label.name, "off", | 
| 4367 | 		    MAX_AUDIO_DEV_LEN); | 
| 4368 | 		devinfo->un.e.member[1].ord = 1; | 
| 4369 | 		strlcpy(devinfo->un.e.member[1].label.name, "on", | 
| 4370 | 		    MAX_AUDIO_DEV_LEN); | 
| 4371 | 		break; | 
| 4372 | 	case UAUDIO_MIX_NUM: | 
| 4373 | 		devinfo->type = AUDIO_MIXER_VALUE; | 
| 4374 | 		devinfo->un.v.num_channels = uaudio_mixer_nchan(m, NULL); | 
| 4375 | 		devinfo->un.v.delta = 1; | 
| 4376 | 		break; | 
| 4377 | 	case UAUDIO_MIX_ENUM: | 
| 4378 | 		 | 
| 4379 | 		devinfo->type = AUDIO_MIXER_ENUM; | 
| 4380 | 		devinfo->un.e.num_mem = 0; | 
| 4381 | 		break; | 
| 4382 | 	} | 
| 4383 | 	return 0; | 
| 4384 | } | 
| 4385 |   | 
| 4386 | int | 
| 4387 | uaudio_get_port(void *arg, struct mixer_ctrl *ctl) | 
| 4388 | { | 
| 4389 | 	struct uaudio_softc *sc = arg; | 
| 4390 | 	int rc; | 
| 4391 |   | 
| 4392 | 	usbd_ref_incr(sc->udev); | 
| 4393 | 	rc = uaudio_get_port_do(sc, ctl); | 
| 4394 | 	usbd_ref_decr(sc->udev); | 
| 4395 | 	return rc; | 
| 4396 | } | 
| 4397 |   | 
| 4398 | int | 
| 4399 | uaudio_set_port(void *arg, struct mixer_ctrl *ctl) | 
| 4400 | { | 
| 4401 | 	struct uaudio_softc *sc = arg; | 
| 4402 | 	int rc; | 
| 4403 |   | 
| 4404 | 	usbd_ref_incr(sc->udev); | 
| 4405 | 	rc = uaudio_set_port_do(sc, ctl); | 
| 4406 | 	usbd_ref_decr(sc->udev); | 
| 4407 | 	return rc; | 
| 4408 | } | 
| 4409 |   | 
| 4410 | int | 
| 4411 | uaudio_query_devinfo(void *arg, struct mixer_devinfo *devinfo) | 
| 4412 | { | 
| 4413 | 	struct uaudio_softc *sc = arg; | 
| 4414 | 	int rc; | 
| 4415 |   | 
| 4416 | 	usbd_ref_incr(sc->udev); | 
| 4417 | 	rc = uaudio_query_devinfo_do(sc, devinfo); | 
| 4418 | 	usbd_ref_decr(sc->udev); | 
| 4419 | 	return rc; | 
| 4420 | } |