em28xx.h 15 KB

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