1 /*
2    em28xx.h - driver for Empia EM2800/EM2820/2840 USB video capture devices
3 
4    Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 		      Ludovico Cavedon <cavedon@sssup.it>
6 		      Mauro Carvalho Chehab <mchehab@infradead.org>
7 
8    Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9 
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.
14 
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.
19 
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.
23  */
24 
25 #ifndef _EM28XX_H
26 #define _EM28XX_H
27 
28 #include <linux/workqueue.h>
29 #include <linux/i2c.h>
30 #include <linux/mutex.h>
31 #include <linux/videodev2.h>
32 
33 #include <media/videobuf-vmalloc.h>
34 #include <media/v4l2-device.h>
35 #include <media/ir-kbd-i2c.h>
36 #include <media/rc-core.h>
37 #if defined(CONFIG_VIDEO_EM28XX_DVB) || defined(CONFIG_VIDEO_EM28XX_DVB_MODULE)
38 #include <media/videobuf-dvb.h>
39 #endif
40 #include "tuner-xc2028.h"
41 #include "xc5000.h"
42 #include "em28xx-reg.h"
43 
44 /* Boards supported by driver */
45 #define EM2800_BOARD_UNKNOWN			0
46 #define EM2820_BOARD_UNKNOWN			1
47 #define EM2820_BOARD_TERRATEC_CINERGY_250	2
48 #define EM2820_BOARD_PINNACLE_USB_2		3
49 #define EM2820_BOARD_HAUPPAUGE_WINTV_USB_2      4
50 #define EM2820_BOARD_MSI_VOX_USB_2              5
51 #define EM2800_BOARD_TERRATEC_CINERGY_200       6
52 #define EM2800_BOARD_LEADTEK_WINFAST_USBII      7
53 #define EM2800_BOARD_KWORLD_USB2800             8
54 #define EM2820_BOARD_PINNACLE_DVC_90		9
55 #define EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900	10
56 #define EM2880_BOARD_TERRATEC_HYBRID_XS		11
57 #define EM2820_BOARD_KWORLD_PVRTV2800RF		12
58 #define EM2880_BOARD_TERRATEC_PRODIGY_XS	13
59 #define EM2820_BOARD_PROLINK_PLAYTV_USB2	14
60 #define EM2800_BOARD_VGEAR_POCKETTV             15
61 #define EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950	16
62 #define EM2880_BOARD_PINNACLE_PCTV_HD_PRO	17
63 #define EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2	18
64 #define EM2860_BOARD_SAA711X_REFERENCE_DESIGN	19
65 #define EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600   20
66 #define EM2800_BOARD_GRABBEEX_USB2800           21
67 #define EM2750_BOARD_UNKNOWN			  22
68 #define EM2750_BOARD_DLCW_130			  23
69 #define EM2820_BOARD_DLINK_USB_TV		  24
70 #define EM2820_BOARD_GADMEI_UTV310		  25
71 #define EM2820_BOARD_HERCULES_SMART_TV_USB2	  26
72 #define EM2820_BOARD_PINNACLE_USB_2_FM1216ME	  27
73 #define EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE 28
74 #define EM2860_BOARD_TVP5150_REFERENCE_DESIGN	  29
75 #define EM2820_BOARD_VIDEOLOGY_20K14XUSB	  30
76 #define EM2821_BOARD_USBGEAR_VD204		  31
77 #define EM2821_BOARD_SUPERCOMP_USB_2		  32
78 #define EM2860_BOARD_ELGATO_VIDEO_CAPTURE	  33
79 #define EM2860_BOARD_TERRATEC_HYBRID_XS		  34
80 #define EM2860_BOARD_TYPHOON_DVD_MAKER		  35
81 #define EM2860_BOARD_NETGMBH_CAM		  36
82 #define EM2860_BOARD_GADMEI_UTV330		  37
83 #define EM2861_BOARD_YAKUMO_MOVIE_MIXER		  38
84 #define EM2861_BOARD_KWORLD_PVRTV_300U		  39
85 #define EM2861_BOARD_PLEXTOR_PX_TV100U		  40
86 #define EM2870_BOARD_KWORLD_350U		  41
87 #define EM2870_BOARD_KWORLD_355U		  42
88 #define EM2870_BOARD_TERRATEC_XS		  43
89 #define EM2870_BOARD_TERRATEC_XS_MT2060		  44
90 #define EM2870_BOARD_PINNACLE_PCTV_DVB		  45
91 #define EM2870_BOARD_COMPRO_VIDEOMATE		  46
92 #define EM2880_BOARD_KWORLD_DVB_305U		  47
93 #define EM2880_BOARD_KWORLD_DVB_310U		  48
94 #define EM2880_BOARD_MSI_DIGIVOX_AD		  49
95 #define EM2880_BOARD_MSI_DIGIVOX_AD_II		  50
96 #define EM2880_BOARD_TERRATEC_HYBRID_XS_FR	  51
97 #define EM2881_BOARD_DNT_DA2_HYBRID		  52
98 #define EM2881_BOARD_PINNACLE_HYBRID_PRO	  53
99 #define EM2882_BOARD_KWORLD_VS_DVBT		  54
100 #define EM2882_BOARD_TERRATEC_HYBRID_XS		  55
101 #define EM2882_BOARD_PINNACLE_HYBRID_PRO_330E	  56
102 #define EM2883_BOARD_KWORLD_HYBRID_330U                  57
103 #define EM2820_BOARD_COMPRO_VIDEOMATE_FORYOU	  58
104 #define EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850	  60
105 #define EM2820_BOARD_PROLINK_PLAYTV_BOX4_USB2	  61
106 #define EM2820_BOARD_GADMEI_TVR200		  62
107 #define EM2860_BOARD_KAIOMY_TVNPC_U2              63
108 #define EM2860_BOARD_EASYCAP                      64
109 #define EM2820_BOARD_IODATA_GVMVP_SZ		  65
110 #define EM2880_BOARD_EMPIRE_DUAL_TV		  66
111 #define EM2860_BOARD_TERRATEC_GRABBY		  67
112 #define EM2860_BOARD_TERRATEC_AV350		  68
113 #define EM2882_BOARD_KWORLD_ATSC_315U		  69
114 #define EM2882_BOARD_EVGA_INDTUBE		  70
115 #define EM2820_BOARD_SILVERCREST_WEBCAM           71
116 #define EM2861_BOARD_GADMEI_UTV330PLUS           72
117 #define EM2870_BOARD_REDDO_DVB_C_USB_BOX          73
118 #define EM2800_BOARD_VC211A			  74
119 #define EM2882_BOARD_DIKOM_DK300		  75
120 #define EM2870_BOARD_KWORLD_A340		  76
121 #define EM2874_BOARD_LEADERSHIP_ISDBT		  77
122 #define EM28174_BOARD_PCTV_290E                   78
123 #define EM2884_BOARD_TERRATEC_H5		  79
124 #define EM28174_BOARD_PCTV_460E                   80
125 #define EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C	  81
126 #define EM2884_BOARD_CINERGY_HTC_STICK		  82
127 #define EM2860_BOARD_HT_VIDBOX_NW03 		  83
128 
129 /* Limits minimum and default number of buffers */
130 #define EM28XX_MIN_BUF 4
131 #define EM28XX_DEF_BUF 8
132 
133 /*Limits the max URB message size */
134 #define URB_MAX_CTRL_SIZE 80
135 
136 /* Params for validated field */
137 #define EM28XX_BOARD_NOT_VALIDATED 1
138 #define EM28XX_BOARD_VALIDATED	   0
139 
140 /* Params for em28xx_cmd() audio */
141 #define EM28XX_START_AUDIO      1
142 #define EM28XX_STOP_AUDIO       0
143 
144 /* maximum number of em28xx boards */
145 #define EM28XX_MAXBOARDS 4 /*FIXME: should be bigger */
146 
147 /* maximum number of frames that can be queued */
148 #define EM28XX_NUM_FRAMES 5
149 /* number of frames that get used for v4l2_read() */
150 #define EM28XX_NUM_READ_FRAMES 2
151 
152 /* number of buffers for isoc transfers */
153 #define EM28XX_NUM_BUFS 5
154 
155 /* number of packets for each buffer
156    windows requests only 64 packets .. so we better do the same
157    this is what I found out for all alternate numbers there!
158  */
159 #define EM28XX_NUM_PACKETS 64
160 
161 #define EM28XX_INTERLACED_DEFAULT 1
162 
163 /*
164 #define (use usbview if you want to get the other alternate number infos)
165 #define
166 #define alternate number 2
167 #define 			Endpoint Address: 82
168 			Direction: in
169 			Attribute: 1
170 			Type: Isoc
171 			Max Packet Size: 1448
172 			Interval: 125us
173 
174   alternate number 7
175 
176 			Endpoint Address: 82
177 			Direction: in
178 			Attribute: 1
179 			Type: Isoc
180 			Max Packet Size: 3072
181 			Interval: 125us
182 */
183 
184 /* time to wait when stopping the isoc transfer */
185 #define EM28XX_URB_TIMEOUT \
186 			msecs_to_jiffies(EM28XX_NUM_BUFS * EM28XX_NUM_PACKETS)
187 
188 /* time in msecs to wait for i2c writes to finish */
189 #define EM2800_I2C_WRITE_TIMEOUT 20
190 
191 enum em28xx_mode {
192 	EM28XX_SUSPEND,
193 	EM28XX_ANALOG_MODE,
194 	EM28XX_DIGITAL_MODE,
195 };
196 
197 
198 struct em28xx;
199 
200 struct em28xx_usb_isoc_ctl {
201 		/* max packet size of isoc transaction */
202 	int				max_pkt_size;
203 
204 		/* number of allocated urbs */
205 	int				num_bufs;
206 
207 		/* urb for isoc transfers */
208 	struct urb			**urb;
209 
210 		/* transfer buffers for isoc transfer */
211 	char				**transfer_buffer;
212 
213 		/* Last buffer command and region */
214 	u8				cmd;
215 	int				pos, size, pktsize;
216 
217 		/* Last field: ODD or EVEN? */
218 	int				field;
219 
220 		/* Stores incomplete commands */
221 	u32				tmp_buf;
222 	int				tmp_buf_len;
223 
224 		/* Stores already requested buffers */
225 	struct em28xx_buffer    	*vid_buf;
226 	struct em28xx_buffer    	*vbi_buf;
227 
228 		/* Stores the number of received fields */
229 	int				nfields;
230 
231 		/* isoc urb callback */
232 	int (*isoc_copy) (struct em28xx *dev, struct urb *urb);
233 
234 };
235 
236 /* Struct to enumberate video formats */
237 struct em28xx_fmt {
238 	char  *name;
239 	u32   fourcc;          /* v4l2 format id */
240 	int   depth;
241 	int   reg;
242 };
243 
244 /* buffer for one video frame */
245 struct em28xx_buffer {
246 	/* common v4l buffer stuff -- must be first */
247 	struct videobuf_buffer vb;
248 
249 	struct list_head frame;
250 	int top_field;
251 	int receiving;
252 };
253 
254 struct em28xx_dmaqueue {
255 	struct list_head       active;
256 	struct list_head       queued;
257 
258 	wait_queue_head_t          wq;
259 
260 	/* Counters to control buffer fill */
261 	int                        pos;
262 };
263 
264 /* io methods */
265 enum em28xx_io_method {
266 	IO_NONE,
267 	IO_READ,
268 	IO_MMAP,
269 };
270 
271 /* inputs */
272 
273 #define MAX_EM28XX_INPUT 4
274 enum enum28xx_itype {
275 	EM28XX_VMUX_COMPOSITE1 = 1,
276 	EM28XX_VMUX_COMPOSITE2,
277 	EM28XX_VMUX_COMPOSITE3,
278 	EM28XX_VMUX_COMPOSITE4,
279 	EM28XX_VMUX_SVIDEO,
280 	EM28XX_VMUX_TELEVISION,
281 	EM28XX_VMUX_CABLE,
282 	EM28XX_VMUX_DVB,
283 	EM28XX_VMUX_DEBUG,
284 	EM28XX_RADIO,
285 };
286 
287 enum em28xx_ac97_mode {
288 	EM28XX_NO_AC97 = 0,
289 	EM28XX_AC97_EM202,
290 	EM28XX_AC97_SIGMATEL,
291 	EM28XX_AC97_OTHER,
292 };
293 
294 struct em28xx_audio_mode {
295 	enum em28xx_ac97_mode ac97;
296 
297 	u16 ac97_feat;
298 	u32 ac97_vendor_id;
299 
300 	unsigned int has_audio:1;
301 
302 	unsigned int i2s_3rates:1;
303 	unsigned int i2s_5rates:1;
304 };
305 
306 /* em28xx has two audio inputs: tuner and line in.
307    However, on most devices, an auxiliary AC97 codec device is used.
308    The AC97 device may have several different inputs and outputs,
309    depending on their model. So, it is possible to use AC97 mixer to
310    address more than two different entries.
311  */
312 enum em28xx_amux {
313 	/* This is the only entry for em28xx tuner input */
314 	EM28XX_AMUX_VIDEO,	/* em28xx tuner, AC97 mixer Video */
315 
316 	EM28XX_AMUX_LINE_IN,	/* AC97 mixer Line In */
317 
318 	/* Some less-common mixer setups */
319 	EM28XX_AMUX_VIDEO2,	/* em28xx Line in, AC97 mixer Video */
320 	EM28XX_AMUX_PHONE,
321 	EM28XX_AMUX_MIC,
322 	EM28XX_AMUX_CD,
323 	EM28XX_AMUX_AUX,
324 	EM28XX_AMUX_PCM_OUT,
325 };
326 
327 enum em28xx_aout {
328 	/* AC97 outputs */
329 	EM28XX_AOUT_MASTER = 1 << 0,
330 	EM28XX_AOUT_LINE   = 1 << 1,
331 	EM28XX_AOUT_MONO   = 1 << 2,
332 	EM28XX_AOUT_LFE    = 1 << 3,
333 	EM28XX_AOUT_SURR   = 1 << 4,
334 
335 	/* PCM IN Mixer - used by AC97_RECORD_SELECT register */
336 	EM28XX_AOUT_PCM_IN = 1 << 7,
337 
338 	/* Bits 10-8 are used to indicate the PCM IN record select */
339 	EM28XX_AOUT_PCM_MIC_PCM = 0 << 8,
340 	EM28XX_AOUT_PCM_CD	= 1 << 8,
341 	EM28XX_AOUT_PCM_VIDEO	= 2 << 8,
342 	EM28XX_AOUT_PCM_AUX	= 3 << 8,
343 	EM28XX_AOUT_PCM_LINE	= 4 << 8,
344 	EM28XX_AOUT_PCM_STEREO	= 5 << 8,
345 	EM28XX_AOUT_PCM_MONO	= 6 << 8,
346 	EM28XX_AOUT_PCM_PHONE	= 7 << 8,
347 };
348 
ac97_return_record_select(int a_out)349 static inline int ac97_return_record_select(int a_out)
350 {
351 	return (a_out & 0x700) >> 8;
352 }
353 
354 struct em28xx_reg_seq {
355 	int reg;
356 	unsigned char val, mask;
357 	int sleep;
358 };
359 
360 struct em28xx_input {
361 	enum enum28xx_itype type;
362 	unsigned int vmux;
363 	enum em28xx_amux amux;
364 	enum em28xx_aout aout;
365 	struct em28xx_reg_seq *gpio;
366 };
367 
368 #define INPUT(nr) (&em28xx_boards[dev->model].input[nr])
369 
370 enum em28xx_decoder {
371 	EM28XX_NODECODER = 0,
372 	EM28XX_TVP5150,
373 	EM28XX_SAA711X,
374 };
375 
376 enum em28xx_sensor {
377 	EM28XX_NOSENSOR = 0,
378 	EM28XX_MT9V011,
379 	EM28XX_MT9M001,
380 	EM28XX_MT9M111,
381 };
382 
383 enum em28xx_adecoder {
384 	EM28XX_NOADECODER = 0,
385 	EM28XX_TVAUDIO,
386 };
387 
388 struct em28xx_board {
389 	char *name;
390 	int vchannels;
391 	int tuner_type;
392 	int tuner_addr;
393 
394 	/* i2c flags */
395 	unsigned int tda9887_conf;
396 
397 	/* GPIO sequences */
398 	struct em28xx_reg_seq *dvb_gpio;
399 	struct em28xx_reg_seq *suspend_gpio;
400 	struct em28xx_reg_seq *tuner_gpio;
401 	struct em28xx_reg_seq *mute_gpio;
402 
403 	unsigned int is_em2800:1;
404 	unsigned int has_msp34xx:1;
405 	unsigned int mts_firmware:1;
406 	unsigned int max_range_640_480:1;
407 	unsigned int has_dvb:1;
408 	unsigned int has_snapshot_button:1;
409 	unsigned int is_webcam:1;
410 	unsigned int valid:1;
411 	unsigned int has_ir_i2c:1;
412 
413 	unsigned char xclk, i2c_speed;
414 	unsigned char radio_addr;
415 	unsigned short tvaudio_addr;
416 
417 	enum em28xx_decoder decoder;
418 	enum em28xx_adecoder adecoder;
419 
420 	struct em28xx_input       input[MAX_EM28XX_INPUT];
421 	struct em28xx_input	  radio;
422 	char			  *ir_codes;
423 };
424 
425 struct em28xx_eeprom {
426 	u32 id;			/* 0x9567eb1a */
427 	u16 vendor_ID;
428 	u16 product_ID;
429 
430 	u16 chip_conf;
431 
432 	u16 board_conf;
433 
434 	u16 string1, string2, string3;
435 
436 	u8 string_idx_table;
437 };
438 
439 /* device states */
440 enum em28xx_dev_state {
441 	DEV_INITIALIZED = 0x01,
442 	DEV_DISCONNECTED = 0x02,
443 	DEV_MISCONFIGURED = 0x04,
444 };
445 
446 #define EM28XX_AUDIO_BUFS 5
447 #define EM28XX_NUM_AUDIO_PACKETS 64
448 #define EM28XX_AUDIO_MAX_PACKET_SIZE 196 /* static value */
449 #define EM28XX_CAPTURE_STREAM_EN 1
450 
451 /* em28xx extensions */
452 #define EM28XX_AUDIO   0x10
453 #define EM28XX_DVB     0x20
454 
455 /* em28xx resource types (used for res_get/res_lock etc */
456 #define EM28XX_RESOURCE_VIDEO 0x01
457 #define EM28XX_RESOURCE_VBI   0x02
458 
459 struct em28xx_audio {
460 	char name[50];
461 	char *transfer_buffer[EM28XX_AUDIO_BUFS];
462 	struct urb *urb[EM28XX_AUDIO_BUFS];
463 	struct usb_device *udev;
464 	unsigned int capture_transfer_done;
465 	struct snd_pcm_substream   *capture_pcm_substream;
466 
467 	unsigned int hwptr_done_capture;
468 	struct snd_card            *sndcard;
469 
470 	int users;
471 	spinlock_t slock;
472 };
473 
474 struct em28xx;
475 
476 struct em28xx_fh {
477 	struct em28xx *dev;
478 	int           radio;
479 	unsigned int  resources;
480 
481 	struct videobuf_queue        vb_vidq;
482 	struct videobuf_queue        vb_vbiq;
483 
484 	enum v4l2_buf_type           type;
485 };
486 
487 /* main device struct */
488 struct em28xx {
489 	/* generic device properties */
490 	char name[30];		/* name (including minor) of the device */
491 	int model;		/* index in the device_data struct */
492 	int devno;		/* marks the number of this device */
493 	enum em28xx_chip_id chip_id;
494 
495 	int audio_ifnum;
496 
497 	struct v4l2_device v4l2_dev;
498 	struct em28xx_board board;
499 
500 	/* Webcam specific fields */
501 	enum em28xx_sensor em28xx_sensor;
502 	int sensor_xres, sensor_yres;
503 	int sensor_xtal;
504 
505 	/* Allows progressive (e. g. non-interlaced) mode */
506 	int progressive;
507 
508 	/* Vinmode/Vinctl used at the driver */
509 	int vinmode, vinctl;
510 
511 	unsigned int has_audio_class:1;
512 	unsigned int has_alsa_audio:1;
513 	unsigned int is_audio_only:1;
514 
515 	/* Controls audio streaming */
516 	struct work_struct wq_trigger;              /* Trigger to start/stop audio for alsa module */
517 	 atomic_t       stream_started;      /* stream should be running if true */
518 
519 	struct em28xx_fmt *format;
520 
521 	struct em28xx_IR *ir;
522 
523 	/* Some older em28xx chips needs a waiting time after writing */
524 	unsigned int wait_after_write;
525 
526 	struct list_head	devlist;
527 
528 	u32 i2s_speed;		/* I2S speed for audio digital stream */
529 
530 	struct em28xx_audio_mode audio_mode;
531 
532 	int tuner_type;		/* type of the tuner */
533 	int tuner_addr;		/* tuner address */
534 	int tda9887_conf;
535 	/* i2c i/o */
536 	struct i2c_adapter i2c_adap;
537 	struct i2c_client i2c_client;
538 	/* video for linux */
539 	int users;		/* user count for exclusive use */
540 	struct video_device *vdev;	/* video for linux device struct */
541 	v4l2_std_id norm;	/* selected tv norm */
542 	int ctl_freq;		/* selected frequency */
543 	unsigned int ctl_input;	/* selected input */
544 	unsigned int ctl_ainput;/* selected audio input */
545 	unsigned int ctl_aoutput;/* selected audio output */
546 	int mute;
547 	int volume;
548 	/* frame properties */
549 	int width;		/* current frame width */
550 	int height;		/* current frame height */
551 	unsigned hscale;	/* horizontal scale factor (see datasheet) */
552 	unsigned vscale;	/* vertical scale factor (see datasheet) */
553 	int interlaced;		/* 1=interlace fileds, 0=just top fileds */
554 	unsigned int video_bytesread;	/* Number of bytes read */
555 
556 	unsigned long hash;	/* eeprom hash - for boards with generic ID */
557 	unsigned long i2c_hash;	/* i2c devicelist hash -
558 				   for boards with generic ID */
559 
560 	struct em28xx_audio adev;
561 
562 	/* states */
563 	enum em28xx_dev_state state;
564 	enum em28xx_io_method io;
565 
566 	/* vbi related state tracking */
567 	int capture_type;
568 	int vbi_read;
569 	unsigned char cur_field;
570 	unsigned int vbi_width;
571 	unsigned int vbi_height; /* lines per field */
572 
573 	struct work_struct         request_module_wk;
574 
575 	/* locks */
576 	struct mutex lock;
577 	struct mutex ctrl_urb_lock;	/* protects urb_buf */
578 	/* spinlock_t queue_lock; */
579 	struct list_head inqueue, outqueue;
580 	wait_queue_head_t open, wait_frame, wait_stream;
581 	struct video_device *vbi_dev;
582 	struct video_device *radio_dev;
583 
584 	/* resources in use */
585 	unsigned int resources;
586 
587 	unsigned char eedata[256];
588 
589 	/* Isoc control struct */
590 	struct em28xx_dmaqueue vidq;
591 	struct em28xx_dmaqueue vbiq;
592 	struct em28xx_usb_isoc_ctl isoc_ctl;
593 	spinlock_t slock;
594 
595 	/* usb transfer */
596 	struct usb_device *udev;	/* the usb device */
597 	int alt;		/* alternate */
598 	int max_pkt_size;	/* max packet size of isoc transaction */
599 	int num_alt;		/* Number of alternative settings */
600 	unsigned int *alt_max_pkt_size;	/* array of wMaxPacketSize */
601 	int dvb_alt;				/* alternate for DVB */
602 	unsigned int dvb_max_pkt_size;		/* wMaxPacketSize for DVB */
603 	struct urb *urb[EM28XX_NUM_BUFS];	/* urb for isoc transfers */
604 	char *transfer_buffer[EM28XX_NUM_BUFS];	/* transfer buffers for isoc
605 						   transfer */
606 	char urb_buf[URB_MAX_CTRL_SIZE];	/* urb control msg buffer */
607 
608 	/* helper funcs that call usb_control_msg */
609 	int (*em28xx_write_regs) (struct em28xx *dev, u16 reg,
610 					char *buf, int len);
611 	int (*em28xx_read_reg) (struct em28xx *dev, u16 reg);
612 	int (*em28xx_read_reg_req_len) (struct em28xx *dev, u8 req, u16 reg,
613 					char *buf, int len);
614 	int (*em28xx_write_regs_req) (struct em28xx *dev, u8 req, u16 reg,
615 				      char *buf, int len);
616 	int (*em28xx_read_reg_req) (struct em28xx *dev, u8 req, u16 reg);
617 
618 	enum em28xx_mode mode;
619 
620 	/* register numbers for GPO/GPIO registers */
621 	u16 reg_gpo_num, reg_gpio_num;
622 
623 	/* Caches GPO and GPIO registers */
624 	unsigned char	reg_gpo, reg_gpio;
625 
626 	/* Snapshot button */
627 	char snapshot_button_path[30];	/* path of the input dev */
628 	struct input_dev *sbutton_input_dev;
629 	struct delayed_work sbutton_query_work;
630 
631 	struct em28xx_dvb *dvb;
632 
633 	/* I2C keyboard data */
634 	struct IR_i2c_init_data init_data;
635 };
636 
637 struct em28xx_ops {
638 	struct list_head next;
639 	char *name;
640 	int id;
641 	int (*init)(struct em28xx *);
642 	int (*fini)(struct em28xx *);
643 };
644 
645 /* Provided by em28xx-i2c.c */
646 void em28xx_do_i2c_scan(struct em28xx *dev);
647 int  em28xx_i2c_register(struct em28xx *dev);
648 int  em28xx_i2c_unregister(struct em28xx *dev);
649 
650 /* Provided by em28xx-core.c */
651 
652 u32 em28xx_request_buffers(struct em28xx *dev, u32 count);
653 void em28xx_queue_unusedframes(struct em28xx *dev);
654 void em28xx_release_buffers(struct em28xx *dev);
655 
656 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
657 			    char *buf, int len);
658 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg);
659 int em28xx_read_reg(struct em28xx *dev, u16 reg);
660 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
661 			  int len);
662 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len);
663 int em28xx_write_reg(struct em28xx *dev, u16 reg, u8 val);
664 int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
665 				 u8 bitmask);
666 
667 int em28xx_read_ac97(struct em28xx *dev, u8 reg);
668 int em28xx_write_ac97(struct em28xx *dev, u8 reg, u16 val);
669 
670 int em28xx_audio_analog_set(struct em28xx *dev);
671 int em28xx_audio_setup(struct em28xx *dev);
672 
673 int em28xx_colorlevels_set_default(struct em28xx *dev);
674 int em28xx_capture_start(struct em28xx *dev, int start);
675 int em28xx_vbi_supported(struct em28xx *dev);
676 int em28xx_set_outfmt(struct em28xx *dev);
677 int em28xx_resolution_set(struct em28xx *dev);
678 int em28xx_set_alternate(struct em28xx *dev);
679 int em28xx_init_isoc(struct em28xx *dev, int max_packets,
680 		     int num_bufs, int max_pkt_size,
681 		     int (*isoc_copy) (struct em28xx *dev, struct urb *urb));
682 void em28xx_uninit_isoc(struct em28xx *dev);
683 int em28xx_isoc_dvb_max_packetsize(struct em28xx *dev);
684 int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode);
685 int em28xx_gpio_set(struct em28xx *dev, struct em28xx_reg_seq *gpio);
686 void em28xx_wake_i2c(struct em28xx *dev);
687 int em28xx_register_extension(struct em28xx_ops *dev);
688 void em28xx_unregister_extension(struct em28xx_ops *dev);
689 void em28xx_init_extension(struct em28xx *dev);
690 void em28xx_close_extension(struct em28xx *dev);
691 
692 /* Provided by em28xx-video.c */
693 int em28xx_register_analog_devices(struct em28xx *dev);
694 void em28xx_release_analog_resources(struct em28xx *dev);
695 
696 /* Provided by em28xx-cards.c */
697 extern int em2800_variant_detect(struct usb_device *udev, int model);
698 extern void em28xx_pre_card_setup(struct em28xx *dev);
699 extern void em28xx_card_setup(struct em28xx *dev);
700 extern struct em28xx_board em28xx_boards[];
701 extern struct usb_device_id em28xx_id_table[];
702 extern const unsigned int em28xx_bcount;
703 void em28xx_register_i2c_ir(struct em28xx *dev);
704 int em28xx_tuner_callback(void *ptr, int component, int command, int arg);
705 void em28xx_release_resources(struct em28xx *dev);
706 
707 /* Provided by em28xx-input.c */
708 
709 #ifdef CONFIG_VIDEO_EM28XX_RC
710 
711 int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw);
712 int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw);
713 int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
714 				     u32 *ir_raw);
715 int em28xx_get_key_winfast_usbii_deluxe(struct IR_i2c *ir, u32 *ir_key,
716 				     u32 *ir_raw);
717 void em28xx_register_snapshot_button(struct em28xx *dev);
718 void em28xx_deregister_snapshot_button(struct em28xx *dev);
719 
720 int em28xx_ir_init(struct em28xx *dev);
721 int em28xx_ir_fini(struct em28xx *dev);
722 
723 #else
724 
725 #define em28xx_get_key_terratec			NULL
726 #define em28xx_get_key_em_haup			NULL
727 #define em28xx_get_key_pinnacle_usb_grey	NULL
728 #define em28xx_get_key_winfast_usbii_deluxe	NULL
729 
em28xx_register_snapshot_button(struct em28xx * dev)730 static inline void em28xx_register_snapshot_button(struct em28xx *dev) {}
em28xx_deregister_snapshot_button(struct em28xx * dev)731 static inline void em28xx_deregister_snapshot_button(struct em28xx *dev) {}
em28xx_ir_init(struct em28xx * dev)732 static inline int em28xx_ir_init(struct em28xx *dev) { return 0; }
em28xx_ir_fini(struct em28xx * dev)733 static inline int em28xx_ir_fini(struct em28xx *dev) { return 0; }
734 
735 #endif
736 
737 /* Provided by em28xx-vbi.c */
738 extern struct videobuf_queue_ops em28xx_vbi_qops;
739 
740 /* printk macros */
741 
742 #define em28xx_err(fmt, arg...) do {\
743 	printk(KERN_ERR fmt , ##arg); } while (0)
744 
745 #define em28xx_errdev(fmt, arg...) do {\
746 	printk(KERN_ERR "%s: "fmt,\
747 			dev->name , ##arg); } while (0)
748 
749 #define em28xx_info(fmt, arg...) do {\
750 	printk(KERN_INFO "%s: "fmt,\
751 			dev->name , ##arg); } while (0)
752 #define em28xx_warn(fmt, arg...) do {\
753 	printk(KERN_WARNING "%s: "fmt,\
754 			dev->name , ##arg); } while (0)
755 
em28xx_compression_disable(struct em28xx * dev)756 static inline int em28xx_compression_disable(struct em28xx *dev)
757 {
758 	/* side effect of disabling scaler and mixer */
759 	return em28xx_write_reg(dev, EM28XX_R26_COMPR, 0x00);
760 }
761 
em28xx_contrast_get(struct em28xx * dev)762 static inline int em28xx_contrast_get(struct em28xx *dev)
763 {
764 	return em28xx_read_reg(dev, EM28XX_R20_YGAIN) & 0x1f;
765 }
766 
em28xx_brightness_get(struct em28xx * dev)767 static inline int em28xx_brightness_get(struct em28xx *dev)
768 {
769 	return em28xx_read_reg(dev, EM28XX_R21_YOFFSET);
770 }
771 
em28xx_saturation_get(struct em28xx * dev)772 static inline int em28xx_saturation_get(struct em28xx *dev)
773 {
774 	return em28xx_read_reg(dev, EM28XX_R22_UVGAIN) & 0x1f;
775 }
776 
em28xx_u_balance_get(struct em28xx * dev)777 static inline int em28xx_u_balance_get(struct em28xx *dev)
778 {
779 	return em28xx_read_reg(dev, EM28XX_R23_UOFFSET);
780 }
781 
em28xx_v_balance_get(struct em28xx * dev)782 static inline int em28xx_v_balance_get(struct em28xx *dev)
783 {
784 	return em28xx_read_reg(dev, EM28XX_R24_VOFFSET);
785 }
786 
em28xx_gamma_get(struct em28xx * dev)787 static inline int em28xx_gamma_get(struct em28xx *dev)
788 {
789 	return em28xx_read_reg(dev, EM28XX_R14_GAMMA) & 0x3f;
790 }
791 
em28xx_contrast_set(struct em28xx * dev,s32 val)792 static inline int em28xx_contrast_set(struct em28xx *dev, s32 val)
793 {
794 	u8 tmp = (u8) val;
795 	return em28xx_write_regs(dev, EM28XX_R20_YGAIN, &tmp, 1);
796 }
797 
em28xx_brightness_set(struct em28xx * dev,s32 val)798 static inline int em28xx_brightness_set(struct em28xx *dev, s32 val)
799 {
800 	u8 tmp = (u8) val;
801 	return em28xx_write_regs(dev, EM28XX_R21_YOFFSET, &tmp, 1);
802 }
803 
em28xx_saturation_set(struct em28xx * dev,s32 val)804 static inline int em28xx_saturation_set(struct em28xx *dev, s32 val)
805 {
806 	u8 tmp = (u8) val;
807 	return em28xx_write_regs(dev, EM28XX_R22_UVGAIN, &tmp, 1);
808 }
809 
em28xx_u_balance_set(struct em28xx * dev,s32 val)810 static inline int em28xx_u_balance_set(struct em28xx *dev, s32 val)
811 {
812 	u8 tmp = (u8) val;
813 	return em28xx_write_regs(dev, EM28XX_R23_UOFFSET, &tmp, 1);
814 }
815 
em28xx_v_balance_set(struct em28xx * dev,s32 val)816 static inline int em28xx_v_balance_set(struct em28xx *dev, s32 val)
817 {
818 	u8 tmp = (u8) val;
819 	return em28xx_write_regs(dev, EM28XX_R24_VOFFSET, &tmp, 1);
820 }
821 
em28xx_gamma_set(struct em28xx * dev,s32 val)822 static inline int em28xx_gamma_set(struct em28xx *dev, s32 val)
823 {
824 	u8 tmp = (u8) val;
825 	return em28xx_write_regs(dev, EM28XX_R14_GAMMA, &tmp, 1);
826 }
827 
828 /*FIXME: maxw should be dependent of alt mode */
norm_maxw(struct em28xx * dev)829 static inline unsigned int norm_maxw(struct em28xx *dev)
830 {
831 	if (dev->board.is_webcam)
832 		return dev->sensor_xres;
833 
834 	if (dev->board.max_range_640_480)
835 		return 640;
836 
837 	return 720;
838 }
839 
norm_maxh(struct em28xx * dev)840 static inline unsigned int norm_maxh(struct em28xx *dev)
841 {
842 	if (dev->board.is_webcam)
843 		return dev->sensor_yres;
844 
845 	if (dev->board.max_range_640_480)
846 		return 480;
847 
848 	return (dev->norm & V4L2_STD_625_50) ? 576 : 480;
849 }
850 #endif
851