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