au0828.h 10 KB

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