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