Path: blob/master/drivers/media/video/em28xx/em28xx-core.c
17778 views
/*1em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices23Copyright (C) 2005 Ludovico Cavedon <[email protected]>4Markus Rechberger <[email protected]>5Mauro Carvalho Chehab <[email protected]>6Sascha Sommer <[email protected]>78This program is free software; you can redistribute it and/or modify9it under the terms of the GNU General Public License as published by10the Free Software Foundation; either version 2 of the License, or11(at your option) any later version.1213This program is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16GNU General Public License for more details.1718You should have received a copy of the GNU General Public License19along with this program; if not, write to the Free Software20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.21*/2223#include <linux/init.h>24#include <linux/list.h>25#include <linux/module.h>26#include <linux/slab.h>27#include <linux/usb.h>28#include <linux/vmalloc.h>29#include <media/v4l2-common.h>3031#include "em28xx.h"3233/* #define ENABLE_DEBUG_ISOC_FRAMES */3435static unsigned int core_debug;36module_param(core_debug, int, 0644);37MODULE_PARM_DESC(core_debug, "enable debug messages [core]");3839#define em28xx_coredbg(fmt, arg...) do {\40if (core_debug) \41printk(KERN_INFO "%s %s :"fmt, \42dev->name, __func__ , ##arg); } while (0)4344static unsigned int reg_debug;45module_param(reg_debug, int, 0644);46MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");4748#define em28xx_regdbg(fmt, arg...) do {\49if (reg_debug) \50printk(KERN_INFO "%s %s :"fmt, \51dev->name, __func__ , ##arg); } while (0)5253static int alt;54module_param(alt, int, 0644);55MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");5657static unsigned int disable_vbi;58module_param(disable_vbi, int, 0644);59MODULE_PARM_DESC(disable_vbi, "disable vbi support");6061/* FIXME */62#define em28xx_isocdbg(fmt, arg...) do {\63if (core_debug) \64printk(KERN_INFO "%s %s :"fmt, \65dev->name, __func__ , ##arg); } while (0)6667/*68* em28xx_read_reg_req()69* reads data from the usb device specifying bRequest70*/71int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,72char *buf, int len)73{74int ret;75int pipe = usb_rcvctrlpipe(dev->udev, 0);7677if (dev->state & DEV_DISCONNECTED)78return -ENODEV;7980if (len > URB_MAX_CTRL_SIZE)81return -EINVAL;8283if (reg_debug) {84printk(KERN_DEBUG "(pipe 0x%08x): "85"IN: %02x %02x %02x %02x %02x %02x %02x %02x ",86pipe,87USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,88req, 0, 0,89reg & 0xff, reg >> 8,90len & 0xff, len >> 8);91}9293mutex_lock(&dev->ctrl_urb_lock);94ret = usb_control_msg(dev->udev, pipe, req,95USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,960x0000, reg, dev->urb_buf, len, HZ);97if (ret < 0) {98if (reg_debug)99printk(" failed!\n");100mutex_unlock(&dev->ctrl_urb_lock);101return ret;102}103104if (len)105memcpy(buf, dev->urb_buf, len);106107mutex_unlock(&dev->ctrl_urb_lock);108109if (reg_debug) {110int byte;111112printk("<<<");113for (byte = 0; byte < len; byte++)114printk(" %02x", (unsigned char)buf[byte]);115printk("\n");116}117118return ret;119}120121/*122* em28xx_read_reg_req()123* reads data from the usb device specifying bRequest124*/125int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)126{127int ret;128u8 val;129130ret = em28xx_read_reg_req_len(dev, req, reg, &val, 1);131if (ret < 0)132return ret;133134return val;135}136137int em28xx_read_reg(struct em28xx *dev, u16 reg)138{139return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);140}141142/*143* em28xx_write_regs_req()144* sends data to the usb device, specifying bRequest145*/146int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,147int len)148{149int ret;150int pipe = usb_sndctrlpipe(dev->udev, 0);151152if (dev->state & DEV_DISCONNECTED)153return -ENODEV;154155if ((len < 1) || (len > URB_MAX_CTRL_SIZE))156return -EINVAL;157158if (reg_debug) {159int byte;160161printk(KERN_DEBUG "(pipe 0x%08x): "162"OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",163pipe,164USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,165req, 0, 0,166reg & 0xff, reg >> 8,167len & 0xff, len >> 8);168169for (byte = 0; byte < len; byte++)170printk(" %02x", (unsigned char)buf[byte]);171printk("\n");172}173174mutex_lock(&dev->ctrl_urb_lock);175memcpy(dev->urb_buf, buf, len);176ret = usb_control_msg(dev->udev, pipe, req,177USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,1780x0000, reg, dev->urb_buf, len, HZ);179mutex_unlock(&dev->ctrl_urb_lock);180181if (dev->wait_after_write)182msleep(dev->wait_after_write);183184return ret;185}186187int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)188{189int rc;190191rc = em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);192193/* Stores GPO/GPIO values at the cache, if changed194Only write values should be stored, since input on a GPIO195register will return the input bits.196Not sure what happens on reading GPO register.197*/198if (rc >= 0) {199if (reg == dev->reg_gpo_num)200dev->reg_gpo = buf[0];201else if (reg == dev->reg_gpio_num)202dev->reg_gpio = buf[0];203}204205return rc;206}207208/* Write a single register */209int em28xx_write_reg(struct em28xx *dev, u16 reg, u8 val)210{211return em28xx_write_regs(dev, reg, &val, 1);212}213214/*215* em28xx_write_reg_bits()216* sets only some bits (specified by bitmask) of a register, by first reading217* the actual value218*/219int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,220u8 bitmask)221{222int oldval;223u8 newval;224225/* Uses cache for gpo/gpio registers */226if (reg == dev->reg_gpo_num)227oldval = dev->reg_gpo;228else if (reg == dev->reg_gpio_num)229oldval = dev->reg_gpio;230else231oldval = em28xx_read_reg(dev, reg);232233if (oldval < 0)234return oldval;235236newval = (((u8) oldval) & ~bitmask) | (val & bitmask);237238return em28xx_write_regs(dev, reg, &newval, 1);239}240241/*242* em28xx_is_ac97_ready()243* Checks if ac97 is ready244*/245static int em28xx_is_ac97_ready(struct em28xx *dev)246{247int ret, i;248249/* Wait up to 50 ms for AC97 command to complete */250for (i = 0; i < 10; i++, msleep(5)) {251ret = em28xx_read_reg(dev, EM28XX_R43_AC97BUSY);252if (ret < 0)253return ret;254255if (!(ret & 0x01))256return 0;257}258259em28xx_warn("AC97 command still being executed: not handled properly!\n");260return -EBUSY;261}262263/*264* em28xx_read_ac97()265* write a 16 bit value to the specified AC97 address (LSB first!)266*/267int em28xx_read_ac97(struct em28xx *dev, u8 reg)268{269int ret;270u8 addr = (reg & 0x7f) | 0x80;271u16 val;272273ret = em28xx_is_ac97_ready(dev);274if (ret < 0)275return ret;276277ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);278if (ret < 0)279return ret;280281ret = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R40_AC97LSB,282(u8 *)&val, sizeof(val));283284if (ret < 0)285return ret;286return le16_to_cpu(val);287}288289/*290* em28xx_write_ac97()291* write a 16 bit value to the specified AC97 address (LSB first!)292*/293int em28xx_write_ac97(struct em28xx *dev, u8 reg, u16 val)294{295int ret;296u8 addr = reg & 0x7f;297__le16 value;298299value = cpu_to_le16(val);300301ret = em28xx_is_ac97_ready(dev);302if (ret < 0)303return ret;304305ret = em28xx_write_regs(dev, EM28XX_R40_AC97LSB, (u8 *) &value, 2);306if (ret < 0)307return ret;308309ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);310if (ret < 0)311return ret;312313return 0;314}315316struct em28xx_vol_table {317enum em28xx_amux mux;318u8 reg;319};320321static struct em28xx_vol_table inputs[] = {322{ EM28XX_AMUX_VIDEO, AC97_VIDEO_VOL },323{ EM28XX_AMUX_LINE_IN, AC97_LINEIN_VOL },324{ EM28XX_AMUX_PHONE, AC97_PHONE_VOL },325{ EM28XX_AMUX_MIC, AC97_MIC_VOL },326{ EM28XX_AMUX_CD, AC97_CD_VOL },327{ EM28XX_AMUX_AUX, AC97_AUX_VOL },328{ EM28XX_AMUX_PCM_OUT, AC97_PCM_OUT_VOL },329};330331static int set_ac97_input(struct em28xx *dev)332{333int ret, i;334enum em28xx_amux amux = dev->ctl_ainput;335336/* EM28XX_AMUX_VIDEO2 is a special case used to indicate that337em28xx should point to LINE IN, while AC97 should use VIDEO338*/339if (amux == EM28XX_AMUX_VIDEO2)340amux = EM28XX_AMUX_VIDEO;341342/* Mute all entres but the one that were selected */343for (i = 0; i < ARRAY_SIZE(inputs); i++) {344if (amux == inputs[i].mux)345ret = em28xx_write_ac97(dev, inputs[i].reg, 0x0808);346else347ret = em28xx_write_ac97(dev, inputs[i].reg, 0x8000);348349if (ret < 0)350em28xx_warn("couldn't setup AC97 register %d\n",351inputs[i].reg);352}353return 0;354}355356static int em28xx_set_audio_source(struct em28xx *dev)357{358int ret;359u8 input;360361if (dev->board.is_em2800) {362if (dev->ctl_ainput == EM28XX_AMUX_VIDEO)363input = EM2800_AUDIO_SRC_TUNER;364else365input = EM2800_AUDIO_SRC_LINE;366367ret = em28xx_write_regs(dev, EM2800_R08_AUDIOSRC, &input, 1);368if (ret < 0)369return ret;370}371372if (dev->board.has_msp34xx)373input = EM28XX_AUDIO_SRC_TUNER;374else {375switch (dev->ctl_ainput) {376case EM28XX_AMUX_VIDEO:377input = EM28XX_AUDIO_SRC_TUNER;378break;379default:380input = EM28XX_AUDIO_SRC_LINE;381break;382}383}384385if (dev->board.mute_gpio && dev->mute)386em28xx_gpio_set(dev, dev->board.mute_gpio);387else388em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);389390ret = em28xx_write_reg_bits(dev, EM28XX_R0E_AUDIOSRC, input, 0xc0);391if (ret < 0)392return ret;393msleep(5);394395switch (dev->audio_mode.ac97) {396case EM28XX_NO_AC97:397break;398default:399ret = set_ac97_input(dev);400}401402return ret;403}404405static const struct em28xx_vol_table outputs[] = {406{ EM28XX_AOUT_MASTER, AC97_MASTER_VOL },407{ EM28XX_AOUT_LINE, AC97_LINE_LEVEL_VOL },408{ EM28XX_AOUT_MONO, AC97_MASTER_MONO_VOL },409{ EM28XX_AOUT_LFE, AC97_LFE_MASTER_VOL },410{ EM28XX_AOUT_SURR, AC97_SURR_MASTER_VOL },411};412413int em28xx_audio_analog_set(struct em28xx *dev)414{415int ret, i;416u8 xclk;417418if (!dev->audio_mode.has_audio)419return 0;420421/* It is assumed that all devices use master volume for output.422It would be possible to use also line output.423*/424if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {425/* Mute all outputs */426for (i = 0; i < ARRAY_SIZE(outputs); i++) {427ret = em28xx_write_ac97(dev, outputs[i].reg, 0x8000);428if (ret < 0)429em28xx_warn("couldn't setup AC97 register %d\n",430outputs[i].reg);431}432}433434xclk = dev->board.xclk & 0x7f;435if (!dev->mute)436xclk |= EM28XX_XCLK_AUDIO_UNMUTE;437438ret = em28xx_write_reg(dev, EM28XX_R0F_XCLK, xclk);439if (ret < 0)440return ret;441msleep(10);442443/* Selects the proper audio input */444ret = em28xx_set_audio_source(dev);445446/* Sets volume */447if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {448int vol;449450em28xx_write_ac97(dev, AC97_POWER_DOWN_CTRL, 0x4200);451em28xx_write_ac97(dev, AC97_EXT_AUD_CTRL, 0x0031);452em28xx_write_ac97(dev, AC97_PCM_IN_SRATE, 0xbb80);453454/* LSB: left channel - both channels with the same level */455vol = (0x1f - dev->volume) | ((0x1f - dev->volume) << 8);456457/* Mute device, if needed */458if (dev->mute)459vol |= 0x8000;460461/* Sets volume */462for (i = 0; i < ARRAY_SIZE(outputs); i++) {463if (dev->ctl_aoutput & outputs[i].mux)464ret = em28xx_write_ac97(dev, outputs[i].reg,465vol);466if (ret < 0)467em28xx_warn("couldn't setup AC97 register %d\n",468outputs[i].reg);469}470471if (dev->ctl_aoutput & EM28XX_AOUT_PCM_IN) {472int sel = ac97_return_record_select(dev->ctl_aoutput);473474/* Use the same input for both left and right475channels */476sel |= (sel << 8);477478em28xx_write_ac97(dev, AC97_RECORD_SELECT, sel);479}480}481482return ret;483}484EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);485486int em28xx_audio_setup(struct em28xx *dev)487{488int vid1, vid2, feat, cfg;489u32 vid;490491if (dev->chip_id == CHIP_ID_EM2870 || dev->chip_id == CHIP_ID_EM2874492|| dev->chip_id == CHIP_ID_EM28174) {493/* Digital only device - don't load any alsa module */494dev->audio_mode.has_audio = 0;495dev->has_audio_class = 0;496dev->has_alsa_audio = 0;497return 0;498}499500/* If device doesn't support Usb Audio Class, use vendor class */501if (!dev->has_audio_class)502dev->has_alsa_audio = 1;503504dev->audio_mode.has_audio = 1;505506/* See how this device is configured */507cfg = em28xx_read_reg(dev, EM28XX_R00_CHIPCFG);508em28xx_info("Config register raw data: 0x%02x\n", cfg);509if (cfg < 0) {510/* Register read error? */511cfg = EM28XX_CHIPCFG_AC97; /* Be conservative */512} else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) == 0x00) {513/* The device doesn't have vendor audio at all */514dev->has_alsa_audio = 0;515dev->audio_mode.has_audio = 0;516return 0;517} else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) ==518EM28XX_CHIPCFG_I2S_3_SAMPRATES) {519em28xx_info("I2S Audio (3 sample rates)\n");520dev->audio_mode.i2s_3rates = 1;521} else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) ==522EM28XX_CHIPCFG_I2S_5_SAMPRATES) {523em28xx_info("I2S Audio (5 sample rates)\n");524dev->audio_mode.i2s_5rates = 1;525}526527if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) != EM28XX_CHIPCFG_AC97) {528/* Skip the code that does AC97 vendor detection */529dev->audio_mode.ac97 = EM28XX_NO_AC97;530goto init_audio;531}532533dev->audio_mode.ac97 = EM28XX_AC97_OTHER;534535vid1 = em28xx_read_ac97(dev, AC97_VENDOR_ID1);536if (vid1 < 0) {537/*538* Device likely doesn't support AC97539* Note: (some) em2800 devices without eeprom reports 0x91 on540* CHIPCFG register, even not having an AC97 chip541*/542em28xx_warn("AC97 chip type couldn't be determined\n");543dev->audio_mode.ac97 = EM28XX_NO_AC97;544dev->has_alsa_audio = 0;545dev->audio_mode.has_audio = 0;546goto init_audio;547}548549vid2 = em28xx_read_ac97(dev, AC97_VENDOR_ID2);550if (vid2 < 0)551goto init_audio;552553vid = vid1 << 16 | vid2;554555dev->audio_mode.ac97_vendor_id = vid;556em28xx_warn("AC97 vendor ID = 0x%08x\n", vid);557558feat = em28xx_read_ac97(dev, AC97_RESET);559if (feat < 0)560goto init_audio;561562dev->audio_mode.ac97_feat = feat;563em28xx_warn("AC97 features = 0x%04x\n", feat);564565/* Try to identify what audio processor we have */566if ((vid == 0xffffffff) && (feat == 0x6a90))567dev->audio_mode.ac97 = EM28XX_AC97_EM202;568else if ((vid >> 8) == 0x838476)569dev->audio_mode.ac97 = EM28XX_AC97_SIGMATEL;570571init_audio:572/* Reports detected AC97 processor */573switch (dev->audio_mode.ac97) {574case EM28XX_NO_AC97:575em28xx_info("No AC97 audio processor\n");576break;577case EM28XX_AC97_EM202:578em28xx_info("Empia 202 AC97 audio processor detected\n");579break;580case EM28XX_AC97_SIGMATEL:581em28xx_info("Sigmatel audio processor detected(stac 97%02x)\n",582dev->audio_mode.ac97_vendor_id & 0xff);583break;584case EM28XX_AC97_OTHER:585em28xx_warn("Unknown AC97 audio processor detected!\n");586break;587default:588break;589}590591return em28xx_audio_analog_set(dev);592}593EXPORT_SYMBOL_GPL(em28xx_audio_setup);594595int em28xx_colorlevels_set_default(struct em28xx *dev)596{597em28xx_write_reg(dev, EM28XX_R20_YGAIN, 0x10); /* contrast */598em28xx_write_reg(dev, EM28XX_R21_YOFFSET, 0x00); /* brightness */599em28xx_write_reg(dev, EM28XX_R22_UVGAIN, 0x10); /* saturation */600em28xx_write_reg(dev, EM28XX_R23_UOFFSET, 0x00);601em28xx_write_reg(dev, EM28XX_R24_VOFFSET, 0x00);602em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, 0x00);603604em28xx_write_reg(dev, EM28XX_R14_GAMMA, 0x20);605em28xx_write_reg(dev, EM28XX_R15_RGAIN, 0x20);606em28xx_write_reg(dev, EM28XX_R16_GGAIN, 0x20);607em28xx_write_reg(dev, EM28XX_R17_BGAIN, 0x20);608em28xx_write_reg(dev, EM28XX_R18_ROFFSET, 0x00);609em28xx_write_reg(dev, EM28XX_R19_GOFFSET, 0x00);610return em28xx_write_reg(dev, EM28XX_R1A_BOFFSET, 0x00);611}612613int em28xx_capture_start(struct em28xx *dev, int start)614{615int rc;616617if (dev->chip_id == CHIP_ID_EM2874 || dev->chip_id == CHIP_ID_EM28174) {618/* The Transport Stream Enable Register moved in em2874 */619if (!start) {620rc = em28xx_write_reg_bits(dev, EM2874_R5F_TS_ENABLE,6210x00,622EM2874_TS1_CAPTURE_ENABLE);623return rc;624}625626/* Enable Transport Stream */627rc = em28xx_write_reg_bits(dev, EM2874_R5F_TS_ENABLE,628EM2874_TS1_CAPTURE_ENABLE,629EM2874_TS1_CAPTURE_ENABLE);630return rc;631}632633634/* FIXME: which is the best order? */635/* video registers are sampled by VREF */636rc = em28xx_write_reg_bits(dev, EM28XX_R0C_USBSUSP,637start ? 0x10 : 0x00, 0x10);638if (rc < 0)639return rc;640641if (!start) {642/* disable video capture */643rc = em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x27);644return rc;645}646647if (dev->board.is_webcam)648rc = em28xx_write_reg(dev, 0x13, 0x0c);649650/* enable video capture */651rc = em28xx_write_reg(dev, 0x48, 0x00);652653if (dev->mode == EM28XX_ANALOG_MODE)654rc = em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x67);655else656rc = em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x37);657658msleep(6);659660return rc;661}662663int em28xx_vbi_supported(struct em28xx *dev)664{665/* Modprobe option to manually disable */666if (disable_vbi == 1)667return 0;668669if (dev->chip_id == CHIP_ID_EM2860 ||670dev->chip_id == CHIP_ID_EM2883)671return 1;672673/* Version of em28xx that does not support VBI */674return 0;675}676677int em28xx_set_outfmt(struct em28xx *dev)678{679int ret;680u8 vinctrl;681682ret = em28xx_write_reg_bits(dev, EM28XX_R27_OUTFMT,683dev->format->reg | 0x20, 0xff);684if (ret < 0)685return ret;686687ret = em28xx_write_reg(dev, EM28XX_R10_VINMODE, dev->vinmode);688if (ret < 0)689return ret;690691vinctrl = dev->vinctl;692if (em28xx_vbi_supported(dev) == 1) {693vinctrl |= EM28XX_VINCTRL_VBI_RAW;694em28xx_write_reg(dev, EM28XX_R34_VBI_START_H, 0x00);695em28xx_write_reg(dev, EM28XX_R36_VBI_WIDTH, dev->vbi_width/4);696em28xx_write_reg(dev, EM28XX_R37_VBI_HEIGHT, dev->vbi_height);697if (dev->norm & V4L2_STD_525_60) {698/* NTSC */699em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x09);700} else if (dev->norm & V4L2_STD_625_50) {701/* PAL */702em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x07);703}704}705706return em28xx_write_reg(dev, EM28XX_R11_VINCTRL, vinctrl);707}708709static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,710u8 ymin, u8 ymax)711{712em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n",713xmin, ymin, xmax, ymax);714715em28xx_write_regs(dev, EM28XX_R28_XMIN, &xmin, 1);716em28xx_write_regs(dev, EM28XX_R29_XMAX, &xmax, 1);717em28xx_write_regs(dev, EM28XX_R2A_YMIN, &ymin, 1);718return em28xx_write_regs(dev, EM28XX_R2B_YMAX, &ymax, 1);719}720721static int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,722u16 width, u16 height)723{724u8 cwidth = width;725u8 cheight = height;726u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);727728em28xx_coredbg("em28xx Area Set: (%d,%d)\n",729(width | (overflow & 2) << 7),730(height | (overflow & 1) << 8));731732em28xx_write_regs(dev, EM28XX_R1C_HSTART, &hstart, 1);733em28xx_write_regs(dev, EM28XX_R1D_VSTART, &vstart, 1);734em28xx_write_regs(dev, EM28XX_R1E_CWIDTH, &cwidth, 1);735em28xx_write_regs(dev, EM28XX_R1F_CHEIGHT, &cheight, 1);736return em28xx_write_regs(dev, EM28XX_R1B_OFLOW, &overflow, 1);737}738739static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)740{741u8 mode;742/* the em2800 scaler only supports scaling down to 50% */743744if (dev->board.is_em2800) {745mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);746} else {747u8 buf[2];748749buf[0] = h;750buf[1] = h >> 8;751em28xx_write_regs(dev, EM28XX_R30_HSCALELOW, (char *)buf, 2);752753buf[0] = v;754buf[1] = v >> 8;755em28xx_write_regs(dev, EM28XX_R32_VSCALELOW, (char *)buf, 2);756/* it seems that both H and V scalers must be active757to work correctly */758mode = (h || v) ? 0x30 : 0x00;759}760return em28xx_write_reg_bits(dev, EM28XX_R26_COMPR, mode, 0x30);761}762763/* FIXME: this only function read values from dev */764int em28xx_resolution_set(struct em28xx *dev)765{766int width, height;767width = norm_maxw(dev);768height = norm_maxh(dev);769770/* Properly setup VBI */771dev->vbi_width = 720;772if (dev->norm & V4L2_STD_525_60)773dev->vbi_height = 12;774else775dev->vbi_height = 18;776777if (!dev->progressive)778height >>= norm_maxh(dev);779780em28xx_set_outfmt(dev);781782783em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);784785/* If we don't set the start position to 2 in VBI mode, we end up786with line 20/21 being YUYV encoded instead of being in 8-bit787greyscale. The core of the issue is that line 21 (and line 23 for788PAL WSS) are inside of active video region, and as a result they789get the pixelformatting associated with that area. So by cropping790it out, we end up with the same format as the rest of the VBI791region */792if (em28xx_vbi_supported(dev) == 1)793em28xx_capture_area_set(dev, 0, 2, width >> 2, height >> 2);794else795em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);796797return em28xx_scaler_set(dev, dev->hscale, dev->vscale);798}799800int em28xx_set_alternate(struct em28xx *dev)801{802int errCode, prev_alt = dev->alt;803int i;804unsigned int min_pkt_size = dev->width * 2 + 4;805806/*807* alt = 0 is used only for control messages, so, only values808* greater than 0 can be used for streaming.809*/810if (alt && alt < dev->num_alt) {811em28xx_coredbg("alternate forced to %d\n", dev->alt);812dev->alt = alt;813goto set_alt;814}815816/* When image size is bigger than a certain value,817the frame size should be increased, otherwise, only818green screen will be received.819*/820if (dev->width * 2 * dev->height > 720 * 240 * 2)821min_pkt_size *= 2;822823for (i = 0; i < dev->num_alt; i++) {824/* stop when the selected alt setting offers enough bandwidth */825if (dev->alt_max_pkt_size[i] >= min_pkt_size) {826dev->alt = i;827break;828/* otherwise make sure that we end up with the maximum bandwidth829because the min_pkt_size equation might be wrong...830*/831} else if (dev->alt_max_pkt_size[i] >832dev->alt_max_pkt_size[dev->alt])833dev->alt = i;834}835836set_alt:837if (dev->alt != prev_alt) {838em28xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",839min_pkt_size, dev->alt);840dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];841em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",842dev->alt, dev->max_pkt_size);843errCode = usb_set_interface(dev->udev, 0, dev->alt);844if (errCode < 0) {845em28xx_errdev("cannot change alternate number to %d (error=%i)\n",846dev->alt, errCode);847return errCode;848}849}850return 0;851}852853int em28xx_gpio_set(struct em28xx *dev, struct em28xx_reg_seq *gpio)854{855int rc = 0;856857if (!gpio)858return rc;859860if (dev->mode != EM28XX_SUSPEND) {861em28xx_write_reg(dev, 0x48, 0x00);862if (dev->mode == EM28XX_ANALOG_MODE)863em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x67);864else865em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x37);866msleep(6);867}868869/* Send GPIO reset sequences specified at board entry */870while (gpio->sleep >= 0) {871if (gpio->reg >= 0) {872rc = em28xx_write_reg_bits(dev,873gpio->reg,874gpio->val,875gpio->mask);876if (rc < 0)877return rc;878}879if (gpio->sleep > 0)880msleep(gpio->sleep);881882gpio++;883}884return rc;885}886887int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode)888{889if (dev->mode == set_mode)890return 0;891892if (set_mode == EM28XX_SUSPEND) {893dev->mode = set_mode;894895/* FIXME: add suspend support for ac97 */896897return em28xx_gpio_set(dev, dev->board.suspend_gpio);898}899900dev->mode = set_mode;901902if (dev->mode == EM28XX_DIGITAL_MODE)903return em28xx_gpio_set(dev, dev->board.dvb_gpio);904else905return em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);906}907EXPORT_SYMBOL_GPL(em28xx_set_mode);908909/* ------------------------------------------------------------------910URB control911------------------------------------------------------------------*/912913/*914* IRQ callback, called by URB callback915*/916static void em28xx_irq_callback(struct urb *urb)917{918struct em28xx *dev = urb->context;919int rc, i;920921switch (urb->status) {922case 0: /* success */923case -ETIMEDOUT: /* NAK */924break;925case -ECONNRESET: /* kill */926case -ENOENT:927case -ESHUTDOWN:928return;929default: /* error */930em28xx_isocdbg("urb completition error %d.\n", urb->status);931break;932}933934/* Copy data from URB */935spin_lock(&dev->slock);936rc = dev->isoc_ctl.isoc_copy(dev, urb);937spin_unlock(&dev->slock);938939/* Reset urb buffers */940for (i = 0; i < urb->number_of_packets; i++) {941urb->iso_frame_desc[i].status = 0;942urb->iso_frame_desc[i].actual_length = 0;943}944urb->status = 0;945946urb->status = usb_submit_urb(urb, GFP_ATOMIC);947if (urb->status) {948em28xx_isocdbg("urb resubmit failed (error=%i)\n",949urb->status);950}951}952953/*954* Stop and Deallocate URBs955*/956void em28xx_uninit_isoc(struct em28xx *dev)957{958struct urb *urb;959int i;960961em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");962963dev->isoc_ctl.nfields = -1;964for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {965urb = dev->isoc_ctl.urb[i];966if (urb) {967if (!irqs_disabled())968usb_kill_urb(urb);969else970usb_unlink_urb(urb);971972if (dev->isoc_ctl.transfer_buffer[i]) {973usb_free_coherent(dev->udev,974urb->transfer_buffer_length,975dev->isoc_ctl.transfer_buffer[i],976urb->transfer_dma);977}978usb_free_urb(urb);979dev->isoc_ctl.urb[i] = NULL;980}981dev->isoc_ctl.transfer_buffer[i] = NULL;982}983984kfree(dev->isoc_ctl.urb);985kfree(dev->isoc_ctl.transfer_buffer);986987dev->isoc_ctl.urb = NULL;988dev->isoc_ctl.transfer_buffer = NULL;989dev->isoc_ctl.num_bufs = 0;990991em28xx_capture_start(dev, 0);992}993EXPORT_SYMBOL_GPL(em28xx_uninit_isoc);994995/*996* Allocate URBs and start IRQ997*/998int em28xx_init_isoc(struct em28xx *dev, int max_packets,999int num_bufs, int max_pkt_size,1000int (*isoc_copy) (struct em28xx *dev, struct urb *urb))1001{1002struct em28xx_dmaqueue *dma_q = &dev->vidq;1003struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;1004int i;1005int sb_size, pipe;1006struct urb *urb;1007int j, k;1008int rc;10091010em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");10111012/* De-allocates all pending stuff */1013em28xx_uninit_isoc(dev);10141015dev->isoc_ctl.isoc_copy = isoc_copy;1016dev->isoc_ctl.num_bufs = num_bufs;10171018dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);1019if (!dev->isoc_ctl.urb) {1020em28xx_errdev("cannot alloc memory for usb buffers\n");1021return -ENOMEM;1022}10231024dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,1025GFP_KERNEL);1026if (!dev->isoc_ctl.transfer_buffer) {1027em28xx_errdev("cannot allocate memory for usb transfer\n");1028kfree(dev->isoc_ctl.urb);1029return -ENOMEM;1030}10311032dev->isoc_ctl.max_pkt_size = max_pkt_size;1033dev->isoc_ctl.vid_buf = NULL;1034dev->isoc_ctl.vbi_buf = NULL;10351036sb_size = max_packets * dev->isoc_ctl.max_pkt_size;10371038/* allocate urbs and transfer buffers */1039for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {1040urb = usb_alloc_urb(max_packets, GFP_KERNEL);1041if (!urb) {1042em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);1043em28xx_uninit_isoc(dev);1044return -ENOMEM;1045}1046dev->isoc_ctl.urb[i] = urb;10471048dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,1049sb_size, GFP_KERNEL, &urb->transfer_dma);1050if (!dev->isoc_ctl.transfer_buffer[i]) {1051em28xx_err("unable to allocate %i bytes for transfer"1052" buffer %i%s\n",1053sb_size, i,1054in_interrupt() ? " while in int" : "");1055em28xx_uninit_isoc(dev);1056return -ENOMEM;1057}1058memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);10591060/* FIXME: this is a hack - should be1061'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'1062should also be using 'desc.bInterval'1063*/1064pipe = usb_rcvisocpipe(dev->udev,1065dev->mode == EM28XX_ANALOG_MODE ? 0x82 : 0x84);10661067usb_fill_int_urb(urb, dev->udev, pipe,1068dev->isoc_ctl.transfer_buffer[i], sb_size,1069em28xx_irq_callback, dev, 1);10701071urb->number_of_packets = max_packets;1072urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;10731074k = 0;1075for (j = 0; j < max_packets; j++) {1076urb->iso_frame_desc[j].offset = k;1077urb->iso_frame_desc[j].length =1078dev->isoc_ctl.max_pkt_size;1079k += dev->isoc_ctl.max_pkt_size;1080}1081}10821083init_waitqueue_head(&dma_q->wq);1084init_waitqueue_head(&vbi_dma_q->wq);10851086em28xx_capture_start(dev, 1);10871088/* submit urbs and enables IRQ */1089for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {1090rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);1091if (rc) {1092em28xx_err("submit of urb %i failed (error=%i)\n", i,1093rc);1094em28xx_uninit_isoc(dev);1095return rc;1096}1097}10981099return 0;1100}1101EXPORT_SYMBOL_GPL(em28xx_init_isoc);11021103/* Determine the packet size for the DVB stream for the given device1104(underlying value programmed into the eeprom) */1105int em28xx_isoc_dvb_max_packetsize(struct em28xx *dev)1106{1107unsigned int chip_cfg2;1108unsigned int packet_size = 564;11091110if (dev->chip_id == CHIP_ID_EM2874) {1111/* FIXME - for now assume 564 like it was before, but the1112em2874 code should be added to return the proper value... */1113packet_size = 564;1114} else if (dev->chip_id == CHIP_ID_EM28174) {1115/* FIXME same as em2874. 564 was enough for 22 Mbit DVB-T1116but too much for 44 Mbit DVB-C. */1117packet_size = 752;1118} else {1119/* TS max packet size stored in bits 1-0 of R01 */1120chip_cfg2 = em28xx_read_reg(dev, EM28XX_R01_CHIPCFG2);1121switch (chip_cfg2 & EM28XX_CHIPCFG2_TS_PACKETSIZE_MASK) {1122case EM28XX_CHIPCFG2_TS_PACKETSIZE_188:1123packet_size = 188;1124break;1125case EM28XX_CHIPCFG2_TS_PACKETSIZE_376:1126packet_size = 376;1127break;1128case EM28XX_CHIPCFG2_TS_PACKETSIZE_564:1129packet_size = 564;1130break;1131case EM28XX_CHIPCFG2_TS_PACKETSIZE_752:1132packet_size = 752;1133break;1134}1135}11361137em28xx_coredbg("dvb max packet size=%d\n", packet_size);1138return packet_size;1139}1140EXPORT_SYMBOL_GPL(em28xx_isoc_dvb_max_packetsize);11411142/*1143* em28xx_wake_i2c()1144* configure i2c attached devices1145*/1146void em28xx_wake_i2c(struct em28xx *dev)1147{1148v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0);1149v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,1150INPUT(dev->ctl_input)->vmux, 0, 0);1151v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);1152}11531154/*1155* Device control list1156*/11571158static LIST_HEAD(em28xx_devlist);1159static DEFINE_MUTEX(em28xx_devlist_mutex);11601161/*1162* em28xx_realease_resources()1163* unregisters the v4l2,i2c and usb devices1164* called when the device gets disconected or at module unload1165*/1166void em28xx_remove_from_devlist(struct em28xx *dev)1167{1168mutex_lock(&em28xx_devlist_mutex);1169list_del(&dev->devlist);1170mutex_unlock(&em28xx_devlist_mutex);1171};11721173void em28xx_add_into_devlist(struct em28xx *dev)1174{1175mutex_lock(&em28xx_devlist_mutex);1176list_add_tail(&dev->devlist, &em28xx_devlist);1177mutex_unlock(&em28xx_devlist_mutex);1178};11791180/*1181* Extension interface1182*/11831184static LIST_HEAD(em28xx_extension_devlist);11851186int em28xx_register_extension(struct em28xx_ops *ops)1187{1188struct em28xx *dev = NULL;11891190mutex_lock(&em28xx_devlist_mutex);1191list_add_tail(&ops->next, &em28xx_extension_devlist);1192list_for_each_entry(dev, &em28xx_devlist, devlist) {1193ops->init(dev);1194}1195printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);1196mutex_unlock(&em28xx_devlist_mutex);1197return 0;1198}1199EXPORT_SYMBOL(em28xx_register_extension);12001201void em28xx_unregister_extension(struct em28xx_ops *ops)1202{1203struct em28xx *dev = NULL;12041205mutex_lock(&em28xx_devlist_mutex);1206list_for_each_entry(dev, &em28xx_devlist, devlist) {1207ops->fini(dev);1208}1209printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);1210list_del(&ops->next);1211mutex_unlock(&em28xx_devlist_mutex);1212}1213EXPORT_SYMBOL(em28xx_unregister_extension);12141215void em28xx_init_extension(struct em28xx *dev)1216{1217struct em28xx_ops *ops = NULL;12181219mutex_lock(&em28xx_devlist_mutex);1220if (!list_empty(&em28xx_extension_devlist)) {1221list_for_each_entry(ops, &em28xx_extension_devlist, next) {1222if (ops->init)1223ops->init(dev);1224}1225}1226mutex_unlock(&em28xx_devlist_mutex);1227}12281229void em28xx_close_extension(struct em28xx *dev)1230{1231struct em28xx_ops *ops = NULL;12321233mutex_lock(&em28xx_devlist_mutex);1234if (!list_empty(&em28xx_extension_devlist)) {1235list_for_each_entry(ops, &em28xx_extension_devlist, next) {1236if (ops->fini)1237ops->fini(dev);1238}1239}1240mutex_unlock(&em28xx_devlist_mutex);1241}124212431244