blob: 4bbcf480d5e785f30a32db877e6525ac8b8e3dfb [file] [log] [blame]
Janne Grunau9aba42e2009-03-18 18:10:04 -03001/*
Janne Grunaue86da6f2009-03-19 19:00:35 -03002 * Hauppauge HD PVR USB driver - video 4 linux 2 interface
Janne Grunau9aba42e2009-03-18 18:10:04 -03003 *
4 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2.
9 *
10 */
11
12#include <linux/kernel.h>
Mauro Carvalho Chehab54d80902013-03-21 15:46:18 -030013#include <linux/kconfig.h>
Janne Grunau9aba42e2009-03-18 18:10:04 -030014#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/uaccess.h>
19#include <linux/usb.h>
20#include <linux/mutex.h>
Janne Grunau9aba42e2009-03-18 18:10:04 -030021#include <linux/workqueue.h>
22
23#include <linux/videodev2.h>
24#include <media/v4l2-dev.h>
25#include <media/v4l2-common.h>
26#include <media/v4l2-ioctl.h>
27#include "hdpvr.h"
28
Alan Young39bcb3a2010-07-26 08:50:32 -030029#define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
Janne Grunau9aba42e2009-03-18 18:10:04 -030030
Janne Grunau9ef77ad2009-03-27 20:09:40 -030031#define print_buffer_status() { \
32 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
33 "%s:%d buffer stat: %d free, %d proc\n", \
34 __func__, __LINE__, \
35 list_size(&dev->free_buff_list), \
36 list_size(&dev->rec_buff_list)); }
Janne Grunaud211bfc2009-03-26 20:29:39 -030037
Janne Grunau9aba42e2009-03-18 18:10:04 -030038static uint list_size(struct list_head *list)
39{
40 struct list_head *tmp;
41 uint count = 0;
42
43 list_for_each(tmp, list) {
44 count++;
45 }
46
47 return count;
48}
49
50/*=========================================================================*/
51/* urb callback */
52static void hdpvr_read_bulk_callback(struct urb *urb)
53{
54 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
55 struct hdpvr_device *dev = buf->dev;
56
57 /* marking buffer as received and wake waiting */
58 buf->status = BUFSTAT_READY;
59 wake_up_interruptible(&dev->wait_data);
60}
61
62/*=========================================================================*/
63/* bufffer bits */
64
65/* function expects dev->io_mutex to be hold by caller */
66int hdpvr_cancel_queue(struct hdpvr_device *dev)
67{
68 struct hdpvr_buffer *buf;
69
70 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
71 usb_kill_urb(buf->urb);
72 buf->status = BUFSTAT_AVAILABLE;
73 }
74
75 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
76
77 return 0;
78}
79
80static int hdpvr_free_queue(struct list_head *q)
81{
82 struct list_head *tmp;
83 struct list_head *p;
84 struct hdpvr_buffer *buf;
85 struct urb *urb;
86
87 for (p = q->next; p != q;) {
88 buf = list_entry(p, struct hdpvr_buffer, buff_list);
89
90 urb = buf->urb;
Daniel Mack997ea582010-04-12 13:17:25 +020091 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
92 urb->transfer_buffer, urb->transfer_dma);
Janne Grunau9aba42e2009-03-18 18:10:04 -030093 usb_free_urb(urb);
94 tmp = p->next;
95 list_del(p);
96 kfree(buf);
97 p = tmp;
98 }
99
100 return 0;
101}
102
103/* function expects dev->io_mutex to be hold by caller */
104int hdpvr_free_buffers(struct hdpvr_device *dev)
105{
106 hdpvr_cancel_queue(dev);
107
108 hdpvr_free_queue(&dev->free_buff_list);
109 hdpvr_free_queue(&dev->rec_buff_list);
110
111 return 0;
112}
113
114/* function expects dev->io_mutex to be hold by caller */
115int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
116{
117 uint i;
118 int retval = -ENOMEM;
119 u8 *mem;
120 struct hdpvr_buffer *buf;
121 struct urb *urb;
122
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300123 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300124 "allocating %u buffers\n", count);
125
126 for (i = 0; i < count; i++) {
127
128 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
129 if (!buf) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300130 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300131 goto exit;
132 }
133 buf->dev = dev;
134
135 urb = usb_alloc_urb(0, GFP_KERNEL);
136 if (!urb) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300137 v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
Julia Lawallcd0e2802009-11-21 08:49:41 -0300138 goto exit_urb;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300139 }
140 buf->urb = urb;
141
Daniel Mack997ea582010-04-12 13:17:25 +0200142 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
143 &urb->transfer_dma);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300144 if (!mem) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300145 v4l2_err(&dev->v4l2_dev,
146 "cannot allocate usb transfer buffer\n");
Julia Lawallcd0e2802009-11-21 08:49:41 -0300147 goto exit_urb_buffer;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300148 }
149
150 usb_fill_bulk_urb(buf->urb, dev->udev,
151 usb_rcvbulkpipe(dev->udev,
152 dev->bulk_in_endpointAddr),
153 mem, dev->bulk_in_size,
154 hdpvr_read_bulk_callback, buf);
155
James M McLaren4f5c9332010-10-03 19:09:18 -0300156 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300157 buf->status = BUFSTAT_AVAILABLE;
158 list_add_tail(&buf->buff_list, &dev->free_buff_list);
159 }
160 return 0;
Julia Lawallcd0e2802009-11-21 08:49:41 -0300161exit_urb_buffer:
162 usb_free_urb(urb);
163exit_urb:
164 kfree(buf);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300165exit:
166 hdpvr_free_buffers(dev);
167 return retval;
168}
169
170static int hdpvr_submit_buffers(struct hdpvr_device *dev)
171{
172 struct hdpvr_buffer *buf;
173 struct urb *urb;
174 int ret = 0, err_count = 0;
175
176 mutex_lock(&dev->io_mutex);
177
178 while (dev->status == STATUS_STREAMING &&
179 !list_empty(&dev->free_buff_list)) {
180
181 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
182 buff_list);
183 if (buf->status != BUFSTAT_AVAILABLE) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300184 v4l2_err(&dev->v4l2_dev,
Thadeu Lima de Souza Cascardo4b512d22009-04-14 23:14:10 -0300185 "buffer not marked as available\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300186 ret = -EFAULT;
187 goto err;
188 }
189
190 urb = buf->urb;
191 urb->status = 0;
192 urb->actual_length = 0;
193 ret = usb_submit_urb(urb, GFP_KERNEL);
194 if (ret) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300195 v4l2_err(&dev->v4l2_dev,
196 "usb_submit_urb in %s returned %d\n",
197 __func__, ret);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300198 if (++err_count > 2)
199 break;
200 continue;
201 }
202 buf->status = BUFSTAT_INPROGRESS;
203 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
204 }
205err:
Janne Grunaud211bfc2009-03-26 20:29:39 -0300206 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300207 mutex_unlock(&dev->io_mutex);
208 return ret;
209}
210
211static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
212{
213 struct hdpvr_buffer *buf;
214
215 mutex_lock(&dev->io_mutex);
216
217 if (list_empty(&dev->rec_buff_list)) {
218 mutex_unlock(&dev->io_mutex);
219 return NULL;
220 }
221
222 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
223 buff_list);
224 mutex_unlock(&dev->io_mutex);
225
226 return buf;
227}
228
229static void hdpvr_transmit_buffers(struct work_struct *work)
230{
231 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
232 worker);
233
234 while (dev->status == STATUS_STREAMING) {
235
236 if (hdpvr_submit_buffers(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300237 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300238 goto error;
239 }
240 if (wait_event_interruptible(dev->wait_buffer,
241 !list_empty(&dev->free_buff_list) ||
242 dev->status != STATUS_STREAMING))
243 goto error;
244 }
245
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300246 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300247 "transmit worker exited\n");
248 return;
249error:
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300250 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300251 "transmit buffers errored\n");
252 dev->status = STATUS_ERROR;
253}
254
255/* function expects dev->io_mutex to be hold by caller */
256static int hdpvr_start_streaming(struct hdpvr_device *dev)
257{
258 int ret;
259 struct hdpvr_video_info *vidinf;
260
261 if (dev->status == STATUS_STREAMING)
262 return 0;
263 else if (dev->status != STATUS_IDLE)
264 return -EAGAIN;
265
266 vidinf = get_video_info(dev);
267
268 if (vidinf) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300269 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300270 "video signal: %dx%d@%dhz\n", vidinf->width,
271 vidinf->height, vidinf->fps);
272 kfree(vidinf);
273
274 /* start streaming 2 request */
275 ret = usb_control_msg(dev->udev,
276 usb_sndctrlpipe(dev->udev, 0),
277 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300278 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300279 "encoder start control request returned %d\n", ret);
280
281 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
282
Janne Grunauafa15952012-02-02 13:35:21 -0300283 dev->status = STATUS_STREAMING;
284
Janne Grunau9aba42e2009-03-18 18:10:04 -0300285 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
286 queue_work(dev->workqueue, &dev->worker);
287
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300288 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300289 "streaming started\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300290
291 return 0;
292 }
293 msleep(250);
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300294 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300295 "no video signal at input %d\n", dev->options.video_input);
296 return -EAGAIN;
297}
298
299
300/* function expects dev->io_mutex to be hold by caller */
301static int hdpvr_stop_streaming(struct hdpvr_device *dev)
302{
Márton Németh85d682b2010-01-23 14:44:34 +0100303 int actual_length;
304 uint c = 0;
Janne Grunau7d771ff2009-03-27 20:21:17 -0300305 u8 *buf;
306
Janne Grunau9aba42e2009-03-18 18:10:04 -0300307 if (dev->status == STATUS_IDLE)
308 return 0;
309 else if (dev->status != STATUS_STREAMING)
310 return -EAGAIN;
311
Janne Grunau7d771ff2009-03-27 20:21:17 -0300312 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
313 if (!buf)
314 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
315 "for emptying the internal device buffer. "
316 "Next capture start will be slow\n");
317
Janne Grunau9aba42e2009-03-18 18:10:04 -0300318 dev->status = STATUS_SHUTTING_DOWN;
319 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
Janne Grunau48f98f72009-03-26 20:56:06 -0300320 mutex_unlock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300321
322 wake_up_interruptible(&dev->wait_buffer);
323 msleep(50);
324
325 flush_workqueue(dev->workqueue);
326
Janne Grunau48f98f72009-03-26 20:56:06 -0300327 mutex_lock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300328 /* kill the still outstanding urbs */
329 hdpvr_cancel_queue(dev);
330
Janne Grunau7d771ff2009-03-27 20:21:17 -0300331 /* emptying the device buffer beforeshutting it down */
332 while (buf && ++c < 500 &&
333 !usb_bulk_msg(dev->udev,
334 usb_rcvbulkpipe(dev->udev,
335 dev->bulk_in_endpointAddr),
336 buf, dev->bulk_in_size, &actual_length,
337 BULK_URB_TIMEOUT)) {
Janne Grunau7d771ff2009-03-27 20:21:17 -0300338 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
339 "%2d: got %d bytes\n", c, actual_length);
340 }
341 kfree(buf);
342 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
343 "used %d urbs to empty device buffers\n", c-1);
344 msleep(10);
345
Janne Grunau9aba42e2009-03-18 18:10:04 -0300346 dev->status = STATUS_IDLE;
347
348 return 0;
349}
350
351
352/*=======================================================================*/
353/*
354 * video 4 linux 2 file operations
355 */
356
Janne Grunau9aba42e2009-03-18 18:10:04 -0300357static int hdpvr_release(struct file *file)
358{
Hans Verkuilede197a2013-04-06 06:00:17 -0300359 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300360
361 if (!dev)
362 return -ENODEV;
363
364 mutex_lock(&dev->io_mutex);
Hans Verkuilede197a2013-04-06 06:00:17 -0300365 if (file->private_data == dev->owner) {
Janne Grunau9aba42e2009-03-18 18:10:04 -0300366 hdpvr_stop_streaming(dev);
Hans Verkuilede197a2013-04-06 06:00:17 -0300367 dev->owner = NULL;
368 }
Janne Grunau9aba42e2009-03-18 18:10:04 -0300369 mutex_unlock(&dev->io_mutex);
370
Hans Verkuilede197a2013-04-06 06:00:17 -0300371 return v4l2_fh_release(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300372}
373
374/*
375 * hdpvr_v4l2_read()
376 * will allocate buffers when called for the first time
377 */
378static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
379 loff_t *pos)
380{
Hans Verkuilede197a2013-04-06 06:00:17 -0300381 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300382 struct hdpvr_buffer *buf = NULL;
383 struct urb *urb;
384 unsigned int ret = 0;
385 int rem, cnt;
386
387 if (*pos)
388 return -ESPIPE;
389
390 if (!dev)
391 return -ENODEV;
392
393 mutex_lock(&dev->io_mutex);
394 if (dev->status == STATUS_IDLE) {
395 if (hdpvr_start_streaming(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300396 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
397 "start_streaming failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300398 ret = -EIO;
399 msleep(200);
400 dev->status = STATUS_IDLE;
401 mutex_unlock(&dev->io_mutex);
402 goto err;
403 }
Hans Verkuilede197a2013-04-06 06:00:17 -0300404 dev->owner = file->private_data;
Janne Grunaud211bfc2009-03-26 20:29:39 -0300405 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300406 }
407 mutex_unlock(&dev->io_mutex);
408
409 /* wait for the first buffer */
410 if (!(file->f_flags & O_NONBLOCK)) {
411 if (wait_event_interruptible(dev->wait_data,
412 hdpvr_get_next_buffer(dev)))
413 return -ERESTARTSYS;
414 }
415
416 buf = hdpvr_get_next_buffer(dev);
417
418 while (count > 0 && buf) {
419
420 if (buf->status != BUFSTAT_READY &&
421 dev->status != STATUS_DISCONNECTED) {
422 /* return nonblocking */
423 if (file->f_flags & O_NONBLOCK) {
424 if (!ret)
425 ret = -EAGAIN;
426 goto err;
427 }
428
429 if (wait_event_interruptible(dev->wait_data,
430 buf->status == BUFSTAT_READY)) {
431 ret = -ERESTARTSYS;
432 goto err;
433 }
434 }
435
436 if (buf->status != BUFSTAT_READY)
437 break;
438
439 /* set remaining bytes to copy */
440 urb = buf->urb;
441 rem = urb->actual_length - buf->pos;
442 cnt = rem > count ? count : rem;
443
444 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
445 cnt)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300446 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300447 if (!ret)
448 ret = -EFAULT;
449 goto err;
450 }
451
452 buf->pos += cnt;
453 count -= cnt;
454 buffer += cnt;
455 ret += cnt;
456
457 /* finished, take next buffer */
458 if (buf->pos == urb->actual_length) {
459 mutex_lock(&dev->io_mutex);
460 buf->pos = 0;
461 buf->status = BUFSTAT_AVAILABLE;
462
463 list_move_tail(&buf->buff_list, &dev->free_buff_list);
464
Janne Grunaud211bfc2009-03-26 20:29:39 -0300465 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300466
467 mutex_unlock(&dev->io_mutex);
468
469 wake_up_interruptible(&dev->wait_buffer);
470
471 buf = hdpvr_get_next_buffer(dev);
472 }
473 }
474err:
475 if (!ret && !buf)
476 ret = -EAGAIN;
477 return ret;
478}
479
480static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
481{
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300482 struct hdpvr_buffer *buf = NULL;
Hans Verkuilede197a2013-04-06 06:00:17 -0300483 struct hdpvr_device *dev = video_drvdata(filp);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300484 unsigned int mask = 0;
485
486 mutex_lock(&dev->io_mutex);
487
Laurent Pinchart957b4aa2009-11-27 13:57:22 -0300488 if (!video_is_registered(dev->video_dev)) {
Jiri Slaby00c1e212009-07-30 20:00:44 -0300489 mutex_unlock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300490 return -EIO;
Jiri Slaby00c1e212009-07-30 20:00:44 -0300491 }
Janne Grunau9aba42e2009-03-18 18:10:04 -0300492
493 if (dev->status == STATUS_IDLE) {
494 if (hdpvr_start_streaming(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300495 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
496 "start_streaming failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300497 dev->status = STATUS_IDLE;
Hans Verkuilede197a2013-04-06 06:00:17 -0300498 } else {
499 dev->owner = filp->private_data;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300500 }
501
Janne Grunaud211bfc2009-03-26 20:29:39 -0300502 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300503 }
504 mutex_unlock(&dev->io_mutex);
505
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300506 buf = hdpvr_get_next_buffer(dev);
507 /* only wait if no data is available */
508 if (!buf || buf->status != BUFSTAT_READY) {
509 poll_wait(filp, &dev->wait_data, wait);
510 buf = hdpvr_get_next_buffer(dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300511 }
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300512 if (buf && buf->status == BUFSTAT_READY)
513 mask |= POLLIN | POLLRDNORM;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300514
515 return mask;
516}
517
518
Janne Grunau9aba42e2009-03-18 18:10:04 -0300519static const struct v4l2_file_operations hdpvr_fops = {
520 .owner = THIS_MODULE,
Hans Verkuilede197a2013-04-06 06:00:17 -0300521 .open = v4l2_fh_open,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300522 .release = hdpvr_release,
523 .read = hdpvr_read,
524 .poll = hdpvr_poll,
Janne Grunau76717b82009-03-18 20:57:04 -0300525 .unlocked_ioctl = video_ioctl2,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300526};
527
528/*=======================================================================*/
529/*
530 * V4L2 ioctl handling
531 */
532
533static int vidioc_querycap(struct file *file, void *priv,
534 struct v4l2_capability *cap)
535{
536 struct hdpvr_device *dev = video_drvdata(file);
537
538 strcpy(cap->driver, "hdpvr");
Lars Hanischfdd70c32010-02-14 08:57:39 -0300539 strcpy(cap->card, "Hauppauge HD PVR");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300540 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Janne Grunau9aba42e2009-03-18 18:10:04 -0300541 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
542 V4L2_CAP_AUDIO |
543 V4L2_CAP_READWRITE;
544 return 0;
545}
546
547static int vidioc_s_std(struct file *file, void *private_data,
Hans Verkuil314527a2013-03-15 06:10:40 -0300548 v4l2_std_id std)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300549{
Hans Verkuilede197a2013-04-06 06:00:17 -0300550 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300551 u8 std_type = 1;
552
Hans Verkuil314527a2013-03-15 06:10:40 -0300553 if (std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
Janne Grunau9aba42e2009-03-18 18:10:04 -0300554 std_type = 0;
555
556 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
557}
558
559static const char *iname[] = {
560 [HDPVR_COMPONENT] = "Component",
561 [HDPVR_SVIDEO] = "S-Video",
562 [HDPVR_COMPOSITE] = "Composite",
563};
564
565static int vidioc_enum_input(struct file *file, void *priv,
566 struct v4l2_input *i)
567{
Hans Verkuilede197a2013-04-06 06:00:17 -0300568 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300569 unsigned int n;
570
571 n = i->index;
572 if (n >= HDPVR_VIDEO_INPUTS)
573 return -EINVAL;
574
575 i->type = V4L2_INPUT_TYPE_CAMERA;
576
577 strncpy(i->name, iname[n], sizeof(i->name) - 1);
578 i->name[sizeof(i->name) - 1] = '\0';
579
580 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
581
582 i->std = dev->video_dev->tvnorms;
583
584 return 0;
585}
586
587static int vidioc_s_input(struct file *file, void *private_data,
588 unsigned int index)
589{
Hans Verkuilede197a2013-04-06 06:00:17 -0300590 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300591 int retval;
592
593 if (index >= HDPVR_VIDEO_INPUTS)
594 return -EINVAL;
595
596 if (dev->status != STATUS_IDLE)
597 return -EAGAIN;
598
599 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
600 if (!retval)
601 dev->options.video_input = index;
602
603 return retval;
604}
605
606static int vidioc_g_input(struct file *file, void *private_data,
607 unsigned int *index)
608{
Hans Verkuilede197a2013-04-06 06:00:17 -0300609 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300610
611 *index = dev->options.video_input;
612 return 0;
613}
614
615
616static const char *audio_iname[] = {
617 [HDPVR_RCA_FRONT] = "RCA front",
618 [HDPVR_RCA_BACK] = "RCA back",
619 [HDPVR_SPDIF] = "SPDIF",
620};
621
622static int vidioc_enumaudio(struct file *file, void *priv,
623 struct v4l2_audio *audio)
624{
625 unsigned int n;
626
627 n = audio->index;
628 if (n >= HDPVR_AUDIO_INPUTS)
629 return -EINVAL;
630
631 audio->capability = V4L2_AUDCAP_STEREO;
632
633 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
634 audio->name[sizeof(audio->name) - 1] = '\0';
635
636 return 0;
637}
638
639static int vidioc_s_audio(struct file *file, void *private_data,
Hans Verkuil0e8025b92012-09-04 11:59:31 -0300640 const struct v4l2_audio *audio)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300641{
Hans Verkuilede197a2013-04-06 06:00:17 -0300642 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300643 int retval;
644
645 if (audio->index >= HDPVR_AUDIO_INPUTS)
646 return -EINVAL;
647
648 if (dev->status != STATUS_IDLE)
649 return -EAGAIN;
650
651 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
652 if (!retval)
653 dev->options.audio_input = audio->index;
654
655 return retval;
656}
657
658static int vidioc_g_audio(struct file *file, void *private_data,
659 struct v4l2_audio *audio)
660{
Hans Verkuilede197a2013-04-06 06:00:17 -0300661 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300662
663 audio->index = dev->options.audio_input;
664 audio->capability = V4L2_AUDCAP_STEREO;
665 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
666 audio->name[sizeof(audio->name) - 1] = '\0';
667 return 0;
668}
669
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300670static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300671{
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300672 struct hdpvr_device *dev =
673 container_of(ctrl->handler, struct hdpvr_device, hdl);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300674
675 switch (ctrl->id) {
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300676 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
677 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
678 dev->video_bitrate->val >= dev->video_bitrate_peak->val)
679 dev->video_bitrate_peak->val =
680 dev->video_bitrate->val + 100000;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300681 break;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300682 }
683 return 0;
684}
685
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300686static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300687{
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300688 struct hdpvr_device *dev =
689 container_of(ctrl->handler, struct hdpvr_device, hdl);
690 struct hdpvr_options *opt = &dev->options;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300691 int ret = -EINVAL;
692
693 switch (ctrl->id) {
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300694 case V4L2_CID_BRIGHTNESS:
695 ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
696 if (ret)
697 break;
698 dev->options.brightness = ctrl->val;
699 return 0;
700 case V4L2_CID_CONTRAST:
701 ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
702 if (ret)
703 break;
704 dev->options.contrast = ctrl->val;
705 return 0;
706 case V4L2_CID_SATURATION:
707 ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
708 if (ret)
709 break;
710 dev->options.saturation = ctrl->val;
711 return 0;
712 case V4L2_CID_HUE:
713 ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
714 if (ret)
715 break;
716 dev->options.hue = ctrl->val;
717 return 0;
718 case V4L2_CID_SHARPNESS:
719 ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
720 if (ret)
721 break;
722 dev->options.sharpness = ctrl->val;
723 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300724 case V4L2_CID_MPEG_AUDIO_ENCODING:
725 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300726 opt->audio_codec = ctrl->val;
727 return hdpvr_set_audio(dev, opt->audio_input,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300728 opt->audio_codec);
729 }
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300730 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300731 case V4L2_CID_MPEG_VIDEO_ENCODING:
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300732 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300733/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
734/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
735/* opt->gop_mode |= 0x2; */
736/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
737/* opt->gop_mode); */
738/* } */
739/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
740/* opt->gop_mode &= ~0x2; */
741/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
742/* opt->gop_mode); */
743/* } */
744/* break; */
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300745 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
746 uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
747 uint bitrate = dev->video_bitrate->val / 100000;
748
749 if (ctrl->is_new) {
750 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
751 opt->bitrate_mode = HDPVR_CONSTANT;
752 else
753 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300754 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
755 opt->bitrate_mode);
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300756 v4l2_ctrl_activate(dev->video_bitrate_peak,
757 ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300758 }
Janne Grunau9aba42e2009-03-18 18:10:04 -0300759
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300760 if (dev->video_bitrate_peak->is_new ||
761 dev->video_bitrate->is_new) {
762 opt->bitrate = bitrate;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300763 opt->peak_bitrate = peak_bitrate;
764 hdpvr_set_bitrate(dev);
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300765 }
766 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300767 }
768 case V4L2_CID_MPEG_STREAM_TYPE:
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300769 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300770 default:
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300771 break;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300772 }
773 return ret;
774}
775
Janne Grunau9aba42e2009-03-18 18:10:04 -0300776static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
777 struct v4l2_fmtdesc *f)
778{
779
780 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
781 return -EINVAL;
782
783 f->flags = V4L2_FMT_FLAG_COMPRESSED;
784 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
785 f->pixelformat = V4L2_PIX_FMT_MPEG;
786
787 return 0;
788}
789
790static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
791 struct v4l2_format *f)
792{
Hans Verkuilede197a2013-04-06 06:00:17 -0300793 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300794 struct hdpvr_video_info *vid_info;
795
796 if (!dev)
797 return -ENODEV;
798
799 vid_info = get_video_info(dev);
800 if (!vid_info)
801 return -EFAULT;
802
803 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
804 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
805 f->fmt.pix.width = vid_info->width;
806 f->fmt.pix.height = vid_info->height;
807 f->fmt.pix.sizeimage = dev->bulk_in_size;
808 f->fmt.pix.colorspace = 0;
809 f->fmt.pix.bytesperline = 0;
810 f->fmt.pix.field = V4L2_FIELD_ANY;
811
812 kfree(vid_info);
813 return 0;
814}
815
Janne Grunau76717b82009-03-18 20:57:04 -0300816static int vidioc_encoder_cmd(struct file *filp, void *priv,
817 struct v4l2_encoder_cmd *a)
818{
Hans Verkuilede197a2013-04-06 06:00:17 -0300819 struct hdpvr_device *dev = video_drvdata(filp);
820 int res = 0;
Janne Grunau76717b82009-03-18 20:57:04 -0300821
822 mutex_lock(&dev->io_mutex);
Hans Verkuilede197a2013-04-06 06:00:17 -0300823 a->flags = 0;
Janne Grunau76717b82009-03-18 20:57:04 -0300824
Janne Grunau76717b82009-03-18 20:57:04 -0300825 switch (a->cmd) {
826 case V4L2_ENC_CMD_START:
Hans Verkuilede197a2013-04-06 06:00:17 -0300827 if (dev->owner && filp->private_data != dev->owner) {
828 res = -EBUSY;
829 break;
830 }
831 if (dev->status == STATUS_STREAMING)
832 break;
Janne Grunau76717b82009-03-18 20:57:04 -0300833 res = hdpvr_start_streaming(dev);
Hans Verkuilede197a2013-04-06 06:00:17 -0300834 if (!res)
835 dev->owner = filp->private_data;
836 else
837 dev->status = STATUS_IDLE;
Janne Grunau76717b82009-03-18 20:57:04 -0300838 break;
839 case V4L2_ENC_CMD_STOP:
Hans Verkuilede197a2013-04-06 06:00:17 -0300840 if (dev->owner && filp->private_data != dev->owner) {
841 res = -EBUSY;
842 break;
843 }
844 if (dev->status == STATUS_IDLE)
845 break;
Janne Grunau76717b82009-03-18 20:57:04 -0300846 res = hdpvr_stop_streaming(dev);
Hans Verkuilede197a2013-04-06 06:00:17 -0300847 if (!res)
848 dev->owner = NULL;
Janne Grunau76717b82009-03-18 20:57:04 -0300849 break;
850 default:
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300851 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau76717b82009-03-18 20:57:04 -0300852 "Unsupported encoder cmd %d\n", a->cmd);
Janne Grunau48f98f72009-03-26 20:56:06 -0300853 res = -EINVAL;
Hans Verkuilede197a2013-04-06 06:00:17 -0300854 break;
Janne Grunau76717b82009-03-18 20:57:04 -0300855 }
Hans Verkuilede197a2013-04-06 06:00:17 -0300856
Janne Grunau76717b82009-03-18 20:57:04 -0300857 mutex_unlock(&dev->io_mutex);
858 return res;
859}
860
861static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
862 struct v4l2_encoder_cmd *a)
863{
Hans Verkuilede197a2013-04-06 06:00:17 -0300864 a->flags = 0;
Janne Grunau76717b82009-03-18 20:57:04 -0300865 switch (a->cmd) {
866 case V4L2_ENC_CMD_START:
867 case V4L2_ENC_CMD_STOP:
868 return 0;
869 default:
870 return -EINVAL;
871 }
872}
Janne Grunau9aba42e2009-03-18 18:10:04 -0300873
874static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
875 .vidioc_querycap = vidioc_querycap,
876 .vidioc_s_std = vidioc_s_std,
877 .vidioc_enum_input = vidioc_enum_input,
878 .vidioc_g_input = vidioc_g_input,
879 .vidioc_s_input = vidioc_s_input,
880 .vidioc_enumaudio = vidioc_enumaudio,
881 .vidioc_g_audio = vidioc_g_audio,
882 .vidioc_s_audio = vidioc_s_audio,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300883 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
884 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
Janne Grunau76717b82009-03-18 20:57:04 -0300885 .vidioc_encoder_cmd = vidioc_encoder_cmd,
886 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300887};
888
889static void hdpvr_device_release(struct video_device *vdev)
890{
891 struct hdpvr_device *dev = video_get_drvdata(vdev);
892
893 hdpvr_delete(dev);
Hans Verkuilf2b305c2010-05-02 08:01:04 -0300894 mutex_lock(&dev->io_mutex);
895 destroy_workqueue(dev->workqueue);
896 mutex_unlock(&dev->io_mutex);
897
898 v4l2_device_unregister(&dev->v4l2_dev);
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300899 v4l2_ctrl_handler_free(&dev->hdl);
Hans Verkuilf2b305c2010-05-02 08:01:04 -0300900
901 /* deregister I2C adapter */
Mauro Carvalho Chehab54d80902013-03-21 15:46:18 -0300902#if IS_ENABLED(CONFIG_I2C)
Hans Verkuilf2b305c2010-05-02 08:01:04 -0300903 mutex_lock(&dev->i2c_mutex);
Jarod Wilson324b04b2011-01-14 16:25:21 -0300904 i2c_del_adapter(&dev->i2c_adapter);
Hans Verkuilf2b305c2010-05-02 08:01:04 -0300905 mutex_unlock(&dev->i2c_mutex);
906#endif /* CONFIG_I2C */
907
908 kfree(dev->usbc_buf);
909 kfree(dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300910}
911
912static const struct video_device hdpvr_video_template = {
Janne Grunau9aba42e2009-03-18 18:10:04 -0300913 .fops = &hdpvr_fops,
914 .release = hdpvr_device_release,
915 .ioctl_ops = &hdpvr_ioctl_ops,
916 .tvnorms =
917 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
918 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
919 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
920 V4L2_STD_PAL_60,
Hans Verkuil99362e12009-08-07 07:10:52 -0300921 .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
922 V4L2_STD_PAL_60,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300923};
924
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300925static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
926 .try_ctrl = hdpvr_try_ctrl,
927 .s_ctrl = hdpvr_s_ctrl,
928};
929
Janne Grunaua50ab2912009-03-26 14:40:55 -0300930int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
931 int devnum)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300932{
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300933 struct v4l2_ctrl_handler *hdl = &dev->hdl;
934 bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
935 int res;
936
937 v4l2_ctrl_handler_init(hdl, 11);
938 if (dev->fw_ver > 0x15) {
939 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
940 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
941 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
942 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
943 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
944 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
945 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
946 V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
947 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
948 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
949 } else {
950 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
951 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
952 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
953 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
954 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
955 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
956 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
957 V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
958 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
959 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
960 }
961
962 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
963 V4L2_CID_MPEG_STREAM_TYPE,
964 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
965 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
966 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
967 V4L2_CID_MPEG_AUDIO_ENCODING,
968 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
969 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC);
970 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
971 V4L2_CID_MPEG_VIDEO_ENCODING,
972 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
973 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
974
975 dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
976 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
977 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
978 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
979
980 dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
981 V4L2_CID_MPEG_VIDEO_BITRATE,
982 1000000, 13500000, 100000, 6500000);
983 dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
984 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
985 1100000, 20200000, 100000, 9000000);
986 dev->v4l2_dev.ctrl_handler = hdl;
987 if (hdl->error) {
988 res = hdl->error;
989 v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
990 goto error;
991 }
992 v4l2_ctrl_cluster(3, &dev->video_mode);
993 res = v4l2_ctrl_handler_setup(hdl);
994 if (res < 0) {
995 v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
996 goto error;
997 }
998
Janne Grunau9aba42e2009-03-18 18:10:04 -0300999 /* setup and register video device */
1000 dev->video_dev = video_device_alloc();
1001 if (!dev->video_dev) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -03001002 v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001003 res = -ENOMEM;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001004 goto error;
1005 }
1006
Hans Verkuilede197a2013-04-06 06:00:17 -03001007 *dev->video_dev = hdpvr_video_template;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001008 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
Hans Verkuilede197a2013-04-06 06:00:17 -03001009 dev->video_dev->v4l2_dev = &dev->v4l2_dev;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001010 video_set_drvdata(dev->video_dev, dev);
1011
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001012 res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum);
1013 if (res < 0) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -03001014 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -03001015 goto error;
1016 }
1017
1018 return 0;
1019error:
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001020 v4l2_ctrl_handler_free(hdl);
1021 return res;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001022}