]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/em28xx/em28xx.h
V4L/DVB (6952): Add code for autoloading em28xx-alsa, if needed
[linux-2.6-omap-h63xx.git] / drivers / media / video / em28xx / em28xx.h
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/videodev2.h>
29 #include <linux/i2c.h>
30 #include <linux/mutex.h>
31 #include <media/ir-kbd-i2c.h>
32
33 /* Boards supported by driver */
34
35 #define EM2800_BOARD_UNKNOWN                    0
36 #define EM2820_BOARD_UNKNOWN                    1
37 #define EM2820_BOARD_TERRATEC_CINERGY_250       2
38 #define EM2820_BOARD_PINNACLE_USB_2             3
39 #define EM2820_BOARD_HAUPPAUGE_WINTV_USB_2      4
40 #define EM2820_BOARD_MSI_VOX_USB_2              5
41 #define EM2800_BOARD_TERRATEC_CINERGY_200       6
42 #define EM2800_BOARD_LEADTEK_WINFAST_USBII      7
43 #define EM2800_BOARD_KWORLD_USB2800             8
44 #define EM2820_BOARD_PINNACLE_DVC_90            9
45 #define EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900    10
46 #define EM2880_BOARD_TERRATEC_HYBRID_XS         11
47 #define EM2820_BOARD_KWORLD_PVRTV2800RF         12
48 #define EM2880_BOARD_TERRATEC_PRODIGY_XS        13
49 #define EM2820_BOARD_PROLINK_PLAYTV_USB2        14
50 #define EM2800_BOARD_VGEAR_POCKETTV             15
51 #define EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950    16
52
53 #define UNSET -1
54
55 /* maximum number of em28xx boards */
56 #define EM28XX_MAXBOARDS 1 /*FIXME: should be bigger */
57
58 /* maximum number of frames that can be queued */
59 #define EM28XX_NUM_FRAMES 5
60 /* number of frames that get used for v4l2_read() */
61 #define EM28XX_NUM_READ_FRAMES 2
62
63 /* number of buffers for isoc transfers */
64 #define EM28XX_NUM_BUFS 5
65
66 /* number of packets for each buffer
67    windows requests only 40 packets .. so we better do the same
68    this is what I found out for all alternate numbers there!
69  */
70 #define EM28XX_NUM_PACKETS 40
71
72 /* default alternate; 0 means choose the best */
73 #define EM28XX_PINOUT 0
74
75 #define EM28XX_INTERLACED_DEFAULT 1
76
77 /*
78 #define (use usbview if you want to get the other alternate number infos)
79 #define
80 #define alternate number 2
81 #define                         Endpoint Address: 82
82                         Direction: in
83                         Attribute: 1
84                         Type: Isoc
85                         Max Packet Size: 1448
86                         Interval: 125us
87
88   alternate number 7
89
90                         Endpoint Address: 82
91                         Direction: in
92                         Attribute: 1
93                         Type: Isoc
94                         Max Packet Size: 3072
95                         Interval: 125us
96 */
97
98 /* time to wait when stopping the isoc transfer */
99 #define EM28XX_URB_TIMEOUT       msecs_to_jiffies(EM28XX_NUM_BUFS * EM28XX_NUM_PACKETS)
100
101 /* time in msecs to wait for i2c writes to finish */
102 #define EM2800_I2C_WRITE_TIMEOUT 20
103
104 /* the various frame states */
105 enum em28xx_frame_state {
106         F_UNUSED = 0,
107         F_QUEUED,
108         F_GRABBING,
109         F_DONE,
110         F_ERROR,
111 };
112
113 /* stream states */
114 enum em28xx_stream_state {
115         STREAM_OFF,
116         STREAM_INTERRUPT,
117         STREAM_ON,
118 };
119
120 /* frames */
121 struct em28xx_frame_t {
122         void *bufmem;
123         struct v4l2_buffer buf;
124         enum em28xx_frame_state state;
125         struct list_head frame;
126         unsigned long vma_use_count;
127         int top_field;
128         int fieldbytesused;
129 };
130
131 /* io methods */
132 enum em28xx_io_method {
133         IO_NONE,
134         IO_READ,
135         IO_MMAP,
136 };
137
138 /* inputs */
139
140 #define MAX_EM28XX_INPUT 4
141 enum enum28xx_itype {
142         EM28XX_VMUX_COMPOSITE1 = 1,
143         EM28XX_VMUX_COMPOSITE2,
144         EM28XX_VMUX_COMPOSITE3,
145         EM28XX_VMUX_COMPOSITE4,
146         EM28XX_VMUX_SVIDEO,
147         EM28XX_VMUX_TELEVISION,
148         EM28XX_VMUX_CABLE,
149         EM28XX_VMUX_DVB,
150         EM28XX_VMUX_DEBUG,
151         EM28XX_RADIO,
152 };
153
154 enum em28xx_amux {
155         EM28XX_AMUX_VIDEO,
156         EM28XX_AMUX_LINE_IN,
157         EM28XX_AMUX_AC97_VIDEO,
158         EM28XX_AMUX_AC97_LINE_IN,
159 };
160
161 struct em28xx_input {
162         enum enum28xx_itype type;
163         unsigned int vmux;
164         enum em28xx_amux amux;
165 };
166
167 #define INPUT(nr) (&em28xx_boards[dev->model].input[nr])
168
169 enum em28xx_decoder {
170         EM28XX_TVP5150,
171         EM28XX_SAA7113,
172         EM28XX_SAA7114
173 };
174
175 struct em28xx_board {
176         char *name;
177         int vchannels;
178         int tuner_type;
179
180         /* i2c flags */
181         unsigned int tda9887_conf;
182
183         unsigned int is_em2800:1;
184         unsigned int has_tuner:1;
185         unsigned int has_msp34xx:1;
186         unsigned int mts_firmware:1;
187         unsigned int analog_gpio;
188
189         enum em28xx_decoder decoder;
190
191         struct em28xx_input       input[MAX_EM28XX_INPUT];
192 };
193
194 struct em28xx_eeprom {
195         u32 id;                 /* 0x9567eb1a */
196         u16 vendor_ID;
197         u16 product_ID;
198
199         u16 chip_conf;
200
201         u16 board_conf;
202
203         u16 string1, string2, string3;
204
205         u8 string_idx_table;
206 };
207
208 /* device states */
209 enum em28xx_dev_state {
210         DEV_INITIALIZED = 0x01,
211         DEV_DISCONNECTED = 0x02,
212         DEV_MISCONFIGURED = 0x04,
213 };
214
215 #define EM28XX_AUDIO_BUFS 5
216 #define EM28XX_NUM_AUDIO_PACKETS 64
217 #define EM28XX_AUDIO_MAX_PACKET_SIZE 196 /* static value */
218 #define EM28XX_CAPTURE_STREAM_EN 1
219 #define EM28XX_AUDIO   0x10
220
221 struct em28xx_audio {
222         char name[50];
223         char *transfer_buffer[EM28XX_AUDIO_BUFS];
224         struct urb *urb[EM28XX_AUDIO_BUFS];
225         struct usb_device *udev;
226         unsigned int capture_transfer_done;
227         struct snd_pcm_substream   *capture_pcm_substream;
228
229         unsigned int hwptr_done_capture;
230         struct snd_card            *sndcard;
231
232         int users, shutdown;
233         enum em28xx_stream_state capture_stream;
234         spinlock_t slock;
235 };
236
237 /* main device struct */
238 struct em28xx {
239         /* generic device properties */
240         char name[30];          /* name (including minor) of the device */
241         int model;              /* index in the device_data struct */
242         int devno;              /* marks the number of this device */
243         unsigned int analog_gpio;
244         unsigned int is_em2800:1;
245         unsigned int has_tuner:1;
246         unsigned int has_msp34xx:1;
247         unsigned int has_tda9887:1;
248         unsigned int stream_on:1;       /* Locks streams */
249         unsigned int has_audio_class:1;
250
251         int video_inputs;       /* number of video inputs */
252         struct list_head        devlist;
253
254         u32 i2s_speed;          /* I2S speed for audio digital stream */
255
256         enum em28xx_decoder decoder;
257
258         int tuner_type;         /* type of the tuner */
259         int tuner_addr;         /* tuner address */
260         int tda9887_conf;
261         /* i2c i/o */
262         struct i2c_adapter i2c_adap;
263         struct i2c_client i2c_client;
264         /* video for linux */
265         int users;              /* user count for exclusive use */
266         struct video_device *vdev;      /* video for linux device struct */
267         v4l2_std_id norm;       /* selected tv norm */
268         int ctl_freq;           /* selected frequency */
269         unsigned int ctl_input; /* selected input */
270         unsigned int ctl_ainput;        /* slected audio input */
271         int mute;
272         int volume;
273         /* frame properties */
274         struct em28xx_frame_t frame[EM28XX_NUM_FRAMES]; /* list of frames */
275         int num_frames;         /* number of frames currently in use */
276         unsigned int frame_count;       /* total number of transfered frames */
277         struct em28xx_frame_t *frame_current;   /* the frame that is being filled */
278         int width;              /* current frame width */
279         int height;             /* current frame height */
280         int frame_size;         /* current frame size */
281         int field_size;         /* current field size */
282         int bytesperline;
283         int hscale;             /* horizontal scale factor (see datasheet) */
284         int vscale;             /* vertical scale factor (see datasheet) */
285         int interlaced;         /* 1=interlace fileds, 0=just top fileds */
286         int type;
287         unsigned int video_bytesread;   /* Number of bytes read */
288
289         unsigned long hash;     /* eeprom hash - for boards with generic ID */
290         unsigned long i2c_hash; /* i2c devicelist hash - for boards with generic ID */
291
292         struct em28xx_audio *adev;
293
294         /* states */
295         enum em28xx_dev_state state;
296         enum em28xx_stream_state stream;
297         enum em28xx_io_method io;
298
299         struct work_struct         request_module_wk;
300
301         /* locks */
302         struct mutex lock;
303         spinlock_t queue_lock;
304         struct list_head inqueue, outqueue;
305         wait_queue_head_t open, wait_frame, wait_stream;
306         struct video_device *vbi_dev;
307
308         unsigned char eedata[256];
309
310         /* usb transfer */
311         struct usb_device *udev;        /* the usb device */
312         int alt;                /* alternate */
313         int max_pkt_size;       /* max packet size of isoc transaction */
314         int num_alt;            /* Number of alternative settings */
315         unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
316         struct urb *urb[EM28XX_NUM_BUFS];       /* urb for isoc transfers */
317         char *transfer_buffer[EM28XX_NUM_BUFS]; /* transfer buffers for isoc transfer */
318         /* helper funcs that call usb_control_msg */
319         int (*em28xx_write_regs) (struct em28xx * dev, u16 reg, char *buf,
320                                   int len);
321         int (*em28xx_read_reg) (struct em28xx * dev, u16 reg);
322         int (*em28xx_read_reg_req_len) (struct em28xx * dev, u8 req, u16 reg,
323                                         char *buf, int len);
324         int (*em28xx_write_regs_req) (struct em28xx * dev, u8 req, u16 reg,
325                                       char *buf, int len);
326         int (*em28xx_read_reg_req) (struct em28xx * dev, u8 req, u16 reg);
327 };
328
329 struct em28xx_fh {
330         struct em28xx *dev;
331         unsigned int  stream_on:1;      /* Locks streams */
332         int           radio;
333 };
334
335 struct em28xx_ops {
336         struct list_head next;
337         char *name;
338         int id;
339         int (*init)(struct em28xx *);
340         int (*fini)(struct em28xx *);
341 };
342
343 /* Provided by em28xx-i2c.c */
344
345 void em28xx_i2c_call_clients(struct em28xx *dev, unsigned int cmd, void *arg);
346 void em28xx_do_i2c_scan(struct em28xx *dev);
347 int em28xx_i2c_register(struct em28xx *dev);
348 int em28xx_i2c_unregister(struct em28xx *dev);
349
350 /* Provided by em28xx-input.c */
351
352 void em28xx_set_ir(struct em28xx * dev,struct IR_i2c *ir);
353
354 /* Provided by em28xx-core.c */
355
356 u32 em28xx_request_buffers(struct em28xx *dev, u32 count);
357 void em28xx_queue_unusedframes(struct em28xx *dev);
358 void em28xx_release_buffers(struct em28xx *dev);
359
360 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
361                             char *buf, int len);
362 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg);
363 int em28xx_read_reg(struct em28xx *dev, u16 reg);
364 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
365                           int len);
366 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len);
367 int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
368                           u8 bitmask);
369 int em28xx_set_audio_source(struct em28xx *dev);
370 int em28xx_audio_analog_set(struct em28xx *dev);
371
372 int em28xx_colorlevels_set_default(struct em28xx *dev);
373 int em28xx_capture_start(struct em28xx *dev, int start);
374 int em28xx_outfmt_set_yuv422(struct em28xx *dev);
375 int em28xx_resolution_set(struct em28xx *dev);
376 int em28xx_init_isoc(struct em28xx *dev);
377 void em28xx_uninit_isoc(struct em28xx *dev);
378 int em28xx_set_alternate(struct em28xx *dev);
379
380 /* Provided by em28xx-video.c */
381 int em28xx_register_extension(struct em28xx_ops *dev);
382 void em28xx_unregister_extension(struct em28xx_ops *dev);
383
384 /* Provided by em28xx-cards.c */
385 extern int em2800_variant_detect(struct usb_device* udev,int model);
386 extern void em28xx_pre_card_setup(struct em28xx *dev);
387 extern void em28xx_card_setup(struct em28xx *dev);
388 extern struct em28xx_board em28xx_boards[];
389 extern struct usb_device_id em28xx_id_table[];
390 extern const unsigned int em28xx_bcount;
391
392 /* em2800 registers */
393 #define EM2800_AUDIOSRC_REG 0x08
394
395 /* em28xx registers */
396 #define I2C_CLK_REG     0x06
397 #define CHIPID_REG      0x0a
398 #define USBSUSP_REG     0x0c    /* */
399
400 #define AUDIOSRC_REG    0x0e
401 #define XCLK_REG        0x0f
402
403 #define VINMODE_REG     0x10
404 #define VINCTRL_REG     0x11
405 #define VINENABLE_REG   0x12    /* */
406
407 #define GAMMA_REG       0x14
408 #define RGAIN_REG       0x15
409 #define GGAIN_REG       0x16
410 #define BGAIN_REG       0x17
411 #define ROFFSET_REG     0x18
412 #define GOFFSET_REG     0x19
413 #define BOFFSET_REG     0x1a
414
415 #define OFLOW_REG       0x1b
416 #define HSTART_REG      0x1c
417 #define VSTART_REG      0x1d
418 #define CWIDTH_REG      0x1e
419 #define CHEIGHT_REG     0x1f
420
421 #define YGAIN_REG       0x20
422 #define YOFFSET_REG     0x21
423 #define UVGAIN_REG      0x22
424 #define UOFFSET_REG     0x23
425 #define VOFFSET_REG     0x24
426 #define SHARPNESS_REG   0x25
427
428 #define COMPR_REG       0x26
429 #define OUTFMT_REG      0x27
430
431 #define XMIN_REG        0x28
432 #define XMAX_REG        0x29
433 #define YMIN_REG        0x2a
434 #define YMAX_REG        0x2b
435
436 #define HSCALELOW_REG   0x30
437 #define HSCALEHIGH_REG  0x31
438 #define VSCALELOW_REG   0x32
439 #define VSCALEHIGH_REG  0x33
440
441 #define AC97LSB_REG     0x40
442 #define AC97MSB_REG     0x41
443 #define AC97ADDR_REG    0x42
444 #define AC97BUSY_REG    0x43
445
446 /* em202 registers */
447 #define MASTER_AC97     0x02
448 #define LINE_IN_AC97    0x10
449 #define VIDEO_AC97      0x14
450
451 /* register settings */
452 #define EM2800_AUDIO_SRC_TUNER  0x0d
453 #define EM2800_AUDIO_SRC_LINE   0x0c
454 #define EM28XX_AUDIO_SRC_TUNER  0xc0
455 #define EM28XX_AUDIO_SRC_LINE   0x80
456
457 /* printk macros */
458
459 #define em28xx_err(fmt, arg...) do {\
460         printk(KERN_ERR fmt , ##arg); } while (0)
461
462 #define em28xx_errdev(fmt, arg...) do {\
463         printk(KERN_ERR "%s: "fmt,\
464                         dev->name , ##arg); } while (0)
465
466 #define em28xx_info(fmt, arg...) do {\
467         printk(KERN_INFO "%s: "fmt,\
468                         dev->name , ##arg); } while (0)
469 #define em28xx_warn(fmt, arg...) do {\
470         printk(KERN_WARNING "%s: "fmt,\
471                         dev->name , ##arg); } while (0)
472
473 inline static int em28xx_compression_disable(struct em28xx *dev)
474 {
475         /* side effect of disabling scaler and mixer */
476         return em28xx_write_regs(dev, COMPR_REG, "\x00", 1);
477 }
478
479 inline static int em28xx_contrast_get(struct em28xx *dev)
480 {
481         return em28xx_read_reg(dev, YGAIN_REG) & 0x1f;
482 }
483
484 inline static int em28xx_brightness_get(struct em28xx *dev)
485 {
486         return em28xx_read_reg(dev, YOFFSET_REG);
487 }
488
489 inline static int em28xx_saturation_get(struct em28xx *dev)
490 {
491         return em28xx_read_reg(dev, UVGAIN_REG) & 0x1f;
492 }
493
494 inline static int em28xx_u_balance_get(struct em28xx *dev)
495 {
496         return em28xx_read_reg(dev, UOFFSET_REG);
497 }
498
499 inline static int em28xx_v_balance_get(struct em28xx *dev)
500 {
501         return em28xx_read_reg(dev, VOFFSET_REG);
502 }
503
504 inline static int em28xx_gamma_get(struct em28xx *dev)
505 {
506         return em28xx_read_reg(dev, GAMMA_REG) & 0x3f;
507 }
508
509 inline static int em28xx_contrast_set(struct em28xx *dev, s32 val)
510 {
511         u8 tmp = (u8) val;
512         return em28xx_write_regs(dev, YGAIN_REG, &tmp, 1);
513 }
514
515 inline static int em28xx_brightness_set(struct em28xx *dev, s32 val)
516 {
517         u8 tmp = (u8) val;
518         return em28xx_write_regs(dev, YOFFSET_REG, &tmp, 1);
519 }
520
521 inline static int em28xx_saturation_set(struct em28xx *dev, s32 val)
522 {
523         u8 tmp = (u8) val;
524         return em28xx_write_regs(dev, UVGAIN_REG, &tmp, 1);
525 }
526
527 inline static int em28xx_u_balance_set(struct em28xx *dev, s32 val)
528 {
529         u8 tmp = (u8) val;
530         return em28xx_write_regs(dev, UOFFSET_REG, &tmp, 1);
531 }
532
533 inline static int em28xx_v_balance_set(struct em28xx *dev, s32 val)
534 {
535         u8 tmp = (u8) val;
536         return em28xx_write_regs(dev, VOFFSET_REG, &tmp, 1);
537 }
538
539 inline static int em28xx_gamma_set(struct em28xx *dev, s32 val)
540 {
541         u8 tmp = (u8) val;
542         return em28xx_write_regs(dev, GAMMA_REG, &tmp, 1);
543 }
544
545 /*FIXME: maxw should be dependent of alt mode */
546 inline static unsigned int norm_maxw(struct em28xx *dev)
547 {
548         switch (dev->model) {
549         case EM2820_BOARD_MSI_VOX_USB_2:
550                 return 640;
551         default:
552                 return 720;
553         }
554 }
555
556 inline static unsigned int norm_maxh(struct em28xx *dev)
557 {
558         switch (dev->model) {
559         case EM2820_BOARD_MSI_VOX_USB_2:
560                 return 480;
561         default:
562                 return (dev->norm & V4L2_STD_625_50) ? 576 : 480;
563         }
564 }
565 #endif