blob: 3b480005ce3be6576fd83348e016af8688b990fc [file] [log] [blame]
Steven Toth265a6512008-04-18 21:34:00 -03001/*
2 * Driver for the Auvitek AU0828 USB bridge
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
Steven Toth265a6512008-04-18 21:34:00 -03005 *
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
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -030022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Steven Toth265a6512008-04-18 21:34:00 -030024#include <linux/usb.h>
25#include <linux/i2c.h>
26#include <linux/i2c-algo-bit.h>
Steven Toth28930fa2008-03-29 19:53:07 -030027#include <media/tveeprom.h>
Steven Toth265a6512008-04-18 21:34:00 -030028
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030029/* Analog */
30#include <linux/videodev2.h>
Shuah Khan05439b12015-01-29 13:41:32 -030031#include <media/videobuf2-vmalloc.h>
Devin Heitmuellerb14667f2009-03-11 03:01:04 -030032#include <media/v4l2-device.h>
Hans Verkuile8c26f42013-02-15 08:55:41 -030033#include <media/v4l2-ctrls.h>
Hans Verkuil83b09422013-02-15 09:14:00 -030034#include <media/v4l2-fh.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030035
Steven Toth265a6512008-04-18 21:34:00 -030036/* DVB */
37#include "demux.h"
38#include "dmxdev.h"
39#include "dvb_demux.h"
40#include "dvb_frontend.h"
41#include "dvb_net.h"
42#include "dvbdev.h"
43
44#include "au0828-reg.h"
45#include "au0828-cards.h"
46
Steven Toth265a6512008-04-18 21:34:00 -030047#define URB_COUNT 16
Steven Toth265a6512008-04-18 21:34:00 -030048#define URB_BUFSIZE (0xe522)
49
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030050/* Analog constants */
51#define NTSC_STD_W 720
52#define NTSC_STD_H 480
53
54#define AU0828_INTERLACED_DEFAULT 1
55#define V4L2_CID_PRIVATE_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
56
57/* Defination for AU0828 USB transfer */
58#define AU0828_MAX_ISO_BUFS 12 /* maybe resize this value in the future */
Devin Heitmueller83f859c2010-06-25 01:33:39 -030059#define AU0828_ISO_PACKETS_PER_URB 128
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030060
61#define AU0828_MIN_BUF 4
62#define AU0828_DEF_BUF 8
63
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030064#define AU0828_MAX_INPUT 4
65
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -030066/* au0828 resource types (used for res_get/res_lock etc */
67#define AU0828_RESOURCE_VIDEO 0x01
68#define AU0828_RESOURCE_VBI 0x02
69
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030070enum au0828_itype {
Devin Heitmueller220be772009-03-11 03:01:01 -030071 AU0828_VMUX_UNDEFINED = 0,
72 AU0828_VMUX_COMPOSITE,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030073 AU0828_VMUX_SVIDEO,
74 AU0828_VMUX_CABLE,
75 AU0828_VMUX_TELEVISION,
76 AU0828_VMUX_DVB,
77 AU0828_VMUX_DEBUG
78};
79
80struct au0828_input {
81 enum au0828_itype type;
82 unsigned int vmux;
83 unsigned int amux;
84 void (*audio_setup) (void *priv, int enable);
85};
86
Steven Toth265a6512008-04-18 21:34:00 -030087struct au0828_board {
88 char *name;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -030089 unsigned int tuner_type;
90 unsigned char tuner_addr;
Devin Heitmueller16af6f52009-04-01 00:11:31 -030091 unsigned char i2c_clk_divider;
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -030092 unsigned char has_ir_i2c:1;
Mauro Carvalho Chehabb13b47e2014-08-18 08:51:28 -030093 unsigned char has_analog:1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030094 struct au0828_input input[AU0828_MAX_INPUT];
95
Steven Toth265a6512008-04-18 21:34:00 -030096};
97
98struct 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;
Tim Mester46099812014-01-07 01:29:24 -0300108 int start_count;
109 int stop_count;
Mauro Carvalho Chehabf6fef862014-05-09 06:17:55 -0300110
111 int (*set_frontend)(struct dvb_frontend *fe);
Steven Toth265a6512008-04-18 21:34:00 -0300112};
113
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300114enum au0828_stream_state {
115 STREAM_OFF,
116 STREAM_INTERRUPT,
117 STREAM_ON
118};
119
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300120#define AUVI_INPUT(nr) (dev->board.input[nr])
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300121
122/* device state */
123enum au0828_dev_state {
124 DEV_INITIALIZED = 0x01,
125 DEV_DISCONNECTED = 0x02,
126 DEV_MISCONFIGURED = 0x04
127};
128
Shuah Khan05439b12015-01-29 13:41:32 -0300129struct au0828_dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300130
131struct 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 */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300156 struct au0828_buffer *buf;
157 struct au0828_buffer *vbi_buf;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300158
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 */
168struct au0828_buffer {
169 /* common v4l buffer stuff -- must be first */
Shuah Khan05439b12015-01-29 13:41:32 -0300170 struct vb2_buffer vb;
171 struct list_head list;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300172
Shuah Khan05439b12015-01-29 13:41:32 -0300173 void *mem;
174 unsigned long length;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300175 int top_field;
Shuah Khan05439b12015-01-29 13:41:32 -0300176 /* pointer to vmalloc memory address in vb */
177 char *vb_buf;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300178};
179
180struct au0828_dmaqueue {
181 struct list_head active;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300182 /* Counters to control buffer fill */
Shuah Khan05439b12015-01-29 13:41:32 -0300183 int pos;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300184};
185
Steven Toth265a6512008-04-18 21:34:00 -0300186struct au0828_dev {
187 struct mutex mutex;
188 struct usb_device *usbdev;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300189 int boardnr;
190 struct au0828_board board;
Steven Toth265a6512008-04-18 21:34:00 -0300191 u8 ctrlmsg[64];
192
193 /* I2C */
194 struct i2c_adapter i2c_adap;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300195 struct i2c_algorithm i2c_algo;
Steven Toth265a6512008-04-18 21:34:00 -0300196 struct i2c_client i2c_client;
197 u32 i2c_rc;
198
199 /* Digital */
200 struct au0828_dvb dvb;
Devin Heitmueller43f2ccc2012-08-06 22:46:53 -0300201 struct work_struct restart_streaming;
Steven Toth265a6512008-04-18 21:34:00 -0300202
Michael Krufky8a4e7862012-12-04 11:30:00 -0300203#ifdef CONFIG_VIDEO_AU0828_V4L2
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300204 /* Analog */
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300205 struct v4l2_device v4l2_dev;
Hans Verkuile8c26f42013-02-15 08:55:41 -0300206 struct v4l2_ctrl_handler v4l2_ctrl_hdl;
Michael Krufky8a4e7862012-12-04 11:30:00 -0300207#endif
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300208#ifdef CONFIG_VIDEO_AU0828_RC
209 struct au0828_rc *ir;
210#endif
211
Shuah Khan41071bb2015-02-26 19:33:13 -0300212 struct video_device vdev;
213 struct video_device vbi_dev;
Shuah Khan05439b12015-01-29 13:41:32 -0300214
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
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300224 struct timer_list vid_timeout;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300225 int vid_timeout_running;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300226 struct timer_list vbi_timeout;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300227 int vbi_timeout_running;
Shuah Khan05439b12015-01-29 13:41:32 -0300228
229 int users;
230 int streaming_users;
231
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300232 int width;
233 int height;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300234 int vbi_width;
235 int vbi_height;
236 u32 vbi_read;
Hans Verkuil33b6b892013-02-15 09:22:37 -0300237 v4l2_std_id std;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300238 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;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300246 int ctrl_freq;
247 int input_type;
Devin Heitmuellerc98bc032012-08-06 22:46:59 -0300248 int std_set_in_tuner_core;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300249 unsigned int ctrl_input;
250 enum au0828_dev_state 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;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300258 struct au0828_dmaqueue vbiq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300259 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
Mauro Carvalho Chehab29309772014-08-09 23:14:21 -0300271 /* DVB USB / URB Related */
272 bool urb_streaming, need_urb_start;
Steven Toth265a6512008-04-18 21:34:00 -0300273 struct urb *urbs[URB_COUNT];
Tim Mesterf251b3e2014-01-07 01:29:25 -0300274
275 /* Preallocated transfer digital transfer buffers */
276
277 char *dig_transfer_buffer[URB_COUNT];
Steven Toth265a6512008-04-18 21:34:00 -0300278};
279
Shuah Khan05439b12015-01-29 13:41:32 -0300280
Steven Toth265a6512008-04-18 21:34:00 -0300281/* ----------------------------------------------------------- */
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300282#define au0828_read(dev, reg) au0828_readreg(dev, reg)
283#define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
284#define au0828_andor(dev, reg, mask, value) \
285 au0828_writereg(dev, reg, \
286 (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
Steven Toth265a6512008-04-18 21:34:00 -0300287
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300288#define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
289#define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
Steven Toth265a6512008-04-18 21:34:00 -0300290
291/* ----------------------------------------------------------- */
292/* au0828-core.c */
293extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
294extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300295extern int au0828_debug;
Steven Toth265a6512008-04-18 21:34:00 -0300296
297/* ----------------------------------------------------------- */
298/* au0828-cards.c */
299extern struct au0828_board au0828_boards[];
300extern struct usb_device_id au0828_usb_id_table[];
Steven Toth265a6512008-04-18 21:34:00 -0300301extern void au0828_gpio_setup(struct au0828_dev *dev);
Michael Krufkyd7cba042008-09-12 13:31:45 -0300302extern int au0828_tuner_callback(void *priv, int component,
303 int command, int arg);
Steven Toth28930fa2008-03-29 19:53:07 -0300304extern void au0828_card_setup(struct au0828_dev *dev);
Steven Toth265a6512008-04-18 21:34:00 -0300305
306/* ----------------------------------------------------------- */
307/* au0828-i2c.c */
308extern int au0828_i2c_register(struct au0828_dev *dev);
309extern int au0828_i2c_unregister(struct au0828_dev *dev);
Steven Toth265a6512008-04-18 21:34:00 -0300310
311/* ----------------------------------------------------------- */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300312/* au0828-video.c */
Shuah Khan05439b12015-01-29 13:41:32 -0300313extern int au0828_analog_register(struct au0828_dev *dev,
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -0300314 struct usb_interface *interface);
Shuah Khan05439b12015-01-29 13:41:32 -0300315extern void au0828_analog_unregister(struct au0828_dev *dev);
316extern int au0828_start_analog_streaming(struct vb2_queue *vq,
317 unsigned int count);
318extern void au0828_stop_vbi_streaming(struct vb2_queue *vq);
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -0300319#ifdef CONFIG_VIDEO_AU0828_V4L2
Shuah Khan05439b12015-01-29 13:41:32 -0300320extern void au0828_v4l2_suspend(struct au0828_dev *dev);
321extern void au0828_v4l2_resume(struct au0828_dev *dev);
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -0300322#else
323static inline void au0828_v4l2_suspend(struct au0828_dev *dev) { };
324static inline void au0828_v4l2_resume(struct au0828_dev *dev) { };
325#endif
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300326
327/* ----------------------------------------------------------- */
Steven Toth265a6512008-04-18 21:34:00 -0300328/* au0828-dvb.c */
329extern int au0828_dvb_register(struct au0828_dev *dev);
330extern void au0828_dvb_unregister(struct au0828_dev *dev);
Mauro Carvalho Chehabb799de72014-08-09 21:47:13 -0300331void au0828_dvb_suspend(struct au0828_dev *dev);
332void au0828_dvb_resume(struct au0828_dev *dev);
Steven Tothbc3c6132008-04-18 21:39:11 -0300333
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300334/* au0828-vbi.c */
Shuah Khan05439b12015-01-29 13:41:32 -0300335extern struct vb2_ops au0828_vbi_qops;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300336
Steven Tothbc3c6132008-04-18 21:39:11 -0300337#define dprintk(level, fmt, arg...)\
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300338 do { if (au0828_debug & level)\
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300339 printk(KERN_DEBUG pr_fmt(fmt), ## arg);\
Steven Tothbc3c6132008-04-18 21:39:11 -0300340 } while (0)
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300341
342/* au0828-input.c */
Shuah Khan917cbcd2014-08-08 21:36:18 -0300343#ifdef CONFIG_VIDEO_AU0828_RC
344extern int au0828_rc_register(struct au0828_dev *dev);
345extern void au0828_rc_unregister(struct au0828_dev *dev);
346extern int au0828_rc_suspend(struct au0828_dev *dev);
347extern int au0828_rc_resume(struct au0828_dev *dev);
348#else
349static inline int au0828_rc_register(struct au0828_dev *dev) { return 0; }
350static inline void au0828_rc_unregister(struct au0828_dev *dev) { }
351static inline int au0828_rc_suspend(struct au0828_dev *dev) { return 0; }
352static inline int au0828_rc_resume(struct au0828_dev *dev) { return 0; }
353#endif