V4L/DVB (11228): hdpvr: use debugging macro for buffer status
[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);
303
304 wake_up_interruptible(&dev->wait_buffer);
305 msleep(50);
306
307 flush_workqueue(dev->workqueue);
308
309 /* kill the still outstanding urbs */
310 hdpvr_cancel_queue(dev);
311
312 dev->status = STATUS_IDLE;
313
314 return 0;
315}
316
317
318/*=======================================================================*/
319/*
320 * video 4 linux 2 file operations
321 */
322
323static int hdpvr_open(struct file *file)
324{
325 struct hdpvr_device *dev;
326 struct hdpvr_fh *fh;
327 int retval = -ENOMEM;
328
329 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
330 if (!dev) {
331 err("open failing with with ENODEV");
332 retval = -ENODEV;
333 goto err;
334 }
335
336 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
337 if (!fh) {
338 err("Out of memory?");
339 goto err;
340 }
341 /* lock the device to allow correctly handling errors
342 * in resumption */
343 mutex_lock(&dev->io_mutex);
344 dev->open_count++;
345
346 fh->dev = dev;
347
348 /* save our object in the file's private structure */
349 file->private_data = fh;
350
351 retval = 0;
352err:
353 mutex_unlock(&dev->io_mutex);
354 return retval;
355}
356
357static int hdpvr_release(struct file *file)
358{
359 struct hdpvr_fh *fh = (struct hdpvr_fh *)file->private_data;
360 struct hdpvr_device *dev = fh->dev;
361
362 if (!dev)
363 return -ENODEV;
364
365 mutex_lock(&dev->io_mutex);
366 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
367 hdpvr_stop_streaming(dev);
368
369 mutex_unlock(&dev->io_mutex);
370
371 return 0;
372}
373
374/*
375 * hdpvr_v4l2_read()
376 * will allocate buffers when called for the first time
377 */
378static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
379 loff_t *pos)
380{
381 struct hdpvr_fh *fh = file->private_data;
382 struct hdpvr_device *dev = fh->dev;
383 struct hdpvr_buffer *buf = NULL;
384 struct urb *urb;
385 unsigned int ret = 0;
386 int rem, cnt;
387
388 if (*pos)
389 return -ESPIPE;
390
391 if (!dev)
392 return -ENODEV;
393
394 mutex_lock(&dev->io_mutex);
395 if (dev->status == STATUS_IDLE) {
396 if (hdpvr_start_streaming(dev)) {
397 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
398 "start_streaming failed");
399 ret = -EIO;
400 msleep(200);
401 dev->status = STATUS_IDLE;
402 mutex_unlock(&dev->io_mutex);
403 goto err;
404 }
d211bfcb 405 print_buffer_status();
9aba42ef
JG
406 }
407 mutex_unlock(&dev->io_mutex);
408
409 /* wait for the first buffer */
410 if (!(file->f_flags & O_NONBLOCK)) {
411 if (wait_event_interruptible(dev->wait_data,
412 hdpvr_get_next_buffer(dev)))
413 return -ERESTARTSYS;
414 }
415
416 buf = hdpvr_get_next_buffer(dev);
417
418 while (count > 0 && buf) {
419
420 if (buf->status != BUFSTAT_READY &&
421 dev->status != STATUS_DISCONNECTED) {
422 /* return nonblocking */
423 if (file->f_flags & O_NONBLOCK) {
424 if (!ret)
425 ret = -EAGAIN;
426 goto err;
427 }
428
429 if (wait_event_interruptible(dev->wait_data,
430 buf->status == BUFSTAT_READY)) {
431 ret = -ERESTARTSYS;
432 goto err;
433 }
434 }
435
436 if (buf->status != BUFSTAT_READY)
437 break;
438
439 /* set remaining bytes to copy */
440 urb = buf->urb;
441 rem = urb->actual_length - buf->pos;
442 cnt = rem > count ? count : rem;
443
444 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
445 cnt)) {
446 err("read: copy_to_user failed");
447 if (!ret)
448 ret = -EFAULT;
449 goto err;
450 }
451
452 buf->pos += cnt;
453 count -= cnt;
454 buffer += cnt;
455 ret += cnt;
456
457 /* finished, take next buffer */
458 if (buf->pos == urb->actual_length) {
459 mutex_lock(&dev->io_mutex);
460 buf->pos = 0;
461 buf->status = BUFSTAT_AVAILABLE;
462
463 list_move_tail(&buf->buff_list, &dev->free_buff_list);
464
d211bfcb 465 print_buffer_status();
9aba42ef
JG
466
467 mutex_unlock(&dev->io_mutex);
468
469 wake_up_interruptible(&dev->wait_buffer);
470
471 buf = hdpvr_get_next_buffer(dev);
472 }
473 }
474err:
475 if (!ret && !buf)
476 ret = -EAGAIN;
477 return ret;
478}
479
480static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
481{
482 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
483 struct hdpvr_device *dev = fh->dev;
484 unsigned int mask = 0;
485
486 mutex_lock(&dev->io_mutex);
487
488 if (video_is_unregistered(dev->video_dev))
489 return -EIO;
490
491 if (dev->status == STATUS_IDLE) {
492 if (hdpvr_start_streaming(dev)) {
493 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
494 "start_streaming failed");
495 dev->status = STATUS_IDLE;
496 }
497
d211bfcb 498 print_buffer_status();
9aba42ef
JG
499 }
500 mutex_unlock(&dev->io_mutex);
501
502 poll_wait(filp, &dev->wait_data, wait);
503
504 mutex_lock(&dev->io_mutex);
505 if (!list_empty(&dev->rec_buff_list)) {
506
507 struct hdpvr_buffer *buf = list_entry(dev->rec_buff_list.next,
508 struct hdpvr_buffer,
509 buff_list);
510
511 if (buf->status == BUFSTAT_READY)
512 mask |= POLLIN | POLLRDNORM;
513 }
514 mutex_unlock(&dev->io_mutex);
515
516 return mask;
517}
518
519
9aba42ef
JG
520static const struct v4l2_file_operations hdpvr_fops = {
521 .owner = THIS_MODULE,
522 .open = hdpvr_open,
523 .release = hdpvr_release,
524 .read = hdpvr_read,
525 .poll = hdpvr_poll,
76717b88 526 .unlocked_ioctl = video_ioctl2,
9aba42ef
JG
527};
528
529/*=======================================================================*/
530/*
531 * V4L2 ioctl handling
532 */
533
534static int vidioc_querycap(struct file *file, void *priv,
535 struct v4l2_capability *cap)
536{
537 struct hdpvr_device *dev = video_drvdata(file);
538
539 strcpy(cap->driver, "hdpvr");
540 strcpy(cap->card, "Haupauge HD PVR");
541 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
542 cap->version = HDPVR_VERSION;
543 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
544 V4L2_CAP_AUDIO |
545 V4L2_CAP_READWRITE;
546 return 0;
547}
548
549static int vidioc_s_std(struct file *file, void *private_data,
550 v4l2_std_id *std)
551{
552 struct hdpvr_fh *fh = file->private_data;
553 struct hdpvr_device *dev = fh->dev;
554 u8 std_type = 1;
555
556 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
557 std_type = 0;
558
559 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
560}
561
562static const char *iname[] = {
563 [HDPVR_COMPONENT] = "Component",
564 [HDPVR_SVIDEO] = "S-Video",
565 [HDPVR_COMPOSITE] = "Composite",
566};
567
568static int vidioc_enum_input(struct file *file, void *priv,
569 struct v4l2_input *i)
570{
571 struct hdpvr_fh *fh = file->private_data;
572 struct hdpvr_device *dev = fh->dev;
573 unsigned int n;
574
575 n = i->index;
576 if (n >= HDPVR_VIDEO_INPUTS)
577 return -EINVAL;
578
579 i->type = V4L2_INPUT_TYPE_CAMERA;
580
581 strncpy(i->name, iname[n], sizeof(i->name) - 1);
582 i->name[sizeof(i->name) - 1] = '\0';
583
584 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
585
586 i->std = dev->video_dev->tvnorms;
587
588 return 0;
589}
590
591static int vidioc_s_input(struct file *file, void *private_data,
592 unsigned int index)
593{
594 struct hdpvr_fh *fh = file->private_data;
595 struct hdpvr_device *dev = fh->dev;
596 int retval;
597
598 if (index >= HDPVR_VIDEO_INPUTS)
599 return -EINVAL;
600
601 if (dev->status != STATUS_IDLE)
602 return -EAGAIN;
603
604 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
605 if (!retval)
606 dev->options.video_input = index;
607
608 return retval;
609}
610
611static int vidioc_g_input(struct file *file, void *private_data,
612 unsigned int *index)
613{
614 struct hdpvr_fh *fh = file->private_data;
615 struct hdpvr_device *dev = fh->dev;
616
617 *index = dev->options.video_input;
618 return 0;
619}
620
621
622static const char *audio_iname[] = {
623 [HDPVR_RCA_FRONT] = "RCA front",
624 [HDPVR_RCA_BACK] = "RCA back",
625 [HDPVR_SPDIF] = "SPDIF",
626};
627
628static int vidioc_enumaudio(struct file *file, void *priv,
629 struct v4l2_audio *audio)
630{
631 unsigned int n;
632
633 n = audio->index;
634 if (n >= HDPVR_AUDIO_INPUTS)
635 return -EINVAL;
636
637 audio->capability = V4L2_AUDCAP_STEREO;
638
639 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
640 audio->name[sizeof(audio->name) - 1] = '\0';
641
642 return 0;
643}
644
645static int vidioc_s_audio(struct file *file, void *private_data,
646 struct v4l2_audio *audio)
647{
648 struct hdpvr_fh *fh = file->private_data;
649 struct hdpvr_device *dev = fh->dev;
650 int retval;
651
652 if (audio->index >= HDPVR_AUDIO_INPUTS)
653 return -EINVAL;
654
655 if (dev->status != STATUS_IDLE)
656 return -EAGAIN;
657
658 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
659 if (!retval)
660 dev->options.audio_input = audio->index;
661
662 return retval;
663}
664
665static int vidioc_g_audio(struct file *file, void *private_data,
666 struct v4l2_audio *audio)
667{
668 struct hdpvr_fh *fh = file->private_data;
669 struct hdpvr_device *dev = fh->dev;
670
671 audio->index = dev->options.audio_input;
672 audio->capability = V4L2_AUDCAP_STEREO;
673 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
674 audio->name[sizeof(audio->name) - 1] = '\0';
675 return 0;
676}
677
678static const s32 supported_v4l2_ctrls[] = {
679 V4L2_CID_BRIGHTNESS,
680 V4L2_CID_CONTRAST,
681 V4L2_CID_SATURATION,
682 V4L2_CID_HUE,
683 V4L2_CID_SHARPNESS,
684 V4L2_CID_MPEG_AUDIO_ENCODING,
685 V4L2_CID_MPEG_VIDEO_ENCODING,
686 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
687 V4L2_CID_MPEG_VIDEO_BITRATE,
688 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
689};
690
691static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
692 int ac3)
693{
694 int err;
695
696 switch (qc->id) {
697 case V4L2_CID_BRIGHTNESS:
698 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
699 case V4L2_CID_CONTRAST:
700 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
701 case V4L2_CID_SATURATION:
702 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
703 case V4L2_CID_HUE:
704 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
705 case V4L2_CID_SHARPNESS:
706 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
707 case V4L2_CID_MPEG_AUDIO_ENCODING:
708 return v4l2_ctrl_query_fill(
709 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
710 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
711 : V4L2_MPEG_AUDIO_ENCODING_AAC,
712 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
713 case V4L2_CID_MPEG_VIDEO_ENCODING:
714 return v4l2_ctrl_query_fill(
715 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
716 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
717 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
718
719/* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
720/* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
721 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
722 return v4l2_ctrl_query_fill(
723 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
724 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
725 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
726
727 case V4L2_CID_MPEG_VIDEO_BITRATE:
728 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
729 6500000);
730 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
731 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
732 9000000);
733 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
734 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
735 return err;
736 default:
737 return -EINVAL;
738 }
739}
740
741static int vidioc_queryctrl(struct file *file, void *private_data,
742 struct v4l2_queryctrl *qc)
743{
744 struct hdpvr_fh *fh = file->private_data;
745 struct hdpvr_device *dev = fh->dev;
746 int i, next;
747 u32 id = qc->id;
748
749 memset(qc, 0, sizeof(*qc));
750
751 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
752 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
753
754 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
755 if (next) {
756 if (qc->id < supported_v4l2_ctrls[i])
757 qc->id = supported_v4l2_ctrls[i];
758 else
759 continue;
760 }
761
762 if (qc->id == supported_v4l2_ctrls[i])
763 return fill_queryctrl(&dev->options, qc,
764 dev->flags & HDPVR_FLAG_AC3_CAP);
765
766 if (qc->id < supported_v4l2_ctrls[i])
767 break;
768 }
769
770 return -EINVAL;
771}
772
773static int vidioc_g_ctrl(struct file *file, void *private_data,
774 struct v4l2_control *ctrl)
775{
776 struct hdpvr_fh *fh = file->private_data;
777 struct hdpvr_device *dev = fh->dev;
778
779 switch (ctrl->id) {
780 case V4L2_CID_BRIGHTNESS:
781 ctrl->value = dev->options.brightness;
782 break;
783 case V4L2_CID_CONTRAST:
784 ctrl->value = dev->options.contrast;
785 break;
786 case V4L2_CID_SATURATION:
787 ctrl->value = dev->options.saturation;
788 break;
789 case V4L2_CID_HUE:
790 ctrl->value = dev->options.hue;
791 break;
792 case V4L2_CID_SHARPNESS:
793 ctrl->value = dev->options.sharpness;
794 break;
795 default:
796 return -EINVAL;
797 }
798 return 0;
799}
800
801static int vidioc_s_ctrl(struct file *file, void *private_data,
802 struct v4l2_control *ctrl)
803{
804 struct hdpvr_fh *fh = file->private_data;
805 struct hdpvr_device *dev = fh->dev;
806 int retval;
807
808 switch (ctrl->id) {
809 case V4L2_CID_BRIGHTNESS:
810 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
811 if (!retval)
812 dev->options.brightness = ctrl->value;
813 break;
814 case V4L2_CID_CONTRAST:
815 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
816 if (!retval)
817 dev->options.contrast = ctrl->value;
818 break;
819 case V4L2_CID_SATURATION:
820 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
821 if (!retval)
822 dev->options.saturation = ctrl->value;
823 break;
824 case V4L2_CID_HUE:
825 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
826 if (!retval)
827 dev->options.hue = ctrl->value;
828 break;
829 case V4L2_CID_SHARPNESS:
830 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
831 if (!retval)
832 dev->options.sharpness = ctrl->value;
833 break;
834 default:
835 return -EINVAL;
836 }
837
838 return retval;
839}
840
841
842static int hdpvr_get_ctrl(struct hdpvr_options *opt,
843 struct v4l2_ext_control *ctrl)
844{
845 switch (ctrl->id) {
846 case V4L2_CID_MPEG_AUDIO_ENCODING:
847 ctrl->value = opt->audio_codec;
848 break;
849 case V4L2_CID_MPEG_VIDEO_ENCODING:
850 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
851 break;
852/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
853/* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
854/* break; */
855 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
856 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
857 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
858 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
859 break;
860 case V4L2_CID_MPEG_VIDEO_BITRATE:
861 ctrl->value = opt->bitrate * 100000;
862 break;
863 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
864 ctrl->value = opt->peak_bitrate * 100000;
865 break;
866 case V4L2_CID_MPEG_STREAM_TYPE:
867 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
868 break;
869 default:
870 return -EINVAL;
871 }
872 return 0;
873}
874
875static int vidioc_g_ext_ctrls(struct file *file, void *priv,
876 struct v4l2_ext_controls *ctrls)
877{
878 struct hdpvr_fh *fh = file->private_data;
879 struct hdpvr_device *dev = fh->dev;
880 int i, err = 0;
881
882 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
883 for (i = 0; i < ctrls->count; i++) {
884 struct v4l2_ext_control *ctrl = ctrls->controls + i;
885
886 err = hdpvr_get_ctrl(&dev->options, ctrl);
887 if (err) {
888 ctrls->error_idx = i;
889 break;
890 }
891 }
892 return err;
893
894 }
895
896 return -EINVAL;
897}
898
899
900static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
901{
902 int ret = -EINVAL;
903
904 switch (ctrl->id) {
905 case V4L2_CID_MPEG_AUDIO_ENCODING:
906 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
907 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
908 ret = 0;
909 break;
910 case V4L2_CID_MPEG_VIDEO_ENCODING:
911 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
912 ret = 0;
913 break;
914/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
915/* if (ctrl->value == 0 || ctrl->value == 128) */
916/* ret = 0; */
917/* break; */
918 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
919 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
920 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
921 ret = 0;
922 break;
923 case V4L2_CID_MPEG_VIDEO_BITRATE:
924 {
925 uint bitrate = ctrl->value / 100000;
926 if (bitrate >= 10 && bitrate <= 135)
927 ret = 0;
928 break;
929 }
930 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
931 {
932 uint peak_bitrate = ctrl->value / 100000;
933 if (peak_bitrate >= 10 && peak_bitrate <= 202)
934 ret = 0;
935 break;
936 }
937 case V4L2_CID_MPEG_STREAM_TYPE:
938 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
939 ret = 0;
940 break;
941 default:
942 return -EINVAL;
943 }
944 return 0;
945}
946
947static int vidioc_try_ext_ctrls(struct file *file, void *priv,
948 struct v4l2_ext_controls *ctrls)
949{
950 struct hdpvr_fh *fh = file->private_data;
951 struct hdpvr_device *dev = fh->dev;
952 int i, err = 0;
953
954 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
955 for (i = 0; i < ctrls->count; i++) {
956 struct v4l2_ext_control *ctrl = ctrls->controls + i;
957
958 err = hdpvr_try_ctrl(ctrl,
959 dev->flags & HDPVR_FLAG_AC3_CAP);
960 if (err) {
961 ctrls->error_idx = i;
962 break;
963 }
964 }
965 return err;
966 }
967
968 return -EINVAL;
969}
970
971
972static int hdpvr_set_ctrl(struct hdpvr_device *dev,
973 struct v4l2_ext_control *ctrl)
974{
975 struct hdpvr_options *opt = &dev->options;
976 int ret = 0;
977
978 switch (ctrl->id) {
979 case V4L2_CID_MPEG_AUDIO_ENCODING:
980 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
981 opt->audio_codec = ctrl->value;
982 ret = hdpvr_set_audio(dev, opt->audio_input,
983 opt->audio_codec);
984 }
985 break;
986 case V4L2_CID_MPEG_VIDEO_ENCODING:
987 break;
988/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
989/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
990/* opt->gop_mode |= 0x2; */
991/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
992/* opt->gop_mode); */
993/* } */
994/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
995/* opt->gop_mode &= ~0x2; */
996/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
997/* opt->gop_mode); */
998/* } */
999/* break; */
1000 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1001 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1002 opt->bitrate_mode != HDPVR_CONSTANT) {
1003 opt->bitrate_mode = HDPVR_CONSTANT;
1004 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1005 opt->bitrate_mode);
1006 }
1007 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1008 opt->bitrate_mode == HDPVR_CONSTANT) {
1009 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1010 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1011 opt->bitrate_mode);
1012 }
1013 break;
1014 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1015 uint bitrate = ctrl->value / 100000;
1016
1017 opt->bitrate = bitrate;
1018 if (bitrate >= opt->peak_bitrate)
1019 opt->peak_bitrate = bitrate+1;
1020
1021 hdpvr_set_bitrate(dev);
1022 break;
1023 }
1024 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1025 uint peak_bitrate = ctrl->value / 100000;
1026
1027 if (opt->bitrate_mode == HDPVR_CONSTANT)
1028 break;
1029
1030 if (opt->bitrate < peak_bitrate) {
1031 opt->peak_bitrate = peak_bitrate;
1032 hdpvr_set_bitrate(dev);
1033 } else
1034 ret = -EINVAL;
1035 break;
1036 }
1037 case V4L2_CID_MPEG_STREAM_TYPE:
1038 break;
1039 default:
1040 return -EINVAL;
1041 }
1042 return ret;
1043}
1044
1045static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1046 struct v4l2_ext_controls *ctrls)
1047{
1048 struct hdpvr_fh *fh = file->private_data;
1049 struct hdpvr_device *dev = fh->dev;
1050 int i, err = 0;
1051
1052 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1053 for (i = 0; i < ctrls->count; i++) {
1054 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1055
1056 err = hdpvr_try_ctrl(ctrl,
1057 dev->flags & HDPVR_FLAG_AC3_CAP);
1058 if (err) {
1059 ctrls->error_idx = i;
1060 break;
1061 }
1062 err = hdpvr_set_ctrl(dev, ctrl);
1063 if (err) {
1064 ctrls->error_idx = i;
1065 break;
1066 }
1067 }
1068 return err;
1069
1070 }
1071
1072 return -EINVAL;
1073}
1074
1075static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1076 struct v4l2_fmtdesc *f)
1077{
1078
1079 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1080 return -EINVAL;
1081
1082 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1083 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1084 f->pixelformat = V4L2_PIX_FMT_MPEG;
1085
1086 return 0;
1087}
1088
1089static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1090 struct v4l2_format *f)
1091{
1092 struct hdpvr_fh *fh = file->private_data;
1093 struct hdpvr_device *dev = fh->dev;
1094 struct hdpvr_video_info *vid_info;
1095
1096 if (!dev)
1097 return -ENODEV;
1098
1099 vid_info = get_video_info(dev);
1100 if (!vid_info)
1101 return -EFAULT;
1102
1103 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1104 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1105 f->fmt.pix.width = vid_info->width;
1106 f->fmt.pix.height = vid_info->height;
1107 f->fmt.pix.sizeimage = dev->bulk_in_size;
1108 f->fmt.pix.colorspace = 0;
1109 f->fmt.pix.bytesperline = 0;
1110 f->fmt.pix.field = V4L2_FIELD_ANY;
1111
1112 kfree(vid_info);
1113 return 0;
1114}
1115
76717b88
JG
1116static int vidioc_encoder_cmd(struct file *filp, void *priv,
1117 struct v4l2_encoder_cmd *a)
1118{
1119 struct hdpvr_fh *fh = filp->private_data;
1120 struct hdpvr_device *dev = fh->dev;
1121 int res;
1122
1123 mutex_lock(&dev->io_mutex);
1124
1125 memset(&a->raw, 0, sizeof(a->raw));
1126 switch (a->cmd) {
1127 case V4L2_ENC_CMD_START:
1128 a->flags = 0;
1129 res = hdpvr_start_streaming(dev);
1130 break;
1131 case V4L2_ENC_CMD_STOP:
1132 res = hdpvr_stop_streaming(dev);
1133 break;
1134 default:
1135 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
1136 "Unsupported encoder cmd %d\n", a->cmd);
1137 return -EINVAL;
1138 }
1139 mutex_unlock(&dev->io_mutex);
1140 return res;
1141}
1142
1143static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1144 struct v4l2_encoder_cmd *a)
1145{
1146 switch (a->cmd) {
1147 case V4L2_ENC_CMD_START:
1148 case V4L2_ENC_CMD_STOP:
1149 return 0;
1150 default:
1151 return -EINVAL;
1152 }
1153}
9aba42ef
JG
1154
1155static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1156 .vidioc_querycap = vidioc_querycap,
1157 .vidioc_s_std = vidioc_s_std,
1158 .vidioc_enum_input = vidioc_enum_input,
1159 .vidioc_g_input = vidioc_g_input,
1160 .vidioc_s_input = vidioc_s_input,
1161 .vidioc_enumaudio = vidioc_enumaudio,
1162 .vidioc_g_audio = vidioc_g_audio,
1163 .vidioc_s_audio = vidioc_s_audio,
1164 .vidioc_queryctrl = vidioc_queryctrl,
1165 .vidioc_g_ctrl = vidioc_g_ctrl,
1166 .vidioc_s_ctrl = vidioc_s_ctrl,
1167 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1168 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1169 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1170 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1171 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
76717b88
JG
1172 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1173 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
9aba42ef
JG
1174};
1175
1176static void hdpvr_device_release(struct video_device *vdev)
1177{
1178 struct hdpvr_device *dev = video_get_drvdata(vdev);
1179
1180 hdpvr_delete(dev);
1181}
1182
1183static const struct video_device hdpvr_video_template = {
1184/* .type = VFL_TYPE_GRABBER, */
1185/* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1186 .fops = &hdpvr_fops,
1187 .release = hdpvr_device_release,
1188 .ioctl_ops = &hdpvr_ioctl_ops,
1189 .tvnorms =
1190 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1191 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1192 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1193 V4L2_STD_PAL_60,
1194};
1195
1196int hdpvr_register_videodev(struct hdpvr_device *dev, int devnum)
1197{
1198 /* setup and register video device */
1199 dev->video_dev = video_device_alloc();
1200 if (!dev->video_dev) {
1201 err("video_device_alloc() failed");
1202 goto error;
1203 }
1204
1205 *(dev->video_dev) = hdpvr_video_template;
1206 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1207 dev->video_dev->parent = &dev->udev->dev;
1208 video_set_drvdata(dev->video_dev, dev);
1209
1210 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1211 err("V4L2 device registration failed");
1212 goto error;
1213 }
1214
1215 return 0;
1216error:
1217 return -ENOMEM;
1218}
This page took 0.076795 seconds and 5 git commands to generate.