blob: 8276072bc55ac8b285a89474c554de5942111b25 [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>
Junghak Sung2d700712015-09-22 10:30:30 -030031#include <media/videobuf2-v4l2.h>
Shuah Khan05439b12015-01-29 13:41:32 -030032#include <media/videobuf2-vmalloc.h>
Devin Heitmuellerb14667f2009-03-11 03:01:04 -030033#include <media/v4l2-device.h>
Hans Verkuile8c26f42013-02-15 08:55:41 -030034#include <media/v4l2-ctrls.h>
Hans Verkuil83b09422013-02-15 09:14:00 -030035#include <media/v4l2-fh.h>
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -030036#include <media/media-device.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030037
Steven Toth265a6512008-04-18 21:34:00 -030038/* 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
Steven Toth265a6512008-04-18 21:34:00 -030049#define URB_COUNT 16
Steven Toth265a6512008-04-18 21:34:00 -030050#define URB_BUFSIZE (0xe522)
51
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030052/* 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 */
Devin Heitmueller83f859c2010-06-25 01:33:39 -030061#define AU0828_ISO_PACKETS_PER_URB 128
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030062
63#define AU0828_MIN_BUF 4
64#define AU0828_DEF_BUF 8
65
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030066#define AU0828_MAX_INPUT 4
67
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -030068/* au0828 resource types (used for res_get/res_lock etc */
69#define AU0828_RESOURCE_VIDEO 0x01
70#define AU0828_RESOURCE_VBI 0x02
71
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030072enum au0828_itype {
Devin Heitmueller220be772009-03-11 03:01:01 -030073 AU0828_VMUX_UNDEFINED = 0,
74 AU0828_VMUX_COMPOSITE,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030075 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
Steven Toth265a6512008-04-18 21:34:00 -030089struct au0828_board {
90 char *name;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -030091 unsigned int tuner_type;
92 unsigned char tuner_addr;
Devin Heitmueller16af6f52009-04-01 00:11:31 -030093 unsigned char i2c_clk_divider;
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -030094 unsigned char has_ir_i2c:1;
Mauro Carvalho Chehabb13b47e2014-08-18 08:51:28 -030095 unsigned char has_analog:1;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030096 struct au0828_input input[AU0828_MAX_INPUT];
Steven Toth265a6512008-04-18 21:34:00 -030097};
98
99struct au0828_dvb {
100 struct mutex lock;
101 struct dvb_adapter adapter;
102 struct dvb_frontend *frontend;
103 struct dvb_demux demux;
104 struct dmxdev dmxdev;
105 struct dmx_frontend fe_hw;
106 struct dmx_frontend fe_mem;
107 struct dvb_net net;
108 int feeding;
Tim Mester46099812014-01-07 01:29:24 -0300109 int start_count;
110 int stop_count;
Mauro Carvalho Chehabf6fef862014-05-09 06:17:55 -0300111
112 int (*set_frontend)(struct dvb_frontend *fe);
Steven Toth265a6512008-04-18 21:34:00 -0300113};
114
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300115enum au0828_stream_state {
116 STREAM_OFF,
117 STREAM_INTERRUPT,
118 STREAM_ON
119};
120
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300121#define AUVI_INPUT(nr) (dev->board.input[nr])
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300122
123/* device state */
124enum au0828_dev_state {
125 DEV_INITIALIZED = 0x01,
126 DEV_DISCONNECTED = 0x02,
127 DEV_MISCONFIGURED = 0x04
128};
129
Shuah Khan05439b12015-01-29 13:41:32 -0300130struct au0828_dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300131
132struct au0828_usb_isoc_ctl {
133 /* max packet size of isoc transaction */
134 int max_pkt_size;
135
136 /* number of allocated urbs */
137 int num_bufs;
138
139 /* urb for isoc transfers */
140 struct urb **urb;
141
142 /* transfer buffers for isoc transfer */
143 char **transfer_buffer;
144
145 /* Last buffer command and region */
146 u8 cmd;
147 int pos, size, pktsize;
148
149 /* Last field: ODD or EVEN? */
150 int field;
151
152 /* Stores incomplete commands */
153 u32 tmp_buf;
154 int tmp_buf_len;
155
156 /* Stores already requested buffers */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300157 struct au0828_buffer *buf;
158 struct au0828_buffer *vbi_buf;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300159
160 /* Stores the number of received fields */
161 int nfields;
162
163 /* isoc urb callback */
164 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
165
166};
167
168/* buffer for one video frame */
169struct au0828_buffer {
170 /* common v4l buffer stuff -- must be first */
Junghak Sung2d700712015-09-22 10:30:30 -0300171 struct vb2_v4l2_buffer vb;
Shuah Khan05439b12015-01-29 13:41:32 -0300172 struct list_head list;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300173
Shuah Khan05439b12015-01-29 13:41:32 -0300174 void *mem;
175 unsigned long length;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300176 int top_field;
Shuah Khan05439b12015-01-29 13:41:32 -0300177 /* pointer to vmalloc memory address in vb */
178 char *vb_buf;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300179};
180
181struct au0828_dmaqueue {
182 struct list_head active;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300183 /* Counters to control buffer fill */
Shuah Khan05439b12015-01-29 13:41:32 -0300184 int pos;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300185};
186
Steven Toth265a6512008-04-18 21:34:00 -0300187struct au0828_dev {
188 struct mutex mutex;
189 struct usb_device *usbdev;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300190 int boardnr;
191 struct au0828_board board;
Steven Toth265a6512008-04-18 21:34:00 -0300192 u8 ctrlmsg[64];
193
194 /* I2C */
195 struct i2c_adapter i2c_adap;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300196 struct i2c_algorithm i2c_algo;
Steven Toth265a6512008-04-18 21:34:00 -0300197 struct i2c_client i2c_client;
198 u32 i2c_rc;
199
200 /* Digital */
201 struct au0828_dvb dvb;
Devin Heitmueller43f2ccc2012-08-06 22:46:53 -0300202 struct work_struct restart_streaming;
Steven Toth265a6512008-04-18 21:34:00 -0300203
Michael Krufky8a4e7862012-12-04 11:30:00 -0300204#ifdef CONFIG_VIDEO_AU0828_V4L2
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300205 /* Analog */
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300206 struct v4l2_device v4l2_dev;
Hans Verkuile8c26f42013-02-15 08:55:41 -0300207 struct v4l2_ctrl_handler v4l2_ctrl_hdl;
Michael Krufky8a4e7862012-12-04 11:30:00 -0300208#endif
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300209#ifdef CONFIG_VIDEO_AU0828_RC
210 struct au0828_rc *ir;
211#endif
212
Shuah Khan41071bb2015-02-26 19:33:13 -0300213 struct video_device vdev;
214 struct video_device vbi_dev;
Shuah Khan05439b12015-01-29 13:41:32 -0300215
216 /* Videobuf2 */
217 struct vb2_queue vb_vidq;
218 struct vb2_queue vb_vbiq;
219 struct mutex vb_queue_lock;
220 struct mutex vb_vbi_queue_lock;
221
222 unsigned int frame_count;
223 unsigned int vbi_frame_count;
224
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300225 struct timer_list vid_timeout;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300226 int vid_timeout_running;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300227 struct timer_list vbi_timeout;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300228 int vbi_timeout_running;
Shuah Khan05439b12015-01-29 13:41:32 -0300229
230 int users;
231 int streaming_users;
232
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300233 int width;
234 int height;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300235 int vbi_width;
236 int vbi_height;
237 u32 vbi_read;
Hans Verkuil33b6b892013-02-15 09:22:37 -0300238 v4l2_std_id std;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300239 u32 field_size;
240 u32 frame_size;
241 u32 bytesperline;
242 int type;
243 u8 ctrl_ainput;
244 __u8 isoc_in_endpointaddr;
245 u8 isoc_init_ok;
246 int greenscreen_detected;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300247 int ctrl_freq;
248 int input_type;
Devin Heitmuellerc98bc032012-08-06 22:46:59 -0300249 int std_set_in_tuner_core;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300250 unsigned int ctrl_input;
251 enum au0828_dev_state dev_state;
252 enum au0828_stream_state stream_state;
253 wait_queue_head_t open;
254
255 struct mutex lock;
256
257 /* Isoc control struct */
258 struct au0828_dmaqueue vidq;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300259 struct au0828_dmaqueue vbiq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300260 struct au0828_usb_isoc_ctl isoc_ctl;
261 spinlock_t slock;
262
263 /* usb transfer */
264 int alt; /* alternate */
265 int max_pkt_size; /* max packet size of isoc transaction */
266 int num_alt; /* Number of alternative settings */
267 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
268 struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */
269 char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
270 transfer */
271
Mauro Carvalho Chehab29309772014-08-09 23:14:21 -0300272 /* DVB USB / URB Related */
273 bool urb_streaming, need_urb_start;
Steven Toth265a6512008-04-18 21:34:00 -0300274 struct urb *urbs[URB_COUNT];
Tim Mesterf251b3e2014-01-07 01:29:25 -0300275
276 /* Preallocated transfer digital transfer buffers */
277
278 char *dig_transfer_buffer[URB_COUNT];
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300279
280#ifdef CONFIG_MEDIA_CONTROLLER
281 struct media_device *media_dev;
282 struct media_pad video_pad, vbi_pad;
Mauro Carvalho Chehab0158e7b2015-06-18 13:06:38 -0300283 struct media_entity *decoder;
Mauro Carvalho Chehabd1f33732015-08-31 11:43:09 -0300284 struct media_entity input_ent[AU0828_MAX_INPUT];
285 struct media_pad input_pad[AU0828_MAX_INPUT];
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300286#endif
Steven Toth265a6512008-04-18 21:34:00 -0300287};
288
Shuah Khan05439b12015-01-29 13:41:32 -0300289
Steven Toth265a6512008-04-18 21:34:00 -0300290/* ----------------------------------------------------------- */
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300291#define au0828_read(dev, reg) au0828_readreg(dev, reg)
292#define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
293#define au0828_andor(dev, reg, mask, value) \
294 au0828_writereg(dev, reg, \
295 (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
Steven Toth265a6512008-04-18 21:34:00 -0300296
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300297#define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
298#define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
Steven Toth265a6512008-04-18 21:34:00 -0300299
300/* ----------------------------------------------------------- */
301/* au0828-core.c */
302extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
303extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300304extern int au0828_debug;
Steven Toth265a6512008-04-18 21:34:00 -0300305
306/* ----------------------------------------------------------- */
307/* au0828-cards.c */
308extern struct au0828_board au0828_boards[];
309extern struct usb_device_id au0828_usb_id_table[];
Steven Toth265a6512008-04-18 21:34:00 -0300310extern void au0828_gpio_setup(struct au0828_dev *dev);
Michael Krufkyd7cba042008-09-12 13:31:45 -0300311extern int au0828_tuner_callback(void *priv, int component,
312 int command, int arg);
Steven Toth28930fa2008-03-29 19:53:07 -0300313extern void au0828_card_setup(struct au0828_dev *dev);
Steven Toth265a6512008-04-18 21:34:00 -0300314
315/* ----------------------------------------------------------- */
316/* au0828-i2c.c */
317extern int au0828_i2c_register(struct au0828_dev *dev);
318extern int au0828_i2c_unregister(struct au0828_dev *dev);
Steven Toth265a6512008-04-18 21:34:00 -0300319
320/* ----------------------------------------------------------- */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300321/* au0828-video.c */
Shuah Khan05439b12015-01-29 13:41:32 -0300322extern int au0828_analog_register(struct au0828_dev *dev,
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -0300323 struct usb_interface *interface);
Shuah Khan05439b12015-01-29 13:41:32 -0300324extern void au0828_analog_unregister(struct au0828_dev *dev);
325extern int au0828_start_analog_streaming(struct vb2_queue *vq,
326 unsigned int count);
327extern void au0828_stop_vbi_streaming(struct vb2_queue *vq);
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -0300328#ifdef CONFIG_VIDEO_AU0828_V4L2
Shuah Khan05439b12015-01-29 13:41:32 -0300329extern void au0828_v4l2_suspend(struct au0828_dev *dev);
330extern void au0828_v4l2_resume(struct au0828_dev *dev);
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -0300331#else
332static inline void au0828_v4l2_suspend(struct au0828_dev *dev) { };
333static inline void au0828_v4l2_resume(struct au0828_dev *dev) { };
334#endif
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300335
336/* ----------------------------------------------------------- */
Steven Toth265a6512008-04-18 21:34:00 -0300337/* au0828-dvb.c */
338extern int au0828_dvb_register(struct au0828_dev *dev);
339extern void au0828_dvb_unregister(struct au0828_dev *dev);
Mauro Carvalho Chehabb799de72014-08-09 21:47:13 -0300340void au0828_dvb_suspend(struct au0828_dev *dev);
341void au0828_dvb_resume(struct au0828_dev *dev);
Steven Tothbc3c6132008-04-18 21:39:11 -0300342
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300343/* au0828-vbi.c */
Shuah Khan05439b12015-01-29 13:41:32 -0300344extern struct vb2_ops au0828_vbi_qops;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300345
Steven Tothbc3c6132008-04-18 21:39:11 -0300346#define dprintk(level, fmt, arg...)\
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300347 do { if (au0828_debug & level)\
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300348 printk(KERN_DEBUG pr_fmt(fmt), ## arg);\
Steven Tothbc3c6132008-04-18 21:39:11 -0300349 } while (0)
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300350
351/* au0828-input.c */
Shuah Khan917cbcd2014-08-08 21:36:18 -0300352#ifdef CONFIG_VIDEO_AU0828_RC
353extern int au0828_rc_register(struct au0828_dev *dev);
354extern void au0828_rc_unregister(struct au0828_dev *dev);
355extern int au0828_rc_suspend(struct au0828_dev *dev);
356extern int au0828_rc_resume(struct au0828_dev *dev);
357#else
358static inline int au0828_rc_register(struct au0828_dev *dev) { return 0; }
359static inline void au0828_rc_unregister(struct au0828_dev *dev) { }
360static inline int au0828_rc_suspend(struct au0828_dev *dev) { return 0; }
361static inline int au0828_rc_resume(struct au0828_dev *dev) { return 0; }
362#endif