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