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)  * STK1160 driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2012 Ezequiel Garcia
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * <elezegarcia--a.t--gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Based on Easycap driver by R.M. Thomas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	Copyright (C) 2010 R.M. Thomas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *	<rmthomas--a.t--sciolus.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "stk1160.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static unsigned int debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) module_param(debug, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) MODULE_PARM_DESC(debug, "enable debug messages");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static inline void print_err_status(struct stk1160 *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 				     int packet, int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	char *errmsg = "Unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		errmsg = "unlinked synchronously";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		errmsg = "unlinked asynchronously";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	case -ENOSR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		errmsg = "Buffer error (overrun)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		errmsg = "Stalled (device not responding)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	case -EOVERFLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		errmsg = "Babble (bad cable?)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	case -EPROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		errmsg = "Bit-stuff error (bad cable?)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	case -EILSEQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		errmsg = "CRC/Timeout (could be anything)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	case -ETIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		errmsg = "Device does not respond";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (packet < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		printk_ratelimited(KERN_WARNING "URB status %d [%s].\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 				status, errmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		printk_ratelimited(KERN_INFO "URB packet %d, status %d [%s].\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			       packet, status, errmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) struct stk1160_buffer *stk1160_next_buffer(struct stk1160 *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct stk1160_buffer *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* Current buffer must be NULL when this functions gets called */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	WARN_ON(dev->isoc_ctl.buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	spin_lock_irqsave(&dev->buf_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (!list_empty(&dev->avail_bufs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		buf = list_first_entry(&dev->avail_bufs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 				struct stk1160_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		list_del(&buf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	spin_unlock_irqrestore(&dev->buf_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) void stk1160_buffer_done(struct stk1160 *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct stk1160_buffer *buf = dev->isoc_ctl.buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	buf->vb.sequence = dev->sequence++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	buf->vb.field = V4L2_FIELD_INTERLACED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->bytesused);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	dev->isoc_ctl.buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) void stk1160_copy_video(struct stk1160 *dev, u8 *src, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int linesdone, lineoff, lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int bytesperline = dev->width * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct stk1160_buffer *buf = dev->isoc_ctl.buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	u8 *dst = buf->mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	int remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	 * TODO: These stk1160_dbg are very spammy!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 * We should 1) check why we are getting them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	 * and 2) add ratelimit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	 * UPDATE: One of the reasons (the only one?) for getting these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 * is incorrect standard (mismatch between expected and configured).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	 * So perhaps, we could add a counter for errors. When the counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	 * reaches some value, we simply stop streaming.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	len -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	src += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	remain = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	linesdone = buf->pos / bytesperline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	lineoff = buf->pos % bytesperline; /* offset in current line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!buf->odd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		dst += bytesperline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/* Multiply linesdone by two, to take account of the other field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	dst += linesdone * bytesperline * 2 + lineoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	/* Copy the remaining of current line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (remain < (bytesperline - lineoff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		lencopy = remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		lencopy = bytesperline - lineoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	 * Check if we have enough space left in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	 * In that case, we force loop exit after copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (lencopy > buf->bytesused - buf->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		lencopy = buf->bytesused - buf->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		remain = lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* Check if the copy is done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (lencopy == 0 || remain == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/* Let the bug hunt begin! sanity checks! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (lencopy < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		stk1160_dbg("copy skipped: negative lencopy\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if ((unsigned long)dst + lencopy >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		(unsigned long)buf->mem + buf->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	memcpy(dst, src, lencopy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	buf->bytesused += lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	buf->pos += lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	remain -= lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	/* Copy current field line by line, interlacing with the other field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	while (remain > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		dst += lencopy + bytesperline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		src += lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		/* Copy one line at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		if (remain < bytesperline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			lencopy = remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			lencopy = bytesperline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		 * Check if we have enough space left in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		 * In that case, we force loop exit after copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		if (lencopy > buf->bytesused - buf->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			lencopy = buf->bytesused - buf->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			remain = lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		/* Check if the copy is done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		if (lencopy == 0 || remain == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		if (lencopy < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			printk_ratelimited(KERN_WARNING "stk1160: negative lencopy detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if ((unsigned long)dst + lencopy >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			(unsigned long)buf->mem + buf->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		memcpy(dst, src, lencopy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		remain -= lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		buf->bytesused += lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		buf->pos += lencopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  * Controls the isoc copy of each urb packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static void stk1160_process_isoc(struct stk1160 *dev, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	int i, len, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	u8 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		stk1160_warn("%s called with null device\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (urb->status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		/* Print status and drop current packet (or field?) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		print_err_status(dev, -1, urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	for (i = 0; i < urb->number_of_packets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		status = urb->iso_frame_desc[i].status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			print_err_status(dev, i, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		/* Get packet actual length and pointer to data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		len = urb->iso_frame_desc[i].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		/* Empty packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if (len <= 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		 * An 8-byte packet sequence means end of field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		 * So if we don't have any packet, we start receiving one now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		 * and if we do have a packet, then we are done with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		 * These end of field packets are always 0xc0 or 0x80,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		 * but not always 8-byte long so we don't check packet length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (p[0] == 0xc0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			 * If first byte is 0xc0 then we received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			 * second field, and frame has ended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			if (dev->isoc_ctl.buf != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 				stk1160_buffer_done(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			dev->isoc_ctl.buf = stk1160_next_buffer(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			if (dev->isoc_ctl.buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		 * If we don't have a buffer here, then it means we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		 * haven't found the start mark sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (dev->isoc_ctl.buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		if (p[0] == 0xc0 || p[0] == 0x80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			/* We set next packet parity and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			 * continue to get next one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			dev->isoc_ctl.buf->odd = *p & 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			dev->isoc_ctl.buf->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		stk1160_copy_video(dev, p, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * IRQ callback, called by URB callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static void stk1160_isoc_irq(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct stk1160 *dev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	case -ECONNRESET:   /* kill */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		/* TODO: check uvc driver: he frees the queue here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		stk1160_err("urb error! status %d\n", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	stk1160_process_isoc(dev, urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	/* Reset urb buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	for (i = 0; i < urb->number_of_packets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		urb->iso_frame_desc[i].status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		urb->iso_frame_desc[i].actual_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	rc = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		stk1160_err("urb re-submit failed (%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  * Cancel urbs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  * This function can't be called in atomic context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) void stk1160_cancel_isoc(struct stk1160 *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	int i, num_bufs = dev->isoc_ctl.num_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	 * This check is not necessary, but we add it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	 * to avoid a spurious debug message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (!num_bufs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	stk1160_dbg("killing %d urbs...\n", num_bufs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	for (i = 0; i < num_bufs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		 * To kill urbs we can't be in atomic context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		 * We don't care for NULL pointer since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		 * usb_kill_urb allows it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		usb_kill_urb(dev->isoc_ctl.urb[i]);
^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) 	stk1160_dbg("all urbs killed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * Releases urb and transfer buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  * Obviusly, associated urb must be killed before releasing it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) void stk1160_free_isoc(struct stk1160 *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	int i, num_bufs = dev->isoc_ctl.num_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	stk1160_dbg("freeing %d urb buffers...\n", num_bufs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	for (i = 0; i < num_bufs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		urb = dev->isoc_ctl.urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		if (urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			if (dev->isoc_ctl.transfer_buffer[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) #ifndef CONFIG_DMA_NONCOHERENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				usb_free_coherent(dev->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 					urb->transfer_buffer_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 					dev->isoc_ctl.transfer_buffer[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 					urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 				kfree(dev->isoc_ctl.transfer_buffer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			dev->isoc_ctl.urb[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		dev->isoc_ctl.transfer_buffer[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	kfree(dev->isoc_ctl.urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	kfree(dev->isoc_ctl.transfer_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	dev->isoc_ctl.urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	dev->isoc_ctl.transfer_buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	dev->isoc_ctl.num_bufs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	stk1160_dbg("all urb buffers freed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  * Helper for cancelling and freeing urbs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)  * This function can't be called in atomic context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) void stk1160_uninit_isoc(struct stk1160 *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	stk1160_cancel_isoc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	stk1160_free_isoc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  * Allocate URBs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) int stk1160_alloc_isoc(struct stk1160 *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	int i, j, k, sb_size, max_packets, num_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	 * It may be necessary to release isoc here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	 * since isoc are only released on disconnection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	 * (see new_pkt_size flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (dev->isoc_ctl.num_bufs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		stk1160_uninit_isoc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	stk1160_dbg("allocating urbs...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	num_bufs = STK1160_NUM_BUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	max_packets = STK1160_NUM_PACKETS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	sb_size = max_packets * dev->max_pkt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	dev->isoc_ctl.buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (!dev->isoc_ctl.urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		stk1160_err("out of memory for urb array\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs, sizeof(void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 						GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (!dev->isoc_ctl.transfer_buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		stk1160_err("out of memory for usb transfers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		kfree(dev->isoc_ctl.urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	/* allocate urbs and transfer buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	for (i = 0; i < num_bufs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			goto free_i_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		dev->isoc_ctl.urb[i] = urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) #ifndef CONFIG_DMA_NONCOHERENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			sb_size, GFP_KERNEL, &urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		dev->isoc_ctl.transfer_buffer[i] = kmalloc(sb_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		if (!dev->isoc_ctl.transfer_buffer[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			stk1160_err("cannot alloc %d bytes for tx[%d] buffer\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 				sb_size, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			/* Not enough transfer buffers, so just give up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			if (i < STK1160_MIN_BUFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 				goto free_i_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			goto nomore_tx_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		 * FIXME: Where can I get the endpoint?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		urb->dev = dev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		urb->pipe = usb_rcvisocpipe(dev->udev, STK1160_EP_VIDEO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		urb->transfer_buffer = dev->isoc_ctl.transfer_buffer[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		urb->transfer_buffer_length = sb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		urb->complete = stk1160_isoc_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		urb->context = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		urb->interval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		urb->start_frame = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		urb->number_of_packets = max_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) #ifndef CONFIG_DMA_NONCOHERENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		urb->transfer_flags = URB_ISO_ASAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		for (j = 0; j < max_packets; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			urb->iso_frame_desc[j].offset = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			urb->iso_frame_desc[j].length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 					dev->isoc_ctl.max_pkt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			k += dev->isoc_ctl.max_pkt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	stk1160_dbg("%d urbs allocated\n", num_bufs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	/* At last we can say we have some buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	dev->isoc_ctl.num_bufs = num_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) nomore_tx_bufs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	 * Failed to allocate desired buffer count. However, we may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	 * enough to work fine, so we just free the extra urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	 * store the allocated count and keep going, fingers crossed!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	usb_free_urb(dev->isoc_ctl.urb[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	dev->isoc_ctl.urb[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	stk1160_warn("%d urbs allocated. Trying to continue...\n", i - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	dev->isoc_ctl.num_bufs = i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) free_i_bufs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	/* Save the allocated buffers so far, so we can properly free them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	dev->isoc_ctl.num_bufs = i+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	stk1160_free_isoc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)