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