blob: 6d43d75493ea0ea35570c8716e1de51aaf5d881b [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>
Hans Verkuil3315c592013-03-20 15:29:52 -030024#include <linux/v4l2-dv-timings.h>
Janne Grunau9aba42e2009-03-18 18:10:04 -030025#include <media/v4l2-dev.h>
26#include <media/v4l2-common.h>
Hans Verkuil25764152013-07-29 08:40:56 -030027#include <media/v4l2-dv-timings.h>
Janne Grunau9aba42e2009-03-18 18:10:04 -030028#include <media/v4l2-ioctl.h>
Hans Verkuil65fe42d2013-02-12 08:26:59 -030029#include <media/v4l2-event.h>
Janne Grunau9aba42e2009-03-18 18:10:04 -030030#include "hdpvr.h"
31
Alan Young39bcb3a2010-07-26 08:50:32 -030032#define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
Janne Grunau9aba42e2009-03-18 18:10:04 -030033
Janne Grunau9ef77ad2009-03-27 20:09:40 -030034#define print_buffer_status() { \
35 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
36 "%s:%d buffer stat: %d free, %d proc\n", \
37 __func__, __LINE__, \
38 list_size(&dev->free_buff_list), \
39 list_size(&dev->rec_buff_list)); }
Janne Grunaud211bfc2009-03-26 20:29:39 -030040
Hans Verkuil3315c592013-03-20 15:29:52 -030041static const struct v4l2_dv_timings hdpvr_dv_timings[] = {
42 V4L2_DV_BT_CEA_720X480I59_94,
43 V4L2_DV_BT_CEA_720X576I50,
44 V4L2_DV_BT_CEA_720X480P59_94,
45 V4L2_DV_BT_CEA_720X576P50,
46 V4L2_DV_BT_CEA_1280X720P50,
47 V4L2_DV_BT_CEA_1280X720P60,
48 V4L2_DV_BT_CEA_1920X1080I50,
49 V4L2_DV_BT_CEA_1920X1080I60,
50};
51
52/* Use 480i59 as the default timings */
53#define HDPVR_DEF_DV_TIMINGS_IDX (0)
54
55struct hdpvr_fh {
56 struct v4l2_fh fh;
57 bool legacy_mode;
58};
59
Janne Grunau9aba42e2009-03-18 18:10:04 -030060static uint list_size(struct list_head *list)
61{
62 struct list_head *tmp;
63 uint count = 0;
64
65 list_for_each(tmp, list) {
66 count++;
67 }
68
69 return count;
70}
71
72/*=========================================================================*/
73/* urb callback */
74static void hdpvr_read_bulk_callback(struct urb *urb)
75{
76 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
77 struct hdpvr_device *dev = buf->dev;
78
79 /* marking buffer as received and wake waiting */
80 buf->status = BUFSTAT_READY;
81 wake_up_interruptible(&dev->wait_data);
82}
83
84/*=========================================================================*/
Hans Verkuil34458572014-06-16 09:08:29 -030085/* buffer bits */
Janne Grunau9aba42e2009-03-18 18:10:04 -030086
87/* function expects dev->io_mutex to be hold by caller */
88int hdpvr_cancel_queue(struct hdpvr_device *dev)
89{
90 struct hdpvr_buffer *buf;
91
92 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
93 usb_kill_urb(buf->urb);
94 buf->status = BUFSTAT_AVAILABLE;
95 }
96
97 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
98
99 return 0;
100}
101
102static int hdpvr_free_queue(struct list_head *q)
103{
104 struct list_head *tmp;
105 struct list_head *p;
106 struct hdpvr_buffer *buf;
107 struct urb *urb;
108
109 for (p = q->next; p != q;) {
110 buf = list_entry(p, struct hdpvr_buffer, buff_list);
111
112 urb = buf->urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200113 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
114 urb->transfer_buffer, urb->transfer_dma);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300115 usb_free_urb(urb);
116 tmp = p->next;
117 list_del(p);
118 kfree(buf);
119 p = tmp;
120 }
121
122 return 0;
123}
124
125/* function expects dev->io_mutex to be hold by caller */
126int hdpvr_free_buffers(struct hdpvr_device *dev)
127{
128 hdpvr_cancel_queue(dev);
129
130 hdpvr_free_queue(&dev->free_buff_list);
131 hdpvr_free_queue(&dev->rec_buff_list);
132
133 return 0;
134}
135
136/* function expects dev->io_mutex to be hold by caller */
137int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
138{
139 uint i;
140 int retval = -ENOMEM;
141 u8 *mem;
142 struct hdpvr_buffer *buf;
143 struct urb *urb;
144
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300145 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300146 "allocating %u buffers\n", count);
147
148 for (i = 0; i < count; i++) {
149
150 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
151 if (!buf) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300152 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300153 goto exit;
154 }
155 buf->dev = dev;
156
157 urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang90831662016-08-11 23:03:55 +0200158 if (!urb)
Julia Lawallcd0e2802009-11-21 08:49:41 -0300159 goto exit_urb;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300160 buf->urb = urb;
161
Daniel Mack997ea582010-04-12 13:17:25 +0200162 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
163 &urb->transfer_dma);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300164 if (!mem) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300165 v4l2_err(&dev->v4l2_dev,
166 "cannot allocate usb transfer buffer\n");
Julia Lawallcd0e2802009-11-21 08:49:41 -0300167 goto exit_urb_buffer;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300168 }
169
170 usb_fill_bulk_urb(buf->urb, dev->udev,
171 usb_rcvbulkpipe(dev->udev,
172 dev->bulk_in_endpointAddr),
173 mem, dev->bulk_in_size,
174 hdpvr_read_bulk_callback, buf);
175
James M McLaren4f5c9332010-10-03 19:09:18 -0300176 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300177 buf->status = BUFSTAT_AVAILABLE;
178 list_add_tail(&buf->buff_list, &dev->free_buff_list);
179 }
180 return 0;
Julia Lawallcd0e2802009-11-21 08:49:41 -0300181exit_urb_buffer:
182 usb_free_urb(urb);
183exit_urb:
184 kfree(buf);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300185exit:
186 hdpvr_free_buffers(dev);
187 return retval;
188}
189
190static int hdpvr_submit_buffers(struct hdpvr_device *dev)
191{
192 struct hdpvr_buffer *buf;
193 struct urb *urb;
194 int ret = 0, err_count = 0;
195
196 mutex_lock(&dev->io_mutex);
197
198 while (dev->status == STATUS_STREAMING &&
199 !list_empty(&dev->free_buff_list)) {
200
201 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
202 buff_list);
203 if (buf->status != BUFSTAT_AVAILABLE) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300204 v4l2_err(&dev->v4l2_dev,
Thadeu Lima de Souza Cascardo4b512d22009-04-14 23:14:10 -0300205 "buffer not marked as available\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300206 ret = -EFAULT;
207 goto err;
208 }
209
210 urb = buf->urb;
211 urb->status = 0;
212 urb->actual_length = 0;
213 ret = usb_submit_urb(urb, GFP_KERNEL);
214 if (ret) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300215 v4l2_err(&dev->v4l2_dev,
216 "usb_submit_urb in %s returned %d\n",
217 __func__, ret);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300218 if (++err_count > 2)
219 break;
220 continue;
221 }
222 buf->status = BUFSTAT_INPROGRESS;
223 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
224 }
225err:
Janne Grunaud211bfc2009-03-26 20:29:39 -0300226 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300227 mutex_unlock(&dev->io_mutex);
228 return ret;
229}
230
231static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
232{
233 struct hdpvr_buffer *buf;
234
235 mutex_lock(&dev->io_mutex);
236
237 if (list_empty(&dev->rec_buff_list)) {
238 mutex_unlock(&dev->io_mutex);
239 return NULL;
240 }
241
242 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
243 buff_list);
244 mutex_unlock(&dev->io_mutex);
245
246 return buf;
247}
248
249static void hdpvr_transmit_buffers(struct work_struct *work)
250{
251 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
252 worker);
253
254 while (dev->status == STATUS_STREAMING) {
255
256 if (hdpvr_submit_buffers(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300257 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300258 goto error;
259 }
260 if (wait_event_interruptible(dev->wait_buffer,
261 !list_empty(&dev->free_buff_list) ||
262 dev->status != STATUS_STREAMING))
263 goto error;
264 }
265
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300266 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300267 "transmit worker exited\n");
268 return;
269error:
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300270 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300271 "transmit buffers errored\n");
272 dev->status = STATUS_ERROR;
273}
274
275/* function expects dev->io_mutex to be hold by caller */
276static int hdpvr_start_streaming(struct hdpvr_device *dev)
277{
278 int ret;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300279 struct hdpvr_video_info vidinf;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300280
281 if (dev->status == STATUS_STREAMING)
282 return 0;
Hans Verkuil79f10b62013-05-29 03:55:14 -0300283 if (dev->status != STATUS_IDLE)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300284 return -EAGAIN;
285
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300286 ret = get_video_info(dev, &vidinf);
Hans Verkuil5f454d82013-05-29 03:55:15 -0300287 if (ret < 0)
288 return ret;
289
290 if (!vidinf.valid) {
Hans Verkuil79f10b62013-05-29 03:55:14 -0300291 msleep(250);
292 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
293 "no video signal at input %d\n", dev->options.video_input);
294 return -EAGAIN;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300295 }
Hans Verkuil79f10b62013-05-29 03:55:14 -0300296
297 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
298 "video signal: %dx%d@%dhz\n", vidinf.width,
299 vidinf.height, vidinf.fps);
300
301 /* start streaming 2 request */
302 ret = usb_control_msg(dev->udev,
303 usb_sndctrlpipe(dev->udev, 0),
304 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
305 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
306 "encoder start control request returned %d\n", ret);
307 if (ret < 0)
308 return ret;
309
310 ret = hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
311 if (ret)
312 return ret;
313
314 dev->status = STATUS_STREAMING;
315
316 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
Bhaktipriya Shridhar5612e192016-07-02 07:47:31 -0300317 schedule_work(&dev->worker);
Hans Verkuil79f10b62013-05-29 03:55:14 -0300318
319 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
320 "streaming started\n");
321
322 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300323}
324
325
326/* function expects dev->io_mutex to be hold by caller */
327static int hdpvr_stop_streaming(struct hdpvr_device *dev)
328{
Márton Németh85d682b2010-01-23 14:44:34 +0100329 int actual_length;
330 uint c = 0;
Janne Grunau7d771ff2009-03-27 20:21:17 -0300331 u8 *buf;
332
Janne Grunau9aba42e2009-03-18 18:10:04 -0300333 if (dev->status == STATUS_IDLE)
334 return 0;
335 else if (dev->status != STATUS_STREAMING)
336 return -EAGAIN;
337
Janne Grunau7d771ff2009-03-27 20:21:17 -0300338 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
339 if (!buf)
340 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
341 "for emptying the internal device buffer. "
342 "Next capture start will be slow\n");
343
Janne Grunau9aba42e2009-03-18 18:10:04 -0300344 dev->status = STATUS_SHUTTING_DOWN;
345 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
Janne Grunau48f98f72009-03-26 20:56:06 -0300346 mutex_unlock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300347
348 wake_up_interruptible(&dev->wait_buffer);
349 msleep(50);
350
Bhaktipriya Shridhar5612e192016-07-02 07:47:31 -0300351 flush_work(&dev->worker);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300352
Janne Grunau48f98f72009-03-26 20:56:06 -0300353 mutex_lock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300354 /* kill the still outstanding urbs */
355 hdpvr_cancel_queue(dev);
356
Janne Grunau7d771ff2009-03-27 20:21:17 -0300357 /* emptying the device buffer beforeshutting it down */
358 while (buf && ++c < 500 &&
359 !usb_bulk_msg(dev->udev,
360 usb_rcvbulkpipe(dev->udev,
361 dev->bulk_in_endpointAddr),
362 buf, dev->bulk_in_size, &actual_length,
363 BULK_URB_TIMEOUT)) {
Janne Grunau7d771ff2009-03-27 20:21:17 -0300364 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
365 "%2d: got %d bytes\n", c, actual_length);
366 }
367 kfree(buf);
368 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
369 "used %d urbs to empty device buffers\n", c-1);
370 msleep(10);
371
Janne Grunau9aba42e2009-03-18 18:10:04 -0300372 dev->status = STATUS_IDLE;
373
374 return 0;
375}
376
377
378/*=======================================================================*/
379/*
380 * video 4 linux 2 file operations
381 */
382
Hans Verkuil3315c592013-03-20 15:29:52 -0300383static int hdpvr_open(struct file *file)
384{
385 struct hdpvr_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
386
387 if (fh == NULL)
388 return -ENOMEM;
389 fh->legacy_mode = true;
390 v4l2_fh_init(&fh->fh, video_devdata(file));
391 v4l2_fh_add(&fh->fh);
392 file->private_data = fh;
393 return 0;
394}
395
Janne Grunau9aba42e2009-03-18 18:10:04 -0300396static int hdpvr_release(struct file *file)
397{
Hans Verkuilede197a2013-04-06 06:00:17 -0300398 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300399
Janne Grunau9aba42e2009-03-18 18:10:04 -0300400 mutex_lock(&dev->io_mutex);
Hans Verkuilede197a2013-04-06 06:00:17 -0300401 if (file->private_data == dev->owner) {
Janne Grunau9aba42e2009-03-18 18:10:04 -0300402 hdpvr_stop_streaming(dev);
Hans Verkuilede197a2013-04-06 06:00:17 -0300403 dev->owner = NULL;
404 }
Janne Grunau9aba42e2009-03-18 18:10:04 -0300405 mutex_unlock(&dev->io_mutex);
406
Hans Verkuilede197a2013-04-06 06:00:17 -0300407 return v4l2_fh_release(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300408}
409
410/*
411 * hdpvr_v4l2_read()
412 * will allocate buffers when called for the first time
413 */
414static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
415 loff_t *pos)
416{
Hans Verkuilede197a2013-04-06 06:00:17 -0300417 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300418 struct hdpvr_buffer *buf = NULL;
419 struct urb *urb;
420 unsigned int ret = 0;
421 int rem, cnt;
422
423 if (*pos)
424 return -ESPIPE;
425
Janne Grunau9aba42e2009-03-18 18:10:04 -0300426 mutex_lock(&dev->io_mutex);
427 if (dev->status == STATUS_IDLE) {
428 if (hdpvr_start_streaming(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300429 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
430 "start_streaming failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300431 ret = -EIO;
432 msleep(200);
433 dev->status = STATUS_IDLE;
434 mutex_unlock(&dev->io_mutex);
435 goto err;
436 }
Hans Verkuilede197a2013-04-06 06:00:17 -0300437 dev->owner = file->private_data;
Janne Grunaud211bfc2009-03-26 20:29:39 -0300438 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300439 }
440 mutex_unlock(&dev->io_mutex);
441
442 /* wait for the first buffer */
443 if (!(file->f_flags & O_NONBLOCK)) {
444 if (wait_event_interruptible(dev->wait_data,
445 hdpvr_get_next_buffer(dev)))
446 return -ERESTARTSYS;
447 }
448
449 buf = hdpvr_get_next_buffer(dev);
450
451 while (count > 0 && buf) {
452
453 if (buf->status != BUFSTAT_READY &&
454 dev->status != STATUS_DISCONNECTED) {
455 /* return nonblocking */
456 if (file->f_flags & O_NONBLOCK) {
457 if (!ret)
458 ret = -EAGAIN;
459 goto err;
460 }
461
462 if (wait_event_interruptible(dev->wait_data,
Markus Elfringb68554c2015-12-29 08:02:43 -0200463 buf->status == BUFSTAT_READY))
464 return -ERESTARTSYS;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300465 }
466
467 if (buf->status != BUFSTAT_READY)
468 break;
469
470 /* set remaining bytes to copy */
471 urb = buf->urb;
472 rem = urb->actual_length - buf->pos;
473 cnt = rem > count ? count : rem;
474
475 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
476 cnt)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300477 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300478 if (!ret)
479 ret = -EFAULT;
480 goto err;
481 }
482
483 buf->pos += cnt;
484 count -= cnt;
485 buffer += cnt;
486 ret += cnt;
487
488 /* finished, take next buffer */
489 if (buf->pos == urb->actual_length) {
490 mutex_lock(&dev->io_mutex);
491 buf->pos = 0;
492 buf->status = BUFSTAT_AVAILABLE;
493
494 list_move_tail(&buf->buff_list, &dev->free_buff_list);
495
Janne Grunaud211bfc2009-03-26 20:29:39 -0300496 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300497
498 mutex_unlock(&dev->io_mutex);
499
500 wake_up_interruptible(&dev->wait_buffer);
501
502 buf = hdpvr_get_next_buffer(dev);
503 }
504 }
505err:
506 if (!ret && !buf)
507 ret = -EAGAIN;
508 return ret;
509}
510
511static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
512{
Hans Verkuil65fe42d2013-02-12 08:26:59 -0300513 unsigned long req_events = poll_requested_events(wait);
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300514 struct hdpvr_buffer *buf = NULL;
Hans Verkuilede197a2013-04-06 06:00:17 -0300515 struct hdpvr_device *dev = video_drvdata(filp);
Hans Verkuil65fe42d2013-02-12 08:26:59 -0300516 unsigned int mask = v4l2_ctrl_poll(filp, wait);
517
518 if (!(req_events & (POLLIN | POLLRDNORM)))
519 return mask;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300520
521 mutex_lock(&dev->io_mutex);
522
Janne Grunau9aba42e2009-03-18 18:10:04 -0300523 if (dev->status == STATUS_IDLE) {
524 if (hdpvr_start_streaming(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300525 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
526 "start_streaming failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300527 dev->status = STATUS_IDLE;
Hans Verkuilede197a2013-04-06 06:00:17 -0300528 } else {
529 dev->owner = filp->private_data;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300530 }
531
Janne Grunaud211bfc2009-03-26 20:29:39 -0300532 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300533 }
534 mutex_unlock(&dev->io_mutex);
535
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300536 buf = hdpvr_get_next_buffer(dev);
537 /* only wait if no data is available */
538 if (!buf || buf->status != BUFSTAT_READY) {
539 poll_wait(filp, &dev->wait_data, wait);
540 buf = hdpvr_get_next_buffer(dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300541 }
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300542 if (buf && buf->status == BUFSTAT_READY)
543 mask |= POLLIN | POLLRDNORM;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300544
545 return mask;
546}
547
548
Janne Grunau9aba42e2009-03-18 18:10:04 -0300549static const struct v4l2_file_operations hdpvr_fops = {
550 .owner = THIS_MODULE,
Hans Verkuil3315c592013-03-20 15:29:52 -0300551 .open = hdpvr_open,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300552 .release = hdpvr_release,
553 .read = hdpvr_read,
554 .poll = hdpvr_poll,
Janne Grunau76717b82009-03-18 20:57:04 -0300555 .unlocked_ioctl = video_ioctl2,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300556};
557
558/*=======================================================================*/
559/*
560 * V4L2 ioctl handling
561 */
562
563static int vidioc_querycap(struct file *file, void *priv,
564 struct v4l2_capability *cap)
565{
566 struct hdpvr_device *dev = video_drvdata(file);
567
568 strcpy(cap->driver, "hdpvr");
Lars Hanischfdd70c32010-02-14 08:57:39 -0300569 strcpy(cap->card, "Hauppauge HD PVR");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300570 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Hans Verkuil41022fc2013-02-12 09:21:36 -0300571 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
572 V4L2_CAP_READWRITE;
573 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300574 return 0;
575}
576
Hans Verkuil5854bf82013-04-10 14:26:52 -0300577static int vidioc_s_std(struct file *file, void *_fh,
Hans Verkuil314527a2013-03-15 06:10:40 -0300578 v4l2_std_id std)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300579{
Hans Verkuilede197a2013-04-06 06:00:17 -0300580 struct hdpvr_device *dev = video_drvdata(file);
Hans Verkuil5854bf82013-04-10 14:26:52 -0300581 struct hdpvr_fh *fh = _fh;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300582 u8 std_type = 1;
583
Hans Verkuil5854bf82013-04-10 14:26:52 -0300584 if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
Hans Verkuil8f69da92013-03-19 09:53:29 -0300585 return -ENODATA;
586 if (dev->status != STATUS_IDLE)
587 return -EBUSY;
588 if (std & V4L2_STD_525_60)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300589 std_type = 0;
Hans Verkuil8f69da92013-03-19 09:53:29 -0300590 dev->cur_std = std;
591 dev->width = 720;
592 dev->height = std_type ? 576 : 480;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300593
594 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
595}
596
Hans Verkuil5854bf82013-04-10 14:26:52 -0300597static int vidioc_g_std(struct file *file, void *_fh,
Hans Verkuil8f69da92013-03-19 09:53:29 -0300598 v4l2_std_id *std)
599{
600 struct hdpvr_device *dev = video_drvdata(file);
Hans Verkuil5854bf82013-04-10 14:26:52 -0300601 struct hdpvr_fh *fh = _fh;
Hans Verkuil8f69da92013-03-19 09:53:29 -0300602
Hans Verkuil5854bf82013-04-10 14:26:52 -0300603 if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
Hans Verkuil8f69da92013-03-19 09:53:29 -0300604 return -ENODATA;
605 *std = dev->cur_std;
606 return 0;
607}
608
Hans Verkuil5854bf82013-04-10 14:26:52 -0300609static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a)
Hans Verkuil8f69da92013-03-19 09:53:29 -0300610{
611 struct hdpvr_device *dev = video_drvdata(file);
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300612 struct hdpvr_video_info vid_info;
Hans Verkuil5854bf82013-04-10 14:26:52 -0300613 struct hdpvr_fh *fh = _fh;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300614 int ret;
Hans Verkuil8f69da92013-03-19 09:53:29 -0300615
Hans Verkuilab6e1342013-05-29 03:55:13 -0300616 *a = V4L2_STD_UNKNOWN;
Hans Verkuil5854bf82013-04-10 14:26:52 -0300617 if (dev->options.video_input == HDPVR_COMPONENT)
618 return fh->legacy_mode ? 0 : -ENODATA;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300619 ret = get_video_info(dev, &vid_info);
Hans Verkuil5f454d82013-05-29 03:55:15 -0300620 if (vid_info.valid && vid_info.width == 720 &&
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300621 (vid_info.height == 480 || vid_info.height == 576)) {
622 *a = (vid_info.height == 480) ?
Hans Verkuil8f69da92013-03-19 09:53:29 -0300623 V4L2_STD_525_60 : V4L2_STD_625_50;
624 }
Hans Verkuil5f454d82013-05-29 03:55:15 -0300625 return ret;
Hans Verkuil8f69da92013-03-19 09:53:29 -0300626}
627
Hans Verkuil3315c592013-03-20 15:29:52 -0300628static int vidioc_s_dv_timings(struct file *file, void *_fh,
629 struct v4l2_dv_timings *timings)
630{
631 struct hdpvr_device *dev = video_drvdata(file);
632 struct hdpvr_fh *fh = _fh;
633 int i;
634
635 fh->legacy_mode = false;
636 if (dev->options.video_input)
637 return -ENODATA;
638 if (dev->status != STATUS_IDLE)
639 return -EBUSY;
640 for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++)
Hans Verkuil85f9e062015-11-13 09:46:26 -0200641 if (v4l2_match_dv_timings(timings, hdpvr_dv_timings + i, 0, false))
Hans Verkuil3315c592013-03-20 15:29:52 -0300642 break;
643 if (i == ARRAY_SIZE(hdpvr_dv_timings))
644 return -EINVAL;
645 dev->cur_dv_timings = hdpvr_dv_timings[i];
646 dev->width = hdpvr_dv_timings[i].bt.width;
647 dev->height = hdpvr_dv_timings[i].bt.height;
648 return 0;
649}
650
651static int vidioc_g_dv_timings(struct file *file, void *_fh,
652 struct v4l2_dv_timings *timings)
653{
654 struct hdpvr_device *dev = video_drvdata(file);
655 struct hdpvr_fh *fh = _fh;
656
657 fh->legacy_mode = false;
658 if (dev->options.video_input)
659 return -ENODATA;
660 *timings = dev->cur_dv_timings;
661 return 0;
662}
663
664static int vidioc_query_dv_timings(struct file *file, void *_fh,
665 struct v4l2_dv_timings *timings)
666{
667 struct hdpvr_device *dev = video_drvdata(file);
668 struct hdpvr_fh *fh = _fh;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300669 struct hdpvr_video_info vid_info;
Hans Verkuil3315c592013-03-20 15:29:52 -0300670 bool interlaced;
671 int ret = 0;
672 int i;
673
674 fh->legacy_mode = false;
675 if (dev->options.video_input)
676 return -ENODATA;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300677 ret = get_video_info(dev, &vid_info);
678 if (ret)
Hans Verkuil5f454d82013-05-29 03:55:15 -0300679 return ret;
680 if (!vid_info.valid)
Hans Verkuil3315c592013-03-20 15:29:52 -0300681 return -ENOLCK;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300682 interlaced = vid_info.fps <= 30;
Hans Verkuil3315c592013-03-20 15:29:52 -0300683 for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
684 const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
685 unsigned hsize;
686 unsigned vsize;
687 unsigned fps;
688
Hans Verkuileacf8f92013-07-29 08:40:59 -0300689 hsize = V4L2_DV_BT_FRAME_WIDTH(bt);
690 vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Hans Verkuil3315c592013-03-20 15:29:52 -0300691 fps = (unsigned)bt->pixelclock / (hsize * vsize);
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300692 if (bt->width != vid_info.width ||
693 bt->height != vid_info.height ||
Hans Verkuil3315c592013-03-20 15:29:52 -0300694 bt->interlaced != interlaced ||
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300695 (fps != vid_info.fps && fps + 1 != vid_info.fps))
Hans Verkuil3315c592013-03-20 15:29:52 -0300696 continue;
697 *timings = hdpvr_dv_timings[i];
698 break;
699 }
700 if (i == ARRAY_SIZE(hdpvr_dv_timings))
701 ret = -ERANGE;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300702
Hans Verkuil3315c592013-03-20 15:29:52 -0300703 return ret;
704}
705
706static int vidioc_enum_dv_timings(struct file *file, void *_fh,
707 struct v4l2_enum_dv_timings *timings)
708{
709 struct hdpvr_device *dev = video_drvdata(file);
710 struct hdpvr_fh *fh = _fh;
711
712 fh->legacy_mode = false;
713 memset(timings->reserved, 0, sizeof(timings->reserved));
714 if (dev->options.video_input)
715 return -ENODATA;
716 if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
717 return -EINVAL;
718 timings->timings = hdpvr_dv_timings[timings->index];
719 return 0;
720}
721
722static int vidioc_dv_timings_cap(struct file *file, void *_fh,
723 struct v4l2_dv_timings_cap *cap)
724{
725 struct hdpvr_device *dev = video_drvdata(file);
726 struct hdpvr_fh *fh = _fh;
727
728 fh->legacy_mode = false;
729 if (dev->options.video_input)
730 return -ENODATA;
731 cap->type = V4L2_DV_BT_656_1120;
732 cap->bt.min_width = 720;
733 cap->bt.max_width = 1920;
734 cap->bt.min_height = 480;
735 cap->bt.max_height = 1080;
736 cap->bt.min_pixelclock = 27000000;
737 cap->bt.max_pixelclock = 74250000;
738 cap->bt.standards = V4L2_DV_BT_STD_CEA861;
739 cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
740 return 0;
741}
742
Janne Grunau9aba42e2009-03-18 18:10:04 -0300743static const char *iname[] = {
744 [HDPVR_COMPONENT] = "Component",
745 [HDPVR_SVIDEO] = "S-Video",
746 [HDPVR_COMPOSITE] = "Composite",
747};
748
Hans Verkuil5854bf82013-04-10 14:26:52 -0300749static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300750{
Janne Grunau9aba42e2009-03-18 18:10:04 -0300751 unsigned int n;
752
753 n = i->index;
754 if (n >= HDPVR_VIDEO_INPUTS)
755 return -EINVAL;
756
757 i->type = V4L2_INPUT_TYPE_CAMERA;
758
759 strncpy(i->name, iname[n], sizeof(i->name) - 1);
760 i->name[sizeof(i->name) - 1] = '\0';
761
762 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
763
Hans Verkuil8f69da92013-03-19 09:53:29 -0300764 i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
765 i->std = n ? V4L2_STD_ALL : 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300766
767 return 0;
768}
769
Hans Verkuil5854bf82013-04-10 14:26:52 -0300770static int vidioc_s_input(struct file *file, void *_fh,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300771 unsigned int index)
772{
Hans Verkuilede197a2013-04-06 06:00:17 -0300773 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300774 int retval;
775
776 if (index >= HDPVR_VIDEO_INPUTS)
777 return -EINVAL;
778
779 if (dev->status != STATUS_IDLE)
Hans Verkuil97caa312013-02-12 09:26:30 -0300780 return -EBUSY;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300781
782 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
Hans Verkuil8f69da92013-03-19 09:53:29 -0300783 if (!retval) {
Janne Grunau9aba42e2009-03-18 18:10:04 -0300784 dev->options.video_input = index;
Hans Verkuil5854bf82013-04-10 14:26:52 -0300785 /*
786 * Unfortunately gstreamer calls ENUMSTD and bails out if it
787 * won't find any formats, even though component input is
788 * selected. This means that we have to leave tvnorms at
789 * V4L2_STD_ALL. We cannot use the 'legacy' trick since
790 * tvnorms is set at the device node level and not at the
791 * filehandle level.
792 *
793 * Comment this out for now, but if the legacy mode can be
794 * removed in the future, then this code should be enabled
795 * again.
Hans Verkuil4b304092015-03-09 13:34:09 -0300796 dev->video_dev.tvnorms =
Hans Verkuil5854bf82013-04-10 14:26:52 -0300797 (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
798 */
Hans Verkuil8f69da92013-03-19 09:53:29 -0300799 }
Janne Grunau9aba42e2009-03-18 18:10:04 -0300800
801 return retval;
802}
803
804static int vidioc_g_input(struct file *file, void *private_data,
805 unsigned int *index)
806{
Hans Verkuilede197a2013-04-06 06:00:17 -0300807 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300808
809 *index = dev->options.video_input;
810 return 0;
811}
812
813
814static const char *audio_iname[] = {
815 [HDPVR_RCA_FRONT] = "RCA front",
816 [HDPVR_RCA_BACK] = "RCA back",
817 [HDPVR_SPDIF] = "SPDIF",
818};
819
820static int vidioc_enumaudio(struct file *file, void *priv,
821 struct v4l2_audio *audio)
822{
823 unsigned int n;
824
825 n = audio->index;
826 if (n >= HDPVR_AUDIO_INPUTS)
827 return -EINVAL;
828
829 audio->capability = V4L2_AUDCAP_STEREO;
830
831 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
832 audio->name[sizeof(audio->name) - 1] = '\0';
833
834 return 0;
835}
836
837static int vidioc_s_audio(struct file *file, void *private_data,
Hans Verkuil0e8025b92012-09-04 11:59:31 -0300838 const struct v4l2_audio *audio)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300839{
Hans Verkuilede197a2013-04-06 06:00:17 -0300840 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300841 int retval;
842
843 if (audio->index >= HDPVR_AUDIO_INPUTS)
844 return -EINVAL;
845
846 if (dev->status != STATUS_IDLE)
Hans Verkuil97caa312013-02-12 09:26:30 -0300847 return -EBUSY;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300848
849 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
850 if (!retval)
851 dev->options.audio_input = audio->index;
852
853 return retval;
854}
855
856static int vidioc_g_audio(struct file *file, void *private_data,
857 struct v4l2_audio *audio)
858{
Hans Verkuilede197a2013-04-06 06:00:17 -0300859 struct hdpvr_device *dev = video_drvdata(file);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300860
861 audio->index = dev->options.audio_input;
862 audio->capability = V4L2_AUDCAP_STEREO;
863 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
864 audio->name[sizeof(audio->name) - 1] = '\0';
865 return 0;
866}
867
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300868static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300869{
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300870 struct hdpvr_device *dev =
871 container_of(ctrl->handler, struct hdpvr_device, hdl);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300872
873 switch (ctrl->id) {
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300874 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
875 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
876 dev->video_bitrate->val >= dev->video_bitrate_peak->val)
877 dev->video_bitrate_peak->val =
878 dev->video_bitrate->val + 100000;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300879 break;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300880 }
881 return 0;
882}
883
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300884static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300885{
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300886 struct hdpvr_device *dev =
887 container_of(ctrl->handler, struct hdpvr_device, hdl);
888 struct hdpvr_options *opt = &dev->options;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300889 int ret = -EINVAL;
890
891 switch (ctrl->id) {
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300892 case V4L2_CID_BRIGHTNESS:
893 ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
894 if (ret)
895 break;
896 dev->options.brightness = ctrl->val;
897 return 0;
898 case V4L2_CID_CONTRAST:
899 ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
900 if (ret)
901 break;
902 dev->options.contrast = ctrl->val;
903 return 0;
904 case V4L2_CID_SATURATION:
905 ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
906 if (ret)
907 break;
908 dev->options.saturation = ctrl->val;
909 return 0;
910 case V4L2_CID_HUE:
911 ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
912 if (ret)
913 break;
914 dev->options.hue = ctrl->val;
915 return 0;
916 case V4L2_CID_SHARPNESS:
917 ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
918 if (ret)
919 break;
920 dev->options.sharpness = ctrl->val;
921 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300922 case V4L2_CID_MPEG_AUDIO_ENCODING:
923 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300924 opt->audio_codec = ctrl->val;
Hans Verkuil34458572014-06-16 09:08:29 -0300925 return hdpvr_set_audio(dev, opt->audio_input + 1,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300926 opt->audio_codec);
927 }
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300928 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300929 case V4L2_CID_MPEG_VIDEO_ENCODING:
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300930 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300931/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
932/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
933/* opt->gop_mode |= 0x2; */
934/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
935/* opt->gop_mode); */
936/* } */
937/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
938/* opt->gop_mode &= ~0x2; */
939/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
940/* opt->gop_mode); */
941/* } */
942/* break; */
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300943 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
944 uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
945 uint bitrate = dev->video_bitrate->val / 100000;
946
947 if (ctrl->is_new) {
948 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
949 opt->bitrate_mode = HDPVR_CONSTANT;
950 else
951 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300952 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
953 opt->bitrate_mode);
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300954 v4l2_ctrl_activate(dev->video_bitrate_peak,
955 ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300956 }
Janne Grunau9aba42e2009-03-18 18:10:04 -0300957
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300958 if (dev->video_bitrate_peak->is_new ||
959 dev->video_bitrate->is_new) {
960 opt->bitrate = bitrate;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300961 opt->peak_bitrate = peak_bitrate;
962 hdpvr_set_bitrate(dev);
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300963 }
964 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300965 }
966 case V4L2_CID_MPEG_STREAM_TYPE:
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300967 return 0;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300968 default:
Hans Verkuil99c77aa2013-03-19 09:30:50 -0300969 break;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300970 }
971 return ret;
972}
973
Janne Grunau9aba42e2009-03-18 18:10:04 -0300974static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
975 struct v4l2_fmtdesc *f)
976{
Hans Verkuil97caa312013-02-12 09:26:30 -0300977 if (f->index != 0)
Janne Grunau9aba42e2009-03-18 18:10:04 -0300978 return -EINVAL;
979
980 f->flags = V4L2_FMT_FLAG_COMPRESSED;
981 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
982 f->pixelformat = V4L2_PIX_FMT_MPEG;
983
984 return 0;
985}
986
Hans Verkuil3315c592013-03-20 15:29:52 -0300987static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300988 struct v4l2_format *f)
989{
Hans Verkuilede197a2013-04-06 06:00:17 -0300990 struct hdpvr_device *dev = video_drvdata(file);
Hans Verkuil3315c592013-03-20 15:29:52 -0300991 struct hdpvr_fh *fh = _fh;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -0300992 int ret;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300993
Hans Verkuil3315c592013-03-20 15:29:52 -0300994 /*
995 * The original driver would always returns the current detected
996 * resolution as the format (and EFAULT if it couldn't be detected).
997 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
998 * better way of doing this, but to stay compatible with existing
999 * applications we assume legacy mode every time an application opens
1000 * the device. Only if one of the new DV_TIMINGS ioctls is called
1001 * will the filehandle go into 'normal' mode where g_fmt returns the
1002 * last set format.
1003 */
1004 if (fh->legacy_mode) {
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -03001005 struct hdpvr_video_info vid_info;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001006
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -03001007 ret = get_video_info(dev, &vid_info);
Hans Verkuil5f454d82013-05-29 03:55:15 -03001008 if (ret < 0)
1009 return ret;
1010 if (!vid_info.valid)
Hans Verkuil3315c592013-03-20 15:29:52 -03001011 return -EFAULT;
Leonid Kegulskiy4d601c42013-05-13 07:10:42 -03001012 f->fmt.pix.width = vid_info.width;
1013 f->fmt.pix.height = vid_info.height;
Hans Verkuil3315c592013-03-20 15:29:52 -03001014 } else {
1015 f->fmt.pix.width = dev->width;
1016 f->fmt.pix.height = dev->height;
1017 }
Janne Grunau9aba42e2009-03-18 18:10:04 -03001018 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001019 f->fmt.pix.sizeimage = dev->bulk_in_size;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001020 f->fmt.pix.bytesperline = 0;
Hans Verkuil3315c592013-03-20 15:29:52 -03001021 if (f->fmt.pix.width == 720) {
1022 /* SDTV formats */
1023 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1024 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1025 } else {
1026 /* HDTV formats */
Hans Verkuil93e6a852014-07-05 05:51:55 -03001027 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Hans Verkuil3315c592013-03-20 15:29:52 -03001028 f->fmt.pix.field = V4L2_FIELD_NONE;
1029 }
Janne Grunau9aba42e2009-03-18 18:10:04 -03001030 return 0;
1031}
1032
Janne Grunau76717b82009-03-18 20:57:04 -03001033static int vidioc_encoder_cmd(struct file *filp, void *priv,
1034 struct v4l2_encoder_cmd *a)
1035{
Hans Verkuilede197a2013-04-06 06:00:17 -03001036 struct hdpvr_device *dev = video_drvdata(filp);
1037 int res = 0;
Janne Grunau76717b82009-03-18 20:57:04 -03001038
1039 mutex_lock(&dev->io_mutex);
Hans Verkuilede197a2013-04-06 06:00:17 -03001040 a->flags = 0;
Janne Grunau76717b82009-03-18 20:57:04 -03001041
Janne Grunau76717b82009-03-18 20:57:04 -03001042 switch (a->cmd) {
1043 case V4L2_ENC_CMD_START:
Hans Verkuilede197a2013-04-06 06:00:17 -03001044 if (dev->owner && filp->private_data != dev->owner) {
1045 res = -EBUSY;
1046 break;
1047 }
1048 if (dev->status == STATUS_STREAMING)
1049 break;
Janne Grunau76717b82009-03-18 20:57:04 -03001050 res = hdpvr_start_streaming(dev);
Hans Verkuilede197a2013-04-06 06:00:17 -03001051 if (!res)
1052 dev->owner = filp->private_data;
1053 else
1054 dev->status = STATUS_IDLE;
Janne Grunau76717b82009-03-18 20:57:04 -03001055 break;
1056 case V4L2_ENC_CMD_STOP:
Hans Verkuilede197a2013-04-06 06:00:17 -03001057 if (dev->owner && filp->private_data != dev->owner) {
1058 res = -EBUSY;
1059 break;
1060 }
1061 if (dev->status == STATUS_IDLE)
1062 break;
Janne Grunau76717b82009-03-18 20:57:04 -03001063 res = hdpvr_stop_streaming(dev);
Hans Verkuilede197a2013-04-06 06:00:17 -03001064 if (!res)
1065 dev->owner = NULL;
Janne Grunau76717b82009-03-18 20:57:04 -03001066 break;
1067 default:
Janne Grunau9ef77ad2009-03-27 20:09:40 -03001068 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau76717b82009-03-18 20:57:04 -03001069 "Unsupported encoder cmd %d\n", a->cmd);
Janne Grunau48f98f72009-03-26 20:56:06 -03001070 res = -EINVAL;
Hans Verkuilede197a2013-04-06 06:00:17 -03001071 break;
Janne Grunau76717b82009-03-18 20:57:04 -03001072 }
Hans Verkuilede197a2013-04-06 06:00:17 -03001073
Janne Grunau76717b82009-03-18 20:57:04 -03001074 mutex_unlock(&dev->io_mutex);
1075 return res;
1076}
1077
1078static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1079 struct v4l2_encoder_cmd *a)
1080{
Hans Verkuilede197a2013-04-06 06:00:17 -03001081 a->flags = 0;
Janne Grunau76717b82009-03-18 20:57:04 -03001082 switch (a->cmd) {
1083 case V4L2_ENC_CMD_START:
1084 case V4L2_ENC_CMD_STOP:
1085 return 0;
1086 default:
1087 return -EINVAL;
1088 }
1089}
Janne Grunau9aba42e2009-03-18 18:10:04 -03001090
1091static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1092 .vidioc_querycap = vidioc_querycap,
1093 .vidioc_s_std = vidioc_s_std,
Hans Verkuil8f69da92013-03-19 09:53:29 -03001094 .vidioc_g_std = vidioc_g_std,
1095 .vidioc_querystd = vidioc_querystd,
Hans Verkuil3315c592013-03-20 15:29:52 -03001096 .vidioc_s_dv_timings = vidioc_s_dv_timings,
1097 .vidioc_g_dv_timings = vidioc_g_dv_timings,
1098 .vidioc_query_dv_timings= vidioc_query_dv_timings,
1099 .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
1100 .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
Janne Grunau9aba42e2009-03-18 18:10:04 -03001101 .vidioc_enum_input = vidioc_enum_input,
1102 .vidioc_g_input = vidioc_g_input,
1103 .vidioc_s_input = vidioc_s_input,
1104 .vidioc_enumaudio = vidioc_enumaudio,
1105 .vidioc_g_audio = vidioc_g_audio,
1106 .vidioc_s_audio = vidioc_s_audio,
Hans Verkuil65fe42d2013-02-12 08:26:59 -03001107 .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
1108 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
Hans Verkuil3315c592013-03-20 15:29:52 -03001109 .vidioc_s_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1110 .vidioc_try_fmt_vid_cap = vidioc_g_fmt_vid_cap,
Janne Grunau76717b82009-03-18 20:57:04 -03001111 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1112 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
Hans Verkuil65fe42d2013-02-12 08:26:59 -03001113 .vidioc_log_status = v4l2_ctrl_log_status,
1114 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1115 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Janne Grunau9aba42e2009-03-18 18:10:04 -03001116};
1117
1118static void hdpvr_device_release(struct video_device *vdev)
1119{
1120 struct hdpvr_device *dev = video_get_drvdata(vdev);
1121
1122 hdpvr_delete(dev);
Hans Verkuilf2b305c2010-05-02 08:01:04 -03001123 mutex_lock(&dev->io_mutex);
Bhaktipriya Shridhar5612e192016-07-02 07:47:31 -03001124 flush_work(&dev->worker);
Hans Verkuilf2b305c2010-05-02 08:01:04 -03001125 mutex_unlock(&dev->io_mutex);
1126
1127 v4l2_device_unregister(&dev->v4l2_dev);
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001128 v4l2_ctrl_handler_free(&dev->hdl);
Hans Verkuilf2b305c2010-05-02 08:01:04 -03001129
1130 /* deregister I2C adapter */
Mauro Carvalho Chehab54d80902013-03-21 15:46:18 -03001131#if IS_ENABLED(CONFIG_I2C)
Hans Verkuilf2b305c2010-05-02 08:01:04 -03001132 mutex_lock(&dev->i2c_mutex);
Jarod Wilson324b04b2011-01-14 16:25:21 -03001133 i2c_del_adapter(&dev->i2c_adapter);
Hans Verkuilf2b305c2010-05-02 08:01:04 -03001134 mutex_unlock(&dev->i2c_mutex);
1135#endif /* CONFIG_I2C */
1136
1137 kfree(dev->usbc_buf);
1138 kfree(dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -03001139}
1140
1141static const struct video_device hdpvr_video_template = {
Janne Grunau9aba42e2009-03-18 18:10:04 -03001142 .fops = &hdpvr_fops,
1143 .release = hdpvr_device_release,
1144 .ioctl_ops = &hdpvr_ioctl_ops,
Hans Verkuil5854bf82013-04-10 14:26:52 -03001145 .tvnorms = V4L2_STD_ALL,
Janne Grunau9aba42e2009-03-18 18:10:04 -03001146};
1147
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001148static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
1149 .try_ctrl = hdpvr_try_ctrl,
1150 .s_ctrl = hdpvr_s_ctrl,
1151};
1152
Janne Grunaua50ab292009-03-26 14:40:55 -03001153int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1154 int devnum)
Janne Grunau9aba42e2009-03-18 18:10:04 -03001155{
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001156 struct v4l2_ctrl_handler *hdl = &dev->hdl;
1157 bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
1158 int res;
1159
Hans Verkuil8f69da92013-03-19 09:53:29 -03001160 dev->cur_std = V4L2_STD_525_60;
1161 dev->width = 720;
1162 dev->height = 480;
Hans Verkuil3315c592013-03-20 15:29:52 -03001163 dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX];
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001164 v4l2_ctrl_handler_init(hdl, 11);
1165 if (dev->fw_ver > 0x15) {
1166 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1167 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
1168 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1169 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
1170 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1171 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
1172 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1173 V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
1174 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1175 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1176 } else {
1177 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1178 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
1179 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1180 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
1181 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1182 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
1183 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1184 V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
1185 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1186 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1187 }
1188
1189 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1190 V4L2_CID_MPEG_STREAM_TYPE,
1191 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
1192 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
1193 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1194 V4L2_CID_MPEG_AUDIO_ENCODING,
1195 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
Hans Verkuil34458572014-06-16 09:08:29 -03001196 0x7, ac3 ? dev->options.audio_codec : V4L2_MPEG_AUDIO_ENCODING_AAC);
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001197 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1198 V4L2_CID_MPEG_VIDEO_ENCODING,
1199 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
1200 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
1201
1202 dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1203 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1204 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1205 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1206
1207 dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1208 V4L2_CID_MPEG_VIDEO_BITRATE,
1209 1000000, 13500000, 100000, 6500000);
1210 dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1211 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1212 1100000, 20200000, 100000, 9000000);
1213 dev->v4l2_dev.ctrl_handler = hdl;
1214 if (hdl->error) {
1215 res = hdl->error;
1216 v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
1217 goto error;
1218 }
1219 v4l2_ctrl_cluster(3, &dev->video_mode);
1220 res = v4l2_ctrl_handler_setup(hdl);
1221 if (res < 0) {
1222 v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
1223 goto error;
1224 }
1225
Janne Grunau9aba42e2009-03-18 18:10:04 -03001226 /* setup and register video device */
Hans Verkuil4b304092015-03-09 13:34:09 -03001227 dev->video_dev = hdpvr_video_template;
1228 strcpy(dev->video_dev.name, "Hauppauge HD PVR");
1229 dev->video_dev.v4l2_dev = &dev->v4l2_dev;
1230 video_set_drvdata(&dev->video_dev, dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -03001231
Hans Verkuil4b304092015-03-09 13:34:09 -03001232 res = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER, devnum);
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001233 if (res < 0) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -03001234 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -03001235 goto error;
1236 }
1237
1238 return 0;
1239error:
Hans Verkuil99c77aa2013-03-19 09:30:50 -03001240 v4l2_ctrl_handler_free(hdl);
1241 return res;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001242}