Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Auvitek AU0828 USB bridge |
| 3 | * |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 4 | * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org> |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 5 | * |
| 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 Toth | 28930fa | 2008-03-29 19:53:07 -0300 | [diff] [blame] | 25 | #include <media/tveeprom.h> |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 26 | |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 27 | /* Analog */ |
| 28 | #include <linux/videodev2.h> |
| 29 | #include <media/videobuf-vmalloc.h> |
Devin Heitmueller | b14667f | 2009-03-11 03:01:04 -0300 | [diff] [blame] | 30 | #include <media/v4l2-device.h> |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 31 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 32 | /* 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 Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 45 | #define URB_BUFSIZE (0xe522) |
| 46 | |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 47 | /* 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 */ |
Devin Heitmueller | 83f859c | 2010-06-25 01:33:39 -0300 | [diff] [blame^] | 56 | #define AU0828_ISO_PACKETS_PER_URB 128 |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 57 | |
| 58 | #define AU0828_MIN_BUF 4 |
| 59 | #define AU0828_DEF_BUF 8 |
| 60 | |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 61 | #define AU0828_MAX_INPUT 4 |
| 62 | |
Devin Heitmueller | 7f8eacd | 2010-05-29 17:18:45 -0300 | [diff] [blame] | 63 | /* au0828 resource types (used for res_get/res_lock etc */ |
| 64 | #define AU0828_RESOURCE_VIDEO 0x01 |
| 65 | #define AU0828_RESOURCE_VBI 0x02 |
| 66 | |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 67 | enum au0828_itype { |
Devin Heitmueller | 220be77 | 2009-03-11 03:01:01 -0300 | [diff] [blame] | 68 | AU0828_VMUX_UNDEFINED = 0, |
| 69 | AU0828_VMUX_COMPOSITE, |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 70 | AU0828_VMUX_SVIDEO, |
| 71 | AU0828_VMUX_CABLE, |
| 72 | AU0828_VMUX_TELEVISION, |
| 73 | AU0828_VMUX_DVB, |
| 74 | AU0828_VMUX_DEBUG |
| 75 | }; |
| 76 | |
| 77 | struct 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 Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 84 | struct au0828_board { |
| 85 | char *name; |
Devin Heitmueller | f1add5b | 2009-03-11 03:00:47 -0300 | [diff] [blame] | 86 | unsigned int tuner_type; |
| 87 | unsigned char tuner_addr; |
Devin Heitmueller | 16af6f5 | 2009-04-01 00:11:31 -0300 | [diff] [blame] | 88 | unsigned char i2c_clk_divider; |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 89 | struct au0828_input input[AU0828_MAX_INPUT]; |
| 90 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct au0828_dvb { |
| 94 | struct mutex lock; |
| 95 | struct dvb_adapter adapter; |
| 96 | struct dvb_frontend *frontend; |
| 97 | struct dvb_demux demux; |
| 98 | struct dmxdev dmxdev; |
| 99 | struct dmx_frontend fe_hw; |
| 100 | struct dmx_frontend fe_mem; |
| 101 | struct dvb_net net; |
| 102 | int feeding; |
| 103 | }; |
| 104 | |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 105 | enum au0828_stream_state { |
| 106 | STREAM_OFF, |
| 107 | STREAM_INTERRUPT, |
| 108 | STREAM_ON |
| 109 | }; |
| 110 | |
Devin Heitmueller | f1add5b | 2009-03-11 03:00:47 -0300 | [diff] [blame] | 111 | #define AUVI_INPUT(nr) (dev->board.input[nr]) |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 112 | |
| 113 | /* device state */ |
| 114 | enum au0828_dev_state { |
| 115 | DEV_INITIALIZED = 0x01, |
| 116 | DEV_DISCONNECTED = 0x02, |
| 117 | DEV_MISCONFIGURED = 0x04 |
| 118 | }; |
| 119 | |
| 120 | struct au0828_fh { |
| 121 | struct au0828_dev *dev; |
Devin Heitmueller | 7f8eacd | 2010-05-29 17:18:45 -0300 | [diff] [blame] | 122 | unsigned int resources; |
| 123 | |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 124 | struct videobuf_queue vb_vidq; |
Devin Heitmueller | 7f8eacd | 2010-05-29 17:18:45 -0300 | [diff] [blame] | 125 | struct videobuf_queue vb_vbiq; |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 126 | enum v4l2_buf_type type; |
| 127 | }; |
| 128 | |
| 129 | struct au0828_usb_isoc_ctl { |
| 130 | /* max packet size of isoc transaction */ |
| 131 | int max_pkt_size; |
| 132 | |
| 133 | /* number of allocated urbs */ |
| 134 | int num_bufs; |
| 135 | |
| 136 | /* urb for isoc transfers */ |
| 137 | struct urb **urb; |
| 138 | |
| 139 | /* transfer buffers for isoc transfer */ |
| 140 | char **transfer_buffer; |
| 141 | |
| 142 | /* Last buffer command and region */ |
| 143 | u8 cmd; |
| 144 | int pos, size, pktsize; |
| 145 | |
| 146 | /* Last field: ODD or EVEN? */ |
| 147 | int field; |
| 148 | |
| 149 | /* Stores incomplete commands */ |
| 150 | u32 tmp_buf; |
| 151 | int tmp_buf_len; |
| 152 | |
| 153 | /* Stores already requested buffers */ |
Devin Heitmueller | 7f8eacd | 2010-05-29 17:18:45 -0300 | [diff] [blame] | 154 | struct au0828_buffer *buf; |
| 155 | struct au0828_buffer *vbi_buf; |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 156 | |
| 157 | /* Stores the number of received fields */ |
| 158 | int nfields; |
| 159 | |
| 160 | /* isoc urb callback */ |
| 161 | int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb); |
| 162 | |
| 163 | }; |
| 164 | |
| 165 | /* buffer for one video frame */ |
| 166 | struct au0828_buffer { |
| 167 | /* common v4l buffer stuff -- must be first */ |
| 168 | struct videobuf_buffer vb; |
| 169 | |
| 170 | struct list_head frame; |
| 171 | int top_field; |
| 172 | int receiving; |
| 173 | }; |
| 174 | |
| 175 | struct au0828_dmaqueue { |
| 176 | struct list_head active; |
| 177 | struct list_head queued; |
| 178 | |
| 179 | wait_queue_head_t wq; |
| 180 | |
| 181 | /* Counters to control buffer fill */ |
| 182 | int pos; |
| 183 | }; |
| 184 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 185 | struct au0828_dev { |
| 186 | struct mutex mutex; |
| 187 | struct usb_device *usbdev; |
Devin Heitmueller | f1add5b | 2009-03-11 03:00:47 -0300 | [diff] [blame] | 188 | int boardnr; |
| 189 | struct au0828_board board; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 190 | u8 ctrlmsg[64]; |
| 191 | |
| 192 | /* I2C */ |
| 193 | struct i2c_adapter i2c_adap; |
Devin Heitmueller | b14667f | 2009-03-11 03:01:04 -0300 | [diff] [blame] | 194 | struct i2c_algorithm i2c_algo; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 195 | struct i2c_client i2c_client; |
| 196 | u32 i2c_rc; |
| 197 | |
| 198 | /* Digital */ |
| 199 | struct au0828_dvb dvb; |
| 200 | |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 201 | /* Analog */ |
Devin Heitmueller | b14667f | 2009-03-11 03:01:04 -0300 | [diff] [blame] | 202 | struct v4l2_device v4l2_dev; |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 203 | int users; |
Devin Heitmueller | 7f8eacd | 2010-05-29 17:18:45 -0300 | [diff] [blame] | 204 | unsigned int resources; /* resources in use */ |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 205 | struct video_device *vdev; |
| 206 | struct video_device *vbi_dev; |
| 207 | int width; |
| 208 | int height; |
Devin Heitmueller | 7f8eacd | 2010-05-29 17:18:45 -0300 | [diff] [blame] | 209 | int vbi_width; |
| 210 | int vbi_height; |
| 211 | u32 vbi_read; |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 212 | u32 field_size; |
| 213 | u32 frame_size; |
| 214 | u32 bytesperline; |
| 215 | int type; |
| 216 | u8 ctrl_ainput; |
| 217 | __u8 isoc_in_endpointaddr; |
| 218 | u8 isoc_init_ok; |
| 219 | int greenscreen_detected; |
| 220 | unsigned int frame_count; |
| 221 | int ctrl_freq; |
| 222 | int input_type; |
| 223 | unsigned int ctrl_input; |
| 224 | enum au0828_dev_state dev_state; |
| 225 | enum au0828_stream_state stream_state; |
| 226 | wait_queue_head_t open; |
| 227 | |
| 228 | struct mutex lock; |
| 229 | |
| 230 | /* Isoc control struct */ |
| 231 | struct au0828_dmaqueue vidq; |
Devin Heitmueller | 7f8eacd | 2010-05-29 17:18:45 -0300 | [diff] [blame] | 232 | struct au0828_dmaqueue vbiq; |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 233 | struct au0828_usb_isoc_ctl isoc_ctl; |
| 234 | spinlock_t slock; |
| 235 | |
| 236 | /* usb transfer */ |
| 237 | int alt; /* alternate */ |
| 238 | int max_pkt_size; /* max packet size of isoc transaction */ |
| 239 | int num_alt; /* Number of alternative settings */ |
| 240 | unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */ |
| 241 | struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */ |
| 242 | char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc |
| 243 | transfer */ |
| 244 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 245 | /* USB / URB Related */ |
| 246 | int urb_streaming; |
| 247 | struct urb *urbs[URB_COUNT]; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | /* ----------------------------------------------------------- */ |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 251 | #define au0828_read(dev, reg) au0828_readreg(dev, reg) |
| 252 | #define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value) |
| 253 | #define au0828_andor(dev, reg, mask, value) \ |
| 254 | au0828_writereg(dev, reg, \ |
| 255 | (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask))) |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 256 | |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 257 | #define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit)) |
| 258 | #define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0) |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 259 | |
| 260 | /* ----------------------------------------------------------- */ |
| 261 | /* au0828-core.c */ |
| 262 | extern u32 au0828_read(struct au0828_dev *dev, u16 reg); |
| 263 | extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val); |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 264 | extern int au0828_debug; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 265 | |
| 266 | /* ----------------------------------------------------------- */ |
| 267 | /* au0828-cards.c */ |
| 268 | extern struct au0828_board au0828_boards[]; |
| 269 | extern struct usb_device_id au0828_usb_id_table[]; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 270 | extern void au0828_gpio_setup(struct au0828_dev *dev); |
Michael Krufky | d7cba04 | 2008-09-12 13:31:45 -0300 | [diff] [blame] | 271 | extern int au0828_tuner_callback(void *priv, int component, |
| 272 | int command, int arg); |
Steven Toth | 28930fa | 2008-03-29 19:53:07 -0300 | [diff] [blame] | 273 | extern void au0828_card_setup(struct au0828_dev *dev); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 274 | |
| 275 | /* ----------------------------------------------------------- */ |
| 276 | /* au0828-i2c.c */ |
| 277 | extern int au0828_i2c_register(struct au0828_dev *dev); |
| 278 | extern int au0828_i2c_unregister(struct au0828_dev *dev); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 279 | |
| 280 | /* ----------------------------------------------------------- */ |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 281 | /* au0828-video.c */ |
Devin Heitmueller | fc4ce6c | 2009-03-11 03:01:00 -0300 | [diff] [blame] | 282 | int au0828_analog_register(struct au0828_dev *dev, |
| 283 | struct usb_interface *interface); |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 284 | int au0828_analog_stream_disable(struct au0828_dev *d); |
| 285 | void au0828_analog_unregister(struct au0828_dev *dev); |
| 286 | |
| 287 | /* ----------------------------------------------------------- */ |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 288 | /* au0828-dvb.c */ |
| 289 | extern int au0828_dvb_register(struct au0828_dev *dev); |
| 290 | extern void au0828_dvb_unregister(struct au0828_dev *dev); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 291 | |
Devin Heitmueller | 7f8eacd | 2010-05-29 17:18:45 -0300 | [diff] [blame] | 292 | /* au0828-vbi.c */ |
| 293 | extern struct videobuf_queue_ops au0828_vbi_qops; |
| 294 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 295 | #define dprintk(level, fmt, arg...)\ |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 296 | do { if (au0828_debug & level)\ |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 297 | printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\ |
| 298 | } while (0) |