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