em28xx.h 14 KB

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