au0828.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include <linux/usb.h>
  22. #include <linux/i2c.h>
  23. #include <linux/i2c-algo-bit.h>
  24. #include <media/tveeprom.h>
  25. /* Analog */
  26. #include <linux/videodev2.h>
  27. #include <media/videobuf-vmalloc.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-fh.h>
  31. /* DVB */
  32. #include "demux.h"
  33. #include "dmxdev.h"
  34. #include "dvb_demux.h"
  35. #include "dvb_frontend.h"
  36. #include "dvb_net.h"
  37. #include "dvbdev.h"
  38. #include "au0828-reg.h"
  39. #include "au0828-cards.h"
  40. #define DRIVER_NAME "au0828"
  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. struct au0828_input input[AU0828_MAX_INPUT];
  78. };
  79. struct au0828_dvb {
  80. struct mutex lock;
  81. struct dvb_adapter adapter;
  82. struct dvb_frontend *frontend;
  83. struct dvb_demux demux;
  84. struct dmxdev dmxdev;
  85. struct dmx_frontend fe_hw;
  86. struct dmx_frontend fe_mem;
  87. struct dvb_net net;
  88. int feeding;
  89. };
  90. enum au0828_stream_state {
  91. STREAM_OFF,
  92. STREAM_INTERRUPT,
  93. STREAM_ON
  94. };
  95. #define AUVI_INPUT(nr) (dev->board.input[nr])
  96. /* device state */
  97. enum au0828_dev_state {
  98. DEV_INITIALIZED = 0x01,
  99. DEV_DISCONNECTED = 0x02,
  100. DEV_MISCONFIGURED = 0x04
  101. };
  102. struct au0828_fh {
  103. /* must be the first field of this struct! */
  104. struct v4l2_fh fh;
  105. struct au0828_dev *dev;
  106. unsigned int resources;
  107. struct videobuf_queue vb_vidq;
  108. struct videobuf_queue vb_vbiq;
  109. enum v4l2_buf_type type;
  110. };
  111. struct au0828_usb_isoc_ctl {
  112. /* max packet size of isoc transaction */
  113. int max_pkt_size;
  114. /* number of allocated urbs */
  115. int num_bufs;
  116. /* urb for isoc transfers */
  117. struct urb **urb;
  118. /* transfer buffers for isoc transfer */
  119. char **transfer_buffer;
  120. /* Last buffer command and region */
  121. u8 cmd;
  122. int pos, size, pktsize;
  123. /* Last field: ODD or EVEN? */
  124. int field;
  125. /* Stores incomplete commands */
  126. u32 tmp_buf;
  127. int tmp_buf_len;
  128. /* Stores already requested buffers */
  129. struct au0828_buffer *buf;
  130. struct au0828_buffer *vbi_buf;
  131. /* Stores the number of received fields */
  132. int nfields;
  133. /* isoc urb callback */
  134. int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
  135. };
  136. /* buffer for one video frame */
  137. struct au0828_buffer {
  138. /* common v4l buffer stuff -- must be first */
  139. struct videobuf_buffer vb;
  140. struct list_head frame;
  141. int top_field;
  142. int receiving;
  143. };
  144. struct au0828_dmaqueue {
  145. struct list_head active;
  146. struct list_head queued;
  147. wait_queue_head_t wq;
  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. int users;
  171. unsigned int resources; /* resources in use */
  172. struct video_device *vdev;
  173. struct video_device *vbi_dev;
  174. struct timer_list vid_timeout;
  175. int vid_timeout_running;
  176. struct timer_list vbi_timeout;
  177. int vbi_timeout_running;
  178. int width;
  179. int height;
  180. int vbi_width;
  181. int vbi_height;
  182. u32 vbi_read;
  183. v4l2_std_id std;
  184. u32 field_size;
  185. u32 frame_size;
  186. u32 bytesperline;
  187. int type;
  188. u8 ctrl_ainput;
  189. __u8 isoc_in_endpointaddr;
  190. u8 isoc_init_ok;
  191. int greenscreen_detected;
  192. unsigned int frame_count;
  193. int ctrl_freq;
  194. int input_type;
  195. int std_set_in_tuner_core;
  196. unsigned int ctrl_input;
  197. enum au0828_dev_state dev_state;
  198. enum au0828_stream_state stream_state;
  199. wait_queue_head_t open;
  200. struct mutex lock;
  201. /* Isoc control struct */
  202. struct au0828_dmaqueue vidq;
  203. struct au0828_dmaqueue vbiq;
  204. struct au0828_usb_isoc_ctl isoc_ctl;
  205. spinlock_t slock;
  206. /* usb transfer */
  207. int alt; /* alternate */
  208. int max_pkt_size; /* max packet size of isoc transaction */
  209. int num_alt; /* Number of alternative settings */
  210. unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
  211. struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */
  212. char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
  213. transfer */
  214. /* USB / URB Related */
  215. int urb_streaming;
  216. struct urb *urbs[URB_COUNT];
  217. };
  218. /* ----------------------------------------------------------- */
  219. #define au0828_read(dev, reg) au0828_readreg(dev, reg)
  220. #define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
  221. #define au0828_andor(dev, reg, mask, value) \
  222. au0828_writereg(dev, reg, \
  223. (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
  224. #define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
  225. #define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
  226. /* ----------------------------------------------------------- */
  227. /* au0828-core.c */
  228. extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
  229. extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
  230. extern int au0828_debug;
  231. /* ----------------------------------------------------------- */
  232. /* au0828-cards.c */
  233. extern struct au0828_board au0828_boards[];
  234. extern struct usb_device_id au0828_usb_id_table[];
  235. extern void au0828_gpio_setup(struct au0828_dev *dev);
  236. extern int au0828_tuner_callback(void *priv, int component,
  237. int command, int arg);
  238. extern void au0828_card_setup(struct au0828_dev *dev);
  239. /* ----------------------------------------------------------- */
  240. /* au0828-i2c.c */
  241. extern int au0828_i2c_register(struct au0828_dev *dev);
  242. extern int au0828_i2c_unregister(struct au0828_dev *dev);
  243. /* ----------------------------------------------------------- */
  244. /* au0828-video.c */
  245. int au0828_analog_register(struct au0828_dev *dev,
  246. struct usb_interface *interface);
  247. int au0828_analog_stream_disable(struct au0828_dev *d);
  248. void au0828_analog_unregister(struct au0828_dev *dev);
  249. /* ----------------------------------------------------------- */
  250. /* au0828-dvb.c */
  251. extern int au0828_dvb_register(struct au0828_dev *dev);
  252. extern void au0828_dvb_unregister(struct au0828_dev *dev);
  253. /* au0828-vbi.c */
  254. extern struct videobuf_queue_ops au0828_vbi_qops;
  255. #define dprintk(level, fmt, arg...)\
  256. do { if (au0828_debug & level)\
  257. printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\
  258. } while (0)