treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / usb / hdpvr / hdpvr-video.c
blobbad71d863d395c8582004fd8480507f2fc44ed31
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Hauppauge HD PVR USB driver - video 4 linux 2 interface
5 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
6 */
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/usb.h>
15 #include <linux/mutex.h>
16 #include <linux/workqueue.h>
18 #include <linux/videodev2.h>
19 #include <linux/v4l2-dv-timings.h>
20 #include <media/v4l2-dev.h>
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-dv-timings.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-event.h>
25 #include "hdpvr.h"
27 #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
29 #define print_buffer_status() { \
30 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
31 "%s:%d buffer stat: %d free, %d proc\n", \
32 __func__, __LINE__, \
33 list_size(&dev->free_buff_list), \
34 list_size(&dev->rec_buff_list)); }
36 static const struct v4l2_dv_timings hdpvr_dv_timings[] = {
37 V4L2_DV_BT_CEA_720X480I59_94,
38 V4L2_DV_BT_CEA_720X576I50,
39 V4L2_DV_BT_CEA_720X480P59_94,
40 V4L2_DV_BT_CEA_720X576P50,
41 V4L2_DV_BT_CEA_1280X720P50,
42 V4L2_DV_BT_CEA_1280X720P60,
43 V4L2_DV_BT_CEA_1920X1080I50,
44 V4L2_DV_BT_CEA_1920X1080I60,
47 /* Use 480i59 as the default timings */
48 #define HDPVR_DEF_DV_TIMINGS_IDX (0)
50 struct hdpvr_fh {
51 struct v4l2_fh fh;
52 bool legacy_mode;
55 static uint list_size(struct list_head *list)
57 struct list_head *tmp;
58 uint count = 0;
60 list_for_each(tmp, list) {
61 count++;
64 return count;
67 /*=========================================================================*/
68 /* urb callback */
69 static void hdpvr_read_bulk_callback(struct urb *urb)
71 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
72 struct hdpvr_device *dev = buf->dev;
74 /* marking buffer as received and wake waiting */
75 buf->status = BUFSTAT_READY;
76 wake_up_interruptible(&dev->wait_data);
79 /*=========================================================================*/
80 /* buffer bits */
82 /* function expects dev->io_mutex to be hold by caller */
83 int hdpvr_cancel_queue(struct hdpvr_device *dev)
85 struct hdpvr_buffer *buf;
87 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
88 usb_kill_urb(buf->urb);
89 buf->status = BUFSTAT_AVAILABLE;
92 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
94 return 0;
97 static int hdpvr_free_queue(struct list_head *q)
99 struct list_head *tmp;
100 struct list_head *p;
101 struct hdpvr_buffer *buf;
102 struct urb *urb;
104 for (p = q->next; p != q;) {
105 buf = list_entry(p, struct hdpvr_buffer, buff_list);
107 urb = buf->urb;
108 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
109 urb->transfer_buffer, urb->transfer_dma);
110 usb_free_urb(urb);
111 tmp = p->next;
112 list_del(p);
113 kfree(buf);
114 p = tmp;
117 return 0;
120 /* function expects dev->io_mutex to be hold by caller */
121 int hdpvr_free_buffers(struct hdpvr_device *dev)
123 hdpvr_cancel_queue(dev);
125 hdpvr_free_queue(&dev->free_buff_list);
126 hdpvr_free_queue(&dev->rec_buff_list);
128 return 0;
131 /* function expects dev->io_mutex to be hold by caller */
132 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
134 uint i;
135 int retval = -ENOMEM;
136 u8 *mem;
137 struct hdpvr_buffer *buf;
138 struct urb *urb;
140 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
141 "allocating %u buffers\n", count);
143 for (i = 0; i < count; i++) {
145 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
146 if (!buf) {
147 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
148 goto exit;
150 buf->dev = dev;
152 urb = usb_alloc_urb(0, GFP_KERNEL);
153 if (!urb)
154 goto exit_urb;
155 buf->urb = urb;
157 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
158 &urb->transfer_dma);
159 if (!mem) {
160 v4l2_err(&dev->v4l2_dev,
161 "cannot allocate usb transfer buffer\n");
162 goto exit_urb_buffer;
165 usb_fill_bulk_urb(buf->urb, dev->udev,
166 usb_rcvbulkpipe(dev->udev,
167 dev->bulk_in_endpointAddr),
168 mem, dev->bulk_in_size,
169 hdpvr_read_bulk_callback, buf);
171 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
172 buf->status = BUFSTAT_AVAILABLE;
173 list_add_tail(&buf->buff_list, &dev->free_buff_list);
175 return 0;
176 exit_urb_buffer:
177 usb_free_urb(urb);
178 exit_urb:
179 kfree(buf);
180 exit:
181 hdpvr_free_buffers(dev);
182 return retval;
185 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
187 struct hdpvr_buffer *buf;
188 struct urb *urb;
189 int ret = 0, err_count = 0;
191 mutex_lock(&dev->io_mutex);
193 while (dev->status == STATUS_STREAMING &&
194 !list_empty(&dev->free_buff_list)) {
196 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
197 buff_list);
198 if (buf->status != BUFSTAT_AVAILABLE) {
199 v4l2_err(&dev->v4l2_dev,
200 "buffer not marked as available\n");
201 ret = -EFAULT;
202 goto err;
205 urb = buf->urb;
206 urb->status = 0;
207 urb->actual_length = 0;
208 ret = usb_submit_urb(urb, GFP_KERNEL);
209 if (ret) {
210 v4l2_err(&dev->v4l2_dev,
211 "usb_submit_urb in %s returned %d\n",
212 __func__, ret);
213 if (++err_count > 2)
214 break;
215 continue;
217 buf->status = BUFSTAT_INPROGRESS;
218 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
220 err:
221 print_buffer_status();
222 mutex_unlock(&dev->io_mutex);
223 return ret;
226 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
228 struct hdpvr_buffer *buf;
230 mutex_lock(&dev->io_mutex);
232 if (list_empty(&dev->rec_buff_list)) {
233 mutex_unlock(&dev->io_mutex);
234 return NULL;
237 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
238 buff_list);
239 mutex_unlock(&dev->io_mutex);
241 return buf;
244 static void hdpvr_transmit_buffers(struct work_struct *work)
246 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
247 worker);
249 while (dev->status == STATUS_STREAMING) {
251 if (hdpvr_submit_buffers(dev)) {
252 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
253 goto error;
255 if (wait_event_interruptible(dev->wait_buffer,
256 !list_empty(&dev->free_buff_list) ||
257 dev->status != STATUS_STREAMING))
258 goto error;
261 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
262 "transmit worker exited\n");
263 return;
264 error:
265 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
266 "transmit buffers errored\n");
267 dev->status = STATUS_ERROR;
270 /* function expects dev->io_mutex to be hold by caller */
271 static int hdpvr_start_streaming(struct hdpvr_device *dev)
273 int ret;
274 struct hdpvr_video_info vidinf;
276 if (dev->status == STATUS_STREAMING)
277 return 0;
278 if (dev->status != STATUS_IDLE)
279 return -EAGAIN;
281 ret = get_video_info(dev, &vidinf);
282 if (ret < 0)
283 return ret;
285 if (!vidinf.valid) {
286 msleep(250);
287 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
288 "no video signal at input %d\n", dev->options.video_input);
289 return -EAGAIN;
292 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
293 "video signal: %dx%d@%dhz\n", vidinf.width,
294 vidinf.height, vidinf.fps);
296 /* start streaming 2 request */
297 ret = usb_control_msg(dev->udev,
298 usb_sndctrlpipe(dev->udev, 0),
299 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
300 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
301 "encoder start control request returned %d\n", ret);
302 if (ret < 0)
303 return ret;
305 ret = hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
306 if (ret)
307 return ret;
309 dev->status = STATUS_STREAMING;
311 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
312 schedule_work(&dev->worker);
314 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
315 "streaming started\n");
317 return 0;
321 /* function expects dev->io_mutex to be hold by caller */
322 static int hdpvr_stop_streaming(struct hdpvr_device *dev)
324 int actual_length;
325 uint c = 0;
326 u8 *buf;
328 if (dev->status == STATUS_IDLE)
329 return 0;
330 else if (dev->status != STATUS_STREAMING)
331 return -EAGAIN;
333 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
334 if (!buf)
335 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer for emptying the internal device buffer. Next capture start will be slow\n");
337 dev->status = STATUS_SHUTTING_DOWN;
338 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
339 mutex_unlock(&dev->io_mutex);
341 wake_up_interruptible(&dev->wait_buffer);
342 msleep(50);
344 flush_work(&dev->worker);
346 mutex_lock(&dev->io_mutex);
347 /* kill the still outstanding urbs */
348 hdpvr_cancel_queue(dev);
350 /* emptying the device buffer beforeshutting it down */
351 while (buf && ++c < 500 &&
352 !usb_bulk_msg(dev->udev,
353 usb_rcvbulkpipe(dev->udev,
354 dev->bulk_in_endpointAddr),
355 buf, dev->bulk_in_size, &actual_length,
356 BULK_URB_TIMEOUT)) {
357 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
358 "%2d: got %d bytes\n", c, actual_length);
360 kfree(buf);
361 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
362 "used %d urbs to empty device buffers\n", c-1);
363 msleep(10);
365 dev->status = STATUS_IDLE;
367 return 0;
371 /*=======================================================================*/
373 * video 4 linux 2 file operations
376 static int hdpvr_open(struct file *file)
378 struct hdpvr_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
380 if (fh == NULL)
381 return -ENOMEM;
382 fh->legacy_mode = true;
383 v4l2_fh_init(&fh->fh, video_devdata(file));
384 v4l2_fh_add(&fh->fh);
385 file->private_data = fh;
386 return 0;
389 static int hdpvr_release(struct file *file)
391 struct hdpvr_device *dev = video_drvdata(file);
393 mutex_lock(&dev->io_mutex);
394 if (file->private_data == dev->owner) {
395 hdpvr_stop_streaming(dev);
396 dev->owner = NULL;
398 mutex_unlock(&dev->io_mutex);
400 return v4l2_fh_release(file);
404 * hdpvr_v4l2_read()
405 * will allocate buffers when called for the first time
407 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
408 loff_t *pos)
410 struct hdpvr_device *dev = video_drvdata(file);
411 struct hdpvr_buffer *buf = NULL;
412 struct urb *urb;
413 unsigned int ret = 0;
414 int rem, cnt;
416 if (*pos)
417 return -ESPIPE;
419 mutex_lock(&dev->io_mutex);
420 if (dev->status == STATUS_IDLE) {
421 if (hdpvr_start_streaming(dev)) {
422 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
423 "start_streaming failed\n");
424 ret = -EIO;
425 msleep(200);
426 dev->status = STATUS_IDLE;
427 mutex_unlock(&dev->io_mutex);
428 goto err;
430 dev->owner = file->private_data;
431 print_buffer_status();
433 mutex_unlock(&dev->io_mutex);
435 /* wait for the first buffer */
436 if (!(file->f_flags & O_NONBLOCK)) {
437 if (wait_event_interruptible(dev->wait_data,
438 !list_empty_careful(&dev->rec_buff_list)))
439 return -ERESTARTSYS;
442 buf = hdpvr_get_next_buffer(dev);
444 while (count > 0 && buf) {
446 if (buf->status != BUFSTAT_READY &&
447 dev->status != STATUS_DISCONNECTED) {
448 int err;
449 /* return nonblocking */
450 if (file->f_flags & O_NONBLOCK) {
451 if (!ret)
452 ret = -EAGAIN;
453 goto err;
456 err = wait_event_interruptible_timeout(dev->wait_data,
457 buf->status == BUFSTAT_READY,
458 msecs_to_jiffies(1000));
459 if (err < 0) {
460 ret = err;
461 goto err;
463 if (!err) {
464 v4l2_info(&dev->v4l2_dev,
465 "timeout: restart streaming\n");
466 mutex_lock(&dev->io_mutex);
467 hdpvr_stop_streaming(dev);
468 mutex_unlock(&dev->io_mutex);
470 * The FW needs about 4 seconds after streaming
471 * stopped before it is ready to restart
472 * streaming.
474 msleep(4000);
475 err = hdpvr_start_streaming(dev);
476 if (err) {
477 ret = err;
478 goto err;
483 if (buf->status != BUFSTAT_READY)
484 break;
486 /* set remaining bytes to copy */
487 urb = buf->urb;
488 rem = urb->actual_length - buf->pos;
489 cnt = rem > count ? count : rem;
491 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
492 cnt)) {
493 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
494 if (!ret)
495 ret = -EFAULT;
496 goto err;
499 buf->pos += cnt;
500 count -= cnt;
501 buffer += cnt;
502 ret += cnt;
504 /* finished, take next buffer */
505 if (buf->pos == urb->actual_length) {
506 mutex_lock(&dev->io_mutex);
507 buf->pos = 0;
508 buf->status = BUFSTAT_AVAILABLE;
510 list_move_tail(&buf->buff_list, &dev->free_buff_list);
512 print_buffer_status();
514 mutex_unlock(&dev->io_mutex);
516 wake_up_interruptible(&dev->wait_buffer);
518 buf = hdpvr_get_next_buffer(dev);
521 err:
522 if (!ret && !buf)
523 ret = -EAGAIN;
524 return ret;
527 static __poll_t hdpvr_poll(struct file *filp, poll_table *wait)
529 __poll_t req_events = poll_requested_events(wait);
530 struct hdpvr_buffer *buf = NULL;
531 struct hdpvr_device *dev = video_drvdata(filp);
532 __poll_t mask = v4l2_ctrl_poll(filp, wait);
534 if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
535 return mask;
537 mutex_lock(&dev->io_mutex);
539 if (dev->status == STATUS_IDLE) {
540 if (hdpvr_start_streaming(dev)) {
541 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
542 "start_streaming failed\n");
543 dev->status = STATUS_IDLE;
544 } else {
545 dev->owner = filp->private_data;
548 print_buffer_status();
550 mutex_unlock(&dev->io_mutex);
552 buf = hdpvr_get_next_buffer(dev);
553 /* only wait if no data is available */
554 if (!buf || buf->status != BUFSTAT_READY) {
555 poll_wait(filp, &dev->wait_data, wait);
556 buf = hdpvr_get_next_buffer(dev);
558 if (buf && buf->status == BUFSTAT_READY)
559 mask |= EPOLLIN | EPOLLRDNORM;
561 return mask;
565 static const struct v4l2_file_operations hdpvr_fops = {
566 .owner = THIS_MODULE,
567 .open = hdpvr_open,
568 .release = hdpvr_release,
569 .read = hdpvr_read,
570 .poll = hdpvr_poll,
571 .unlocked_ioctl = video_ioctl2,
574 /*=======================================================================*/
576 * V4L2 ioctl handling
579 static int vidioc_querycap(struct file *file, void *priv,
580 struct v4l2_capability *cap)
582 struct hdpvr_device *dev = video_drvdata(file);
584 strscpy(cap->driver, "hdpvr", sizeof(cap->driver));
585 strscpy(cap->card, "Hauppauge HD PVR", sizeof(cap->card));
586 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
587 return 0;
590 static int vidioc_s_std(struct file *file, void *_fh,
591 v4l2_std_id std)
593 struct hdpvr_device *dev = video_drvdata(file);
594 struct hdpvr_fh *fh = _fh;
595 u8 std_type = 1;
597 if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
598 return -ENODATA;
599 if (dev->status != STATUS_IDLE)
600 return -EBUSY;
601 if (std & V4L2_STD_525_60)
602 std_type = 0;
603 dev->cur_std = std;
604 dev->width = 720;
605 dev->height = std_type ? 576 : 480;
607 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
610 static int vidioc_g_std(struct file *file, void *_fh,
611 v4l2_std_id *std)
613 struct hdpvr_device *dev = video_drvdata(file);
614 struct hdpvr_fh *fh = _fh;
616 if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
617 return -ENODATA;
618 *std = dev->cur_std;
619 return 0;
622 static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a)
624 struct hdpvr_device *dev = video_drvdata(file);
625 struct hdpvr_video_info vid_info;
626 struct hdpvr_fh *fh = _fh;
627 int ret;
629 *a = V4L2_STD_UNKNOWN;
630 if (dev->options.video_input == HDPVR_COMPONENT)
631 return fh->legacy_mode ? 0 : -ENODATA;
632 ret = get_video_info(dev, &vid_info);
633 if (vid_info.valid && vid_info.width == 720 &&
634 (vid_info.height == 480 || vid_info.height == 576)) {
635 *a = (vid_info.height == 480) ?
636 V4L2_STD_525_60 : V4L2_STD_625_50;
638 return ret;
641 static int vidioc_s_dv_timings(struct file *file, void *_fh,
642 struct v4l2_dv_timings *timings)
644 struct hdpvr_device *dev = video_drvdata(file);
645 struct hdpvr_fh *fh = _fh;
646 int i;
648 fh->legacy_mode = false;
649 if (dev->options.video_input)
650 return -ENODATA;
651 if (dev->status != STATUS_IDLE)
652 return -EBUSY;
653 for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++)
654 if (v4l2_match_dv_timings(timings, hdpvr_dv_timings + i, 0, false))
655 break;
656 if (i == ARRAY_SIZE(hdpvr_dv_timings))
657 return -EINVAL;
658 dev->cur_dv_timings = hdpvr_dv_timings[i];
659 dev->width = hdpvr_dv_timings[i].bt.width;
660 dev->height = hdpvr_dv_timings[i].bt.height;
661 return 0;
664 static int vidioc_g_dv_timings(struct file *file, void *_fh,
665 struct v4l2_dv_timings *timings)
667 struct hdpvr_device *dev = video_drvdata(file);
668 struct hdpvr_fh *fh = _fh;
670 fh->legacy_mode = false;
671 if (dev->options.video_input)
672 return -ENODATA;
673 *timings = dev->cur_dv_timings;
674 return 0;
677 static int vidioc_query_dv_timings(struct file *file, void *_fh,
678 struct v4l2_dv_timings *timings)
680 struct hdpvr_device *dev = video_drvdata(file);
681 struct hdpvr_fh *fh = _fh;
682 struct hdpvr_video_info vid_info;
683 bool interlaced;
684 int ret = 0;
685 int i;
687 fh->legacy_mode = false;
688 if (dev->options.video_input)
689 return -ENODATA;
690 ret = get_video_info(dev, &vid_info);
691 if (ret)
692 return ret;
693 if (!vid_info.valid)
694 return -ENOLCK;
695 interlaced = vid_info.fps <= 30;
696 for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
697 const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
698 unsigned hsize;
699 unsigned vsize;
700 unsigned fps;
702 hsize = V4L2_DV_BT_FRAME_WIDTH(bt);
703 vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
704 fps = (unsigned)bt->pixelclock / (hsize * vsize);
705 if (bt->width != vid_info.width ||
706 bt->height != vid_info.height ||
707 bt->interlaced != interlaced ||
708 (fps != vid_info.fps && fps + 1 != vid_info.fps))
709 continue;
710 *timings = hdpvr_dv_timings[i];
711 break;
713 if (i == ARRAY_SIZE(hdpvr_dv_timings))
714 ret = -ERANGE;
716 return ret;
719 static int vidioc_enum_dv_timings(struct file *file, void *_fh,
720 struct v4l2_enum_dv_timings *timings)
722 struct hdpvr_device *dev = video_drvdata(file);
723 struct hdpvr_fh *fh = _fh;
725 fh->legacy_mode = false;
726 memset(timings->reserved, 0, sizeof(timings->reserved));
727 if (dev->options.video_input)
728 return -ENODATA;
729 if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
730 return -EINVAL;
731 timings->timings = hdpvr_dv_timings[timings->index];
732 return 0;
735 static int vidioc_dv_timings_cap(struct file *file, void *_fh,
736 struct v4l2_dv_timings_cap *cap)
738 struct hdpvr_device *dev = video_drvdata(file);
739 struct hdpvr_fh *fh = _fh;
741 fh->legacy_mode = false;
742 if (dev->options.video_input)
743 return -ENODATA;
744 cap->type = V4L2_DV_BT_656_1120;
745 cap->bt.min_width = 720;
746 cap->bt.max_width = 1920;
747 cap->bt.min_height = 480;
748 cap->bt.max_height = 1080;
749 cap->bt.min_pixelclock = 27000000;
750 cap->bt.max_pixelclock = 74250000;
751 cap->bt.standards = V4L2_DV_BT_STD_CEA861;
752 cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
753 return 0;
756 static const char *iname[] = {
757 [HDPVR_COMPONENT] = "Component",
758 [HDPVR_SVIDEO] = "S-Video",
759 [HDPVR_COMPOSITE] = "Composite",
762 static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i)
764 unsigned int n;
766 n = i->index;
767 if (n >= HDPVR_VIDEO_INPUTS)
768 return -EINVAL;
770 i->type = V4L2_INPUT_TYPE_CAMERA;
772 strscpy(i->name, iname[n], sizeof(i->name));
774 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
776 i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
777 i->std = n ? V4L2_STD_ALL : 0;
779 return 0;
782 static int vidioc_s_input(struct file *file, void *_fh,
783 unsigned int index)
785 struct hdpvr_device *dev = video_drvdata(file);
786 int retval;
788 if (index >= HDPVR_VIDEO_INPUTS)
789 return -EINVAL;
791 if (dev->status != STATUS_IDLE)
792 return -EBUSY;
794 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
795 if (!retval) {
796 dev->options.video_input = index;
798 * Unfortunately gstreamer calls ENUMSTD and bails out if it
799 * won't find any formats, even though component input is
800 * selected. This means that we have to leave tvnorms at
801 * V4L2_STD_ALL. We cannot use the 'legacy' trick since
802 * tvnorms is set at the device node level and not at the
803 * filehandle level.
805 * Comment this out for now, but if the legacy mode can be
806 * removed in the future, then this code should be enabled
807 * again.
808 dev->video_dev.tvnorms =
809 (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
813 return retval;
816 static int vidioc_g_input(struct file *file, void *private_data,
817 unsigned int *index)
819 struct hdpvr_device *dev = video_drvdata(file);
821 *index = dev->options.video_input;
822 return 0;
826 static const char *audio_iname[] = {
827 [HDPVR_RCA_FRONT] = "RCA front",
828 [HDPVR_RCA_BACK] = "RCA back",
829 [HDPVR_SPDIF] = "SPDIF",
832 static int vidioc_enumaudio(struct file *file, void *priv,
833 struct v4l2_audio *audio)
835 unsigned int n;
837 n = audio->index;
838 if (n >= HDPVR_AUDIO_INPUTS)
839 return -EINVAL;
841 audio->capability = V4L2_AUDCAP_STEREO;
843 strscpy(audio->name, audio_iname[n], sizeof(audio->name));
845 return 0;
848 static int vidioc_s_audio(struct file *file, void *private_data,
849 const struct v4l2_audio *audio)
851 struct hdpvr_device *dev = video_drvdata(file);
852 int retval;
854 if (audio->index >= HDPVR_AUDIO_INPUTS)
855 return -EINVAL;
857 if (dev->status != STATUS_IDLE)
858 return -EBUSY;
860 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
861 if (!retval)
862 dev->options.audio_input = audio->index;
864 return retval;
867 static int vidioc_g_audio(struct file *file, void *private_data,
868 struct v4l2_audio *audio)
870 struct hdpvr_device *dev = video_drvdata(file);
872 audio->index = dev->options.audio_input;
873 audio->capability = V4L2_AUDCAP_STEREO;
874 strscpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
875 return 0;
878 static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
880 struct hdpvr_device *dev =
881 container_of(ctrl->handler, struct hdpvr_device, hdl);
883 switch (ctrl->id) {
884 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
885 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
886 dev->video_bitrate->val >= dev->video_bitrate_peak->val)
887 dev->video_bitrate_peak->val =
888 dev->video_bitrate->val + 100000;
889 break;
891 return 0;
894 static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
896 struct hdpvr_device *dev =
897 container_of(ctrl->handler, struct hdpvr_device, hdl);
898 struct hdpvr_options *opt = &dev->options;
899 int ret = -EINVAL;
901 switch (ctrl->id) {
902 case V4L2_CID_BRIGHTNESS:
903 ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
904 if (ret)
905 break;
906 dev->options.brightness = ctrl->val;
907 return 0;
908 case V4L2_CID_CONTRAST:
909 ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
910 if (ret)
911 break;
912 dev->options.contrast = ctrl->val;
913 return 0;
914 case V4L2_CID_SATURATION:
915 ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
916 if (ret)
917 break;
918 dev->options.saturation = ctrl->val;
919 return 0;
920 case V4L2_CID_HUE:
921 ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
922 if (ret)
923 break;
924 dev->options.hue = ctrl->val;
925 return 0;
926 case V4L2_CID_SHARPNESS:
927 ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
928 if (ret)
929 break;
930 dev->options.sharpness = ctrl->val;
931 return 0;
932 case V4L2_CID_MPEG_AUDIO_ENCODING:
933 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
934 opt->audio_codec = ctrl->val;
935 return hdpvr_set_audio(dev, opt->audio_input + 1,
936 opt->audio_codec);
938 return 0;
939 case V4L2_CID_MPEG_VIDEO_ENCODING:
940 return 0;
941 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
942 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
943 /* opt->gop_mode |= 0x2; */
944 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
945 /* opt->gop_mode); */
946 /* } */
947 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
948 /* opt->gop_mode &= ~0x2; */
949 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
950 /* opt->gop_mode); */
951 /* } */
952 /* break; */
953 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
954 uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
955 uint bitrate = dev->video_bitrate->val / 100000;
957 if (ctrl->is_new) {
958 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
959 opt->bitrate_mode = HDPVR_CONSTANT;
960 else
961 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
962 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
963 opt->bitrate_mode);
964 v4l2_ctrl_activate(dev->video_bitrate_peak,
965 ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
968 if (dev->video_bitrate_peak->is_new ||
969 dev->video_bitrate->is_new) {
970 opt->bitrate = bitrate;
971 opt->peak_bitrate = peak_bitrate;
972 hdpvr_set_bitrate(dev);
974 return 0;
976 case V4L2_CID_MPEG_STREAM_TYPE:
977 return 0;
978 default:
979 break;
981 return ret;
984 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
985 struct v4l2_fmtdesc *f)
987 if (f->index != 0)
988 return -EINVAL;
990 f->pixelformat = V4L2_PIX_FMT_MPEG;
992 return 0;
995 static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
996 struct v4l2_format *f)
998 struct hdpvr_device *dev = video_drvdata(file);
999 struct hdpvr_fh *fh = _fh;
1000 int ret;
1003 * The original driver would always returns the current detected
1004 * resolution as the format (and EFAULT if it couldn't be detected).
1005 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
1006 * better way of doing this, but to stay compatible with existing
1007 * applications we assume legacy mode every time an application opens
1008 * the device. Only if one of the new DV_TIMINGS ioctls is called
1009 * will the filehandle go into 'normal' mode where g_fmt returns the
1010 * last set format.
1012 if (fh->legacy_mode) {
1013 struct hdpvr_video_info vid_info;
1015 ret = get_video_info(dev, &vid_info);
1016 if (ret < 0)
1017 return ret;
1018 if (!vid_info.valid)
1019 return -EFAULT;
1020 f->fmt.pix.width = vid_info.width;
1021 f->fmt.pix.height = vid_info.height;
1022 } else {
1023 f->fmt.pix.width = dev->width;
1024 f->fmt.pix.height = dev->height;
1026 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1027 f->fmt.pix.sizeimage = dev->bulk_in_size;
1028 f->fmt.pix.bytesperline = 0;
1029 if (f->fmt.pix.width == 720) {
1030 /* SDTV formats */
1031 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1032 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1033 } else {
1034 /* HDTV formats */
1035 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
1036 f->fmt.pix.field = V4L2_FIELD_NONE;
1038 return 0;
1041 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1042 struct v4l2_encoder_cmd *a)
1044 struct hdpvr_device *dev = video_drvdata(filp);
1045 int res = 0;
1047 mutex_lock(&dev->io_mutex);
1048 a->flags = 0;
1050 switch (a->cmd) {
1051 case V4L2_ENC_CMD_START:
1052 if (dev->owner && filp->private_data != dev->owner) {
1053 res = -EBUSY;
1054 break;
1056 if (dev->status == STATUS_STREAMING)
1057 break;
1058 res = hdpvr_start_streaming(dev);
1059 if (!res)
1060 dev->owner = filp->private_data;
1061 else
1062 dev->status = STATUS_IDLE;
1063 break;
1064 case V4L2_ENC_CMD_STOP:
1065 if (dev->owner && filp->private_data != dev->owner) {
1066 res = -EBUSY;
1067 break;
1069 if (dev->status == STATUS_IDLE)
1070 break;
1071 res = hdpvr_stop_streaming(dev);
1072 if (!res)
1073 dev->owner = NULL;
1074 break;
1075 default:
1076 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1077 "Unsupported encoder cmd %d\n", a->cmd);
1078 res = -EINVAL;
1079 break;
1082 mutex_unlock(&dev->io_mutex);
1083 return res;
1086 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1087 struct v4l2_encoder_cmd *a)
1089 a->flags = 0;
1090 switch (a->cmd) {
1091 case V4L2_ENC_CMD_START:
1092 case V4L2_ENC_CMD_STOP:
1093 return 0;
1094 default:
1095 return -EINVAL;
1099 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1100 .vidioc_querycap = vidioc_querycap,
1101 .vidioc_s_std = vidioc_s_std,
1102 .vidioc_g_std = vidioc_g_std,
1103 .vidioc_querystd = vidioc_querystd,
1104 .vidioc_s_dv_timings = vidioc_s_dv_timings,
1105 .vidioc_g_dv_timings = vidioc_g_dv_timings,
1106 .vidioc_query_dv_timings= vidioc_query_dv_timings,
1107 .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
1108 .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
1109 .vidioc_enum_input = vidioc_enum_input,
1110 .vidioc_g_input = vidioc_g_input,
1111 .vidioc_s_input = vidioc_s_input,
1112 .vidioc_enumaudio = vidioc_enumaudio,
1113 .vidioc_g_audio = vidioc_g_audio,
1114 .vidioc_s_audio = vidioc_s_audio,
1115 .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
1116 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1117 .vidioc_s_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1118 .vidioc_try_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1119 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1120 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1121 .vidioc_log_status = v4l2_ctrl_log_status,
1122 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1123 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1126 static void hdpvr_device_release(struct video_device *vdev)
1128 struct hdpvr_device *dev = video_get_drvdata(vdev);
1130 hdpvr_delete(dev);
1131 flush_work(&dev->worker);
1133 v4l2_device_unregister(&dev->v4l2_dev);
1134 v4l2_ctrl_handler_free(&dev->hdl);
1136 /* deregister I2C adapter */
1137 #if IS_ENABLED(CONFIG_I2C)
1138 mutex_lock(&dev->i2c_mutex);
1139 i2c_del_adapter(&dev->i2c_adapter);
1140 mutex_unlock(&dev->i2c_mutex);
1141 #endif /* CONFIG_I2C */
1143 kfree(dev->usbc_buf);
1144 kfree(dev);
1147 static const struct video_device hdpvr_video_template = {
1148 .fops = &hdpvr_fops,
1149 .release = hdpvr_device_release,
1150 .ioctl_ops = &hdpvr_ioctl_ops,
1151 .tvnorms = V4L2_STD_ALL,
1152 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
1153 V4L2_CAP_READWRITE,
1156 static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
1157 .try_ctrl = hdpvr_try_ctrl,
1158 .s_ctrl = hdpvr_s_ctrl,
1161 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1162 int devnum)
1164 struct v4l2_ctrl_handler *hdl = &dev->hdl;
1165 bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
1166 int res;
1168 dev->cur_std = V4L2_STD_525_60;
1169 dev->width = 720;
1170 dev->height = 480;
1171 dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX];
1172 v4l2_ctrl_handler_init(hdl, 11);
1173 if (dev->fw_ver > 0x15) {
1174 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1175 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
1176 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1177 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
1178 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1179 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
1180 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1181 V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
1182 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1183 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1184 } else {
1185 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1186 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
1187 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1188 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
1189 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1190 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
1191 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1192 V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
1193 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1194 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1197 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1198 V4L2_CID_MPEG_STREAM_TYPE,
1199 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
1200 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
1201 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1202 V4L2_CID_MPEG_AUDIO_ENCODING,
1203 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
1204 0x7, ac3 ? dev->options.audio_codec : V4L2_MPEG_AUDIO_ENCODING_AAC);
1205 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1206 V4L2_CID_MPEG_VIDEO_ENCODING,
1207 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
1208 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
1210 dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1211 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1212 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1213 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1215 dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1216 V4L2_CID_MPEG_VIDEO_BITRATE,
1217 1000000, 13500000, 100000, 6500000);
1218 dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1219 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1220 1100000, 20200000, 100000, 9000000);
1221 dev->v4l2_dev.ctrl_handler = hdl;
1222 if (hdl->error) {
1223 res = hdl->error;
1224 v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
1225 goto error;
1227 v4l2_ctrl_cluster(3, &dev->video_mode);
1228 res = v4l2_ctrl_handler_setup(hdl);
1229 if (res < 0) {
1230 v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
1231 goto error;
1234 /* setup and register video device */
1235 dev->video_dev = hdpvr_video_template;
1236 strscpy(dev->video_dev.name, "Hauppauge HD PVR",
1237 sizeof(dev->video_dev.name));
1238 dev->video_dev.v4l2_dev = &dev->v4l2_dev;
1239 video_set_drvdata(&dev->video_dev, dev);
1241 res = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER, devnum);
1242 if (res < 0) {
1243 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
1244 goto error;
1247 return 0;
1248 error:
1249 v4l2_ctrl_handler_free(hdl);
1250 return res;