Path: blob/master/drivers/media/video/hdpvr/hdpvr-core.c
17633 views
/*1* Hauppauge HD PVR USB driver2*3* Copyright (C) 2001-2004 Greg Kroah-Hartman ([email protected])4* Copyright (C) 2008 Janne Grunau ([email protected])5* Copyright (C) 2008 John Poet6*7* This program is free software; you can redistribute it and/or8* modify it under the terms of the GNU General Public License as9* published by the Free Software Foundation, version 2.10*11*/1213#include <linux/kernel.h>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 <asm/atomic.h>20#include <linux/usb.h>21#include <linux/mutex.h>22#include <linux/i2c.h>2324#include <linux/videodev2.h>25#include <media/v4l2-dev.h>26#include <media/v4l2-common.h>2728#include "hdpvr.h"2930static int video_nr[HDPVR_MAX] = {[0 ... (HDPVR_MAX - 1)] = UNSET};31module_param_array(video_nr, int, NULL, 0);32MODULE_PARM_DESC(video_nr, "video device number (-1=Auto)");3334/* holds the number of currently registered devices */35static atomic_t dev_nr = ATOMIC_INIT(-1);3637int hdpvr_debug;38module_param(hdpvr_debug, int, S_IRUGO|S_IWUSR);39MODULE_PARM_DESC(hdpvr_debug, "enable debugging output");4041static uint default_video_input = HDPVR_VIDEO_INPUTS;42module_param(default_video_input, uint, S_IRUGO|S_IWUSR);43MODULE_PARM_DESC(default_video_input, "default video input: 0=Component / "44"1=S-Video / 2=Composite");4546static uint default_audio_input = HDPVR_AUDIO_INPUTS;47module_param(default_audio_input, uint, S_IRUGO|S_IWUSR);48MODULE_PARM_DESC(default_audio_input, "default audio input: 0=RCA back / "49"1=RCA front / 2=S/PDIF");5051static int boost_audio;52module_param(boost_audio, bool, S_IRUGO|S_IWUSR);53MODULE_PARM_DESC(boost_audio, "boost the audio signal");545556/* table of devices that work with this driver */57static struct usb_device_id hdpvr_table[] = {58{ USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID) },59{ USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID1) },60{ USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID2) },61{ USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID3) },62{ USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID4) },63{ } /* Terminating entry */64};65MODULE_DEVICE_TABLE(usb, hdpvr_table);666768void hdpvr_delete(struct hdpvr_device *dev)69{70hdpvr_free_buffers(dev);7172if (dev->video_dev)73video_device_release(dev->video_dev);7475usb_put_dev(dev->udev);76}7778static void challenge(u8 *bytes)79{80u64 *i64P, tmp64;81uint i, idx;8283for (idx = 0; idx < 32; ++idx) {8485if (idx & 0x3)86bytes[(idx >> 3) + 3] = bytes[(idx >> 2) & 0x3];8788switch (idx & 0x3) {89case 0x3:90bytes[2] += bytes[3] * 4 + bytes[4] + bytes[5];91bytes[4] += bytes[(idx & 0x1) * 2] * 9 + 9;92break;93case 0x1:94bytes[0] *= 8;95bytes[0] += 7*idx + 4;96bytes[6] += bytes[3] * 3;97break;98case 0x0:99bytes[3 - (idx >> 3)] = bytes[idx >> 2];100bytes[5] += bytes[6] * 3;101for (i = 0; i < 3; i++)102bytes[3] *= bytes[3] + 1;103break;104case 0x2:105for (i = 0; i < 3; i++)106bytes[1] *= bytes[6] + 1;107for (i = 0; i < 3; i++) {108i64P = (u64 *)bytes;109tmp64 = le64_to_cpup(i64P);110tmp64 <<= bytes[7] & 0x0f;111*i64P += cpu_to_le64(tmp64);112}113break;114}115}116}117118/* try to init the device like the windows driver */119static int device_authorization(struct hdpvr_device *dev)120{121122int ret, retval = -ENOMEM;123char request_type = 0x38, rcv_request = 0x81;124char *response;125#ifdef HDPVR_DEBUG126size_t buf_size = 46;127char *print_buf = kzalloc(5*buf_size+1, GFP_KERNEL);128if (!print_buf) {129v4l2_err(&dev->v4l2_dev, "Out of memory\n");130return retval;131}132#endif133134mutex_lock(&dev->usbc_mutex);135ret = usb_control_msg(dev->udev,136usb_rcvctrlpipe(dev->udev, 0),137rcv_request, 0x80 | request_type,1380x0400, 0x0003,139dev->usbc_buf, 46,14010000);141if (ret != 46) {142v4l2_err(&dev->v4l2_dev,143"unexpected answer of status request, len %d\n", ret);144goto unlock;145}146#ifdef HDPVR_DEBUG147else {148hex_dump_to_buffer(dev->usbc_buf, 46, 16, 1, print_buf,1495*buf_size+1, 0);150v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,151"Status request returned, len %d: %s\n",152ret, print_buf);153}154#endif155156v4l2_info(&dev->v4l2_dev, "firmware version 0x%x dated %s\n",157dev->usbc_buf[1], &dev->usbc_buf[2]);158159switch (dev->usbc_buf[1]) {160case HDPVR_FIRMWARE_VERSION:161dev->flags &= ~HDPVR_FLAG_AC3_CAP;162break;163case HDPVR_FIRMWARE_VERSION_AC3:164case HDPVR_FIRMWARE_VERSION_0X12:165case HDPVR_FIRMWARE_VERSION_0X15:166dev->flags |= HDPVR_FLAG_AC3_CAP;167break;168default:169v4l2_info(&dev->v4l2_dev, "untested firmware, the driver might"170" not work.\n");171if (dev->usbc_buf[1] >= HDPVR_FIRMWARE_VERSION_AC3)172dev->flags |= HDPVR_FLAG_AC3_CAP;173else174dev->flags &= ~HDPVR_FLAG_AC3_CAP;175}176177response = dev->usbc_buf+38;178#ifdef HDPVR_DEBUG179hex_dump_to_buffer(response, 8, 16, 1, print_buf, 5*buf_size+1, 0);180v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, "challenge: %s\n",181print_buf);182#endif183challenge(response);184#ifdef HDPVR_DEBUG185hex_dump_to_buffer(response, 8, 16, 1, print_buf, 5*buf_size+1, 0);186v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, " response: %s\n",187print_buf);188#endif189190msleep(100);191ret = usb_control_msg(dev->udev,192usb_sndctrlpipe(dev->udev, 0),1930xd1, 0x00 | request_type,1940x0000, 0x0000,195response, 8,19610000);197v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,198"magic request returned %d\n", ret);199200retval = ret != 8;201unlock:202mutex_unlock(&dev->usbc_mutex);203return retval;204}205206static int hdpvr_device_init(struct hdpvr_device *dev)207{208int ret;209u8 *buf;210struct hdpvr_video_info *vidinf;211212if (device_authorization(dev))213return -EACCES;214215/* default options for init */216hdpvr_set_options(dev);217218/* set filter options */219mutex_lock(&dev->usbc_mutex);220buf = dev->usbc_buf;221buf[0] = 0x03; buf[1] = 0x03; buf[2] = 0x00; buf[3] = 0x00;222ret = usb_control_msg(dev->udev,223usb_sndctrlpipe(dev->udev, 0),2240x01, 0x38,225CTRL_LOW_PASS_FILTER_VALUE, CTRL_DEFAULT_INDEX,226buf, 4,2271000);228v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,229"control request returned %d\n", ret);230mutex_unlock(&dev->usbc_mutex);231232vidinf = get_video_info(dev);233if (!vidinf)234v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,235"no valid video signal or device init failed\n");236else237kfree(vidinf);238239/* enable fan and bling leds */240mutex_lock(&dev->usbc_mutex);241buf[0] = 0x1;242ret = usb_control_msg(dev->udev,243usb_sndctrlpipe(dev->udev, 0),2440xd4, 0x38, 0, 0, buf, 1,2451000);246v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,247"control request returned %d\n", ret);248249/* boost analog audio */250buf[0] = boost_audio;251ret = usb_control_msg(dev->udev,252usb_sndctrlpipe(dev->udev, 0),2530xd5, 0x38, 0, 0, buf, 1,2541000);255v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,256"control request returned %d\n", ret);257mutex_unlock(&dev->usbc_mutex);258259dev->status = STATUS_IDLE;260return 0;261}262263static const struct hdpvr_options hdpvr_default_options = {264.video_std = HDPVR_60HZ,265.video_input = HDPVR_COMPONENT,266.audio_input = HDPVR_RCA_BACK,267.bitrate = 65, /* 6 mbps */268.peak_bitrate = 90, /* 9 mbps */269.bitrate_mode = HDPVR_CONSTANT,270.gop_mode = HDPVR_SIMPLE_IDR_GOP,271.audio_codec = V4L2_MPEG_AUDIO_ENCODING_AAC,272.brightness = 0x86,273.contrast = 0x80,274.hue = 0x80,275.saturation = 0x80,276.sharpness = 0x80,277};278279static int hdpvr_probe(struct usb_interface *interface,280const struct usb_device_id *id)281{282struct hdpvr_device *dev;283struct usb_host_interface *iface_desc;284struct usb_endpoint_descriptor *endpoint;285struct i2c_client *client;286size_t buffer_size;287int i;288int retval = -ENOMEM;289290/* allocate memory for our device state and initialize it */291dev = kzalloc(sizeof(*dev), GFP_KERNEL);292if (!dev) {293err("Out of memory");294goto error;295}296297dev->workqueue = 0;298299/* register v4l2_device early so it can be used for printks */300if (v4l2_device_register(&interface->dev, &dev->v4l2_dev)) {301err("v4l2_device_register failed");302goto error;303}304305mutex_init(&dev->io_mutex);306mutex_init(&dev->i2c_mutex);307mutex_init(&dev->usbc_mutex);308dev->usbc_buf = kmalloc(64, GFP_KERNEL);309if (!dev->usbc_buf) {310v4l2_err(&dev->v4l2_dev, "Out of memory\n");311goto error;312}313314init_waitqueue_head(&dev->wait_buffer);315init_waitqueue_head(&dev->wait_data);316317dev->workqueue = create_singlethread_workqueue("hdpvr_buffer");318if (!dev->workqueue)319goto error;320321/* init video transfer queues */322INIT_LIST_HEAD(&dev->free_buff_list);323INIT_LIST_HEAD(&dev->rec_buff_list);324325dev->options = hdpvr_default_options;326327if (default_video_input < HDPVR_VIDEO_INPUTS)328dev->options.video_input = default_video_input;329330if (default_audio_input < HDPVR_AUDIO_INPUTS) {331dev->options.audio_input = default_audio_input;332if (default_audio_input == HDPVR_SPDIF)333dev->options.audio_codec =334V4L2_MPEG_AUDIO_ENCODING_AC3;335}336337dev->udev = usb_get_dev(interface_to_usbdev(interface));338339/* set up the endpoint information */340/* use only the first bulk-in and bulk-out endpoints */341iface_desc = interface->cur_altsetting;342for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {343endpoint = &iface_desc->endpoint[i].desc;344345if (!dev->bulk_in_endpointAddr &&346usb_endpoint_is_bulk_in(endpoint)) {347/* USB interface description is buggy, reported max348* packet size is 512 bytes, windows driver uses 8192 */349buffer_size = 8192;350dev->bulk_in_size = buffer_size;351dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;352}353354}355if (!dev->bulk_in_endpointAddr) {356v4l2_err(&dev->v4l2_dev, "Could not find bulk-in endpoint\n");357goto error;358}359360/* init the device */361if (hdpvr_device_init(dev)) {362v4l2_err(&dev->v4l2_dev, "device init failed\n");363goto error;364}365366mutex_lock(&dev->io_mutex);367if (hdpvr_alloc_buffers(dev, NUM_BUFFERS)) {368mutex_unlock(&dev->io_mutex);369v4l2_err(&dev->v4l2_dev,370"allocating transfer buffers failed\n");371goto error;372}373mutex_unlock(&dev->io_mutex);374375if (hdpvr_register_videodev(dev, &interface->dev,376video_nr[atomic_inc_return(&dev_nr)])) {377v4l2_err(&dev->v4l2_dev, "registering videodev failed\n");378goto error;379}380381#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)382retval = hdpvr_register_i2c_adapter(dev);383if (retval < 0) {384v4l2_err(&dev->v4l2_dev, "i2c adapter register failed\n");385goto error;386}387388client = hdpvr_register_ir_rx_i2c(dev);389if (!client) {390v4l2_err(&dev->v4l2_dev, "i2c IR RX device register failed\n");391goto reg_fail;392}393394client = hdpvr_register_ir_tx_i2c(dev);395if (!client) {396v4l2_err(&dev->v4l2_dev, "i2c IR TX device register failed\n");397goto reg_fail;398}399#endif400401/* let the user know what node this device is now attached to */402v4l2_info(&dev->v4l2_dev, "device now attached to %s\n",403video_device_node_name(dev->video_dev));404return 0;405406reg_fail:407#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)408i2c_del_adapter(&dev->i2c_adapter);409#endif410error:411if (dev) {412/* Destroy single thread */413if (dev->workqueue)414destroy_workqueue(dev->workqueue);415/* this frees allocated memory */416hdpvr_delete(dev);417}418return retval;419}420421static void hdpvr_disconnect(struct usb_interface *interface)422{423struct hdpvr_device *dev = to_hdpvr_dev(usb_get_intfdata(interface));424425v4l2_info(&dev->v4l2_dev, "device %s disconnected\n",426video_device_node_name(dev->video_dev));427/* prevent more I/O from starting and stop any ongoing */428mutex_lock(&dev->io_mutex);429dev->status = STATUS_DISCONNECTED;430wake_up_interruptible(&dev->wait_data);431wake_up_interruptible(&dev->wait_buffer);432mutex_unlock(&dev->io_mutex);433v4l2_device_disconnect(&dev->v4l2_dev);434msleep(100);435flush_workqueue(dev->workqueue);436mutex_lock(&dev->io_mutex);437hdpvr_cancel_queue(dev);438mutex_unlock(&dev->io_mutex);439#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)440i2c_del_adapter(&dev->i2c_adapter);441#endif442video_unregister_device(dev->video_dev);443atomic_dec(&dev_nr);444}445446447static struct usb_driver hdpvr_usb_driver = {448.name = "hdpvr",449.probe = hdpvr_probe,450.disconnect = hdpvr_disconnect,451.id_table = hdpvr_table,452};453454static int __init hdpvr_init(void)455{456int result;457458/* register this driver with the USB subsystem */459result = usb_register(&hdpvr_usb_driver);460if (result)461err("usb_register failed. Error number %d", result);462463return result;464}465466static void __exit hdpvr_exit(void)467{468/* deregister this driver with the USB subsystem */469usb_deregister(&hdpvr_usb_driver);470}471472module_init(hdpvr_init);473module_exit(hdpvr_exit);474475MODULE_LICENSE("GPL");476MODULE_AUTHOR("Janne Grunau");477MODULE_DESCRIPTION("Hauppauge HD PVR driver");478479480