au0828.h 9.3 KB

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