blob: ad400482b2f316a0b5df1fcefc7d0dd0851ebed2 [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
22#include <linux/usb.h>
23#include <linux/i2c.h>
24#include <linux/i2c-algo-bit.h>
Steven Toth28930fa2008-03-29 19:53:07 -030025#include <media/tveeprom.h>
Steven Toth265a6512008-04-18 21:34:00 -030026
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030027/* Analog */
28#include <linux/videodev2.h>
29#include <media/videobuf-vmalloc.h>
Devin Heitmuellerb14667f2009-03-11 03:01:04 -030030#include <media/v4l2-device.h>
Hans Verkuile8c26f42013-02-15 08:55:41 -030031#include <media/v4l2-ctrls.h>
Hans Verkuil83b09422013-02-15 09:14:00 -030032#include <media/v4l2-fh.h>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030033
Steven Toth265a6512008-04-18 21:34:00 -030034/* DVB */
35#include "demux.h"
36#include "dmxdev.h"
37#include "dvb_demux.h"
38#include "dvb_frontend.h"
39#include "dvb_net.h"
40#include "dvbdev.h"
41
42#include "au0828-reg.h"
43#include "au0828-cards.h"
44
45#define DRIVER_NAME "au0828"
46#define URB_COUNT 16
Steven Toth265a6512008-04-18 21:34:00 -030047#define URB_BUFSIZE (0xe522)
48
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030049/* Analog constants */
50#define NTSC_STD_W 720
51#define NTSC_STD_H 480
52
53#define AU0828_INTERLACED_DEFAULT 1
54#define V4L2_CID_PRIVATE_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
55
56/* Defination for AU0828 USB transfer */
57#define AU0828_MAX_ISO_BUFS 12 /* maybe resize this value in the future */
Devin Heitmueller83f859c2010-06-25 01:33:39 -030058#define AU0828_ISO_PACKETS_PER_URB 128
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030059
60#define AU0828_MIN_BUF 4
61#define AU0828_DEF_BUF 8
62
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030063#define AU0828_MAX_INPUT 4
64
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -030065/* au0828 resource types (used for res_get/res_lock etc */
66#define AU0828_RESOURCE_VIDEO 0x01
67#define AU0828_RESOURCE_VBI 0x02
68
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030069enum au0828_itype {
Devin Heitmueller220be772009-03-11 03:01:01 -030070 AU0828_VMUX_UNDEFINED = 0,
71 AU0828_VMUX_COMPOSITE,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030072 AU0828_VMUX_SVIDEO,
73 AU0828_VMUX_CABLE,
74 AU0828_VMUX_TELEVISION,
75 AU0828_VMUX_DVB,
76 AU0828_VMUX_DEBUG
77};
78
79struct au0828_input {
80 enum au0828_itype type;
81 unsigned int vmux;
82 unsigned int amux;
83 void (*audio_setup) (void *priv, int enable);
84};
85
Steven Toth265a6512008-04-18 21:34:00 -030086struct au0828_board {
87 char *name;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -030088 unsigned int tuner_type;
89 unsigned char tuner_addr;
Devin Heitmueller16af6f52009-04-01 00:11:31 -030090 unsigned char i2c_clk_divider;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030091 struct au0828_input input[AU0828_MAX_INPUT];
92
Steven Toth265a6512008-04-18 21:34:00 -030093};
94
95struct 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};
106
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300107enum au0828_stream_state {
108 STREAM_OFF,
109 STREAM_INTERRUPT,
110 STREAM_ON
111};
112
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300113#define AUVI_INPUT(nr) (dev->board.input[nr])
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300114
115/* device state */
116enum au0828_dev_state {
117 DEV_INITIALIZED = 0x01,
118 DEV_DISCONNECTED = 0x02,
119 DEV_MISCONFIGURED = 0x04
120};
121
122struct au0828_fh {
Hans Verkuil83b09422013-02-15 09:14:00 -0300123 /* must be the first field of this struct! */
124 struct v4l2_fh fh;
125
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300126 struct au0828_dev *dev;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300127 unsigned int resources;
128
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300129 struct videobuf_queue vb_vidq;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300130 struct videobuf_queue vb_vbiq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300131 enum v4l2_buf_type type;
132};
133
134struct au0828_usb_isoc_ctl {
135 /* max packet size of isoc transaction */
136 int max_pkt_size;
137
138 /* number of allocated urbs */
139 int num_bufs;
140
141 /* urb for isoc transfers */
142 struct urb **urb;
143
144 /* transfer buffers for isoc transfer */
145 char **transfer_buffer;
146
147 /* Last buffer command and region */
148 u8 cmd;
149 int pos, size, pktsize;
150
151 /* Last field: ODD or EVEN? */
152 int field;
153
154 /* Stores incomplete commands */
155 u32 tmp_buf;
156 int tmp_buf_len;
157
158 /* Stores already requested buffers */
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300159 struct au0828_buffer *buf;
160 struct au0828_buffer *vbi_buf;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300161
162 /* Stores the number of received fields */
163 int nfields;
164
165 /* isoc urb callback */
166 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
167
168};
169
170/* buffer for one video frame */
171struct au0828_buffer {
172 /* common v4l buffer stuff -- must be first */
173 struct videobuf_buffer vb;
174
175 struct list_head frame;
176 int top_field;
177 int receiving;
178};
179
180struct au0828_dmaqueue {
181 struct list_head active;
182 struct list_head queued;
183
184 wait_queue_head_t wq;
185
186 /* Counters to control buffer fill */
187 int pos;
188};
189
Steven Toth265a6512008-04-18 21:34:00 -0300190struct au0828_dev {
191 struct mutex mutex;
192 struct usb_device *usbdev;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300193 int boardnr;
194 struct au0828_board board;
Steven Toth265a6512008-04-18 21:34:00 -0300195 u8 ctrlmsg[64];
196
197 /* I2C */
198 struct i2c_adapter i2c_adap;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300199 struct i2c_algorithm i2c_algo;
Steven Toth265a6512008-04-18 21:34:00 -0300200 struct i2c_client i2c_client;
201 u32 i2c_rc;
202
203 /* Digital */
204 struct au0828_dvb dvb;
Devin Heitmueller43f2ccc2012-08-06 22:46:53 -0300205 struct work_struct restart_streaming;
Steven Toth265a6512008-04-18 21:34:00 -0300206
Michael Krufky8a4e7862012-12-04 11:30:00 -0300207#ifdef CONFIG_VIDEO_AU0828_V4L2
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300208 /* Analog */
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300209 struct v4l2_device v4l2_dev;
Hans Verkuile8c26f42013-02-15 08:55:41 -0300210 struct v4l2_ctrl_handler v4l2_ctrl_hdl;
Michael Krufky8a4e7862012-12-04 11:30:00 -0300211#endif
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300212 int users;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300213 unsigned int resources; /* resources in use */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300214 struct video_device *vdev;
215 struct video_device *vbi_dev;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300216 struct timer_list vid_timeout;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300217 int vid_timeout_running;
Devin Heitmueller6e04b7b2010-09-01 22:03:43 -0300218 struct timer_list vbi_timeout;
Devin Heitmueller78ca5002010-10-09 14:43:53 -0300219 int vbi_timeout_running;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300220 int width;
221 int height;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300222 int vbi_width;
223 int vbi_height;
224 u32 vbi_read;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300225 u32 field_size;
226 u32 frame_size;
227 u32 bytesperline;
228 int type;
229 u8 ctrl_ainput;
230 __u8 isoc_in_endpointaddr;
231 u8 isoc_init_ok;
232 int greenscreen_detected;
233 unsigned int frame_count;
234 int ctrl_freq;
235 int input_type;
Devin Heitmuellerc98bc032012-08-06 22:46:59 -0300236 int std_set_in_tuner_core;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300237 unsigned int ctrl_input;
238 enum au0828_dev_state dev_state;
239 enum au0828_stream_state stream_state;
240 wait_queue_head_t open;
241
242 struct mutex lock;
243
244 /* Isoc control struct */
245 struct au0828_dmaqueue vidq;
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300246 struct au0828_dmaqueue vbiq;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300247 struct au0828_usb_isoc_ctl isoc_ctl;
248 spinlock_t slock;
249
250 /* usb transfer */
251 int alt; /* alternate */
252 int max_pkt_size; /* max packet size of isoc transaction */
253 int num_alt; /* Number of alternative settings */
254 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
255 struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */
256 char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
257 transfer */
258
Steven Toth265a6512008-04-18 21:34:00 -0300259 /* USB / URB Related */
260 int urb_streaming;
261 struct urb *urbs[URB_COUNT];
Steven Toth265a6512008-04-18 21:34:00 -0300262};
263
264/* ----------------------------------------------------------- */
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300265#define au0828_read(dev, reg) au0828_readreg(dev, reg)
266#define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
267#define au0828_andor(dev, reg, mask, value) \
268 au0828_writereg(dev, reg, \
269 (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
Steven Toth265a6512008-04-18 21:34:00 -0300270
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300271#define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
272#define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
Steven Toth265a6512008-04-18 21:34:00 -0300273
274/* ----------------------------------------------------------- */
275/* au0828-core.c */
276extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
277extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300278extern int au0828_debug;
Steven Toth265a6512008-04-18 21:34:00 -0300279
280/* ----------------------------------------------------------- */
281/* au0828-cards.c */
282extern struct au0828_board au0828_boards[];
283extern struct usb_device_id au0828_usb_id_table[];
Steven Toth265a6512008-04-18 21:34:00 -0300284extern void au0828_gpio_setup(struct au0828_dev *dev);
Michael Krufkyd7cba042008-09-12 13:31:45 -0300285extern int au0828_tuner_callback(void *priv, int component,
286 int command, int arg);
Steven Toth28930fa2008-03-29 19:53:07 -0300287extern void au0828_card_setup(struct au0828_dev *dev);
Steven Toth265a6512008-04-18 21:34:00 -0300288
289/* ----------------------------------------------------------- */
290/* au0828-i2c.c */
291extern int au0828_i2c_register(struct au0828_dev *dev);
292extern int au0828_i2c_unregister(struct au0828_dev *dev);
Steven Toth265a6512008-04-18 21:34:00 -0300293
294/* ----------------------------------------------------------- */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300295/* au0828-video.c */
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -0300296int au0828_analog_register(struct au0828_dev *dev,
297 struct usb_interface *interface);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300298int au0828_analog_stream_disable(struct au0828_dev *d);
299void au0828_analog_unregister(struct au0828_dev *dev);
300
301/* ----------------------------------------------------------- */
Steven Toth265a6512008-04-18 21:34:00 -0300302/* au0828-dvb.c */
303extern int au0828_dvb_register(struct au0828_dev *dev);
304extern void au0828_dvb_unregister(struct au0828_dev *dev);
Steven Tothbc3c6132008-04-18 21:39:11 -0300305
Devin Heitmueller7f8eacd2010-05-29 17:18:45 -0300306/* au0828-vbi.c */
307extern struct videobuf_queue_ops au0828_vbi_qops;
308
Steven Tothbc3c6132008-04-18 21:39:11 -0300309#define dprintk(level, fmt, arg...)\
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300310 do { if (au0828_debug & level)\
Steven Tothbc3c6132008-04-18 21:39:11 -0300311 printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\
312 } while (0)