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