au0828.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Driver for the Auvitek AU0828 USB bridge
  3. *
  4. * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  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. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/usb.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c-algo-bit.h>
  25. #include <media/tveeprom.h>
  26. /* Analog */
  27. #include <linux/videodev2.h>
  28. #include <media/videobuf-vmalloc.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/v4l2-ctrls.h>
  31. #include <media/v4l2-fh.h>
  32. /* DVB */
  33. #include "demux.h"
  34. #include "dmxdev.h"
  35. #include "dvb_demux.h"
  36. #include "dvb_frontend.h"
  37. #include "dvb_net.h"
  38. #include "dvbdev.h"
  39. #include "au0828-reg.h"
  40. #include "au0828-cards.h"
  41. #define URB_COUNT 16
  42. #define URB_BUFSIZE (0xe522)
  43. /* Analog constants */
  44. #define NTSC_STD_W 720
  45. #define NTSC_STD_H 480
  46. #define AU0828_INTERLACED_DEFAULT 1
  47. #define V4L2_CID_PRIVATE_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
  48. /* Defination for AU0828 USB transfer */
  49. #define AU0828_MAX_ISO_BUFS 12 /* maybe resize this value in the future */
  50. #define AU0828_ISO_PACKETS_PER_URB 128
  51. #define AU0828_MIN_BUF 4
  52. #define AU0828_DEF_BUF 8
  53. #define AU0828_MAX_INPUT 4
  54. /* au0828 resource types (used for res_get/res_lock etc */
  55. #define AU0828_RESOURCE_VIDEO 0x01
  56. #define AU0828_RESOURCE_VBI 0x02
  57. enum au0828_itype {
  58. AU0828_VMUX_UNDEFINED = 0,
  59. AU0828_VMUX_COMPOSITE,
  60. AU0828_VMUX_SVIDEO,
  61. AU0828_VMUX_CABLE,
  62. AU0828_VMUX_TELEVISION,
  63. AU0828_VMUX_DVB,
  64. AU0828_VMUX_DEBUG
  65. };
  66. struct au0828_input {
  67. enum au0828_itype type;
  68. unsigned int vmux;
  69. unsigned int amux;
  70. void (*audio_setup) (void *priv, int enable);
  71. };
  72. struct au0828_board {
  73. char *name;
  74. unsigned int tuner_type;
  75. unsigned char tuner_addr;
  76. unsigned char i2c_clk_divider;
  77. unsigned char has_ir_i2c:1;
  78. unsigned char has_analog:1;
  79. struct au0828_input input[AU0828_MAX_INPUT];
  80. };
  81. struct au0828_dvb {
  82. struct mutex lock;
  83. struct dvb_adapter adapter;
  84. struct dvb_frontend *frontend;
  85. struct dvb_demux demux;
  86. struct dmxdev dmxdev;
  87. struct dmx_frontend fe_hw;
  88. struct dmx_frontend fe_mem;
  89. struct dvb_net net;
  90. int feeding;
  91. int start_count;
  92. int stop_count;
  93. int (*set_frontend)(struct dvb_frontend *fe);
  94. };
  95. enum au0828_stream_state {
  96. STREAM_OFF,
  97. STREAM_INTERRUPT,
  98. STREAM_ON
  99. };
  100. #define AUVI_INPUT(nr) (dev->board.input[nr])
  101. /* device state */
  102. enum au0828_dev_state {
  103. DEV_INITIALIZED = 0x01,
  104. DEV_DISCONNECTED = 0x02,
  105. DEV_MISCONFIGURED = 0x04
  106. };
  107. struct au0828_fh {
  108. /* must be the first field of this struct! */
  109. struct v4l2_fh fh;
  110. struct au0828_dev *dev;
  111. unsigned int resources;
  112. struct videobuf_queue vb_vidq;
  113. struct videobuf_queue vb_vbiq;
  114. enum v4l2_buf_type type;
  115. };
  116. struct au0828_usb_isoc_ctl {
  117. /* max packet size of isoc transaction */
  118. int max_pkt_size;
  119. /* number of allocated urbs */
  120. int num_bufs;
  121. /* urb for isoc transfers */
  122. struct urb **urb;
  123. /* transfer buffers for isoc transfer */
  124. char **transfer_buffer;
  125. /* Last buffer command and region */
  126. u8 cmd;
  127. int pos, size, pktsize;
  128. /* Last field: ODD or EVEN? */
  129. int field;
  130. /* Stores incomplete commands */
  131. u32 tmp_buf;
  132. int tmp_buf_len;
  133. /* Stores already requested buffers */
  134. struct au0828_buffer *buf;
  135. struct au0828_buffer *vbi_buf;
  136. /* Stores the number of received fields */
  137. int nfields;
  138. /* isoc urb callback */
  139. int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
  140. };
  141. /* buffer for one video frame */
  142. struct au0828_buffer {
  143. /* common v4l buffer stuff -- must be first */
  144. struct videobuf_buffer vb;
  145. struct list_head frame;
  146. int top_field;
  147. int receiving;
  148. };
  149. struct au0828_dmaqueue {
  150. struct list_head active;
  151. struct list_head queued;
  152. wait_queue_head_t wq;
  153. /* Counters to control buffer fill */
  154. int pos;
  155. };
  156. struct au0828_dev {
  157. struct mutex mutex;
  158. struct usb_device *usbdev;
  159. int boardnr;
  160. struct au0828_board board;
  161. u8 ctrlmsg[64];
  162. /* I2C */
  163. struct i2c_adapter i2c_adap;
  164. struct i2c_algorithm i2c_algo;
  165. struct i2c_client i2c_client;
  166. u32 i2c_rc;
  167. /* Digital */
  168. struct au0828_dvb dvb;
  169. struct work_struct restart_streaming;
  170. #ifdef CONFIG_VIDEO_AU0828_V4L2
  171. /* Analog */
  172. struct v4l2_device v4l2_dev;
  173. struct v4l2_ctrl_handler v4l2_ctrl_hdl;
  174. #endif
  175. #ifdef CONFIG_VIDEO_AU0828_RC
  176. struct au0828_rc *ir;
  177. #endif
  178. int users;
  179. unsigned int resources; /* resources in use */
  180. struct video_device *vdev;
  181. struct video_device *vbi_dev;
  182. struct timer_list vid_timeout;
  183. int vid_timeout_running;
  184. struct timer_list vbi_timeout;
  185. int vbi_timeout_running;
  186. int width;
  187. int height;
  188. int vbi_width;
  189. int vbi_height;
  190. u32 vbi_read;
  191. v4l2_std_id std;
  192. u32 field_size;
  193. u32 frame_size;
  194. u32 bytesperline;
  195. int type;
  196. u8 ctrl_ainput;
  197. __u8 isoc_in_endpointaddr;
  198. u8 isoc_init_ok;
  199. int greenscreen_detected;
  200. unsigned int frame_count;
  201. int ctrl_freq;
  202. int input_type;
  203. int std_set_in_tuner_core;
  204. unsigned int ctrl_input;
  205. enum au0828_dev_state dev_state;
  206. enum au0828_stream_state stream_state;
  207. wait_queue_head_t open;
  208. struct mutex lock;
  209. /* Isoc control struct */
  210. struct au0828_dmaqueue vidq;
  211. struct au0828_dmaqueue vbiq;
  212. struct au0828_usb_isoc_ctl isoc_ctl;
  213. spinlock_t slock;
  214. /* usb transfer */
  215. int alt; /* alternate */
  216. int max_pkt_size; /* max packet size of isoc transaction */
  217. int num_alt; /* Number of alternative settings */
  218. unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
  219. struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */
  220. char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
  221. transfer */
  222. /* DVB USB / URB Related */
  223. bool urb_streaming, need_urb_start;
  224. struct urb *urbs[URB_COUNT];
  225. /* Preallocated transfer digital transfer buffers */
  226. char *dig_transfer_buffer[URB_COUNT];
  227. };
  228. /* ----------------------------------------------------------- */
  229. #define au0828_read(dev, reg) au0828_readreg(dev, reg)
  230. #define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
  231. #define au0828_andor(dev, reg, mask, value) \
  232. au0828_writereg(dev, reg, \
  233. (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
  234. #define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
  235. #define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
  236. /* ----------------------------------------------------------- */
  237. /* au0828-core.c */
  238. extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
  239. extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
  240. extern int au0828_debug;
  241. /* ----------------------------------------------------------- */
  242. /* au0828-cards.c */
  243. extern struct au0828_board au0828_boards[];
  244. extern struct usb_device_id au0828_usb_id_table[];
  245. extern void au0828_gpio_setup(struct au0828_dev *dev);
  246. extern int au0828_tuner_callback(void *priv, int component,
  247. int command, int arg);
  248. extern void au0828_card_setup(struct au0828_dev *dev);
  249. /* ----------------------------------------------------------- */
  250. /* au0828-i2c.c */
  251. extern int au0828_i2c_register(struct au0828_dev *dev);
  252. extern int au0828_i2c_unregister(struct au0828_dev *dev);
  253. /* ----------------------------------------------------------- */
  254. /* au0828-video.c */
  255. int au0828_analog_register(struct au0828_dev *dev,
  256. struct usb_interface *interface);
  257. int au0828_analog_stream_disable(struct au0828_dev *d);
  258. void au0828_analog_unregister(struct au0828_dev *dev);
  259. #ifdef CONFIG_VIDEO_AU0828_V4L2
  260. void au0828_v4l2_suspend(struct au0828_dev *dev);
  261. void au0828_v4l2_resume(struct au0828_dev *dev);
  262. #else
  263. static inline void au0828_v4l2_suspend(struct au0828_dev *dev) { };
  264. static inline void au0828_v4l2_resume(struct au0828_dev *dev) { };
  265. #endif
  266. /* ----------------------------------------------------------- */
  267. /* au0828-dvb.c */
  268. extern int au0828_dvb_register(struct au0828_dev *dev);
  269. extern void au0828_dvb_unregister(struct au0828_dev *dev);
  270. void au0828_dvb_suspend(struct au0828_dev *dev);
  271. void au0828_dvb_resume(struct au0828_dev *dev);
  272. /* au0828-vbi.c */
  273. extern struct videobuf_queue_ops au0828_vbi_qops;
  274. #define dprintk(level, fmt, arg...)\
  275. do { if (au0828_debug & level)\
  276. printk(KERN_DEBUG pr_fmt(fmt), ## arg);\
  277. } while (0)
  278. /* au0828-input.c */
  279. #ifdef CONFIG_VIDEO_AU0828_RC
  280. extern int au0828_rc_register(struct au0828_dev *dev);
  281. extern void au0828_rc_unregister(struct au0828_dev *dev);
  282. extern int au0828_rc_suspend(struct au0828_dev *dev);
  283. extern int au0828_rc_resume(struct au0828_dev *dev);
  284. #else
  285. static inline int au0828_rc_register(struct au0828_dev *dev) { return 0; }
  286. static inline void au0828_rc_unregister(struct au0828_dev *dev) { }
  287. static inline int au0828_rc_suspend(struct au0828_dev *dev) { return 0; }
  288. static inline int au0828_rc_resume(struct au0828_dev *dev) { return 0; }
  289. #endif