Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1 | /* |
Janne Grunau | e86da6f | 2009-03-19 19:00:35 -0300 | [diff] [blame] | 2 | * Hauppauge HD PVR USB driver |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 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/usb.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/workqueue.h> |
| 16 | #include <linux/videodev2.h> |
| 17 | |
Janne Grunau | 06630aec9 | 2009-03-27 20:01:40 -0300 | [diff] [blame] | 18 | #include <media/v4l2-device.h> |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 19 | #include <media/v4l2-ctrls.h> |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 20 | #include <media/ir-kbd-i2c.h> |
Janne Grunau | 06630aec9 | 2009-03-27 20:01:40 -0300 | [diff] [blame] | 21 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 22 | #define HDPVR_MAX 8 |
Jarod Wilson | 559d162 | 2011-01-14 16:40:32 -0300 | [diff] [blame] | 23 | #define HDPVR_I2C_MAX_SIZE 128 |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 24 | |
| 25 | /* Define these values to match your devices */ |
| 26 | #define HD_PVR_VENDOR_ID 0x2040 |
| 27 | #define HD_PVR_PRODUCT_ID 0x4900 |
| 28 | #define HD_PVR_PRODUCT_ID1 0x4901 |
| 29 | #define HD_PVR_PRODUCT_ID2 0x4902 |
Janne Grunau | e902a68 | 2010-10-11 10:29:36 -0300 | [diff] [blame] | 30 | #define HD_PVR_PRODUCT_ID4 0x4903 |
Janne Grunau | c22425f | 2010-02-03 15:51:23 +0100 | [diff] [blame] | 31 | #define HD_PVR_PRODUCT_ID3 0x4982 |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 32 | |
| 33 | #define UNSET (-1U) |
| 34 | |
| 35 | #define NUM_BUFFERS 64 |
| 36 | |
Janne Grunau | 35b5366 | 2010-07-27 11:03:35 -0300 | [diff] [blame] | 37 | #define HDPVR_FIRMWARE_VERSION 0x08 |
| 38 | #define HDPVR_FIRMWARE_VERSION_AC3 0x0d |
| 39 | #define HDPVR_FIRMWARE_VERSION_0X12 0x12 |
| 40 | #define HDPVR_FIRMWARE_VERSION_0X15 0x15 |
Hans Verkuil | c7a2c84 | 2013-03-20 04:23:51 -0300 | [diff] [blame^] | 41 | #define HDPVR_FIRMWARE_VERSION_0X1E 0x1e |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 42 | |
| 43 | /* #define HDPVR_DEBUG */ |
| 44 | |
| 45 | extern int hdpvr_debug; |
| 46 | |
| 47 | #define MSG_INFO 1 |
| 48 | #define MSG_BUFFER 2 |
| 49 | |
| 50 | struct hdpvr_options { |
| 51 | u8 video_std; |
| 52 | u8 video_input; |
| 53 | u8 audio_input; |
| 54 | u8 bitrate; /* in 100kbps */ |
| 55 | u8 peak_bitrate; /* in 100kbps */ |
| 56 | u8 bitrate_mode; |
| 57 | u8 gop_mode; |
| 58 | enum v4l2_mpeg_audio_encoding audio_codec; |
| 59 | u8 brightness; |
| 60 | u8 contrast; |
| 61 | u8 hue; |
| 62 | u8 saturation; |
| 63 | u8 sharpness; |
| 64 | }; |
| 65 | |
| 66 | /* Structure to hold all of our device specific stuff */ |
| 67 | struct hdpvr_device { |
| 68 | /* the v4l device for this device */ |
| 69 | struct video_device *video_dev; |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 70 | /* the control handler for this device */ |
| 71 | struct v4l2_ctrl_handler hdl; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 72 | /* the usb device for this device */ |
| 73 | struct usb_device *udev; |
Janne Grunau | 06630aec9 | 2009-03-27 20:01:40 -0300 | [diff] [blame] | 74 | /* v4l2-device unused */ |
| 75 | struct v4l2_device v4l2_dev; |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 76 | struct { /* video mode/bitrate control cluster */ |
| 77 | struct v4l2_ctrl *video_mode; |
| 78 | struct v4l2_ctrl *video_bitrate; |
| 79 | struct v4l2_ctrl *video_bitrate_peak; |
| 80 | }; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 81 | |
| 82 | /* the max packet size of the bulk endpoint */ |
| 83 | size_t bulk_in_size; |
| 84 | /* the address of the bulk in endpoint */ |
| 85 | __u8 bulk_in_endpointAddr; |
| 86 | |
| 87 | /* holds the current device status */ |
| 88 | __u8 status; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 89 | |
| 90 | /* holds the cureent set options */ |
| 91 | struct hdpvr_options options; |
| 92 | |
| 93 | uint flags; |
| 94 | |
| 95 | /* synchronize I/O */ |
| 96 | struct mutex io_mutex; |
| 97 | /* available buffers */ |
| 98 | struct list_head free_buff_list; |
| 99 | /* in progress buffers */ |
| 100 | struct list_head rec_buff_list; |
| 101 | /* waitqueue for buffers */ |
| 102 | wait_queue_head_t wait_buffer; |
| 103 | /* waitqueue for data */ |
| 104 | wait_queue_head_t wait_data; |
| 105 | /**/ |
| 106 | struct workqueue_struct *workqueue; |
| 107 | /**/ |
| 108 | struct work_struct worker; |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 109 | /* current stream owner */ |
| 110 | struct v4l2_fh *owner; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 111 | |
| 112 | /* I2C adapter */ |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame] | 113 | struct i2c_adapter i2c_adapter; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 114 | /* I2C lock */ |
| 115 | struct mutex i2c_mutex; |
Jarod Wilson | 559d162 | 2011-01-14 16:40:32 -0300 | [diff] [blame] | 116 | /* I2C message buffer space */ |
| 117 | char i2c_buf[HDPVR_I2C_MAX_SIZE]; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 118 | |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 119 | /* For passing data to ir-kbd-i2c */ |
| 120 | struct IR_i2c_init_data ir_i2c_init_data; |
| 121 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 122 | /* usb control transfer buffer and lock */ |
| 123 | struct mutex usbc_mutex; |
| 124 | u8 *usbc_buf; |
Taylor Ralph | 8423c0c | 2011-10-20 23:33:23 -0300 | [diff] [blame] | 125 | u8 fw_ver; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 126 | }; |
| 127 | |
Hans Verkuil | f2b305c | 2010-05-02 08:01:04 -0300 | [diff] [blame] | 128 | static inline struct hdpvr_device *to_hdpvr_dev(struct v4l2_device *v4l2_dev) |
| 129 | { |
| 130 | return container_of(v4l2_dev, struct hdpvr_device, v4l2_dev); |
| 131 | } |
| 132 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 133 | |
| 134 | /* buffer one bulk urb of data */ |
| 135 | struct hdpvr_buffer { |
| 136 | struct list_head buff_list; |
| 137 | |
| 138 | struct urb *urb; |
| 139 | |
| 140 | struct hdpvr_device *dev; |
| 141 | |
| 142 | uint pos; |
| 143 | |
| 144 | __u8 status; |
| 145 | }; |
| 146 | |
| 147 | /* */ |
| 148 | |
| 149 | struct hdpvr_video_info { |
| 150 | u16 width; |
| 151 | u16 height; |
| 152 | u8 fps; |
| 153 | }; |
| 154 | |
| 155 | enum { |
| 156 | STATUS_UNINITIALIZED = 0, |
| 157 | STATUS_IDLE, |
| 158 | STATUS_STARTING, |
| 159 | STATUS_SHUTTING_DOWN, |
| 160 | STATUS_STREAMING, |
| 161 | STATUS_ERROR, |
| 162 | STATUS_DISCONNECTED, |
| 163 | }; |
| 164 | |
| 165 | enum { |
| 166 | HDPVR_FLAG_AC3_CAP = 1, |
| 167 | }; |
| 168 | |
| 169 | enum { |
| 170 | BUFSTAT_UNINITIALIZED = 0, |
| 171 | BUFSTAT_AVAILABLE, |
| 172 | BUFSTAT_INPROGRESS, |
| 173 | BUFSTAT_READY, |
| 174 | }; |
| 175 | |
| 176 | #define CTRL_START_STREAMING_VALUE 0x0700 |
| 177 | #define CTRL_STOP_STREAMING_VALUE 0x0800 |
| 178 | #define CTRL_BITRATE_VALUE 0x1000 |
| 179 | #define CTRL_BITRATE_MODE_VALUE 0x1200 |
| 180 | #define CTRL_GOP_MODE_VALUE 0x1300 |
| 181 | #define CTRL_VIDEO_INPUT_VALUE 0x1500 |
| 182 | #define CTRL_VIDEO_STD_TYPE 0x1700 |
| 183 | #define CTRL_AUDIO_INPUT_VALUE 0x2500 |
| 184 | #define CTRL_BRIGHTNESS 0x2900 |
| 185 | #define CTRL_CONTRAST 0x2a00 |
| 186 | #define CTRL_HUE 0x2b00 |
| 187 | #define CTRL_SATURATION 0x2c00 |
| 188 | #define CTRL_SHARPNESS 0x2d00 |
| 189 | #define CTRL_LOW_PASS_FILTER_VALUE 0x3100 |
| 190 | |
| 191 | #define CTRL_DEFAULT_INDEX 0x0003 |
| 192 | |
| 193 | |
| 194 | /* :0 s 38 01 1000 0003 0004 4 = 0a00ca00 |
| 195 | * BITRATE SETTING |
| 196 | * 1st and 2nd byte (little endian): average bitrate in 100 000 bit/s |
| 197 | * min: 1 mbit/s, max: 13.5 mbit/s |
| 198 | * 3rd and 4th byte (little endian): peak bitrate in 100 000 bit/s |
| 199 | * min: average + 100kbit/s, |
| 200 | * max: 20.2 mbit/s |
| 201 | */ |
| 202 | |
| 203 | /* :0 s 38 01 1200 0003 0001 1 = 02 |
| 204 | * BIT RATE MODE |
| 205 | * constant = 1, variable (peak) = 2, variable (average) = 3 |
| 206 | */ |
| 207 | |
| 208 | /* :0 s 38 01 1300 0003 0001 1 = 03 |
| 209 | * GOP MODE (2 bit) |
| 210 | * low bit 0/1: advanced/simple GOP |
| 211 | * high bit 0/1: IDR(4/32/128) / no IDR (4/32/0) |
| 212 | */ |
| 213 | |
| 214 | /* :0 s 38 01 1700 0003 0001 1 = 00 |
| 215 | * VIDEO STANDARD or FREQUNCY 0 = 60hz, 1 = 50hz |
| 216 | */ |
| 217 | |
| 218 | /* :0 s 38 01 3100 0003 0004 4 = 03030000 |
| 219 | * FILTER CONTROL |
| 220 | * 1st byte luma low pass filter strength, |
| 221 | * 2nd byte chroma low pass filter strength, |
| 222 | * 3rd byte MF enable chroma, min=0, max=1 |
| 223 | * 4th byte n |
| 224 | */ |
| 225 | |
| 226 | |
| 227 | /* :0 s 38 b9 0001 0000 0000 0 */ |
| 228 | |
| 229 | |
| 230 | |
| 231 | /* :0 s 38 d3 0000 0000 0001 1 = 00 */ |
| 232 | /* ret = usb_control_msg(dev->udev, */ |
| 233 | /* usb_sndctrlpipe(dev->udev, 0), */ |
| 234 | /* 0xd3, 0x38, */ |
| 235 | /* 0, 0, */ |
| 236 | /* "\0", 1, */ |
| 237 | /* 1000); */ |
| 238 | |
| 239 | /* info("control request returned %d", ret); */ |
| 240 | /* msleep(5000); */ |
| 241 | |
| 242 | |
| 243 | /* :0 s b8 81 1400 0003 0005 5 < |
| 244 | * :0 0 5 = d0024002 19 |
| 245 | * QUERY FRAME SIZE AND RATE |
| 246 | * 1st and 2nd byte (little endian): horizontal resolution |
| 247 | * 3rd and 4th byte (little endian): vertical resolution |
| 248 | * 5th byte: frame rate |
| 249 | */ |
| 250 | |
| 251 | /* :0 s b8 81 1800 0003 0003 3 < |
| 252 | * :0 0 3 = 030104 |
| 253 | * QUERY SIGNAL AND DETECTED LINES, maybe INPUT |
| 254 | */ |
| 255 | |
| 256 | enum hdpvr_video_std { |
| 257 | HDPVR_60HZ = 0, |
| 258 | HDPVR_50HZ, |
| 259 | }; |
| 260 | |
| 261 | enum hdpvr_video_input { |
| 262 | HDPVR_COMPONENT = 0, |
| 263 | HDPVR_SVIDEO, |
| 264 | HDPVR_COMPOSITE, |
| 265 | HDPVR_VIDEO_INPUTS |
| 266 | }; |
| 267 | |
| 268 | enum hdpvr_audio_inputs { |
| 269 | HDPVR_RCA_BACK = 0, |
| 270 | HDPVR_RCA_FRONT, |
| 271 | HDPVR_SPDIF, |
| 272 | HDPVR_AUDIO_INPUTS |
| 273 | }; |
| 274 | |
| 275 | enum hdpvr_bitrate_mode { |
| 276 | HDPVR_CONSTANT = 1, |
| 277 | HDPVR_VARIABLE_PEAK, |
| 278 | HDPVR_VARIABLE_AVERAGE, |
| 279 | }; |
| 280 | |
| 281 | enum hdpvr_gop_mode { |
| 282 | HDPVR_ADVANCED_IDR_GOP = 0, |
| 283 | HDPVR_SIMPLE_IDR_GOP, |
| 284 | HDPVR_ADVANCED_NOIDR_GOP, |
| 285 | HDPVR_SIMPLE_NOIDR_GOP, |
| 286 | }; |
| 287 | |
| 288 | void hdpvr_delete(struct hdpvr_device *dev); |
| 289 | |
| 290 | /*========================================================================*/ |
| 291 | /* hardware control functions */ |
| 292 | int hdpvr_set_options(struct hdpvr_device *dev); |
| 293 | |
| 294 | int hdpvr_set_bitrate(struct hdpvr_device *dev); |
| 295 | |
| 296 | int hdpvr_set_audio(struct hdpvr_device *dev, u8 input, |
| 297 | enum v4l2_mpeg_audio_encoding codec); |
| 298 | |
| 299 | int hdpvr_config_call(struct hdpvr_device *dev, uint value, |
| 300 | unsigned char valbuf); |
| 301 | |
| 302 | struct hdpvr_video_info *get_video_info(struct hdpvr_device *dev); |
| 303 | |
| 304 | /* :0 s b8 81 1800 0003 0003 3 < */ |
| 305 | /* :0 0 3 = 0301ff */ |
| 306 | int get_input_lines_info(struct hdpvr_device *dev); |
| 307 | |
| 308 | |
| 309 | /*========================================================================*/ |
| 310 | /* v4l2 registration */ |
Janne Grunau | a50ab29 | 2009-03-26 14:40:55 -0300 | [diff] [blame] | 311 | int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent, |
| 312 | int devnumber); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 313 | |
| 314 | int hdpvr_cancel_queue(struct hdpvr_device *dev); |
| 315 | |
| 316 | /*========================================================================*/ |
| 317 | /* i2c adapter registration */ |
| 318 | int hdpvr_register_i2c_adapter(struct hdpvr_device *dev); |
| 319 | |
Jarod Wilson | 7f2a06d | 2011-01-19 18:10:14 -0300 | [diff] [blame] | 320 | struct i2c_client *hdpvr_register_ir_rx_i2c(struct hdpvr_device *dev); |
| 321 | struct i2c_client *hdpvr_register_ir_tx_i2c(struct hdpvr_device *dev); |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 322 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 323 | /*========================================================================*/ |
| 324 | /* buffer management */ |
| 325 | int hdpvr_free_buffers(struct hdpvr_device *dev); |
| 326 | int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count); |