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