au0828.h 9.9 KB

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