1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #include <linux/pci.h>
34 #include <linux/i2c.h>
35 #include <linux/kdev_t.h>
36 #include <linux/mutex.h>
37 #include <linux/crc32.h>
38 #include <linux/kthread.h>
39 #include <linux/freezer.h>
40
41 #include <media/tuner.h>
42 #include <media/tveeprom.h>
43 #include <media/dvb_demux.h>
44 #include <media/dvb_frontend.h>
45 #include <media/dvb_net.h>
46 #include <media/dvbdev.h>
47 #include <media/dmxdev.h>
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include <media/v4l2-device.h>
51 #include <media/v4l2-ctrls.h>
52 #include <media/v4l2-event.h>
53
54 #include "saa7164-reg.h"
55 #include "saa7164-types.h"
56
57 #define SAA7164_MAXBOARDS 8
58
59 #define UNSET (-1U)
60 #define SAA7164_BOARD_NOAUTO UNSET
61 #define SAA7164_BOARD_UNKNOWN 0
62 #define SAA7164_BOARD_UNKNOWN_REV2 1
63 #define SAA7164_BOARD_UNKNOWN_REV3 2
64 #define SAA7164_BOARD_HAUPPAUGE_HVR2250 3
65 #define SAA7164_BOARD_HAUPPAUGE_HVR2200 4
66 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5
67 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6
68 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7
69 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
70 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_4 9
71 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_5 10
72 #define SAA7164_BOARD_HAUPPAUGE_HVR2255proto 11
73 #define SAA7164_BOARD_HAUPPAUGE_HVR2255 12
74 #define SAA7164_BOARD_HAUPPAUGE_HVR2205 13
75
76 #define SAA7164_MAX_UNITS 8
77 #define SAA7164_TS_NUMBER_OF_LINES 312
78 #define SAA7164_PS_NUMBER_OF_LINES 256
79 #define SAA7164_PT_ENTRIES 16
80 #define SAA7164_MAX_ENCODER_BUFFERS 64
81 #define SAA7164_MAX_VBI_BUFFERS 64
82
83
84 #define SAA7164_PORT_TS1 (0)
85 #define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
86 #define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
87 #define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
88 #define SAA7164_PORT_VBI1 (SAA7164_PORT_ENC2 + 1)
89 #define SAA7164_PORT_VBI2 (SAA7164_PORT_VBI1 + 1)
90 #define SAA7164_MAX_PORTS (SAA7164_PORT_VBI2 + 1)
91
92 #define DBGLVL_FW 4
93 #define DBGLVL_DVB 8
94 #define DBGLVL_I2C 16
95 #define DBGLVL_API 32
96 #define DBGLVL_CMD 64
97 #define DBGLVL_BUS 128
98 #define DBGLVL_IRQ 256
99 #define DBGLVL_BUF 512
100 #define DBGLVL_ENC 1024
101 #define DBGLVL_VBI 2048
102 #define DBGLVL_THR 4096
103 #define DBGLVL_CPU 8192
104
105 #define SAA7164_NORMS \
106 (V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP)
107
108
109 #define SAA7164_TV_MIN_FREQ (44U * 16U)
110 #define SAA7164_TV_MAX_FREQ (958U * 16U)
111
112 enum port_t {
113 SAA7164_MPEG_UNDEFINED = 0,
114 SAA7164_MPEG_DVB,
115 SAA7164_MPEG_ENCODER,
116 SAA7164_MPEG_VBI,
117 };
118
119 enum saa7164_i2c_bus_nr {
120 SAA7164_I2C_BUS_0 = 0,
121 SAA7164_I2C_BUS_1,
122 SAA7164_I2C_BUS_2,
123 };
124
125 enum saa7164_buffer_flags {
126 SAA7164_BUFFER_UNDEFINED = 0,
127 SAA7164_BUFFER_FREE,
128 SAA7164_BUFFER_BUSY,
129 SAA7164_BUFFER_FULL
130 };
131
132 enum saa7164_unit_type {
133 SAA7164_UNIT_UNDEFINED = 0,
134 SAA7164_UNIT_DIGITAL_DEMODULATOR,
135 SAA7164_UNIT_ANALOG_DEMODULATOR,
136 SAA7164_UNIT_TUNER,
137 SAA7164_UNIT_EEPROM,
138 SAA7164_UNIT_ZILOG_IRBLASTER,
139 SAA7164_UNIT_ENCODER,
140 };
141
142
143
144
145
146
147
148
149
150 struct saa7164_unit {
151 enum saa7164_unit_type type;
152 u8 id;
153 char *name;
154 enum saa7164_i2c_bus_nr i2c_bus_nr;
155 u8 i2c_bus_addr;
156 u8 i2c_reg_len;
157 };
158
159 struct saa7164_board {
160 char *name;
161 enum port_t porta, portb, portc,
162 portd, porte, portf;
163 enum {
164 SAA7164_CHIP_UNDEFINED = 0,
165 SAA7164_CHIP_REV2,
166 SAA7164_CHIP_REV3,
167 } chiprev;
168 struct saa7164_unit unit[SAA7164_MAX_UNITS];
169 };
170
171 struct saa7164_subid {
172 u16 subvendor;
173 u16 subdevice;
174 u32 card;
175 };
176
177 struct saa7164_encoder_fh {
178 struct v4l2_fh fh;
179 struct saa7164_port *port;
180 atomic_t v4l_reading;
181 };
182
183 struct saa7164_vbi_fh {
184 struct v4l2_fh fh;
185 struct saa7164_port *port;
186 atomic_t v4l_reading;
187 };
188
189 struct saa7164_histogram_bucket {
190 u32 val;
191 u32 count;
192 u64 update_time;
193 };
194
195 struct saa7164_histogram {
196 char name[32];
197 struct saa7164_histogram_bucket counter1[64];
198 };
199
200 struct saa7164_user_buffer {
201 struct list_head list;
202
203
204 u8 *data;
205 u32 pos;
206 u32 actual_size;
207
208 u32 crc;
209 };
210
211 struct saa7164_fw_status {
212
213
214 u32 status;
215 u32 mode;
216 u32 spec;
217 u32 inst;
218 u32 cpuload;
219 u32 remainheap;
220
221
222 u32 version;
223 u32 major;
224 u32 sub;
225 u32 rel;
226 u32 buildnr;
227 };
228
229 struct saa7164_dvb {
230 struct mutex lock;
231 struct dvb_adapter adapter;
232 struct dvb_frontend *frontend;
233 struct dvb_demux demux;
234 struct dmxdev dmxdev;
235 struct dmx_frontend fe_hw;
236 struct dmx_frontend fe_mem;
237 struct dvb_net net;
238 int feeding;
239 };
240
241 struct saa7164_i2c {
242 struct saa7164_dev *dev;
243
244 enum saa7164_i2c_bus_nr nr;
245
246
247 struct i2c_adapter i2c_adap;
248 struct i2c_client i2c_client;
249 u32 i2c_rc;
250 };
251
252 struct saa7164_tvnorm {
253 char *name;
254 v4l2_std_id id;
255 };
256
257 struct saa7164_encoder_params {
258 struct saa7164_tvnorm encodernorm;
259 u32 height;
260 u32 width;
261 u32 is_50hz;
262 u32 bitrate;
263 u32 bitrate_peak;
264 u32 bitrate_mode;
265 u32 stream_type;
266
267 u32 audio_sampling_freq;
268 u32 ctl_mute;
269 u32 ctl_aspect;
270 u32 refdist;
271 u32 gop_size;
272 };
273
274 struct saa7164_vbi_params {
275 struct saa7164_tvnorm encodernorm;
276 u32 height;
277 u32 width;
278 u32 is_50hz;
279 u32 bitrate;
280 u32 bitrate_peak;
281 u32 bitrate_mode;
282 u32 stream_type;
283
284 u32 audio_sampling_freq;
285 u32 ctl_mute;
286 u32 ctl_aspect;
287 u32 refdist;
288 u32 gop_size;
289 };
290
291 struct saa7164_port;
292
293 struct saa7164_buffer {
294 struct list_head list;
295
296
297 int idx;
298
299 struct saa7164_port *port;
300
301
302
303 enum saa7164_buffer_flags flags;
304
305
306 u32 pci_size;
307 u64 *cpu;
308 dma_addr_t dma;
309 u32 crc;
310
311
312 u32 pt_size;
313 u64 *pt_cpu;
314 dma_addr_t pt_dma;
315
316
317 u32 pos;
318 u32 actual_size;
319 };
320
321 struct saa7164_port {
322
323 struct saa7164_dev *dev;
324 enum port_t type;
325 int nr;
326
327
328
329
330 struct tmHWStreamParameters hw_streamingparams;
331
332
333 struct tmComResDMATermDescrHeader hwcfg;
334
335
336 u32 bufcounter;
337 u32 pitch;
338 u32 bufsize;
339 u32 bufoffset;
340 u32 bufptr32l;
341 u32 bufptr32h;
342 u64 bufptr64;
343
344 u32 numpte;
345
346 struct mutex dmaqueue_lock;
347 struct saa7164_buffer dmaqueue;
348
349 u64 last_irq_msecs, last_svc_msecs;
350 u64 last_irq_msecs_diff, last_svc_msecs_diff;
351 u32 last_svc_wp;
352 u32 last_svc_rp;
353 u64 last_irq_svc_msecs_diff;
354 u64 last_read_msecs, last_read_msecs_diff;
355 u64 last_poll_msecs, last_poll_msecs_diff;
356
357 struct saa7164_histogram irq_interval;
358 struct saa7164_histogram svc_interval;
359 struct saa7164_histogram irq_svc_interval;
360 struct saa7164_histogram read_interval;
361 struct saa7164_histogram poll_interval;
362
363
364 struct saa7164_dvb dvb;
365 struct i2c_client *i2c_client_demod;
366 struct i2c_client *i2c_client_tuner;
367
368
369
370
371 struct saa7164_tvnorm encodernorm;
372 struct v4l2_ctrl_handler ctrl_handler;
373 v4l2_std_id std;
374 u32 height;
375 u32 width;
376 u32 freq;
377 u8 mux_input;
378 u8 encoder_profile;
379 u8 video_format;
380 u8 audio_format;
381 u8 video_resolution;
382 u16 ctl_brightness;
383 u16 ctl_contrast;
384 u16 ctl_hue;
385 u16 ctl_saturation;
386 u16 ctl_sharpness;
387 s8 ctl_volume;
388
389 struct tmComResAFeatureDescrHeader audfeat;
390 struct tmComResEncoderDescrHeader encunit;
391 struct tmComResProcDescrHeader vidproc;
392 struct tmComResExtDevDescrHeader ifunit;
393 struct tmComResTunerDescrHeader tunerunit;
394
395 struct work_struct workenc;
396
397
398 struct saa7164_encoder_params encoder_params;
399 struct video_device *v4l_device;
400 atomic_t v4l_reader_count;
401
402 struct saa7164_buffer list_buf_used;
403 struct saa7164_buffer list_buf_free;
404 wait_queue_head_t wait_read;
405
406
407 struct tmComResVBIFormatDescrHeader vbi_fmt_ntsc;
408 struct saa7164_vbi_params vbi_params;
409 struct saa7164_port *enc_port;
410
411
412 u32 sync_errors;
413 u32 v_cc_errors;
414 u32 a_cc_errors;
415 u8 last_v_cc;
416 u8 last_a_cc;
417 u32 done_first_interrupt;
418 };
419
420 struct saa7164_dev {
421 struct list_head devlist;
422 atomic_t refcount;
423
424 struct v4l2_device v4l2_dev;
425
426
427 struct pci_dev *pci;
428 unsigned char pci_rev, pci_lat;
429 int pci_bus, pci_slot;
430 u32 __iomem *lmmio;
431 u8 __iomem *bmmio;
432 u32 __iomem *lmmio2;
433 u8 __iomem *bmmio2;
434 int pci_irqmask;
435
436
437 int nr;
438 int hwrevision;
439 u32 board;
440 char name[16];
441
442
443 struct saa7164_fw_status fw_status;
444 u32 firmwareloaded;
445
446 struct tmComResHWDescr hwdesc;
447 struct tmComResInterfaceDescr intfdesc;
448 struct tmComResBusDescr busdesc;
449
450 struct tmComResBusInfo bus;
451
452
453 u32 int_status;
454 u32 int_ack;
455 bool msi;
456
457 struct cmd cmds[SAA_CMD_MAX_MSG_UNITS];
458 struct mutex lock;
459
460
461 struct saa7164_i2c i2c_bus[3];
462
463
464 struct saa7164_port ports[SAA7164_MAX_PORTS];
465
466
467 struct work_struct workcmd;
468
469
470
471
472 struct task_struct *kthread;
473
474 };
475
476 extern struct list_head saa7164_devlist;
477 extern unsigned int waitsecs;
478 extern unsigned int encoder_buffers;
479 extern unsigned int vbi_buffers;
480
481
482
483 void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr);
484 void saa7164_getfirmwarestatus(struct saa7164_dev *dev);
485 u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev);
486 void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val);
487
488
489
490 int saa7164_downloadfirmware(struct saa7164_dev *dev);
491
492
493
494 extern int saa7164_i2c_register(struct saa7164_i2c *bus);
495 extern int saa7164_i2c_unregister(struct saa7164_i2c *bus);
496 extern void saa7164_call_i2c_clients(struct saa7164_i2c *bus,
497 unsigned int cmd, void *arg);
498
499
500
501 int saa7164_bus_setup(struct saa7164_dev *dev);
502 void saa7164_bus_dump(struct saa7164_dev *dev);
503 int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
504 void *buf);
505 int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
506 void *buf, int peekonly);
507
508
509
510 int saa7164_cmd_send(struct saa7164_dev *dev,
511 u8 id, enum tmComResCmd command, u16 controlselector,
512 u16 size, void *buf);
513 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno);
514 int saa7164_irq_dequeue(struct saa7164_dev *dev);
515
516
517
518 int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version);
519 int saa7164_api_enum_subdevs(struct saa7164_dev *dev);
520 int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
521 u32 datalen, u8 *data);
522 int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr,
523 u32 datalen, u8 *data);
524 int saa7164_api_dif_write(struct saa7164_i2c *bus, u8 addr,
525 u32 datalen, u8 *data);
526 int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen);
527 int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
528 int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
529 int saa7164_api_transition_port(struct saa7164_port *port, u8 mode);
530 int saa7164_api_initialize_dif(struct saa7164_port *port);
531 int saa7164_api_configure_dif(struct saa7164_port *port, u32 std);
532 int saa7164_api_set_encoder(struct saa7164_port *port);
533 int saa7164_api_get_encoder(struct saa7164_port *port);
534 int saa7164_api_set_aspect_ratio(struct saa7164_port *port);
535 int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl);
536 int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl);
537 int saa7164_api_set_videomux(struct saa7164_port *port);
538 int saa7164_api_audio_mute(struct saa7164_port *port, int mute);
539 int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level);
540 int saa7164_api_set_audio_std(struct saa7164_port *port);
541 int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect);
542 int saa7164_api_get_videomux(struct saa7164_port *port);
543 int saa7164_api_set_vbi_format(struct saa7164_port *port);
544 int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level);
545 int saa7164_api_collect_debug(struct saa7164_dev *dev);
546 int saa7164_api_get_load_info(struct saa7164_dev *dev,
547 struct tmFwInfoStruct *i);
548
549
550
551 extern struct saa7164_board saa7164_boards[];
552 extern const unsigned int saa7164_bcount;
553
554 extern struct saa7164_subid saa7164_subids[];
555 extern const unsigned int saa7164_idcount;
556
557 extern void saa7164_card_list(struct saa7164_dev *dev);
558 extern void saa7164_gpio_setup(struct saa7164_dev *dev);
559 extern void saa7164_card_setup(struct saa7164_dev *dev);
560
561 extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr);
562 extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr);
563 extern char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid);
564
565
566
567 extern int saa7164_dvb_register(struct saa7164_port *port);
568 extern int saa7164_dvb_unregister(struct saa7164_port *port);
569
570
571
572 extern struct saa7164_buffer *saa7164_buffer_alloc(
573 struct saa7164_port *port, u32 len);
574 extern int saa7164_buffer_dealloc(struct saa7164_buffer *buf);
575 extern void saa7164_buffer_display(struct saa7164_buffer *buf);
576 extern int saa7164_buffer_activate(struct saa7164_buffer *buf, int i);
577 extern int saa7164_buffer_cfg_port(struct saa7164_port *port);
578 extern struct saa7164_user_buffer *saa7164_buffer_alloc_user(
579 struct saa7164_dev *dev, u32 len);
580 extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf);
581 extern int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i);
582
583
584
585 int saa7164_s_std(struct saa7164_port *port, v4l2_std_id id);
586 int saa7164_g_std(struct saa7164_port *port, v4l2_std_id *id);
587 int saa7164_enum_input(struct file *file, void *priv, struct v4l2_input *i);
588 int saa7164_g_input(struct saa7164_port *port, unsigned int *i);
589 int saa7164_s_input(struct saa7164_port *port, unsigned int i);
590 int saa7164_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t);
591 int saa7164_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t);
592 int saa7164_g_frequency(struct saa7164_port *port, struct v4l2_frequency *f);
593 int saa7164_s_frequency(struct saa7164_port *port,
594 const struct v4l2_frequency *f);
595 int saa7164_encoder_register(struct saa7164_port *port);
596 void saa7164_encoder_unregister(struct saa7164_port *port);
597
598
599
600 int saa7164_vbi_register(struct saa7164_port *port);
601 void saa7164_vbi_unregister(struct saa7164_port *port);
602
603
604
605 extern unsigned int crc_checking;
606
607 extern unsigned int saa_debug;
608 #define dprintk(level, fmt, arg...)\
609 do { if (saa_debug & level)\
610 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
611 } while (0)
612
613 #define log_warn(fmt, arg...)\
614 do { \
615 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
616 } while (0)
617
618 #define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
619 #define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
620
621 #define saa7164_readb(reg) readl(dev->bmmio + (reg))
622 #define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))
623