blob: 93f795960a9478d0efaa327259ea321be96780d7 [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>
13#include <linux/errno.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/uaccess.h>
18#include <linux/usb.h>
19#include <linux/mutex.h>
20#include <linux/version.h>
21#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
29#define BULK_URB_TIMEOUT 1250 /* 1.25 seconds */
30
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 -030038struct hdpvr_fh {
39 struct hdpvr_device *dev;
40};
41
42static uint list_size(struct list_head *list)
43{
44 struct list_head *tmp;
45 uint count = 0;
46
47 list_for_each(tmp, list) {
48 count++;
49 }
50
51 return count;
52}
53
54/*=========================================================================*/
55/* urb callback */
56static void hdpvr_read_bulk_callback(struct urb *urb)
57{
58 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
59 struct hdpvr_device *dev = buf->dev;
60
61 /* marking buffer as received and wake waiting */
62 buf->status = BUFSTAT_READY;
63 wake_up_interruptible(&dev->wait_data);
64}
65
66/*=========================================================================*/
67/* bufffer bits */
68
69/* function expects dev->io_mutex to be hold by caller */
70int hdpvr_cancel_queue(struct hdpvr_device *dev)
71{
72 struct hdpvr_buffer *buf;
73
74 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
75 usb_kill_urb(buf->urb);
76 buf->status = BUFSTAT_AVAILABLE;
77 }
78
79 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
80
81 return 0;
82}
83
84static int hdpvr_free_queue(struct list_head *q)
85{
86 struct list_head *tmp;
87 struct list_head *p;
88 struct hdpvr_buffer *buf;
89 struct urb *urb;
90
91 for (p = q->next; p != q;) {
92 buf = list_entry(p, struct hdpvr_buffer, buff_list);
93
94 urb = buf->urb;
Daniel Mack997ea582010-04-12 13:17:25 +020095 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
96 urb->transfer_buffer, urb->transfer_dma);
Janne Grunau9aba42e2009-03-18 18:10:04 -030097 usb_free_urb(urb);
98 tmp = p->next;
99 list_del(p);
100 kfree(buf);
101 p = tmp;
102 }
103
104 return 0;
105}
106
107/* function expects dev->io_mutex to be hold by caller */
108int hdpvr_free_buffers(struct hdpvr_device *dev)
109{
110 hdpvr_cancel_queue(dev);
111
112 hdpvr_free_queue(&dev->free_buff_list);
113 hdpvr_free_queue(&dev->rec_buff_list);
114
115 return 0;
116}
117
118/* function expects dev->io_mutex to be hold by caller */
119int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
120{
121 uint i;
122 int retval = -ENOMEM;
123 u8 *mem;
124 struct hdpvr_buffer *buf;
125 struct urb *urb;
126
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300127 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300128 "allocating %u buffers\n", count);
129
130 for (i = 0; i < count; i++) {
131
132 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
133 if (!buf) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300134 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300135 goto exit;
136 }
137 buf->dev = dev;
138
139 urb = usb_alloc_urb(0, GFP_KERNEL);
140 if (!urb) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300141 v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
Julia Lawallcd0e2802009-11-21 08:49:41 -0300142 goto exit_urb;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300143 }
144 buf->urb = urb;
145
Daniel Mack997ea582010-04-12 13:17:25 +0200146 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
147 &urb->transfer_dma);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300148 if (!mem) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300149 v4l2_err(&dev->v4l2_dev,
150 "cannot allocate usb transfer buffer\n");
Julia Lawallcd0e2802009-11-21 08:49:41 -0300151 goto exit_urb_buffer;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300152 }
153
154 usb_fill_bulk_urb(buf->urb, dev->udev,
155 usb_rcvbulkpipe(dev->udev,
156 dev->bulk_in_endpointAddr),
157 mem, dev->bulk_in_size,
158 hdpvr_read_bulk_callback, buf);
159
James M McLaren4f5c9332010-10-03 19:09:18 -0300160 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300161 buf->status = BUFSTAT_AVAILABLE;
162 list_add_tail(&buf->buff_list, &dev->free_buff_list);
163 }
164 return 0;
Julia Lawallcd0e2802009-11-21 08:49:41 -0300165exit_urb_buffer:
166 usb_free_urb(urb);
167exit_urb:
168 kfree(buf);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300169exit:
170 hdpvr_free_buffers(dev);
171 return retval;
172}
173
174static int hdpvr_submit_buffers(struct hdpvr_device *dev)
175{
176 struct hdpvr_buffer *buf;
177 struct urb *urb;
178 int ret = 0, err_count = 0;
179
180 mutex_lock(&dev->io_mutex);
181
182 while (dev->status == STATUS_STREAMING &&
183 !list_empty(&dev->free_buff_list)) {
184
185 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
186 buff_list);
187 if (buf->status != BUFSTAT_AVAILABLE) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300188 v4l2_err(&dev->v4l2_dev,
Thadeu Lima de Souza Cascardo4b512d22009-04-14 23:14:10 -0300189 "buffer not marked as available\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300190 ret = -EFAULT;
191 goto err;
192 }
193
194 urb = buf->urb;
195 urb->status = 0;
196 urb->actual_length = 0;
197 ret = usb_submit_urb(urb, GFP_KERNEL);
198 if (ret) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300199 v4l2_err(&dev->v4l2_dev,
200 "usb_submit_urb in %s returned %d\n",
201 __func__, ret);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300202 if (++err_count > 2)
203 break;
204 continue;
205 }
206 buf->status = BUFSTAT_INPROGRESS;
207 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
208 }
209err:
Janne Grunaud211bfc2009-03-26 20:29:39 -0300210 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300211 mutex_unlock(&dev->io_mutex);
212 return ret;
213}
214
215static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
216{
217 struct hdpvr_buffer *buf;
218
219 mutex_lock(&dev->io_mutex);
220
221 if (list_empty(&dev->rec_buff_list)) {
222 mutex_unlock(&dev->io_mutex);
223 return NULL;
224 }
225
226 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
227 buff_list);
228 mutex_unlock(&dev->io_mutex);
229
230 return buf;
231}
232
233static void hdpvr_transmit_buffers(struct work_struct *work)
234{
235 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
236 worker);
237
238 while (dev->status == STATUS_STREAMING) {
239
240 if (hdpvr_submit_buffers(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300241 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300242 goto error;
243 }
244 if (wait_event_interruptible(dev->wait_buffer,
245 !list_empty(&dev->free_buff_list) ||
246 dev->status != STATUS_STREAMING))
247 goto error;
248 }
249
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 worker exited\n");
252 return;
253error:
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300254 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300255 "transmit buffers errored\n");
256 dev->status = STATUS_ERROR;
257}
258
259/* function expects dev->io_mutex to be hold by caller */
260static int hdpvr_start_streaming(struct hdpvr_device *dev)
261{
262 int ret;
263 struct hdpvr_video_info *vidinf;
264
265 if (dev->status == STATUS_STREAMING)
266 return 0;
267 else if (dev->status != STATUS_IDLE)
268 return -EAGAIN;
269
270 vidinf = get_video_info(dev);
271
272 if (vidinf) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300273 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300274 "video signal: %dx%d@%dhz\n", vidinf->width,
275 vidinf->height, vidinf->fps);
276 kfree(vidinf);
277
278 /* start streaming 2 request */
279 ret = usb_control_msg(dev->udev,
280 usb_sndctrlpipe(dev->udev, 0),
281 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300282 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300283 "encoder start control request returned %d\n", ret);
284
285 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
286
287 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
288 queue_work(dev->workqueue, &dev->worker);
289
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300290 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300291 "streaming started\n");
292 dev->status = STATUS_STREAMING;
293
294 return 0;
295 }
296 msleep(250);
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300297 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300298 "no video signal at input %d\n", dev->options.video_input);
299 return -EAGAIN;
300}
301
302
303/* function expects dev->io_mutex to be hold by caller */
304static int hdpvr_stop_streaming(struct hdpvr_device *dev)
305{
Márton Németh85d682b2010-01-23 14:44:34 +0100306 int actual_length;
307 uint c = 0;
Janne Grunau7d771ff2009-03-27 20:21:17 -0300308 u8 *buf;
309
Janne Grunau9aba42e2009-03-18 18:10:04 -0300310 if (dev->status == STATUS_IDLE)
311 return 0;
312 else if (dev->status != STATUS_STREAMING)
313 return -EAGAIN;
314
Janne Grunau7d771ff2009-03-27 20:21:17 -0300315 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
316 if (!buf)
317 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
318 "for emptying the internal device buffer. "
319 "Next capture start will be slow\n");
320
Janne Grunau9aba42e2009-03-18 18:10:04 -0300321 dev->status = STATUS_SHUTTING_DOWN;
322 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
Janne Grunau48f98f72009-03-26 20:56:06 -0300323 mutex_unlock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300324
325 wake_up_interruptible(&dev->wait_buffer);
326 msleep(50);
327
328 flush_workqueue(dev->workqueue);
329
Janne Grunau48f98f72009-03-26 20:56:06 -0300330 mutex_lock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300331 /* kill the still outstanding urbs */
332 hdpvr_cancel_queue(dev);
333
Janne Grunau7d771ff2009-03-27 20:21:17 -0300334 /* emptying the device buffer beforeshutting it down */
335 while (buf && ++c < 500 &&
336 !usb_bulk_msg(dev->udev,
337 usb_rcvbulkpipe(dev->udev,
338 dev->bulk_in_endpointAddr),
339 buf, dev->bulk_in_size, &actual_length,
340 BULK_URB_TIMEOUT)) {
341 /* wait */
342 msleep(5);
343 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
344 "%2d: got %d bytes\n", c, actual_length);
345 }
346 kfree(buf);
347 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
348 "used %d urbs to empty device buffers\n", c-1);
349 msleep(10);
350
Janne Grunau9aba42e2009-03-18 18:10:04 -0300351 dev->status = STATUS_IDLE;
352
353 return 0;
354}
355
356
357/*=======================================================================*/
358/*
359 * video 4 linux 2 file operations
360 */
361
362static int hdpvr_open(struct file *file)
363{
364 struct hdpvr_device *dev;
365 struct hdpvr_fh *fh;
366 int retval = -ENOMEM;
367
368 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
369 if (!dev) {
Julia Lawall42020662010-05-27 09:36:45 -0300370 pr_err("open failing with with ENODEV\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300371 retval = -ENODEV;
372 goto err;
373 }
374
375 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
376 if (!fh) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300377 v4l2_err(&dev->v4l2_dev, "Out of memory\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300378 goto err;
379 }
380 /* lock the device to allow correctly handling errors
381 * in resumption */
382 mutex_lock(&dev->io_mutex);
383 dev->open_count++;
Jiri Slaby00c1e212009-07-30 20:00:44 -0300384 mutex_unlock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300385
386 fh->dev = dev;
387
388 /* save our object in the file's private structure */
389 file->private_data = fh;
390
391 retval = 0;
392err:
Janne Grunau9aba42e2009-03-18 18:10:04 -0300393 return retval;
394}
395
396static int hdpvr_release(struct file *file)
397{
Joe Perchesabf84382010-07-12 17:50:03 -0300398 struct hdpvr_fh *fh = file->private_data;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300399 struct hdpvr_device *dev = fh->dev;
400
401 if (!dev)
402 return -ENODEV;
403
404 mutex_lock(&dev->io_mutex);
405 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
406 hdpvr_stop_streaming(dev);
407
408 mutex_unlock(&dev->io_mutex);
409
410 return 0;
411}
412
413/*
414 * hdpvr_v4l2_read()
415 * will allocate buffers when called for the first time
416 */
417static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
418 loff_t *pos)
419{
420 struct hdpvr_fh *fh = file->private_data;
421 struct hdpvr_device *dev = fh->dev;
422 struct hdpvr_buffer *buf = NULL;
423 struct urb *urb;
424 unsigned int ret = 0;
425 int rem, cnt;
426
427 if (*pos)
428 return -ESPIPE;
429
430 if (!dev)
431 return -ENODEV;
432
433 mutex_lock(&dev->io_mutex);
434 if (dev->status == STATUS_IDLE) {
435 if (hdpvr_start_streaming(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300436 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
437 "start_streaming failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300438 ret = -EIO;
439 msleep(200);
440 dev->status = STATUS_IDLE;
441 mutex_unlock(&dev->io_mutex);
442 goto err;
443 }
Janne Grunaud211bfc2009-03-26 20:29:39 -0300444 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300445 }
446 mutex_unlock(&dev->io_mutex);
447
448 /* wait for the first buffer */
449 if (!(file->f_flags & O_NONBLOCK)) {
450 if (wait_event_interruptible(dev->wait_data,
451 hdpvr_get_next_buffer(dev)))
452 return -ERESTARTSYS;
453 }
454
455 buf = hdpvr_get_next_buffer(dev);
456
457 while (count > 0 && buf) {
458
459 if (buf->status != BUFSTAT_READY &&
460 dev->status != STATUS_DISCONNECTED) {
461 /* return nonblocking */
462 if (file->f_flags & O_NONBLOCK) {
463 if (!ret)
464 ret = -EAGAIN;
465 goto err;
466 }
467
468 if (wait_event_interruptible(dev->wait_data,
469 buf->status == BUFSTAT_READY)) {
470 ret = -ERESTARTSYS;
471 goto err;
472 }
473 }
474
475 if (buf->status != BUFSTAT_READY)
476 break;
477
478 /* set remaining bytes to copy */
479 urb = buf->urb;
480 rem = urb->actual_length - buf->pos;
481 cnt = rem > count ? count : rem;
482
483 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
484 cnt)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300485 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300486 if (!ret)
487 ret = -EFAULT;
488 goto err;
489 }
490
491 buf->pos += cnt;
492 count -= cnt;
493 buffer += cnt;
494 ret += cnt;
495
496 /* finished, take next buffer */
497 if (buf->pos == urb->actual_length) {
498 mutex_lock(&dev->io_mutex);
499 buf->pos = 0;
500 buf->status = BUFSTAT_AVAILABLE;
501
502 list_move_tail(&buf->buff_list, &dev->free_buff_list);
503
Janne Grunaud211bfc2009-03-26 20:29:39 -0300504 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300505
506 mutex_unlock(&dev->io_mutex);
507
508 wake_up_interruptible(&dev->wait_buffer);
509
510 buf = hdpvr_get_next_buffer(dev);
511 }
512 }
513err:
514 if (!ret && !buf)
515 ret = -EAGAIN;
516 return ret;
517}
518
519static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
520{
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300521 struct hdpvr_buffer *buf = NULL;
Joe Perchesabf84382010-07-12 17:50:03 -0300522 struct hdpvr_fh *fh = filp->private_data;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300523 struct hdpvr_device *dev = fh->dev;
524 unsigned int mask = 0;
525
526 mutex_lock(&dev->io_mutex);
527
Laurent Pinchart957b4aa2009-11-27 13:57:22 -0300528 if (!video_is_registered(dev->video_dev)) {
Jiri Slaby00c1e212009-07-30 20:00:44 -0300529 mutex_unlock(&dev->io_mutex);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300530 return -EIO;
Jiri Slaby00c1e212009-07-30 20:00:44 -0300531 }
Janne Grunau9aba42e2009-03-18 18:10:04 -0300532
533 if (dev->status == STATUS_IDLE) {
534 if (hdpvr_start_streaming(dev)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -0300535 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
536 "start_streaming failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300537 dev->status = STATUS_IDLE;
538 }
539
Janne Grunaud211bfc2009-03-26 20:29:39 -0300540 print_buffer_status();
Janne Grunau9aba42e2009-03-18 18:10:04 -0300541 }
542 mutex_unlock(&dev->io_mutex);
543
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300544 buf = hdpvr_get_next_buffer(dev);
545 /* only wait if no data is available */
546 if (!buf || buf->status != BUFSTAT_READY) {
547 poll_wait(filp, &dev->wait_data, wait);
548 buf = hdpvr_get_next_buffer(dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300549 }
Janne Grunaud2ff3ec2009-03-26 14:32:54 -0300550 if (buf && buf->status == BUFSTAT_READY)
551 mask |= POLLIN | POLLRDNORM;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300552
553 return mask;
554}
555
556
Janne Grunau9aba42e2009-03-18 18:10:04 -0300557static const struct v4l2_file_operations hdpvr_fops = {
558 .owner = THIS_MODULE,
559 .open = hdpvr_open,
560 .release = hdpvr_release,
561 .read = hdpvr_read,
562 .poll = hdpvr_poll,
Janne Grunau76717b82009-03-18 20:57:04 -0300563 .unlocked_ioctl = video_ioctl2,
Janne Grunau9aba42e2009-03-18 18:10:04 -0300564};
565
566/*=======================================================================*/
567/*
568 * V4L2 ioctl handling
569 */
570
571static int vidioc_querycap(struct file *file, void *priv,
572 struct v4l2_capability *cap)
573{
574 struct hdpvr_device *dev = video_drvdata(file);
575
576 strcpy(cap->driver, "hdpvr");
Lars Hanischfdd70c32010-02-14 08:57:39 -0300577 strcpy(cap->card, "Hauppauge HD PVR");
Janne Grunau9aba42e2009-03-18 18:10:04 -0300578 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
579 cap->version = HDPVR_VERSION;
580 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
581 V4L2_CAP_AUDIO |
582 V4L2_CAP_READWRITE;
583 return 0;
584}
585
586static int vidioc_s_std(struct file *file, void *private_data,
587 v4l2_std_id *std)
588{
589 struct hdpvr_fh *fh = file->private_data;
590 struct hdpvr_device *dev = fh->dev;
591 u8 std_type = 1;
592
593 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
594 std_type = 0;
595
596 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
597}
598
599static const char *iname[] = {
600 [HDPVR_COMPONENT] = "Component",
601 [HDPVR_SVIDEO] = "S-Video",
602 [HDPVR_COMPOSITE] = "Composite",
603};
604
605static int vidioc_enum_input(struct file *file, void *priv,
606 struct v4l2_input *i)
607{
608 struct hdpvr_fh *fh = file->private_data;
609 struct hdpvr_device *dev = fh->dev;
610 unsigned int n;
611
612 n = i->index;
613 if (n >= HDPVR_VIDEO_INPUTS)
614 return -EINVAL;
615
616 i->type = V4L2_INPUT_TYPE_CAMERA;
617
618 strncpy(i->name, iname[n], sizeof(i->name) - 1);
619 i->name[sizeof(i->name) - 1] = '\0';
620
621 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
622
623 i->std = dev->video_dev->tvnorms;
624
625 return 0;
626}
627
628static int vidioc_s_input(struct file *file, void *private_data,
629 unsigned int index)
630{
631 struct hdpvr_fh *fh = file->private_data;
632 struct hdpvr_device *dev = fh->dev;
633 int retval;
634
635 if (index >= HDPVR_VIDEO_INPUTS)
636 return -EINVAL;
637
638 if (dev->status != STATUS_IDLE)
639 return -EAGAIN;
640
641 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
642 if (!retval)
643 dev->options.video_input = index;
644
645 return retval;
646}
647
648static int vidioc_g_input(struct file *file, void *private_data,
649 unsigned int *index)
650{
651 struct hdpvr_fh *fh = file->private_data;
652 struct hdpvr_device *dev = fh->dev;
653
654 *index = dev->options.video_input;
655 return 0;
656}
657
658
659static const char *audio_iname[] = {
660 [HDPVR_RCA_FRONT] = "RCA front",
661 [HDPVR_RCA_BACK] = "RCA back",
662 [HDPVR_SPDIF] = "SPDIF",
663};
664
665static int vidioc_enumaudio(struct file *file, void *priv,
666 struct v4l2_audio *audio)
667{
668 unsigned int n;
669
670 n = audio->index;
671 if (n >= HDPVR_AUDIO_INPUTS)
672 return -EINVAL;
673
674 audio->capability = V4L2_AUDCAP_STEREO;
675
676 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
677 audio->name[sizeof(audio->name) - 1] = '\0';
678
679 return 0;
680}
681
682static int vidioc_s_audio(struct file *file, void *private_data,
683 struct v4l2_audio *audio)
684{
685 struct hdpvr_fh *fh = file->private_data;
686 struct hdpvr_device *dev = fh->dev;
687 int retval;
688
689 if (audio->index >= HDPVR_AUDIO_INPUTS)
690 return -EINVAL;
691
692 if (dev->status != STATUS_IDLE)
693 return -EAGAIN;
694
695 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
696 if (!retval)
697 dev->options.audio_input = audio->index;
698
699 return retval;
700}
701
702static int vidioc_g_audio(struct file *file, void *private_data,
703 struct v4l2_audio *audio)
704{
705 struct hdpvr_fh *fh = file->private_data;
706 struct hdpvr_device *dev = fh->dev;
707
708 audio->index = dev->options.audio_input;
709 audio->capability = V4L2_AUDCAP_STEREO;
710 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
711 audio->name[sizeof(audio->name) - 1] = '\0';
712 return 0;
713}
714
715static const s32 supported_v4l2_ctrls[] = {
716 V4L2_CID_BRIGHTNESS,
717 V4L2_CID_CONTRAST,
718 V4L2_CID_SATURATION,
719 V4L2_CID_HUE,
720 V4L2_CID_SHARPNESS,
721 V4L2_CID_MPEG_AUDIO_ENCODING,
722 V4L2_CID_MPEG_VIDEO_ENCODING,
723 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
724 V4L2_CID_MPEG_VIDEO_BITRATE,
725 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
726};
727
728static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
729 int ac3)
730{
731 int err;
732
733 switch (qc->id) {
734 case V4L2_CID_BRIGHTNESS:
735 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
736 case V4L2_CID_CONTRAST:
737 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
738 case V4L2_CID_SATURATION:
739 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
740 case V4L2_CID_HUE:
741 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
742 case V4L2_CID_SHARPNESS:
743 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
744 case V4L2_CID_MPEG_AUDIO_ENCODING:
745 return v4l2_ctrl_query_fill(
746 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
747 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
748 : V4L2_MPEG_AUDIO_ENCODING_AAC,
749 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
750 case V4L2_CID_MPEG_VIDEO_ENCODING:
751 return v4l2_ctrl_query_fill(
752 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
753 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
754 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
755
756/* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
757/* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
758 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
759 return v4l2_ctrl_query_fill(
760 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
761 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
762 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
763
764 case V4L2_CID_MPEG_VIDEO_BITRATE:
765 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
766 6500000);
767 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
768 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
769 9000000);
770 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
771 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
772 return err;
773 default:
774 return -EINVAL;
775 }
776}
777
778static int vidioc_queryctrl(struct file *file, void *private_data,
779 struct v4l2_queryctrl *qc)
780{
781 struct hdpvr_fh *fh = file->private_data;
782 struct hdpvr_device *dev = fh->dev;
783 int i, next;
784 u32 id = qc->id;
785
786 memset(qc, 0, sizeof(*qc));
787
788 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
789 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
790
791 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
792 if (next) {
793 if (qc->id < supported_v4l2_ctrls[i])
794 qc->id = supported_v4l2_ctrls[i];
795 else
796 continue;
797 }
798
799 if (qc->id == supported_v4l2_ctrls[i])
800 return fill_queryctrl(&dev->options, qc,
801 dev->flags & HDPVR_FLAG_AC3_CAP);
802
803 if (qc->id < supported_v4l2_ctrls[i])
804 break;
805 }
806
807 return -EINVAL;
808}
809
810static int vidioc_g_ctrl(struct file *file, void *private_data,
811 struct v4l2_control *ctrl)
812{
813 struct hdpvr_fh *fh = file->private_data;
814 struct hdpvr_device *dev = fh->dev;
815
816 switch (ctrl->id) {
817 case V4L2_CID_BRIGHTNESS:
818 ctrl->value = dev->options.brightness;
819 break;
820 case V4L2_CID_CONTRAST:
821 ctrl->value = dev->options.contrast;
822 break;
823 case V4L2_CID_SATURATION:
824 ctrl->value = dev->options.saturation;
825 break;
826 case V4L2_CID_HUE:
827 ctrl->value = dev->options.hue;
828 break;
829 case V4L2_CID_SHARPNESS:
830 ctrl->value = dev->options.sharpness;
831 break;
832 default:
833 return -EINVAL;
834 }
835 return 0;
836}
837
838static int vidioc_s_ctrl(struct file *file, void *private_data,
839 struct v4l2_control *ctrl)
840{
841 struct hdpvr_fh *fh = file->private_data;
842 struct hdpvr_device *dev = fh->dev;
843 int retval;
844
845 switch (ctrl->id) {
846 case V4L2_CID_BRIGHTNESS:
847 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
848 if (!retval)
849 dev->options.brightness = ctrl->value;
850 break;
851 case V4L2_CID_CONTRAST:
852 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
853 if (!retval)
854 dev->options.contrast = ctrl->value;
855 break;
856 case V4L2_CID_SATURATION:
857 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
858 if (!retval)
859 dev->options.saturation = ctrl->value;
860 break;
861 case V4L2_CID_HUE:
862 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
863 if (!retval)
864 dev->options.hue = ctrl->value;
865 break;
866 case V4L2_CID_SHARPNESS:
867 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
868 if (!retval)
869 dev->options.sharpness = ctrl->value;
870 break;
871 default:
872 return -EINVAL;
873 }
874
875 return retval;
876}
877
878
879static int hdpvr_get_ctrl(struct hdpvr_options *opt,
880 struct v4l2_ext_control *ctrl)
881{
882 switch (ctrl->id) {
883 case V4L2_CID_MPEG_AUDIO_ENCODING:
884 ctrl->value = opt->audio_codec;
885 break;
886 case V4L2_CID_MPEG_VIDEO_ENCODING:
887 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
888 break;
889/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
890/* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
891/* break; */
892 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
893 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
894 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
895 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
896 break;
897 case V4L2_CID_MPEG_VIDEO_BITRATE:
898 ctrl->value = opt->bitrate * 100000;
899 break;
900 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
901 ctrl->value = opt->peak_bitrate * 100000;
902 break;
903 case V4L2_CID_MPEG_STREAM_TYPE:
904 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
905 break;
906 default:
907 return -EINVAL;
908 }
909 return 0;
910}
911
912static int vidioc_g_ext_ctrls(struct file *file, void *priv,
913 struct v4l2_ext_controls *ctrls)
914{
915 struct hdpvr_fh *fh = file->private_data;
916 struct hdpvr_device *dev = fh->dev;
917 int i, err = 0;
918
919 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
920 for (i = 0; i < ctrls->count; i++) {
921 struct v4l2_ext_control *ctrl = ctrls->controls + i;
922
923 err = hdpvr_get_ctrl(&dev->options, ctrl);
924 if (err) {
925 ctrls->error_idx = i;
926 break;
927 }
928 }
929 return err;
930
931 }
932
933 return -EINVAL;
934}
935
936
937static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
938{
939 int ret = -EINVAL;
940
941 switch (ctrl->id) {
942 case V4L2_CID_MPEG_AUDIO_ENCODING:
943 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
944 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
945 ret = 0;
946 break;
947 case V4L2_CID_MPEG_VIDEO_ENCODING:
948 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
949 ret = 0;
950 break;
951/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
952/* if (ctrl->value == 0 || ctrl->value == 128) */
953/* ret = 0; */
954/* break; */
955 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
956 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
957 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
958 ret = 0;
959 break;
960 case V4L2_CID_MPEG_VIDEO_BITRATE:
961 {
962 uint bitrate = ctrl->value / 100000;
963 if (bitrate >= 10 && bitrate <= 135)
964 ret = 0;
965 break;
966 }
967 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
968 {
969 uint peak_bitrate = ctrl->value / 100000;
970 if (peak_bitrate >= 10 && peak_bitrate <= 202)
971 ret = 0;
972 break;
973 }
974 case V4L2_CID_MPEG_STREAM_TYPE:
975 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
976 ret = 0;
977 break;
978 default:
979 return -EINVAL;
980 }
981 return 0;
982}
983
984static int vidioc_try_ext_ctrls(struct file *file, void *priv,
985 struct v4l2_ext_controls *ctrls)
986{
987 struct hdpvr_fh *fh = file->private_data;
988 struct hdpvr_device *dev = fh->dev;
989 int i, err = 0;
990
991 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
992 for (i = 0; i < ctrls->count; i++) {
993 struct v4l2_ext_control *ctrl = ctrls->controls + i;
994
995 err = hdpvr_try_ctrl(ctrl,
996 dev->flags & HDPVR_FLAG_AC3_CAP);
997 if (err) {
998 ctrls->error_idx = i;
999 break;
1000 }
1001 }
1002 return err;
1003 }
1004
1005 return -EINVAL;
1006}
1007
1008
1009static int hdpvr_set_ctrl(struct hdpvr_device *dev,
1010 struct v4l2_ext_control *ctrl)
1011{
1012 struct hdpvr_options *opt = &dev->options;
1013 int ret = 0;
1014
1015 switch (ctrl->id) {
1016 case V4L2_CID_MPEG_AUDIO_ENCODING:
1017 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
1018 opt->audio_codec = ctrl->value;
1019 ret = hdpvr_set_audio(dev, opt->audio_input,
1020 opt->audio_codec);
1021 }
1022 break;
1023 case V4L2_CID_MPEG_VIDEO_ENCODING:
1024 break;
1025/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1026/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1027/* opt->gop_mode |= 0x2; */
1028/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1029/* opt->gop_mode); */
1030/* } */
1031/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1032/* opt->gop_mode &= ~0x2; */
1033/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1034/* opt->gop_mode); */
1035/* } */
1036/* break; */
1037 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1038 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1039 opt->bitrate_mode != HDPVR_CONSTANT) {
1040 opt->bitrate_mode = HDPVR_CONSTANT;
1041 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1042 opt->bitrate_mode);
1043 }
1044 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1045 opt->bitrate_mode == HDPVR_CONSTANT) {
1046 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1047 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1048 opt->bitrate_mode);
1049 }
1050 break;
1051 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1052 uint bitrate = ctrl->value / 100000;
1053
1054 opt->bitrate = bitrate;
1055 if (bitrate >= opt->peak_bitrate)
1056 opt->peak_bitrate = bitrate+1;
1057
1058 hdpvr_set_bitrate(dev);
1059 break;
1060 }
1061 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1062 uint peak_bitrate = ctrl->value / 100000;
1063
1064 if (opt->bitrate_mode == HDPVR_CONSTANT)
1065 break;
1066
1067 if (opt->bitrate < peak_bitrate) {
1068 opt->peak_bitrate = peak_bitrate;
1069 hdpvr_set_bitrate(dev);
1070 } else
1071 ret = -EINVAL;
1072 break;
1073 }
1074 case V4L2_CID_MPEG_STREAM_TYPE:
1075 break;
1076 default:
1077 return -EINVAL;
1078 }
1079 return ret;
1080}
1081
1082static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1083 struct v4l2_ext_controls *ctrls)
1084{
1085 struct hdpvr_fh *fh = file->private_data;
1086 struct hdpvr_device *dev = fh->dev;
1087 int i, err = 0;
1088
1089 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1090 for (i = 0; i < ctrls->count; i++) {
1091 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1092
1093 err = hdpvr_try_ctrl(ctrl,
1094 dev->flags & HDPVR_FLAG_AC3_CAP);
1095 if (err) {
1096 ctrls->error_idx = i;
1097 break;
1098 }
1099 err = hdpvr_set_ctrl(dev, ctrl);
1100 if (err) {
1101 ctrls->error_idx = i;
1102 break;
1103 }
1104 }
1105 return err;
1106
1107 }
1108
1109 return -EINVAL;
1110}
1111
1112static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1113 struct v4l2_fmtdesc *f)
1114{
1115
1116 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1117 return -EINVAL;
1118
1119 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1120 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1121 f->pixelformat = V4L2_PIX_FMT_MPEG;
1122
1123 return 0;
1124}
1125
1126static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1127 struct v4l2_format *f)
1128{
1129 struct hdpvr_fh *fh = file->private_data;
1130 struct hdpvr_device *dev = fh->dev;
1131 struct hdpvr_video_info *vid_info;
1132
1133 if (!dev)
1134 return -ENODEV;
1135
1136 vid_info = get_video_info(dev);
1137 if (!vid_info)
1138 return -EFAULT;
1139
1140 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1141 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1142 f->fmt.pix.width = vid_info->width;
1143 f->fmt.pix.height = vid_info->height;
1144 f->fmt.pix.sizeimage = dev->bulk_in_size;
1145 f->fmt.pix.colorspace = 0;
1146 f->fmt.pix.bytesperline = 0;
1147 f->fmt.pix.field = V4L2_FIELD_ANY;
1148
1149 kfree(vid_info);
1150 return 0;
1151}
1152
Janne Grunau76717b82009-03-18 20:57:04 -03001153static int vidioc_encoder_cmd(struct file *filp, void *priv,
1154 struct v4l2_encoder_cmd *a)
1155{
1156 struct hdpvr_fh *fh = filp->private_data;
1157 struct hdpvr_device *dev = fh->dev;
1158 int res;
1159
1160 mutex_lock(&dev->io_mutex);
1161
1162 memset(&a->raw, 0, sizeof(a->raw));
1163 switch (a->cmd) {
1164 case V4L2_ENC_CMD_START:
1165 a->flags = 0;
1166 res = hdpvr_start_streaming(dev);
1167 break;
1168 case V4L2_ENC_CMD_STOP:
1169 res = hdpvr_stop_streaming(dev);
1170 break;
1171 default:
Janne Grunau9ef77ad2009-03-27 20:09:40 -03001172 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
Janne Grunau76717b82009-03-18 20:57:04 -03001173 "Unsupported encoder cmd %d\n", a->cmd);
Janne Grunau48f98f72009-03-26 20:56:06 -03001174 res = -EINVAL;
Janne Grunau76717b82009-03-18 20:57:04 -03001175 }
1176 mutex_unlock(&dev->io_mutex);
1177 return res;
1178}
1179
1180static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1181 struct v4l2_encoder_cmd *a)
1182{
1183 switch (a->cmd) {
1184 case V4L2_ENC_CMD_START:
1185 case V4L2_ENC_CMD_STOP:
1186 return 0;
1187 default:
1188 return -EINVAL;
1189 }
1190}
Janne Grunau9aba42e2009-03-18 18:10:04 -03001191
1192static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1193 .vidioc_querycap = vidioc_querycap,
1194 .vidioc_s_std = vidioc_s_std,
1195 .vidioc_enum_input = vidioc_enum_input,
1196 .vidioc_g_input = vidioc_g_input,
1197 .vidioc_s_input = vidioc_s_input,
1198 .vidioc_enumaudio = vidioc_enumaudio,
1199 .vidioc_g_audio = vidioc_g_audio,
1200 .vidioc_s_audio = vidioc_s_audio,
1201 .vidioc_queryctrl = vidioc_queryctrl,
1202 .vidioc_g_ctrl = vidioc_g_ctrl,
1203 .vidioc_s_ctrl = vidioc_s_ctrl,
1204 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1205 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1206 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1207 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1208 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
Janne Grunau76717b82009-03-18 20:57:04 -03001209 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1210 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
Janne Grunau9aba42e2009-03-18 18:10:04 -03001211};
1212
1213static void hdpvr_device_release(struct video_device *vdev)
1214{
1215 struct hdpvr_device *dev = video_get_drvdata(vdev);
1216
1217 hdpvr_delete(dev);
Hans Verkuilf2b305c2010-05-02 08:01:04 -03001218 mutex_lock(&dev->io_mutex);
1219 destroy_workqueue(dev->workqueue);
1220 mutex_unlock(&dev->io_mutex);
1221
1222 v4l2_device_unregister(&dev->v4l2_dev);
1223
1224 /* deregister I2C adapter */
1225#ifdef CONFIG_I2C
1226 mutex_lock(&dev->i2c_mutex);
1227 if (dev->i2c_adapter)
1228 i2c_del_adapter(dev->i2c_adapter);
1229 kfree(dev->i2c_adapter);
1230 dev->i2c_adapter = NULL;
1231 mutex_unlock(&dev->i2c_mutex);
1232#endif /* CONFIG_I2C */
1233
1234 kfree(dev->usbc_buf);
1235 kfree(dev);
Janne Grunau9aba42e2009-03-18 18:10:04 -03001236}
1237
1238static const struct video_device hdpvr_video_template = {
1239/* .type = VFL_TYPE_GRABBER, */
1240/* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1241 .fops = &hdpvr_fops,
1242 .release = hdpvr_device_release,
1243 .ioctl_ops = &hdpvr_ioctl_ops,
1244 .tvnorms =
1245 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1246 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1247 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1248 V4L2_STD_PAL_60,
Hans Verkuil99362e12009-08-07 07:10:52 -03001249 .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
1250 V4L2_STD_PAL_60,
Janne Grunau9aba42e2009-03-18 18:10:04 -03001251};
1252
Janne Grunaua50ab292009-03-26 14:40:55 -03001253int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1254 int devnum)
Janne Grunau9aba42e2009-03-18 18:10:04 -03001255{
1256 /* setup and register video device */
1257 dev->video_dev = video_device_alloc();
1258 if (!dev->video_dev) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -03001259 v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -03001260 goto error;
1261 }
1262
1263 *(dev->video_dev) = hdpvr_video_template;
1264 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
Janne Grunaua50ab292009-03-26 14:40:55 -03001265 dev->video_dev->parent = parent;
Janne Grunau9aba42e2009-03-18 18:10:04 -03001266 video_set_drvdata(dev->video_dev, dev);
1267
1268 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
Janne Grunau9ef77ad2009-03-27 20:09:40 -03001269 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
Janne Grunau9aba42e2009-03-18 18:10:04 -03001270 goto error;
1271 }
1272
1273 return 0;
1274error:
1275 return -ENOMEM;
1276}