1 // SPDX-License-Identifier: GPL-2.0
2 // tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3 //
4 // Copyright (c) 2006-2007 Mauro Carvalho Chehab <mchehab@kernel.org>
5 //
6 // Copyright (c) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 //	- Fixed module load/unload
8 
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
12 #include <linux/fs.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/mm.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/random.h>
20 #include <linux/usb.h>
21 #include <linux/videodev2.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-event.h>
24 #include <media/tuner.h>
25 #include <linux/interrupt.h>
26 #include <linux/kthread.h>
27 #include <linux/highmem.h>
28 #include <linux/freezer.h>
29 
30 #include "tm6000-regs.h"
31 #include "tm6000.h"
32 
33 #define BUFFER_TIMEOUT     msecs_to_jiffies(2000)  /* 2 seconds */
34 
35 /* Limits minimum and default number of buffers */
36 #define TM6000_MIN_BUF 4
37 #define TM6000_DEF_BUF 8
38 
39 #define TM6000_NUM_URB_BUF 8
40 
41 #define TM6000_MAX_ISO_PACKETS	46	/* Max number of ISO packets */
42 
43 /* Declare static vars that will be used as parameters */
44 static unsigned int vid_limit = 16;	/* Video memory limit, in Mb */
45 static int video_nr = -1;		/* /dev/videoN, -1 for autodetect */
46 static int radio_nr = -1;		/* /dev/radioN, -1 for autodetect */
47 static bool keep_urb;			/* keep urb buffers allocated */
48 
49 /* Debug level */
50 int tm6000_debug;
51 EXPORT_SYMBOL_GPL(tm6000_debug);
52 
53 static struct tm6000_fmt format[] = {
54 	{
55 		.name     = "4:2:2, packed, YVY2",
56 		.fourcc   = V4L2_PIX_FMT_YUYV,
57 		.depth    = 16,
58 	}, {
59 		.name     = "4:2:2, packed, UYVY",
60 		.fourcc   = V4L2_PIX_FMT_UYVY,
61 		.depth    = 16,
62 	}, {
63 		.name     = "A/V + VBI mux packet",
64 		.fourcc   = V4L2_PIX_FMT_TM6000,
65 		.depth    = 16,
66 	}
67 };
68 
69 /* ------------------------------------------------------------------
70  *	DMA and thread functions
71  * ------------------------------------------------------------------
72  */
73 
74 #define norm_maxw(a) 720
75 #define norm_maxh(a) 576
76 
77 #define norm_minw(a) norm_maxw(a)
78 #define norm_minh(a) norm_maxh(a)
79 
80 /*
81  * video-buf generic routine to get the next available buffer
82  */
get_next_buf(struct tm6000_dmaqueue * dma_q,struct tm6000_buffer ** buf)83 static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
84 			       struct tm6000_buffer   **buf)
85 {
86 	struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
87 
88 	if (list_empty(&dma_q->active)) {
89 		dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
90 		*buf = NULL;
91 		return;
92 	}
93 
94 	*buf = list_entry(dma_q->active.next,
95 			struct tm6000_buffer, vb.queue);
96 }
97 
98 /*
99  * Announces that a buffer were filled and request the next
100  */
buffer_filled(struct tm6000_core * dev,struct tm6000_dmaqueue * dma_q,struct tm6000_buffer * buf)101 static inline void buffer_filled(struct tm6000_core *dev,
102 				 struct tm6000_dmaqueue *dma_q,
103 				 struct tm6000_buffer *buf)
104 {
105 	/* Advice that buffer was filled */
106 	dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
107 	buf->vb.state = VIDEOBUF_DONE;
108 	buf->vb.field_count++;
109 	v4l2_get_timestamp(&buf->vb.ts);
110 
111 	list_del(&buf->vb.queue);
112 	wake_up(&buf->vb.done);
113 }
114 
115 /*
116  * Identify the tm5600/6000 buffer header type and properly handles
117  */
copy_streams(u8 * data,unsigned long len,struct urb * urb)118 static int copy_streams(u8 *data, unsigned long len,
119 			struct urb *urb)
120 {
121 	struct tm6000_dmaqueue  *dma_q = urb->context;
122 	struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
123 	u8 *ptr = data, *endp = data+len;
124 	unsigned long header = 0;
125 	int rc = 0;
126 	unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
127 	struct tm6000_buffer *vbuf = NULL;
128 	char *voutp = NULL;
129 	unsigned int linewidth;
130 
131 	if (!dev->radio) {
132 		/* get video buffer */
133 		get_next_buf(dma_q, &vbuf);
134 
135 		if (!vbuf)
136 			return rc;
137 		voutp = videobuf_to_vmalloc(&vbuf->vb);
138 
139 		if (!voutp)
140 			return 0;
141 	}
142 
143 	for (ptr = data; ptr < endp;) {
144 		if (!dev->isoc_ctl.cmd) {
145 			/* Header */
146 			if (dev->isoc_ctl.tmp_buf_len > 0) {
147 				/* from last urb or packet */
148 				header = dev->isoc_ctl.tmp_buf;
149 				if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
150 					memcpy((u8 *)&header +
151 						dev->isoc_ctl.tmp_buf_len,
152 						ptr,
153 						4 - dev->isoc_ctl.tmp_buf_len);
154 					ptr += 4 - dev->isoc_ctl.tmp_buf_len;
155 				}
156 				dev->isoc_ctl.tmp_buf_len = 0;
157 			} else {
158 				if (ptr + 3 >= endp) {
159 					/* have incomplete header */
160 					dev->isoc_ctl.tmp_buf_len = endp - ptr;
161 					memcpy(&dev->isoc_ctl.tmp_buf, ptr,
162 						dev->isoc_ctl.tmp_buf_len);
163 					return rc;
164 				}
165 				/* Seek for sync */
166 				for (; ptr < endp - 3; ptr++) {
167 					if (*(ptr + 3) == 0x47)
168 						break;
169 				}
170 				/* Get message header */
171 				header = *(unsigned long *)ptr;
172 				ptr += 4;
173 			}
174 
175 			/* split the header fields */
176 			size = ((header & 0x7e) << 1);
177 			if (size > 0)
178 				size -= 4;
179 			block = (header >> 7) & 0xf;
180 			field = (header >> 11) & 0x1;
181 			line  = (header >> 12) & 0x1ff;
182 			cmd   = (header >> 21) & 0x7;
183 			/* Validates haeder fields */
184 			if (size > TM6000_URB_MSG_LEN)
185 				size = TM6000_URB_MSG_LEN;
186 			pktsize = TM6000_URB_MSG_LEN;
187 			/*
188 			 * calculate position in buffer and change the buffer
189 			 */
190 			switch (cmd) {
191 			case TM6000_URB_MSG_VIDEO:
192 				if (!dev->radio) {
193 					if ((dev->isoc_ctl.vfield != field) &&
194 						(field == 1)) {
195 						/*
196 						 * Announces that a new buffer
197 						 * were filled
198 						 */
199 						buffer_filled(dev, dma_q, vbuf);
200 						dprintk(dev, V4L2_DEBUG_ISOC,
201 							"new buffer filled\n");
202 						get_next_buf(dma_q, &vbuf);
203 						if (!vbuf)
204 							return rc;
205 						voutp = videobuf_to_vmalloc(&vbuf->vb);
206 						if (!voutp)
207 							return rc;
208 						memset(voutp, 0, vbuf->vb.size);
209 					}
210 					linewidth = vbuf->vb.width << 1;
211 					pos = ((line << 1) - field - 1) *
212 					linewidth + block * TM6000_URB_MSG_LEN;
213 					/* Don't allow to write out of the buffer */
214 					if (pos + size > vbuf->vb.size)
215 						cmd = TM6000_URB_MSG_ERR;
216 					dev->isoc_ctl.vfield = field;
217 				}
218 				break;
219 			case TM6000_URB_MSG_VBI:
220 				break;
221 			case TM6000_URB_MSG_AUDIO:
222 			case TM6000_URB_MSG_PTS:
223 				size = pktsize; /* Size is always 180 bytes */
224 				break;
225 			}
226 		} else {
227 			/* Continue the last copy */
228 			cmd = dev->isoc_ctl.cmd;
229 			size = dev->isoc_ctl.size;
230 			pos = dev->isoc_ctl.pos;
231 			pktsize = dev->isoc_ctl.pktsize;
232 			field = dev->isoc_ctl.field;
233 		}
234 		cpysize = (endp - ptr > size) ? size : endp - ptr;
235 		if (cpysize) {
236 			/* copy data in different buffers */
237 			switch (cmd) {
238 			case TM6000_URB_MSG_VIDEO:
239 				/* Fills video buffer */
240 				if (vbuf)
241 					memcpy(&voutp[pos], ptr, cpysize);
242 				break;
243 			case TM6000_URB_MSG_AUDIO: {
244 				int i;
245 				for (i = 0; i < cpysize; i += 2)
246 					swab16s((u16 *)(ptr + i));
247 
248 				tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
249 				break;
250 			}
251 			case TM6000_URB_MSG_VBI:
252 				/* Need some code to copy vbi buffer */
253 				break;
254 			case TM6000_URB_MSG_PTS: {
255 				/* Need some code to copy pts */
256 				u32 pts;
257 				pts = *(u32 *)ptr;
258 				dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
259 					field, pts);
260 				break;
261 			}
262 			}
263 		}
264 		if (ptr + pktsize > endp) {
265 			/*
266 			 * End of URB packet, but cmd processing is not
267 			 * complete. Preserve the state for a next packet
268 			 */
269 			dev->isoc_ctl.pos = pos + cpysize;
270 			dev->isoc_ctl.size = size - cpysize;
271 			dev->isoc_ctl.cmd = cmd;
272 			dev->isoc_ctl.field = field;
273 			dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
274 			ptr += endp - ptr;
275 		} else {
276 			dev->isoc_ctl.cmd = 0;
277 			ptr += pktsize;
278 		}
279 	}
280 	return 0;
281 }
282 
283 /*
284  * Identify the tm5600/6000 buffer header type and properly handles
285  */
copy_multiplexed(u8 * ptr,unsigned long len,struct urb * urb)286 static int copy_multiplexed(u8 *ptr, unsigned long len,
287 			struct urb *urb)
288 {
289 	struct tm6000_dmaqueue  *dma_q = urb->context;
290 	struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
291 	unsigned int pos = dev->isoc_ctl.pos, cpysize;
292 	int rc = 1;
293 	struct tm6000_buffer *buf;
294 	char *outp = NULL;
295 
296 	get_next_buf(dma_q, &buf);
297 	if (buf)
298 		outp = videobuf_to_vmalloc(&buf->vb);
299 
300 	if (!outp)
301 		return 0;
302 
303 	while (len > 0) {
304 		cpysize = min(len, buf->vb.size-pos);
305 		memcpy(&outp[pos], ptr, cpysize);
306 		pos += cpysize;
307 		ptr += cpysize;
308 		len -= cpysize;
309 		if (pos >= buf->vb.size) {
310 			pos = 0;
311 			/* Announces that a new buffer were filled */
312 			buffer_filled(dev, dma_q, buf);
313 			dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
314 			get_next_buf(dma_q, &buf);
315 			if (!buf)
316 				break;
317 			outp = videobuf_to_vmalloc(&(buf->vb));
318 			if (!outp)
319 				return rc;
320 			pos = 0;
321 		}
322 	}
323 
324 	dev->isoc_ctl.pos = pos;
325 	return rc;
326 }
327 
print_err_status(struct tm6000_core * dev,int packet,int status)328 static inline void print_err_status(struct tm6000_core *dev,
329 				     int packet, int status)
330 {
331 	char *errmsg = "Unknown";
332 
333 	switch (status) {
334 	case -ENOENT:
335 		errmsg = "unlinked synchronously";
336 		break;
337 	case -ECONNRESET:
338 		errmsg = "unlinked asynchronously";
339 		break;
340 	case -ENOSR:
341 		errmsg = "Buffer error (overrun)";
342 		break;
343 	case -EPIPE:
344 		errmsg = "Stalled (device not responding)";
345 		break;
346 	case -EOVERFLOW:
347 		errmsg = "Babble (bad cable?)";
348 		break;
349 	case -EPROTO:
350 		errmsg = "Bit-stuff error (bad cable?)";
351 		break;
352 	case -EILSEQ:
353 		errmsg = "CRC/Timeout (could be anything)";
354 		break;
355 	case -ETIME:
356 		errmsg = "Device does not respond";
357 		break;
358 	}
359 	if (packet < 0) {
360 		dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
361 			status, errmsg);
362 	} else {
363 		dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
364 			packet, status, errmsg);
365 	}
366 }
367 
368 
369 /*
370  * Controls the isoc copy of each urb packet
371  */
tm6000_isoc_copy(struct urb * urb)372 static inline int tm6000_isoc_copy(struct urb *urb)
373 {
374 	struct tm6000_dmaqueue  *dma_q = urb->context;
375 	struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
376 	int i, len = 0, rc = 1, status;
377 	char *p;
378 
379 	if (urb->status < 0) {
380 		print_err_status(dev, -1, urb->status);
381 		return 0;
382 	}
383 
384 	for (i = 0; i < urb->number_of_packets; i++) {
385 		status = urb->iso_frame_desc[i].status;
386 
387 		if (status < 0) {
388 			print_err_status(dev, i, status);
389 			continue;
390 		}
391 
392 		len = urb->iso_frame_desc[i].actual_length;
393 
394 		if (len > 0) {
395 			p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
396 			if (!urb->iso_frame_desc[i].status) {
397 				if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
398 					rc = copy_multiplexed(p, len, urb);
399 					if (rc <= 0)
400 						return rc;
401 				} else {
402 					copy_streams(p, len, urb);
403 				}
404 			}
405 		}
406 	}
407 	return rc;
408 }
409 
410 /* ------------------------------------------------------------------
411  *	URB control
412  * ------------------------------------------------------------------
413  */
414 
415 /*
416  * IRQ callback, called by URB callback
417  */
tm6000_irq_callback(struct urb * urb)418 static void tm6000_irq_callback(struct urb *urb)
419 {
420 	struct tm6000_dmaqueue  *dma_q = urb->context;
421 	struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
422 	int i;
423 
424 	switch (urb->status) {
425 	case 0:
426 	case -ETIMEDOUT:
427 		break;
428 
429 	case -ECONNRESET:
430 	case -ENOENT:
431 	case -ESHUTDOWN:
432 		return;
433 
434 	default:
435 		tm6000_err("urb completion error %d.\n", urb->status);
436 		break;
437 	}
438 
439 	spin_lock(&dev->slock);
440 	tm6000_isoc_copy(urb);
441 	spin_unlock(&dev->slock);
442 
443 	/* Reset urb buffers */
444 	for (i = 0; i < urb->number_of_packets; i++) {
445 		urb->iso_frame_desc[i].status = 0;
446 		urb->iso_frame_desc[i].actual_length = 0;
447 	}
448 
449 	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
450 	if (urb->status)
451 		tm6000_err("urb resubmit failed (error=%i)\n",
452 			urb->status);
453 }
454 
455 /*
456  * Allocate URB buffers
457  */
tm6000_alloc_urb_buffers(struct tm6000_core * dev)458 static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
459 {
460 	int num_bufs = TM6000_NUM_URB_BUF;
461 	int i;
462 
463 	if (dev->urb_buffer)
464 		return 0;
465 
466 	dev->urb_buffer = kmalloc_array(num_bufs, sizeof(void *), GFP_KERNEL);
467 	if (!dev->urb_buffer)
468 		return -ENOMEM;
469 
470 	dev->urb_dma = kmalloc_array(num_bufs, sizeof(dma_addr_t *),
471 				     GFP_KERNEL);
472 	if (!dev->urb_dma)
473 		return -ENOMEM;
474 
475 	for (i = 0; i < num_bufs; i++) {
476 		dev->urb_buffer[i] = usb_alloc_coherent(
477 					dev->udev, dev->urb_size,
478 					GFP_KERNEL, &dev->urb_dma[i]);
479 		if (!dev->urb_buffer[i]) {
480 			tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
481 				    dev->urb_size, i);
482 			return -ENOMEM;
483 		}
484 		memset(dev->urb_buffer[i], 0, dev->urb_size);
485 	}
486 
487 	return 0;
488 }
489 
490 /*
491  * Free URB buffers
492  */
tm6000_free_urb_buffers(struct tm6000_core * dev)493 static int tm6000_free_urb_buffers(struct tm6000_core *dev)
494 {
495 	int i;
496 
497 	if (!dev->urb_buffer)
498 		return 0;
499 
500 	for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
501 		if (dev->urb_buffer[i]) {
502 			usb_free_coherent(dev->udev,
503 					dev->urb_size,
504 					dev->urb_buffer[i],
505 					dev->urb_dma[i]);
506 			dev->urb_buffer[i] = NULL;
507 		}
508 	}
509 	kfree(dev->urb_buffer);
510 	kfree(dev->urb_dma);
511 	dev->urb_buffer = NULL;
512 	dev->urb_dma = NULL;
513 
514 	return 0;
515 }
516 
517 /*
518  * Stop and Deallocate URBs
519  */
tm6000_uninit_isoc(struct tm6000_core * dev)520 static void tm6000_uninit_isoc(struct tm6000_core *dev)
521 {
522 	struct urb *urb;
523 	int i;
524 
525 	dev->isoc_ctl.buf = NULL;
526 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
527 		urb = dev->isoc_ctl.urb[i];
528 		if (urb) {
529 			usb_kill_urb(urb);
530 			usb_unlink_urb(urb);
531 			usb_free_urb(urb);
532 			dev->isoc_ctl.urb[i] = NULL;
533 		}
534 		dev->isoc_ctl.transfer_buffer[i] = NULL;
535 	}
536 
537 	if (!keep_urb)
538 		tm6000_free_urb_buffers(dev);
539 
540 	kfree(dev->isoc_ctl.urb);
541 	kfree(dev->isoc_ctl.transfer_buffer);
542 
543 	dev->isoc_ctl.urb = NULL;
544 	dev->isoc_ctl.transfer_buffer = NULL;
545 	dev->isoc_ctl.num_bufs = 0;
546 }
547 
548 /*
549  * Assign URBs and start IRQ
550  */
tm6000_prepare_isoc(struct tm6000_core * dev)551 static int tm6000_prepare_isoc(struct tm6000_core *dev)
552 {
553 	struct tm6000_dmaqueue *dma_q = &dev->vidq;
554 	int i, j, sb_size, pipe, size, max_packets;
555 	int num_bufs = TM6000_NUM_URB_BUF;
556 	struct urb *urb;
557 
558 	/* De-allocates all pending stuff */
559 	tm6000_uninit_isoc(dev);
560 	/* Stop interrupt USB pipe */
561 	tm6000_ir_int_stop(dev);
562 
563 	usb_set_interface(dev->udev,
564 			  dev->isoc_in.bInterfaceNumber,
565 			  dev->isoc_in.bAlternateSetting);
566 
567 	/* Start interrupt USB pipe */
568 	tm6000_ir_int_start(dev);
569 
570 	pipe = usb_rcvisocpipe(dev->udev,
571 			       dev->isoc_in.endp->desc.bEndpointAddress &
572 			       USB_ENDPOINT_NUMBER_MASK);
573 
574 	size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
575 
576 	if (size > dev->isoc_in.maxsize)
577 		size = dev->isoc_in.maxsize;
578 
579 	dev->isoc_ctl.max_pkt_size = size;
580 
581 	max_packets = TM6000_MAX_ISO_PACKETS;
582 	sb_size = max_packets * size;
583 	dev->urb_size = sb_size;
584 
585 	dev->isoc_ctl.num_bufs = num_bufs;
586 
587 	dev->isoc_ctl.urb = kmalloc_array(num_bufs, sizeof(void *),
588 					  GFP_KERNEL);
589 	if (!dev->isoc_ctl.urb)
590 		return -ENOMEM;
591 
592 	dev->isoc_ctl.transfer_buffer = kmalloc_array(num_bufs,
593 						      sizeof(void *),
594 						      GFP_KERNEL);
595 	if (!dev->isoc_ctl.transfer_buffer) {
596 		kfree(dev->isoc_ctl.urb);
597 		return -ENOMEM;
598 	}
599 
600 	dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets (%d bytes) of %d bytes each to handle %u size\n",
601 		    max_packets, num_bufs, sb_size,
602 		    dev->isoc_in.maxsize, size);
603 
604 
605 	if (tm6000_alloc_urb_buffers(dev) < 0) {
606 		tm6000_err("cannot allocate memory for urb buffers\n");
607 
608 		/* call free, as some buffers might have been allocated */
609 		tm6000_free_urb_buffers(dev);
610 		kfree(dev->isoc_ctl.urb);
611 		kfree(dev->isoc_ctl.transfer_buffer);
612 		return -ENOMEM;
613 	}
614 
615 	/* allocate urbs and transfer buffers */
616 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
617 		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
618 		if (!urb) {
619 			tm6000_uninit_isoc(dev);
620 			tm6000_free_urb_buffers(dev);
621 			return -ENOMEM;
622 		}
623 		dev->isoc_ctl.urb[i] = urb;
624 
625 		urb->transfer_dma = dev->urb_dma[i];
626 		dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
627 
628 		usb_fill_bulk_urb(urb, dev->udev, pipe,
629 				  dev->isoc_ctl.transfer_buffer[i], sb_size,
630 				  tm6000_irq_callback, dma_q);
631 		urb->interval = dev->isoc_in.endp->desc.bInterval;
632 		urb->number_of_packets = max_packets;
633 		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
634 
635 		for (j = 0; j < max_packets; j++) {
636 			urb->iso_frame_desc[j].offset = size * j;
637 			urb->iso_frame_desc[j].length = size;
638 		}
639 	}
640 
641 	return 0;
642 }
643 
tm6000_start_thread(struct tm6000_core * dev)644 static int tm6000_start_thread(struct tm6000_core *dev)
645 {
646 	struct tm6000_dmaqueue *dma_q = &dev->vidq;
647 	int i;
648 
649 	dma_q->frame = 0;
650 	dma_q->ini_jiffies = jiffies;
651 
652 	init_waitqueue_head(&dma_q->wq);
653 
654 	/* submit urbs and enables IRQ */
655 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
656 		int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
657 		if (rc) {
658 			tm6000_err("submit of urb %i failed (error=%i)\n", i,
659 				   rc);
660 			tm6000_uninit_isoc(dev);
661 			return rc;
662 		}
663 	}
664 
665 	return 0;
666 }
667 
668 /* ------------------------------------------------------------------
669  *	Videobuf operations
670  * ------------------------------------------------------------------
671  */
672 
673 static int
buffer_setup(struct videobuf_queue * vq,unsigned int * count,unsigned int * size)674 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
675 {
676 	struct tm6000_fh *fh = vq->priv_data;
677 
678 	*size = fh->fmt->depth * fh->width * fh->height >> 3;
679 	if (0 == *count)
680 		*count = TM6000_DEF_BUF;
681 
682 	if (*count < TM6000_MIN_BUF)
683 		*count = TM6000_MIN_BUF;
684 
685 	while (*size * *count > vid_limit * 1024 * 1024)
686 		(*count)--;
687 
688 	return 0;
689 }
690 
free_buffer(struct videobuf_queue * vq,struct tm6000_buffer * buf)691 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
692 {
693 	struct tm6000_fh *fh = vq->priv_data;
694 	struct tm6000_core   *dev = fh->dev;
695 	unsigned long flags;
696 
697 	BUG_ON(in_interrupt());
698 
699 	/* We used to wait for the buffer to finish here, but this didn't work
700 	   because, as we were keeping the state as VIDEOBUF_QUEUED,
701 	   videobuf_queue_cancel marked it as finished for us.
702 	   (Also, it could wedge forever if the hardware was misconfigured.)
703 
704 	   This should be safe; by the time we get here, the buffer isn't
705 	   queued anymore. If we ever start marking the buffers as
706 	   VIDEOBUF_ACTIVE, it won't be, though.
707 	*/
708 	spin_lock_irqsave(&dev->slock, flags);
709 	if (dev->isoc_ctl.buf == buf)
710 		dev->isoc_ctl.buf = NULL;
711 	spin_unlock_irqrestore(&dev->slock, flags);
712 
713 	videobuf_vmalloc_free(&buf->vb);
714 	buf->vb.state = VIDEOBUF_NEEDS_INIT;
715 }
716 
717 static int
buffer_prepare(struct videobuf_queue * vq,struct videobuf_buffer * vb,enum v4l2_field field)718 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
719 						enum v4l2_field field)
720 {
721 	struct tm6000_fh     *fh  = vq->priv_data;
722 	struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
723 	struct tm6000_core   *dev = fh->dev;
724 	int rc = 0;
725 
726 	BUG_ON(NULL == fh->fmt);
727 
728 
729 	/* FIXME: It assumes depth=2 */
730 	/* The only currently supported format is 16 bits/pixel */
731 	buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
732 	if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
733 		return -EINVAL;
734 
735 	if (buf->fmt       != fh->fmt    ||
736 	    buf->vb.width  != fh->width  ||
737 	    buf->vb.height != fh->height ||
738 	    buf->vb.field  != field) {
739 		buf->fmt       = fh->fmt;
740 		buf->vb.width  = fh->width;
741 		buf->vb.height = fh->height;
742 		buf->vb.field  = field;
743 		buf->vb.state = VIDEOBUF_NEEDS_INIT;
744 	}
745 
746 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
747 		rc = videobuf_iolock(vq, &buf->vb, NULL);
748 		if (rc != 0)
749 			goto fail;
750 	}
751 
752 	if (!dev->isoc_ctl.num_bufs) {
753 		rc = tm6000_prepare_isoc(dev);
754 		if (rc < 0)
755 			goto fail;
756 
757 		rc = tm6000_start_thread(dev);
758 		if (rc < 0)
759 			goto fail;
760 
761 	}
762 
763 	buf->vb.state = VIDEOBUF_PREPARED;
764 	return 0;
765 
766 fail:
767 	free_buffer(vq, buf);
768 	return rc;
769 }
770 
771 static void
buffer_queue(struct videobuf_queue * vq,struct videobuf_buffer * vb)772 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
773 {
774 	struct tm6000_buffer    *buf     = container_of(vb, struct tm6000_buffer, vb);
775 	struct tm6000_fh        *fh      = vq->priv_data;
776 	struct tm6000_core      *dev     = fh->dev;
777 	struct tm6000_dmaqueue  *vidq    = &dev->vidq;
778 
779 	buf->vb.state = VIDEOBUF_QUEUED;
780 	list_add_tail(&buf->vb.queue, &vidq->active);
781 }
782 
buffer_release(struct videobuf_queue * vq,struct videobuf_buffer * vb)783 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
784 {
785 	struct tm6000_buffer   *buf  = container_of(vb, struct tm6000_buffer, vb);
786 
787 	free_buffer(vq, buf);
788 }
789 
790 static const struct videobuf_queue_ops tm6000_video_qops = {
791 	.buf_setup      = buffer_setup,
792 	.buf_prepare    = buffer_prepare,
793 	.buf_queue      = buffer_queue,
794 	.buf_release    = buffer_release,
795 };
796 
797 /* ------------------------------------------------------------------
798  *	IOCTL handling
799  * ------------------------------------------------------------------
800  */
801 
is_res_read(struct tm6000_core * dev,struct tm6000_fh * fh)802 static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
803 {
804 	/* Is the current fh handling it? if so, that's OK */
805 	if (dev->resources == fh && dev->is_res_read)
806 		return true;
807 
808 	return false;
809 }
810 
is_res_streaming(struct tm6000_core * dev,struct tm6000_fh * fh)811 static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
812 {
813 	/* Is the current fh handling it? if so, that's OK */
814 	if (dev->resources == fh)
815 		return true;
816 
817 	return false;
818 }
819 
res_get(struct tm6000_core * dev,struct tm6000_fh * fh,bool is_res_read)820 static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
821 		   bool is_res_read)
822 {
823 	/* Is the current fh handling it? if so, that's OK */
824 	if (dev->resources == fh && dev->is_res_read == is_res_read)
825 		return true;
826 
827 	/* is it free? */
828 	if (dev->resources)
829 		return false;
830 
831 	/* grab it */
832 	dev->resources = fh;
833 	dev->is_res_read = is_res_read;
834 	dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
835 	return true;
836 }
837 
res_free(struct tm6000_core * dev,struct tm6000_fh * fh)838 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
839 {
840 	/* Is the current fh handling it? if so, that's OK */
841 	if (dev->resources != fh)
842 		return;
843 
844 	dev->resources = NULL;
845 	dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
846 }
847 
848 /* ------------------------------------------------------------------
849  *	IOCTL vidioc handling
850  * ------------------------------------------------------------------
851  */
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)852 static int vidioc_querycap(struct file *file, void  *priv,
853 					struct v4l2_capability *cap)
854 {
855 	struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
856 	struct video_device *vdev = video_devdata(file);
857 
858 	strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
859 	strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
860 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
861 	if (dev->tuner_type != TUNER_ABSENT)
862 		cap->device_caps |= V4L2_CAP_TUNER;
863 	if (vdev->vfl_type == VFL_TYPE_GRABBER)
864 		cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
865 				V4L2_CAP_STREAMING |
866 				V4L2_CAP_READWRITE;
867 	else
868 		cap->device_caps |= V4L2_CAP_RADIO;
869 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
870 		V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
871 
872 	return 0;
873 }
874 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)875 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
876 					struct v4l2_fmtdesc *f)
877 {
878 	if (f->index >= ARRAY_SIZE(format))
879 		return -EINVAL;
880 
881 	strlcpy(f->description, format[f->index].name, sizeof(f->description));
882 	f->pixelformat = format[f->index].fourcc;
883 	return 0;
884 }
885 
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)886 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
887 					struct v4l2_format *f)
888 {
889 	struct tm6000_fh  *fh = priv;
890 
891 	f->fmt.pix.width        = fh->width;
892 	f->fmt.pix.height       = fh->height;
893 	f->fmt.pix.field        = fh->vb_vidq.field;
894 	f->fmt.pix.pixelformat  = fh->fmt->fourcc;
895 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
896 	f->fmt.pix.bytesperline =
897 		(f->fmt.pix.width * fh->fmt->depth) >> 3;
898 	f->fmt.pix.sizeimage =
899 		f->fmt.pix.height * f->fmt.pix.bytesperline;
900 
901 	return 0;
902 }
903 
format_by_fourcc(unsigned int fourcc)904 static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
905 {
906 	unsigned int i;
907 
908 	for (i = 0; i < ARRAY_SIZE(format); i++)
909 		if (format[i].fourcc == fourcc)
910 			return format+i;
911 	return NULL;
912 }
913 
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)914 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
915 			struct v4l2_format *f)
916 {
917 	struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
918 	struct tm6000_fmt *fmt;
919 	enum v4l2_field field;
920 
921 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
922 	if (NULL == fmt) {
923 		dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
924 			f->fmt.pix.pixelformat);
925 		return -EINVAL;
926 	}
927 
928 	field = f->fmt.pix.field;
929 
930 	field = V4L2_FIELD_INTERLACED;
931 
932 	tm6000_get_std_res(dev);
933 
934 	f->fmt.pix.width  = dev->width;
935 	f->fmt.pix.height = dev->height;
936 
937 	f->fmt.pix.width &= ~0x01;
938 
939 	f->fmt.pix.field = field;
940 
941 	f->fmt.pix.bytesperline =
942 		(f->fmt.pix.width * fmt->depth) >> 3;
943 	f->fmt.pix.sizeimage =
944 		f->fmt.pix.height * f->fmt.pix.bytesperline;
945 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
946 
947 	return 0;
948 }
949 
950 /*FIXME: This seems to be generic enough to be at videodev2 */
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)951 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
952 					struct v4l2_format *f)
953 {
954 	struct tm6000_fh  *fh = priv;
955 	struct tm6000_core *dev = fh->dev;
956 	int ret = vidioc_try_fmt_vid_cap(file, fh, f);
957 	if (ret < 0)
958 		return ret;
959 
960 	fh->fmt           = format_by_fourcc(f->fmt.pix.pixelformat);
961 	fh->width         = f->fmt.pix.width;
962 	fh->height        = f->fmt.pix.height;
963 	fh->vb_vidq.field = f->fmt.pix.field;
964 	fh->type          = f->type;
965 
966 	dev->fourcc       = f->fmt.pix.pixelformat;
967 
968 	tm6000_set_fourcc_format(dev);
969 
970 	return 0;
971 }
972 
vidioc_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * p)973 static int vidioc_reqbufs(struct file *file, void *priv,
974 			   struct v4l2_requestbuffers *p)
975 {
976 	struct tm6000_fh  *fh = priv;
977 
978 	return videobuf_reqbufs(&fh->vb_vidq, p);
979 }
980 
vidioc_querybuf(struct file * file,void * priv,struct v4l2_buffer * p)981 static int vidioc_querybuf(struct file *file, void *priv,
982 			    struct v4l2_buffer *p)
983 {
984 	struct tm6000_fh  *fh = priv;
985 
986 	return videobuf_querybuf(&fh->vb_vidq, p);
987 }
988 
vidioc_qbuf(struct file * file,void * priv,struct v4l2_buffer * p)989 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
990 {
991 	struct tm6000_fh  *fh = priv;
992 
993 	return videobuf_qbuf(&fh->vb_vidq, p);
994 }
995 
vidioc_dqbuf(struct file * file,void * priv,struct v4l2_buffer * p)996 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
997 {
998 	struct tm6000_fh  *fh = priv;
999 
1000 	return videobuf_dqbuf(&fh->vb_vidq, p,
1001 				file->f_flags & O_NONBLOCK);
1002 }
1003 
vidioc_streamon(struct file * file,void * priv,enum v4l2_buf_type i)1004 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1005 {
1006 	struct tm6000_fh *fh = priv;
1007 	struct tm6000_core *dev = fh->dev;
1008 
1009 	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1010 		return -EINVAL;
1011 	if (i != fh->type)
1012 		return -EINVAL;
1013 
1014 	if (!res_get(dev, fh, false))
1015 		return -EBUSY;
1016 	return videobuf_streamon(&fh->vb_vidq);
1017 }
1018 
vidioc_streamoff(struct file * file,void * priv,enum v4l2_buf_type i)1019 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1020 {
1021 	struct tm6000_fh *fh = priv;
1022 	struct tm6000_core *dev = fh->dev;
1023 
1024 	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1025 		return -EINVAL;
1026 
1027 	if (i != fh->type)
1028 		return -EINVAL;
1029 
1030 	videobuf_streamoff(&fh->vb_vidq);
1031 	res_free(dev, fh);
1032 
1033 	return 0;
1034 }
1035 
vidioc_s_std(struct file * file,void * priv,v4l2_std_id norm)1036 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1037 {
1038 	int rc = 0;
1039 	struct tm6000_fh *fh = priv;
1040 	struct tm6000_core *dev = fh->dev;
1041 
1042 	dev->norm = norm;
1043 	rc = tm6000_init_analog_mode(dev);
1044 
1045 	fh->width  = dev->width;
1046 	fh->height = dev->height;
1047 
1048 	if (rc < 0)
1049 		return rc;
1050 
1051 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
1052 
1053 	return 0;
1054 }
1055 
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * norm)1056 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1057 {
1058 	struct tm6000_fh *fh = priv;
1059 	struct tm6000_core *dev = fh->dev;
1060 
1061 	*norm = dev->norm;
1062 	return 0;
1063 }
1064 
1065 static const char *iname[] = {
1066 	[TM6000_INPUT_TV] = "Television",
1067 	[TM6000_INPUT_COMPOSITE1] = "Composite 1",
1068 	[TM6000_INPUT_COMPOSITE2] = "Composite 2",
1069 	[TM6000_INPUT_SVIDEO] = "S-Video",
1070 };
1071 
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)1072 static int vidioc_enum_input(struct file *file, void *priv,
1073 				struct v4l2_input *i)
1074 {
1075 	struct tm6000_fh   *fh = priv;
1076 	struct tm6000_core *dev = fh->dev;
1077 	unsigned int n;
1078 
1079 	n = i->index;
1080 	if (n >= 3)
1081 		return -EINVAL;
1082 
1083 	if (!dev->vinput[n].type)
1084 		return -EINVAL;
1085 
1086 	i->index = n;
1087 
1088 	if (dev->vinput[n].type == TM6000_INPUT_TV)
1089 		i->type = V4L2_INPUT_TYPE_TUNER;
1090 	else
1091 		i->type = V4L2_INPUT_TYPE_CAMERA;
1092 
1093 	strcpy(i->name, iname[dev->vinput[n].type]);
1094 
1095 	i->std = TM6000_STD;
1096 
1097 	return 0;
1098 }
1099 
vidioc_g_input(struct file * file,void * priv,unsigned int * i)1100 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1101 {
1102 	struct tm6000_fh   *fh = priv;
1103 	struct tm6000_core *dev = fh->dev;
1104 
1105 	*i = dev->input;
1106 
1107 	return 0;
1108 }
1109 
vidioc_s_input(struct file * file,void * priv,unsigned int i)1110 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1111 {
1112 	struct tm6000_fh   *fh = priv;
1113 	struct tm6000_core *dev = fh->dev;
1114 	int rc = 0;
1115 
1116 	if (i >= 3)
1117 		return -EINVAL;
1118 	if (!dev->vinput[i].type)
1119 		return -EINVAL;
1120 
1121 	dev->input = i;
1122 
1123 	rc = vidioc_s_std(file, priv, dev->norm);
1124 
1125 	return rc;
1126 }
1127 
1128 /* --- controls ---------------------------------------------- */
1129 
tm6000_s_ctrl(struct v4l2_ctrl * ctrl)1130 static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
1131 {
1132 	struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
1133 	u8  val = ctrl->val;
1134 
1135 	switch (ctrl->id) {
1136 	case V4L2_CID_CONTRAST:
1137 		tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1138 		return 0;
1139 	case V4L2_CID_BRIGHTNESS:
1140 		tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1141 		return 0;
1142 	case V4L2_CID_SATURATION:
1143 		tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1144 		return 0;
1145 	case V4L2_CID_HUE:
1146 		tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1147 		return 0;
1148 	}
1149 	return -EINVAL;
1150 }
1151 
1152 static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
1153 	.s_ctrl = tm6000_s_ctrl,
1154 };
1155 
tm6000_radio_s_ctrl(struct v4l2_ctrl * ctrl)1156 static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
1157 {
1158 	struct tm6000_core *dev = container_of(ctrl->handler,
1159 			struct tm6000_core, radio_ctrl_handler);
1160 	u8  val = ctrl->val;
1161 
1162 	switch (ctrl->id) {
1163 	case V4L2_CID_AUDIO_MUTE:
1164 		dev->ctl_mute = val;
1165 		tm6000_tvaudio_set_mute(dev, val);
1166 		return 0;
1167 	case V4L2_CID_AUDIO_VOLUME:
1168 		dev->ctl_volume = val;
1169 		tm6000_set_volume(dev, val);
1170 		return 0;
1171 	}
1172 	return -EINVAL;
1173 }
1174 
1175 static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
1176 	.s_ctrl = tm6000_radio_s_ctrl,
1177 };
1178 
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)1179 static int vidioc_g_tuner(struct file *file, void *priv,
1180 				struct v4l2_tuner *t)
1181 {
1182 	struct tm6000_fh   *fh  = priv;
1183 	struct tm6000_core *dev = fh->dev;
1184 
1185 	if (UNSET == dev->tuner_type)
1186 		return -ENOTTY;
1187 	if (0 != t->index)
1188 		return -EINVAL;
1189 
1190 	strcpy(t->name, "Television");
1191 	t->type       = V4L2_TUNER_ANALOG_TV;
1192 	t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
1193 	t->rangehigh  = 0xffffffffUL;
1194 	t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1195 
1196 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1197 
1198 	t->audmode = dev->amode;
1199 
1200 	return 0;
1201 }
1202 
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)1203 static int vidioc_s_tuner(struct file *file, void *priv,
1204 				const struct v4l2_tuner *t)
1205 {
1206 	struct tm6000_fh   *fh  = priv;
1207 	struct tm6000_core *dev = fh->dev;
1208 
1209 	if (UNSET == dev->tuner_type)
1210 		return -ENOTTY;
1211 	if (0 != t->index)
1212 		return -EINVAL;
1213 
1214 	if (t->audmode > V4L2_TUNER_MODE_STEREO)
1215 		dev->amode = V4L2_TUNER_MODE_STEREO;
1216 	else
1217 		dev->amode = t->audmode;
1218 	dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1219 
1220 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1221 
1222 	return 0;
1223 }
1224 
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)1225 static int vidioc_g_frequency(struct file *file, void *priv,
1226 				struct v4l2_frequency *f)
1227 {
1228 	struct tm6000_fh   *fh  = priv;
1229 	struct tm6000_core *dev = fh->dev;
1230 
1231 	if (UNSET == dev->tuner_type)
1232 		return -ENOTTY;
1233 	if (f->tuner)
1234 		return -EINVAL;
1235 
1236 	f->frequency = dev->freq;
1237 
1238 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1239 
1240 	return 0;
1241 }
1242 
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)1243 static int vidioc_s_frequency(struct file *file, void *priv,
1244 				const struct v4l2_frequency *f)
1245 {
1246 	struct tm6000_fh   *fh  = priv;
1247 	struct tm6000_core *dev = fh->dev;
1248 
1249 	if (UNSET == dev->tuner_type)
1250 		return -ENOTTY;
1251 	if (f->tuner != 0)
1252 		return -EINVAL;
1253 
1254 	dev->freq = f->frequency;
1255 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1256 
1257 	return 0;
1258 }
1259 
radio_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)1260 static int radio_g_tuner(struct file *file, void *priv,
1261 					struct v4l2_tuner *t)
1262 {
1263 	struct tm6000_fh *fh = file->private_data;
1264 	struct tm6000_core *dev = fh->dev;
1265 
1266 	if (0 != t->index)
1267 		return -EINVAL;
1268 
1269 	memset(t, 0, sizeof(*t));
1270 	strcpy(t->name, "Radio");
1271 	t->type = V4L2_TUNER_RADIO;
1272 	t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1273 	t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1274 	t->audmode = V4L2_TUNER_MODE_STEREO;
1275 
1276 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1277 
1278 	return 0;
1279 }
1280 
radio_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)1281 static int radio_s_tuner(struct file *file, void *priv,
1282 					const struct v4l2_tuner *t)
1283 {
1284 	struct tm6000_fh *fh = file->private_data;
1285 	struct tm6000_core *dev = fh->dev;
1286 
1287 	if (0 != t->index)
1288 		return -EINVAL;
1289 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1290 	return 0;
1291 }
1292 
1293 /* ------------------------------------------------------------------
1294 	File operations for the device
1295    ------------------------------------------------------------------*/
1296 
__tm6000_open(struct file * file)1297 static int __tm6000_open(struct file *file)
1298 {
1299 	struct video_device *vdev = video_devdata(file);
1300 	struct tm6000_core *dev = video_drvdata(file);
1301 	struct tm6000_fh *fh;
1302 	enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1303 	int rc;
1304 	int radio = 0;
1305 
1306 	dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1307 		video_device_node_name(vdev));
1308 
1309 	switch (vdev->vfl_type) {
1310 	case VFL_TYPE_GRABBER:
1311 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1312 		break;
1313 	case VFL_TYPE_VBI:
1314 		type = V4L2_BUF_TYPE_VBI_CAPTURE;
1315 		break;
1316 	case VFL_TYPE_RADIO:
1317 		radio = 1;
1318 		break;
1319 	default:
1320 		return -EINVAL;
1321 	}
1322 
1323 	/* If more than one user, mutex should be added */
1324 	dev->users++;
1325 
1326 	dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1327 		video_device_node_name(vdev), v4l2_type_names[type],
1328 		dev->users);
1329 
1330 	/* allocate + initialize per filehandle data */
1331 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1332 	if (NULL == fh) {
1333 		dev->users--;
1334 		return -ENOMEM;
1335 	}
1336 
1337 	v4l2_fh_init(&fh->fh, vdev);
1338 	file->private_data = fh;
1339 	fh->dev      = dev;
1340 	fh->radio    = radio;
1341 	dev->radio   = radio;
1342 	fh->type     = type;
1343 	dev->fourcc  = format[0].fourcc;
1344 
1345 	fh->fmt      = format_by_fourcc(dev->fourcc);
1346 
1347 	tm6000_get_std_res(dev);
1348 
1349 	fh->width = dev->width;
1350 	fh->height = dev->height;
1351 
1352 	dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=%p, dev=%p, dev->vidq=%p\n",
1353 		fh, dev, &dev->vidq);
1354 	dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
1355 		list_empty(&dev->vidq.queued));
1356 	dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
1357 		list_empty(&dev->vidq.active));
1358 
1359 	/* initialize hardware on analog mode */
1360 	rc = tm6000_init_analog_mode(dev);
1361 	if (rc < 0) {
1362 		v4l2_fh_exit(&fh->fh);
1363 		kfree(fh);
1364 		return rc;
1365 	}
1366 
1367 	dev->mode = TM6000_MODE_ANALOG;
1368 
1369 	if (!fh->radio) {
1370 		videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1371 				NULL, &dev->slock,
1372 				fh->type,
1373 				V4L2_FIELD_INTERLACED,
1374 				sizeof(struct tm6000_buffer), fh, &dev->lock);
1375 	} else {
1376 		dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
1377 		tm6000_set_audio_rinput(dev);
1378 		v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1379 		tm6000_prepare_isoc(dev);
1380 		tm6000_start_thread(dev);
1381 	}
1382 	v4l2_fh_add(&fh->fh);
1383 
1384 	return 0;
1385 }
1386 
tm6000_open(struct file * file)1387 static int tm6000_open(struct file *file)
1388 {
1389 	struct video_device *vdev = video_devdata(file);
1390 	int res;
1391 
1392 	mutex_lock(vdev->lock);
1393 	res = __tm6000_open(file);
1394 	mutex_unlock(vdev->lock);
1395 	return res;
1396 }
1397 
1398 static ssize_t
tm6000_read(struct file * file,char __user * data,size_t count,loff_t * pos)1399 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1400 {
1401 	struct tm6000_fh *fh = file->private_data;
1402 	struct tm6000_core *dev = fh->dev;
1403 
1404 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1405 		int res;
1406 
1407 		if (!res_get(fh->dev, fh, true))
1408 			return -EBUSY;
1409 
1410 		if (mutex_lock_interruptible(&dev->lock))
1411 			return -ERESTARTSYS;
1412 		res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1413 					file->f_flags & O_NONBLOCK);
1414 		mutex_unlock(&dev->lock);
1415 		return res;
1416 	}
1417 	return 0;
1418 }
1419 
1420 static __poll_t
__tm6000_poll(struct file * file,struct poll_table_struct * wait)1421 __tm6000_poll(struct file *file, struct poll_table_struct *wait)
1422 {
1423 	__poll_t req_events = poll_requested_events(wait);
1424 	struct tm6000_fh        *fh = file->private_data;
1425 	struct tm6000_buffer    *buf;
1426 	__poll_t res = 0;
1427 
1428 	if (v4l2_event_pending(&fh->fh))
1429 		res = EPOLLPRI;
1430 	else if (req_events & EPOLLPRI)
1431 		poll_wait(file, &fh->fh.wait, wait);
1432 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1433 		return res | EPOLLERR;
1434 
1435 	if (!!is_res_streaming(fh->dev, fh))
1436 		return res | EPOLLERR;
1437 
1438 	if (!is_res_read(fh->dev, fh)) {
1439 		/* streaming capture */
1440 		if (list_empty(&fh->vb_vidq.stream))
1441 			return res | EPOLLERR;
1442 		buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
1443 		poll_wait(file, &buf->vb.done, wait);
1444 		if (buf->vb.state == VIDEOBUF_DONE ||
1445 		    buf->vb.state == VIDEOBUF_ERROR)
1446 			return res | EPOLLIN | EPOLLRDNORM;
1447 	} else if (req_events & (EPOLLIN | EPOLLRDNORM)) {
1448 		/* read() capture */
1449 		return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
1450 	}
1451 	return res;
1452 }
1453 
tm6000_poll(struct file * file,struct poll_table_struct * wait)1454 static __poll_t tm6000_poll(struct file *file, struct poll_table_struct *wait)
1455 {
1456 	struct tm6000_fh *fh = file->private_data;
1457 	struct tm6000_core *dev = fh->dev;
1458 	__poll_t res;
1459 
1460 	mutex_lock(&dev->lock);
1461 	res = __tm6000_poll(file, wait);
1462 	mutex_unlock(&dev->lock);
1463 	return res;
1464 }
1465 
tm6000_release(struct file * file)1466 static int tm6000_release(struct file *file)
1467 {
1468 	struct tm6000_fh         *fh = file->private_data;
1469 	struct tm6000_core      *dev = fh->dev;
1470 	struct video_device    *vdev = video_devdata(file);
1471 
1472 	dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1473 		video_device_node_name(vdev), dev->users);
1474 
1475 	mutex_lock(&dev->lock);
1476 	dev->users--;
1477 
1478 	res_free(dev, fh);
1479 
1480 	if (!dev->users) {
1481 		tm6000_uninit_isoc(dev);
1482 
1483 		/* Stop interrupt USB pipe */
1484 		tm6000_ir_int_stop(dev);
1485 
1486 		usb_reset_configuration(dev->udev);
1487 
1488 		if (dev->int_in.endp)
1489 			usb_set_interface(dev->udev,
1490 					dev->isoc_in.bInterfaceNumber, 2);
1491 		else
1492 			usb_set_interface(dev->udev,
1493 					dev->isoc_in.bInterfaceNumber, 0);
1494 
1495 		/* Start interrupt USB pipe */
1496 		tm6000_ir_int_start(dev);
1497 
1498 		if (!fh->radio)
1499 			videobuf_mmap_free(&fh->vb_vidq);
1500 	}
1501 	v4l2_fh_del(&fh->fh);
1502 	v4l2_fh_exit(&fh->fh);
1503 	kfree(fh);
1504 	mutex_unlock(&dev->lock);
1505 
1506 	return 0;
1507 }
1508 
tm6000_mmap(struct file * file,struct vm_area_struct * vma)1509 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1510 {
1511 	struct tm6000_fh *fh = file->private_data;
1512 	struct tm6000_core *dev = fh->dev;
1513 	int res;
1514 
1515 	if (mutex_lock_interruptible(&dev->lock))
1516 		return -ERESTARTSYS;
1517 	res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1518 	mutex_unlock(&dev->lock);
1519 	return res;
1520 }
1521 
1522 static const struct v4l2_file_operations tm6000_fops = {
1523 	.owner = THIS_MODULE,
1524 	.open = tm6000_open,
1525 	.release = tm6000_release,
1526 	.unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1527 	.read = tm6000_read,
1528 	.poll = tm6000_poll,
1529 	.mmap = tm6000_mmap,
1530 };
1531 
1532 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1533 	.vidioc_querycap          = vidioc_querycap,
1534 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1535 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1536 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1537 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1538 	.vidioc_s_std             = vidioc_s_std,
1539 	.vidioc_g_std             = vidioc_g_std,
1540 	.vidioc_enum_input        = vidioc_enum_input,
1541 	.vidioc_g_input           = vidioc_g_input,
1542 	.vidioc_s_input           = vidioc_s_input,
1543 	.vidioc_g_tuner           = vidioc_g_tuner,
1544 	.vidioc_s_tuner           = vidioc_s_tuner,
1545 	.vidioc_g_frequency       = vidioc_g_frequency,
1546 	.vidioc_s_frequency       = vidioc_s_frequency,
1547 	.vidioc_streamon          = vidioc_streamon,
1548 	.vidioc_streamoff         = vidioc_streamoff,
1549 	.vidioc_reqbufs           = vidioc_reqbufs,
1550 	.vidioc_querybuf          = vidioc_querybuf,
1551 	.vidioc_qbuf              = vidioc_qbuf,
1552 	.vidioc_dqbuf             = vidioc_dqbuf,
1553 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1554 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1555 };
1556 
1557 static struct video_device tm6000_template = {
1558 	.name		= "tm6000",
1559 	.fops           = &tm6000_fops,
1560 	.ioctl_ops      = &video_ioctl_ops,
1561 	.release	= video_device_release_empty,
1562 	.tvnorms        = TM6000_STD,
1563 };
1564 
1565 static const struct v4l2_file_operations radio_fops = {
1566 	.owner		= THIS_MODULE,
1567 	.open		= tm6000_open,
1568 	.poll		= v4l2_ctrl_poll,
1569 	.release	= tm6000_release,
1570 	.unlocked_ioctl	= video_ioctl2,
1571 };
1572 
1573 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1574 	.vidioc_querycap	= vidioc_querycap,
1575 	.vidioc_g_tuner		= radio_g_tuner,
1576 	.vidioc_s_tuner		= radio_s_tuner,
1577 	.vidioc_g_frequency	= vidioc_g_frequency,
1578 	.vidioc_s_frequency	= vidioc_s_frequency,
1579 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1580 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1581 };
1582 
1583 static struct video_device tm6000_radio_template = {
1584 	.name			= "tm6000",
1585 	.fops			= &radio_fops,
1586 	.ioctl_ops		= &radio_ioctl_ops,
1587 };
1588 
1589 /* -----------------------------------------------------------------
1590  *	Initialization and module stuff
1591  * ------------------------------------------------------------------
1592  */
1593 
vdev_init(struct tm6000_core * dev,struct video_device * vfd,const struct video_device * template,const char * type_name)1594 static void vdev_init(struct tm6000_core *dev,
1595 		struct video_device *vfd,
1596 		const struct video_device
1597 		*template, const char *type_name)
1598 {
1599 	*vfd = *template;
1600 	vfd->v4l2_dev = &dev->v4l2_dev;
1601 	vfd->release = video_device_release_empty;
1602 	vfd->lock = &dev->lock;
1603 
1604 	snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1605 
1606 	video_set_drvdata(vfd, dev);
1607 }
1608 
tm6000_v4l2_register(struct tm6000_core * dev)1609 int tm6000_v4l2_register(struct tm6000_core *dev)
1610 {
1611 	int ret = 0;
1612 
1613 	v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1614 	v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1615 	v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1616 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1617 	v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1618 			V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1619 	v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1620 			V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1621 	v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1622 			V4L2_CID_CONTRAST, 0, 255, 1, 119);
1623 	v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1624 			V4L2_CID_SATURATION, 0, 255, 1, 112);
1625 	v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1626 			V4L2_CID_HUE, -128, 127, 1, 0);
1627 	v4l2_ctrl_add_handler(&dev->ctrl_handler,
1628 			&dev->radio_ctrl_handler, NULL);
1629 
1630 	if (dev->radio_ctrl_handler.error)
1631 		ret = dev->radio_ctrl_handler.error;
1632 	if (!ret && dev->ctrl_handler.error)
1633 		ret = dev->ctrl_handler.error;
1634 	if (ret)
1635 		goto free_ctrl;
1636 
1637 	vdev_init(dev, &dev->vfd, &tm6000_template, "video");
1638 
1639 	dev->vfd.ctrl_handler = &dev->ctrl_handler;
1640 
1641 	/* init video dma queues */
1642 	INIT_LIST_HEAD(&dev->vidq.active);
1643 	INIT_LIST_HEAD(&dev->vidq.queued);
1644 
1645 	ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
1646 
1647 	if (ret < 0) {
1648 		printk(KERN_INFO "%s: can't register video device\n",
1649 		       dev->name);
1650 		goto free_ctrl;
1651 	}
1652 
1653 	printk(KERN_INFO "%s: registered device %s\n",
1654 	       dev->name, video_device_node_name(&dev->vfd));
1655 
1656 	if (dev->caps.has_radio) {
1657 		vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
1658 							   "radio");
1659 		dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1660 		ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
1661 					    radio_nr);
1662 		if (ret < 0) {
1663 			printk(KERN_INFO "%s: can't register radio device\n",
1664 			       dev->name);
1665 			goto unreg_video;
1666 		}
1667 
1668 		printk(KERN_INFO "%s: registered device %s\n",
1669 		       dev->name, video_device_node_name(&dev->radio_dev));
1670 	}
1671 
1672 	printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1673 	return ret;
1674 
1675 unreg_video:
1676 	video_unregister_device(&dev->vfd);
1677 free_ctrl:
1678 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1679 	v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1680 	return ret;
1681 }
1682 
tm6000_v4l2_unregister(struct tm6000_core * dev)1683 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1684 {
1685 	video_unregister_device(&dev->vfd);
1686 
1687 	/* if URB buffers are still allocated free them now */
1688 	tm6000_free_urb_buffers(dev);
1689 
1690 	video_unregister_device(&dev->radio_dev);
1691 	return 0;
1692 }
1693 
tm6000_v4l2_exit(void)1694 int tm6000_v4l2_exit(void)
1695 {
1696 	return 0;
1697 }
1698 
1699 module_param(video_nr, int, 0);
1700 MODULE_PARM_DESC(video_nr, "Allow changing video device number");
1701 
1702 module_param_named(debug, tm6000_debug, int, 0444);
1703 MODULE_PARM_DESC(debug, "activates debug info");
1704 
1705 module_param(vid_limit, int, 0644);
1706 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
1707 
1708 module_param(keep_urb, bool, 0);
1709 MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");
1710