Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "usb_stream.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) /*                             setup                                  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) static unsigned usb_stream_next_packet_size(struct usb_stream_kernel *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	struct usb_stream *s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	sk->out_phase_peeked = (sk->out_phase & 0xffff) + sk->freqn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	return (sk->out_phase_peeked >> 16) * s->cfg.frame_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static void playback_prep_freqn(struct usb_stream_kernel *sk, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct usb_stream *s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	int pack, lb = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	for (pack = 0; pack < sk->n_o_ps; pack++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		int l = usb_stream_next_packet_size(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		if (s->idle_outsize + lb + l > s->period_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 			goto check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		sk->out_phase = sk->out_phase_peeked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		urb->iso_frame_desc[pack].offset = lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		urb->iso_frame_desc[pack].length = l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		lb += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	snd_printdd(KERN_DEBUG "%i\n", lb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) check:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	urb->number_of_packets = pack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	urb->transfer_buffer_length = lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	s->idle_outsize += lb - s->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	snd_printdd(KERN_DEBUG "idle=%i ul=%i ps=%i\n", s->idle_outsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		    lb, s->period_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int init_pipe_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			   struct urb **urbs, char *transfer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			   struct usb_device *dev, int pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int u, p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	int maxpacket = use_packsize ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		use_packsize : usb_maxpacket(dev, pipe, usb_pipeout(pipe));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int transfer_length = maxpacket * sk->n_o_ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	for (u = 0; u < USB_STREAM_NURBS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	     ++u, transfer += transfer_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		struct urb *urb = urbs[u];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		struct usb_iso_packet_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		urb->transfer_buffer = transfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		urb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		urb->pipe = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		urb->number_of_packets = sk->n_o_ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		urb->context = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		urb->interval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (usb_pipeout(pipe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (usb_urb_ep_type_check(urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		urb->transfer_buffer_length = transfer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		desc = urb->iso_frame_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		desc->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		desc->length = maxpacket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		for (p = 1; p < sk->n_o_ps; ++p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			desc[p].offset = desc[p - 1].offset + maxpacket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			desc[p].length = maxpacket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static int init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		      struct usb_device *dev, int in_pipe, int out_pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct usb_stream	*s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	char			*indata = (char *)s + sizeof(*s) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 					sizeof(struct usb_stream_packet) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 					s->inpackets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	int			u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	for (u = 0; u < USB_STREAM_NURBS; ++u) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		if (!sk->inurb[u])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (!sk->outurb[u])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	    init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			   out_pipe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * this will overflow at approx 524 kHz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static inline unsigned get_usb_full_speed_rate(unsigned rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return ((rate << 13) + 62) / 125;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * this will overflow at approx 4 MHz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static inline unsigned get_usb_high_speed_rate(unsigned rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return ((rate << 10) + 62) / 125;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) void usb_stream_free(struct usb_stream_kernel *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct usb_stream *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	for (u = 0; u < USB_STREAM_NURBS; ++u) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		usb_free_urb(sk->inurb[u]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		sk->inurb[u] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		usb_free_urb(sk->outurb[u]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		sk->outurb[u] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (sk->write_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		free_pages_exact(sk->write_page, s->write_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		sk->write_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	free_pages_exact(s, s->read_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	sk->s = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 				  struct usb_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				  unsigned in_endpoint, unsigned out_endpoint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				  unsigned sample_rate, unsigned use_packsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				  unsigned period_frames, unsigned frame_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int packets, max_packsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	int in_pipe, out_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int read_size = sizeof(struct usb_stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int write_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	in_pipe = usb_rcvisocpipe(dev, in_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	out_pipe = usb_sndisocpipe(dev, out_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	max_packsize = use_packsize ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		use_packsize : usb_maxpacket(dev, in_pipe, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		t_period = period_frames / sample_rate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		iso_packs = t_period / t_iso_frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			= (period_frames / sample_rate) * (1 / t_iso_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	packets = period_frames * usb_frames / sample_rate + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (dev->speed == USB_SPEED_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		packets = (packets + 7) & ~7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	read_size += packets * USB_STREAM_URBDEPTH *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		(max_packsize + sizeof(struct usb_stream_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	max_packsize = usb_maxpacket(dev, out_pipe, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	sk->s = alloc_pages_exact(read_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				  GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (!sk->s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		pr_warn("us122l: couldn't allocate read buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	sk->s->read_size = read_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	sk->s->cfg.sample_rate = sample_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	sk->s->cfg.frame_size = frame_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	sk->n_o_ps = packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	sk->s->cfg.period_frames = period_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	sk->s->period_size = frame_size * period_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	sk->s->write_size = write_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	sk->write_page = alloc_pages_exact(write_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 					   GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (!sk->write_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		pr_warn("us122l: couldn't allocate write buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		usb_stream_free(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	/* calculate the frequency in 16.16 format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (dev->speed == USB_SPEED_FULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		sk->freqn = get_usb_full_speed_rate(sample_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		sk->freqn = get_usb_high_speed_rate(sample_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (init_urbs(sk, use_packsize, dev, in_pipe, out_pipe) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		usb_stream_free(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	sk->s->state = usb_stream_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /*                             start                                  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	bool r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (unlikely(urb->status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			snd_printk(KERN_WARNING "status=%i\n", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		sk->iso_frame_balance = 0x7FFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	r = sk->iso_frame_balance == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		sk->i_urb = urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	sk->iso_frame_balance += urb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return balance_check(sk, urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	sk->iso_frame_balance -= urb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return balance_check(sk, urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	int u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	for (u = 0; u < USB_STREAM_NURBS; u++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		struct urb *urb = urbs[u];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		urb->complete = complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		struct urb *inurb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct usb_stream *s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	struct urb *io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct usb_iso_packet_descriptor *id, *od;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	int p = 0, lb = 0, l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	io = sk->idle_outurb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	od = io->iso_frame_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	for (; s->sync_packet < 0; ++p, ++s->sync_packet) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		struct urb *ii = sk->completed_inurb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		id = ii->iso_frame_desc +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			ii->number_of_packets + s->sync_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		l = id->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		od[p].length = l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		od[p].offset = lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		lb += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	for (;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	     s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	     ++p, ++s->sync_packet) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		l = inurb->iso_frame_desc[s->sync_packet].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (s->idle_outsize + lb + l > s->period_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			goto check_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		od[p].length = l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		od[p].offset = lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		lb += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) check_ok:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	s->sync_packet -= inurb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (unlikely(s->sync_packet < -2 || s->sync_packet > 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		snd_printk(KERN_WARNING "invalid sync_packet = %i;"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			   " p=%i nop=%i %i %x %x %x > %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			   s->sync_packet, p, inurb->number_of_packets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			   s->idle_outsize + lb + l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			   s->idle_outsize, lb,  l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			   s->period_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (unlikely(lb % s->cfg.frame_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		snd_printk(KERN_WARNING"invalid outsize = %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			   lb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	s->idle_outsize += lb - s->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	io->number_of_packets = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	io->transfer_buffer_length = lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (s->idle_outsize <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static void prepare_inurb(int number_of_packets, struct urb *iu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct usb_iso_packet_descriptor *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	int p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	iu->number_of_packets = number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	id = iu->iso_frame_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	id->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	for (p = 0; p < iu->number_of_packets - 1; ++p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		id[p + 1].offset = id[p].offset + id[p].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	iu->transfer_buffer_length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		id[0].length * iu->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static int submit_urbs(struct usb_stream_kernel *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		       struct urb *inurb, struct urb *outurb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		goto report_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	sk->idle_inurb = sk->completed_inurb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	sk->completed_inurb = inurb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		goto report_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	sk->idle_outurb = sk->completed_outurb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	sk->completed_outurb = outurb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) report_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	snd_printk(KERN_ERR "%i\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) #ifdef DEBUG_LOOP_BACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)   This loop_back() shows how to read/write the period data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static void loop_back(struct usb_stream *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	char *i, *o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	int il, ol, l, p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	struct urb *iu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	struct usb_iso_packet_descriptor *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	o = s->playback1st_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	ol = s->playback1st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (s->insplit_pack >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		iu = sk->idle_inurb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		id = iu->iso_frame_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		p = s->insplit_pack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		goto second;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) loop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	for (; p < iu->number_of_packets && l < s->period_size; ++p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		i = iu->transfer_buffer + id[p].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		il = id[p].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		if (l + il > s->period_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			il = s->period_size - l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		if (il <= ol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			memcpy(o, i, il);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			o += il;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			ol -= il;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			memcpy(o, i, ol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			singen_6pack(o, ol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			o = s->playback_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			memcpy(o, i + ol, il - ol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			o += il - ol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			ol = s->period_size - s->playback1st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		l += il;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (iu == sk->completed_inurb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		if (l != s->period_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			       l/(int)s->cfg.frame_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) second:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	iu = sk->completed_inurb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	id = iu->iso_frame_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	goto loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static void loop_back(struct usb_stream *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static void stream_idle(struct usb_stream_kernel *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			struct urb *inurb, struct urb *outurb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	struct usb_stream *s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	int l, p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	int insize = s->idle_insize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	int urb_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	s->inpacket_split = s->next_inpacket_split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	s->inpacket_split_at = s->next_inpacket_split_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	s->next_inpacket_split = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	s->next_inpacket_split_at = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	for (p = 0; p < inurb->number_of_packets; ++p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		l = id[p].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		if (unlikely(l == 0 || id[p].status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			snd_printk(KERN_WARNING "underrun, status=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 				   id[p].status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		s->inpacket_head++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		s->inpacket_head %= s->inpackets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		if (s->inpacket_split == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			s->inpacket_split = s->inpacket_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		s->inpacket[s->inpacket_head].offset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			id[p].offset + (inurb->transfer_buffer - (void *)s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		s->inpacket[s->inpacket_head].length = l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		if (insize + l > s->period_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		    s->next_inpacket_split == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			s->next_inpacket_split = s->inpacket_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			s->next_inpacket_split_at = s->period_size - insize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		insize += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		urb_size += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	s->idle_insize += urb_size - s->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (s->idle_insize < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		snd_printk(KERN_WARNING "%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			   (s->idle_insize)/(int)s->cfg.frame_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	s->insize_done += urb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	l = s->idle_outsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 				  sk->write_page) - l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (usb_stream_prepare_playback(sk, inurb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		sk->write_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (submit_urbs(sk, inurb, outurb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	loop_back(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	s->periods_done++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	wake_up_all(&sk->sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	s->state = usb_stream_xrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	wake_up_all(&sk->sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static void i_capture_idle(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	struct usb_stream_kernel *sk = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (balance_capture(sk, urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		stream_idle(sk, urb, sk->i_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) static void i_playback_idle(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	struct usb_stream_kernel *sk = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (balance_playback(sk, urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		stream_idle(sk, sk->i_urb, urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static void stream_start(struct usb_stream_kernel *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			 struct urb *inurb, struct urb *outurb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	struct usb_stream *s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (s->state >= usb_stream_sync1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		int l, p, max_diff, max_diff_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		int urb_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		unsigned frames_per_packet, min_frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		frames_per_packet = (s->period_size - s->idle_insize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		frames_per_packet <<= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		frames_per_packet /=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			s->cfg.frame_size * inurb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		frames_per_packet++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		max_diff_0 = s->cfg.frame_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		if (s->cfg.period_frames >= 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			max_diff_0 <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		if (s->cfg.period_frames >= 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			max_diff_0 <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		max_diff = max_diff_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		for (p = 0; p < inurb->number_of_packets; ++p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			int diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 			l = inurb->iso_frame_desc[p].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			urb_size += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			min_frames += frames_per_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 			diff = urb_size -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 				(min_frames >> 8) * s->cfg.frame_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 			if (diff < max_diff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 				snd_printdd(KERN_DEBUG "%i %i %i %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 					    s->insize_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 					    urb_size / (int)s->cfg.frame_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 					    inurb->number_of_packets, diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 				max_diff = diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		s->idle_insize -= max_diff - max_diff_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		s->idle_insize += urb_size - s->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		if (s->idle_insize < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			snd_printk(KERN_WARNING "%i %i %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 				   s->idle_insize, urb_size, s->period_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		} else if (s->idle_insize == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			s->next_inpacket_split =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 				(s->inpacket_head + 1) % s->inpackets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			s->next_inpacket_split_at = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			unsigned split = s->inpacket_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 			l = s->idle_insize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			while (l > s->inpacket[split].length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 				l -= s->inpacket[split].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 				if (split == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 					split = s->inpackets - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 					split--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			s->next_inpacket_split = split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			s->next_inpacket_split_at =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 				s->inpacket[split].length - l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		s->insize_done += urb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		if (usb_stream_prepare_playback(sk, inurb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		playback_prep_freqn(sk, sk->idle_outurb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (submit_urbs(sk, inurb, outurb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		/* just guesswork                            ^^^^^^ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		s->state = usb_stream_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		subs_set_complete(sk->inurb, i_capture_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		subs_set_complete(sk->outurb, i_playback_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static void i_capture_start(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	struct usb_stream_kernel *sk = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	struct usb_stream *s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	int p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	int empty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	if (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		snd_printk(KERN_WARNING "status=%i\n", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	for (p = 0; p < urb->number_of_packets; ++p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		int l = id[p].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		if (l < s->cfg.frame_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 			++empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 			if (s->state >= usb_stream_sync0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 				snd_printk(KERN_WARNING "%i\n", l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		s->inpacket_head++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		s->inpacket_head %= s->inpackets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		s->inpacket[s->inpacket_head].offset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 			id[p].offset + (urb->transfer_buffer - (void *)s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		s->inpacket[s->inpacket_head].length = l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) #ifdef SHOW_EMPTY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	if (empty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		       urb->iso_frame_desc[0].actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		for (pack = 1; pack < urb->number_of_packets; ++pack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 			int l = urb->iso_frame_desc[pack].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 			printk(KERN_CONT " %i", l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	if (!empty && s->state < usb_stream_sync1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		++s->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	if (balance_capture(sk, urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		stream_start(sk, urb, sk->i_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) static void i_playback_start(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	struct usb_stream_kernel *sk = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	if (balance_playback(sk, urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		stream_start(sk, sk->i_urb, urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) int usb_stream_start(struct usb_stream_kernel *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	struct usb_stream *s = sk->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	int frame = 0, iters = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	int u, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	int try = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	if (s->state != usb_stream_stopped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	subs_set_complete(sk->inurb, i_capture_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	subs_set_complete(sk->outurb, i_playback_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	memset(sk->write_page, 0, s->write_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) dotry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	s->insize_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	s->idle_insize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	s->idle_outsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	s->sync_packet = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	s->inpacket_head = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	sk->iso_frame_balance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	++try;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	for (u = 0; u < 2; u++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		struct urb *inurb = sk->inurb[u];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		struct urb *outurb = sk->outurb[u];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		playback_prep_freqn(sk, outurb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		inurb->number_of_packets = outurb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		inurb->transfer_buffer_length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 			inurb->number_of_packets *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 			inurb->iso_frame_desc[0].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		if (u == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 			int now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 			struct usb_device *dev = inurb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 			frame = usb_get_current_frame_number(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 			do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 				now = usb_get_current_frame_number(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 				++iters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 			} while (now > -1 && now == frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		err = usb_submit_urb(inurb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 			snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 				   " returned %i\n", u, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		err = usb_submit_urb(outurb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 				   " returned %i\n", u, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		if (inurb->start_frame != outurb->start_frame) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 			snd_printd(KERN_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 				   "u[%i] start_frames differ in:%u out:%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 				   u, inurb->start_frame, outurb->start_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 			goto check_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	try = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) check_retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	if (try) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 		usb_stream_stop(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		if (try < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 			msleep(1500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 			snd_printd(KERN_DEBUG "goto dotry;\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 			goto dotry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		snd_printk(KERN_WARNING"couldn't start"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 			   " all urbs on the same start_frame.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) /* wait, check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		int wait_ms = 3000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		while (s->state != usb_stream_ready && wait_ms > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 			snd_printdd(KERN_DEBUG "%i\n", s->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 			msleep(200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 			wait_ms -= 200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	return s->state == usb_stream_ready ? 0 : -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) /*                             stop                                   */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) void usb_stream_stop(struct usb_stream_kernel *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	int u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	if (!sk->s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	for (u = 0; u < USB_STREAM_NURBS; ++u) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 		usb_kill_urb(sk->inurb[u]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		usb_kill_urb(sk->outurb[u]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	sk->s->state = usb_stream_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	msleep(400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }