au0828.h 10 KB

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