em28xx.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. em28xx.h - driver for Empia EM2800/EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
  4. Ludovico Cavedon <cavedon@sssup.it>
  5. Mauro Carvalho Chehab <mchehab@infradead.org>
  6. Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
  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. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #ifndef _EM28XX_H
  20. #define _EM28XX_H
  21. #include <linux/videodev.h>
  22. #include <linux/i2c.h>
  23. #include <linux/mutex.h>
  24. #include <media/ir-kbd-i2c.h>
  25. /* Boards supported by driver */
  26. #define EM2800_BOARD_UNKNOWN 0
  27. #define EM2820_BOARD_UNKNOWN 1
  28. #define EM2820_BOARD_TERRATEC_CINERGY_250 2
  29. #define EM2820_BOARD_PINNACLE_USB_2 3
  30. #define EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 4
  31. #define EM2820_BOARD_MSI_VOX_USB_2 5
  32. #define EM2800_BOARD_TERRATEC_CINERGY_200 6
  33. #define EM2800_BOARD_LEADTEK_WINFAST_USBII 7
  34. #define EM2800_BOARD_KWORLD_USB2800 8
  35. #define EM2820_BOARD_PINNACLE_DVC_90 9
  36. #define EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900 10
  37. #define EM2880_BOARD_TERRATEC_HYBRID_XS 11
  38. #define EM2820_BOARD_KWORLD_PVRTV2800RF 12
  39. #define EM2880_BOARD_TERRATEC_PRODIGY_XS 13
  40. #define UNSET -1
  41. /* maximum number of em28xx boards */
  42. #define EM28XX_MAXBOARDS 1 /*FIXME: should be bigger */
  43. /* maximum number of frames that can be queued */
  44. #define EM28XX_NUM_FRAMES 5
  45. /* number of frames that get used for v4l2_read() */
  46. #define EM28XX_NUM_READ_FRAMES 2
  47. /* number of buffers for isoc transfers */
  48. #define EM28XX_NUM_BUFS 5
  49. /* number of packets for each buffer
  50. windows requests only 40 packets .. so we better do the same
  51. this is what I found out for all alternate numbers there!
  52. */
  53. #define EM28XX_NUM_PACKETS 40
  54. /* default alternate; 0 means choose the best */
  55. #define EM28XX_PINOUT 0
  56. #define EM28XX_INTERLACED_DEFAULT 1
  57. /*
  58. #define (use usbview if you want to get the other alternate number infos)
  59. #define
  60. #define alternate number 2
  61. #define Endpoint Address: 82
  62. Direction: in
  63. Attribute: 1
  64. Type: Isoc
  65. Max Packet Size: 1448
  66. Interval: 125us
  67. alternate number 7
  68. Endpoint Address: 82
  69. Direction: in
  70. Attribute: 1
  71. Type: Isoc
  72. Max Packet Size: 3072
  73. Interval: 125us
  74. */
  75. /* time to wait when stopping the isoc transfer */
  76. #define EM28XX_URB_TIMEOUT msecs_to_jiffies(EM28XX_NUM_BUFS * EM28XX_NUM_PACKETS)
  77. /* time in msecs to wait for i2c writes to finish */
  78. #define EM2800_I2C_WRITE_TIMEOUT 20
  79. /* the various frame states */
  80. enum em28xx_frame_state {
  81. F_UNUSED = 0,
  82. F_QUEUED,
  83. F_GRABBING,
  84. F_DONE,
  85. F_ERROR,
  86. };
  87. /* stream states */
  88. enum em28xx_stream_state {
  89. STREAM_OFF,
  90. STREAM_INTERRUPT,
  91. STREAM_ON,
  92. };
  93. /* frames */
  94. struct em28xx_frame_t {
  95. void *bufmem;
  96. struct v4l2_buffer buf;
  97. enum em28xx_frame_state state;
  98. struct list_head frame;
  99. unsigned long vma_use_count;
  100. int top_field;
  101. int fieldbytesused;
  102. };
  103. /* io methods */
  104. enum em28xx_io_method {
  105. IO_NONE,
  106. IO_READ,
  107. IO_MMAP,
  108. };
  109. /* inputs */
  110. #define MAX_EM28XX_INPUT 4
  111. enum enum28xx_itype {
  112. EM28XX_VMUX_COMPOSITE1 = 1,
  113. EM28XX_VMUX_COMPOSITE2,
  114. EM28XX_VMUX_COMPOSITE3,
  115. EM28XX_VMUX_COMPOSITE4,
  116. EM28XX_VMUX_SVIDEO,
  117. EM28XX_VMUX_TELEVISION,
  118. EM28XX_VMUX_CABLE,
  119. EM28XX_VMUX_DVB,
  120. EM28XX_VMUX_DEBUG,
  121. EM28XX_RADIO,
  122. };
  123. struct em28xx_input {
  124. enum enum28xx_itype type;
  125. unsigned int vmux;
  126. unsigned int amux;
  127. };
  128. #define INPUT(nr) (&em28xx_boards[dev->model].input[nr])
  129. enum em28xx_decoder {
  130. EM28XX_TVP5150,
  131. EM28XX_SAA7113,
  132. EM28XX_SAA7114
  133. };
  134. struct em28xx_board {
  135. char *name;
  136. int vchannels;
  137. int norm;
  138. int tuner_type;
  139. /* i2c flags */
  140. unsigned int is_em2800;
  141. unsigned int tda9887_conf;
  142. unsigned int has_tuner:1;
  143. unsigned int has_msp34xx:1;
  144. enum em28xx_decoder decoder;
  145. struct em28xx_input input[MAX_EM28XX_INPUT];
  146. };
  147. struct em28xx_eeprom {
  148. u32 id; /* 0x9567eb1a */
  149. u16 vendor_ID;
  150. u16 product_ID;
  151. u16 chip_conf;
  152. u16 board_conf;
  153. u16 string1, string2, string3;
  154. u8 string_idx_table;
  155. };
  156. /* device states */
  157. enum em28xx_dev_state {
  158. DEV_INITIALIZED = 0x01,
  159. DEV_DISCONNECTED = 0x02,
  160. DEV_MISCONFIGURED = 0x04,
  161. };
  162. /* tvnorms */
  163. struct em28xx_tvnorm {
  164. char *name;
  165. v4l2_std_id id;
  166. /* mode for saa7113h */
  167. int mode;
  168. };
  169. /* main device struct */
  170. struct em28xx {
  171. /* generic device properties */
  172. char name[30]; /* name (including minor) of the device */
  173. int model; /* index in the device_data struct */
  174. int devno; /* marks the number of this device */
  175. unsigned int is_em2800;
  176. int video_inputs; /* number of video inputs */
  177. struct list_head devlist;
  178. unsigned int has_tuner:1;
  179. unsigned int has_msp34xx:1;
  180. unsigned int has_tda9887:1;
  181. u32 i2s_speed; /* I2S speed for audio digital stream */
  182. enum em28xx_decoder decoder;
  183. int tuner_type; /* type of the tuner */
  184. int tuner_addr; /* tuner address */
  185. int tda9887_conf;
  186. /* i2c i/o */
  187. struct i2c_adapter i2c_adap;
  188. struct i2c_client i2c_client;
  189. /* video for linux */
  190. int users; /* user count for exclusive use */
  191. struct video_device *vdev; /* video for linux device struct */
  192. struct video_picture vpic; /* picture settings only used to init saa7113h */
  193. struct em28xx_tvnorm *tvnorm; /* selected tv norm */
  194. int ctl_freq; /* selected frequency */
  195. unsigned int ctl_input; /* selected input */
  196. unsigned int ctl_ainput; /* slected audio input */
  197. int mute;
  198. int volume;
  199. /* frame properties */
  200. struct em28xx_frame_t frame[EM28XX_NUM_FRAMES]; /* list of frames */
  201. int num_frames; /* number of frames currently in use */
  202. unsigned int frame_count; /* total number of transfered frames */
  203. struct em28xx_frame_t *frame_current; /* the frame that is being filled */
  204. int width; /* current frame width */
  205. int height; /* current frame height */
  206. int frame_size; /* current frame size */
  207. int field_size; /* current field size */
  208. int bytesperline;
  209. int hscale; /* horizontal scale factor (see datasheet) */
  210. int vscale; /* vertical scale factor (see datasheet) */
  211. int interlaced; /* 1=interlace fileds, 0=just top fileds */
  212. int type;
  213. /* states */
  214. enum em28xx_dev_state state;
  215. enum em28xx_stream_state stream;
  216. enum em28xx_io_method io;
  217. /* locks */
  218. struct mutex lock, fileop_lock;
  219. spinlock_t queue_lock;
  220. struct list_head inqueue, outqueue;
  221. wait_queue_head_t open, wait_frame, wait_stream;
  222. struct video_device *vbi_dev;
  223. unsigned char eedata[256];
  224. /* usb transfer */
  225. struct usb_device *udev; /* the usb device */
  226. int alt; /* alternate */
  227. int max_pkt_size; /* max packet size of isoc transaction */
  228. int num_alt; /* Number of alternative settings */
  229. unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
  230. struct urb *urb[EM28XX_NUM_BUFS]; /* urb for isoc transfers */
  231. char *transfer_buffer[EM28XX_NUM_BUFS]; /* transfer buffers for isoc transfer */
  232. /* helper funcs that call usb_control_msg */
  233. int (*em28xx_write_regs) (struct em28xx * dev, u16 reg, char *buf,
  234. int len);
  235. int (*em28xx_read_reg) (struct em28xx * dev, u16 reg);
  236. int (*em28xx_read_reg_req_len) (struct em28xx * dev, u8 req, u16 reg,
  237. char *buf, int len);
  238. int (*em28xx_write_regs_req) (struct em28xx * dev, u8 req, u16 reg,
  239. char *buf, int len);
  240. int (*em28xx_read_reg_req) (struct em28xx * dev, u8 req, u16 reg);
  241. };
  242. /* Provided by em28xx-i2c.c */
  243. void em28xx_i2c_call_clients(struct em28xx *dev, unsigned int cmd, void *arg);
  244. int em28xx_i2c_register(struct em28xx *dev);
  245. int em28xx_i2c_unregister(struct em28xx *dev);
  246. /* Provided by em28xx-input.c */
  247. void em28xx_set_ir(struct em28xx * dev,struct IR_i2c *ir);
  248. /* Provided by em28xx-core.c */
  249. u32 em28xx_request_buffers(struct em28xx *dev, u32 count);
  250. void em28xx_queue_unusedframes(struct em28xx *dev);
  251. void em28xx_release_buffers(struct em28xx *dev);
  252. int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
  253. char *buf, int len);
  254. int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg);
  255. int em28xx_read_reg(struct em28xx *dev, u16 reg);
  256. int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
  257. int len);
  258. int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len);
  259. int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
  260. u8 bitmask);
  261. int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val);
  262. int em28xx_audio_analog_set(struct em28xx *dev);
  263. int em28xx_colorlevels_set_default(struct em28xx *dev);
  264. int em28xx_capture_start(struct em28xx *dev, int start);
  265. int em28xx_outfmt_set_yuv422(struct em28xx *dev);
  266. int em28xx_resolution_set(struct em28xx *dev);
  267. int em28xx_init_isoc(struct em28xx *dev);
  268. void em28xx_uninit_isoc(struct em28xx *dev);
  269. int em28xx_set_alternate(struct em28xx *dev);
  270. /* Provided by em28xx-cards.c */
  271. extern int em2800_variant_detect(struct usb_device* udev,int model);
  272. extern void em28xx_pre_card_setup(struct em28xx *dev);
  273. extern void em28xx_card_setup(struct em28xx *dev);
  274. extern struct em28xx_board em28xx_boards[];
  275. extern struct usb_device_id em28xx_id_table[];
  276. extern const unsigned int em28xx_bcount;
  277. /* em28xx registers */
  278. #define CHIPID_REG 0x0a
  279. #define USBSUSP_REG 0x0c /* */
  280. #define AUDIOSRC_REG 0x0e
  281. #define XCLK_REG 0x0f
  282. #define VINMODE_REG 0x10
  283. #define VINCTRL_REG 0x11
  284. #define VINENABLE_REG 0x12 /* */
  285. #define GAMMA_REG 0x14
  286. #define RGAIN_REG 0x15
  287. #define GGAIN_REG 0x16
  288. #define BGAIN_REG 0x17
  289. #define ROFFSET_REG 0x18
  290. #define GOFFSET_REG 0x19
  291. #define BOFFSET_REG 0x1a
  292. #define OFLOW_REG 0x1b
  293. #define HSTART_REG 0x1c
  294. #define VSTART_REG 0x1d
  295. #define CWIDTH_REG 0x1e
  296. #define CHEIGHT_REG 0x1f
  297. #define YGAIN_REG 0x20
  298. #define YOFFSET_REG 0x21
  299. #define UVGAIN_REG 0x22
  300. #define UOFFSET_REG 0x23
  301. #define VOFFSET_REG 0x24
  302. #define SHARPNESS_REG 0x25
  303. #define COMPR_REG 0x26
  304. #define OUTFMT_REG 0x27
  305. #define XMIN_REG 0x28
  306. #define XMAX_REG 0x29
  307. #define YMIN_REG 0x2a
  308. #define YMAX_REG 0x2b
  309. #define HSCALELOW_REG 0x30
  310. #define HSCALEHIGH_REG 0x31
  311. #define VSCALELOW_REG 0x32
  312. #define VSCALEHIGH_REG 0x33
  313. #define AC97LSB_REG 0x40
  314. #define AC97MSB_REG 0x41
  315. #define AC97ADDR_REG 0x42
  316. #define AC97BUSY_REG 0x43
  317. /* em202 registers */
  318. #define MASTER_AC97 0x02
  319. #define VIDEO_AC97 0x14
  320. /* register settings */
  321. #define EM28XX_AUDIO_SRC_TUNER 0xc0
  322. #define EM28XX_AUDIO_SRC_LINE 0x80
  323. /* printk macros */
  324. #define em28xx_err(fmt, arg...) do {\
  325. printk(KERN_ERR fmt , ##arg); } while (0)
  326. #define em28xx_errdev(fmt, arg...) do {\
  327. printk(KERN_ERR "%s: "fmt,\
  328. dev->name , ##arg); } while (0)
  329. #define em28xx_info(fmt, arg...) do {\
  330. printk(KERN_INFO "%s: "fmt,\
  331. dev->name , ##arg); } while (0)
  332. #define em28xx_warn(fmt, arg...) do {\
  333. printk(KERN_WARNING "%s: "fmt,\
  334. dev->name , ##arg); } while (0)
  335. inline static int em28xx_audio_source(struct em28xx *dev, int input)
  336. {
  337. return em28xx_write_reg_bits(dev, AUDIOSRC_REG, input, 0xc0);
  338. }
  339. inline static int em28xx_audio_usb_mute(struct em28xx *dev, int mute)
  340. {
  341. return em28xx_write_reg_bits(dev, XCLK_REG, mute ? 0x00 : 0x80, 0x80);
  342. }
  343. inline static int em28xx_audio_analog_setup(struct em28xx *dev)
  344. {
  345. /* unmute video mixer with default volume level */
  346. return em28xx_write_ac97(dev, VIDEO_AC97, "\x08\x08");
  347. }
  348. inline static int em28xx_compression_disable(struct em28xx *dev)
  349. {
  350. /* side effect of disabling scaler and mixer */
  351. return em28xx_write_regs(dev, COMPR_REG, "\x00", 1);
  352. }
  353. inline static int em28xx_contrast_get(struct em28xx *dev)
  354. {
  355. return em28xx_read_reg(dev, YGAIN_REG) & 0x1f;
  356. }
  357. inline static int em28xx_brightness_get(struct em28xx *dev)
  358. {
  359. return em28xx_read_reg(dev, YOFFSET_REG);
  360. }
  361. inline static int em28xx_saturation_get(struct em28xx *dev)
  362. {
  363. return em28xx_read_reg(dev, UVGAIN_REG) & 0x1f;
  364. }
  365. inline static int em28xx_u_balance_get(struct em28xx *dev)
  366. {
  367. return em28xx_read_reg(dev, UOFFSET_REG);
  368. }
  369. inline static int em28xx_v_balance_get(struct em28xx *dev)
  370. {
  371. return em28xx_read_reg(dev, VOFFSET_REG);
  372. }
  373. inline static int em28xx_gamma_get(struct em28xx *dev)
  374. {
  375. return em28xx_read_reg(dev, GAMMA_REG) & 0x3f;
  376. }
  377. inline static int em28xx_contrast_set(struct em28xx *dev, s32 val)
  378. {
  379. u8 tmp = (u8) val;
  380. return em28xx_write_regs(dev, YGAIN_REG, &tmp, 1);
  381. }
  382. inline static int em28xx_brightness_set(struct em28xx *dev, s32 val)
  383. {
  384. u8 tmp = (u8) val;
  385. return em28xx_write_regs(dev, YOFFSET_REG, &tmp, 1);
  386. }
  387. inline static int em28xx_saturation_set(struct em28xx *dev, s32 val)
  388. {
  389. u8 tmp = (u8) val;
  390. return em28xx_write_regs(dev, UVGAIN_REG, &tmp, 1);
  391. }
  392. inline static int em28xx_u_balance_set(struct em28xx *dev, s32 val)
  393. {
  394. u8 tmp = (u8) val;
  395. return em28xx_write_regs(dev, UOFFSET_REG, &tmp, 1);
  396. }
  397. inline static int em28xx_v_balance_set(struct em28xx *dev, s32 val)
  398. {
  399. u8 tmp = (u8) val;
  400. return em28xx_write_regs(dev, VOFFSET_REG, &tmp, 1);
  401. }
  402. inline static int em28xx_gamma_set(struct em28xx *dev, s32 val)
  403. {
  404. u8 tmp = (u8) val;
  405. return em28xx_write_regs(dev, GAMMA_REG, &tmp, 1);
  406. }
  407. /*FIXME: maxw should be dependent of alt mode */
  408. inline static unsigned int norm_maxw(struct em28xx *dev)
  409. {
  410. switch(dev->model){
  411. case (EM2820_BOARD_MSI_VOX_USB_2): return(640);
  412. default: return(720);
  413. }
  414. }
  415. inline static unsigned int norm_maxh(struct em28xx *dev)
  416. {
  417. switch(dev->model){
  418. case (EM2820_BOARD_MSI_VOX_USB_2): return(480);
  419. default: return (dev->tvnorm->id & V4L2_STD_625_50) ? 576 : 480;
  420. }
  421. }
  422. #endif