2 em2820-cards.c - driver for Empia EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/videodev.h>
29 #include <linux/i2c.h>
31 /* maximum number of frames that can be queued */
32 #define EM2820_NUM_FRAMES 5
33 /* number of frames that get used for v4l2_read() */
34 #define EM2820_NUM_READ_FRAMES 2
36 /* number of buffers for isoc transfers */
37 #define EM2820_NUM_BUFS 5
39 /* number of packets for each buffer */
40 // windows requests only 40 packets .. so we better do the same
41 // this is what I found out for all alternate numbers there!
43 #define EM2820_NUM_PACKETS 40
45 /* packet size for each packet */
46 /* no longer needed: read from endpoint descriptor */
47 //#define EM2820_MAX_PACKET_SIZE 3072 //7
48 //#define EM2820_MAX_PACKET_SIZE 2892 //6
49 //#define EM2820_MAX_PACKET_SIZE 2580 //5
50 //#define EM2820_MAX_PACKET_SIZE 1448 //2
52 /* default alternate; 0 means choose the best */
53 #define EM2820_PINOUT 0
54 #define EM2820_MAX_ALT 7
56 #define EM2820_INTERLACED_DEFAULT 1
59 #define (use usbview if you want to get the other alternate number infos)
61 #define alternate number 2
62 #define Endpoint Address: 82
79 /* time to wait when stopping the isoc transfer */
80 #define EM2820_URB_TIMEOUT msecs_to_jiffies(EM2820_NUM_BUFS * EM2820_NUM_PACKETS)
82 /* the various frame states */
83 enum em2820_frame_state
{
92 enum em2820_stream_state
{
99 struct em2820_frame_t
{
101 struct v4l2_buffer buf
;
102 enum em2820_frame_state state
;
103 struct list_head frame
;
104 unsigned long vma_use_count
;
110 enum em2820_io_method
{
118 #define MAX_EM2820_INPUT 4
119 enum enum2820_itype
{
120 EM2820_VMUX_COMPOSITE1
= 1,
121 EM2820_VMUX_COMPOSITE2
,
122 EM2820_VMUX_COMPOSITE3
,
123 EM2820_VMUX_COMPOSITE4
,
125 EM2820_VMUX_TELEVISION
,
132 struct em2820_input
{
133 enum enum2820_itype type
;
138 #define INPUT(nr) (&em2820_boards[dev->model].input[nr])
140 enum em2820_decoder
{
146 struct em2820_board
{
154 unsigned int tda9887_conf
;
156 unsigned int has_tuner
:1;
157 unsigned int has_msp34xx
:1;
159 enum em2820_decoder decoder
;
161 struct em2820_input input
[MAX_EM2820_INPUT
];
164 struct em2820_eeprom
{
165 u32 id
; /* 0x9567eb1a */
173 u16 string1
, string2
, string3
;
179 enum em2820_dev_state
{
180 DEV_INITIALIZED
= 0x01,
181 DEV_DISCONNECTED
= 0x02,
182 DEV_MISCONFIGURED
= 0x04,
186 struct em2820_tvnorm
{
189 /* mode for saa7113h */
193 /* main device struct */
195 /* generic device properties */
196 char name
[30]; /* name (including minor) of the device */
197 int model
; /* index in the device_data struct */
198 int video_inputs
; /* number of video inputs */
199 unsigned int has_tuner
:1;
200 unsigned int has_msp34xx
:1;
201 unsigned int has_tda9887
:1;
203 enum em2820_decoder decoder
;
205 int tuner_type
; /* type of the tuner */
206 int tuner_addr
; /* tuner address */
209 struct i2c_adapter i2c_adap
;
210 struct i2c_client i2c_client
;
211 /* video for linux */
212 int users
; /* user count for exclusive use */
213 struct video_device
*vdev
; /* video for linux device struct */
214 struct video_picture vpic
; /* picture settings only used to init saa7113h */
215 struct em2820_tvnorm
*tvnorm
; /* selected tv norm */
216 int ctl_freq
; /* selected frequency */
217 unsigned int ctl_input
; /* selected input */
218 unsigned int ctl_ainput
; /* slected audio input */
221 /* frame properties */
222 struct em2820_frame_t frame
[EM2820_NUM_FRAMES
]; /* list of frames */
223 int num_frames
; /* number of frames currently in use */
224 unsigned int frame_count
; /* total number of transfered frames */
225 struct em2820_frame_t
*frame_current
; /* the frame that is being filled */
226 int width
; /* current frame width */
227 int height
; /* current frame height */
228 int frame_size
; /* current frame size */
229 int field_size
; /* current field size */
231 int hscale
; /* horizontal scale factor (see datasheet) */
232 int vscale
; /* vertical scale factor (see datasheet) */
233 int interlaced
; /* 1=interlace fileds, 0=just top fileds */
237 enum em2820_dev_state state
;
238 enum em2820_stream_state stream
;
239 enum em2820_io_method io
;
241 struct semaphore lock
, fileop_lock
;
242 spinlock_t queue_lock
;
243 struct list_head inqueue
, outqueue
;
244 wait_queue_head_t open
, wait_frame
, wait_stream
;
245 struct video_device
*vbi_dev
;
247 unsigned char eedata
[256];
250 struct usb_device
*udev
; /* the usb device */
251 int alt
; /* alternate */
252 int max_pkt_size
; /* max packet size of isoc transaction */
253 unsigned int alt_max_pkt_size
[EM2820_MAX_ALT
+ 1]; /* array of wMaxPacketSize */
254 struct urb
*urb
[EM2820_NUM_BUFS
]; /* urb for isoc transfers */
255 char *transfer_buffer
[EM2820_NUM_BUFS
]; /* transfer buffers for isoc transfer */
256 /* helper funcs that call usb_control_msg */
257 int (*em2820_write_regs
) (struct em2820
* dev
, u16 reg
, char *buf
,
259 int (*em2820_read_reg
) (struct em2820
* dev
, u16 reg
);
260 int (*em2820_read_reg_req_len
) (struct em2820
* dev
, u8 req
, u16 reg
,
262 int (*em2820_write_regs_req
) (struct em2820
* dev
, u8 req
, u16 reg
,
264 int (*em2820_read_reg_req
) (struct em2820
* dev
, u8 req
, u16 reg
);
267 /* Provided by em2820-i2c.c */
269 void em2820_i2c_call_clients(struct em2820
*dev
, unsigned int cmd
, void *arg
);
270 int em2820_i2c_register(struct em2820
*dev
);
271 int em2820_i2c_unregister(struct em2820
*dev
);
273 /* Provided by em2820-core.c */
275 void em2820_print_ioctl(char *name
, unsigned int cmd
);
277 u32
em2820_request_buffers(struct em2820
*dev
, u32 count
);
278 void em2820_queue_unusedframes(struct em2820
*dev
);
279 void em2820_release_buffers(struct em2820
*dev
);
281 int em2820_read_reg_req_len(struct em2820
*dev
, u8 req
, u16 reg
,
283 int em2820_read_reg_req(struct em2820
*dev
, u8 req
, u16 reg
);
284 int em2820_read_reg(struct em2820
*dev
, u16 reg
);
285 int em2820_write_regs_req(struct em2820
*dev
, u8 req
, u16 reg
, char *buf
,
287 int em2820_write_regs(struct em2820
*dev
, u16 reg
, char *buf
, int len
);
288 int em2820_write_reg_bits(struct em2820
*dev
, u16 reg
, u8 val
,
290 int em2820_write_ac97(struct em2820
*dev
, u8 reg
, u8
* val
);
291 int em2820_audio_analog_set(struct em2820
*dev
);
292 int em2820_colorlevels_set_default(struct em2820
*dev
);
293 int em2820_capture_start(struct em2820
*dev
, int start
);
294 int em2820_outfmt_set_yuv422(struct em2820
*dev
);
295 int em2820_accumulator_set(struct em2820
*dev
, u8 xmin
, u8 xmax
, u8 ymin
,
297 int em2820_capture_area_set(struct em2820
*dev
, u8 hstart
, u8 vstart
,
298 u16 width
, u16 height
);
299 int em2820_scaler_set(struct em2820
*dev
, u16 h
, u16 v
);
300 int em2820_resolution_set(struct em2820
*dev
);
301 void em2820_isocIrq(struct urb
*urb
, struct pt_regs
*regs
);
302 int em2820_init_isoc(struct em2820
*dev
);
303 void em2820_uninit_isoc(struct em2820
*dev
);
304 int em2820_set_alternate(struct em2820
*dev
);
306 /* Provided by em2820-cards.c */
307 extern void em2820_card_setup(struct em2820
*dev
);
308 extern struct em2820_board em2820_boards
[];
309 extern struct usb_device_id em2820_id_table
[];
311 /* em2820 registers */
312 #define USBSUSP_REG 0x0c /* */
314 #define AUDIOSRC_REG 0x0e
315 #define XCLK_REG 0x0f
317 #define VINMODE_REG 0x10
318 #define VINCTRL_REG 0x11
319 #define VINENABLE_REG 0x12 /* */
321 #define GAMMA_REG 0x14
322 #define RGAIN_REG 0x15
323 #define GGAIN_REG 0x16
324 #define BGAIN_REG 0x17
325 #define ROFFSET_REG 0x18
326 #define GOFFSET_REG 0x19
327 #define BOFFSET_REG 0x1a
329 #define OFLOW_REG 0x1b
330 #define HSTART_REG 0x1c
331 #define VSTART_REG 0x1d
332 #define CWIDTH_REG 0x1e
333 #define CHEIGHT_REG 0x1f
335 #define YGAIN_REG 0x20
336 #define YOFFSET_REG 0x21
337 #define UVGAIN_REG 0x22
338 #define UOFFSET_REG 0x23
339 #define VOFFSET_REG 0x24
340 #define SHARPNESS_REG 0x25
342 #define COMPR_REG 0x26
343 #define OUTFMT_REG 0x27
345 #define XMIN_REG 0x28
346 #define XMAX_REG 0x29
347 #define YMIN_REG 0x2a
348 #define YMAX_REG 0x2b
350 #define HSCALELOW_REG 0x30
351 #define HSCALEHIGH_REG 0x31
352 #define VSCALELOW_REG 0x32
353 #define VSCALEHIGH_REG 0x33
355 #define AC97LSB_REG 0x40
356 #define AC97MSB_REG 0x41
357 #define AC97ADDR_REG 0x42
358 #define AC97BUSY_REG 0x43
360 /* em202 registers */
361 #define MASTER_AC97 0x02
362 #define VIDEO_AC97 0x14
364 /* register settings */
365 #define EM2820_AUDIO_SRC_TUNER 0xc0
366 #define EM2820_AUDIO_SRC_LINE 0x80
370 #define em2820_err(fmt, arg...) do {\
371 printk(KERN_ERR fmt , ##arg); } while (0)
373 #define em2820_errdev(fmt, arg...) do {\
374 printk(KERN_ERR "%s: "fmt,\
375 dev->name , ##arg); } while (0)
377 #define em2820_info(fmt, arg...) do {\
378 printk(KERN_INFO "%s: "fmt,\
379 dev->name , ##arg); } while (0)
380 #define em2820_warn(fmt, arg...) do {\
381 printk(KERN_WARNING "%s: "fmt,\
382 dev->name , ##arg); } while (0)
384 inline static int em2820_audio_source(struct em2820
*dev
, int input
)
386 return em2820_write_reg_bits(dev
, AUDIOSRC_REG
, input
, 0xc0);
389 inline static int em2820_audio_usb_mute(struct em2820
*dev
, int mute
)
391 return em2820_write_reg_bits(dev
, XCLK_REG
, mute
? 0x00 : 0x80, 0x80);
394 inline static int em2820_audio_analog_setup(struct em2820
*dev
)
396 /* unmute video mixer with default volume level */
397 return em2820_write_ac97(dev
, VIDEO_AC97
, "\x08\x08");
400 inline static int em2820_compression_disable(struct em2820
*dev
)
402 /* side effect of disabling scaler and mixer */
403 return em2820_write_regs(dev
, COMPR_REG
, "\x00", 1);
406 inline static int em2820_contrast_get(struct em2820
*dev
)
408 return em2820_read_reg(dev
, YGAIN_REG
) & 0x1f;
411 inline static int em2820_brightness_get(struct em2820
*dev
)
413 return em2820_read_reg(dev
, YOFFSET_REG
);
416 inline static int em2820_saturation_get(struct em2820
*dev
)
418 return em2820_read_reg(dev
, UVGAIN_REG
) & 0x1f;
421 inline static int em2820_u_balance_get(struct em2820
*dev
)
423 return em2820_read_reg(dev
, UOFFSET_REG
);
426 inline static int em2820_v_balance_get(struct em2820
*dev
)
428 return em2820_read_reg(dev
, VOFFSET_REG
);
431 inline static int em2820_gamma_get(struct em2820
*dev
)
433 return em2820_read_reg(dev
, GAMMA_REG
) & 0x3f;
436 inline static int em2820_contrast_set(struct em2820
*dev
, s32 val
)
439 return em2820_write_regs(dev
, YGAIN_REG
, &tmp
, 1);
442 inline static int em2820_brightness_set(struct em2820
*dev
, s32 val
)
445 return em2820_write_regs(dev
, YOFFSET_REG
, &tmp
, 1);
448 inline static int em2820_saturation_set(struct em2820
*dev
, s32 val
)
451 return em2820_write_regs(dev
, UVGAIN_REG
, &tmp
, 1);
454 inline static int em2820_u_balance_set(struct em2820
*dev
, s32 val
)
457 return em2820_write_regs(dev
, UOFFSET_REG
, &tmp
, 1);
460 inline static int em2820_v_balance_set(struct em2820
*dev
, s32 val
)
463 return em2820_write_regs(dev
, VOFFSET_REG
, &tmp
, 1);
466 inline static int em2820_gamma_set(struct em2820
*dev
, s32 val
)
469 return em2820_write_regs(dev
, GAMMA_REG
, &tmp
, 1);
472 /*FIXME: maxw should be dependent of alt mode */
473 #define norm_maxw(dev) 720
474 inline static unsigned int norm_maxh(struct em2820
*dev
)
476 return (dev
->tvnorm
->id
& V4L2_STD_625_50
) ? 576 : 480;