blob: 050c6b9fec53f72ad6f9ff39691732148fe06b32 [file] [log] [blame]
Janne Grunau9aba42e2009-03-18 18:10:04 -03001/*
Janne Grunaue86da6f2009-03-19 19:00:35 -03002 * Hauppauge HD PVR USB driver
Janne Grunau9aba42e2009-03-18 18:10:04 -03003 *
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 Grunau06630ae2009-03-27 20:01:40 -030018#include <media/v4l2-device.h>
Hans Verkuil99c77aa2013-03-19 09:30:50 -030019#include <media/v4l2-ctrls.h>
Andy Wallsea6c0602010-12-28 22:46:13 -030020#include <media/ir-kbd-i2c.h>
Janne Grunau06630ae2009-03-27 20:01:40 -030021
Janne Grunau9aba42e2009-03-18 18:10:04 -030022#define HDPVR_MAX 8
Jarod Wilson559d1622011-01-14 16:40:32 -030023#define HDPVR_I2C_MAX_SIZE 128
Janne Grunau9aba42e2009-03-18 18:10:04 -030024
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 Grunaue902a682010-10-11 10:29:36 -030030#define HD_PVR_PRODUCT_ID4 0x4903
Janne Grunauc22425f2010-02-03 15:51:23 +010031#define HD_PVR_PRODUCT_ID3 0x4982
Janne Grunau9aba42e2009-03-18 18:10:04 -030032
33#define UNSET (-1U)
34
35#define NUM_BUFFERS 64
36
Janne Grunau35b53662010-07-27 11:03:35 -030037#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 Verkuilc7a2c842013-03-20 04:23:51 -030041#define HDPVR_FIRMWARE_VERSION_0X1E 0x1e
Janne Grunau9aba42e2009-03-18 18:10:04 -030042
43/* #define HDPVR_DEBUG */
44
45extern int hdpvr_debug;
46
47#define MSG_INFO 1
48#define MSG_BUFFER 2
49
50struct 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 */
67struct hdpvr_device {
68 /* the v4l device for this device */
69 struct video_device *video_dev;
Hans Verkuil99c77aa2013-03-19 09:30:50 -030070 /* the control handler for this device */
71 struct v4l2_ctrl_handler hdl;
Janne Grunau9aba42e2009-03-18 18:10:04 -030072 /* the usb device for this device */
73 struct usb_device *udev;
Janne Grunau06630ae2009-03-27 20:01:40 -030074 /* v4l2-device unused */
75 struct v4l2_device v4l2_dev;
Hans Verkuil99c77aa2013-03-19 09:30:50 -030076 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 };
Hans Verkuil8f69da92013-03-19 09:53:29 -030081 /* v4l2 format */
82 uint width, height;
Janne Grunau9aba42e2009-03-18 18:10:04 -030083
84 /* the max packet size of the bulk endpoint */
85 size_t bulk_in_size;
86 /* the address of the bulk in endpoint */
87 __u8 bulk_in_endpointAddr;
88
89 /* holds the current device status */
90 __u8 status;
Janne Grunau9aba42e2009-03-18 18:10:04 -030091
Hans Verkuil8f69da92013-03-19 09:53:29 -030092 /* holds the current set options */
Janne Grunau9aba42e2009-03-18 18:10:04 -030093 struct hdpvr_options options;
Hans Verkuil8f69da92013-03-19 09:53:29 -030094 v4l2_std_id cur_std;
Janne Grunau9aba42e2009-03-18 18:10:04 -030095
96 uint flags;
97
98 /* synchronize I/O */
99 struct mutex io_mutex;
100 /* available buffers */
101 struct list_head free_buff_list;
102 /* in progress buffers */
103 struct list_head rec_buff_list;
104 /* waitqueue for buffers */
105 wait_queue_head_t wait_buffer;
106 /* waitqueue for data */
107 wait_queue_head_t wait_data;
108 /**/
109 struct workqueue_struct *workqueue;
110 /**/
111 struct work_struct worker;
Hans Verkuilede197a2013-04-06 06:00:17 -0300112 /* current stream owner */
113 struct v4l2_fh *owner;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300114
115 /* I2C adapter */
Jarod Wilson324b04b2011-01-14 16:25:21 -0300116 struct i2c_adapter i2c_adapter;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300117 /* I2C lock */
118 struct mutex i2c_mutex;
Jarod Wilson559d1622011-01-14 16:40:32 -0300119 /* I2C message buffer space */
120 char i2c_buf[HDPVR_I2C_MAX_SIZE];
Janne Grunau9aba42e2009-03-18 18:10:04 -0300121
Andy Wallsea6c0602010-12-28 22:46:13 -0300122 /* For passing data to ir-kbd-i2c */
123 struct IR_i2c_init_data ir_i2c_init_data;
124
Janne Grunau9aba42e2009-03-18 18:10:04 -0300125 /* usb control transfer buffer and lock */
126 struct mutex usbc_mutex;
127 u8 *usbc_buf;
Taylor Ralph8423c0c2011-10-20 23:33:23 -0300128 u8 fw_ver;
Janne Grunau9aba42e2009-03-18 18:10:04 -0300129};
130
Hans Verkuilf2b305c2010-05-02 08:01:04 -0300131static inline struct hdpvr_device *to_hdpvr_dev(struct v4l2_device *v4l2_dev)
132{
133 return container_of(v4l2_dev, struct hdpvr_device, v4l2_dev);
134}
135
Janne Grunau9aba42e2009-03-18 18:10:04 -0300136
137/* buffer one bulk urb of data */
138struct hdpvr_buffer {
139 struct list_head buff_list;
140
141 struct urb *urb;
142
143 struct hdpvr_device *dev;
144
145 uint pos;
146
147 __u8 status;
148};
149
150/* */
151
152struct hdpvr_video_info {
153 u16 width;
154 u16 height;
155 u8 fps;
156};
157
158enum {
159 STATUS_UNINITIALIZED = 0,
160 STATUS_IDLE,
161 STATUS_STARTING,
162 STATUS_SHUTTING_DOWN,
163 STATUS_STREAMING,
164 STATUS_ERROR,
165 STATUS_DISCONNECTED,
166};
167
168enum {
169 HDPVR_FLAG_AC3_CAP = 1,
170};
171
172enum {
173 BUFSTAT_UNINITIALIZED = 0,
174 BUFSTAT_AVAILABLE,
175 BUFSTAT_INPROGRESS,
176 BUFSTAT_READY,
177};
178
179#define CTRL_START_STREAMING_VALUE 0x0700
180#define CTRL_STOP_STREAMING_VALUE 0x0800
181#define CTRL_BITRATE_VALUE 0x1000
182#define CTRL_BITRATE_MODE_VALUE 0x1200
183#define CTRL_GOP_MODE_VALUE 0x1300
184#define CTRL_VIDEO_INPUT_VALUE 0x1500
185#define CTRL_VIDEO_STD_TYPE 0x1700
186#define CTRL_AUDIO_INPUT_VALUE 0x2500
187#define CTRL_BRIGHTNESS 0x2900
188#define CTRL_CONTRAST 0x2a00
189#define CTRL_HUE 0x2b00
190#define CTRL_SATURATION 0x2c00
191#define CTRL_SHARPNESS 0x2d00
192#define CTRL_LOW_PASS_FILTER_VALUE 0x3100
193
194#define CTRL_DEFAULT_INDEX 0x0003
195
196
197 /* :0 s 38 01 1000 0003 0004 4 = 0a00ca00
198 * BITRATE SETTING
199 * 1st and 2nd byte (little endian): average bitrate in 100 000 bit/s
200 * min: 1 mbit/s, max: 13.5 mbit/s
201 * 3rd and 4th byte (little endian): peak bitrate in 100 000 bit/s
202 * min: average + 100kbit/s,
203 * max: 20.2 mbit/s
204 */
205
206 /* :0 s 38 01 1200 0003 0001 1 = 02
207 * BIT RATE MODE
208 * constant = 1, variable (peak) = 2, variable (average) = 3
209 */
210
211 /* :0 s 38 01 1300 0003 0001 1 = 03
212 * GOP MODE (2 bit)
213 * low bit 0/1: advanced/simple GOP
214 * high bit 0/1: IDR(4/32/128) / no IDR (4/32/0)
215 */
216
217 /* :0 s 38 01 1700 0003 0001 1 = 00
218 * VIDEO STANDARD or FREQUNCY 0 = 60hz, 1 = 50hz
219 */
220
221 /* :0 s 38 01 3100 0003 0004 4 = 03030000
222 * FILTER CONTROL
223 * 1st byte luma low pass filter strength,
224 * 2nd byte chroma low pass filter strength,
225 * 3rd byte MF enable chroma, min=0, max=1
226 * 4th byte n
227 */
228
229
230 /* :0 s 38 b9 0001 0000 0000 0 */
231
232
233
234/* :0 s 38 d3 0000 0000 0001 1 = 00 */
235/* ret = usb_control_msg(dev->udev, */
236/* usb_sndctrlpipe(dev->udev, 0), */
237/* 0xd3, 0x38, */
238/* 0, 0, */
239/* "\0", 1, */
240/* 1000); */
241
242/* info("control request returned %d", ret); */
243/* msleep(5000); */
244
245
246 /* :0 s b8 81 1400 0003 0005 5 <
247 * :0 0 5 = d0024002 19
248 * QUERY FRAME SIZE AND RATE
249 * 1st and 2nd byte (little endian): horizontal resolution
250 * 3rd and 4th byte (little endian): vertical resolution
251 * 5th byte: frame rate
252 */
253
254 /* :0 s b8 81 1800 0003 0003 3 <
255 * :0 0 3 = 030104
256 * QUERY SIGNAL AND DETECTED LINES, maybe INPUT
257 */
258
259enum hdpvr_video_std {
260 HDPVR_60HZ = 0,
261 HDPVR_50HZ,
262};
263
264enum hdpvr_video_input {
265 HDPVR_COMPONENT = 0,
266 HDPVR_SVIDEO,
267 HDPVR_COMPOSITE,
268 HDPVR_VIDEO_INPUTS
269};
270
271enum hdpvr_audio_inputs {
272 HDPVR_RCA_BACK = 0,
273 HDPVR_RCA_FRONT,
274 HDPVR_SPDIF,
275 HDPVR_AUDIO_INPUTS
276};
277
278enum hdpvr_bitrate_mode {
279 HDPVR_CONSTANT = 1,
280 HDPVR_VARIABLE_PEAK,
281 HDPVR_VARIABLE_AVERAGE,
282};
283
284enum hdpvr_gop_mode {
285 HDPVR_ADVANCED_IDR_GOP = 0,
286 HDPVR_SIMPLE_IDR_GOP,
287 HDPVR_ADVANCED_NOIDR_GOP,
288 HDPVR_SIMPLE_NOIDR_GOP,
289};
290
291void hdpvr_delete(struct hdpvr_device *dev);
292
293/*========================================================================*/
294/* hardware control functions */
295int hdpvr_set_options(struct hdpvr_device *dev);
296
297int hdpvr_set_bitrate(struct hdpvr_device *dev);
298
299int hdpvr_set_audio(struct hdpvr_device *dev, u8 input,
300 enum v4l2_mpeg_audio_encoding codec);
301
302int hdpvr_config_call(struct hdpvr_device *dev, uint value,
303 unsigned char valbuf);
304
305struct hdpvr_video_info *get_video_info(struct hdpvr_device *dev);
306
307/* :0 s b8 81 1800 0003 0003 3 < */
308/* :0 0 3 = 0301ff */
309int get_input_lines_info(struct hdpvr_device *dev);
310
311
312/*========================================================================*/
313/* v4l2 registration */
Janne Grunaua50ab292009-03-26 14:40:55 -0300314int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
315 int devnumber);
Janne Grunau9aba42e2009-03-18 18:10:04 -0300316
317int hdpvr_cancel_queue(struct hdpvr_device *dev);
318
319/*========================================================================*/
320/* i2c adapter registration */
321int hdpvr_register_i2c_adapter(struct hdpvr_device *dev);
322
Jarod Wilson7f2a06d2011-01-19 18:10:14 -0300323struct i2c_client *hdpvr_register_ir_rx_i2c(struct hdpvr_device *dev);
324struct i2c_client *hdpvr_register_ir_tx_i2c(struct hdpvr_device *dev);
Andy Wallsea6c0602010-12-28 22:46:13 -0300325
Janne Grunau9aba42e2009-03-18 18:10:04 -0300326/*========================================================================*/
327/* buffer management */
328int hdpvr_free_buffers(struct hdpvr_device *dev);
329int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count);