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