2 em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Sascha Sommer <saschasommer@freenet.de>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/usb.h>
28 #include <linux/vmalloc.h>
32 /* #define ENABLE_DEBUG_ISOC_FRAMES */
34 static unsigned int core_debug;
35 module_param(core_debug,int,0644);
36 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
38 #define em28xx_coredbg(fmt, arg...) do {\
40 printk(KERN_INFO "%s %s :"fmt, \
41 dev->name, __func__ , ##arg); } while (0)
43 static unsigned int reg_debug;
44 module_param(reg_debug,int,0644);
45 MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
47 #define em28xx_regdbg(fmt, arg...) do {\
49 printk(KERN_INFO "%s %s :"fmt, \
50 dev->name, __func__ , ##arg); } while (0)
52 static int alt = EM28XX_PINOUT;
53 module_param(alt, int, 0644);
54 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
57 * em28xx_read_reg_req()
58 * reads data from the usb device specifying bRequest
60 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
65 if (dev->state & DEV_DISCONNECTED)
68 em28xx_regdbg("req=%02x, reg=%02x ", req, reg);
70 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
71 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
72 0x0000, reg, buf, len, HZ);
75 printk(ret < 0 ? " failed!\n" : "%02x values: ", ret);
76 for (byte = 0; byte < len; byte++) {
77 printk(" %02x", (unsigned char)buf[byte]);
86 * em28xx_read_reg_req()
87 * reads data from the usb device specifying bRequest
89 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
94 if (dev->state & DEV_DISCONNECTED)
97 em28xx_regdbg("req=%02x, reg=%02x:", req, reg);
99 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
100 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
101 0x0000, reg, &val, 1, HZ);
104 printk(ret < 0 ? " failed!\n" :
105 "%02x\n", (unsigned char) val);
113 int em28xx_read_reg(struct em28xx *dev, u16 reg)
115 return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
119 * em28xx_write_regs_req()
120 * sends data to the usb device, specifying bRequest
122 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
127 /*usb_control_msg seems to expect a kmalloced buffer */
130 if (dev->state & DEV_DISCONNECTED)
133 bufs = kmalloc(len, GFP_KERNEL);
135 em28xx_regdbg("req=%02x reg=%02x:", req, reg);
139 for (i = 0; i < len; ++i)
140 printk (" %02x", (unsigned char)buf[i]);
146 memcpy(bufs, buf, len);
147 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
148 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
149 0x0000, reg, bufs, len, HZ);
150 msleep(5); /* FIXME: magic number */
155 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
157 return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
161 * em28xx_write_reg_bits()
162 * sets only some bits (specified by bitmask) of a register, by first reading
165 static int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
170 if ((oldval = em28xx_read_reg(dev, reg)) < 0)
172 newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
173 return em28xx_write_regs(dev, reg, &newval, 1);
177 * em28xx_write_ac97()
178 * write a 16 bit value to the specified AC97 address (LSB first!)
180 static int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 *val)
183 u8 addr = reg & 0x7f;
184 if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
186 if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
189 /* Wait up to 50 ms for AC97 command to complete */
190 for (i = 0; i < 10; i++) {
191 if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0)
197 em28xx_warn ("AC97 command still being executed: not handled properly!\n");
201 static int em28xx_set_audio_source(struct em28xx *dev)
203 static char *enable = "\x08\x08";
204 static char *disable = "\x08\x88";
205 char *video = enable, *line = disable;
209 if (dev->is_em2800) {
211 input = EM2800_AUDIO_SRC_LINE;
213 input = EM2800_AUDIO_SRC_TUNER;
215 ret = em28xx_write_regs(dev, EM2800_AUDIOSRC_REG, &input, 1);
220 if (dev->has_msp34xx)
221 input = EM28XX_AUDIO_SRC_TUNER;
223 switch (dev->ctl_ainput) {
224 case EM28XX_AMUX_VIDEO:
225 input = EM28XX_AUDIO_SRC_TUNER;
227 case EM28XX_AMUX_LINE_IN:
228 input = EM28XX_AUDIO_SRC_LINE;
230 case EM28XX_AMUX_AC97_VIDEO:
231 input = EM28XX_AUDIO_SRC_LINE;
233 case EM28XX_AMUX_AC97_LINE_IN:
234 input = EM28XX_AUDIO_SRC_LINE;
241 ret = em28xx_write_reg_bits(dev, AUDIOSRC_REG, input, 0xc0);
246 /* Sets AC97 mixer registers
247 This is seems to be needed, even for non-ac97 configs
249 ret = em28xx_write_ac97(dev, VIDEO_AC97, video);
253 ret = em28xx_write_ac97(dev, LINE_IN_AC97, line);
258 int em28xx_audio_analog_set(struct em28xx *dev)
261 char s[2] = { 0x00, 0x00 };
264 s[0] |= 0x1f - dev->volume;
265 s[1] |= 0x1f - dev->volume;
269 ret = em28xx_write_ac97(dev, MASTER_AC97, s);
274 if (dev->has_12mhz_i2s)
280 ret = em28xx_write_reg_bits(dev, XCLK_REG, xclk, 0xa7);
285 /* Selects the proper audio input */
286 ret = em28xx_set_audio_source(dev);
291 ret = em28xx_write_ac97(dev, MASTER_AC97, s);
295 EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
297 int em28xx_colorlevels_set_default(struct em28xx *dev)
299 em28xx_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
300 em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
301 em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
302 em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
303 em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
304 em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
306 em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
307 em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
308 em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
309 em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
310 em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
311 em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
312 return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
315 int em28xx_capture_start(struct em28xx *dev, int start)
318 /* FIXME: which is the best order? */
319 /* video registers are sampled by VREF */
320 if ((ret = em28xx_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
323 /* enable video capture */
324 return em28xx_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
327 int em28xx_outfmt_set_yuv422(struct em28xx *dev)
329 em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
330 em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
331 return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
334 static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
337 em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
339 em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
340 em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
341 em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
342 return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
345 static int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
346 u16 width, u16 height)
350 u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
352 em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
353 (height | (overflow & 1) << 8));
355 em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
356 em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
357 em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
358 em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
359 return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
362 static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
365 /* the em2800 scaler only supports scaling down to 50% */
367 mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
372 em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
375 em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
376 /* it seems that both H and V scalers must be active to work correctly */
377 mode = (h || v)? 0x30: 0x00;
379 return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
382 /* FIXME: this only function read values from dev */
383 int em28xx_resolution_set(struct em28xx *dev)
386 width = norm_maxw(dev);
387 height = norm_maxh(dev) >> 1;
389 em28xx_outfmt_set_yuv422(dev);
390 em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
391 em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
392 return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
395 int em28xx_set_alternate(struct em28xx *dev)
397 int errCode, prev_alt = dev->alt;
399 unsigned int min_pkt_size = dev->bytesperline + 4;
401 /* When image size is bigger than a certain value,
402 the frame size should be increased, otherwise, only
403 green screen will be received.
405 if (dev->frame_size > 720*240*2)
408 for (i = 0; i < dev->num_alt; i++) {
409 /* stop when the selected alt setting offers enough bandwidth */
410 if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
413 /* otherwise make sure that we end up with the maximum bandwidth
414 because the min_pkt_size equation might be wrong...
416 } else if (dev->alt_max_pkt_size[i] >
417 dev->alt_max_pkt_size[dev->alt])
421 if (dev->alt != prev_alt) {
422 em28xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
423 min_pkt_size, dev->alt);
424 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
425 em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
426 dev->alt, dev->max_pkt_size);
427 errCode = usb_set_interface(dev->udev, 0, dev->alt);
429 em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",