Path: blob/master/drivers/media/video/hdpvr/hdpvr-video.c
17687 views
/*1* Hauppauge HD PVR USB driver - video 4 linux 2 interface2*3* Copyright (C) 2008 Janne Grunau ([email protected])4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public License as7* published by the Free Software Foundation, version 2.8*9*/1011#include <linux/kernel.h>12#include <linux/errno.h>13#include <linux/init.h>14#include <linux/slab.h>15#include <linux/module.h>16#include <linux/uaccess.h>17#include <linux/usb.h>18#include <linux/mutex.h>19#include <linux/version.h>20#include <linux/workqueue.h>2122#include <linux/videodev2.h>23#include <media/v4l2-dev.h>24#include <media/v4l2-common.h>25#include <media/v4l2-ioctl.h>26#include "hdpvr.h"2728#define BULK_URB_TIMEOUT 90 /* 0.09 seconds */2930#define print_buffer_status() { \31v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \32"%s:%d buffer stat: %d free, %d proc\n", \33__func__, __LINE__, \34list_size(&dev->free_buff_list), \35list_size(&dev->rec_buff_list)); }3637struct hdpvr_fh {38struct hdpvr_device *dev;39};4041static uint list_size(struct list_head *list)42{43struct list_head *tmp;44uint count = 0;4546list_for_each(tmp, list) {47count++;48}4950return count;51}5253/*=========================================================================*/54/* urb callback */55static void hdpvr_read_bulk_callback(struct urb *urb)56{57struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;58struct hdpvr_device *dev = buf->dev;5960/* marking buffer as received and wake waiting */61buf->status = BUFSTAT_READY;62wake_up_interruptible(&dev->wait_data);63}6465/*=========================================================================*/66/* bufffer bits */6768/* function expects dev->io_mutex to be hold by caller */69int hdpvr_cancel_queue(struct hdpvr_device *dev)70{71struct hdpvr_buffer *buf;7273list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {74usb_kill_urb(buf->urb);75buf->status = BUFSTAT_AVAILABLE;76}7778list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);7980return 0;81}8283static int hdpvr_free_queue(struct list_head *q)84{85struct list_head *tmp;86struct list_head *p;87struct hdpvr_buffer *buf;88struct urb *urb;8990for (p = q->next; p != q;) {91buf = list_entry(p, struct hdpvr_buffer, buff_list);9293urb = buf->urb;94usb_free_coherent(urb->dev, urb->transfer_buffer_length,95urb->transfer_buffer, urb->transfer_dma);96usb_free_urb(urb);97tmp = p->next;98list_del(p);99kfree(buf);100p = tmp;101}102103return 0;104}105106/* function expects dev->io_mutex to be hold by caller */107int hdpvr_free_buffers(struct hdpvr_device *dev)108{109hdpvr_cancel_queue(dev);110111hdpvr_free_queue(&dev->free_buff_list);112hdpvr_free_queue(&dev->rec_buff_list);113114return 0;115}116117/* function expects dev->io_mutex to be hold by caller */118int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)119{120uint i;121int retval = -ENOMEM;122u8 *mem;123struct hdpvr_buffer *buf;124struct urb *urb;125126v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,127"allocating %u buffers\n", count);128129for (i = 0; i < count; i++) {130131buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);132if (!buf) {133v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");134goto exit;135}136buf->dev = dev;137138urb = usb_alloc_urb(0, GFP_KERNEL);139if (!urb) {140v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");141goto exit_urb;142}143buf->urb = urb;144145mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,146&urb->transfer_dma);147if (!mem) {148v4l2_err(&dev->v4l2_dev,149"cannot allocate usb transfer buffer\n");150goto exit_urb_buffer;151}152153usb_fill_bulk_urb(buf->urb, dev->udev,154usb_rcvbulkpipe(dev->udev,155dev->bulk_in_endpointAddr),156mem, dev->bulk_in_size,157hdpvr_read_bulk_callback, buf);158159buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;160buf->status = BUFSTAT_AVAILABLE;161list_add_tail(&buf->buff_list, &dev->free_buff_list);162}163return 0;164exit_urb_buffer:165usb_free_urb(urb);166exit_urb:167kfree(buf);168exit:169hdpvr_free_buffers(dev);170return retval;171}172173static int hdpvr_submit_buffers(struct hdpvr_device *dev)174{175struct hdpvr_buffer *buf;176struct urb *urb;177int ret = 0, err_count = 0;178179mutex_lock(&dev->io_mutex);180181while (dev->status == STATUS_STREAMING &&182!list_empty(&dev->free_buff_list)) {183184buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,185buff_list);186if (buf->status != BUFSTAT_AVAILABLE) {187v4l2_err(&dev->v4l2_dev,188"buffer not marked as available\n");189ret = -EFAULT;190goto err;191}192193urb = buf->urb;194urb->status = 0;195urb->actual_length = 0;196ret = usb_submit_urb(urb, GFP_KERNEL);197if (ret) {198v4l2_err(&dev->v4l2_dev,199"usb_submit_urb in %s returned %d\n",200__func__, ret);201if (++err_count > 2)202break;203continue;204}205buf->status = BUFSTAT_INPROGRESS;206list_move_tail(&buf->buff_list, &dev->rec_buff_list);207}208err:209print_buffer_status();210mutex_unlock(&dev->io_mutex);211return ret;212}213214static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)215{216struct hdpvr_buffer *buf;217218mutex_lock(&dev->io_mutex);219220if (list_empty(&dev->rec_buff_list)) {221mutex_unlock(&dev->io_mutex);222return NULL;223}224225buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,226buff_list);227mutex_unlock(&dev->io_mutex);228229return buf;230}231232static void hdpvr_transmit_buffers(struct work_struct *work)233{234struct hdpvr_device *dev = container_of(work, struct hdpvr_device,235worker);236237while (dev->status == STATUS_STREAMING) {238239if (hdpvr_submit_buffers(dev)) {240v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");241goto error;242}243if (wait_event_interruptible(dev->wait_buffer,244!list_empty(&dev->free_buff_list) ||245dev->status != STATUS_STREAMING))246goto error;247}248249v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,250"transmit worker exited\n");251return;252error:253v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,254"transmit buffers errored\n");255dev->status = STATUS_ERROR;256}257258/* function expects dev->io_mutex to be hold by caller */259static int hdpvr_start_streaming(struct hdpvr_device *dev)260{261int ret;262struct hdpvr_video_info *vidinf;263264if (dev->status == STATUS_STREAMING)265return 0;266else if (dev->status != STATUS_IDLE)267return -EAGAIN;268269vidinf = get_video_info(dev);270271if (vidinf) {272v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,273"video signal: %dx%d@%dhz\n", vidinf->width,274vidinf->height, vidinf->fps);275kfree(vidinf);276277/* start streaming 2 request */278ret = usb_control_msg(dev->udev,279usb_sndctrlpipe(dev->udev, 0),2800xb8, 0x38, 0x1, 0, NULL, 0, 8000);281v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,282"encoder start control request returned %d\n", ret);283284hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);285286INIT_WORK(&dev->worker, hdpvr_transmit_buffers);287queue_work(dev->workqueue, &dev->worker);288289v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,290"streaming started\n");291dev->status = STATUS_STREAMING;292293return 0;294}295msleep(250);296v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,297"no video signal at input %d\n", dev->options.video_input);298return -EAGAIN;299}300301302/* function expects dev->io_mutex to be hold by caller */303static int hdpvr_stop_streaming(struct hdpvr_device *dev)304{305int actual_length;306uint c = 0;307u8 *buf;308309if (dev->status == STATUS_IDLE)310return 0;311else if (dev->status != STATUS_STREAMING)312return -EAGAIN;313314buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);315if (!buf)316v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "317"for emptying the internal device buffer. "318"Next capture start will be slow\n");319320dev->status = STATUS_SHUTTING_DOWN;321hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);322mutex_unlock(&dev->io_mutex);323324wake_up_interruptible(&dev->wait_buffer);325msleep(50);326327flush_workqueue(dev->workqueue);328329mutex_lock(&dev->io_mutex);330/* kill the still outstanding urbs */331hdpvr_cancel_queue(dev);332333/* emptying the device buffer beforeshutting it down */334while (buf && ++c < 500 &&335!usb_bulk_msg(dev->udev,336usb_rcvbulkpipe(dev->udev,337dev->bulk_in_endpointAddr),338buf, dev->bulk_in_size, &actual_length,339BULK_URB_TIMEOUT)) {340v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,341"%2d: got %d bytes\n", c, actual_length);342}343kfree(buf);344v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,345"used %d urbs to empty device buffers\n", c-1);346msleep(10);347348dev->status = STATUS_IDLE;349350return 0;351}352353354/*=======================================================================*/355/*356* video 4 linux 2 file operations357*/358359static int hdpvr_open(struct file *file)360{361struct hdpvr_device *dev;362struct hdpvr_fh *fh;363int retval = -ENOMEM;364365dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));366if (!dev) {367pr_err("open failing with with ENODEV\n");368retval = -ENODEV;369goto err;370}371372fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);373if (!fh) {374v4l2_err(&dev->v4l2_dev, "Out of memory\n");375goto err;376}377/* lock the device to allow correctly handling errors378* in resumption */379mutex_lock(&dev->io_mutex);380dev->open_count++;381mutex_unlock(&dev->io_mutex);382383fh->dev = dev;384385/* save our object in the file's private structure */386file->private_data = fh;387388retval = 0;389err:390return retval;391}392393static int hdpvr_release(struct file *file)394{395struct hdpvr_fh *fh = file->private_data;396struct hdpvr_device *dev = fh->dev;397398if (!dev)399return -ENODEV;400401mutex_lock(&dev->io_mutex);402if (!(--dev->open_count) && dev->status == STATUS_STREAMING)403hdpvr_stop_streaming(dev);404405mutex_unlock(&dev->io_mutex);406407return 0;408}409410/*411* hdpvr_v4l2_read()412* will allocate buffers when called for the first time413*/414static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,415loff_t *pos)416{417struct hdpvr_fh *fh = file->private_data;418struct hdpvr_device *dev = fh->dev;419struct hdpvr_buffer *buf = NULL;420struct urb *urb;421unsigned int ret = 0;422int rem, cnt;423424if (*pos)425return -ESPIPE;426427if (!dev)428return -ENODEV;429430mutex_lock(&dev->io_mutex);431if (dev->status == STATUS_IDLE) {432if (hdpvr_start_streaming(dev)) {433v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,434"start_streaming failed\n");435ret = -EIO;436msleep(200);437dev->status = STATUS_IDLE;438mutex_unlock(&dev->io_mutex);439goto err;440}441print_buffer_status();442}443mutex_unlock(&dev->io_mutex);444445/* wait for the first buffer */446if (!(file->f_flags & O_NONBLOCK)) {447if (wait_event_interruptible(dev->wait_data,448hdpvr_get_next_buffer(dev)))449return -ERESTARTSYS;450}451452buf = hdpvr_get_next_buffer(dev);453454while (count > 0 && buf) {455456if (buf->status != BUFSTAT_READY &&457dev->status != STATUS_DISCONNECTED) {458/* return nonblocking */459if (file->f_flags & O_NONBLOCK) {460if (!ret)461ret = -EAGAIN;462goto err;463}464465if (wait_event_interruptible(dev->wait_data,466buf->status == BUFSTAT_READY)) {467ret = -ERESTARTSYS;468goto err;469}470}471472if (buf->status != BUFSTAT_READY)473break;474475/* set remaining bytes to copy */476urb = buf->urb;477rem = urb->actual_length - buf->pos;478cnt = rem > count ? count : rem;479480if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,481cnt)) {482v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");483if (!ret)484ret = -EFAULT;485goto err;486}487488buf->pos += cnt;489count -= cnt;490buffer += cnt;491ret += cnt;492493/* finished, take next buffer */494if (buf->pos == urb->actual_length) {495mutex_lock(&dev->io_mutex);496buf->pos = 0;497buf->status = BUFSTAT_AVAILABLE;498499list_move_tail(&buf->buff_list, &dev->free_buff_list);500501print_buffer_status();502503mutex_unlock(&dev->io_mutex);504505wake_up_interruptible(&dev->wait_buffer);506507buf = hdpvr_get_next_buffer(dev);508}509}510err:511if (!ret && !buf)512ret = -EAGAIN;513return ret;514}515516static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)517{518struct hdpvr_buffer *buf = NULL;519struct hdpvr_fh *fh = filp->private_data;520struct hdpvr_device *dev = fh->dev;521unsigned int mask = 0;522523mutex_lock(&dev->io_mutex);524525if (!video_is_registered(dev->video_dev)) {526mutex_unlock(&dev->io_mutex);527return -EIO;528}529530if (dev->status == STATUS_IDLE) {531if (hdpvr_start_streaming(dev)) {532v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,533"start_streaming failed\n");534dev->status = STATUS_IDLE;535}536537print_buffer_status();538}539mutex_unlock(&dev->io_mutex);540541buf = hdpvr_get_next_buffer(dev);542/* only wait if no data is available */543if (!buf || buf->status != BUFSTAT_READY) {544poll_wait(filp, &dev->wait_data, wait);545buf = hdpvr_get_next_buffer(dev);546}547if (buf && buf->status == BUFSTAT_READY)548mask |= POLLIN | POLLRDNORM;549550return mask;551}552553554static const struct v4l2_file_operations hdpvr_fops = {555.owner = THIS_MODULE,556.open = hdpvr_open,557.release = hdpvr_release,558.read = hdpvr_read,559.poll = hdpvr_poll,560.unlocked_ioctl = video_ioctl2,561};562563/*=======================================================================*/564/*565* V4L2 ioctl handling566*/567568static int vidioc_querycap(struct file *file, void *priv,569struct v4l2_capability *cap)570{571struct hdpvr_device *dev = video_drvdata(file);572573strcpy(cap->driver, "hdpvr");574strcpy(cap->card, "Hauppauge HD PVR");575usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));576cap->version = HDPVR_VERSION;577cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |578V4L2_CAP_AUDIO |579V4L2_CAP_READWRITE;580return 0;581}582583static int vidioc_s_std(struct file *file, void *private_data,584v4l2_std_id *std)585{586struct hdpvr_fh *fh = file->private_data;587struct hdpvr_device *dev = fh->dev;588u8 std_type = 1;589590if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))591std_type = 0;592593return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);594}595596static const char *iname[] = {597[HDPVR_COMPONENT] = "Component",598[HDPVR_SVIDEO] = "S-Video",599[HDPVR_COMPOSITE] = "Composite",600};601602static int vidioc_enum_input(struct file *file, void *priv,603struct v4l2_input *i)604{605struct hdpvr_fh *fh = file->private_data;606struct hdpvr_device *dev = fh->dev;607unsigned int n;608609n = i->index;610if (n >= HDPVR_VIDEO_INPUTS)611return -EINVAL;612613i->type = V4L2_INPUT_TYPE_CAMERA;614615strncpy(i->name, iname[n], sizeof(i->name) - 1);616i->name[sizeof(i->name) - 1] = '\0';617618i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;619620i->std = dev->video_dev->tvnorms;621622return 0;623}624625static int vidioc_s_input(struct file *file, void *private_data,626unsigned int index)627{628struct hdpvr_fh *fh = file->private_data;629struct hdpvr_device *dev = fh->dev;630int retval;631632if (index >= HDPVR_VIDEO_INPUTS)633return -EINVAL;634635if (dev->status != STATUS_IDLE)636return -EAGAIN;637638retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);639if (!retval)640dev->options.video_input = index;641642return retval;643}644645static int vidioc_g_input(struct file *file, void *private_data,646unsigned int *index)647{648struct hdpvr_fh *fh = file->private_data;649struct hdpvr_device *dev = fh->dev;650651*index = dev->options.video_input;652return 0;653}654655656static const char *audio_iname[] = {657[HDPVR_RCA_FRONT] = "RCA front",658[HDPVR_RCA_BACK] = "RCA back",659[HDPVR_SPDIF] = "SPDIF",660};661662static int vidioc_enumaudio(struct file *file, void *priv,663struct v4l2_audio *audio)664{665unsigned int n;666667n = audio->index;668if (n >= HDPVR_AUDIO_INPUTS)669return -EINVAL;670671audio->capability = V4L2_AUDCAP_STEREO;672673strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);674audio->name[sizeof(audio->name) - 1] = '\0';675676return 0;677}678679static int vidioc_s_audio(struct file *file, void *private_data,680struct v4l2_audio *audio)681{682struct hdpvr_fh *fh = file->private_data;683struct hdpvr_device *dev = fh->dev;684int retval;685686if (audio->index >= HDPVR_AUDIO_INPUTS)687return -EINVAL;688689if (dev->status != STATUS_IDLE)690return -EAGAIN;691692retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);693if (!retval)694dev->options.audio_input = audio->index;695696return retval;697}698699static int vidioc_g_audio(struct file *file, void *private_data,700struct v4l2_audio *audio)701{702struct hdpvr_fh *fh = file->private_data;703struct hdpvr_device *dev = fh->dev;704705audio->index = dev->options.audio_input;706audio->capability = V4L2_AUDCAP_STEREO;707strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));708audio->name[sizeof(audio->name) - 1] = '\0';709return 0;710}711712static const s32 supported_v4l2_ctrls[] = {713V4L2_CID_BRIGHTNESS,714V4L2_CID_CONTRAST,715V4L2_CID_SATURATION,716V4L2_CID_HUE,717V4L2_CID_SHARPNESS,718V4L2_CID_MPEG_AUDIO_ENCODING,719V4L2_CID_MPEG_VIDEO_ENCODING,720V4L2_CID_MPEG_VIDEO_BITRATE_MODE,721V4L2_CID_MPEG_VIDEO_BITRATE,722V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,723};724725static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,726int ac3)727{728int err;729730switch (qc->id) {731case V4L2_CID_BRIGHTNESS:732return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);733case V4L2_CID_CONTRAST:734return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);735case V4L2_CID_SATURATION:736return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);737case V4L2_CID_HUE:738return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);739case V4L2_CID_SHARPNESS:740return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);741case V4L2_CID_MPEG_AUDIO_ENCODING:742return v4l2_ctrl_query_fill(743qc, V4L2_MPEG_AUDIO_ENCODING_AAC,744ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3745: V4L2_MPEG_AUDIO_ENCODING_AAC,7461, V4L2_MPEG_AUDIO_ENCODING_AAC);747case V4L2_CID_MPEG_VIDEO_ENCODING:748return v4l2_ctrl_query_fill(749qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,750V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,751V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);752753/* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */754/* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */755case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:756return v4l2_ctrl_query_fill(757qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,758V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,759V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);760761case V4L2_CID_MPEG_VIDEO_BITRATE:762return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,7636500000);764case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:765err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,7669000000);767if (!err && opt->bitrate_mode == HDPVR_CONSTANT)768qc->flags |= V4L2_CTRL_FLAG_INACTIVE;769return err;770default:771return -EINVAL;772}773}774775static int vidioc_queryctrl(struct file *file, void *private_data,776struct v4l2_queryctrl *qc)777{778struct hdpvr_fh *fh = file->private_data;779struct hdpvr_device *dev = fh->dev;780int i, next;781u32 id = qc->id;782783memset(qc, 0, sizeof(*qc));784785next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);786qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;787788for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {789if (next) {790if (qc->id < supported_v4l2_ctrls[i])791qc->id = supported_v4l2_ctrls[i];792else793continue;794}795796if (qc->id == supported_v4l2_ctrls[i])797return fill_queryctrl(&dev->options, qc,798dev->flags & HDPVR_FLAG_AC3_CAP);799800if (qc->id < supported_v4l2_ctrls[i])801break;802}803804return -EINVAL;805}806807static int vidioc_g_ctrl(struct file *file, void *private_data,808struct v4l2_control *ctrl)809{810struct hdpvr_fh *fh = file->private_data;811struct hdpvr_device *dev = fh->dev;812813switch (ctrl->id) {814case V4L2_CID_BRIGHTNESS:815ctrl->value = dev->options.brightness;816break;817case V4L2_CID_CONTRAST:818ctrl->value = dev->options.contrast;819break;820case V4L2_CID_SATURATION:821ctrl->value = dev->options.saturation;822break;823case V4L2_CID_HUE:824ctrl->value = dev->options.hue;825break;826case V4L2_CID_SHARPNESS:827ctrl->value = dev->options.sharpness;828break;829default:830return -EINVAL;831}832return 0;833}834835static int vidioc_s_ctrl(struct file *file, void *private_data,836struct v4l2_control *ctrl)837{838struct hdpvr_fh *fh = file->private_data;839struct hdpvr_device *dev = fh->dev;840int retval;841842switch (ctrl->id) {843case V4L2_CID_BRIGHTNESS:844retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);845if (!retval)846dev->options.brightness = ctrl->value;847break;848case V4L2_CID_CONTRAST:849retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);850if (!retval)851dev->options.contrast = ctrl->value;852break;853case V4L2_CID_SATURATION:854retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);855if (!retval)856dev->options.saturation = ctrl->value;857break;858case V4L2_CID_HUE:859retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);860if (!retval)861dev->options.hue = ctrl->value;862break;863case V4L2_CID_SHARPNESS:864retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);865if (!retval)866dev->options.sharpness = ctrl->value;867break;868default:869return -EINVAL;870}871872return retval;873}874875876static int hdpvr_get_ctrl(struct hdpvr_options *opt,877struct v4l2_ext_control *ctrl)878{879switch (ctrl->id) {880case V4L2_CID_MPEG_AUDIO_ENCODING:881ctrl->value = opt->audio_codec;882break;883case V4L2_CID_MPEG_VIDEO_ENCODING:884ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;885break;886/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */887/* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */888/* break; */889case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:890ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT891? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR892: V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;893break;894case V4L2_CID_MPEG_VIDEO_BITRATE:895ctrl->value = opt->bitrate * 100000;896break;897case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:898ctrl->value = opt->peak_bitrate * 100000;899break;900case V4L2_CID_MPEG_STREAM_TYPE:901ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;902break;903default:904return -EINVAL;905}906return 0;907}908909static int vidioc_g_ext_ctrls(struct file *file, void *priv,910struct v4l2_ext_controls *ctrls)911{912struct hdpvr_fh *fh = file->private_data;913struct hdpvr_device *dev = fh->dev;914int i, err = 0;915916if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {917for (i = 0; i < ctrls->count; i++) {918struct v4l2_ext_control *ctrl = ctrls->controls + i;919920err = hdpvr_get_ctrl(&dev->options, ctrl);921if (err) {922ctrls->error_idx = i;923break;924}925}926return err;927928}929930return -EINVAL;931}932933934static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)935{936int ret = -EINVAL;937938switch (ctrl->id) {939case V4L2_CID_MPEG_AUDIO_ENCODING:940if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||941(ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))942ret = 0;943break;944case V4L2_CID_MPEG_VIDEO_ENCODING:945if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)946ret = 0;947break;948/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */949/* if (ctrl->value == 0 || ctrl->value == 128) */950/* ret = 0; */951/* break; */952case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:953if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||954ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)955ret = 0;956break;957case V4L2_CID_MPEG_VIDEO_BITRATE:958{959uint bitrate = ctrl->value / 100000;960if (bitrate >= 10 && bitrate <= 135)961ret = 0;962break;963}964case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:965{966uint peak_bitrate = ctrl->value / 100000;967if (peak_bitrate >= 10 && peak_bitrate <= 202)968ret = 0;969break;970}971case V4L2_CID_MPEG_STREAM_TYPE:972if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)973ret = 0;974break;975default:976return -EINVAL;977}978return 0;979}980981static int vidioc_try_ext_ctrls(struct file *file, void *priv,982struct v4l2_ext_controls *ctrls)983{984struct hdpvr_fh *fh = file->private_data;985struct hdpvr_device *dev = fh->dev;986int i, err = 0;987988if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {989for (i = 0; i < ctrls->count; i++) {990struct v4l2_ext_control *ctrl = ctrls->controls + i;991992err = hdpvr_try_ctrl(ctrl,993dev->flags & HDPVR_FLAG_AC3_CAP);994if (err) {995ctrls->error_idx = i;996break;997}998}999return err;1000}10011002return -EINVAL;1003}100410051006static int hdpvr_set_ctrl(struct hdpvr_device *dev,1007struct v4l2_ext_control *ctrl)1008{1009struct hdpvr_options *opt = &dev->options;1010int ret = 0;10111012switch (ctrl->id) {1013case V4L2_CID_MPEG_AUDIO_ENCODING:1014if (dev->flags & HDPVR_FLAG_AC3_CAP) {1015opt->audio_codec = ctrl->value;1016ret = hdpvr_set_audio(dev, opt->audio_input,1017opt->audio_codec);1018}1019break;1020case V4L2_CID_MPEG_VIDEO_ENCODING:1021break;1022/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */1023/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */1024/* opt->gop_mode |= 0x2; */1025/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */1026/* opt->gop_mode); */1027/* } */1028/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */1029/* opt->gop_mode &= ~0x2; */1030/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */1031/* opt->gop_mode); */1032/* } */1033/* break; */1034case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:1035if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&1036opt->bitrate_mode != HDPVR_CONSTANT) {1037opt->bitrate_mode = HDPVR_CONSTANT;1038hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,1039opt->bitrate_mode);1040}1041if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&1042opt->bitrate_mode == HDPVR_CONSTANT) {1043opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;1044hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,1045opt->bitrate_mode);1046}1047break;1048case V4L2_CID_MPEG_VIDEO_BITRATE: {1049uint bitrate = ctrl->value / 100000;10501051opt->bitrate = bitrate;1052if (bitrate >= opt->peak_bitrate)1053opt->peak_bitrate = bitrate+1;10541055hdpvr_set_bitrate(dev);1056break;1057}1058case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {1059uint peak_bitrate = ctrl->value / 100000;10601061if (opt->bitrate_mode == HDPVR_CONSTANT)1062break;10631064if (opt->bitrate < peak_bitrate) {1065opt->peak_bitrate = peak_bitrate;1066hdpvr_set_bitrate(dev);1067} else1068ret = -EINVAL;1069break;1070}1071case V4L2_CID_MPEG_STREAM_TYPE:1072break;1073default:1074return -EINVAL;1075}1076return ret;1077}10781079static int vidioc_s_ext_ctrls(struct file *file, void *priv,1080struct v4l2_ext_controls *ctrls)1081{1082struct hdpvr_fh *fh = file->private_data;1083struct hdpvr_device *dev = fh->dev;1084int i, err = 0;10851086if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {1087for (i = 0; i < ctrls->count; i++) {1088struct v4l2_ext_control *ctrl = ctrls->controls + i;10891090err = hdpvr_try_ctrl(ctrl,1091dev->flags & HDPVR_FLAG_AC3_CAP);1092if (err) {1093ctrls->error_idx = i;1094break;1095}1096err = hdpvr_set_ctrl(dev, ctrl);1097if (err) {1098ctrls->error_idx = i;1099break;1100}1101}1102return err;11031104}11051106return -EINVAL;1107}11081109static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,1110struct v4l2_fmtdesc *f)1111{11121113if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)1114return -EINVAL;11151116f->flags = V4L2_FMT_FLAG_COMPRESSED;1117strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);1118f->pixelformat = V4L2_PIX_FMT_MPEG;11191120return 0;1121}11221123static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,1124struct v4l2_format *f)1125{1126struct hdpvr_fh *fh = file->private_data;1127struct hdpvr_device *dev = fh->dev;1128struct hdpvr_video_info *vid_info;11291130if (!dev)1131return -ENODEV;11321133vid_info = get_video_info(dev);1134if (!vid_info)1135return -EFAULT;11361137f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;1138f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;1139f->fmt.pix.width = vid_info->width;1140f->fmt.pix.height = vid_info->height;1141f->fmt.pix.sizeimage = dev->bulk_in_size;1142f->fmt.pix.colorspace = 0;1143f->fmt.pix.bytesperline = 0;1144f->fmt.pix.field = V4L2_FIELD_ANY;11451146kfree(vid_info);1147return 0;1148}11491150static int vidioc_encoder_cmd(struct file *filp, void *priv,1151struct v4l2_encoder_cmd *a)1152{1153struct hdpvr_fh *fh = filp->private_data;1154struct hdpvr_device *dev = fh->dev;1155int res;11561157mutex_lock(&dev->io_mutex);11581159memset(&a->raw, 0, sizeof(a->raw));1160switch (a->cmd) {1161case V4L2_ENC_CMD_START:1162a->flags = 0;1163res = hdpvr_start_streaming(dev);1164break;1165case V4L2_ENC_CMD_STOP:1166res = hdpvr_stop_streaming(dev);1167break;1168default:1169v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,1170"Unsupported encoder cmd %d\n", a->cmd);1171res = -EINVAL;1172}1173mutex_unlock(&dev->io_mutex);1174return res;1175}11761177static int vidioc_try_encoder_cmd(struct file *filp, void *priv,1178struct v4l2_encoder_cmd *a)1179{1180switch (a->cmd) {1181case V4L2_ENC_CMD_START:1182case V4L2_ENC_CMD_STOP:1183return 0;1184default:1185return -EINVAL;1186}1187}11881189static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {1190.vidioc_querycap = vidioc_querycap,1191.vidioc_s_std = vidioc_s_std,1192.vidioc_enum_input = vidioc_enum_input,1193.vidioc_g_input = vidioc_g_input,1194.vidioc_s_input = vidioc_s_input,1195.vidioc_enumaudio = vidioc_enumaudio,1196.vidioc_g_audio = vidioc_g_audio,1197.vidioc_s_audio = vidioc_s_audio,1198.vidioc_queryctrl = vidioc_queryctrl,1199.vidioc_g_ctrl = vidioc_g_ctrl,1200.vidioc_s_ctrl = vidioc_s_ctrl,1201.vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,1202.vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,1203.vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,1204.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,1205.vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,1206.vidioc_encoder_cmd = vidioc_encoder_cmd,1207.vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,1208};12091210static void hdpvr_device_release(struct video_device *vdev)1211{1212struct hdpvr_device *dev = video_get_drvdata(vdev);12131214hdpvr_delete(dev);1215mutex_lock(&dev->io_mutex);1216destroy_workqueue(dev->workqueue);1217mutex_unlock(&dev->io_mutex);12181219v4l2_device_unregister(&dev->v4l2_dev);12201221/* deregister I2C adapter */1222#if defined(CONFIG_I2C) || (CONFIG_I2C_MODULE)1223mutex_lock(&dev->i2c_mutex);1224i2c_del_adapter(&dev->i2c_adapter);1225mutex_unlock(&dev->i2c_mutex);1226#endif /* CONFIG_I2C */12271228kfree(dev->usbc_buf);1229kfree(dev);1230}12311232static const struct video_device hdpvr_video_template = {1233/* .type = VFL_TYPE_GRABBER, */1234/* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */1235.fops = &hdpvr_fops,1236.release = hdpvr_device_release,1237.ioctl_ops = &hdpvr_ioctl_ops,1238.tvnorms =1239V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |1240V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |1241V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |1242V4L2_STD_PAL_60,1243.current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M |1244V4L2_STD_PAL_60,1245};12461247int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,1248int devnum)1249{1250/* setup and register video device */1251dev->video_dev = video_device_alloc();1252if (!dev->video_dev) {1253v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");1254goto error;1255}12561257*(dev->video_dev) = hdpvr_video_template;1258strcpy(dev->video_dev->name, "Hauppauge HD PVR");1259dev->video_dev->parent = parent;1260video_set_drvdata(dev->video_dev, dev);12611262if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {1263v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");1264goto error;1265}12661267return 0;1268error:1269return -ENOMEM;1270}127112721273