[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / media / video / hdpvr / hdpvr-video.c
blob2eb9dc2ebe59797a9ffeac30eea0dbbd7eeae504
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_buffer_free(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;
144 buf->urb = urb;
146 mem = usb_buffer_alloc(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;
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->status = BUFSTAT_AVAILABLE;
161 list_add_tail(&buf->buff_list, &dev->free_buff_list);
163 return 0;
164 exit:
165 hdpvr_free_buffers(dev);
166 return retval;
169 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
171 struct hdpvr_buffer *buf;
172 struct urb *urb;
173 int ret = 0, err_count = 0;
175 mutex_lock(&dev->io_mutex);
177 while (dev->status == STATUS_STREAMING &&
178 !list_empty(&dev->free_buff_list)) {
180 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
181 buff_list);
182 if (buf->status != BUFSTAT_AVAILABLE) {
183 v4l2_err(&dev->v4l2_dev,
184 "buffer not marked as available\n");
185 ret = -EFAULT;
186 goto err;
189 urb = buf->urb;
190 urb->status = 0;
191 urb->actual_length = 0;
192 ret = usb_submit_urb(urb, GFP_KERNEL);
193 if (ret) {
194 v4l2_err(&dev->v4l2_dev,
195 "usb_submit_urb in %s returned %d\n",
196 __func__, ret);
197 if (++err_count > 2)
198 break;
199 continue;
201 buf->status = BUFSTAT_INPROGRESS;
202 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
204 err:
205 print_buffer_status();
206 mutex_unlock(&dev->io_mutex);
207 return ret;
210 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
212 struct hdpvr_buffer *buf;
214 mutex_lock(&dev->io_mutex);
216 if (list_empty(&dev->rec_buff_list)) {
217 mutex_unlock(&dev->io_mutex);
218 return NULL;
221 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
222 buff_list);
223 mutex_unlock(&dev->io_mutex);
225 return buf;
228 static void hdpvr_transmit_buffers(struct work_struct *work)
230 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
231 worker);
233 while (dev->status == STATUS_STREAMING) {
235 if (hdpvr_submit_buffers(dev)) {
236 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
237 goto error;
239 if (wait_event_interruptible(dev->wait_buffer,
240 !list_empty(&dev->free_buff_list) ||
241 dev->status != STATUS_STREAMING))
242 goto error;
245 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
246 "transmit worker exited\n");
247 return;
248 error:
249 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
250 "transmit buffers errored\n");
251 dev->status = STATUS_ERROR;
254 /* function expects dev->io_mutex to be hold by caller */
255 static int hdpvr_start_streaming(struct hdpvr_device *dev)
257 int ret;
258 struct hdpvr_video_info *vidinf;
260 if (dev->status == STATUS_STREAMING)
261 return 0;
262 else if (dev->status != STATUS_IDLE)
263 return -EAGAIN;
265 vidinf = get_video_info(dev);
267 if (vidinf) {
268 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
269 "video signal: %dx%d@%dhz\n", vidinf->width,
270 vidinf->height, vidinf->fps);
271 kfree(vidinf);
273 /* start streaming 2 request */
274 ret = usb_control_msg(dev->udev,
275 usb_sndctrlpipe(dev->udev, 0),
276 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
277 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
278 "encoder start control request returned %d\n", ret);
280 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
282 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
283 queue_work(dev->workqueue, &dev->worker);
285 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
286 "streaming started\n");
287 dev->status = STATUS_STREAMING;
289 return 0;
291 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;
298 /* function expects dev->io_mutex to be hold by caller */
299 static int hdpvr_stop_streaming(struct hdpvr_device *dev)
301 uint actual_length, c = 0;
302 u8 *buf;
304 if (dev->status == STATUS_IDLE)
305 return 0;
306 else if (dev->status != STATUS_STREAMING)
307 return -EAGAIN;
309 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
310 if (!buf)
311 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
312 "for emptying the internal device buffer. "
313 "Next capture start will be slow\n");
315 dev->status = STATUS_SHUTTING_DOWN;
316 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
317 mutex_unlock(&dev->io_mutex);
319 wake_up_interruptible(&dev->wait_buffer);
320 msleep(50);
322 flush_workqueue(dev->workqueue);
324 mutex_lock(&dev->io_mutex);
325 /* kill the still outstanding urbs */
326 hdpvr_cancel_queue(dev);
328 /* emptying the device buffer beforeshutting it down */
329 while (buf && ++c < 500 &&
330 !usb_bulk_msg(dev->udev,
331 usb_rcvbulkpipe(dev->udev,
332 dev->bulk_in_endpointAddr),
333 buf, dev->bulk_in_size, &actual_length,
334 BULK_URB_TIMEOUT)) {
335 /* wait */
336 msleep(5);
337 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
338 "%2d: got %d bytes\n", c, actual_length);
340 kfree(buf);
341 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
342 "used %d urbs to empty device buffers\n", c-1);
343 msleep(10);
345 dev->status = STATUS_IDLE;
347 return 0;
351 /*=======================================================================*/
353 * video 4 linux 2 file operations
356 static int hdpvr_open(struct file *file)
358 struct hdpvr_device *dev;
359 struct hdpvr_fh *fh;
360 int retval = -ENOMEM;
362 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
363 if (!dev) {
364 v4l2_err(&dev->v4l2_dev, "open failing with with ENODEV\n");
365 retval = -ENODEV;
366 goto err;
369 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
370 if (!fh) {
371 v4l2_err(&dev->v4l2_dev, "Out of memory\n");
372 goto err;
374 /* lock the device to allow correctly handling errors
375 * in resumption */
376 mutex_lock(&dev->io_mutex);
377 dev->open_count++;
378 mutex_unlock(&dev->io_mutex);
380 fh->dev = dev;
382 /* save our object in the file's private structure */
383 file->private_data = fh;
385 retval = 0;
386 err:
387 return retval;
390 static int hdpvr_release(struct file *file)
392 struct hdpvr_fh *fh = (struct hdpvr_fh *)file->private_data;
393 struct hdpvr_device *dev = fh->dev;
395 if (!dev)
396 return -ENODEV;
398 mutex_lock(&dev->io_mutex);
399 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
400 hdpvr_stop_streaming(dev);
402 mutex_unlock(&dev->io_mutex);
404 return 0;
408 * hdpvr_v4l2_read()
409 * will allocate buffers when called for the first time
411 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
412 loff_t *pos)
414 struct hdpvr_fh *fh = file->private_data;
415 struct hdpvr_device *dev = fh->dev;
416 struct hdpvr_buffer *buf = NULL;
417 struct urb *urb;
418 unsigned int ret = 0;
419 int rem, cnt;
421 if (*pos)
422 return -ESPIPE;
424 if (!dev)
425 return -ENODEV;
427 mutex_lock(&dev->io_mutex);
428 if (dev->status == STATUS_IDLE) {
429 if (hdpvr_start_streaming(dev)) {
430 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
431 "start_streaming failed\n");
432 ret = -EIO;
433 msleep(200);
434 dev->status = STATUS_IDLE;
435 mutex_unlock(&dev->io_mutex);
436 goto err;
438 print_buffer_status();
440 mutex_unlock(&dev->io_mutex);
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;
449 buf = hdpvr_get_next_buffer(dev);
451 while (count > 0 && buf) {
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;
462 if (wait_event_interruptible(dev->wait_data,
463 buf->status == BUFSTAT_READY)) {
464 ret = -ERESTARTSYS;
465 goto err;
469 if (buf->status != BUFSTAT_READY)
470 break;
472 /* set remaining bytes to copy */
473 urb = buf->urb;
474 rem = urb->actual_length - buf->pos;
475 cnt = rem > count ? count : rem;
477 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
478 cnt)) {
479 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
480 if (!ret)
481 ret = -EFAULT;
482 goto err;
485 buf->pos += cnt;
486 count -= cnt;
487 buffer += cnt;
488 ret += cnt;
490 /* finished, take next buffer */
491 if (buf->pos == urb->actual_length) {
492 mutex_lock(&dev->io_mutex);
493 buf->pos = 0;
494 buf->status = BUFSTAT_AVAILABLE;
496 list_move_tail(&buf->buff_list, &dev->free_buff_list);
498 print_buffer_status();
500 mutex_unlock(&dev->io_mutex);
502 wake_up_interruptible(&dev->wait_buffer);
504 buf = hdpvr_get_next_buffer(dev);
507 err:
508 if (!ret && !buf)
509 ret = -EAGAIN;
510 return ret;
513 static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
515 struct hdpvr_buffer *buf = NULL;
516 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
517 struct hdpvr_device *dev = fh->dev;
518 unsigned int mask = 0;
520 mutex_lock(&dev->io_mutex);
522 if (video_is_unregistered(dev->video_dev)) {
523 mutex_unlock(&dev->io_mutex);
524 return -EIO;
527 if (dev->status == STATUS_IDLE) {
528 if (hdpvr_start_streaming(dev)) {
529 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
530 "start_streaming failed\n");
531 dev->status = STATUS_IDLE;
534 print_buffer_status();
536 mutex_unlock(&dev->io_mutex);
538 buf = hdpvr_get_next_buffer(dev);
539 /* only wait if no data is available */
540 if (!buf || buf->status != BUFSTAT_READY) {
541 poll_wait(filp, &dev->wait_data, wait);
542 buf = hdpvr_get_next_buffer(dev);
544 if (buf && buf->status == BUFSTAT_READY)
545 mask |= POLLIN | POLLRDNORM;
547 return mask;
551 static const struct v4l2_file_operations hdpvr_fops = {
552 .owner = THIS_MODULE,
553 .open = hdpvr_open,
554 .release = hdpvr_release,
555 .read = hdpvr_read,
556 .poll = hdpvr_poll,
557 .unlocked_ioctl = video_ioctl2,
560 /*=======================================================================*/
562 * V4L2 ioctl handling
565 static int vidioc_querycap(struct file *file, void *priv,
566 struct v4l2_capability *cap)
568 struct hdpvr_device *dev = video_drvdata(file);
570 strcpy(cap->driver, "hdpvr");
571 strcpy(cap->card, "Haupauge HD PVR");
572 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
573 cap->version = HDPVR_VERSION;
574 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
575 V4L2_CAP_AUDIO |
576 V4L2_CAP_READWRITE;
577 return 0;
580 static int vidioc_s_std(struct file *file, void *private_data,
581 v4l2_std_id *std)
583 struct hdpvr_fh *fh = file->private_data;
584 struct hdpvr_device *dev = fh->dev;
585 u8 std_type = 1;
587 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
588 std_type = 0;
590 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
593 static const char *iname[] = {
594 [HDPVR_COMPONENT] = "Component",
595 [HDPVR_SVIDEO] = "S-Video",
596 [HDPVR_COMPOSITE] = "Composite",
599 static int vidioc_enum_input(struct file *file, void *priv,
600 struct v4l2_input *i)
602 struct hdpvr_fh *fh = file->private_data;
603 struct hdpvr_device *dev = fh->dev;
604 unsigned int n;
606 n = i->index;
607 if (n >= HDPVR_VIDEO_INPUTS)
608 return -EINVAL;
610 i->type = V4L2_INPUT_TYPE_CAMERA;
612 strncpy(i->name, iname[n], sizeof(i->name) - 1);
613 i->name[sizeof(i->name) - 1] = '\0';
615 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
617 i->std = dev->video_dev->tvnorms;
619 return 0;
622 static int vidioc_s_input(struct file *file, void *private_data,
623 unsigned int index)
625 struct hdpvr_fh *fh = file->private_data;
626 struct hdpvr_device *dev = fh->dev;
627 int retval;
629 if (index >= HDPVR_VIDEO_INPUTS)
630 return -EINVAL;
632 if (dev->status != STATUS_IDLE)
633 return -EAGAIN;
635 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
636 if (!retval)
637 dev->options.video_input = index;
639 return retval;
642 static int vidioc_g_input(struct file *file, void *private_data,
643 unsigned int *index)
645 struct hdpvr_fh *fh = file->private_data;
646 struct hdpvr_device *dev = fh->dev;
648 *index = dev->options.video_input;
649 return 0;
653 static const char *audio_iname[] = {
654 [HDPVR_RCA_FRONT] = "RCA front",
655 [HDPVR_RCA_BACK] = "RCA back",
656 [HDPVR_SPDIF] = "SPDIF",
659 static int vidioc_enumaudio(struct file *file, void *priv,
660 struct v4l2_audio *audio)
662 unsigned int n;
664 n = audio->index;
665 if (n >= HDPVR_AUDIO_INPUTS)
666 return -EINVAL;
668 audio->capability = V4L2_AUDCAP_STEREO;
670 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
671 audio->name[sizeof(audio->name) - 1] = '\0';
673 return 0;
676 static int vidioc_s_audio(struct file *file, void *private_data,
677 struct v4l2_audio *audio)
679 struct hdpvr_fh *fh = file->private_data;
680 struct hdpvr_device *dev = fh->dev;
681 int retval;
683 if (audio->index >= HDPVR_AUDIO_INPUTS)
684 return -EINVAL;
686 if (dev->status != STATUS_IDLE)
687 return -EAGAIN;
689 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
690 if (!retval)
691 dev->options.audio_input = audio->index;
693 return retval;
696 static int vidioc_g_audio(struct file *file, void *private_data,
697 struct v4l2_audio *audio)
699 struct hdpvr_fh *fh = file->private_data;
700 struct hdpvr_device *dev = fh->dev;
702 audio->index = dev->options.audio_input;
703 audio->capability = V4L2_AUDCAP_STEREO;
704 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
705 audio->name[sizeof(audio->name) - 1] = '\0';
706 return 0;
709 static const s32 supported_v4l2_ctrls[] = {
710 V4L2_CID_BRIGHTNESS,
711 V4L2_CID_CONTRAST,
712 V4L2_CID_SATURATION,
713 V4L2_CID_HUE,
714 V4L2_CID_SHARPNESS,
715 V4L2_CID_MPEG_AUDIO_ENCODING,
716 V4L2_CID_MPEG_VIDEO_ENCODING,
717 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
718 V4L2_CID_MPEG_VIDEO_BITRATE,
719 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
722 static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
723 int ac3)
725 int err;
727 switch (qc->id) {
728 case V4L2_CID_BRIGHTNESS:
729 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
730 case V4L2_CID_CONTRAST:
731 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
732 case V4L2_CID_SATURATION:
733 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
734 case V4L2_CID_HUE:
735 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
736 case V4L2_CID_SHARPNESS:
737 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
738 case V4L2_CID_MPEG_AUDIO_ENCODING:
739 return v4l2_ctrl_query_fill(
740 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
741 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
742 : V4L2_MPEG_AUDIO_ENCODING_AAC,
743 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
744 case V4L2_CID_MPEG_VIDEO_ENCODING:
745 return v4l2_ctrl_query_fill(
746 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
747 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
748 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
750 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
751 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
752 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
753 return v4l2_ctrl_query_fill(
754 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
755 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
756 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
758 case V4L2_CID_MPEG_VIDEO_BITRATE:
759 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
760 6500000);
761 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
762 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
763 9000000);
764 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
765 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
766 return err;
767 default:
768 return -EINVAL;
772 static int vidioc_queryctrl(struct file *file, void *private_data,
773 struct v4l2_queryctrl *qc)
775 struct hdpvr_fh *fh = file->private_data;
776 struct hdpvr_device *dev = fh->dev;
777 int i, next;
778 u32 id = qc->id;
780 memset(qc, 0, sizeof(*qc));
782 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
783 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
785 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
786 if (next) {
787 if (qc->id < supported_v4l2_ctrls[i])
788 qc->id = supported_v4l2_ctrls[i];
789 else
790 continue;
793 if (qc->id == supported_v4l2_ctrls[i])
794 return fill_queryctrl(&dev->options, qc,
795 dev->flags & HDPVR_FLAG_AC3_CAP);
797 if (qc->id < supported_v4l2_ctrls[i])
798 break;
801 return -EINVAL;
804 static int vidioc_g_ctrl(struct file *file, void *private_data,
805 struct v4l2_control *ctrl)
807 struct hdpvr_fh *fh = file->private_data;
808 struct hdpvr_device *dev = fh->dev;
810 switch (ctrl->id) {
811 case V4L2_CID_BRIGHTNESS:
812 ctrl->value = dev->options.brightness;
813 break;
814 case V4L2_CID_CONTRAST:
815 ctrl->value = dev->options.contrast;
816 break;
817 case V4L2_CID_SATURATION:
818 ctrl->value = dev->options.saturation;
819 break;
820 case V4L2_CID_HUE:
821 ctrl->value = dev->options.hue;
822 break;
823 case V4L2_CID_SHARPNESS:
824 ctrl->value = dev->options.sharpness;
825 break;
826 default:
827 return -EINVAL;
829 return 0;
832 static int vidioc_s_ctrl(struct file *file, void *private_data,
833 struct v4l2_control *ctrl)
835 struct hdpvr_fh *fh = file->private_data;
836 struct hdpvr_device *dev = fh->dev;
837 int retval;
839 switch (ctrl->id) {
840 case V4L2_CID_BRIGHTNESS:
841 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
842 if (!retval)
843 dev->options.brightness = ctrl->value;
844 break;
845 case V4L2_CID_CONTRAST:
846 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
847 if (!retval)
848 dev->options.contrast = ctrl->value;
849 break;
850 case V4L2_CID_SATURATION:
851 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
852 if (!retval)
853 dev->options.saturation = ctrl->value;
854 break;
855 case V4L2_CID_HUE:
856 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
857 if (!retval)
858 dev->options.hue = ctrl->value;
859 break;
860 case V4L2_CID_SHARPNESS:
861 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
862 if (!retval)
863 dev->options.sharpness = ctrl->value;
864 break;
865 default:
866 return -EINVAL;
869 return retval;
873 static int hdpvr_get_ctrl(struct hdpvr_options *opt,
874 struct v4l2_ext_control *ctrl)
876 switch (ctrl->id) {
877 case V4L2_CID_MPEG_AUDIO_ENCODING:
878 ctrl->value = opt->audio_codec;
879 break;
880 case V4L2_CID_MPEG_VIDEO_ENCODING:
881 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
882 break;
883 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
884 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
885 /* break; */
886 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
887 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
888 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
889 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
890 break;
891 case V4L2_CID_MPEG_VIDEO_BITRATE:
892 ctrl->value = opt->bitrate * 100000;
893 break;
894 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
895 ctrl->value = opt->peak_bitrate * 100000;
896 break;
897 case V4L2_CID_MPEG_STREAM_TYPE:
898 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
899 break;
900 default:
901 return -EINVAL;
903 return 0;
906 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
907 struct v4l2_ext_controls *ctrls)
909 struct hdpvr_fh *fh = file->private_data;
910 struct hdpvr_device *dev = fh->dev;
911 int i, err = 0;
913 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
914 for (i = 0; i < ctrls->count; i++) {
915 struct v4l2_ext_control *ctrl = ctrls->controls + i;
917 err = hdpvr_get_ctrl(&dev->options, ctrl);
918 if (err) {
919 ctrls->error_idx = i;
920 break;
923 return err;
927 return -EINVAL;
931 static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
933 int ret = -EINVAL;
935 switch (ctrl->id) {
936 case V4L2_CID_MPEG_AUDIO_ENCODING:
937 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
938 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
939 ret = 0;
940 break;
941 case V4L2_CID_MPEG_VIDEO_ENCODING:
942 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
943 ret = 0;
944 break;
945 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
946 /* if (ctrl->value == 0 || ctrl->value == 128) */
947 /* ret = 0; */
948 /* break; */
949 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
950 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
951 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
952 ret = 0;
953 break;
954 case V4L2_CID_MPEG_VIDEO_BITRATE:
956 uint bitrate = ctrl->value / 100000;
957 if (bitrate >= 10 && bitrate <= 135)
958 ret = 0;
959 break;
961 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
963 uint peak_bitrate = ctrl->value / 100000;
964 if (peak_bitrate >= 10 && peak_bitrate <= 202)
965 ret = 0;
966 break;
968 case V4L2_CID_MPEG_STREAM_TYPE:
969 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
970 ret = 0;
971 break;
972 default:
973 return -EINVAL;
975 return 0;
978 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
979 struct v4l2_ext_controls *ctrls)
981 struct hdpvr_fh *fh = file->private_data;
982 struct hdpvr_device *dev = fh->dev;
983 int i, err = 0;
985 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
986 for (i = 0; i < ctrls->count; i++) {
987 struct v4l2_ext_control *ctrl = ctrls->controls + i;
989 err = hdpvr_try_ctrl(ctrl,
990 dev->flags & HDPVR_FLAG_AC3_CAP);
991 if (err) {
992 ctrls->error_idx = i;
993 break;
996 return err;
999 return -EINVAL;
1003 static int hdpvr_set_ctrl(struct hdpvr_device *dev,
1004 struct v4l2_ext_control *ctrl)
1006 struct hdpvr_options *opt = &dev->options;
1007 int ret = 0;
1009 switch (ctrl->id) {
1010 case V4L2_CID_MPEG_AUDIO_ENCODING:
1011 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
1012 opt->audio_codec = ctrl->value;
1013 ret = hdpvr_set_audio(dev, opt->audio_input,
1014 opt->audio_codec);
1016 break;
1017 case V4L2_CID_MPEG_VIDEO_ENCODING:
1018 break;
1019 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1020 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1021 /* opt->gop_mode |= 0x2; */
1022 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1023 /* opt->gop_mode); */
1024 /* } */
1025 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1026 /* opt->gop_mode &= ~0x2; */
1027 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1028 /* opt->gop_mode); */
1029 /* } */
1030 /* break; */
1031 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1032 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1033 opt->bitrate_mode != HDPVR_CONSTANT) {
1034 opt->bitrate_mode = HDPVR_CONSTANT;
1035 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1036 opt->bitrate_mode);
1038 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1039 opt->bitrate_mode == HDPVR_CONSTANT) {
1040 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1041 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1042 opt->bitrate_mode);
1044 break;
1045 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1046 uint bitrate = ctrl->value / 100000;
1048 opt->bitrate = bitrate;
1049 if (bitrate >= opt->peak_bitrate)
1050 opt->peak_bitrate = bitrate+1;
1052 hdpvr_set_bitrate(dev);
1053 break;
1055 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1056 uint peak_bitrate = ctrl->value / 100000;
1058 if (opt->bitrate_mode == HDPVR_CONSTANT)
1059 break;
1061 if (opt->bitrate < peak_bitrate) {
1062 opt->peak_bitrate = peak_bitrate;
1063 hdpvr_set_bitrate(dev);
1064 } else
1065 ret = -EINVAL;
1066 break;
1068 case V4L2_CID_MPEG_STREAM_TYPE:
1069 break;
1070 default:
1071 return -EINVAL;
1073 return ret;
1076 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1077 struct v4l2_ext_controls *ctrls)
1079 struct hdpvr_fh *fh = file->private_data;
1080 struct hdpvr_device *dev = fh->dev;
1081 int i, err = 0;
1083 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1084 for (i = 0; i < ctrls->count; i++) {
1085 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1087 err = hdpvr_try_ctrl(ctrl,
1088 dev->flags & HDPVR_FLAG_AC3_CAP);
1089 if (err) {
1090 ctrls->error_idx = i;
1091 break;
1093 err = hdpvr_set_ctrl(dev, ctrl);
1094 if (err) {
1095 ctrls->error_idx = i;
1096 break;
1099 return err;
1103 return -EINVAL;
1106 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1107 struct v4l2_fmtdesc *f)
1110 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1111 return -EINVAL;
1113 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1114 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1115 f->pixelformat = V4L2_PIX_FMT_MPEG;
1117 return 0;
1120 static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1121 struct v4l2_format *f)
1123 struct hdpvr_fh *fh = file->private_data;
1124 struct hdpvr_device *dev = fh->dev;
1125 struct hdpvr_video_info *vid_info;
1127 if (!dev)
1128 return -ENODEV;
1130 vid_info = get_video_info(dev);
1131 if (!vid_info)
1132 return -EFAULT;
1134 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1135 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1136 f->fmt.pix.width = vid_info->width;
1137 f->fmt.pix.height = vid_info->height;
1138 f->fmt.pix.sizeimage = dev->bulk_in_size;
1139 f->fmt.pix.colorspace = 0;
1140 f->fmt.pix.bytesperline = 0;
1141 f->fmt.pix.field = V4L2_FIELD_ANY;
1143 kfree(vid_info);
1144 return 0;
1147 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1148 struct v4l2_encoder_cmd *a)
1150 struct hdpvr_fh *fh = filp->private_data;
1151 struct hdpvr_device *dev = fh->dev;
1152 int res;
1154 mutex_lock(&dev->io_mutex);
1156 memset(&a->raw, 0, sizeof(a->raw));
1157 switch (a->cmd) {
1158 case V4L2_ENC_CMD_START:
1159 a->flags = 0;
1160 res = hdpvr_start_streaming(dev);
1161 break;
1162 case V4L2_ENC_CMD_STOP:
1163 res = hdpvr_stop_streaming(dev);
1164 break;
1165 default:
1166 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1167 "Unsupported encoder cmd %d\n", a->cmd);
1168 res = -EINVAL;
1170 mutex_unlock(&dev->io_mutex);
1171 return res;
1174 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1175 struct v4l2_encoder_cmd *a)
1177 switch (a->cmd) {
1178 case V4L2_ENC_CMD_START:
1179 case V4L2_ENC_CMD_STOP:
1180 return 0;
1181 default:
1182 return -EINVAL;
1186 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1187 .vidioc_querycap = vidioc_querycap,
1188 .vidioc_s_std = vidioc_s_std,
1189 .vidioc_enum_input = vidioc_enum_input,
1190 .vidioc_g_input = vidioc_g_input,
1191 .vidioc_s_input = vidioc_s_input,
1192 .vidioc_enumaudio = vidioc_enumaudio,
1193 .vidioc_g_audio = vidioc_g_audio,
1194 .vidioc_s_audio = vidioc_s_audio,
1195 .vidioc_queryctrl = vidioc_queryctrl,
1196 .vidioc_g_ctrl = vidioc_g_ctrl,
1197 .vidioc_s_ctrl = vidioc_s_ctrl,
1198 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1199 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1200 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1201 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1202 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1203 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1204 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1207 static void hdpvr_device_release(struct video_device *vdev)
1209 struct hdpvr_device *dev = video_get_drvdata(vdev);
1211 hdpvr_delete(dev);
1214 static const struct video_device hdpvr_video_template = {
1215 /* .type = VFL_TYPE_GRABBER, */
1216 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1217 .fops = &hdpvr_fops,
1218 .release = hdpvr_device_release,
1219 .ioctl_ops = &hdpvr_ioctl_ops,
1220 .tvnorms =
1221 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1222 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1223 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1224 V4L2_STD_PAL_60,
1225 .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
1226 V4L2_STD_PAL_60,
1229 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1230 int devnum)
1232 /* setup and register video device */
1233 dev->video_dev = video_device_alloc();
1234 if (!dev->video_dev) {
1235 v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
1236 goto error;
1239 *(dev->video_dev) = hdpvr_video_template;
1240 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1241 dev->video_dev->parent = parent;
1242 video_set_drvdata(dev->video_dev, dev);
1244 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1245 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
1246 goto error;
1249 return 0;
1250 error:
1251 return -ENOMEM;