[media] hdpvr: add g/querystd, remove deprecated current_norm
[deliverable/linux.git] / drivers / media / usb / 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>
54d80904 13#include <linux/kconfig.h>
9aba42ef
JG
14#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/uaccess.h>
19#include <linux/usb.h>
20#include <linux/mutex.h>
9aba42ef
JG
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>
65fe42d6 27#include <media/v4l2-event.h>
9aba42ef
JG
28#include "hdpvr.h"
29
39bcb3ae 30#define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
9aba42ef 31
9ef77adf
JG
32#define print_buffer_status() { \
33 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
34 "%s:%d buffer stat: %d free, %d proc\n", \
35 __func__, __LINE__, \
36 list_size(&dev->free_buff_list), \
37 list_size(&dev->rec_buff_list)); }
d211bfcb 38
9aba42ef
JG
39static uint list_size(struct list_head *list)
40{
41 struct list_head *tmp;
42 uint count = 0;
43
44 list_for_each(tmp, list) {
45 count++;
46 }
47
48 return count;
49}
50
51/*=========================================================================*/
52/* urb callback */
53static void hdpvr_read_bulk_callback(struct urb *urb)
54{
55 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
56 struct hdpvr_device *dev = buf->dev;
57
58 /* marking buffer as received and wake waiting */
59 buf->status = BUFSTAT_READY;
60 wake_up_interruptible(&dev->wait_data);
61}
62
63/*=========================================================================*/
64/* bufffer bits */
65
66/* function expects dev->io_mutex to be hold by caller */
67int hdpvr_cancel_queue(struct hdpvr_device *dev)
68{
69 struct hdpvr_buffer *buf;
70
71 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
72 usb_kill_urb(buf->urb);
73 buf->status = BUFSTAT_AVAILABLE;
74 }
75
76 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
77
78 return 0;
79}
80
81static int hdpvr_free_queue(struct list_head *q)
82{
83 struct list_head *tmp;
84 struct list_head *p;
85 struct hdpvr_buffer *buf;
86 struct urb *urb;
87
88 for (p = q->next; p != q;) {
89 buf = list_entry(p, struct hdpvr_buffer, buff_list);
90
91 urb = buf->urb;
997ea58e
DM
92 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
93 urb->transfer_buffer, urb->transfer_dma);
9aba42ef
JG
94 usb_free_urb(urb);
95 tmp = p->next;
96 list_del(p);
97 kfree(buf);
98 p = tmp;
99 }
100
101 return 0;
102}
103
104/* function expects dev->io_mutex to be hold by caller */
105int hdpvr_free_buffers(struct hdpvr_device *dev)
106{
107 hdpvr_cancel_queue(dev);
108
109 hdpvr_free_queue(&dev->free_buff_list);
110 hdpvr_free_queue(&dev->rec_buff_list);
111
112 return 0;
113}
114
115/* function expects dev->io_mutex to be hold by caller */
116int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
117{
118 uint i;
119 int retval = -ENOMEM;
120 u8 *mem;
121 struct hdpvr_buffer *buf;
122 struct urb *urb;
123
9ef77adf 124 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
9aba42ef
JG
125 "allocating %u buffers\n", count);
126
127 for (i = 0; i < count; i++) {
128
129 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
130 if (!buf) {
9ef77adf 131 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
9aba42ef
JG
132 goto exit;
133 }
134 buf->dev = dev;
135
136 urb = usb_alloc_urb(0, GFP_KERNEL);
137 if (!urb) {
9ef77adf 138 v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
cd0e280f 139 goto exit_urb;
9aba42ef
JG
140 }
141 buf->urb = urb;
142
997ea58e
DM
143 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
144 &urb->transfer_dma);
9aba42ef 145 if (!mem) {
9ef77adf
JG
146 v4l2_err(&dev->v4l2_dev,
147 "cannot allocate usb transfer buffer\n");
cd0e280f 148 goto exit_urb_buffer;
9aba42ef
JG
149 }
150
151 usb_fill_bulk_urb(buf->urb, dev->udev,
152 usb_rcvbulkpipe(dev->udev,
153 dev->bulk_in_endpointAddr),
154 mem, dev->bulk_in_size,
155 hdpvr_read_bulk_callback, buf);
156
4f5c933a 157 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
9aba42ef
JG
158 buf->status = BUFSTAT_AVAILABLE;
159 list_add_tail(&buf->buff_list, &dev->free_buff_list);
160 }
161 return 0;
cd0e280f
JL
162exit_urb_buffer:
163 usb_free_urb(urb);
164exit_urb:
165 kfree(buf);
9aba42ef
JG
166exit:
167 hdpvr_free_buffers(dev);
168 return retval;
169}
170
171static int hdpvr_submit_buffers(struct hdpvr_device *dev)
172{
173 struct hdpvr_buffer *buf;
174 struct urb *urb;
175 int ret = 0, err_count = 0;
176
177 mutex_lock(&dev->io_mutex);
178
179 while (dev->status == STATUS_STREAMING &&
180 !list_empty(&dev->free_buff_list)) {
181
182 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
183 buff_list);
184 if (buf->status != BUFSTAT_AVAILABLE) {
9ef77adf 185 v4l2_err(&dev->v4l2_dev,
4b512d26 186 "buffer not marked as available\n");
9aba42ef
JG
187 ret = -EFAULT;
188 goto err;
189 }
190
191 urb = buf->urb;
192 urb->status = 0;
193 urb->actual_length = 0;
194 ret = usb_submit_urb(urb, GFP_KERNEL);
195 if (ret) {
9ef77adf
JG
196 v4l2_err(&dev->v4l2_dev,
197 "usb_submit_urb in %s returned %d\n",
198 __func__, ret);
9aba42ef
JG
199 if (++err_count > 2)
200 break;
201 continue;
202 }
203 buf->status = BUFSTAT_INPROGRESS;
204 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
205 }
206err:
d211bfcb 207 print_buffer_status();
9aba42ef
JG
208 mutex_unlock(&dev->io_mutex);
209 return ret;
210}
211
212static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
213{
214 struct hdpvr_buffer *buf;
215
216 mutex_lock(&dev->io_mutex);
217
218 if (list_empty(&dev->rec_buff_list)) {
219 mutex_unlock(&dev->io_mutex);
220 return NULL;
221 }
222
223 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
224 buff_list);
225 mutex_unlock(&dev->io_mutex);
226
227 return buf;
228}
229
230static void hdpvr_transmit_buffers(struct work_struct *work)
231{
232 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
233 worker);
234
235 while (dev->status == STATUS_STREAMING) {
236
237 if (hdpvr_submit_buffers(dev)) {
9ef77adf 238 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
9aba42ef
JG
239 goto error;
240 }
241 if (wait_event_interruptible(dev->wait_buffer,
242 !list_empty(&dev->free_buff_list) ||
243 dev->status != STATUS_STREAMING))
244 goto error;
245 }
246
9ef77adf 247 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
9aba42ef
JG
248 "transmit worker exited\n");
249 return;
250error:
9ef77adf 251 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
9aba42ef
JG
252 "transmit buffers errored\n");
253 dev->status = STATUS_ERROR;
254}
255
256/* function expects dev->io_mutex to be hold by caller */
257static int hdpvr_start_streaming(struct hdpvr_device *dev)
258{
259 int ret;
260 struct hdpvr_video_info *vidinf;
261
262 if (dev->status == STATUS_STREAMING)
263 return 0;
264 else if (dev->status != STATUS_IDLE)
265 return -EAGAIN;
266
267 vidinf = get_video_info(dev);
268
269 if (vidinf) {
9ef77adf 270 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
9aba42ef
JG
271 "video signal: %dx%d@%dhz\n", vidinf->width,
272 vidinf->height, vidinf->fps);
273 kfree(vidinf);
274
275 /* start streaming 2 request */
276 ret = usb_control_msg(dev->udev,
277 usb_sndctrlpipe(dev->udev, 0),
278 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
9ef77adf 279 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
9aba42ef
JG
280 "encoder start control request returned %d\n", ret);
281
282 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
283
afa15953
JG
284 dev->status = STATUS_STREAMING;
285
9aba42ef
JG
286 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
287 queue_work(dev->workqueue, &dev->worker);
288
9ef77adf 289 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
9aba42ef 290 "streaming started\n");
9aba42ef
JG
291
292 return 0;
293 }
294 msleep(250);
9ef77adf 295 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
9aba42ef
JG
296 "no video signal at input %d\n", dev->options.video_input);
297 return -EAGAIN;
298}
299
300
301/* function expects dev->io_mutex to be hold by caller */
302static int hdpvr_stop_streaming(struct hdpvr_device *dev)
303{
85d682b9
MN
304 int actual_length;
305 uint c = 0;
7d771ff0
JG
306 u8 *buf;
307
9aba42ef
JG
308 if (dev->status == STATUS_IDLE)
309 return 0;
310 else if (dev->status != STATUS_STREAMING)
311 return -EAGAIN;
312
7d771ff0
JG
313 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
314 if (!buf)
315 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
316 "for emptying the internal device buffer. "
317 "Next capture start will be slow\n");
318
9aba42ef
JG
319 dev->status = STATUS_SHUTTING_DOWN;
320 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
48f98f75 321 mutex_unlock(&dev->io_mutex);
9aba42ef
JG
322
323 wake_up_interruptible(&dev->wait_buffer);
324 msleep(50);
325
326 flush_workqueue(dev->workqueue);
327
48f98f75 328 mutex_lock(&dev->io_mutex);
9aba42ef
JG
329 /* kill the still outstanding urbs */
330 hdpvr_cancel_queue(dev);
331
7d771ff0
JG
332 /* emptying the device buffer beforeshutting it down */
333 while (buf && ++c < 500 &&
334 !usb_bulk_msg(dev->udev,
335 usb_rcvbulkpipe(dev->udev,
336 dev->bulk_in_endpointAddr),
337 buf, dev->bulk_in_size, &actual_length,
338 BULK_URB_TIMEOUT)) {
7d771ff0
JG
339 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
340 "%2d: got %d bytes\n", c, actual_length);
341 }
342 kfree(buf);
343 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
344 "used %d urbs to empty device buffers\n", c-1);
345 msleep(10);
346
9aba42ef
JG
347 dev->status = STATUS_IDLE;
348
349 return 0;
350}
351
352
353/*=======================================================================*/
354/*
355 * video 4 linux 2 file operations
356 */
357
9aba42ef
JG
358static int hdpvr_release(struct file *file)
359{
ede197aa 360 struct hdpvr_device *dev = video_drvdata(file);
9aba42ef
JG
361
362 if (!dev)
363 return -ENODEV;
364
365 mutex_lock(&dev->io_mutex);
ede197aa 366 if (file->private_data == dev->owner) {
9aba42ef 367 hdpvr_stop_streaming(dev);
ede197aa
HV
368 dev->owner = NULL;
369 }
9aba42ef
JG
370 mutex_unlock(&dev->io_mutex);
371
ede197aa 372 return v4l2_fh_release(file);
9aba42ef
JG
373}
374
375/*
376 * hdpvr_v4l2_read()
377 * will allocate buffers when called for the first time
378 */
379static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
380 loff_t *pos)
381{
ede197aa 382 struct hdpvr_device *dev = video_drvdata(file);
9aba42ef
JG
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)) {
9ef77adf
JG
397 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
398 "start_streaming failed\n");
9aba42ef
JG
399 ret = -EIO;
400 msleep(200);
401 dev->status = STATUS_IDLE;
402 mutex_unlock(&dev->io_mutex);
403 goto err;
404 }
ede197aa 405 dev->owner = file->private_data;
d211bfcb 406 print_buffer_status();
9aba42ef
JG
407 }
408 mutex_unlock(&dev->io_mutex);
409
410 /* wait for the first buffer */
411 if (!(file->f_flags & O_NONBLOCK)) {
412 if (wait_event_interruptible(dev->wait_data,
413 hdpvr_get_next_buffer(dev)))
414 return -ERESTARTSYS;
415 }
416
417 buf = hdpvr_get_next_buffer(dev);
418
419 while (count > 0 && buf) {
420
421 if (buf->status != BUFSTAT_READY &&
422 dev->status != STATUS_DISCONNECTED) {
423 /* return nonblocking */
424 if (file->f_flags & O_NONBLOCK) {
425 if (!ret)
426 ret = -EAGAIN;
427 goto err;
428 }
429
430 if (wait_event_interruptible(dev->wait_data,
431 buf->status == BUFSTAT_READY)) {
432 ret = -ERESTARTSYS;
433 goto err;
434 }
435 }
436
437 if (buf->status != BUFSTAT_READY)
438 break;
439
440 /* set remaining bytes to copy */
441 urb = buf->urb;
442 rem = urb->actual_length - buf->pos;
443 cnt = rem > count ? count : rem;
444
445 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
446 cnt)) {
9ef77adf 447 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
9aba42ef
JG
448 if (!ret)
449 ret = -EFAULT;
450 goto err;
451 }
452
453 buf->pos += cnt;
454 count -= cnt;
455 buffer += cnt;
456 ret += cnt;
457
458 /* finished, take next buffer */
459 if (buf->pos == urb->actual_length) {
460 mutex_lock(&dev->io_mutex);
461 buf->pos = 0;
462 buf->status = BUFSTAT_AVAILABLE;
463
464 list_move_tail(&buf->buff_list, &dev->free_buff_list);
465
d211bfcb 466 print_buffer_status();
9aba42ef
JG
467
468 mutex_unlock(&dev->io_mutex);
469
470 wake_up_interruptible(&dev->wait_buffer);
471
472 buf = hdpvr_get_next_buffer(dev);
473 }
474 }
475err:
476 if (!ret && !buf)
477 ret = -EAGAIN;
478 return ret;
479}
480
481static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
482{
65fe42d6 483 unsigned long req_events = poll_requested_events(wait);
d2ff3ec8 484 struct hdpvr_buffer *buf = NULL;
ede197aa 485 struct hdpvr_device *dev = video_drvdata(filp);
65fe42d6 486 unsigned int mask = v4l2_ctrl_poll(filp, wait);
9aba42ef 487
65fe42d6
HV
488 if (!(req_events & (POLLIN | POLLRDNORM)))
489 return mask;
9aba42ef 490
65fe42d6 491 mutex_lock(&dev->io_mutex);
9aba42ef
JG
492
493 if (dev->status == STATUS_IDLE) {
494 if (hdpvr_start_streaming(dev)) {
9ef77adf
JG
495 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
496 "start_streaming failed\n");
9aba42ef 497 dev->status = STATUS_IDLE;
ede197aa
HV
498 } else {
499 dev->owner = filp->private_data;
9aba42ef
JG
500 }
501
d211bfcb 502 print_buffer_status();
9aba42ef
JG
503 }
504 mutex_unlock(&dev->io_mutex);
505
d2ff3ec8
JG
506 buf = hdpvr_get_next_buffer(dev);
507 /* only wait if no data is available */
508 if (!buf || buf->status != BUFSTAT_READY) {
509 poll_wait(filp, &dev->wait_data, wait);
510 buf = hdpvr_get_next_buffer(dev);
9aba42ef 511 }
d2ff3ec8
JG
512 if (buf && buf->status == BUFSTAT_READY)
513 mask |= POLLIN | POLLRDNORM;
9aba42ef
JG
514
515 return mask;
516}
517
518
9aba42ef
JG
519static const struct v4l2_file_operations hdpvr_fops = {
520 .owner = THIS_MODULE,
ede197aa 521 .open = v4l2_fh_open,
9aba42ef
JG
522 .release = hdpvr_release,
523 .read = hdpvr_read,
524 .poll = hdpvr_poll,
76717b88 525 .unlocked_ioctl = video_ioctl2,
9aba42ef
JG
526};
527
528/*=======================================================================*/
529/*
530 * V4L2 ioctl handling
531 */
532
533static int vidioc_querycap(struct file *file, void *priv,
534 struct v4l2_capability *cap)
535{
536 struct hdpvr_device *dev = video_drvdata(file);
537
538 strcpy(cap->driver, "hdpvr");
fdd70c33 539 strcpy(cap->card, "Hauppauge HD PVR");
9aba42ef 540 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
41022fcb
HV
541 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
542 V4L2_CAP_READWRITE;
543 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
9aba42ef
JG
544 return 0;
545}
546
547static int vidioc_s_std(struct file *file, void *private_data,
314527ac 548 v4l2_std_id std)
9aba42ef 549{
ede197aa 550 struct hdpvr_device *dev = video_drvdata(file);
9aba42ef
JG
551 u8 std_type = 1;
552
8f69da95
HV
553 if (dev->options.video_input == HDPVR_COMPONENT)
554 return -ENODATA;
555 if (dev->status != STATUS_IDLE)
556 return -EBUSY;
557 if (std & V4L2_STD_525_60)
9aba42ef 558 std_type = 0;
8f69da95
HV
559 dev->cur_std = std;
560 dev->width = 720;
561 dev->height = std_type ? 576 : 480;
9aba42ef
JG
562
563 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
564}
565
8f69da95
HV
566static int vidioc_g_std(struct file *file, void *private_data,
567 v4l2_std_id *std)
568{
569 struct hdpvr_device *dev = video_drvdata(file);
570
571 if (dev->options.video_input == HDPVR_COMPONENT)
572 return -ENODATA;
573 *std = dev->cur_std;
574 return 0;
575}
576
577static int vidioc_querystd(struct file *file, void *fh, v4l2_std_id *a)
578{
579 struct hdpvr_device *dev = video_drvdata(file);
580 struct hdpvr_video_info *vid_info;
581
582 if (dev->options.video_input == HDPVR_COMPONENT)
583 return -ENODATA;
584 *a = V4L2_STD_ALL;
585 vid_info = get_video_info(dev);
586 if (vid_info == NULL)
587 return 0;
588 if (vid_info->width == 720 &&
589 (vid_info->height == 480 || vid_info->height == 576)) {
590 *a = (vid_info->height == 480) ?
591 V4L2_STD_525_60 : V4L2_STD_625_50;
592 }
593 kfree(vid_info);
594 return 0;
595}
596
9aba42ef
JG
597static const char *iname[] = {
598 [HDPVR_COMPONENT] = "Component",
599 [HDPVR_SVIDEO] = "S-Video",
600 [HDPVR_COMPOSITE] = "Composite",
601};
602
603static int vidioc_enum_input(struct file *file, void *priv,
604 struct v4l2_input *i)
605{
9aba42ef
JG
606 unsigned int n;
607
608 n = i->index;
609 if (n >= HDPVR_VIDEO_INPUTS)
610 return -EINVAL;
611
612 i->type = V4L2_INPUT_TYPE_CAMERA;
613
614 strncpy(i->name, iname[n], sizeof(i->name) - 1);
615 i->name[sizeof(i->name) - 1] = '\0';
616
617 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
618
8f69da95
HV
619 i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
620 i->std = n ? V4L2_STD_ALL : 0;
9aba42ef
JG
621
622 return 0;
623}
624
625static int vidioc_s_input(struct file *file, void *private_data,
626 unsigned int index)
627{
ede197aa 628 struct hdpvr_device *dev = video_drvdata(file);
9aba42ef
JG
629 int retval;
630
631 if (index >= HDPVR_VIDEO_INPUTS)
632 return -EINVAL;
633
634 if (dev->status != STATUS_IDLE)
97caa318 635 return -EBUSY;
9aba42ef
JG
636
637 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
8f69da95 638 if (!retval) {
9aba42ef 639 dev->options.video_input = index;
8f69da95
HV
640 dev->video_dev->tvnorms =
641 index != HDPVR_COMPONENT ? V4L2_STD_ALL : 0;
642 }
9aba42ef
JG
643
644 return retval;
645}
646
647static int vidioc_g_input(struct file *file, void *private_data,
648 unsigned int *index)
649{
ede197aa 650 struct hdpvr_device *dev = video_drvdata(file);
9aba42ef
JG
651
652 *index = dev->options.video_input;
653 return 0;
654}
655
656
657static const char *audio_iname[] = {
658 [HDPVR_RCA_FRONT] = "RCA front",
659 [HDPVR_RCA_BACK] = "RCA back",
660 [HDPVR_SPDIF] = "SPDIF",
661};
662
663static int vidioc_enumaudio(struct file *file, void *priv,
664 struct v4l2_audio *audio)
665{
666 unsigned int n;
667
668 n = audio->index;
669 if (n >= HDPVR_AUDIO_INPUTS)
670 return -EINVAL;
671
672 audio->capability = V4L2_AUDCAP_STEREO;
673
674 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
675 audio->name[sizeof(audio->name) - 1] = '\0';
676
677 return 0;
678}
679
680static int vidioc_s_audio(struct file *file, void *private_data,
0e8025b9 681 const struct v4l2_audio *audio)
9aba42ef 682{
ede197aa 683 struct hdpvr_device *dev = video_drvdata(file);
9aba42ef
JG
684 int retval;
685
686 if (audio->index >= HDPVR_AUDIO_INPUTS)
687 return -EINVAL;
688
689 if (dev->status != STATUS_IDLE)
97caa318 690 return -EBUSY;
9aba42ef
JG
691
692 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
693 if (!retval)
694 dev->options.audio_input = audio->index;
695
696 return retval;
697}
698
699static int vidioc_g_audio(struct file *file, void *private_data,
700 struct v4l2_audio *audio)
701{
ede197aa 702 struct hdpvr_device *dev = video_drvdata(file);
9aba42ef
JG
703
704 audio->index = dev->options.audio_input;
705 audio->capability = V4L2_AUDCAP_STEREO;
706 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
707 audio->name[sizeof(audio->name) - 1] = '\0';
708 return 0;
709}
710
99c77aa4 711static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
9aba42ef 712{
99c77aa4
HV
713 struct hdpvr_device *dev =
714 container_of(ctrl->handler, struct hdpvr_device, hdl);
9aba42ef
JG
715
716 switch (ctrl->id) {
99c77aa4
HV
717 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
718 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
719 dev->video_bitrate->val >= dev->video_bitrate_peak->val)
720 dev->video_bitrate_peak->val =
721 dev->video_bitrate->val + 100000;
9aba42ef 722 break;
9aba42ef
JG
723 }
724 return 0;
725}
726
99c77aa4 727static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
9aba42ef 728{
99c77aa4
HV
729 struct hdpvr_device *dev =
730 container_of(ctrl->handler, struct hdpvr_device, hdl);
731 struct hdpvr_options *opt = &dev->options;
732 int ret = -EINVAL;
9aba42ef
JG
733
734 switch (ctrl->id) {
735 case V4L2_CID_BRIGHTNESS:
99c77aa4
HV
736 ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
737 if (ret)
738 break;
739 dev->options.brightness = ctrl->val;
740 return 0;
9aba42ef 741 case V4L2_CID_CONTRAST:
99c77aa4
HV
742 ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
743 if (ret)
744 break;
745 dev->options.contrast = ctrl->val;
746 return 0;
9aba42ef 747 case V4L2_CID_SATURATION:
99c77aa4
HV
748 ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
749 if (ret)
750 break;
751 dev->options.saturation = ctrl->val;
752 return 0;
9aba42ef 753 case V4L2_CID_HUE:
99c77aa4
HV
754 ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
755 if (ret)
756 break;
757 dev->options.hue = ctrl->val;
758 return 0;
9aba42ef 759 case V4L2_CID_SHARPNESS:
99c77aa4
HV
760 ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
761 if (ret)
762 break;
763 dev->options.sharpness = ctrl->val;
764 return 0;
9aba42ef
JG
765 case V4L2_CID_MPEG_AUDIO_ENCODING:
766 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
99c77aa4
HV
767 opt->audio_codec = ctrl->val;
768 return hdpvr_set_audio(dev, opt->audio_input,
9aba42ef
JG
769 opt->audio_codec);
770 }
99c77aa4 771 return 0;
9aba42ef 772 case V4L2_CID_MPEG_VIDEO_ENCODING:
99c77aa4 773 return 0;
9aba42ef
JG
774/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
775/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
776/* opt->gop_mode |= 0x2; */
777/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
778/* opt->gop_mode); */
779/* } */
780/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
781/* opt->gop_mode &= ~0x2; */
782/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
783/* opt->gop_mode); */
784/* } */
785/* break; */
99c77aa4
HV
786 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
787 uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
788 uint bitrate = dev->video_bitrate->val / 100000;
789
790 if (ctrl->is_new) {
791 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
792 opt->bitrate_mode = HDPVR_CONSTANT;
793 else
794 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
9aba42ef
JG
795 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
796 opt->bitrate_mode);
99c77aa4
HV
797 v4l2_ctrl_activate(dev->video_bitrate_peak,
798 ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
9aba42ef 799 }
9aba42ef 800
99c77aa4
HV
801 if (dev->video_bitrate_peak->is_new ||
802 dev->video_bitrate->is_new) {
803 opt->bitrate = bitrate;
9aba42ef
JG
804 opt->peak_bitrate = peak_bitrate;
805 hdpvr_set_bitrate(dev);
99c77aa4
HV
806 }
807 return 0;
9aba42ef
JG
808 }
809 case V4L2_CID_MPEG_STREAM_TYPE:
99c77aa4 810 return 0;
9aba42ef 811 default:
99c77aa4 812 break;
9aba42ef
JG
813 }
814 return ret;
815}
816
9aba42ef
JG
817static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
818 struct v4l2_fmtdesc *f)
819{
97caa318 820 if (f->index != 0)
9aba42ef
JG
821 return -EINVAL;
822
823 f->flags = V4L2_FMT_FLAG_COMPRESSED;
824 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
825 f->pixelformat = V4L2_PIX_FMT_MPEG;
826
827 return 0;
828}
829
830static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
831 struct v4l2_format *f)
832{
ede197aa 833 struct hdpvr_device *dev = video_drvdata(file);
9aba42ef
JG
834 struct hdpvr_video_info *vid_info;
835
836 if (!dev)
837 return -ENODEV;
838
839 vid_info = get_video_info(dev);
840 if (!vid_info)
841 return -EFAULT;
842
843 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
844 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
845 f->fmt.pix.width = vid_info->width;
846 f->fmt.pix.height = vid_info->height;
847 f->fmt.pix.sizeimage = dev->bulk_in_size;
848 f->fmt.pix.colorspace = 0;
849 f->fmt.pix.bytesperline = 0;
850 f->fmt.pix.field = V4L2_FIELD_ANY;
851
852 kfree(vid_info);
853 return 0;
854}
855
76717b88
JG
856static int vidioc_encoder_cmd(struct file *filp, void *priv,
857 struct v4l2_encoder_cmd *a)
858{
ede197aa
HV
859 struct hdpvr_device *dev = video_drvdata(filp);
860 int res = 0;
76717b88
JG
861
862 mutex_lock(&dev->io_mutex);
ede197aa 863 a->flags = 0;
76717b88 864
76717b88
JG
865 switch (a->cmd) {
866 case V4L2_ENC_CMD_START:
ede197aa
HV
867 if (dev->owner && filp->private_data != dev->owner) {
868 res = -EBUSY;
869 break;
870 }
871 if (dev->status == STATUS_STREAMING)
872 break;
76717b88 873 res = hdpvr_start_streaming(dev);
ede197aa
HV
874 if (!res)
875 dev->owner = filp->private_data;
876 else
877 dev->status = STATUS_IDLE;
76717b88
JG
878 break;
879 case V4L2_ENC_CMD_STOP:
ede197aa
HV
880 if (dev->owner && filp->private_data != dev->owner) {
881 res = -EBUSY;
882 break;
883 }
884 if (dev->status == STATUS_IDLE)
885 break;
76717b88 886 res = hdpvr_stop_streaming(dev);
ede197aa
HV
887 if (!res)
888 dev->owner = NULL;
76717b88
JG
889 break;
890 default:
9ef77adf 891 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
76717b88 892 "Unsupported encoder cmd %d\n", a->cmd);
48f98f75 893 res = -EINVAL;
ede197aa 894 break;
76717b88 895 }
ede197aa 896
76717b88
JG
897 mutex_unlock(&dev->io_mutex);
898 return res;
899}
900
901static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
902 struct v4l2_encoder_cmd *a)
903{
ede197aa 904 a->flags = 0;
76717b88
JG
905 switch (a->cmd) {
906 case V4L2_ENC_CMD_START:
907 case V4L2_ENC_CMD_STOP:
908 return 0;
909 default:
910 return -EINVAL;
911 }
912}
9aba42ef
JG
913
914static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
915 .vidioc_querycap = vidioc_querycap,
916 .vidioc_s_std = vidioc_s_std,
8f69da95
HV
917 .vidioc_g_std = vidioc_g_std,
918 .vidioc_querystd = vidioc_querystd,
9aba42ef
JG
919 .vidioc_enum_input = vidioc_enum_input,
920 .vidioc_g_input = vidioc_g_input,
921 .vidioc_s_input = vidioc_s_input,
922 .vidioc_enumaudio = vidioc_enumaudio,
923 .vidioc_g_audio = vidioc_g_audio,
924 .vidioc_s_audio = vidioc_s_audio,
65fe42d6
HV
925 .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
926 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
76717b88
JG
927 .vidioc_encoder_cmd = vidioc_encoder_cmd,
928 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
65fe42d6
HV
929 .vidioc_log_status = v4l2_ctrl_log_status,
930 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
931 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
9aba42ef
JG
932};
933
934static void hdpvr_device_release(struct video_device *vdev)
935{
936 struct hdpvr_device *dev = video_get_drvdata(vdev);
937
938 hdpvr_delete(dev);
f2b305cd
HV
939 mutex_lock(&dev->io_mutex);
940 destroy_workqueue(dev->workqueue);
941 mutex_unlock(&dev->io_mutex);
942
943 v4l2_device_unregister(&dev->v4l2_dev);
99c77aa4 944 v4l2_ctrl_handler_free(&dev->hdl);
f2b305cd
HV
945
946 /* deregister I2C adapter */
54d80904 947#if IS_ENABLED(CONFIG_I2C)
f2b305cd 948 mutex_lock(&dev->i2c_mutex);
324b04ba 949 i2c_del_adapter(&dev->i2c_adapter);
f2b305cd
HV
950 mutex_unlock(&dev->i2c_mutex);
951#endif /* CONFIG_I2C */
952
953 kfree(dev->usbc_buf);
954 kfree(dev);
9aba42ef
JG
955}
956
957static const struct video_device hdpvr_video_template = {
9aba42ef
JG
958 .fops = &hdpvr_fops,
959 .release = hdpvr_device_release,
960 .ioctl_ops = &hdpvr_ioctl_ops,
9aba42ef
JG
961};
962
99c77aa4
HV
963static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
964 .try_ctrl = hdpvr_try_ctrl,
965 .s_ctrl = hdpvr_s_ctrl,
966};
967
a50ab291
JG
968int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
969 int devnum)
9aba42ef 970{
99c77aa4
HV
971 struct v4l2_ctrl_handler *hdl = &dev->hdl;
972 bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
973 int res;
974
8f69da95
HV
975 dev->cur_std = V4L2_STD_525_60;
976 dev->width = 720;
977 dev->height = 480;
99c77aa4
HV
978 v4l2_ctrl_handler_init(hdl, 11);
979 if (dev->fw_ver > 0x15) {
980 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
981 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
982 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
983 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
984 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
985 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
986 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
987 V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
988 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
989 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
990 } else {
991 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
992 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
993 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
994 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
995 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
996 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
997 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
998 V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
999 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1000 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1001 }
1002
1003 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1004 V4L2_CID_MPEG_STREAM_TYPE,
1005 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
1006 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
1007 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1008 V4L2_CID_MPEG_AUDIO_ENCODING,
1009 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
1010 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC);
1011 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1012 V4L2_CID_MPEG_VIDEO_ENCODING,
1013 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
1014 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
1015
1016 dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1017 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1018 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1019 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1020
1021 dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1022 V4L2_CID_MPEG_VIDEO_BITRATE,
1023 1000000, 13500000, 100000, 6500000);
1024 dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1025 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1026 1100000, 20200000, 100000, 9000000);
1027 dev->v4l2_dev.ctrl_handler = hdl;
1028 if (hdl->error) {
1029 res = hdl->error;
1030 v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
1031 goto error;
1032 }
1033 v4l2_ctrl_cluster(3, &dev->video_mode);
1034 res = v4l2_ctrl_handler_setup(hdl);
1035 if (res < 0) {
1036 v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
1037 goto error;
1038 }
1039
9aba42ef
JG
1040 /* setup and register video device */
1041 dev->video_dev = video_device_alloc();
1042 if (!dev->video_dev) {
9ef77adf 1043 v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
99c77aa4 1044 res = -ENOMEM;
9aba42ef
JG
1045 goto error;
1046 }
1047
ede197aa 1048 *dev->video_dev = hdpvr_video_template;
9aba42ef 1049 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
ede197aa 1050 dev->video_dev->v4l2_dev = &dev->v4l2_dev;
9aba42ef 1051 video_set_drvdata(dev->video_dev, dev);
65fe42d6 1052 set_bit(V4L2_FL_USE_FH_PRIO, &dev->video_dev->flags);
9aba42ef 1053
99c77aa4
HV
1054 res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum);
1055 if (res < 0) {
9ef77adf 1056 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
9aba42ef
JG
1057 goto error;
1058 }
1059
1060 return 0;
1061error:
99c77aa4
HV
1062 v4l2_ctrl_handler_free(hdl);
1063 return res;
9aba42ef 1064}
This page took 0.500306 seconds and 5 git commands to generate.