Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / media / video / hdpvr / hdpvr-video.c
blob11ffe9cc178065b222517871e8a567a379ff1ac3
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/workqueue.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-dev.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ioctl.h>
26 #include "hdpvr.h"
28 #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
30 #define print_buffer_status() { \
31 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
32 "%s:%d buffer stat: %d free, %d proc\n", \
33 __func__, __LINE__, \
34 list_size(&dev->free_buff_list), \
35 list_size(&dev->rec_buff_list)); }
37 struct hdpvr_fh {
38 struct hdpvr_device *dev;
41 static uint list_size(struct list_head *list)
43 struct list_head *tmp;
44 uint count = 0;
46 list_for_each(tmp, list) {
47 count++;
50 return count;
53 /*=========================================================================*/
54 /* urb callback */
55 static void hdpvr_read_bulk_callback(struct urb *urb)
57 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
58 struct hdpvr_device *dev = buf->dev;
60 /* marking buffer as received and wake waiting */
61 buf->status = BUFSTAT_READY;
62 wake_up_interruptible(&dev->wait_data);
65 /*=========================================================================*/
66 /* bufffer bits */
68 /* function expects dev->io_mutex to be hold by caller */
69 int hdpvr_cancel_queue(struct hdpvr_device *dev)
71 struct hdpvr_buffer *buf;
73 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
74 usb_kill_urb(buf->urb);
75 buf->status = BUFSTAT_AVAILABLE;
78 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
80 return 0;
83 static int hdpvr_free_queue(struct list_head *q)
85 struct list_head *tmp;
86 struct list_head *p;
87 struct hdpvr_buffer *buf;
88 struct urb *urb;
90 for (p = q->next; p != q;) {
91 buf = list_entry(p, struct hdpvr_buffer, buff_list);
93 urb = buf->urb;
94 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
95 urb->transfer_buffer, urb->transfer_dma);
96 usb_free_urb(urb);
97 tmp = p->next;
98 list_del(p);
99 kfree(buf);
100 p = tmp;
103 return 0;
106 /* function expects dev->io_mutex to be hold by caller */
107 int hdpvr_free_buffers(struct hdpvr_device *dev)
109 hdpvr_cancel_queue(dev);
111 hdpvr_free_queue(&dev->free_buff_list);
112 hdpvr_free_queue(&dev->rec_buff_list);
114 return 0;
117 /* function expects dev->io_mutex to be hold by caller */
118 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
120 uint i;
121 int retval = -ENOMEM;
122 u8 *mem;
123 struct hdpvr_buffer *buf;
124 struct urb *urb;
126 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
127 "allocating %u buffers\n", count);
129 for (i = 0; i < count; i++) {
131 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
132 if (!buf) {
133 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
134 goto exit;
136 buf->dev = dev;
138 urb = usb_alloc_urb(0, GFP_KERNEL);
139 if (!urb) {
140 v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
141 goto exit_urb;
143 buf->urb = urb;
145 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
146 &urb->transfer_dma);
147 if (!mem) {
148 v4l2_err(&dev->v4l2_dev,
149 "cannot allocate usb transfer buffer\n");
150 goto exit_urb_buffer;
153 usb_fill_bulk_urb(buf->urb, dev->udev,
154 usb_rcvbulkpipe(dev->udev,
155 dev->bulk_in_endpointAddr),
156 mem, dev->bulk_in_size,
157 hdpvr_read_bulk_callback, buf);
159 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
160 buf->status = BUFSTAT_AVAILABLE;
161 list_add_tail(&buf->buff_list, &dev->free_buff_list);
163 return 0;
164 exit_urb_buffer:
165 usb_free_urb(urb);
166 exit_urb:
167 kfree(buf);
168 exit:
169 hdpvr_free_buffers(dev);
170 return retval;
173 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
175 struct hdpvr_buffer *buf;
176 struct urb *urb;
177 int ret = 0, err_count = 0;
179 mutex_lock(&dev->io_mutex);
181 while (dev->status == STATUS_STREAMING &&
182 !list_empty(&dev->free_buff_list)) {
184 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
185 buff_list);
186 if (buf->status != BUFSTAT_AVAILABLE) {
187 v4l2_err(&dev->v4l2_dev,
188 "buffer not marked as available\n");
189 ret = -EFAULT;
190 goto err;
193 urb = buf->urb;
194 urb->status = 0;
195 urb->actual_length = 0;
196 ret = usb_submit_urb(urb, GFP_KERNEL);
197 if (ret) {
198 v4l2_err(&dev->v4l2_dev,
199 "usb_submit_urb in %s returned %d\n",
200 __func__, ret);
201 if (++err_count > 2)
202 break;
203 continue;
205 buf->status = BUFSTAT_INPROGRESS;
206 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
208 err:
209 print_buffer_status();
210 mutex_unlock(&dev->io_mutex);
211 return ret;
214 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
216 struct hdpvr_buffer *buf;
218 mutex_lock(&dev->io_mutex);
220 if (list_empty(&dev->rec_buff_list)) {
221 mutex_unlock(&dev->io_mutex);
222 return NULL;
225 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
226 buff_list);
227 mutex_unlock(&dev->io_mutex);
229 return buf;
232 static void hdpvr_transmit_buffers(struct work_struct *work)
234 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
235 worker);
237 while (dev->status == STATUS_STREAMING) {
239 if (hdpvr_submit_buffers(dev)) {
240 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
241 goto error;
243 if (wait_event_interruptible(dev->wait_buffer,
244 !list_empty(&dev->free_buff_list) ||
245 dev->status != STATUS_STREAMING))
246 goto error;
249 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
250 "transmit worker exited\n");
251 return;
252 error:
253 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
254 "transmit buffers errored\n");
255 dev->status = STATUS_ERROR;
258 /* function expects dev->io_mutex to be hold by caller */
259 static int hdpvr_start_streaming(struct hdpvr_device *dev)
261 int ret;
262 struct hdpvr_video_info *vidinf;
264 if (dev->status == STATUS_STREAMING)
265 return 0;
266 else if (dev->status != STATUS_IDLE)
267 return -EAGAIN;
269 vidinf = get_video_info(dev);
271 if (vidinf) {
272 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
273 "video signal: %dx%d@%dhz\n", vidinf->width,
274 vidinf->height, vidinf->fps);
275 kfree(vidinf);
277 /* start streaming 2 request */
278 ret = usb_control_msg(dev->udev,
279 usb_sndctrlpipe(dev->udev, 0),
280 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
281 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
282 "encoder start control request returned %d\n", ret);
284 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
286 dev->status = STATUS_STREAMING;
288 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
289 queue_work(dev->workqueue, &dev->worker);
291 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
292 "streaming started\n");
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 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
342 "%2d: got %d bytes\n", c, actual_length);
344 kfree(buf);
345 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
346 "used %d urbs to empty device buffers\n", c-1);
347 msleep(10);
349 dev->status = STATUS_IDLE;
351 return 0;
355 /*=======================================================================*/
357 * video 4 linux 2 file operations
360 static int hdpvr_open(struct file *file)
362 struct hdpvr_device *dev;
363 struct hdpvr_fh *fh;
364 int retval = -ENOMEM;
366 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
367 if (!dev) {
368 pr_err("open failing with with ENODEV\n");
369 retval = -ENODEV;
370 goto err;
373 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
374 if (!fh) {
375 v4l2_err(&dev->v4l2_dev, "Out of memory\n");
376 goto err;
378 /* lock the device to allow correctly handling errors
379 * in resumption */
380 mutex_lock(&dev->io_mutex);
381 dev->open_count++;
382 mutex_unlock(&dev->io_mutex);
384 fh->dev = dev;
386 /* save our object in the file's private structure */
387 file->private_data = fh;
389 retval = 0;
390 err:
391 return retval;
394 static int hdpvr_release(struct file *file)
396 struct hdpvr_fh *fh = file->private_data;
397 struct hdpvr_device *dev = fh->dev;
399 if (!dev)
400 return -ENODEV;
402 mutex_lock(&dev->io_mutex);
403 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
404 hdpvr_stop_streaming(dev);
406 mutex_unlock(&dev->io_mutex);
408 return 0;
412 * hdpvr_v4l2_read()
413 * will allocate buffers when called for the first time
415 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
416 loff_t *pos)
418 struct hdpvr_fh *fh = file->private_data;
419 struct hdpvr_device *dev = fh->dev;
420 struct hdpvr_buffer *buf = NULL;
421 struct urb *urb;
422 unsigned int ret = 0;
423 int rem, cnt;
425 if (*pos)
426 return -ESPIPE;
428 if (!dev)
429 return -ENODEV;
431 mutex_lock(&dev->io_mutex);
432 if (dev->status == STATUS_IDLE) {
433 if (hdpvr_start_streaming(dev)) {
434 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
435 "start_streaming failed\n");
436 ret = -EIO;
437 msleep(200);
438 dev->status = STATUS_IDLE;
439 mutex_unlock(&dev->io_mutex);
440 goto err;
442 print_buffer_status();
444 mutex_unlock(&dev->io_mutex);
446 /* wait for the first buffer */
447 if (!(file->f_flags & O_NONBLOCK)) {
448 if (wait_event_interruptible(dev->wait_data,
449 hdpvr_get_next_buffer(dev)))
450 return -ERESTARTSYS;
453 buf = hdpvr_get_next_buffer(dev);
455 while (count > 0 && buf) {
457 if (buf->status != BUFSTAT_READY &&
458 dev->status != STATUS_DISCONNECTED) {
459 /* return nonblocking */
460 if (file->f_flags & O_NONBLOCK) {
461 if (!ret)
462 ret = -EAGAIN;
463 goto err;
466 if (wait_event_interruptible(dev->wait_data,
467 buf->status == BUFSTAT_READY)) {
468 ret = -ERESTARTSYS;
469 goto err;
473 if (buf->status != BUFSTAT_READY)
474 break;
476 /* set remaining bytes to copy */
477 urb = buf->urb;
478 rem = urb->actual_length - buf->pos;
479 cnt = rem > count ? count : rem;
481 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
482 cnt)) {
483 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
484 if (!ret)
485 ret = -EFAULT;
486 goto err;
489 buf->pos += cnt;
490 count -= cnt;
491 buffer += cnt;
492 ret += cnt;
494 /* finished, take next buffer */
495 if (buf->pos == urb->actual_length) {
496 mutex_lock(&dev->io_mutex);
497 buf->pos = 0;
498 buf->status = BUFSTAT_AVAILABLE;
500 list_move_tail(&buf->buff_list, &dev->free_buff_list);
502 print_buffer_status();
504 mutex_unlock(&dev->io_mutex);
506 wake_up_interruptible(&dev->wait_buffer);
508 buf = hdpvr_get_next_buffer(dev);
511 err:
512 if (!ret && !buf)
513 ret = -EAGAIN;
514 return ret;
517 static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
519 struct hdpvr_buffer *buf = NULL;
520 struct hdpvr_fh *fh = filp->private_data;
521 struct hdpvr_device *dev = fh->dev;
522 unsigned int mask = 0;
524 mutex_lock(&dev->io_mutex);
526 if (!video_is_registered(dev->video_dev)) {
527 mutex_unlock(&dev->io_mutex);
528 return -EIO;
531 if (dev->status == STATUS_IDLE) {
532 if (hdpvr_start_streaming(dev)) {
533 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
534 "start_streaming failed\n");
535 dev->status = STATUS_IDLE;
538 print_buffer_status();
540 mutex_unlock(&dev->io_mutex);
542 buf = hdpvr_get_next_buffer(dev);
543 /* only wait if no data is available */
544 if (!buf || buf->status != BUFSTAT_READY) {
545 poll_wait(filp, &dev->wait_data, wait);
546 buf = hdpvr_get_next_buffer(dev);
548 if (buf && buf->status == BUFSTAT_READY)
549 mask |= POLLIN | POLLRDNORM;
551 return mask;
555 static const struct v4l2_file_operations hdpvr_fops = {
556 .owner = THIS_MODULE,
557 .open = hdpvr_open,
558 .release = hdpvr_release,
559 .read = hdpvr_read,
560 .poll = hdpvr_poll,
561 .unlocked_ioctl = video_ioctl2,
564 /*=======================================================================*/
566 * V4L2 ioctl handling
569 static int vidioc_querycap(struct file *file, void *priv,
570 struct v4l2_capability *cap)
572 struct hdpvr_device *dev = video_drvdata(file);
574 strcpy(cap->driver, "hdpvr");
575 strcpy(cap->card, "Hauppauge HD PVR");
576 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
577 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
578 V4L2_CAP_AUDIO |
579 V4L2_CAP_READWRITE;
580 return 0;
583 static int vidioc_s_std(struct file *file, void *private_data,
584 v4l2_std_id *std)
586 struct hdpvr_fh *fh = file->private_data;
587 struct hdpvr_device *dev = fh->dev;
588 u8 std_type = 1;
590 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
591 std_type = 0;
593 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
596 static const char *iname[] = {
597 [HDPVR_COMPONENT] = "Component",
598 [HDPVR_SVIDEO] = "S-Video",
599 [HDPVR_COMPOSITE] = "Composite",
602 static int vidioc_enum_input(struct file *file, void *priv,
603 struct v4l2_input *i)
605 struct hdpvr_fh *fh = file->private_data;
606 struct hdpvr_device *dev = fh->dev;
607 unsigned int n;
609 n = i->index;
610 if (n >= HDPVR_VIDEO_INPUTS)
611 return -EINVAL;
613 i->type = V4L2_INPUT_TYPE_CAMERA;
615 strncpy(i->name, iname[n], sizeof(i->name) - 1);
616 i->name[sizeof(i->name) - 1] = '\0';
618 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
620 i->std = dev->video_dev->tvnorms;
622 return 0;
625 static int vidioc_s_input(struct file *file, void *private_data,
626 unsigned int index)
628 struct hdpvr_fh *fh = file->private_data;
629 struct hdpvr_device *dev = fh->dev;
630 int retval;
632 if (index >= HDPVR_VIDEO_INPUTS)
633 return -EINVAL;
635 if (dev->status != STATUS_IDLE)
636 return -EAGAIN;
638 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
639 if (!retval)
640 dev->options.video_input = index;
642 return retval;
645 static int vidioc_g_input(struct file *file, void *private_data,
646 unsigned int *index)
648 struct hdpvr_fh *fh = file->private_data;
649 struct hdpvr_device *dev = fh->dev;
651 *index = dev->options.video_input;
652 return 0;
656 static const char *audio_iname[] = {
657 [HDPVR_RCA_FRONT] = "RCA front",
658 [HDPVR_RCA_BACK] = "RCA back",
659 [HDPVR_SPDIF] = "SPDIF",
662 static int vidioc_enumaudio(struct file *file, void *priv,
663 struct v4l2_audio *audio)
665 unsigned int n;
667 n = audio->index;
668 if (n >= HDPVR_AUDIO_INPUTS)
669 return -EINVAL;
671 audio->capability = V4L2_AUDCAP_STEREO;
673 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
674 audio->name[sizeof(audio->name) - 1] = '\0';
676 return 0;
679 static int vidioc_s_audio(struct file *file, void *private_data,
680 struct v4l2_audio *audio)
682 struct hdpvr_fh *fh = file->private_data;
683 struct hdpvr_device *dev = fh->dev;
684 int retval;
686 if (audio->index >= HDPVR_AUDIO_INPUTS)
687 return -EINVAL;
689 if (dev->status != STATUS_IDLE)
690 return -EAGAIN;
692 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
693 if (!retval)
694 dev->options.audio_input = audio->index;
696 return retval;
699 static int vidioc_g_audio(struct file *file, void *private_data,
700 struct v4l2_audio *audio)
702 struct hdpvr_fh *fh = file->private_data;
703 struct hdpvr_device *dev = fh->dev;
705 audio->index = dev->options.audio_input;
706 audio->capability = V4L2_AUDCAP_STEREO;
707 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
708 audio->name[sizeof(audio->name) - 1] = '\0';
709 return 0;
712 static const s32 supported_v4l2_ctrls[] = {
713 V4L2_CID_BRIGHTNESS,
714 V4L2_CID_CONTRAST,
715 V4L2_CID_SATURATION,
716 V4L2_CID_HUE,
717 V4L2_CID_SHARPNESS,
718 V4L2_CID_MPEG_AUDIO_ENCODING,
719 V4L2_CID_MPEG_VIDEO_ENCODING,
720 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
721 V4L2_CID_MPEG_VIDEO_BITRATE,
722 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
725 static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
726 int ac3, int fw_ver)
728 int err;
730 if (fw_ver > 0x15) {
731 switch (qc->id) {
732 case V4L2_CID_BRIGHTNESS:
733 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
734 case V4L2_CID_CONTRAST:
735 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x40);
736 case V4L2_CID_SATURATION:
737 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x40);
738 case V4L2_CID_HUE:
739 return v4l2_ctrl_query_fill(qc, 0x0, 0x1e, 1, 0xf);
740 case V4L2_CID_SHARPNESS:
741 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
743 } else {
744 switch (qc->id) {
745 case V4L2_CID_BRIGHTNESS:
746 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
747 case V4L2_CID_CONTRAST:
748 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
749 case V4L2_CID_SATURATION:
750 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
751 case V4L2_CID_HUE:
752 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
753 case V4L2_CID_SHARPNESS:
754 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
758 switch (qc->id) {
759 case V4L2_CID_MPEG_AUDIO_ENCODING:
760 return v4l2_ctrl_query_fill(
761 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
762 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
763 : V4L2_MPEG_AUDIO_ENCODING_AAC,
764 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
765 case V4L2_CID_MPEG_VIDEO_ENCODING:
766 return v4l2_ctrl_query_fill(
767 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
768 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
769 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
771 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
772 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
773 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
774 return v4l2_ctrl_query_fill(
775 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
776 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
777 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
779 case V4L2_CID_MPEG_VIDEO_BITRATE:
780 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
781 6500000);
782 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
783 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
784 9000000);
785 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
786 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
787 return err;
788 default:
789 return -EINVAL;
793 static int vidioc_queryctrl(struct file *file, void *private_data,
794 struct v4l2_queryctrl *qc)
796 struct hdpvr_fh *fh = file->private_data;
797 struct hdpvr_device *dev = fh->dev;
798 int i, next;
799 u32 id = qc->id;
801 memset(qc, 0, sizeof(*qc));
803 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
804 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
806 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
807 if (next) {
808 if (qc->id < supported_v4l2_ctrls[i])
809 qc->id = supported_v4l2_ctrls[i];
810 else
811 continue;
814 if (qc->id == supported_v4l2_ctrls[i])
815 return fill_queryctrl(&dev->options, qc,
816 dev->flags & HDPVR_FLAG_AC3_CAP,
817 dev->fw_ver);
819 if (qc->id < supported_v4l2_ctrls[i])
820 break;
823 return -EINVAL;
826 static int vidioc_g_ctrl(struct file *file, void *private_data,
827 struct v4l2_control *ctrl)
829 struct hdpvr_fh *fh = file->private_data;
830 struct hdpvr_device *dev = fh->dev;
832 switch (ctrl->id) {
833 case V4L2_CID_BRIGHTNESS:
834 ctrl->value = dev->options.brightness;
835 break;
836 case V4L2_CID_CONTRAST:
837 ctrl->value = dev->options.contrast;
838 break;
839 case V4L2_CID_SATURATION:
840 ctrl->value = dev->options.saturation;
841 break;
842 case V4L2_CID_HUE:
843 ctrl->value = dev->options.hue;
844 break;
845 case V4L2_CID_SHARPNESS:
846 ctrl->value = dev->options.sharpness;
847 break;
848 default:
849 return -EINVAL;
851 return 0;
854 static int vidioc_s_ctrl(struct file *file, void *private_data,
855 struct v4l2_control *ctrl)
857 struct hdpvr_fh *fh = file->private_data;
858 struct hdpvr_device *dev = fh->dev;
859 int retval;
861 switch (ctrl->id) {
862 case V4L2_CID_BRIGHTNESS:
863 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
864 if (!retval)
865 dev->options.brightness = ctrl->value;
866 break;
867 case V4L2_CID_CONTRAST:
868 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
869 if (!retval)
870 dev->options.contrast = ctrl->value;
871 break;
872 case V4L2_CID_SATURATION:
873 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
874 if (!retval)
875 dev->options.saturation = ctrl->value;
876 break;
877 case V4L2_CID_HUE:
878 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
879 if (!retval)
880 dev->options.hue = ctrl->value;
881 break;
882 case V4L2_CID_SHARPNESS:
883 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
884 if (!retval)
885 dev->options.sharpness = ctrl->value;
886 break;
887 default:
888 return -EINVAL;
891 return retval;
895 static int hdpvr_get_ctrl(struct hdpvr_options *opt,
896 struct v4l2_ext_control *ctrl)
898 switch (ctrl->id) {
899 case V4L2_CID_MPEG_AUDIO_ENCODING:
900 ctrl->value = opt->audio_codec;
901 break;
902 case V4L2_CID_MPEG_VIDEO_ENCODING:
903 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
904 break;
905 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
906 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
907 /* break; */
908 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
909 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
910 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
911 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
912 break;
913 case V4L2_CID_MPEG_VIDEO_BITRATE:
914 ctrl->value = opt->bitrate * 100000;
915 break;
916 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
917 ctrl->value = opt->peak_bitrate * 100000;
918 break;
919 case V4L2_CID_MPEG_STREAM_TYPE:
920 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
921 break;
922 default:
923 return -EINVAL;
925 return 0;
928 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
929 struct v4l2_ext_controls *ctrls)
931 struct hdpvr_fh *fh = file->private_data;
932 struct hdpvr_device *dev = fh->dev;
933 int i, err = 0;
935 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
936 for (i = 0; i < ctrls->count; i++) {
937 struct v4l2_ext_control *ctrl = ctrls->controls + i;
939 err = hdpvr_get_ctrl(&dev->options, ctrl);
940 if (err) {
941 ctrls->error_idx = i;
942 break;
945 return err;
949 return -EINVAL;
953 static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
955 int ret = -EINVAL;
957 switch (ctrl->id) {
958 case V4L2_CID_MPEG_AUDIO_ENCODING:
959 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
960 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
961 ret = 0;
962 break;
963 case V4L2_CID_MPEG_VIDEO_ENCODING:
964 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
965 ret = 0;
966 break;
967 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
968 /* if (ctrl->value == 0 || ctrl->value == 128) */
969 /* ret = 0; */
970 /* break; */
971 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
972 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
973 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
974 ret = 0;
975 break;
976 case V4L2_CID_MPEG_VIDEO_BITRATE:
978 uint bitrate = ctrl->value / 100000;
979 if (bitrate >= 10 && bitrate <= 135)
980 ret = 0;
981 break;
983 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
985 uint peak_bitrate = ctrl->value / 100000;
986 if (peak_bitrate >= 10 && peak_bitrate <= 202)
987 ret = 0;
988 break;
990 case V4L2_CID_MPEG_STREAM_TYPE:
991 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
992 ret = 0;
993 break;
994 default:
995 return -EINVAL;
997 return 0;
1000 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
1001 struct v4l2_ext_controls *ctrls)
1003 struct hdpvr_fh *fh = file->private_data;
1004 struct hdpvr_device *dev = fh->dev;
1005 int i, err = 0;
1007 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1008 for (i = 0; i < ctrls->count; i++) {
1009 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1011 err = hdpvr_try_ctrl(ctrl,
1012 dev->flags & HDPVR_FLAG_AC3_CAP);
1013 if (err) {
1014 ctrls->error_idx = i;
1015 break;
1018 return err;
1021 return -EINVAL;
1025 static int hdpvr_set_ctrl(struct hdpvr_device *dev,
1026 struct v4l2_ext_control *ctrl)
1028 struct hdpvr_options *opt = &dev->options;
1029 int ret = 0;
1031 switch (ctrl->id) {
1032 case V4L2_CID_MPEG_AUDIO_ENCODING:
1033 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
1034 opt->audio_codec = ctrl->value;
1035 ret = hdpvr_set_audio(dev, opt->audio_input,
1036 opt->audio_codec);
1038 break;
1039 case V4L2_CID_MPEG_VIDEO_ENCODING:
1040 break;
1041 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1042 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1043 /* opt->gop_mode |= 0x2; */
1044 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1045 /* opt->gop_mode); */
1046 /* } */
1047 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1048 /* opt->gop_mode &= ~0x2; */
1049 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1050 /* opt->gop_mode); */
1051 /* } */
1052 /* break; */
1053 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1054 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1055 opt->bitrate_mode != HDPVR_CONSTANT) {
1056 opt->bitrate_mode = HDPVR_CONSTANT;
1057 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1058 opt->bitrate_mode);
1060 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1061 opt->bitrate_mode == HDPVR_CONSTANT) {
1062 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1063 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1064 opt->bitrate_mode);
1066 break;
1067 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1068 uint bitrate = ctrl->value / 100000;
1070 opt->bitrate = bitrate;
1071 if (bitrate >= opt->peak_bitrate)
1072 opt->peak_bitrate = bitrate+1;
1074 hdpvr_set_bitrate(dev);
1075 break;
1077 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1078 uint peak_bitrate = ctrl->value / 100000;
1080 if (opt->bitrate_mode == HDPVR_CONSTANT)
1081 break;
1083 if (opt->bitrate < peak_bitrate) {
1084 opt->peak_bitrate = peak_bitrate;
1085 hdpvr_set_bitrate(dev);
1086 } else
1087 ret = -EINVAL;
1088 break;
1090 case V4L2_CID_MPEG_STREAM_TYPE:
1091 break;
1092 default:
1093 return -EINVAL;
1095 return ret;
1098 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1099 struct v4l2_ext_controls *ctrls)
1101 struct hdpvr_fh *fh = file->private_data;
1102 struct hdpvr_device *dev = fh->dev;
1103 int i, err = 0;
1105 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1106 for (i = 0; i < ctrls->count; i++) {
1107 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1109 err = hdpvr_try_ctrl(ctrl,
1110 dev->flags & HDPVR_FLAG_AC3_CAP);
1111 if (err) {
1112 ctrls->error_idx = i;
1113 break;
1115 err = hdpvr_set_ctrl(dev, ctrl);
1116 if (err) {
1117 ctrls->error_idx = i;
1118 break;
1121 return err;
1125 return -EINVAL;
1128 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1129 struct v4l2_fmtdesc *f)
1132 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1133 return -EINVAL;
1135 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1136 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1137 f->pixelformat = V4L2_PIX_FMT_MPEG;
1139 return 0;
1142 static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1143 struct v4l2_format *f)
1145 struct hdpvr_fh *fh = file->private_data;
1146 struct hdpvr_device *dev = fh->dev;
1147 struct hdpvr_video_info *vid_info;
1149 if (!dev)
1150 return -ENODEV;
1152 vid_info = get_video_info(dev);
1153 if (!vid_info)
1154 return -EFAULT;
1156 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1157 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1158 f->fmt.pix.width = vid_info->width;
1159 f->fmt.pix.height = vid_info->height;
1160 f->fmt.pix.sizeimage = dev->bulk_in_size;
1161 f->fmt.pix.colorspace = 0;
1162 f->fmt.pix.bytesperline = 0;
1163 f->fmt.pix.field = V4L2_FIELD_ANY;
1165 kfree(vid_info);
1166 return 0;
1169 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1170 struct v4l2_encoder_cmd *a)
1172 struct hdpvr_fh *fh = filp->private_data;
1173 struct hdpvr_device *dev = fh->dev;
1174 int res;
1176 mutex_lock(&dev->io_mutex);
1178 memset(&a->raw, 0, sizeof(a->raw));
1179 switch (a->cmd) {
1180 case V4L2_ENC_CMD_START:
1181 a->flags = 0;
1182 res = hdpvr_start_streaming(dev);
1183 break;
1184 case V4L2_ENC_CMD_STOP:
1185 res = hdpvr_stop_streaming(dev);
1186 break;
1187 default:
1188 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1189 "Unsupported encoder cmd %d\n", a->cmd);
1190 res = -EINVAL;
1192 mutex_unlock(&dev->io_mutex);
1193 return res;
1196 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1197 struct v4l2_encoder_cmd *a)
1199 switch (a->cmd) {
1200 case V4L2_ENC_CMD_START:
1201 case V4L2_ENC_CMD_STOP:
1202 return 0;
1203 default:
1204 return -EINVAL;
1208 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1209 .vidioc_querycap = vidioc_querycap,
1210 .vidioc_s_std = vidioc_s_std,
1211 .vidioc_enum_input = vidioc_enum_input,
1212 .vidioc_g_input = vidioc_g_input,
1213 .vidioc_s_input = vidioc_s_input,
1214 .vidioc_enumaudio = vidioc_enumaudio,
1215 .vidioc_g_audio = vidioc_g_audio,
1216 .vidioc_s_audio = vidioc_s_audio,
1217 .vidioc_queryctrl = vidioc_queryctrl,
1218 .vidioc_g_ctrl = vidioc_g_ctrl,
1219 .vidioc_s_ctrl = vidioc_s_ctrl,
1220 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1221 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1222 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1223 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1224 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1225 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1226 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1229 static void hdpvr_device_release(struct video_device *vdev)
1231 struct hdpvr_device *dev = video_get_drvdata(vdev);
1233 hdpvr_delete(dev);
1234 mutex_lock(&dev->io_mutex);
1235 destroy_workqueue(dev->workqueue);
1236 mutex_unlock(&dev->io_mutex);
1238 v4l2_device_unregister(&dev->v4l2_dev);
1240 /* deregister I2C adapter */
1241 #if defined(CONFIG_I2C) || (CONFIG_I2C_MODULE)
1242 mutex_lock(&dev->i2c_mutex);
1243 i2c_del_adapter(&dev->i2c_adapter);
1244 mutex_unlock(&dev->i2c_mutex);
1245 #endif /* CONFIG_I2C */
1247 kfree(dev->usbc_buf);
1248 kfree(dev);
1251 static const struct video_device hdpvr_video_template = {
1252 /* .type = VFL_TYPE_GRABBER, */
1253 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1254 .fops = &hdpvr_fops,
1255 .release = hdpvr_device_release,
1256 .ioctl_ops = &hdpvr_ioctl_ops,
1257 .tvnorms =
1258 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1259 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1260 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1261 V4L2_STD_PAL_60,
1262 .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |
1263 V4L2_STD_PAL_60,
1266 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1267 int devnum)
1269 /* setup and register video device */
1270 dev->video_dev = video_device_alloc();
1271 if (!dev->video_dev) {
1272 v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
1273 goto error;
1276 *(dev->video_dev) = hdpvr_video_template;
1277 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1278 dev->video_dev->parent = parent;
1279 video_set_drvdata(dev->video_dev, dev);
1281 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1282 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
1283 goto error;
1286 return 0;
1287 error:
1288 return -ENOMEM;