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