GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / hdpvr / hdpvr-video.c
blob93f795960a9478d0efaa327259ea321be96780d7
1 /*
2 * Hauppauge HD PVR USB driver - video 4 linux 2 interface
4 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
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.
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>
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"
29 #define BULK_URB_TIMEOUT 1250 /* 1.25 seconds */
31 #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)); }
38 struct hdpvr_fh {
39 struct hdpvr_device *dev;
42 static uint list_size(struct list_head *list)
44 struct list_head *tmp;
45 uint count = 0;
47 list_for_each(tmp, list) {
48 count++;
51 return count;
54 /*=========================================================================*/
55 /* urb callback */
56 static void hdpvr_read_bulk_callback(struct urb *urb)
58 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
59 struct hdpvr_device *dev = buf->dev;
61 /* marking buffer as received and wake waiting */
62 buf->status = BUFSTAT_READY;
63 wake_up_interruptible(&dev->wait_data);
66 /*=========================================================================*/
67 /* bufffer bits */
69 /* function expects dev->io_mutex to be hold by caller */
70 int hdpvr_cancel_queue(struct hdpvr_device *dev)
72 struct hdpvr_buffer *buf;
74 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
75 usb_kill_urb(buf->urb);
76 buf->status = BUFSTAT_AVAILABLE;
79 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
81 return 0;
84 static int hdpvr_free_queue(struct list_head *q)
86 struct list_head *tmp;
87 struct list_head *p;
88 struct hdpvr_buffer *buf;
89 struct urb *urb;
91 for (p = q->next; p != q;) {
92 buf = list_entry(p, struct hdpvr_buffer, buff_list);
94 urb = buf->urb;
95 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
96 urb->transfer_buffer, urb->transfer_dma);
97 usb_free_urb(urb);
98 tmp = p->next;
99 list_del(p);
100 kfree(buf);
101 p = tmp;
104 return 0;
107 /* function expects dev->io_mutex to be hold by caller */
108 int hdpvr_free_buffers(struct hdpvr_device *dev)
110 hdpvr_cancel_queue(dev);
112 hdpvr_free_queue(&dev->free_buff_list);
113 hdpvr_free_queue(&dev->rec_buff_list);
115 return 0;
118 /* function expects dev->io_mutex to be hold by caller */
119 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
121 uint i;
122 int retval = -ENOMEM;
123 u8 *mem;
124 struct hdpvr_buffer *buf;
125 struct urb *urb;
127 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
128 "allocating %u buffers\n", count);
130 for (i = 0; i < count; i++) {
132 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
133 if (!buf) {
134 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
135 goto exit;
137 buf->dev = dev;
139 urb = usb_alloc_urb(0, GFP_KERNEL);
140 if (!urb) {
141 v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
142 goto exit_urb;
144 buf->urb = urb;
146 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
147 &urb->transfer_dma);
148 if (!mem) {
149 v4l2_err(&dev->v4l2_dev,
150 "cannot allocate usb transfer buffer\n");
151 goto exit_urb_buffer;
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);
160 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
161 buf->status = BUFSTAT_AVAILABLE;
162 list_add_tail(&buf->buff_list, &dev->free_buff_list);
164 return 0;
165 exit_urb_buffer:
166 usb_free_urb(urb);
167 exit_urb:
168 kfree(buf);
169 exit:
170 hdpvr_free_buffers(dev);
171 return retval;
174 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
176 struct hdpvr_buffer *buf;
177 struct urb *urb;
178 int ret = 0, err_count = 0;
180 mutex_lock(&dev->io_mutex);
182 while (dev->status == STATUS_STREAMING &&
183 !list_empty(&dev->free_buff_list)) {
185 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
186 buff_list);
187 if (buf->status != BUFSTAT_AVAILABLE) {
188 v4l2_err(&dev->v4l2_dev,
189 "buffer not marked as available\n");
190 ret = -EFAULT;
191 goto err;
194 urb = buf->urb;
195 urb->status = 0;
196 urb->actual_length = 0;
197 ret = usb_submit_urb(urb, GFP_KERNEL);
198 if (ret) {
199 v4l2_err(&dev->v4l2_dev,
200 "usb_submit_urb in %s returned %d\n",
201 __func__, ret);
202 if (++err_count > 2)
203 break;
204 continue;
206 buf->status = BUFSTAT_INPROGRESS;
207 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
209 err:
210 print_buffer_status();
211 mutex_unlock(&dev->io_mutex);
212 return ret;
215 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
217 struct hdpvr_buffer *buf;
219 mutex_lock(&dev->io_mutex);
221 if (list_empty(&dev->rec_buff_list)) {
222 mutex_unlock(&dev->io_mutex);
223 return NULL;
226 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
227 buff_list);
228 mutex_unlock(&dev->io_mutex);
230 return buf;
233 static void hdpvr_transmit_buffers(struct work_struct *work)
235 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
236 worker);
238 while (dev->status == STATUS_STREAMING) {
240 if (hdpvr_submit_buffers(dev)) {
241 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
242 goto error;
244 if (wait_event_interruptible(dev->wait_buffer,
245 !list_empty(&dev->free_buff_list) ||
246 dev->status != STATUS_STREAMING))
247 goto error;
250 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
251 "transmit worker exited\n");
252 return;
253 error:
254 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
255 "transmit buffers errored\n");
256 dev->status = STATUS_ERROR;
259 /* function expects dev->io_mutex to be hold by caller */
260 static int hdpvr_start_streaming(struct hdpvr_device *dev)
262 int ret;
263 struct hdpvr_video_info *vidinf;
265 if (dev->status == STATUS_STREAMING)
266 return 0;
267 else if (dev->status != STATUS_IDLE)
268 return -EAGAIN;
270 vidinf = get_video_info(dev);
272 if (vidinf) {
273 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
274 "video signal: %dx%d@%dhz\n", vidinf->width,
275 vidinf->height, vidinf->fps);
276 kfree(vidinf);
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);
282 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
283 "encoder start control request returned %d\n", ret);
285 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
287 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
288 queue_work(dev->workqueue, &dev->worker);
290 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
291 "streaming started\n");
292 dev->status = STATUS_STREAMING;
294 return 0;
296 msleep(250);
297 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
298 "no video signal at input %d\n", dev->options.video_input);
299 return -EAGAIN;
303 /* function expects dev->io_mutex to be hold by caller */
304 static int hdpvr_stop_streaming(struct hdpvr_device *dev)
306 int actual_length;
307 uint c = 0;
308 u8 *buf;
310 if (dev->status == STATUS_IDLE)
311 return 0;
312 else if (dev->status != STATUS_STREAMING)
313 return -EAGAIN;
315 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");
321 dev->status = STATUS_SHUTTING_DOWN;
322 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
323 mutex_unlock(&dev->io_mutex);
325 wake_up_interruptible(&dev->wait_buffer);
326 msleep(50);
328 flush_workqueue(dev->workqueue);
330 mutex_lock(&dev->io_mutex);
331 /* kill the still outstanding urbs */
332 hdpvr_cancel_queue(dev);
334 /* 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);
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);
351 dev->status = STATUS_IDLE;
353 return 0;
357 /*=======================================================================*/
359 * video 4 linux 2 file operations
362 static int hdpvr_open(struct file *file)
364 struct hdpvr_device *dev;
365 struct hdpvr_fh *fh;
366 int retval = -ENOMEM;
368 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
369 if (!dev) {
370 pr_err("open failing with with ENODEV\n");
371 retval = -ENODEV;
372 goto err;
375 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
376 if (!fh) {
377 v4l2_err(&dev->v4l2_dev, "Out of memory\n");
378 goto err;
380 /* lock the device to allow correctly handling errors
381 * in resumption */
382 mutex_lock(&dev->io_mutex);
383 dev->open_count++;
384 mutex_unlock(&dev->io_mutex);
386 fh->dev = dev;
388 /* save our object in the file's private structure */
389 file->private_data = fh;
391 retval = 0;
392 err:
393 return retval;
396 static int hdpvr_release(struct file *file)
398 struct hdpvr_fh *fh = file->private_data;
399 struct hdpvr_device *dev = fh->dev;
401 if (!dev)
402 return -ENODEV;
404 mutex_lock(&dev->io_mutex);
405 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
406 hdpvr_stop_streaming(dev);
408 mutex_unlock(&dev->io_mutex);
410 return 0;
414 * hdpvr_v4l2_read()
415 * will allocate buffers when called for the first time
417 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
418 loff_t *pos)
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;
427 if (*pos)
428 return -ESPIPE;
430 if (!dev)
431 return -ENODEV;
433 mutex_lock(&dev->io_mutex);
434 if (dev->status == STATUS_IDLE) {
435 if (hdpvr_start_streaming(dev)) {
436 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
437 "start_streaming failed\n");
438 ret = -EIO;
439 msleep(200);
440 dev->status = STATUS_IDLE;
441 mutex_unlock(&dev->io_mutex);
442 goto err;
444 print_buffer_status();
446 mutex_unlock(&dev->io_mutex);
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;
455 buf = hdpvr_get_next_buffer(dev);
457 while (count > 0 && buf) {
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;
468 if (wait_event_interruptible(dev->wait_data,
469 buf->status == BUFSTAT_READY)) {
470 ret = -ERESTARTSYS;
471 goto err;
475 if (buf->status != BUFSTAT_READY)
476 break;
478 /* set remaining bytes to copy */
479 urb = buf->urb;
480 rem = urb->actual_length - buf->pos;
481 cnt = rem > count ? count : rem;
483 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
484 cnt)) {
485 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
486 if (!ret)
487 ret = -EFAULT;
488 goto err;
491 buf->pos += cnt;
492 count -= cnt;
493 buffer += cnt;
494 ret += cnt;
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;
502 list_move_tail(&buf->buff_list, &dev->free_buff_list);
504 print_buffer_status();
506 mutex_unlock(&dev->io_mutex);
508 wake_up_interruptible(&dev->wait_buffer);
510 buf = hdpvr_get_next_buffer(dev);
513 err:
514 if (!ret && !buf)
515 ret = -EAGAIN;
516 return ret;
519 static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
521 struct hdpvr_buffer *buf = NULL;
522 struct hdpvr_fh *fh = filp->private_data;
523 struct hdpvr_device *dev = fh->dev;
524 unsigned int mask = 0;
526 mutex_lock(&dev->io_mutex);
528 if (!video_is_registered(dev->video_dev)) {
529 mutex_unlock(&dev->io_mutex);
530 return -EIO;
533 if (dev->status == STATUS_IDLE) {
534 if (hdpvr_start_streaming(dev)) {
535 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
536 "start_streaming failed\n");
537 dev->status = STATUS_IDLE;
540 print_buffer_status();
542 mutex_unlock(&dev->io_mutex);
544 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);
550 if (buf && buf->status == BUFSTAT_READY)
551 mask |= POLLIN | POLLRDNORM;
553 return mask;
557 static 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,
563 .unlocked_ioctl = video_ioctl2,
566 /*=======================================================================*/
568 * V4L2 ioctl handling
571 static int vidioc_querycap(struct file *file, void *priv,
572 struct v4l2_capability *cap)
574 struct hdpvr_device *dev = video_drvdata(file);
576 strcpy(cap->driver, "hdpvr");
577 strcpy(cap->card, "Hauppauge HD PVR");
578 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;
586 static int vidioc_s_std(struct file *file, void *private_data,
587 v4l2_std_id *std)
589 struct hdpvr_fh *fh = file->private_data;
590 struct hdpvr_device *dev = fh->dev;
591 u8 std_type = 1;
593 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
594 std_type = 0;
596 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
599 static const char *iname[] = {
600 [HDPVR_COMPONENT] = "Component",
601 [HDPVR_SVIDEO] = "S-Video",
602 [HDPVR_COMPOSITE] = "Composite",
605 static int vidioc_enum_input(struct file *file, void *priv,
606 struct v4l2_input *i)
608 struct hdpvr_fh *fh = file->private_data;
609 struct hdpvr_device *dev = fh->dev;
610 unsigned int n;
612 n = i->index;
613 if (n >= HDPVR_VIDEO_INPUTS)
614 return -EINVAL;
616 i->type = V4L2_INPUT_TYPE_CAMERA;
618 strncpy(i->name, iname[n], sizeof(i->name) - 1);
619 i->name[sizeof(i->name) - 1] = '\0';
621 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
623 i->std = dev->video_dev->tvnorms;
625 return 0;
628 static int vidioc_s_input(struct file *file, void *private_data,
629 unsigned int index)
631 struct hdpvr_fh *fh = file->private_data;
632 struct hdpvr_device *dev = fh->dev;
633 int retval;
635 if (index >= HDPVR_VIDEO_INPUTS)
636 return -EINVAL;
638 if (dev->status != STATUS_IDLE)
639 return -EAGAIN;
641 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
642 if (!retval)
643 dev->options.video_input = index;
645 return retval;
648 static int vidioc_g_input(struct file *file, void *private_data,
649 unsigned int *index)
651 struct hdpvr_fh *fh = file->private_data;
652 struct hdpvr_device *dev = fh->dev;
654 *index = dev->options.video_input;
655 return 0;
659 static const char *audio_iname[] = {
660 [HDPVR_RCA_FRONT] = "RCA front",
661 [HDPVR_RCA_BACK] = "RCA back",
662 [HDPVR_SPDIF] = "SPDIF",
665 static int vidioc_enumaudio(struct file *file, void *priv,
666 struct v4l2_audio *audio)
668 unsigned int n;
670 n = audio->index;
671 if (n >= HDPVR_AUDIO_INPUTS)
672 return -EINVAL;
674 audio->capability = V4L2_AUDCAP_STEREO;
676 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
677 audio->name[sizeof(audio->name) - 1] = '\0';
679 return 0;
682 static int vidioc_s_audio(struct file *file, void *private_data,
683 struct v4l2_audio *audio)
685 struct hdpvr_fh *fh = file->private_data;
686 struct hdpvr_device *dev = fh->dev;
687 int retval;
689 if (audio->index >= HDPVR_AUDIO_INPUTS)
690 return -EINVAL;
692 if (dev->status != STATUS_IDLE)
693 return -EAGAIN;
695 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
696 if (!retval)
697 dev->options.audio_input = audio->index;
699 return retval;
702 static int vidioc_g_audio(struct file *file, void *private_data,
703 struct v4l2_audio *audio)
705 struct hdpvr_fh *fh = file->private_data;
706 struct hdpvr_device *dev = fh->dev;
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;
715 static 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,
728 static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
729 int ac3)
731 int err;
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);
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);
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;
778 static int vidioc_queryctrl(struct file *file, void *private_data,
779 struct v4l2_queryctrl *qc)
781 struct hdpvr_fh *fh = file->private_data;
782 struct hdpvr_device *dev = fh->dev;
783 int i, next;
784 u32 id = qc->id;
786 memset(qc, 0, sizeof(*qc));
788 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
789 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
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;
799 if (qc->id == supported_v4l2_ctrls[i])
800 return fill_queryctrl(&dev->options, qc,
801 dev->flags & HDPVR_FLAG_AC3_CAP);
803 if (qc->id < supported_v4l2_ctrls[i])
804 break;
807 return -EINVAL;
810 static int vidioc_g_ctrl(struct file *file, void *private_data,
811 struct v4l2_control *ctrl)
813 struct hdpvr_fh *fh = file->private_data;
814 struct hdpvr_device *dev = fh->dev;
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;
835 return 0;
838 static int vidioc_s_ctrl(struct file *file, void *private_data,
839 struct v4l2_control *ctrl)
841 struct hdpvr_fh *fh = file->private_data;
842 struct hdpvr_device *dev = fh->dev;
843 int retval;
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;
875 return retval;
879 static int hdpvr_get_ctrl(struct hdpvr_options *opt,
880 struct v4l2_ext_control *ctrl)
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;
909 return 0;
912 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
913 struct v4l2_ext_controls *ctrls)
915 struct hdpvr_fh *fh = file->private_data;
916 struct hdpvr_device *dev = fh->dev;
917 int i, err = 0;
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;
923 err = hdpvr_get_ctrl(&dev->options, ctrl);
924 if (err) {
925 ctrls->error_idx = i;
926 break;
929 return err;
933 return -EINVAL;
937 static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
939 int ret = -EINVAL;
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:
962 uint bitrate = ctrl->value / 100000;
963 if (bitrate >= 10 && bitrate <= 135)
964 ret = 0;
965 break;
967 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
969 uint peak_bitrate = ctrl->value / 100000;
970 if (peak_bitrate >= 10 && peak_bitrate <= 202)
971 ret = 0;
972 break;
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;
981 return 0;
984 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
985 struct v4l2_ext_controls *ctrls)
987 struct hdpvr_fh *fh = file->private_data;
988 struct hdpvr_device *dev = fh->dev;
989 int i, err = 0;
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;
995 err = hdpvr_try_ctrl(ctrl,
996 dev->flags & HDPVR_FLAG_AC3_CAP);
997 if (err) {
998 ctrls->error_idx = i;
999 break;
1002 return err;
1005 return -EINVAL;
1009 static int hdpvr_set_ctrl(struct hdpvr_device *dev,
1010 struct v4l2_ext_control *ctrl)
1012 struct hdpvr_options *opt = &dev->options;
1013 int ret = 0;
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);
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);
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);
1050 break;
1051 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1052 uint bitrate = ctrl->value / 100000;
1054 opt->bitrate = bitrate;
1055 if (bitrate >= opt->peak_bitrate)
1056 opt->peak_bitrate = bitrate+1;
1058 hdpvr_set_bitrate(dev);
1059 break;
1061 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1062 uint peak_bitrate = ctrl->value / 100000;
1064 if (opt->bitrate_mode == HDPVR_CONSTANT)
1065 break;
1067 if (opt->bitrate < peak_bitrate) {
1068 opt->peak_bitrate = peak_bitrate;
1069 hdpvr_set_bitrate(dev);
1070 } else
1071 ret = -EINVAL;
1072 break;
1074 case V4L2_CID_MPEG_STREAM_TYPE:
1075 break;
1076 default:
1077 return -EINVAL;
1079 return ret;
1082 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1083 struct v4l2_ext_controls *ctrls)
1085 struct hdpvr_fh *fh = file->private_data;
1086 struct hdpvr_device *dev = fh->dev;
1087 int i, err = 0;
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;
1093 err = hdpvr_try_ctrl(ctrl,
1094 dev->flags & HDPVR_FLAG_AC3_CAP);
1095 if (err) {
1096 ctrls->error_idx = i;
1097 break;
1099 err = hdpvr_set_ctrl(dev, ctrl);
1100 if (err) {
1101 ctrls->error_idx = i;
1102 break;
1105 return err;
1109 return -EINVAL;
1112 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1113 struct v4l2_fmtdesc *f)
1116 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1117 return -EINVAL;
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;
1123 return 0;
1126 static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1127 struct v4l2_format *f)
1129 struct hdpvr_fh *fh = file->private_data;
1130 struct hdpvr_device *dev = fh->dev;
1131 struct hdpvr_video_info *vid_info;
1133 if (!dev)
1134 return -ENODEV;
1136 vid_info = get_video_info(dev);
1137 if (!vid_info)
1138 return -EFAULT;
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;
1149 kfree(vid_info);
1150 return 0;
1153 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1154 struct v4l2_encoder_cmd *a)
1156 struct hdpvr_fh *fh = filp->private_data;
1157 struct hdpvr_device *dev = fh->dev;
1158 int res;
1160 mutex_lock(&dev->io_mutex);
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:
1172 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1173 "Unsupported encoder cmd %d\n", a->cmd);
1174 res = -EINVAL;
1176 mutex_unlock(&dev->io_mutex);
1177 return res;
1180 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1181 struct v4l2_encoder_cmd *a)
1183 switch (a->cmd) {
1184 case V4L2_ENC_CMD_START:
1185 case V4L2_ENC_CMD_STOP:
1186 return 0;
1187 default:
1188 return -EINVAL;
1192 static 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,
1209 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1210 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1213 static void hdpvr_device_release(struct video_device *vdev)
1215 struct hdpvr_device *dev = video_get_drvdata(vdev);
1217 hdpvr_delete(dev);
1218 mutex_lock(&dev->io_mutex);
1219 destroy_workqueue(dev->workqueue);
1220 mutex_unlock(&dev->io_mutex);
1222 v4l2_device_unregister(&dev->v4l2_dev);
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 */
1234 kfree(dev->usbc_buf);
1235 kfree(dev);
1238 static 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,
1249 .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
1250 V4L2_STD_PAL_60,
1253 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1254 int devnum)
1256 /* setup and register video device */
1257 dev->video_dev = video_device_alloc();
1258 if (!dev->video_dev) {
1259 v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
1260 goto error;
1263 *(dev->video_dev) = hdpvr_video_template;
1264 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1265 dev->video_dev->parent = parent;
1266 video_set_drvdata(dev->video_dev, dev);
1268 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1269 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
1270 goto error;
1273 return 0;
1274 error:
1275 return -ENOMEM;