blob: 6ed1a6129731a540d5c73ad9df659c3f56e2b9b9 [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>
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030031
Steven Toth265a6512008-04-18 21:34:00 -030032/* DVB */
33#include "demux.h"
34#include "dmxdev.h"
35#include "dvb_demux.h"
36#include "dvb_frontend.h"
37#include "dvb_net.h"
38#include "dvbdev.h"
39
40#include "au0828-reg.h"
41#include "au0828-cards.h"
42
43#define DRIVER_NAME "au0828"
44#define URB_COUNT 16
Steven Toth265a6512008-04-18 21:34:00 -030045#define URB_BUFSIZE (0xe522)
46
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030047/* Analog constants */
48#define NTSC_STD_W 720
49#define NTSC_STD_H 480
50
51#define AU0828_INTERLACED_DEFAULT 1
52#define V4L2_CID_PRIVATE_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
53
54/* Defination for AU0828 USB transfer */
55#define AU0828_MAX_ISO_BUFS 12 /* maybe resize this value in the future */
56#define AU0828_ISO_PACKETS_PER_URB 10
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030057
58#define AU0828_MIN_BUF 4
59#define AU0828_DEF_BUF 8
60
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030061#define AU0828_MAX_INPUT 4
62
63enum au0828_itype {
Devin Heitmueller220be772009-03-11 03:01:01 -030064 AU0828_VMUX_UNDEFINED = 0,
65 AU0828_VMUX_COMPOSITE,
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030066 AU0828_VMUX_SVIDEO,
67 AU0828_VMUX_CABLE,
68 AU0828_VMUX_TELEVISION,
69 AU0828_VMUX_DVB,
70 AU0828_VMUX_DEBUG
71};
72
73struct au0828_input {
74 enum au0828_itype type;
75 unsigned int vmux;
76 unsigned int amux;
77 void (*audio_setup) (void *priv, int enable);
78};
79
Steven Toth265a6512008-04-18 21:34:00 -030080struct au0828_board {
81 char *name;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -030082 unsigned int tuner_type;
83 unsigned char tuner_addr;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -030084 struct au0828_input input[AU0828_MAX_INPUT];
85
Steven Toth265a6512008-04-18 21:34:00 -030086};
87
88struct au0828_dvb {
89 struct mutex lock;
90 struct dvb_adapter adapter;
91 struct dvb_frontend *frontend;
92 struct dvb_demux demux;
93 struct dmxdev dmxdev;
94 struct dmx_frontend fe_hw;
95 struct dmx_frontend fe_mem;
96 struct dvb_net net;
97 int feeding;
98};
99
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300100enum au0828_stream_state {
101 STREAM_OFF,
102 STREAM_INTERRUPT,
103 STREAM_ON
104};
105
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300106#define AUVI_INPUT(nr) (dev->board.input[nr])
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300107
108/* device state */
109enum au0828_dev_state {
110 DEV_INITIALIZED = 0x01,
111 DEV_DISCONNECTED = 0x02,
112 DEV_MISCONFIGURED = 0x04
113};
114
115struct au0828_fh {
116 struct au0828_dev *dev;
117 unsigned int stream_on:1; /* Locks streams */
118 struct videobuf_queue vb_vidq;
119 enum v4l2_buf_type type;
120};
121
122struct au0828_usb_isoc_ctl {
123 /* max packet size of isoc transaction */
124 int max_pkt_size;
125
126 /* number of allocated urbs */
127 int num_bufs;
128
129 /* urb for isoc transfers */
130 struct urb **urb;
131
132 /* transfer buffers for isoc transfer */
133 char **transfer_buffer;
134
135 /* Last buffer command and region */
136 u8 cmd;
137 int pos, size, pktsize;
138
139 /* Last field: ODD or EVEN? */
140 int field;
141
142 /* Stores incomplete commands */
143 u32 tmp_buf;
144 int tmp_buf_len;
145
146 /* Stores already requested buffers */
147 struct au0828_buffer *buf;
148
149 /* Stores the number of received fields */
150 int nfields;
151
152 /* isoc urb callback */
153 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
154
155};
156
157/* buffer for one video frame */
158struct au0828_buffer {
159 /* common v4l buffer stuff -- must be first */
160 struct videobuf_buffer vb;
161
162 struct list_head frame;
163 int top_field;
164 int receiving;
165};
166
167struct au0828_dmaqueue {
168 struct list_head active;
169 struct list_head queued;
170
171 wait_queue_head_t wq;
172
173 /* Counters to control buffer fill */
174 int pos;
175};
176
Steven Toth265a6512008-04-18 21:34:00 -0300177struct au0828_dev {
178 struct mutex mutex;
179 struct usb_device *usbdev;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300180 int boardnr;
181 struct au0828_board board;
Steven Toth265a6512008-04-18 21:34:00 -0300182 u8 ctrlmsg[64];
183
184 /* I2C */
185 struct i2c_adapter i2c_adap;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300186 struct i2c_algorithm i2c_algo;
Steven Toth265a6512008-04-18 21:34:00 -0300187 struct i2c_client i2c_client;
188 u32 i2c_rc;
189
190 /* Digital */
191 struct au0828_dvb dvb;
192
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300193 /* Analog */
194 struct list_head au0828list;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300195 struct v4l2_device v4l2_dev;
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300196 int users;
197 unsigned int stream_on:1; /* Locks streams */
198 struct video_device *vdev;
199 struct video_device *vbi_dev;
200 int width;
201 int height;
202 u32 field_size;
203 u32 frame_size;
204 u32 bytesperline;
205 int type;
206 u8 ctrl_ainput;
207 __u8 isoc_in_endpointaddr;
208 u8 isoc_init_ok;
209 int greenscreen_detected;
210 unsigned int frame_count;
211 int ctrl_freq;
212 int input_type;
213 unsigned int ctrl_input;
214 enum au0828_dev_state dev_state;
215 enum au0828_stream_state stream_state;
216 wait_queue_head_t open;
217
218 struct mutex lock;
219
220 /* Isoc control struct */
221 struct au0828_dmaqueue vidq;
222 struct au0828_usb_isoc_ctl isoc_ctl;
223 spinlock_t slock;
224
225 /* usb transfer */
226 int alt; /* alternate */
227 int max_pkt_size; /* max packet size of isoc transaction */
228 int num_alt; /* Number of alternative settings */
229 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
230 struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */
231 char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
232 transfer */
233
Steven Toth265a6512008-04-18 21:34:00 -0300234 /* USB / URB Related */
235 int urb_streaming;
236 struct urb *urbs[URB_COUNT];
Steven Toth265a6512008-04-18 21:34:00 -0300237};
238
239/* ----------------------------------------------------------- */
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300240#define au0828_read(dev, reg) au0828_readreg(dev, reg)
241#define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
242#define au0828_andor(dev, reg, mask, value) \
243 au0828_writereg(dev, reg, \
244 (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
Steven Toth265a6512008-04-18 21:34:00 -0300245
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300246#define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
247#define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
Steven Toth265a6512008-04-18 21:34:00 -0300248
249/* ----------------------------------------------------------- */
250/* au0828-core.c */
251extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
252extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300253extern int au0828_debug;
Steven Toth265a6512008-04-18 21:34:00 -0300254
255/* ----------------------------------------------------------- */
256/* au0828-cards.c */
257extern struct au0828_board au0828_boards[];
258extern struct usb_device_id au0828_usb_id_table[];
Steven Toth265a6512008-04-18 21:34:00 -0300259extern void au0828_gpio_setup(struct au0828_dev *dev);
Michael Krufkyd7cba042008-09-12 13:31:45 -0300260extern int au0828_tuner_callback(void *priv, int component,
261 int command, int arg);
Steven Toth28930fa2008-03-29 19:53:07 -0300262extern void au0828_card_setup(struct au0828_dev *dev);
Steven Toth265a6512008-04-18 21:34:00 -0300263
264/* ----------------------------------------------------------- */
265/* au0828-i2c.c */
266extern int au0828_i2c_register(struct au0828_dev *dev);
267extern int au0828_i2c_unregister(struct au0828_dev *dev);
Steven Toth265a6512008-04-18 21:34:00 -0300268
269/* ----------------------------------------------------------- */
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300270/* au0828-video.c */
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -0300271int au0828_analog_register(struct au0828_dev *dev,
272 struct usb_interface *interface);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300273int au0828_analog_stream_disable(struct au0828_dev *d);
274void au0828_analog_unregister(struct au0828_dev *dev);
275
276/* ----------------------------------------------------------- */
Steven Toth265a6512008-04-18 21:34:00 -0300277/* au0828-dvb.c */
278extern int au0828_dvb_register(struct au0828_dev *dev);
279extern void au0828_dvb_unregister(struct au0828_dev *dev);
Steven Tothbc3c6132008-04-18 21:39:11 -0300280
281#define dprintk(level, fmt, arg...)\
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300282 do { if (au0828_debug & level)\
Steven Tothbc3c6132008-04-18 21:39:11 -0300283 printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\
284 } while (0)