1 /*
2    cx231xx.h - driver for Conexant Cx23100/101/102 USB video capture devices
3 
4    Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5 	Based on em28xx driver
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #ifndef _CX231XX_H
23 #define _CX231XX_H
24 
25 #include <linux/videodev2.h>
26 #include <linux/types.h>
27 #include <linux/ioctl.h>
28 #include <linux/i2c.h>
29 #include <linux/workqueue.h>
30 #include <linux/mutex.h>
31 
32 #include <media/cx2341x.h>
33 
34 #include <media/videobuf-vmalloc.h>
35 #include <media/v4l2-device.h>
36 #include <media/v4l2-ctrls.h>
37 #include <media/v4l2-fh.h>
38 #include <media/rc-core.h>
39 #include <media/ir-kbd-i2c.h>
40 #include <media/videobuf-dvb.h>
41 
42 #include "cx231xx-reg.h"
43 #include "cx231xx-pcb-cfg.h"
44 #include "cx231xx-conf-reg.h"
45 
46 #define DRIVER_NAME                     "cx231xx"
47 #define PWR_SLEEP_INTERVAL              10
48 
49 /* I2C addresses for control block in Cx231xx */
50 #define     AFE_DEVICE_ADDRESS		0x60
51 #define     I2S_BLK_DEVICE_ADDRESS	0x98
52 #define     VID_BLK_I2C_ADDRESS		0x88
53 #define     VERVE_I2C_ADDRESS           0x40
54 #define     DIF_USE_BASEBAND            0xFFFFFFFF
55 
56 /* Boards supported by driver */
57 #define CX231XX_BOARD_UNKNOWN		    0
58 #define CX231XX_BOARD_CNXT_CARRAERA	1
59 #define CX231XX_BOARD_CNXT_SHELBY	2
60 #define CX231XX_BOARD_CNXT_RDE_253S	3
61 #define CX231XX_BOARD_CNXT_RDU_253S	4
62 #define CX231XX_BOARD_CNXT_VIDEO_GRABBER	5
63 #define CX231XX_BOARD_CNXT_RDE_250	6
64 #define CX231XX_BOARD_CNXT_RDU_250	7
65 #define CX231XX_BOARD_HAUPPAUGE_EXETER  8
66 #define CX231XX_BOARD_HAUPPAUGE_USBLIVE2 9
67 #define CX231XX_BOARD_PV_PLAYTV_USB_HYBRID 10
68 #define CX231XX_BOARD_PV_XCAPTURE_USB 11
69 #define CX231XX_BOARD_KWORLD_UB430_USB_HYBRID 12
70 #define CX231XX_BOARD_ICONBIT_U100 13
71 #define CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL 14
72 #define CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC 15
73 #define CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2 16
74 #define CX231XX_BOARD_OTG102 17
75 #define CX231XX_BOARD_KWORLD_UB445_USB_HYBRID 18
76 #define CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx 19
77 #define CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx 20
78 
79 /* Limits minimum and default number of buffers */
80 #define CX231XX_MIN_BUF                 4
81 #define CX231XX_DEF_BUF                 12
82 #define CX231XX_DEF_VBI_BUF             6
83 
84 #define VBI_LINE_COUNT                  17
85 #define VBI_LINE_LENGTH                 1440
86 
87 /*Limits the max URB message size */
88 #define URB_MAX_CTRL_SIZE               80
89 
90 /* Params for validated field */
91 #define CX231XX_BOARD_NOT_VALIDATED     1
92 #define CX231XX_BOARD_VALIDATED		0
93 
94 /* maximum number of cx231xx boards */
95 #define CX231XX_MAXBOARDS               8
96 
97 /* maximum number of frames that can be queued */
98 #define CX231XX_NUM_FRAMES              5
99 
100 /* number of buffers for isoc transfers */
101 #define CX231XX_NUM_BUFS                8
102 
103 /* number of packets for each buffer
104    windows requests only 40 packets .. so we better do the same
105    this is what I found out for all alternate numbers there!
106  */
107 #define CX231XX_NUM_PACKETS             40
108 
109 /* default alternate; 0 means choose the best */
110 #define CX231XX_PINOUT                  0
111 
112 #define CX231XX_INTERLACED_DEFAULT      1
113 
114 /* time to wait when stopping the isoc transfer */
115 #define CX231XX_URB_TIMEOUT		\
116 		msecs_to_jiffies(CX231XX_NUM_BUFS * CX231XX_NUM_PACKETS)
117 
118 #define CX231xx_NORMS (\
119 	V4L2_STD_NTSC_M |  V4L2_STD_NTSC_M_JP |  V4L2_STD_NTSC_443 | \
120 	V4L2_STD_PAL_BG |  V4L2_STD_PAL_DK    |  V4L2_STD_PAL_I    | \
121 	V4L2_STD_PAL_M  |  V4L2_STD_PAL_N     |  V4L2_STD_PAL_Nc   | \
122 	V4L2_STD_PAL_60 |  V4L2_STD_SECAM_L   |  V4L2_STD_SECAM_DK)
123 
124 #define SLEEP_S5H1432    30
125 #define CX23417_OSC_EN   8
126 #define CX23417_RESET    9
127 
128 struct cx23417_fmt {
129 	char  *name;
130 	u32   fourcc;          /* v4l2 format id */
131 	int   depth;
132 	int   flags;
133 	u32   cxformat;
134 };
135 enum cx231xx_mode {
136 	CX231XX_SUSPEND,
137 	CX231XX_ANALOG_MODE,
138 	CX231XX_DIGITAL_MODE,
139 };
140 
141 enum cx231xx_std_mode {
142 	CX231XX_TV_AIR = 0,
143 	CX231XX_TV_CABLE
144 };
145 
146 enum cx231xx_stream_state {
147 	STREAM_OFF,
148 	STREAM_INTERRUPT,
149 	STREAM_ON,
150 };
151 
152 struct cx231xx;
153 
154 struct cx231xx_isoc_ctl {
155 	/* max packet size of isoc transaction */
156 	int max_pkt_size;
157 
158 	/* number of allocated urbs */
159 	int num_bufs;
160 
161 	/* urb for isoc transfers */
162 	struct urb **urb;
163 
164 	/* transfer buffers for isoc transfer */
165 	char **transfer_buffer;
166 
167 	/* Last buffer command and region */
168 	u8 cmd;
169 	int pos, size, pktsize;
170 
171 	/* Last field: ODD or EVEN? */
172 	int field;
173 
174 	/* Stores incomplete commands */
175 	u32 tmp_buf;
176 	int tmp_buf_len;
177 
178 	/* Stores already requested buffers */
179 	struct cx231xx_buffer *buf;
180 
181 	/* Stores the number of received fields */
182 	int nfields;
183 
184 	/* isoc urb callback */
185 	int (*isoc_copy) (struct cx231xx *dev, struct urb *urb);
186 };
187 
188 struct cx231xx_bulk_ctl {
189 	/* max packet size of bulk transaction */
190 	int max_pkt_size;
191 
192 	/* number of allocated urbs */
193 	int num_bufs;
194 
195 	/* urb for bulk transfers */
196 	struct urb **urb;
197 
198 	/* transfer buffers for bulk transfer */
199 	char **transfer_buffer;
200 
201 	/* Last buffer command and region */
202 	u8 cmd;
203 	int pos, size, pktsize;
204 
205 	/* Last field: ODD or EVEN? */
206 	int field;
207 
208 	/* Stores incomplete commands */
209 	u32 tmp_buf;
210 	int tmp_buf_len;
211 
212 	/* Stores already requested buffers */
213 	struct cx231xx_buffer *buf;
214 
215 	/* Stores the number of received fields */
216 	int nfields;
217 
218 	/* bulk urb callback */
219 	int (*bulk_copy) (struct cx231xx *dev, struct urb *urb);
220 };
221 
222 struct cx231xx_fmt {
223 	char *name;
224 	u32 fourcc;		/* v4l2 format id */
225 	int depth;
226 	int reg;
227 };
228 
229 /* buffer for one video frame */
230 struct cx231xx_buffer {
231 	/* common v4l buffer stuff -- must be first */
232 	struct videobuf_buffer vb;
233 
234 	struct list_head frame;
235 	int top_field;
236 	int receiving;
237 };
238 
239 enum ps_package_head {
240 	CX231XX_NEED_ADD_PS_PACKAGE_HEAD = 0,
241 	CX231XX_NONEED_PS_PACKAGE_HEAD
242 };
243 
244 struct cx231xx_dmaqueue {
245 	struct list_head active;
246 	struct list_head queued;
247 
248 	wait_queue_head_t wq;
249 
250 	/* Counters to control buffer fill */
251 	int pos;
252 	u8 is_partial_line;
253 	u8 partial_buf[8];
254 	u8 last_sav;
255 	int current_field;
256 	u32 bytes_left_in_line;
257 	u32 lines_completed;
258 	u8 field1_done;
259 	u32 lines_per_field;
260 
261 	/*Mpeg2 control buffer*/
262 	u8 *p_left_data;
263 	u32 left_data_count;
264 	u8 mpeg_buffer_done;
265 	u32 mpeg_buffer_completed;
266 	enum ps_package_head add_ps_package_head;
267 	char ps_head[10];
268 };
269 
270 /* inputs */
271 
272 #define MAX_CX231XX_INPUT               4
273 
274 enum cx231xx_itype {
275 	CX231XX_VMUX_COMPOSITE1 = 1,
276 	CX231XX_VMUX_SVIDEO,
277 	CX231XX_VMUX_TELEVISION,
278 	CX231XX_VMUX_CABLE,
279 	CX231XX_RADIO,
280 	CX231XX_VMUX_DVB,
281 	CX231XX_VMUX_DEBUG
282 };
283 
284 enum cx231xx_v_input {
285 	CX231XX_VIN_1_1 = 0x1,
286 	CX231XX_VIN_2_1,
287 	CX231XX_VIN_3_1,
288 	CX231XX_VIN_4_1,
289 	CX231XX_VIN_1_2 = 0x01,
290 	CX231XX_VIN_2_2,
291 	CX231XX_VIN_3_2,
292 	CX231XX_VIN_1_3 = 0x1,
293 	CX231XX_VIN_2_3,
294 	CX231XX_VIN_3_3,
295 };
296 
297 /* cx231xx has two audio inputs: tuner and line in */
298 enum cx231xx_amux {
299 	/* This is the only entry for cx231xx tuner input */
300 	CX231XX_AMUX_VIDEO,	/* cx231xx tuner */
301 	CX231XX_AMUX_LINE_IN,	/* Line In */
302 };
303 
304 struct cx231xx_reg_seq {
305 	unsigned char bit;
306 	unsigned char val;
307 	int sleep;
308 };
309 
310 struct cx231xx_input {
311 	enum cx231xx_itype type;
312 	unsigned int vmux;
313 	enum cx231xx_amux amux;
314 	struct cx231xx_reg_seq *gpio;
315 };
316 
317 #define INPUT(nr) (&cx231xx_boards[dev->model].input[nr])
318 
319 enum cx231xx_decoder {
320 	CX231XX_NODECODER,
321 	CX231XX_AVDECODER
322 };
323 
324 enum CX231XX_I2C_MASTER_PORT {
325 	I2C_0 = 0,
326 	I2C_1 = 1,
327 	I2C_2 = 2,
328 	I2C_3 = 3
329 };
330 
331 struct cx231xx_board {
332 	char *name;
333 	int vchannels;
334 	int tuner_type;
335 	int tuner_addr;
336 	v4l2_std_id norm;	/* tv norm */
337 
338 	/* demod related */
339 	int demod_addr;
340 	u8 demod_xfer_mode;	/* 0 - Serial; 1 - parallel */
341 
342 	/* GPIO Pins */
343 	struct cx231xx_reg_seq *dvb_gpio;
344 	struct cx231xx_reg_seq *suspend_gpio;
345 	struct cx231xx_reg_seq *tuner_gpio;
346 		/* Negative means don't use it */
347 	s8 tuner_sif_gpio;
348 	s8 tuner_scl_gpio;
349 	s8 tuner_sda_gpio;
350 
351 	/* PIN ctrl */
352 	u32 ctl_pin_status_mask;
353 	u8 agc_analog_digital_select_gpio;
354 	u32 gpio_pin_status_mask;
355 
356 	/* i2c masters */
357 	u8 tuner_i2c_master;
358 	u8 demod_i2c_master;
359 	u8 ir_i2c_master;
360 
361 	/* for devices with I2C chips for IR */
362 	char *rc_map_name;
363 
364 	unsigned int max_range_640_480:1;
365 	unsigned int has_dvb:1;
366 	unsigned int has_417:1;
367 	unsigned int valid:1;
368 	unsigned int no_alt_vanc:1;
369 	unsigned int external_av:1;
370 	unsigned int dont_use_port_3:1;
371 
372 	unsigned char xclk, i2c_speed;
373 
374 	enum cx231xx_decoder decoder;
375 	int output_mode;
376 
377 	struct cx231xx_input input[MAX_CX231XX_INPUT];
378 	struct cx231xx_input radio;
379 	struct rc_map *ir_codes;
380 };
381 
382 /* device states */
383 enum cx231xx_dev_state {
384 	DEV_INITIALIZED = 0x01,
385 	DEV_DISCONNECTED = 0x02,
386 };
387 
388 enum AFE_MODE {
389 	AFE_MODE_LOW_IF,
390 	AFE_MODE_BASEBAND,
391 	AFE_MODE_EU_HI_IF,
392 	AFE_MODE_US_HI_IF,
393 	AFE_MODE_JAPAN_HI_IF
394 };
395 
396 enum AUDIO_INPUT {
397 	AUDIO_INPUT_MUTE,
398 	AUDIO_INPUT_LINE,
399 	AUDIO_INPUT_TUNER_TV,
400 	AUDIO_INPUT_SPDIF,
401 	AUDIO_INPUT_TUNER_FM
402 };
403 
404 #define CX231XX_AUDIO_BUFS              5
405 #define CX231XX_NUM_AUDIO_PACKETS       16
406 #define CX231XX_ISO_NUM_AUDIO_PACKETS	64
407 
408 /* cx231xx extensions */
409 #define CX231XX_AUDIO                   0x10
410 #define CX231XX_DVB                     0x20
411 
412 struct cx231xx_audio {
413 	char name[50];
414 	char *transfer_buffer[CX231XX_AUDIO_BUFS];
415 	struct urb *urb[CX231XX_AUDIO_BUFS];
416 	struct usb_device *udev;
417 	unsigned int capture_transfer_done;
418 	struct snd_pcm_substream *capture_pcm_substream;
419 
420 	unsigned int hwptr_done_capture;
421 	struct snd_card *sndcard;
422 
423 	int users, shutdown;
424 	/* locks */
425 	spinlock_t slock;
426 
427 	int alt;		/* alternate */
428 	int max_pkt_size;	/* max packet size of isoc transaction */
429 	int num_alt;		/* Number of alternative settings */
430 	unsigned int *alt_max_pkt_size;	/* array of wMaxPacketSize */
431 	u16 end_point_addr;
432 };
433 
434 struct cx231xx;
435 
436 struct cx231xx_fh {
437 	struct v4l2_fh fh;
438 	struct cx231xx *dev;
439 	unsigned int stream_on:1;	/* Locks streams */
440 	enum v4l2_buf_type type;
441 
442 	struct videobuf_queue vb_vidq;
443 
444 	/* vbi capture */
445 	struct videobuf_queue      vidq;
446 	struct videobuf_queue      vbiq;
447 
448 	/* MPEG Encoder specifics ONLY */
449 
450 	atomic_t                   v4l_reading;
451 };
452 
453 /*****************************************************************/
454 /* set/get i2c */
455 /* 00--1Mb/s, 01-400kb/s, 10--100kb/s, 11--5Mb/s */
456 #define I2C_SPEED_1M            0x0
457 #define I2C_SPEED_400K          0x1
458 #define I2C_SPEED_100K          0x2
459 #define I2C_SPEED_5M            0x3
460 
461 /* 0-- STOP transaction */
462 #define I2C_STOP                0x0
463 /* 1-- do not transmit STOP at end of transaction */
464 #define I2C_NOSTOP              0x1
465 /* 1--allow slave to insert clock wait states */
466 #define I2C_SYNC                0x1
467 
468 struct cx231xx_i2c {
469 	struct cx231xx *dev;
470 
471 	int nr;
472 
473 	/* i2c i/o */
474 	struct i2c_adapter i2c_adap;
475 	struct i2c_client i2c_client;
476 	u32 i2c_rc;
477 
478 	/* different settings for each bus */
479 	u8 i2c_period;
480 	u8 i2c_nostop;
481 	u8 i2c_reserve;
482 };
483 
484 struct cx231xx_i2c_xfer_data {
485 	u8 dev_addr;
486 	u8 direction;		/* 1 - IN, 0 - OUT */
487 	u8 saddr_len;		/* sub address len */
488 	u16 saddr_dat;		/* sub addr data */
489 	u8 buf_size;		/* buffer size */
490 	u8 *p_buffer;		/* pointer to the buffer */
491 };
492 
493 struct VENDOR_REQUEST_IN {
494 	u8 bRequest;
495 	u16 wValue;
496 	u16 wIndex;
497 	u16 wLength;
498 	u8 direction;
499 	u8 bData;
500 	u8 *pBuff;
501 };
502 
503 struct cx231xx_tvnorm {
504 	char		*name;
505 	v4l2_std_id	id;
506 	u32		cxiformat;
507 	u32		cxoformat;
508 };
509 
510 enum TRANSFER_TYPE {
511 	Raw_Video = 0,
512 	Audio,
513 	Vbi,			/* VANC */
514 	Sliced_cc,		/* HANC */
515 	TS1_serial_mode,
516 	TS2,
517 	TS1_parallel_mode
518 } ;
519 
520 struct cx231xx_video_mode {
521 	/* Isoc control struct */
522 	struct cx231xx_dmaqueue vidq;
523 	struct cx231xx_isoc_ctl isoc_ctl;
524 	struct cx231xx_bulk_ctl bulk_ctl;
525 	/* locks */
526 	spinlock_t slock;
527 
528 	/* usb transfer */
529 	int alt;		/* alternate */
530 	int max_pkt_size;	/* max packet size of isoc transaction */
531 	int num_alt;		/* Number of alternative settings */
532 	unsigned int *alt_max_pkt_size;	/* array of wMaxPacketSize */
533 	u16 end_point_addr;
534 };
535 /*
536 struct cx23885_dmaqueue {
537 	struct list_head       active;
538 	struct list_head       queued;
539 	struct timer_list      timeout;
540 	struct btcx_riscmem    stopper;
541 	u32                    count;
542 };
543 */
544 struct cx231xx_tsport {
545 	struct cx231xx *dev;
546 
547 	int                        nr;
548 	int                        sram_chno;
549 
550 	struct videobuf_dvb_frontends frontends;
551 
552 	/* dma queues */
553 
554 	u32                        ts_packet_size;
555 	u32                        ts_packet_count;
556 
557 	int                        width;
558 	int                        height;
559 
560 	/* locks */
561 	spinlock_t                 slock;
562 
563 	/* registers */
564 	u32                        reg_gpcnt;
565 	u32                        reg_gpcnt_ctl;
566 	u32                        reg_dma_ctl;
567 	u32                        reg_lngth;
568 	u32                        reg_hw_sop_ctrl;
569 	u32                        reg_gen_ctrl;
570 	u32                        reg_bd_pkt_status;
571 	u32                        reg_sop_status;
572 	u32                        reg_fifo_ovfl_stat;
573 	u32                        reg_vld_misc;
574 	u32                        reg_ts_clk_en;
575 	u32                        reg_ts_int_msk;
576 	u32                        reg_ts_int_stat;
577 	u32                        reg_src_sel;
578 
579 	/* Default register vals */
580 	int                        pci_irqmask;
581 	u32                        dma_ctl_val;
582 	u32                        ts_int_msk_val;
583 	u32                        gen_ctrl_val;
584 	u32                        ts_clk_en_val;
585 	u32                        src_sel_val;
586 	u32                        vld_misc_val;
587 	u32                        hw_sop_ctrl_val;
588 
589 	/* Allow a single tsport to have multiple frontends */
590 	u32                        num_frontends;
591 	void                       *port_priv;
592 };
593 
594 /* main device struct */
595 struct cx231xx {
596 	/* generic device properties */
597 	char name[30];		/* name (including minor) of the device */
598 	int model;		/* index in the device_data struct */
599 	int devno;		/* marks the number of this device */
600 
601 	struct cx231xx_board board;
602 
603 	/* For I2C IR support */
604 	struct IR_i2c_init_data    init_data;
605 	struct i2c_client          *ir_i2c_client;
606 
607 	unsigned int stream_on:1;	/* Locks streams */
608 	unsigned int vbi_stream_on:1;	/* Locks streams for VBI */
609 	unsigned int has_audio_class:1;
610 	unsigned int has_alsa_audio:1;
611 
612 	struct cx231xx_fmt *format;
613 
614 	struct v4l2_device v4l2_dev;
615 	struct v4l2_subdev *sd_cx25840;
616 	struct v4l2_subdev *sd_tuner;
617 	struct v4l2_ctrl_handler ctrl_handler;
618 	struct v4l2_ctrl_handler radio_ctrl_handler;
619 	struct cx2341x_handler mpeg_ctrl_handler;
620 
621 	struct work_struct wq_trigger;		/* Trigger to start/stop audio for alsa module */
622 	atomic_t	   stream_started;	/* stream should be running if true */
623 
624 	struct list_head devlist;
625 
626 	int tuner_type;		/* type of the tuner */
627 	int tuner_addr;		/* tuner address */
628 
629 	/* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
630 	struct cx231xx_i2c i2c_bus[3];
631 	unsigned int xc_fw_load_done:1;
632 	/* locks */
633 	struct mutex gpio_i2c_lock;
634 	struct mutex i2c_lock;
635 
636 	/* video for linux */
637 	int users;		/* user count for exclusive use */
638 	struct video_device *vdev;	/* video for linux device struct */
639 	v4l2_std_id norm;	/* selected tv norm */
640 	int ctl_freq;		/* selected frequency */
641 	unsigned int ctl_ainput;	/* selected audio input */
642 
643 	/* frame properties */
644 	int width;		/* current frame width */
645 	int height;		/* current frame height */
646 	int interlaced;		/* 1=interlace fileds, 0=just top fileds */
647 
648 	struct cx231xx_audio adev;
649 
650 	/* states */
651 	enum cx231xx_dev_state state;
652 
653 	struct work_struct request_module_wk;
654 
655 	/* locks */
656 	struct mutex lock;
657 	struct mutex ctrl_urb_lock;	/* protects urb_buf */
658 	struct list_head inqueue, outqueue;
659 	wait_queue_head_t open, wait_frame, wait_stream;
660 	struct video_device *vbi_dev;
661 	struct video_device *radio_dev;
662 
663 	unsigned char eedata[256];
664 
665 	struct cx231xx_video_mode video_mode;
666 	struct cx231xx_video_mode vbi_mode;
667 	struct cx231xx_video_mode sliced_cc_mode;
668 	struct cx231xx_video_mode ts1_mode;
669 
670 	atomic_t devlist_count;
671 
672 	struct usb_device *udev;	/* the usb device */
673 	char urb_buf[URB_MAX_CTRL_SIZE];	/* urb control msg buffer */
674 
675 	/* helper funcs that call usb_control_msg */
676 	int (*cx231xx_read_ctrl_reg) (struct cx231xx *dev, u8 req, u16 reg,
677 				      char *buf, int len);
678 	int (*cx231xx_write_ctrl_reg) (struct cx231xx *dev, u8 req, u16 reg,
679 				       char *buf, int len);
680 	int (*cx231xx_send_usb_command) (struct cx231xx_i2c *i2c_bus,
681 				struct cx231xx_i2c_xfer_data *req_data);
682 	int (*cx231xx_gpio_i2c_read) (struct cx231xx *dev, u8 dev_addr,
683 				      u8 *buf, u8 len);
684 	int (*cx231xx_gpio_i2c_write) (struct cx231xx *dev, u8 dev_addr,
685 				       u8 *buf, u8 len);
686 
687 	int (*cx231xx_set_analog_freq) (struct cx231xx *dev, u32 freq);
688 	int (*cx231xx_reset_analog_tuner) (struct cx231xx *dev);
689 
690 	enum cx231xx_mode mode;
691 
692 	struct cx231xx_dvb *dvb;
693 
694 	/* Cx231xx supported PCB config's */
695 	struct pcb_config current_pcb_config;
696 	u8 current_scenario_idx;
697 	u8 interface_count;
698 	u8 max_iad_interface_count;
699 
700 	/* GPIO related register direction and values */
701 	u32 gpio_dir;
702 	u32 gpio_val;
703 
704 	/* Power Modes */
705 	int power_mode;
706 
707 	/* afe parameters */
708 	enum AFE_MODE afe_mode;
709 	u32 afe_ref_count;
710 
711 	/* video related parameters */
712 	u32 video_input;
713 	u32 active_mode;
714 	u8 vbi_or_sliced_cc_mode;	/* 0 - vbi ; 1 - sliced cc mode */
715 	enum cx231xx_std_mode std_mode;	/* 0 - Air; 1 - cable */
716 
717 	/*mode: digital=1 or analog=0*/
718 	u8 mode_tv;
719 
720 	u8 USE_ISO;
721 	struct cx231xx_tvnorm      encodernorm;
722 	struct cx231xx_tsport      ts1, ts2;
723 	struct video_device        *v4l_device;
724 	atomic_t                   v4l_reader_count;
725 	u32                        freq;
726 	unsigned int               input;
727 	u32                        cx23417_mailbox;
728 	u32                        __iomem *lmmio;
729 	u8                         __iomem *bmmio;
730 };
731 
732 extern struct list_head cx231xx_devlist;
733 
734 #define cx25840_call(cx231xx, o, f, args...) \
735 	v4l2_subdev_call(cx231xx->sd_cx25840, o, f, ##args)
736 #define tuner_call(cx231xx, o, f, args...) \
737 	v4l2_subdev_call(cx231xx->sd_tuner, o, f, ##args)
738 #define call_all(dev, o, f, args...) \
739 	v4l2_device_call_until_err(&dev->v4l2_dev, 0, o, f, ##args)
740 
741 struct cx231xx_ops {
742 	struct list_head next;
743 	char *name;
744 	int id;
745 	int (*init) (struct cx231xx *);
746 	int (*fini) (struct cx231xx *);
747 };
748 
749 /* call back functions in dvb module */
750 int cx231xx_set_analog_freq(struct cx231xx *dev, u32 freq);
751 int cx231xx_reset_analog_tuner(struct cx231xx *dev);
752 
753 /* Provided by cx231xx-i2c.c */
754 void cx231xx_do_i2c_scan(struct cx231xx *dev, struct i2c_client *c);
755 int cx231xx_i2c_register(struct cx231xx_i2c *bus);
756 int cx231xx_i2c_unregister(struct cx231xx_i2c *bus);
757 
758 /* Internal block control functions */
759 int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
760 		 u8 saddr_len, u32 *data, u8 data_len, int master);
761 int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
762 		 u8 saddr_len, u32 data, u8 data_len, int master);
763 int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr,
764 			  u16 saddr, u8 saddr_len, u32 *data, u8 data_len);
765 int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr,
766 			   u16 saddr, u8 saddr_len, u32 data, u8 data_len);
767 int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
768 			   u16 register_address, u8 bit_start, u8 bit_end,
769 			   u32 value);
770 int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
771 					u16 saddr, u32 mask, u32 value);
772 u32 cx231xx_set_field(u32 field_mask, u32 data);
773 
774 /*verve r/w*/
775 void initGPIO(struct cx231xx *dev);
776 void uninitGPIO(struct cx231xx *dev);
777 /* afe related functions */
778 int cx231xx_afe_init_super_block(struct cx231xx *dev, u32 ref_count);
779 int cx231xx_afe_init_channels(struct cx231xx *dev);
780 int cx231xx_afe_setup_AFE_for_baseband(struct cx231xx *dev);
781 int cx231xx_afe_set_input_mux(struct cx231xx *dev, u32 input_mux);
782 int cx231xx_afe_set_mode(struct cx231xx *dev, enum AFE_MODE mode);
783 int cx231xx_afe_update_power_control(struct cx231xx *dev,
784 					enum AV_MODE avmode);
785 int cx231xx_afe_adjust_ref_count(struct cx231xx *dev, u32 video_input);
786 
787 /* i2s block related functions */
788 int cx231xx_i2s_blk_initialize(struct cx231xx *dev);
789 int cx231xx_i2s_blk_update_power_control(struct cx231xx *dev,
790 					enum AV_MODE avmode);
791 int cx231xx_i2s_blk_set_audio_input(struct cx231xx *dev, u8 audio_input);
792 
793 /* DIF related functions */
794 int cx231xx_dif_configure_C2HH_for_low_IF(struct cx231xx *dev, u32 mode,
795 					  u32 function_mode, u32 standard);
796 void cx231xx_set_Colibri_For_LowIF(struct cx231xx *dev, u32 if_freq,
797 					 u8 spectral_invert, u32 mode);
798 u32 cx231xx_Get_Colibri_CarrierOffset(u32 mode, u32 standerd);
799 void cx231xx_set_DIF_bandpass(struct cx231xx *dev, u32 if_freq,
800 					 u8 spectral_invert, u32 mode);
801 void cx231xx_Setup_AFE_for_LowIF(struct cx231xx *dev);
802 void reset_s5h1432_demod(struct cx231xx *dev);
803 void cx231xx_dump_HH_reg(struct cx231xx *dev);
804 void update_HH_register_after_set_DIF(struct cx231xx *dev);
805 void cx231xx_dump_SC_reg(struct cx231xx *dev);
806 
807 
808 
809 int cx231xx_dif_set_standard(struct cx231xx *dev, u32 standard);
810 int cx231xx_tuner_pre_channel_change(struct cx231xx *dev);
811 int cx231xx_tuner_post_channel_change(struct cx231xx *dev);
812 
813 /* video parser functions */
814 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size,
815 			     u32 *p_bytes_used);
816 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
817 				 u32 *p_bytes_used);
818 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
819 		    u8 *p_buffer, u32 bytes_to_copy);
820 void cx231xx_reset_video_buffer(struct cx231xx *dev,
821 				struct cx231xx_dmaqueue *dma_q);
822 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q);
823 u32 cx231xx_copy_video_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
824 			    u8 *p_line, u32 length, int field_number);
825 u32 cx231xx_get_video_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
826 			   u8 sav_eav, u8 *p_buffer, u32 buffer_size);
827 void cx231xx_swab(u16 *from, u16 *to, u16 len);
828 
829 /* Provided by cx231xx-core.c */
830 
831 u32 cx231xx_request_buffers(struct cx231xx *dev, u32 count);
832 void cx231xx_queue_unusedframes(struct cx231xx *dev);
833 void cx231xx_release_buffers(struct cx231xx *dev);
834 
835 /* read from control pipe */
836 int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
837 			  char *buf, int len);
838 
839 /* write to control pipe */
840 int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
841 			   char *buf, int len);
842 int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode);
843 
844 int cx231xx_send_vendor_cmd(struct cx231xx *dev,
845 				struct VENDOR_REQUEST_IN *ven_req);
846 int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
847 				struct cx231xx_i2c_xfer_data *req_data);
848 
849 /* Gpio related functions */
850 int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val,
851 			  u8 len, u8 request, u8 direction);
852 int cx231xx_set_gpio_value(struct cx231xx *dev, int pin_number, int pin_value);
853 int cx231xx_set_gpio_direction(struct cx231xx *dev, int pin_number,
854 			       int pin_value);
855 
856 int cx231xx_gpio_i2c_start(struct cx231xx *dev);
857 int cx231xx_gpio_i2c_end(struct cx231xx *dev);
858 int cx231xx_gpio_i2c_write_byte(struct cx231xx *dev, u8 data);
859 int cx231xx_gpio_i2c_read_byte(struct cx231xx *dev, u8 *buf);
860 int cx231xx_gpio_i2c_read_ack(struct cx231xx *dev);
861 int cx231xx_gpio_i2c_write_ack(struct cx231xx *dev);
862 int cx231xx_gpio_i2c_write_nak(struct cx231xx *dev);
863 
864 int cx231xx_gpio_i2c_read(struct cx231xx *dev, u8 dev_addr, u8 *buf, u8 len);
865 int cx231xx_gpio_i2c_write(struct cx231xx *dev, u8 dev_addr, u8 *buf, u8 len);
866 
867 /* audio related functions */
868 int cx231xx_set_audio_decoder_input(struct cx231xx *dev,
869 				    enum AUDIO_INPUT audio_input);
870 
871 int cx231xx_capture_start(struct cx231xx *dev, int start, u8 media_type);
872 int cx231xx_set_video_alternate(struct cx231xx *dev);
873 int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt);
874 int is_fw_load(struct cx231xx *dev);
875 int cx231xx_check_fw(struct cx231xx *dev);
876 int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
877 		      int num_bufs, int max_pkt_size,
878 		      int (*isoc_copy) (struct cx231xx *dev,
879 					struct urb *urb));
880 int cx231xx_init_bulk(struct cx231xx *dev, int max_packets,
881 		      int num_bufs, int max_pkt_size,
882 		      int (*bulk_copy) (struct cx231xx *dev,
883 					struct urb *urb));
884 void cx231xx_stop_TS1(struct cx231xx *dev);
885 void cx231xx_start_TS1(struct cx231xx *dev);
886 void cx231xx_uninit_isoc(struct cx231xx *dev);
887 void cx231xx_uninit_bulk(struct cx231xx *dev);
888 int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode);
889 int cx231xx_unmute_audio(struct cx231xx *dev);
890 int cx231xx_ep5_bulkout(struct cx231xx *dev, u8 *firmware, u16 size);
891 void cx231xx_disable656(struct cx231xx *dev);
892 void cx231xx_enable656(struct cx231xx *dev);
893 int cx231xx_demod_reset(struct cx231xx *dev);
894 int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio);
895 
896 /* Device list functions */
897 void cx231xx_release_resources(struct cx231xx *dev);
898 void cx231xx_release_analog_resources(struct cx231xx *dev);
899 int cx231xx_register_analog_devices(struct cx231xx *dev);
900 void cx231xx_remove_from_devlist(struct cx231xx *dev);
901 void cx231xx_add_into_devlist(struct cx231xx *dev);
902 void cx231xx_init_extension(struct cx231xx *dev);
903 void cx231xx_close_extension(struct cx231xx *dev);
904 
905 /* hardware init functions */
906 int cx231xx_dev_init(struct cx231xx *dev);
907 void cx231xx_dev_uninit(struct cx231xx *dev);
908 void cx231xx_config_i2c(struct cx231xx *dev);
909 int cx231xx_config(struct cx231xx *dev);
910 
911 /* Stream control functions */
912 int cx231xx_start_stream(struct cx231xx *dev, u32 ep_mask);
913 int cx231xx_stop_stream(struct cx231xx *dev, u32 ep_mask);
914 
915 int cx231xx_initialize_stream_xfer(struct cx231xx *dev, u32 media_type);
916 
917 /* Power control functions */
918 int cx231xx_set_power_mode(struct cx231xx *dev, enum AV_MODE mode);
919 int cx231xx_power_suspend(struct cx231xx *dev);
920 
921 /* chip specific control functions */
922 int cx231xx_init_ctrl_pin_status(struct cx231xx *dev);
923 int cx231xx_set_agc_analog_digital_mux_select(struct cx231xx *dev,
924 					      u8 analog_or_digital);
925 int cx231xx_enable_i2c_port_3(struct cx231xx *dev, bool is_port_3);
926 
927 /* video audio decoder related functions */
928 void video_mux(struct cx231xx *dev, int index);
929 int cx231xx_set_video_input_mux(struct cx231xx *dev, u8 input);
930 int cx231xx_set_decoder_video_input(struct cx231xx *dev, u8 pin_type, u8 input);
931 int cx231xx_do_mode_ctrl_overrides(struct cx231xx *dev);
932 int cx231xx_set_audio_input(struct cx231xx *dev, u8 input);
933 
934 /* Provided by cx231xx-video.c */
935 int cx231xx_register_extension(struct cx231xx_ops *dev);
936 void cx231xx_unregister_extension(struct cx231xx_ops *dev);
937 void cx231xx_init_extension(struct cx231xx *dev);
938 void cx231xx_close_extension(struct cx231xx *dev);
939 int cx231xx_querycap(struct file *file, void *priv,
940 			   struct v4l2_capability *cap);
941 int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t);
942 int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t);
943 int cx231xx_g_frequency(struct file *file, void *priv,
944 			      struct v4l2_frequency *f);
945 int cx231xx_s_frequency(struct file *file, void *priv,
946 			      const struct v4l2_frequency *f);
947 int cx231xx_enum_input(struct file *file, void *priv,
948 			     struct v4l2_input *i);
949 int cx231xx_g_input(struct file *file, void *priv, unsigned int *i);
950 int cx231xx_s_input(struct file *file, void *priv, unsigned int i);
951 int cx231xx_g_chip_info(struct file *file, void *fh, struct v4l2_dbg_chip_info *chip);
952 int cx231xx_g_register(struct file *file, void *priv,
953 			     struct v4l2_dbg_register *reg);
954 int cx231xx_s_register(struct file *file, void *priv,
955 			     const struct v4l2_dbg_register *reg);
956 
957 /* Provided by cx231xx-cards.c */
958 extern void cx231xx_pre_card_setup(struct cx231xx *dev);
959 extern void cx231xx_card_setup(struct cx231xx *dev);
960 extern struct cx231xx_board cx231xx_boards[];
961 extern struct usb_device_id cx231xx_id_table[];
962 extern const unsigned int cx231xx_bcount;
963 int cx231xx_tuner_callback(void *ptr, int component, int command, int arg);
964 
965 /* cx23885-417.c                                               */
966 extern int cx231xx_417_register(struct cx231xx *dev);
967 extern void cx231xx_417_unregister(struct cx231xx *dev);
968 
969 /* cx23885-input.c                                             */
970 
971 #if defined(CONFIG_VIDEO_CX231XX_RC)
972 int cx231xx_ir_init(struct cx231xx *dev);
973 void cx231xx_ir_exit(struct cx231xx *dev);
974 #else
975 #define cx231xx_ir_init(dev)	(0)
976 #define cx231xx_ir_exit(dev)	(0)
977 #endif
978 
979 
980 /* printk macros */
981 
982 #define cx231xx_err(fmt, arg...) do {\
983 	printk(KERN_ERR fmt , ##arg); } while (0)
984 
985 #define cx231xx_errdev(fmt, arg...) do {\
986 	printk(KERN_ERR "%s: "fmt,\
987 			dev->name , ##arg); } while (0)
988 
989 #define cx231xx_info(fmt, arg...) do {\
990 	printk(KERN_INFO "%s: "fmt,\
991 			dev->name , ##arg); } while (0)
992 #define cx231xx_warn(fmt, arg...) do {\
993 	printk(KERN_WARNING "%s: "fmt,\
994 			dev->name , ##arg); } while (0)
995 
996 static inline unsigned int norm_maxw(struct cx231xx *dev)
997 {
998 	if (dev->board.max_range_640_480)
999 		return 640;
1000 	else
1001 		return 720;
1002 }
1003 
1004 static inline unsigned int norm_maxh(struct cx231xx *dev)
1005 {
1006 	if (dev->board.max_range_640_480)
1007 		return 480;
1008 	else
1009 		return (dev->norm & V4L2_STD_625_50) ? 576 : 480;
1010 }
1011 #endif
1012