ov511.c 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127
  1. /*
  2. * OmniVision OV511 Camera-to-USB Bridge Driver
  3. *
  4. * Copyright (c) 1999-2003 Mark W. McClelland
  5. * Original decompression code Copyright 1998-2000 OmniVision Technologies
  6. * Many improvements by Bret Wallach <bwallac1@san.rr.com>
  7. * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000)
  8. * Snapshot code by Kevin Moore
  9. * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
  10. * Changes by Claudio Matsuoka <claudio@conectiva.com>
  11. * Original SAA7111A code by Dave Perks <dperks@ibm.net>
  12. * URB error messages from pwc driver by Nemosoft
  13. * generic_ioctl() code from videodev.c by Gerd Knorr and Alan Cox
  14. * Memory management (rvmalloc) code from bttv driver, by Gerd Knorr and others
  15. *
  16. * Based on the Linux CPiA driver written by Peter Pregler,
  17. * Scott J. Bertin and Johannes Erdfelt.
  18. *
  19. * Please see the file: Documentation/usb/ov511.txt
  20. * and the website at: http://alpha.dyndns.org/ov511
  21. * for more info.
  22. *
  23. * This program is free software; you can redistribute it and/or modify it
  24. * under the terms of the GNU General Public License as published by the
  25. * Free Software Foundation; either version 2 of the License, or (at your
  26. * option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful, but
  29. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  31. * for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software Foundation,
  35. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. */
  37. #include <linux/config.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/slab.h>
  42. #include <linux/ctype.h>
  43. #include <linux/pagemap.h>
  44. #include <asm/semaphore.h>
  45. #include <asm/processor.h>
  46. #include <linux/mm.h>
  47. #include <linux/device.h>
  48. #if defined (__i386__)
  49. #include <asm/cpufeature.h>
  50. #endif
  51. #include "ov511.h"
  52. /*
  53. * Version Information
  54. */
  55. #define DRIVER_VERSION "v1.64 for Linux 2.5"
  56. #define EMAIL "mark@alpha.dyndns.org"
  57. #define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org> & Bret Wallach \
  58. & Orion Sky Lawlor <olawlor@acm.org> & Kevin Moore & Charl P. Botha \
  59. <cpbotha@ieee.org> & Claudio Matsuoka <claudio@conectiva.com>"
  60. #define DRIVER_DESC "ov511 USB Camera Driver"
  61. #define OV511_I2C_RETRIES 3
  62. #define ENABLE_Y_QUANTABLE 1
  63. #define ENABLE_UV_QUANTABLE 1
  64. #define OV511_MAX_UNIT_VIDEO 16
  65. /* Pixel count * bytes per YUV420 pixel (1.5) */
  66. #define MAX_FRAME_SIZE(w, h) ((w) * (h) * 3 / 2)
  67. #define MAX_DATA_SIZE(w, h) (MAX_FRAME_SIZE(w, h) + sizeof(struct timeval))
  68. /* Max size * bytes per YUV420 pixel (1.5) + one extra isoc frame for safety */
  69. #define MAX_RAW_DATA_SIZE(w, h) ((w) * (h) * 3 / 2 + 1024)
  70. #define FATAL_ERROR(rc) ((rc) < 0 && (rc) != -EPERM)
  71. /**********************************************************************
  72. * Module Parameters
  73. * (See ov511.txt for detailed descriptions of these)
  74. **********************************************************************/
  75. /* These variables (and all static globals) default to zero */
  76. static int autobright = 1;
  77. static int autogain = 1;
  78. static int autoexp = 1;
  79. static int debug;
  80. static int snapshot;
  81. static int cams = 1;
  82. static int compress;
  83. static int testpat;
  84. static int dumppix;
  85. static int led = 1;
  86. static int dump_bridge;
  87. static int dump_sensor;
  88. static int printph;
  89. static int phy = 0x1f;
  90. static int phuv = 0x05;
  91. static int pvy = 0x06;
  92. static int pvuv = 0x06;
  93. static int qhy = 0x14;
  94. static int qhuv = 0x03;
  95. static int qvy = 0x04;
  96. static int qvuv = 0x04;
  97. static int lightfreq;
  98. static int bandingfilter;
  99. static int clockdiv = -1;
  100. static int packetsize = -1;
  101. static int framedrop = -1;
  102. static int fastset;
  103. static int force_palette;
  104. static int backlight;
  105. static int unit_video[OV511_MAX_UNIT_VIDEO];
  106. static int remove_zeros;
  107. static int mirror;
  108. static int ov518_color;
  109. module_param(autobright, int, 0);
  110. MODULE_PARM_DESC(autobright, "Sensor automatically changes brightness");
  111. module_param(autogain, int, 0);
  112. MODULE_PARM_DESC(autogain, "Sensor automatically changes gain");
  113. module_param(autoexp, int, 0);
  114. MODULE_PARM_DESC(autoexp, "Sensor automatically changes exposure");
  115. module_param(debug, int, 0);
  116. MODULE_PARM_DESC(debug,
  117. "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=max");
  118. module_param(snapshot, int, 0);
  119. MODULE_PARM_DESC(snapshot, "Enable snapshot mode");
  120. module_param(cams, int, 0);
  121. MODULE_PARM_DESC(cams, "Number of simultaneous cameras");
  122. module_param(compress, int, 0);
  123. MODULE_PARM_DESC(compress, "Turn on compression");
  124. module_param(testpat, int, 0);
  125. MODULE_PARM_DESC(testpat,
  126. "Replace image with vertical bar testpattern (only partially working)");
  127. module_param(dumppix, int, 0);
  128. MODULE_PARM_DESC(dumppix, "Dump raw pixel data");
  129. module_param(led, int, 0);
  130. MODULE_PARM_DESC(led,
  131. "LED policy (OV511+ or later). 0=off, 1=on (default), 2=auto (on when open)");
  132. module_param(dump_bridge, int, 0);
  133. MODULE_PARM_DESC(dump_bridge, "Dump the bridge registers");
  134. module_param(dump_sensor, int, 0);
  135. MODULE_PARM_DESC(dump_sensor, "Dump the sensor registers");
  136. module_param(printph, int, 0);
  137. MODULE_PARM_DESC(printph, "Print frame start/end headers");
  138. module_param(phy, int, 0);
  139. MODULE_PARM_DESC(phy, "Prediction range (horiz. Y)");
  140. module_param(phuv, int, 0);
  141. MODULE_PARM_DESC(phuv, "Prediction range (horiz. UV)");
  142. module_param(pvy, int, 0);
  143. MODULE_PARM_DESC(pvy, "Prediction range (vert. Y)");
  144. module_param(pvuv, int, 0);
  145. MODULE_PARM_DESC(pvuv, "Prediction range (vert. UV)");
  146. module_param(qhy, int, 0);
  147. MODULE_PARM_DESC(qhy, "Quantization threshold (horiz. Y)");
  148. module_param(qhuv, int, 0);
  149. MODULE_PARM_DESC(qhuv, "Quantization threshold (horiz. UV)");
  150. module_param(qvy, int, 0);
  151. MODULE_PARM_DESC(qvy, "Quantization threshold (vert. Y)");
  152. module_param(qvuv, int, 0);
  153. MODULE_PARM_DESC(qvuv, "Quantization threshold (vert. UV)");
  154. module_param(lightfreq, int, 0);
  155. MODULE_PARM_DESC(lightfreq,
  156. "Light frequency. Set to 50 or 60 Hz, or zero for default settings");
  157. module_param(bandingfilter, int, 0);
  158. MODULE_PARM_DESC(bandingfilter,
  159. "Enable banding filter (to reduce effects of fluorescent lighting)");
  160. module_param(clockdiv, int, 0);
  161. MODULE_PARM_DESC(clockdiv, "Force pixel clock divisor to a specific value");
  162. module_param(packetsize, int, 0);
  163. MODULE_PARM_DESC(packetsize, "Force a specific isoc packet size");
  164. module_param(framedrop, int, 0);
  165. MODULE_PARM_DESC(framedrop, "Force a specific frame drop register setting");
  166. module_param(fastset, int, 0);
  167. MODULE_PARM_DESC(fastset, "Allows picture settings to take effect immediately");
  168. module_param(force_palette, int, 0);
  169. MODULE_PARM_DESC(force_palette, "Force the palette to a specific value");
  170. module_param(backlight, int, 0);
  171. MODULE_PARM_DESC(backlight, "For objects that are lit from behind");
  172. static int num_uv;
  173. module_param_array(unit_video, int, &num_uv, 0);
  174. MODULE_PARM_DESC(unit_video,
  175. "Force use of specific minor number(s). 0 is not allowed.");
  176. module_param(remove_zeros, int, 0);
  177. MODULE_PARM_DESC(remove_zeros,
  178. "Remove zero-padding from uncompressed incoming data");
  179. module_param(mirror, int, 0);
  180. MODULE_PARM_DESC(mirror, "Reverse image horizontally");
  181. module_param(ov518_color, int, 0);
  182. MODULE_PARM_DESC(ov518_color, "Enable OV518 color (experimental)");
  183. MODULE_AUTHOR(DRIVER_AUTHOR);
  184. MODULE_DESCRIPTION(DRIVER_DESC);
  185. MODULE_LICENSE("GPL");
  186. /**********************************************************************
  187. * Miscellaneous Globals
  188. **********************************************************************/
  189. static struct usb_driver ov511_driver;
  190. static struct ov51x_decomp_ops *ov511_decomp_ops;
  191. static struct ov51x_decomp_ops *ov511_mmx_decomp_ops;
  192. static struct ov51x_decomp_ops *ov518_decomp_ops;
  193. static struct ov51x_decomp_ops *ov518_mmx_decomp_ops;
  194. /* Number of times to retry a failed I2C transaction. Increase this if you
  195. * are getting "Failed to read sensor ID..." */
  196. static int i2c_detect_tries = 5;
  197. /* MMX support is present in kernel and CPU. Checked upon decomp module load. */
  198. #if defined(__i386__) || defined(__x86_64__)
  199. #define ov51x_mmx_available (cpu_has_mmx)
  200. #else
  201. #define ov51x_mmx_available (0)
  202. #endif
  203. static struct usb_device_id device_table [] = {
  204. { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) },
  205. { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) },
  206. { USB_DEVICE(VEND_OMNIVISION, PROD_OV518) },
  207. { USB_DEVICE(VEND_OMNIVISION, PROD_OV518PLUS) },
  208. { USB_DEVICE(VEND_MATTEL, PROD_ME2CAM) },
  209. { } /* Terminating entry */
  210. };
  211. MODULE_DEVICE_TABLE (usb, device_table);
  212. static unsigned char yQuanTable511[] = OV511_YQUANTABLE;
  213. static unsigned char uvQuanTable511[] = OV511_UVQUANTABLE;
  214. static unsigned char yQuanTable518[] = OV518_YQUANTABLE;
  215. static unsigned char uvQuanTable518[] = OV518_UVQUANTABLE;
  216. /**********************************************************************
  217. * Symbolic Names
  218. **********************************************************************/
  219. /* Known OV511-based cameras */
  220. static struct symbolic_list camlist[] = {
  221. { 0, "Generic Camera (no ID)" },
  222. { 1, "Mustek WCam 3X" },
  223. { 3, "D-Link DSB-C300" },
  224. { 4, "Generic OV511/OV7610" },
  225. { 5, "Puretek PT-6007" },
  226. { 6, "Lifeview USB Life TV (NTSC)" },
  227. { 21, "Creative Labs WebCam 3" },
  228. { 22, "Lifeview USB Life TV (PAL D/K+B/G)" },
  229. { 36, "Koala-Cam" },
  230. { 38, "Lifeview USB Life TV (PAL)" },
  231. { 41, "Samsung Anycam MPC-M10" },
  232. { 43, "Mtekvision Zeca MV402" },
  233. { 46, "Suma eON" },
  234. { 70, "Lifeview USB Life TV (PAL/SECAM)" },
  235. { 100, "Lifeview RoboCam" },
  236. { 102, "AverMedia InterCam Elite" },
  237. { 112, "MediaForte MV300" }, /* or OV7110 evaluation kit */
  238. { 134, "Ezonics EZCam II" },
  239. { 192, "Webeye 2000B" },
  240. { 253, "Alpha Vision Tech. AlphaCam SE" },
  241. { -1, NULL }
  242. };
  243. /* Video4Linux1 Palettes */
  244. static struct symbolic_list v4l1_plist[] = {
  245. { VIDEO_PALETTE_GREY, "GREY" },
  246. { VIDEO_PALETTE_HI240, "HI240" },
  247. { VIDEO_PALETTE_RGB565, "RGB565" },
  248. { VIDEO_PALETTE_RGB24, "RGB24" },
  249. { VIDEO_PALETTE_RGB32, "RGB32" },
  250. { VIDEO_PALETTE_RGB555, "RGB555" },
  251. { VIDEO_PALETTE_YUV422, "YUV422" },
  252. { VIDEO_PALETTE_YUYV, "YUYV" },
  253. { VIDEO_PALETTE_UYVY, "UYVY" },
  254. { VIDEO_PALETTE_YUV420, "YUV420" },
  255. { VIDEO_PALETTE_YUV411, "YUV411" },
  256. { VIDEO_PALETTE_RAW, "RAW" },
  257. { VIDEO_PALETTE_YUV422P,"YUV422P" },
  258. { VIDEO_PALETTE_YUV411P,"YUV411P" },
  259. { VIDEO_PALETTE_YUV420P,"YUV420P" },
  260. { VIDEO_PALETTE_YUV410P,"YUV410P" },
  261. { -1, NULL }
  262. };
  263. static struct symbolic_list brglist[] = {
  264. { BRG_OV511, "OV511" },
  265. { BRG_OV511PLUS, "OV511+" },
  266. { BRG_OV518, "OV518" },
  267. { BRG_OV518PLUS, "OV518+" },
  268. { -1, NULL }
  269. };
  270. static struct symbolic_list senlist[] = {
  271. { SEN_OV76BE, "OV76BE" },
  272. { SEN_OV7610, "OV7610" },
  273. { SEN_OV7620, "OV7620" },
  274. { SEN_OV7620AE, "OV7620AE" },
  275. { SEN_OV6620, "OV6620" },
  276. { SEN_OV6630, "OV6630" },
  277. { SEN_OV6630AE, "OV6630AE" },
  278. { SEN_OV6630AF, "OV6630AF" },
  279. { SEN_OV8600, "OV8600" },
  280. { SEN_KS0127, "KS0127" },
  281. { SEN_KS0127B, "KS0127B" },
  282. { SEN_SAA7111A, "SAA7111A" },
  283. { -1, NULL }
  284. };
  285. /* URB error codes: */
  286. static struct symbolic_list urb_errlist[] = {
  287. { -ENOSR, "Buffer error (overrun)" },
  288. { -EPIPE, "Stalled (device not responding)" },
  289. { -EOVERFLOW, "Babble (bad cable?)" },
  290. { -EPROTO, "Bit-stuff error (bad cable?)" },
  291. { -EILSEQ, "CRC/Timeout" },
  292. { -ETIMEDOUT, "NAK (device does not respond)" },
  293. { -1, NULL }
  294. };
  295. /**********************************************************************
  296. * Memory management
  297. **********************************************************************/
  298. static void *
  299. rvmalloc(unsigned long size)
  300. {
  301. void *mem;
  302. unsigned long adr;
  303. size = PAGE_ALIGN(size);
  304. mem = vmalloc_32(size);
  305. if (!mem)
  306. return NULL;
  307. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  308. adr = (unsigned long) mem;
  309. while (size > 0) {
  310. SetPageReserved(vmalloc_to_page((void *)adr));
  311. adr += PAGE_SIZE;
  312. size -= PAGE_SIZE;
  313. }
  314. return mem;
  315. }
  316. static void
  317. rvfree(void *mem, unsigned long size)
  318. {
  319. unsigned long adr;
  320. if (!mem)
  321. return;
  322. adr = (unsigned long) mem;
  323. while ((long) size > 0) {
  324. ClearPageReserved(vmalloc_to_page((void *)adr));
  325. adr += PAGE_SIZE;
  326. size -= PAGE_SIZE;
  327. }
  328. vfree(mem);
  329. }
  330. /**********************************************************************
  331. *
  332. * Register I/O
  333. *
  334. **********************************************************************/
  335. /* Write an OV51x register */
  336. static int
  337. reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
  338. {
  339. int rc;
  340. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  341. down(&ov->cbuf_lock);
  342. ov->cbuf[0] = value;
  343. rc = usb_control_msg(ov->dev,
  344. usb_sndctrlpipe(ov->dev, 0),
  345. (ov->bclass == BCL_OV518)?1:2 /* REG_IO */,
  346. USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  347. 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
  348. up(&ov->cbuf_lock);
  349. if (rc < 0)
  350. err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
  351. return rc;
  352. }
  353. /* Read from an OV51x register */
  354. /* returns: negative is error, pos or zero is data */
  355. static int
  356. reg_r(struct usb_ov511 *ov, unsigned char reg)
  357. {
  358. int rc;
  359. down(&ov->cbuf_lock);
  360. rc = usb_control_msg(ov->dev,
  361. usb_rcvctrlpipe(ov->dev, 0),
  362. (ov->bclass == BCL_OV518)?1:3 /* REG_IO */,
  363. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  364. 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
  365. if (rc < 0) {
  366. err("reg read: error %d: %s", rc, symbolic(urb_errlist, rc));
  367. } else {
  368. rc = ov->cbuf[0];
  369. PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
  370. }
  371. up(&ov->cbuf_lock);
  372. return rc;
  373. }
  374. /*
  375. * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
  376. * the same position as 1's in "mask" are cleared and set to "value". Bits
  377. * that are in the same position as 0's in "mask" are preserved, regardless
  378. * of their respective state in "value".
  379. */
  380. static int
  381. reg_w_mask(struct usb_ov511 *ov,
  382. unsigned char reg,
  383. unsigned char value,
  384. unsigned char mask)
  385. {
  386. int ret;
  387. unsigned char oldval, newval;
  388. ret = reg_r(ov, reg);
  389. if (ret < 0)
  390. return ret;
  391. oldval = (unsigned char) ret;
  392. oldval &= (~mask); /* Clear the masked bits */
  393. value &= mask; /* Enforce mask on value */
  394. newval = oldval | value; /* Set the desired bits */
  395. return (reg_w(ov, reg, newval));
  396. }
  397. /*
  398. * Writes multiple (n) byte value to a single register. Only valid with certain
  399. * registers (0x30 and 0xc4 - 0xce).
  400. */
  401. static int
  402. ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
  403. {
  404. int rc;
  405. PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
  406. down(&ov->cbuf_lock);
  407. *((__le32 *)ov->cbuf) = __cpu_to_le32(val);
  408. rc = usb_control_msg(ov->dev,
  409. usb_sndctrlpipe(ov->dev, 0),
  410. 1 /* REG_IO */,
  411. USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  412. 0, (__u16)reg, ov->cbuf, n, 1000);
  413. up(&ov->cbuf_lock);
  414. if (rc < 0)
  415. err("reg write multiple: error %d: %s", rc,
  416. symbolic(urb_errlist, rc));
  417. return rc;
  418. }
  419. static int
  420. ov511_upload_quan_tables(struct usb_ov511 *ov)
  421. {
  422. unsigned char *pYTable = yQuanTable511;
  423. unsigned char *pUVTable = uvQuanTable511;
  424. unsigned char val0, val1;
  425. int i, rc, reg = R511_COMP_LUT_BEGIN;
  426. PDEBUG(4, "Uploading quantization tables");
  427. for (i = 0; i < OV511_QUANTABLESIZE / 2; i++) {
  428. if (ENABLE_Y_QUANTABLE) {
  429. val0 = *pYTable++;
  430. val1 = *pYTable++;
  431. val0 &= 0x0f;
  432. val1 &= 0x0f;
  433. val0 |= val1 << 4;
  434. rc = reg_w(ov, reg, val0);
  435. if (rc < 0)
  436. return rc;
  437. }
  438. if (ENABLE_UV_QUANTABLE) {
  439. val0 = *pUVTable++;
  440. val1 = *pUVTable++;
  441. val0 &= 0x0f;
  442. val1 &= 0x0f;
  443. val0 |= val1 << 4;
  444. rc = reg_w(ov, reg + OV511_QUANTABLESIZE/2, val0);
  445. if (rc < 0)
  446. return rc;
  447. }
  448. reg++;
  449. }
  450. return 0;
  451. }
  452. /* OV518 quantization tables are 8x4 (instead of 8x8) */
  453. static int
  454. ov518_upload_quan_tables(struct usb_ov511 *ov)
  455. {
  456. unsigned char *pYTable = yQuanTable518;
  457. unsigned char *pUVTable = uvQuanTable518;
  458. unsigned char val0, val1;
  459. int i, rc, reg = R511_COMP_LUT_BEGIN;
  460. PDEBUG(4, "Uploading quantization tables");
  461. for (i = 0; i < OV518_QUANTABLESIZE / 2; i++) {
  462. if (ENABLE_Y_QUANTABLE) {
  463. val0 = *pYTable++;
  464. val1 = *pYTable++;
  465. val0 &= 0x0f;
  466. val1 &= 0x0f;
  467. val0 |= val1 << 4;
  468. rc = reg_w(ov, reg, val0);
  469. if (rc < 0)
  470. return rc;
  471. }
  472. if (ENABLE_UV_QUANTABLE) {
  473. val0 = *pUVTable++;
  474. val1 = *pUVTable++;
  475. val0 &= 0x0f;
  476. val1 &= 0x0f;
  477. val0 |= val1 << 4;
  478. rc = reg_w(ov, reg + OV518_QUANTABLESIZE/2, val0);
  479. if (rc < 0)
  480. return rc;
  481. }
  482. reg++;
  483. }
  484. return 0;
  485. }
  486. static int
  487. ov51x_reset(struct usb_ov511 *ov, unsigned char reset_type)
  488. {
  489. int rc;
  490. /* Setting bit 0 not allowed on 518/518Plus */
  491. if (ov->bclass == BCL_OV518)
  492. reset_type &= 0xfe;
  493. PDEBUG(4, "Reset: type=0x%02X", reset_type);
  494. rc = reg_w(ov, R51x_SYS_RESET, reset_type);
  495. rc = reg_w(ov, R51x_SYS_RESET, 0);
  496. if (rc < 0)
  497. err("reset: command failed");
  498. return rc;
  499. }
  500. /**********************************************************************
  501. *
  502. * Low-level I2C I/O functions
  503. *
  504. **********************************************************************/
  505. /* NOTE: Do not call this function directly!
  506. * The OV518 I2C I/O procedure is different, hence, this function.
  507. * This is normally only called from i2c_w(). Note that this function
  508. * always succeeds regardless of whether the sensor is present and working.
  509. */
  510. static int
  511. ov518_i2c_write_internal(struct usb_ov511 *ov,
  512. unsigned char reg,
  513. unsigned char value)
  514. {
  515. int rc;
  516. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  517. /* Select camera register */
  518. rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
  519. if (rc < 0)
  520. return rc;
  521. /* Write "value" to I2C data port of OV511 */
  522. rc = reg_w(ov, R51x_I2C_DATA, value);
  523. if (rc < 0)
  524. return rc;
  525. /* Initiate 3-byte write cycle */
  526. rc = reg_w(ov, R518_I2C_CTL, 0x01);
  527. if (rc < 0)
  528. return rc;
  529. return 0;
  530. }
  531. /* NOTE: Do not call this function directly! */
  532. static int
  533. ov511_i2c_write_internal(struct usb_ov511 *ov,
  534. unsigned char reg,
  535. unsigned char value)
  536. {
  537. int rc, retries;
  538. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  539. /* Three byte write cycle */
  540. for (retries = OV511_I2C_RETRIES; ; ) {
  541. /* Select camera register */
  542. rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
  543. if (rc < 0)
  544. break;
  545. /* Write "value" to I2C data port of OV511 */
  546. rc = reg_w(ov, R51x_I2C_DATA, value);
  547. if (rc < 0)
  548. break;
  549. /* Initiate 3-byte write cycle */
  550. rc = reg_w(ov, R511_I2C_CTL, 0x01);
  551. if (rc < 0)
  552. break;
  553. /* Retry until idle */
  554. do
  555. rc = reg_r(ov, R511_I2C_CTL);
  556. while (rc > 0 && ((rc&1) == 0));
  557. if (rc < 0)
  558. break;
  559. /* Ack? */
  560. if ((rc&2) == 0) {
  561. rc = 0;
  562. break;
  563. }
  564. #if 0
  565. /* I2C abort */
  566. reg_w(ov, R511_I2C_CTL, 0x10);
  567. #endif
  568. if (--retries < 0) {
  569. err("i2c write retries exhausted");
  570. rc = -1;
  571. break;
  572. }
  573. }
  574. return rc;
  575. }
  576. /* NOTE: Do not call this function directly!
  577. * The OV518 I2C I/O procedure is different, hence, this function.
  578. * This is normally only called from i2c_r(). Note that this function
  579. * always succeeds regardless of whether the sensor is present and working.
  580. */
  581. static int
  582. ov518_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
  583. {
  584. int rc, value;
  585. /* Select camera register */
  586. rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
  587. if (rc < 0)
  588. return rc;
  589. /* Initiate 2-byte write cycle */
  590. rc = reg_w(ov, R518_I2C_CTL, 0x03);
  591. if (rc < 0)
  592. return rc;
  593. /* Initiate 2-byte read cycle */
  594. rc = reg_w(ov, R518_I2C_CTL, 0x05);
  595. if (rc < 0)
  596. return rc;
  597. value = reg_r(ov, R51x_I2C_DATA);
  598. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  599. return value;
  600. }
  601. /* NOTE: Do not call this function directly!
  602. * returns: negative is error, pos or zero is data */
  603. static int
  604. ov511_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
  605. {
  606. int rc, value, retries;
  607. /* Two byte write cycle */
  608. for (retries = OV511_I2C_RETRIES; ; ) {
  609. /* Select camera register */
  610. rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
  611. if (rc < 0)
  612. return rc;
  613. /* Initiate 2-byte write cycle */
  614. rc = reg_w(ov, R511_I2C_CTL, 0x03);
  615. if (rc < 0)
  616. return rc;
  617. /* Retry until idle */
  618. do
  619. rc = reg_r(ov, R511_I2C_CTL);
  620. while (rc > 0 && ((rc&1) == 0));
  621. if (rc < 0)
  622. return rc;
  623. if ((rc&2) == 0) /* Ack? */
  624. break;
  625. /* I2C abort */
  626. reg_w(ov, R511_I2C_CTL, 0x10);
  627. if (--retries < 0) {
  628. err("i2c write retries exhausted");
  629. return -1;
  630. }
  631. }
  632. /* Two byte read cycle */
  633. for (retries = OV511_I2C_RETRIES; ; ) {
  634. /* Initiate 2-byte read cycle */
  635. rc = reg_w(ov, R511_I2C_CTL, 0x05);
  636. if (rc < 0)
  637. return rc;
  638. /* Retry until idle */
  639. do
  640. rc = reg_r(ov, R511_I2C_CTL);
  641. while (rc > 0 && ((rc&1) == 0));
  642. if (rc < 0)
  643. return rc;
  644. if ((rc&2) == 0) /* Ack? */
  645. break;
  646. /* I2C abort */
  647. rc = reg_w(ov, R511_I2C_CTL, 0x10);
  648. if (rc < 0)
  649. return rc;
  650. if (--retries < 0) {
  651. err("i2c read retries exhausted");
  652. return -1;
  653. }
  654. }
  655. value = reg_r(ov, R51x_I2C_DATA);
  656. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  657. /* This is needed to make i2c_w() work */
  658. rc = reg_w(ov, R511_I2C_CTL, 0x05);
  659. if (rc < 0)
  660. return rc;
  661. return value;
  662. }
  663. /* returns: negative is error, pos or zero is data */
  664. static int
  665. i2c_r(struct usb_ov511 *ov, unsigned char reg)
  666. {
  667. int rc;
  668. down(&ov->i2c_lock);
  669. if (ov->bclass == BCL_OV518)
  670. rc = ov518_i2c_read_internal(ov, reg);
  671. else
  672. rc = ov511_i2c_read_internal(ov, reg);
  673. up(&ov->i2c_lock);
  674. return rc;
  675. }
  676. static int
  677. i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
  678. {
  679. int rc;
  680. down(&ov->i2c_lock);
  681. if (ov->bclass == BCL_OV518)
  682. rc = ov518_i2c_write_internal(ov, reg, value);
  683. else
  684. rc = ov511_i2c_write_internal(ov, reg, value);
  685. up(&ov->i2c_lock);
  686. return rc;
  687. }
  688. /* Do not call this function directly! */
  689. static int
  690. ov51x_i2c_write_mask_internal(struct usb_ov511 *ov,
  691. unsigned char reg,
  692. unsigned char value,
  693. unsigned char mask)
  694. {
  695. int rc;
  696. unsigned char oldval, newval;
  697. if (mask == 0xff) {
  698. newval = value;
  699. } else {
  700. if (ov->bclass == BCL_OV518)
  701. rc = ov518_i2c_read_internal(ov, reg);
  702. else
  703. rc = ov511_i2c_read_internal(ov, reg);
  704. if (rc < 0)
  705. return rc;
  706. oldval = (unsigned char) rc;
  707. oldval &= (~mask); /* Clear the masked bits */
  708. value &= mask; /* Enforce mask on value */
  709. newval = oldval | value; /* Set the desired bits */
  710. }
  711. if (ov->bclass == BCL_OV518)
  712. return (ov518_i2c_write_internal(ov, reg, newval));
  713. else
  714. return (ov511_i2c_write_internal(ov, reg, newval));
  715. }
  716. /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
  717. * the same position as 1's in "mask" are cleared and set to "value". Bits
  718. * that are in the same position as 0's in "mask" are preserved, regardless
  719. * of their respective state in "value".
  720. */
  721. static int
  722. i2c_w_mask(struct usb_ov511 *ov,
  723. unsigned char reg,
  724. unsigned char value,
  725. unsigned char mask)
  726. {
  727. int rc;
  728. down(&ov->i2c_lock);
  729. rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
  730. up(&ov->i2c_lock);
  731. return rc;
  732. }
  733. /* Set the read and write slave IDs. The "slave" argument is the write slave,
  734. * and the read slave will be set to (slave + 1). ov->i2c_lock should be held
  735. * when calling this. This should not be called from outside the i2c I/O
  736. * functions.
  737. */
  738. static int
  739. i2c_set_slave_internal(struct usb_ov511 *ov, unsigned char slave)
  740. {
  741. int rc;
  742. rc = reg_w(ov, R51x_I2C_W_SID, slave);
  743. if (rc < 0)
  744. return rc;
  745. rc = reg_w(ov, R51x_I2C_R_SID, slave + 1);
  746. if (rc < 0)
  747. return rc;
  748. return 0;
  749. }
  750. /* Write to a specific I2C slave ID and register, using the specified mask */
  751. static int
  752. i2c_w_slave(struct usb_ov511 *ov,
  753. unsigned char slave,
  754. unsigned char reg,
  755. unsigned char value,
  756. unsigned char mask)
  757. {
  758. int rc = 0;
  759. down(&ov->i2c_lock);
  760. /* Set new slave IDs */
  761. rc = i2c_set_slave_internal(ov, slave);
  762. if (rc < 0)
  763. goto out;
  764. rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
  765. out:
  766. /* Restore primary IDs */
  767. if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
  768. err("Couldn't restore primary I2C slave");
  769. up(&ov->i2c_lock);
  770. return rc;
  771. }
  772. /* Read from a specific I2C slave ID and register */
  773. static int
  774. i2c_r_slave(struct usb_ov511 *ov,
  775. unsigned char slave,
  776. unsigned char reg)
  777. {
  778. int rc;
  779. down(&ov->i2c_lock);
  780. /* Set new slave IDs */
  781. rc = i2c_set_slave_internal(ov, slave);
  782. if (rc < 0)
  783. goto out;
  784. if (ov->bclass == BCL_OV518)
  785. rc = ov518_i2c_read_internal(ov, reg);
  786. else
  787. rc = ov511_i2c_read_internal(ov, reg);
  788. out:
  789. /* Restore primary IDs */
  790. if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
  791. err("Couldn't restore primary I2C slave");
  792. up(&ov->i2c_lock);
  793. return rc;
  794. }
  795. /* Sets I2C read and write slave IDs. Returns <0 for error */
  796. static int
  797. ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
  798. {
  799. int rc;
  800. down(&ov->i2c_lock);
  801. rc = i2c_set_slave_internal(ov, sid);
  802. if (rc < 0)
  803. goto out;
  804. // FIXME: Is this actually necessary?
  805. rc = ov51x_reset(ov, OV511_RESET_NOREGS);
  806. out:
  807. up(&ov->i2c_lock);
  808. return rc;
  809. }
  810. static int
  811. write_regvals(struct usb_ov511 *ov, struct ov511_regvals * pRegvals)
  812. {
  813. int rc;
  814. while (pRegvals->bus != OV511_DONE_BUS) {
  815. if (pRegvals->bus == OV511_REG_BUS) {
  816. if ((rc = reg_w(ov, pRegvals->reg, pRegvals->val)) < 0)
  817. return rc;
  818. } else if (pRegvals->bus == OV511_I2C_BUS) {
  819. if ((rc = i2c_w(ov, pRegvals->reg, pRegvals->val)) < 0)
  820. return rc;
  821. } else {
  822. err("Bad regval array");
  823. return -1;
  824. }
  825. pRegvals++;
  826. }
  827. return 0;
  828. }
  829. #ifdef OV511_DEBUG
  830. static void
  831. dump_i2c_range(struct usb_ov511 *ov, int reg1, int regn)
  832. {
  833. int i, rc;
  834. for (i = reg1; i <= regn; i++) {
  835. rc = i2c_r(ov, i);
  836. info("Sensor[0x%02X] = 0x%02X", i, rc);
  837. }
  838. }
  839. static void
  840. dump_i2c_regs(struct usb_ov511 *ov)
  841. {
  842. info("I2C REGS");
  843. dump_i2c_range(ov, 0x00, 0x7C);
  844. }
  845. static void
  846. dump_reg_range(struct usb_ov511 *ov, int reg1, int regn)
  847. {
  848. int i, rc;
  849. for (i = reg1; i <= regn; i++) {
  850. rc = reg_r(ov, i);
  851. info("OV511[0x%02X] = 0x%02X", i, rc);
  852. }
  853. }
  854. static void
  855. ov511_dump_regs(struct usb_ov511 *ov)
  856. {
  857. info("CAMERA INTERFACE REGS");
  858. dump_reg_range(ov, 0x10, 0x1f);
  859. info("DRAM INTERFACE REGS");
  860. dump_reg_range(ov, 0x20, 0x23);
  861. info("ISO FIFO REGS");
  862. dump_reg_range(ov, 0x30, 0x31);
  863. info("PIO REGS");
  864. dump_reg_range(ov, 0x38, 0x39);
  865. dump_reg_range(ov, 0x3e, 0x3e);
  866. info("I2C REGS");
  867. dump_reg_range(ov, 0x40, 0x49);
  868. info("SYSTEM CONTROL REGS");
  869. dump_reg_range(ov, 0x50, 0x55);
  870. dump_reg_range(ov, 0x5e, 0x5f);
  871. info("OmniCE REGS");
  872. dump_reg_range(ov, 0x70, 0x79);
  873. /* NOTE: Quantization tables are not readable. You will get the value
  874. * in reg. 0x79 for every table register */
  875. dump_reg_range(ov, 0x80, 0x9f);
  876. dump_reg_range(ov, 0xa0, 0xbf);
  877. }
  878. static void
  879. ov518_dump_regs(struct usb_ov511 *ov)
  880. {
  881. info("VIDEO MODE REGS");
  882. dump_reg_range(ov, 0x20, 0x2f);
  883. info("DATA PUMP AND SNAPSHOT REGS");
  884. dump_reg_range(ov, 0x30, 0x3f);
  885. info("I2C REGS");
  886. dump_reg_range(ov, 0x40, 0x4f);
  887. info("SYSTEM CONTROL AND VENDOR REGS");
  888. dump_reg_range(ov, 0x50, 0x5f);
  889. info("60 - 6F");
  890. dump_reg_range(ov, 0x60, 0x6f);
  891. info("70 - 7F");
  892. dump_reg_range(ov, 0x70, 0x7f);
  893. info("Y QUANTIZATION TABLE");
  894. dump_reg_range(ov, 0x80, 0x8f);
  895. info("UV QUANTIZATION TABLE");
  896. dump_reg_range(ov, 0x90, 0x9f);
  897. info("A0 - BF");
  898. dump_reg_range(ov, 0xa0, 0xbf);
  899. info("CBR");
  900. dump_reg_range(ov, 0xc0, 0xcf);
  901. }
  902. #endif
  903. /*****************************************************************************/
  904. /* Temporarily stops OV511 from functioning. Must do this before changing
  905. * registers while the camera is streaming */
  906. static inline int
  907. ov51x_stop(struct usb_ov511 *ov)
  908. {
  909. PDEBUG(4, "stopping");
  910. ov->stopped = 1;
  911. if (ov->bclass == BCL_OV518)
  912. return (reg_w_mask(ov, R51x_SYS_RESET, 0x3a, 0x3a));
  913. else
  914. return (reg_w(ov, R51x_SYS_RESET, 0x3d));
  915. }
  916. /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
  917. * actually stopped (for performance). */
  918. static inline int
  919. ov51x_restart(struct usb_ov511 *ov)
  920. {
  921. if (ov->stopped) {
  922. PDEBUG(4, "restarting");
  923. ov->stopped = 0;
  924. /* Reinitialize the stream */
  925. if (ov->bclass == BCL_OV518)
  926. reg_w(ov, 0x2f, 0x80);
  927. return (reg_w(ov, R51x_SYS_RESET, 0x00));
  928. }
  929. return 0;
  930. }
  931. /* Sleeps until no frames are active. Returns !0 if got signal */
  932. static int
  933. ov51x_wait_frames_inactive(struct usb_ov511 *ov)
  934. {
  935. return wait_event_interruptible(ov->wq, ov->curframe < 0);
  936. }
  937. /* Resets the hardware snapshot button */
  938. static void
  939. ov51x_clear_snapshot(struct usb_ov511 *ov)
  940. {
  941. if (ov->bclass == BCL_OV511) {
  942. reg_w(ov, R51x_SYS_SNAP, 0x00);
  943. reg_w(ov, R51x_SYS_SNAP, 0x02);
  944. reg_w(ov, R51x_SYS_SNAP, 0x00);
  945. } else if (ov->bclass == BCL_OV518) {
  946. warn("snapshot reset not supported yet on OV518(+)");
  947. } else {
  948. err("clear snap: invalid bridge type");
  949. }
  950. }
  951. #if 0
  952. /* Checks the status of the snapshot button. Returns 1 if it was pressed since
  953. * it was last cleared, and zero in all other cases (including errors) */
  954. static int
  955. ov51x_check_snapshot(struct usb_ov511 *ov)
  956. {
  957. int ret, status = 0;
  958. if (ov->bclass == BCL_OV511) {
  959. ret = reg_r(ov, R51x_SYS_SNAP);
  960. if (ret < 0) {
  961. err("Error checking snspshot status (%d)", ret);
  962. } else if (ret & 0x08) {
  963. status = 1;
  964. }
  965. } else if (ov->bclass == BCL_OV518) {
  966. warn("snapshot check not supported yet on OV518(+)");
  967. } else {
  968. err("check snap: invalid bridge type");
  969. }
  970. return status;
  971. }
  972. #endif
  973. /* This does an initial reset of an OmniVision sensor and ensures that I2C
  974. * is synchronized. Returns <0 for failure.
  975. */
  976. static int
  977. init_ov_sensor(struct usb_ov511 *ov)
  978. {
  979. int i, success;
  980. /* Reset the sensor */
  981. if (i2c_w(ov, 0x12, 0x80) < 0)
  982. return -EIO;
  983. /* Wait for it to initialize */
  984. msleep(150);
  985. for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
  986. if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
  987. (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
  988. success = 1;
  989. continue;
  990. }
  991. /* Reset the sensor */
  992. if (i2c_w(ov, 0x12, 0x80) < 0)
  993. return -EIO;
  994. /* Wait for it to initialize */
  995. msleep(150);
  996. /* Dummy read to sync I2C */
  997. if (i2c_r(ov, 0x00) < 0)
  998. return -EIO;
  999. }
  1000. if (!success)
  1001. return -EIO;
  1002. PDEBUG(1, "I2C synced in %d attempt(s)", i);
  1003. return 0;
  1004. }
  1005. static int
  1006. ov511_set_packet_size(struct usb_ov511 *ov, int size)
  1007. {
  1008. int alt, mult;
  1009. if (ov51x_stop(ov) < 0)
  1010. return -EIO;
  1011. mult = size >> 5;
  1012. if (ov->bridge == BRG_OV511) {
  1013. if (size == 0)
  1014. alt = OV511_ALT_SIZE_0;
  1015. else if (size == 257)
  1016. alt = OV511_ALT_SIZE_257;
  1017. else if (size == 513)
  1018. alt = OV511_ALT_SIZE_513;
  1019. else if (size == 769)
  1020. alt = OV511_ALT_SIZE_769;
  1021. else if (size == 993)
  1022. alt = OV511_ALT_SIZE_993;
  1023. else {
  1024. err("Set packet size: invalid size (%d)", size);
  1025. return -EINVAL;
  1026. }
  1027. } else if (ov->bridge == BRG_OV511PLUS) {
  1028. if (size == 0)
  1029. alt = OV511PLUS_ALT_SIZE_0;
  1030. else if (size == 33)
  1031. alt = OV511PLUS_ALT_SIZE_33;
  1032. else if (size == 129)
  1033. alt = OV511PLUS_ALT_SIZE_129;
  1034. else if (size == 257)
  1035. alt = OV511PLUS_ALT_SIZE_257;
  1036. else if (size == 385)
  1037. alt = OV511PLUS_ALT_SIZE_385;
  1038. else if (size == 513)
  1039. alt = OV511PLUS_ALT_SIZE_513;
  1040. else if (size == 769)
  1041. alt = OV511PLUS_ALT_SIZE_769;
  1042. else if (size == 961)
  1043. alt = OV511PLUS_ALT_SIZE_961;
  1044. else {
  1045. err("Set packet size: invalid size (%d)", size);
  1046. return -EINVAL;
  1047. }
  1048. } else {
  1049. err("Set packet size: Invalid bridge type");
  1050. return -EINVAL;
  1051. }
  1052. PDEBUG(3, "%d, mult=%d, alt=%d", size, mult, alt);
  1053. if (reg_w(ov, R51x_FIFO_PSIZE, mult) < 0)
  1054. return -EIO;
  1055. if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
  1056. err("Set packet size: set interface error");
  1057. return -EBUSY;
  1058. }
  1059. if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
  1060. return -EIO;
  1061. ov->packet_size = size;
  1062. if (ov51x_restart(ov) < 0)
  1063. return -EIO;
  1064. return 0;
  1065. }
  1066. /* Note: Unlike the OV511/OV511+, the size argument does NOT include the
  1067. * optional packet number byte. The actual size *is* stored in ov->packet_size,
  1068. * though. */
  1069. static int
  1070. ov518_set_packet_size(struct usb_ov511 *ov, int size)
  1071. {
  1072. int alt;
  1073. if (ov51x_stop(ov) < 0)
  1074. return -EIO;
  1075. if (ov->bclass == BCL_OV518) {
  1076. if (size == 0)
  1077. alt = OV518_ALT_SIZE_0;
  1078. else if (size == 128)
  1079. alt = OV518_ALT_SIZE_128;
  1080. else if (size == 256)
  1081. alt = OV518_ALT_SIZE_256;
  1082. else if (size == 384)
  1083. alt = OV518_ALT_SIZE_384;
  1084. else if (size == 512)
  1085. alt = OV518_ALT_SIZE_512;
  1086. else if (size == 640)
  1087. alt = OV518_ALT_SIZE_640;
  1088. else if (size == 768)
  1089. alt = OV518_ALT_SIZE_768;
  1090. else if (size == 896)
  1091. alt = OV518_ALT_SIZE_896;
  1092. else {
  1093. err("Set packet size: invalid size (%d)", size);
  1094. return -EINVAL;
  1095. }
  1096. } else {
  1097. err("Set packet size: Invalid bridge type");
  1098. return -EINVAL;
  1099. }
  1100. PDEBUG(3, "%d, alt=%d", size, alt);
  1101. ov->packet_size = size;
  1102. if (size > 0) {
  1103. /* Program ISO FIFO size reg (packet number isn't included) */
  1104. ov518_reg_w32(ov, 0x30, size, 2);
  1105. if (ov->packet_numbering)
  1106. ++ov->packet_size;
  1107. }
  1108. if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
  1109. err("Set packet size: set interface error");
  1110. return -EBUSY;
  1111. }
  1112. /* Initialize the stream */
  1113. if (reg_w(ov, 0x2f, 0x80) < 0)
  1114. return -EIO;
  1115. if (ov51x_restart(ov) < 0)
  1116. return -EIO;
  1117. if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
  1118. return -EIO;
  1119. return 0;
  1120. }
  1121. /* Upload compression params and quantization tables. Returns 0 for success. */
  1122. static int
  1123. ov511_init_compression(struct usb_ov511 *ov)
  1124. {
  1125. int rc = 0;
  1126. if (!ov->compress_inited) {
  1127. reg_w(ov, 0x70, phy);
  1128. reg_w(ov, 0x71, phuv);
  1129. reg_w(ov, 0x72, pvy);
  1130. reg_w(ov, 0x73, pvuv);
  1131. reg_w(ov, 0x74, qhy);
  1132. reg_w(ov, 0x75, qhuv);
  1133. reg_w(ov, 0x76, qvy);
  1134. reg_w(ov, 0x77, qvuv);
  1135. if (ov511_upload_quan_tables(ov) < 0) {
  1136. err("Error uploading quantization tables");
  1137. rc = -EIO;
  1138. goto out;
  1139. }
  1140. }
  1141. ov->compress_inited = 1;
  1142. out:
  1143. return rc;
  1144. }
  1145. /* Upload compression params and quantization tables. Returns 0 for success. */
  1146. static int
  1147. ov518_init_compression(struct usb_ov511 *ov)
  1148. {
  1149. int rc = 0;
  1150. if (!ov->compress_inited) {
  1151. if (ov518_upload_quan_tables(ov) < 0) {
  1152. err("Error uploading quantization tables");
  1153. rc = -EIO;
  1154. goto out;
  1155. }
  1156. }
  1157. ov->compress_inited = 1;
  1158. out:
  1159. return rc;
  1160. }
  1161. /* -------------------------------------------------------------------------- */
  1162. /* Sets sensor's contrast setting to "val" */
  1163. static int
  1164. sensor_set_contrast(struct usb_ov511 *ov, unsigned short val)
  1165. {
  1166. int rc;
  1167. PDEBUG(3, "%d", val);
  1168. if (ov->stop_during_set)
  1169. if (ov51x_stop(ov) < 0)
  1170. return -EIO;
  1171. switch (ov->sensor) {
  1172. case SEN_OV7610:
  1173. case SEN_OV6620:
  1174. {
  1175. rc = i2c_w(ov, OV7610_REG_CNT, val >> 8);
  1176. if (rc < 0)
  1177. goto out;
  1178. break;
  1179. }
  1180. case SEN_OV6630:
  1181. {
  1182. rc = i2c_w_mask(ov, OV7610_REG_CNT, val >> 12, 0x0f);
  1183. if (rc < 0)
  1184. goto out;
  1185. break;
  1186. }
  1187. case SEN_OV7620:
  1188. {
  1189. unsigned char ctab[] = {
  1190. 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
  1191. 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
  1192. };
  1193. /* Use Y gamma control instead. Bit 0 enables it. */
  1194. rc = i2c_w(ov, 0x64, ctab[val>>12]);
  1195. if (rc < 0)
  1196. goto out;
  1197. break;
  1198. }
  1199. case SEN_SAA7111A:
  1200. {
  1201. rc = i2c_w(ov, 0x0b, val >> 9);
  1202. if (rc < 0)
  1203. goto out;
  1204. break;
  1205. }
  1206. default:
  1207. {
  1208. PDEBUG(3, "Unsupported with this sensor");
  1209. rc = -EPERM;
  1210. goto out;
  1211. }
  1212. }
  1213. rc = 0; /* Success */
  1214. ov->contrast = val;
  1215. out:
  1216. if (ov51x_restart(ov) < 0)
  1217. return -EIO;
  1218. return rc;
  1219. }
  1220. /* Gets sensor's contrast setting */
  1221. static int
  1222. sensor_get_contrast(struct usb_ov511 *ov, unsigned short *val)
  1223. {
  1224. int rc;
  1225. switch (ov->sensor) {
  1226. case SEN_OV7610:
  1227. case SEN_OV6620:
  1228. rc = i2c_r(ov, OV7610_REG_CNT);
  1229. if (rc < 0)
  1230. return rc;
  1231. else
  1232. *val = rc << 8;
  1233. break;
  1234. case SEN_OV6630:
  1235. rc = i2c_r(ov, OV7610_REG_CNT);
  1236. if (rc < 0)
  1237. return rc;
  1238. else
  1239. *val = rc << 12;
  1240. break;
  1241. case SEN_OV7620:
  1242. /* Use Y gamma reg instead. Bit 0 is the enable bit. */
  1243. rc = i2c_r(ov, 0x64);
  1244. if (rc < 0)
  1245. return rc;
  1246. else
  1247. *val = (rc & 0xfe) << 8;
  1248. break;
  1249. case SEN_SAA7111A:
  1250. *val = ov->contrast;
  1251. break;
  1252. default:
  1253. PDEBUG(3, "Unsupported with this sensor");
  1254. return -EPERM;
  1255. }
  1256. PDEBUG(3, "%d", *val);
  1257. ov->contrast = *val;
  1258. return 0;
  1259. }
  1260. /* -------------------------------------------------------------------------- */
  1261. /* Sets sensor's brightness setting to "val" */
  1262. static int
  1263. sensor_set_brightness(struct usb_ov511 *ov, unsigned short val)
  1264. {
  1265. int rc;
  1266. PDEBUG(4, "%d", val);
  1267. if (ov->stop_during_set)
  1268. if (ov51x_stop(ov) < 0)
  1269. return -EIO;
  1270. switch (ov->sensor) {
  1271. case SEN_OV7610:
  1272. case SEN_OV76BE:
  1273. case SEN_OV6620:
  1274. case SEN_OV6630:
  1275. rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
  1276. if (rc < 0)
  1277. goto out;
  1278. break;
  1279. case SEN_OV7620:
  1280. /* 7620 doesn't like manual changes when in auto mode */
  1281. if (!ov->auto_brt) {
  1282. rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
  1283. if (rc < 0)
  1284. goto out;
  1285. }
  1286. break;
  1287. case SEN_SAA7111A:
  1288. rc = i2c_w(ov, 0x0a, val >> 8);
  1289. if (rc < 0)
  1290. goto out;
  1291. break;
  1292. default:
  1293. PDEBUG(3, "Unsupported with this sensor");
  1294. rc = -EPERM;
  1295. goto out;
  1296. }
  1297. rc = 0; /* Success */
  1298. ov->brightness = val;
  1299. out:
  1300. if (ov51x_restart(ov) < 0)
  1301. return -EIO;
  1302. return rc;
  1303. }
  1304. /* Gets sensor's brightness setting */
  1305. static int
  1306. sensor_get_brightness(struct usb_ov511 *ov, unsigned short *val)
  1307. {
  1308. int rc;
  1309. switch (ov->sensor) {
  1310. case SEN_OV7610:
  1311. case SEN_OV76BE:
  1312. case SEN_OV7620:
  1313. case SEN_OV6620:
  1314. case SEN_OV6630:
  1315. rc = i2c_r(ov, OV7610_REG_BRT);
  1316. if (rc < 0)
  1317. return rc;
  1318. else
  1319. *val = rc << 8;
  1320. break;
  1321. case SEN_SAA7111A:
  1322. *val = ov->brightness;
  1323. break;
  1324. default:
  1325. PDEBUG(3, "Unsupported with this sensor");
  1326. return -EPERM;
  1327. }
  1328. PDEBUG(3, "%d", *val);
  1329. ov->brightness = *val;
  1330. return 0;
  1331. }
  1332. /* -------------------------------------------------------------------------- */
  1333. /* Sets sensor's saturation (color intensity) setting to "val" */
  1334. static int
  1335. sensor_set_saturation(struct usb_ov511 *ov, unsigned short val)
  1336. {
  1337. int rc;
  1338. PDEBUG(3, "%d", val);
  1339. if (ov->stop_during_set)
  1340. if (ov51x_stop(ov) < 0)
  1341. return -EIO;
  1342. switch (ov->sensor) {
  1343. case SEN_OV7610:
  1344. case SEN_OV76BE:
  1345. case SEN_OV6620:
  1346. case SEN_OV6630:
  1347. rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
  1348. if (rc < 0)
  1349. goto out;
  1350. break;
  1351. case SEN_OV7620:
  1352. // /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
  1353. // rc = ov_i2c_write(ov->dev, 0x62, (val >> 9) & 0x7e);
  1354. // if (rc < 0)
  1355. // goto out;
  1356. rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
  1357. if (rc < 0)
  1358. goto out;
  1359. break;
  1360. case SEN_SAA7111A:
  1361. rc = i2c_w(ov, 0x0c, val >> 9);
  1362. if (rc < 0)
  1363. goto out;
  1364. break;
  1365. default:
  1366. PDEBUG(3, "Unsupported with this sensor");
  1367. rc = -EPERM;
  1368. goto out;
  1369. }
  1370. rc = 0; /* Success */
  1371. ov->colour = val;
  1372. out:
  1373. if (ov51x_restart(ov) < 0)
  1374. return -EIO;
  1375. return rc;
  1376. }
  1377. /* Gets sensor's saturation (color intensity) setting */
  1378. static int
  1379. sensor_get_saturation(struct usb_ov511 *ov, unsigned short *val)
  1380. {
  1381. int rc;
  1382. switch (ov->sensor) {
  1383. case SEN_OV7610:
  1384. case SEN_OV76BE:
  1385. case SEN_OV6620:
  1386. case SEN_OV6630:
  1387. rc = i2c_r(ov, OV7610_REG_SAT);
  1388. if (rc < 0)
  1389. return rc;
  1390. else
  1391. *val = rc << 8;
  1392. break;
  1393. case SEN_OV7620:
  1394. // /* Use UV gamma reg instead. Bits 0 & 7 are reserved. */
  1395. // rc = i2c_r(ov, 0x62);
  1396. // if (rc < 0)
  1397. // return rc;
  1398. // else
  1399. // *val = (rc & 0x7e) << 9;
  1400. rc = i2c_r(ov, OV7610_REG_SAT);
  1401. if (rc < 0)
  1402. return rc;
  1403. else
  1404. *val = rc << 8;
  1405. break;
  1406. case SEN_SAA7111A:
  1407. *val = ov->colour;
  1408. break;
  1409. default:
  1410. PDEBUG(3, "Unsupported with this sensor");
  1411. return -EPERM;
  1412. }
  1413. PDEBUG(3, "%d", *val);
  1414. ov->colour = *val;
  1415. return 0;
  1416. }
  1417. /* -------------------------------------------------------------------------- */
  1418. /* Sets sensor's hue (red/blue balance) setting to "val" */
  1419. static int
  1420. sensor_set_hue(struct usb_ov511 *ov, unsigned short val)
  1421. {
  1422. int rc;
  1423. PDEBUG(3, "%d", val);
  1424. if (ov->stop_during_set)
  1425. if (ov51x_stop(ov) < 0)
  1426. return -EIO;
  1427. switch (ov->sensor) {
  1428. case SEN_OV7610:
  1429. case SEN_OV6620:
  1430. case SEN_OV6630:
  1431. rc = i2c_w(ov, OV7610_REG_RED, 0xFF - (val >> 8));
  1432. if (rc < 0)
  1433. goto out;
  1434. rc = i2c_w(ov, OV7610_REG_BLUE, val >> 8);
  1435. if (rc < 0)
  1436. goto out;
  1437. break;
  1438. case SEN_OV7620:
  1439. // Hue control is causing problems. I will enable it once it's fixed.
  1440. #if 0
  1441. rc = i2c_w(ov, 0x7a, (unsigned char)(val >> 8) + 0xb);
  1442. if (rc < 0)
  1443. goto out;
  1444. rc = i2c_w(ov, 0x79, (unsigned char)(val >> 8) + 0xb);
  1445. if (rc < 0)
  1446. goto out;
  1447. #endif
  1448. break;
  1449. case SEN_SAA7111A:
  1450. rc = i2c_w(ov, 0x0d, (val + 32768) >> 8);
  1451. if (rc < 0)
  1452. goto out;
  1453. break;
  1454. default:
  1455. PDEBUG(3, "Unsupported with this sensor");
  1456. rc = -EPERM;
  1457. goto out;
  1458. }
  1459. rc = 0; /* Success */
  1460. ov->hue = val;
  1461. out:
  1462. if (ov51x_restart(ov) < 0)
  1463. return -EIO;
  1464. return rc;
  1465. }
  1466. /* Gets sensor's hue (red/blue balance) setting */
  1467. static int
  1468. sensor_get_hue(struct usb_ov511 *ov, unsigned short *val)
  1469. {
  1470. int rc;
  1471. switch (ov->sensor) {
  1472. case SEN_OV7610:
  1473. case SEN_OV6620:
  1474. case SEN_OV6630:
  1475. rc = i2c_r(ov, OV7610_REG_BLUE);
  1476. if (rc < 0)
  1477. return rc;
  1478. else
  1479. *val = rc << 8;
  1480. break;
  1481. case SEN_OV7620:
  1482. rc = i2c_r(ov, 0x7a);
  1483. if (rc < 0)
  1484. return rc;
  1485. else
  1486. *val = rc << 8;
  1487. break;
  1488. case SEN_SAA7111A:
  1489. *val = ov->hue;
  1490. break;
  1491. default:
  1492. PDEBUG(3, "Unsupported with this sensor");
  1493. return -EPERM;
  1494. }
  1495. PDEBUG(3, "%d", *val);
  1496. ov->hue = *val;
  1497. return 0;
  1498. }
  1499. /* -------------------------------------------------------------------------- */
  1500. static int
  1501. sensor_set_picture(struct usb_ov511 *ov, struct video_picture *p)
  1502. {
  1503. int rc;
  1504. PDEBUG(4, "sensor_set_picture");
  1505. ov->whiteness = p->whiteness;
  1506. /* Don't return error if a setting is unsupported, or rest of settings
  1507. * will not be performed */
  1508. rc = sensor_set_contrast(ov, p->contrast);
  1509. if (FATAL_ERROR(rc))
  1510. return rc;
  1511. rc = sensor_set_brightness(ov, p->brightness);
  1512. if (FATAL_ERROR(rc))
  1513. return rc;
  1514. rc = sensor_set_saturation(ov, p->colour);
  1515. if (FATAL_ERROR(rc))
  1516. return rc;
  1517. rc = sensor_set_hue(ov, p->hue);
  1518. if (FATAL_ERROR(rc))
  1519. return rc;
  1520. return 0;
  1521. }
  1522. static int
  1523. sensor_get_picture(struct usb_ov511 *ov, struct video_picture *p)
  1524. {
  1525. int rc;
  1526. PDEBUG(4, "sensor_get_picture");
  1527. /* Don't return error if a setting is unsupported, or rest of settings
  1528. * will not be performed */
  1529. rc = sensor_get_contrast(ov, &(p->contrast));
  1530. if (FATAL_ERROR(rc))
  1531. return rc;
  1532. rc = sensor_get_brightness(ov, &(p->brightness));
  1533. if (FATAL_ERROR(rc))
  1534. return rc;
  1535. rc = sensor_get_saturation(ov, &(p->colour));
  1536. if (FATAL_ERROR(rc))
  1537. return rc;
  1538. rc = sensor_get_hue(ov, &(p->hue));
  1539. if (FATAL_ERROR(rc))
  1540. return rc;
  1541. p->whiteness = 105 << 8;
  1542. return 0;
  1543. }
  1544. #if 0
  1545. // FIXME: Exposure range is only 0x00-0x7f in interlace mode
  1546. /* Sets current exposure for sensor. This only has an effect if auto-exposure
  1547. * is off */
  1548. static inline int
  1549. sensor_set_exposure(struct usb_ov511 *ov, unsigned char val)
  1550. {
  1551. int rc;
  1552. PDEBUG(3, "%d", val);
  1553. if (ov->stop_during_set)
  1554. if (ov51x_stop(ov) < 0)
  1555. return -EIO;
  1556. switch (ov->sensor) {
  1557. case SEN_OV6620:
  1558. case SEN_OV6630:
  1559. case SEN_OV7610:
  1560. case SEN_OV7620:
  1561. case SEN_OV76BE:
  1562. case SEN_OV8600:
  1563. rc = i2c_w(ov, 0x10, val);
  1564. if (rc < 0)
  1565. goto out;
  1566. break;
  1567. case SEN_KS0127:
  1568. case SEN_KS0127B:
  1569. case SEN_SAA7111A:
  1570. PDEBUG(3, "Unsupported with this sensor");
  1571. return -EPERM;
  1572. default:
  1573. err("Sensor not supported for set_exposure");
  1574. return -EINVAL;
  1575. }
  1576. rc = 0; /* Success */
  1577. ov->exposure = val;
  1578. out:
  1579. if (ov51x_restart(ov) < 0)
  1580. return -EIO;
  1581. return rc;
  1582. }
  1583. #endif
  1584. /* Gets current exposure level from sensor, regardless of whether it is under
  1585. * manual control. */
  1586. static int
  1587. sensor_get_exposure(struct usb_ov511 *ov, unsigned char *val)
  1588. {
  1589. int rc;
  1590. switch (ov->sensor) {
  1591. case SEN_OV7610:
  1592. case SEN_OV6620:
  1593. case SEN_OV6630:
  1594. case SEN_OV7620:
  1595. case SEN_OV76BE:
  1596. case SEN_OV8600:
  1597. rc = i2c_r(ov, 0x10);
  1598. if (rc < 0)
  1599. return rc;
  1600. else
  1601. *val = rc;
  1602. break;
  1603. case SEN_KS0127:
  1604. case SEN_KS0127B:
  1605. case SEN_SAA7111A:
  1606. val = NULL;
  1607. PDEBUG(3, "Unsupported with this sensor");
  1608. return -EPERM;
  1609. default:
  1610. err("Sensor not supported for get_exposure");
  1611. return -EINVAL;
  1612. }
  1613. PDEBUG(3, "%d", *val);
  1614. ov->exposure = *val;
  1615. return 0;
  1616. }
  1617. /* Turns on or off the LED. Only has an effect with OV511+/OV518(+) */
  1618. static void
  1619. ov51x_led_control(struct usb_ov511 *ov, int enable)
  1620. {
  1621. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1622. if (ov->bridge == BRG_OV511PLUS)
  1623. reg_w(ov, R511_SYS_LED_CTL, enable ? 1 : 0);
  1624. else if (ov->bclass == BCL_OV518)
  1625. reg_w_mask(ov, R518_GPIO_OUT, enable ? 0x02 : 0x00, 0x02);
  1626. return;
  1627. }
  1628. /* Matches the sensor's internal frame rate to the lighting frequency.
  1629. * Valid frequencies are:
  1630. * 50 - 50Hz, for European and Asian lighting
  1631. * 60 - 60Hz, for American lighting
  1632. *
  1633. * Tested with: OV7610, OV7620, OV76BE, OV6620
  1634. * Unsupported: KS0127, KS0127B, SAA7111A
  1635. * Returns: 0 for success
  1636. */
  1637. static int
  1638. sensor_set_light_freq(struct usb_ov511 *ov, int freq)
  1639. {
  1640. int sixty;
  1641. PDEBUG(4, "%d Hz", freq);
  1642. if (freq == 60)
  1643. sixty = 1;
  1644. else if (freq == 50)
  1645. sixty = 0;
  1646. else {
  1647. err("Invalid light freq (%d Hz)", freq);
  1648. return -EINVAL;
  1649. }
  1650. switch (ov->sensor) {
  1651. case SEN_OV7610:
  1652. i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
  1653. i2c_w(ov, 0x2b, sixty?0x00:0xac);
  1654. i2c_w_mask(ov, 0x13, 0x10, 0x10);
  1655. i2c_w_mask(ov, 0x13, 0x00, 0x10);
  1656. break;
  1657. case SEN_OV7620:
  1658. case SEN_OV76BE:
  1659. case SEN_OV8600:
  1660. i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
  1661. i2c_w(ov, 0x2b, sixty?0x00:0xac);
  1662. i2c_w_mask(ov, 0x76, 0x01, 0x01);
  1663. break;
  1664. case SEN_OV6620:
  1665. case SEN_OV6630:
  1666. i2c_w(ov, 0x2b, sixty?0xa8:0x28);
  1667. i2c_w(ov, 0x2a, sixty?0x84:0xa4);
  1668. break;
  1669. case SEN_KS0127:
  1670. case SEN_KS0127B:
  1671. case SEN_SAA7111A:
  1672. PDEBUG(5, "Unsupported with this sensor");
  1673. return -EPERM;
  1674. default:
  1675. err("Sensor not supported for set_light_freq");
  1676. return -EINVAL;
  1677. }
  1678. ov->lightfreq = freq;
  1679. return 0;
  1680. }
  1681. /* If enable is true, turn on the sensor's banding filter, otherwise turn it
  1682. * off. This filter tries to reduce the pattern of horizontal light/dark bands
  1683. * caused by some (usually fluorescent) lighting. The light frequency must be
  1684. * set either before or after enabling it with ov51x_set_light_freq().
  1685. *
  1686. * Tested with: OV7610, OV7620, OV76BE, OV6620.
  1687. * Unsupported: KS0127, KS0127B, SAA7111A
  1688. * Returns: 0 for success
  1689. */
  1690. static int
  1691. sensor_set_banding_filter(struct usb_ov511 *ov, int enable)
  1692. {
  1693. int rc;
  1694. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1695. if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
  1696. || ov->sensor == SEN_SAA7111A) {
  1697. PDEBUG(5, "Unsupported with this sensor");
  1698. return -EPERM;
  1699. }
  1700. rc = i2c_w_mask(ov, 0x2d, enable?0x04:0x00, 0x04);
  1701. if (rc < 0)
  1702. return rc;
  1703. ov->bandfilt = enable;
  1704. return 0;
  1705. }
  1706. /* If enable is true, turn on the sensor's auto brightness control, otherwise
  1707. * turn it off.
  1708. *
  1709. * Unsupported: KS0127, KS0127B, SAA7111A
  1710. * Returns: 0 for success
  1711. */
  1712. static int
  1713. sensor_set_auto_brightness(struct usb_ov511 *ov, int enable)
  1714. {
  1715. int rc;
  1716. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1717. if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
  1718. || ov->sensor == SEN_SAA7111A) {
  1719. PDEBUG(5, "Unsupported with this sensor");
  1720. return -EPERM;
  1721. }
  1722. rc = i2c_w_mask(ov, 0x2d, enable?0x10:0x00, 0x10);
  1723. if (rc < 0)
  1724. return rc;
  1725. ov->auto_brt = enable;
  1726. return 0;
  1727. }
  1728. /* If enable is true, turn on the sensor's auto exposure control, otherwise
  1729. * turn it off.
  1730. *
  1731. * Unsupported: KS0127, KS0127B, SAA7111A
  1732. * Returns: 0 for success
  1733. */
  1734. static int
  1735. sensor_set_auto_exposure(struct usb_ov511 *ov, int enable)
  1736. {
  1737. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1738. switch (ov->sensor) {
  1739. case SEN_OV7610:
  1740. i2c_w_mask(ov, 0x29, enable?0x00:0x80, 0x80);
  1741. break;
  1742. case SEN_OV6620:
  1743. case SEN_OV7620:
  1744. case SEN_OV76BE:
  1745. case SEN_OV8600:
  1746. i2c_w_mask(ov, 0x13, enable?0x01:0x00, 0x01);
  1747. break;
  1748. case SEN_OV6630:
  1749. i2c_w_mask(ov, 0x28, enable?0x00:0x10, 0x10);
  1750. break;
  1751. case SEN_KS0127:
  1752. case SEN_KS0127B:
  1753. case SEN_SAA7111A:
  1754. PDEBUG(5, "Unsupported with this sensor");
  1755. return -EPERM;
  1756. default:
  1757. err("Sensor not supported for set_auto_exposure");
  1758. return -EINVAL;
  1759. }
  1760. ov->auto_exp = enable;
  1761. return 0;
  1762. }
  1763. /* Modifies the sensor's exposure algorithm to allow proper exposure of objects
  1764. * that are illuminated from behind.
  1765. *
  1766. * Tested with: OV6620, OV7620
  1767. * Unsupported: OV7610, OV76BE, KS0127, KS0127B, SAA7111A
  1768. * Returns: 0 for success
  1769. */
  1770. static int
  1771. sensor_set_backlight(struct usb_ov511 *ov, int enable)
  1772. {
  1773. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1774. switch (ov->sensor) {
  1775. case SEN_OV7620:
  1776. case SEN_OV8600:
  1777. i2c_w_mask(ov, 0x68, enable?0xe0:0xc0, 0xe0);
  1778. i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
  1779. i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
  1780. break;
  1781. case SEN_OV6620:
  1782. i2c_w_mask(ov, 0x4e, enable?0xe0:0xc0, 0xe0);
  1783. i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
  1784. i2c_w_mask(ov, 0x0e, enable?0x80:0x00, 0x80);
  1785. break;
  1786. case SEN_OV6630:
  1787. i2c_w_mask(ov, 0x4e, enable?0x80:0x60, 0xe0);
  1788. i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
  1789. i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
  1790. break;
  1791. case SEN_OV7610:
  1792. case SEN_OV76BE:
  1793. case SEN_KS0127:
  1794. case SEN_KS0127B:
  1795. case SEN_SAA7111A:
  1796. PDEBUG(5, "Unsupported with this sensor");
  1797. return -EPERM;
  1798. default:
  1799. err("Sensor not supported for set_backlight");
  1800. return -EINVAL;
  1801. }
  1802. ov->backlight = enable;
  1803. return 0;
  1804. }
  1805. static int
  1806. sensor_set_mirror(struct usb_ov511 *ov, int enable)
  1807. {
  1808. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1809. switch (ov->sensor) {
  1810. case SEN_OV6620:
  1811. case SEN_OV6630:
  1812. case SEN_OV7610:
  1813. case SEN_OV7620:
  1814. case SEN_OV76BE:
  1815. case SEN_OV8600:
  1816. i2c_w_mask(ov, 0x12, enable?0x40:0x00, 0x40);
  1817. break;
  1818. case SEN_KS0127:
  1819. case SEN_KS0127B:
  1820. case SEN_SAA7111A:
  1821. PDEBUG(5, "Unsupported with this sensor");
  1822. return -EPERM;
  1823. default:
  1824. err("Sensor not supported for set_mirror");
  1825. return -EINVAL;
  1826. }
  1827. ov->mirror = enable;
  1828. return 0;
  1829. }
  1830. /* Returns number of bits per pixel (regardless of where they are located;
  1831. * planar or not), or zero for unsupported format.
  1832. */
  1833. static inline int
  1834. get_depth(int palette)
  1835. {
  1836. switch (palette) {
  1837. case VIDEO_PALETTE_GREY: return 8;
  1838. case VIDEO_PALETTE_YUV420: return 12;
  1839. case VIDEO_PALETTE_YUV420P: return 12; /* Planar */
  1840. default: return 0; /* Invalid format */
  1841. }
  1842. }
  1843. /* Bytes per frame. Used by read(). Return of 0 indicates error */
  1844. static inline long int
  1845. get_frame_length(struct ov511_frame *frame)
  1846. {
  1847. if (!frame)
  1848. return 0;
  1849. else
  1850. return ((frame->width * frame->height
  1851. * get_depth(frame->format)) >> 3);
  1852. }
  1853. static int
  1854. mode_init_ov_sensor_regs(struct usb_ov511 *ov, int width, int height,
  1855. int mode, int sub_flag, int qvga)
  1856. {
  1857. int clock;
  1858. /******** Mode (VGA/QVGA) and sensor specific regs ********/
  1859. switch (ov->sensor) {
  1860. case SEN_OV7610:
  1861. i2c_w(ov, 0x14, qvga?0x24:0x04);
  1862. // FIXME: Does this improve the image quality or frame rate?
  1863. #if 0
  1864. i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
  1865. i2c_w(ov, 0x24, 0x10);
  1866. i2c_w(ov, 0x25, qvga?0x40:0x8a);
  1867. i2c_w(ov, 0x2f, qvga?0x30:0xb0);
  1868. i2c_w(ov, 0x35, qvga?0x1c:0x9c);
  1869. #endif
  1870. break;
  1871. case SEN_OV7620:
  1872. // i2c_w(ov, 0x2b, 0x00);
  1873. i2c_w(ov, 0x14, qvga?0xa4:0x84);
  1874. i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
  1875. i2c_w(ov, 0x24, qvga?0x20:0x3a);
  1876. i2c_w(ov, 0x25, qvga?0x30:0x60);
  1877. i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
  1878. i2c_w_mask(ov, 0x67, qvga?0xf0:0x90, 0xf0);
  1879. i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
  1880. break;
  1881. case SEN_OV76BE:
  1882. // i2c_w(ov, 0x2b, 0x00);
  1883. i2c_w(ov, 0x14, qvga?0xa4:0x84);
  1884. // FIXME: Enable this once 7620AE uses 7620 initial settings
  1885. #if 0
  1886. i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
  1887. i2c_w(ov, 0x24, qvga?0x20:0x3a);
  1888. i2c_w(ov, 0x25, qvga?0x30:0x60);
  1889. i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
  1890. i2c_w_mask(ov, 0x67, qvga?0xb0:0x90, 0xf0);
  1891. i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
  1892. #endif
  1893. break;
  1894. case SEN_OV6620:
  1895. i2c_w(ov, 0x14, qvga?0x24:0x04);
  1896. break;
  1897. case SEN_OV6630:
  1898. i2c_w(ov, 0x14, qvga?0xa0:0x80);
  1899. break;
  1900. default:
  1901. err("Invalid sensor");
  1902. return -EINVAL;
  1903. }
  1904. /******** Palette-specific regs ********/
  1905. if (mode == VIDEO_PALETTE_GREY) {
  1906. if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
  1907. /* these aren't valid on the OV6620/OV7620/6630? */
  1908. i2c_w_mask(ov, 0x0e, 0x40, 0x40);
  1909. }
  1910. if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
  1911. && ov518_color) {
  1912. i2c_w_mask(ov, 0x12, 0x00, 0x10);
  1913. i2c_w_mask(ov, 0x13, 0x00, 0x20);
  1914. } else {
  1915. i2c_w_mask(ov, 0x13, 0x20, 0x20);
  1916. }
  1917. } else {
  1918. if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
  1919. /* not valid on the OV6620/OV7620/6630? */
  1920. i2c_w_mask(ov, 0x0e, 0x00, 0x40);
  1921. }
  1922. /* The OV518 needs special treatment. Although both the OV518
  1923. * and the OV6630 support a 16-bit video bus, only the 8 bit Y
  1924. * bus is actually used. The UV bus is tied to ground.
  1925. * Therefore, the OV6630 needs to be in 8-bit multiplexed
  1926. * output mode */
  1927. if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
  1928. && ov518_color) {
  1929. i2c_w_mask(ov, 0x12, 0x10, 0x10);
  1930. i2c_w_mask(ov, 0x13, 0x20, 0x20);
  1931. } else {
  1932. i2c_w_mask(ov, 0x13, 0x00, 0x20);
  1933. }
  1934. }
  1935. /******** Clock programming ********/
  1936. /* The OV6620 needs special handling. This prevents the
  1937. * severe banding that normally occurs */
  1938. if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630)
  1939. {
  1940. /* Clock down */
  1941. i2c_w(ov, 0x2a, 0x04);
  1942. if (ov->compress) {
  1943. // clock = 0; /* This ensures the highest frame rate */
  1944. clock = 3;
  1945. } else if (clockdiv == -1) { /* If user didn't override it */
  1946. clock = 3; /* Gives better exposure time */
  1947. } else {
  1948. clock = clockdiv;
  1949. }
  1950. PDEBUG(4, "Setting clock divisor to %d", clock);
  1951. i2c_w(ov, 0x11, clock);
  1952. i2c_w(ov, 0x2a, 0x84);
  1953. /* This next setting is critical. It seems to improve
  1954. * the gain or the contrast. The "reserved" bits seem
  1955. * to have some effect in this case. */
  1956. i2c_w(ov, 0x2d, 0x85);
  1957. }
  1958. else
  1959. {
  1960. if (ov->compress) {
  1961. clock = 1; /* This ensures the highest frame rate */
  1962. } else if (clockdiv == -1) { /* If user didn't override it */
  1963. /* Calculate and set the clock divisor */
  1964. clock = ((sub_flag ? ov->subw * ov->subh
  1965. : width * height)
  1966. * (mode == VIDEO_PALETTE_GREY ? 2 : 3) / 2)
  1967. / 66000;
  1968. } else {
  1969. clock = clockdiv;
  1970. }
  1971. PDEBUG(4, "Setting clock divisor to %d", clock);
  1972. i2c_w(ov, 0x11, clock);
  1973. }
  1974. /******** Special Features ********/
  1975. if (framedrop >= 0)
  1976. i2c_w(ov, 0x16, framedrop);
  1977. /* Test Pattern */
  1978. i2c_w_mask(ov, 0x12, (testpat?0x02:0x00), 0x02);
  1979. /* Enable auto white balance */
  1980. i2c_w_mask(ov, 0x12, 0x04, 0x04);
  1981. // This will go away as soon as ov51x_mode_init_sensor_regs()
  1982. // is fully tested.
  1983. /* 7620/6620/6630? don't have register 0x35, so play it safe */
  1984. if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
  1985. if (width == 640 && height == 480)
  1986. i2c_w(ov, 0x35, 0x9e);
  1987. else
  1988. i2c_w(ov, 0x35, 0x1e);
  1989. }
  1990. return 0;
  1991. }
  1992. static int
  1993. set_ov_sensor_window(struct usb_ov511 *ov, int width, int height, int mode,
  1994. int sub_flag)
  1995. {
  1996. int ret;
  1997. int hwsbase, hwebase, vwsbase, vwebase, hwsize, vwsize;
  1998. int hoffset, voffset, hwscale = 0, vwscale = 0;
  1999. /* The different sensor ICs handle setting up of window differently.
  2000. * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!!! */
  2001. switch (ov->sensor) {
  2002. case SEN_OV7610:
  2003. case SEN_OV76BE:
  2004. hwsbase = 0x38;
  2005. hwebase = 0x3a;
  2006. vwsbase = vwebase = 0x05;
  2007. break;
  2008. case SEN_OV6620:
  2009. case SEN_OV6630:
  2010. hwsbase = 0x38;
  2011. hwebase = 0x3a;
  2012. vwsbase = 0x05;
  2013. vwebase = 0x06;
  2014. break;
  2015. case SEN_OV7620:
  2016. hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
  2017. hwebase = 0x2f;
  2018. vwsbase = vwebase = 0x05;
  2019. break;
  2020. default:
  2021. err("Invalid sensor");
  2022. return -EINVAL;
  2023. }
  2024. if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630) {
  2025. /* Note: OV518(+) does downsample on its own) */
  2026. if ((width > 176 && height > 144)
  2027. || ov->bclass == BCL_OV518) { /* CIF */
  2028. ret = mode_init_ov_sensor_regs(ov, width, height,
  2029. mode, sub_flag, 0);
  2030. if (ret < 0)
  2031. return ret;
  2032. hwscale = 1;
  2033. vwscale = 1; /* The datasheet says 0; it's wrong */
  2034. hwsize = 352;
  2035. vwsize = 288;
  2036. } else if (width > 176 || height > 144) {
  2037. err("Illegal dimensions");
  2038. return -EINVAL;
  2039. } else { /* QCIF */
  2040. ret = mode_init_ov_sensor_regs(ov, width, height,
  2041. mode, sub_flag, 1);
  2042. if (ret < 0)
  2043. return ret;
  2044. hwsize = 176;
  2045. vwsize = 144;
  2046. }
  2047. } else {
  2048. if (width > 320 && height > 240) { /* VGA */
  2049. ret = mode_init_ov_sensor_regs(ov, width, height,
  2050. mode, sub_flag, 0);
  2051. if (ret < 0)
  2052. return ret;
  2053. hwscale = 2;
  2054. vwscale = 1;
  2055. hwsize = 640;
  2056. vwsize = 480;
  2057. } else if (width > 320 || height > 240) {
  2058. err("Illegal dimensions");
  2059. return -EINVAL;
  2060. } else { /* QVGA */
  2061. ret = mode_init_ov_sensor_regs(ov, width, height,
  2062. mode, sub_flag, 1);
  2063. if (ret < 0)
  2064. return ret;
  2065. hwscale = 1;
  2066. hwsize = 320;
  2067. vwsize = 240;
  2068. }
  2069. }
  2070. /* Center the window */
  2071. hoffset = ((hwsize - width) / 2) >> hwscale;
  2072. voffset = ((vwsize - height) / 2) >> vwscale;
  2073. /* FIXME! - This needs to be changed to support 160x120 and 6620!!! */
  2074. if (sub_flag) {
  2075. i2c_w(ov, 0x17, hwsbase+(ov->subx>>hwscale));
  2076. i2c_w(ov, 0x18, hwebase+((ov->subx+ov->subw)>>hwscale));
  2077. i2c_w(ov, 0x19, vwsbase+(ov->suby>>vwscale));
  2078. i2c_w(ov, 0x1a, vwebase+((ov->suby+ov->subh)>>vwscale));
  2079. } else {
  2080. i2c_w(ov, 0x17, hwsbase + hoffset);
  2081. i2c_w(ov, 0x18, hwebase + hoffset + (hwsize>>hwscale));
  2082. i2c_w(ov, 0x19, vwsbase + voffset);
  2083. i2c_w(ov, 0x1a, vwebase + voffset + (vwsize>>vwscale));
  2084. }
  2085. #ifdef OV511_DEBUG
  2086. if (dump_sensor)
  2087. dump_i2c_regs(ov);
  2088. #endif
  2089. return 0;
  2090. }
  2091. /* Set up the OV511/OV511+ with the given image parameters.
  2092. *
  2093. * Do not put any sensor-specific code in here (including I2C I/O functions)
  2094. */
  2095. static int
  2096. ov511_mode_init_regs(struct usb_ov511 *ov,
  2097. int width, int height, int mode, int sub_flag)
  2098. {
  2099. int hsegs, vsegs;
  2100. if (sub_flag) {
  2101. width = ov->subw;
  2102. height = ov->subh;
  2103. }
  2104. PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
  2105. width, height, mode, sub_flag);
  2106. // FIXME: This should be moved to a 7111a-specific function once
  2107. // subcapture is dealt with properly
  2108. if (ov->sensor == SEN_SAA7111A) {
  2109. if (width == 320 && height == 240) {
  2110. /* No need to do anything special */
  2111. } else if (width == 640 && height == 480) {
  2112. /* Set the OV511 up as 320x480, but keep the
  2113. * V4L resolution as 640x480 */
  2114. width = 320;
  2115. } else {
  2116. err("SAA7111A only allows 320x240 or 640x480");
  2117. return -EINVAL;
  2118. }
  2119. }
  2120. /* Make sure width and height are a multiple of 8 */
  2121. if (width % 8 || height % 8) {
  2122. err("Invalid size (%d, %d) (mode = %d)", width, height, mode);
  2123. return -EINVAL;
  2124. }
  2125. if (width < ov->minwidth || height < ov->minheight) {
  2126. err("Requested dimensions are too small");
  2127. return -EINVAL;
  2128. }
  2129. if (ov51x_stop(ov) < 0)
  2130. return -EIO;
  2131. if (mode == VIDEO_PALETTE_GREY) {
  2132. reg_w(ov, R511_CAM_UV_EN, 0x00);
  2133. reg_w(ov, R511_SNAP_UV_EN, 0x00);
  2134. reg_w(ov, R511_SNAP_OPTS, 0x01);
  2135. } else {
  2136. reg_w(ov, R511_CAM_UV_EN, 0x01);
  2137. reg_w(ov, R511_SNAP_UV_EN, 0x01);
  2138. reg_w(ov, R511_SNAP_OPTS, 0x03);
  2139. }
  2140. /* Here I'm assuming that snapshot size == image size.
  2141. * I hope that's always true. --claudio
  2142. */
  2143. hsegs = (width >> 3) - 1;
  2144. vsegs = (height >> 3) - 1;
  2145. reg_w(ov, R511_CAM_PXCNT, hsegs);
  2146. reg_w(ov, R511_CAM_LNCNT, vsegs);
  2147. reg_w(ov, R511_CAM_PXDIV, 0x00);
  2148. reg_w(ov, R511_CAM_LNDIV, 0x00);
  2149. /* YUV420, low pass filter on */
  2150. reg_w(ov, R511_CAM_OPTS, 0x03);
  2151. /* Snapshot additions */
  2152. reg_w(ov, R511_SNAP_PXCNT, hsegs);
  2153. reg_w(ov, R511_SNAP_LNCNT, vsegs);
  2154. reg_w(ov, R511_SNAP_PXDIV, 0x00);
  2155. reg_w(ov, R511_SNAP_LNDIV, 0x00);
  2156. if (ov->compress) {
  2157. /* Enable Y and UV quantization and compression */
  2158. reg_w(ov, R511_COMP_EN, 0x07);
  2159. reg_w(ov, R511_COMP_LUT_EN, 0x03);
  2160. ov51x_reset(ov, OV511_RESET_OMNICE);
  2161. }
  2162. if (ov51x_restart(ov) < 0)
  2163. return -EIO;
  2164. return 0;
  2165. }
  2166. /* Sets up the OV518/OV518+ with the given image parameters
  2167. *
  2168. * OV518 needs a completely different approach, until we can figure out what
  2169. * the individual registers do. Also, only 15 FPS is supported now.
  2170. *
  2171. * Do not put any sensor-specific code in here (including I2C I/O functions)
  2172. */
  2173. static int
  2174. ov518_mode_init_regs(struct usb_ov511 *ov,
  2175. int width, int height, int mode, int sub_flag)
  2176. {
  2177. int hsegs, vsegs, hi_res;
  2178. if (sub_flag) {
  2179. width = ov->subw;
  2180. height = ov->subh;
  2181. }
  2182. PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
  2183. width, height, mode, sub_flag);
  2184. if (width % 16 || height % 8) {
  2185. err("Invalid size (%d, %d)", width, height);
  2186. return -EINVAL;
  2187. }
  2188. if (width < ov->minwidth || height < ov->minheight) {
  2189. err("Requested dimensions are too small");
  2190. return -EINVAL;
  2191. }
  2192. if (width >= 320 && height >= 240) {
  2193. hi_res = 1;
  2194. } else if (width >= 320 || height >= 240) {
  2195. err("Invalid width/height combination (%d, %d)", width, height);
  2196. return -EINVAL;
  2197. } else {
  2198. hi_res = 0;
  2199. }
  2200. if (ov51x_stop(ov) < 0)
  2201. return -EIO;
  2202. /******** Set the mode ********/
  2203. reg_w(ov, 0x2b, 0);
  2204. reg_w(ov, 0x2c, 0);
  2205. reg_w(ov, 0x2d, 0);
  2206. reg_w(ov, 0x2e, 0);
  2207. reg_w(ov, 0x3b, 0);
  2208. reg_w(ov, 0x3c, 0);
  2209. reg_w(ov, 0x3d, 0);
  2210. reg_w(ov, 0x3e, 0);
  2211. if (ov->bridge == BRG_OV518 && ov518_color) {
  2212. /* OV518 needs U and V swapped */
  2213. i2c_w_mask(ov, 0x15, 0x00, 0x01);
  2214. if (mode == VIDEO_PALETTE_GREY) {
  2215. /* Set 16-bit input format (UV data are ignored) */
  2216. reg_w_mask(ov, 0x20, 0x00, 0x08);
  2217. /* Set 8-bit (4:0:0) output format */
  2218. reg_w_mask(ov, 0x28, 0x00, 0xf0);
  2219. reg_w_mask(ov, 0x38, 0x00, 0xf0);
  2220. } else {
  2221. /* Set 8-bit (YVYU) input format */
  2222. reg_w_mask(ov, 0x20, 0x08, 0x08);
  2223. /* Set 12-bit (4:2:0) output format */
  2224. reg_w_mask(ov, 0x28, 0x80, 0xf0);
  2225. reg_w_mask(ov, 0x38, 0x80, 0xf0);
  2226. }
  2227. } else {
  2228. reg_w(ov, 0x28, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
  2229. reg_w(ov, 0x38, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
  2230. }
  2231. hsegs = width / 16;
  2232. vsegs = height / 4;
  2233. reg_w(ov, 0x29, hsegs);
  2234. reg_w(ov, 0x2a, vsegs);
  2235. reg_w(ov, 0x39, hsegs);
  2236. reg_w(ov, 0x3a, vsegs);
  2237. /* Windows driver does this here; who knows why */
  2238. reg_w(ov, 0x2f, 0x80);
  2239. /******** Set the framerate (to 15 FPS) ********/
  2240. /* Mode independent, but framerate dependent, regs */
  2241. reg_w(ov, 0x51, 0x02); /* Clock divider; lower==faster */
  2242. reg_w(ov, 0x22, 0x18);
  2243. reg_w(ov, 0x23, 0xff);
  2244. if (ov->bridge == BRG_OV518PLUS)
  2245. reg_w(ov, 0x21, 0x19);
  2246. else
  2247. reg_w(ov, 0x71, 0x19); /* Compression-related? */
  2248. // FIXME: Sensor-specific
  2249. /* Bit 5 is what matters here. Of course, it is "reserved" */
  2250. i2c_w(ov, 0x54, 0x23);
  2251. reg_w(ov, 0x2f, 0x80);
  2252. if (ov->bridge == BRG_OV518PLUS) {
  2253. reg_w(ov, 0x24, 0x94);
  2254. reg_w(ov, 0x25, 0x90);
  2255. ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
  2256. ov518_reg_w32(ov, 0xc6, 540, 2); /* 21ch */
  2257. ov518_reg_w32(ov, 0xc7, 540, 2); /* 21ch */
  2258. ov518_reg_w32(ov, 0xc8, 108, 2); /* 6ch */
  2259. ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
  2260. ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
  2261. ov518_reg_w32(ov, 0xcc, 2400, 2); /* 960h */
  2262. ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
  2263. ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
  2264. } else {
  2265. reg_w(ov, 0x24, 0x9f);
  2266. reg_w(ov, 0x25, 0x90);
  2267. ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
  2268. ov518_reg_w32(ov, 0xc6, 500, 2); /* 1f4h */
  2269. ov518_reg_w32(ov, 0xc7, 500, 2); /* 1f4h */
  2270. ov518_reg_w32(ov, 0xc8, 142, 2); /* 8eh */
  2271. ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
  2272. ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
  2273. ov518_reg_w32(ov, 0xcc, 2000, 2); /* 7d0h */
  2274. ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
  2275. ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
  2276. }
  2277. reg_w(ov, 0x2f, 0x80);
  2278. if (ov51x_restart(ov) < 0)
  2279. return -EIO;
  2280. /* Reset it just for good measure */
  2281. if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
  2282. return -EIO;
  2283. return 0;
  2284. }
  2285. /* This is a wrapper around the OV511, OV518, and sensor specific functions */
  2286. static int
  2287. mode_init_regs(struct usb_ov511 *ov,
  2288. int width, int height, int mode, int sub_flag)
  2289. {
  2290. int rc = 0;
  2291. if (!ov || !ov->dev)
  2292. return -EFAULT;
  2293. if (ov->bclass == BCL_OV518) {
  2294. rc = ov518_mode_init_regs(ov, width, height, mode, sub_flag);
  2295. } else {
  2296. rc = ov511_mode_init_regs(ov, width, height, mode, sub_flag);
  2297. }
  2298. if (FATAL_ERROR(rc))
  2299. return rc;
  2300. switch (ov->sensor) {
  2301. case SEN_OV7610:
  2302. case SEN_OV7620:
  2303. case SEN_OV76BE:
  2304. case SEN_OV8600:
  2305. case SEN_OV6620:
  2306. case SEN_OV6630:
  2307. rc = set_ov_sensor_window(ov, width, height, mode, sub_flag);
  2308. break;
  2309. case SEN_KS0127:
  2310. case SEN_KS0127B:
  2311. err("KS0127-series decoders not supported yet");
  2312. rc = -EINVAL;
  2313. break;
  2314. case SEN_SAA7111A:
  2315. // rc = mode_init_saa_sensor_regs(ov, width, height, mode,
  2316. // sub_flag);
  2317. PDEBUG(1, "SAA status = 0x%02X", i2c_r(ov, 0x1f));
  2318. break;
  2319. default:
  2320. err("Unknown sensor");
  2321. rc = -EINVAL;
  2322. }
  2323. if (FATAL_ERROR(rc))
  2324. return rc;
  2325. /* Sensor-independent settings */
  2326. rc = sensor_set_auto_brightness(ov, ov->auto_brt);
  2327. if (FATAL_ERROR(rc))
  2328. return rc;
  2329. rc = sensor_set_auto_exposure(ov, ov->auto_exp);
  2330. if (FATAL_ERROR(rc))
  2331. return rc;
  2332. rc = sensor_set_banding_filter(ov, bandingfilter);
  2333. if (FATAL_ERROR(rc))
  2334. return rc;
  2335. if (ov->lightfreq) {
  2336. rc = sensor_set_light_freq(ov, lightfreq);
  2337. if (FATAL_ERROR(rc))
  2338. return rc;
  2339. }
  2340. rc = sensor_set_backlight(ov, ov->backlight);
  2341. if (FATAL_ERROR(rc))
  2342. return rc;
  2343. rc = sensor_set_mirror(ov, ov->mirror);
  2344. if (FATAL_ERROR(rc))
  2345. return rc;
  2346. return 0;
  2347. }
  2348. /* This sets the default image parameters. This is useful for apps that use
  2349. * read() and do not set these.
  2350. */
  2351. static int
  2352. ov51x_set_default_params(struct usb_ov511 *ov)
  2353. {
  2354. int i;
  2355. /* Set default sizes in case IOCTL (VIDIOCMCAPTURE) is not used
  2356. * (using read() instead). */
  2357. for (i = 0; i < OV511_NUMFRAMES; i++) {
  2358. ov->frame[i].width = ov->maxwidth;
  2359. ov->frame[i].height = ov->maxheight;
  2360. ov->frame[i].bytes_read = 0;
  2361. if (force_palette)
  2362. ov->frame[i].format = force_palette;
  2363. else
  2364. ov->frame[i].format = VIDEO_PALETTE_YUV420;
  2365. ov->frame[i].depth = get_depth(ov->frame[i].format);
  2366. }
  2367. PDEBUG(3, "%dx%d, %s", ov->maxwidth, ov->maxheight,
  2368. symbolic(v4l1_plist, ov->frame[0].format));
  2369. /* Initialize to max width/height, YUV420 or RGB24 (if supported) */
  2370. if (mode_init_regs(ov, ov->maxwidth, ov->maxheight,
  2371. ov->frame[0].format, 0) < 0)
  2372. return -EINVAL;
  2373. return 0;
  2374. }
  2375. /**********************************************************************
  2376. *
  2377. * Video decoder stuff
  2378. *
  2379. **********************************************************************/
  2380. /* Set analog input port of decoder */
  2381. static int
  2382. decoder_set_input(struct usb_ov511 *ov, int input)
  2383. {
  2384. PDEBUG(4, "port %d", input);
  2385. switch (ov->sensor) {
  2386. case SEN_SAA7111A:
  2387. {
  2388. /* Select mode */
  2389. i2c_w_mask(ov, 0x02, input, 0x07);
  2390. /* Bypass chrominance trap for modes 4..7 */
  2391. i2c_w_mask(ov, 0x09, (input > 3) ? 0x80:0x00, 0x80);
  2392. break;
  2393. }
  2394. default:
  2395. return -EINVAL;
  2396. }
  2397. return 0;
  2398. }
  2399. /* Get ASCII name of video input */
  2400. static int
  2401. decoder_get_input_name(struct usb_ov511 *ov, int input, char *name)
  2402. {
  2403. switch (ov->sensor) {
  2404. case SEN_SAA7111A:
  2405. {
  2406. if (input < 0 || input > 7)
  2407. return -EINVAL;
  2408. else if (input < 4)
  2409. sprintf(name, "CVBS-%d", input);
  2410. else // if (input < 8)
  2411. sprintf(name, "S-Video-%d", input - 4);
  2412. break;
  2413. }
  2414. default:
  2415. sprintf(name, "%s", "Camera");
  2416. }
  2417. return 0;
  2418. }
  2419. /* Set norm (NTSC, PAL, SECAM, AUTO) */
  2420. static int
  2421. decoder_set_norm(struct usb_ov511 *ov, int norm)
  2422. {
  2423. PDEBUG(4, "%d", norm);
  2424. switch (ov->sensor) {
  2425. case SEN_SAA7111A:
  2426. {
  2427. int reg_8, reg_e;
  2428. if (norm == VIDEO_MODE_NTSC) {
  2429. reg_8 = 0x40; /* 60 Hz */
  2430. reg_e = 0x00; /* NTSC M / PAL BGHI */
  2431. } else if (norm == VIDEO_MODE_PAL) {
  2432. reg_8 = 0x00; /* 50 Hz */
  2433. reg_e = 0x00; /* NTSC M / PAL BGHI */
  2434. } else if (norm == VIDEO_MODE_AUTO) {
  2435. reg_8 = 0x80; /* Auto field detect */
  2436. reg_e = 0x00; /* NTSC M / PAL BGHI */
  2437. } else if (norm == VIDEO_MODE_SECAM) {
  2438. reg_8 = 0x00; /* 50 Hz */
  2439. reg_e = 0x50; /* SECAM / PAL 4.43 */
  2440. } else {
  2441. return -EINVAL;
  2442. }
  2443. i2c_w_mask(ov, 0x08, reg_8, 0xc0);
  2444. i2c_w_mask(ov, 0x0e, reg_e, 0x70);
  2445. break;
  2446. }
  2447. default:
  2448. return -EINVAL;
  2449. }
  2450. return 0;
  2451. }
  2452. /**********************************************************************
  2453. *
  2454. * Raw data parsing
  2455. *
  2456. **********************************************************************/
  2457. /* Copies a 64-byte segment at pIn to an 8x8 block at pOut. The width of the
  2458. * image at pOut is specified by w.
  2459. */
  2460. static inline void
  2461. make_8x8(unsigned char *pIn, unsigned char *pOut, int w)
  2462. {
  2463. unsigned char *pOut1 = pOut;
  2464. int x, y;
  2465. for (y = 0; y < 8; y++) {
  2466. pOut1 = pOut;
  2467. for (x = 0; x < 8; x++) {
  2468. *pOut1++ = *pIn++;
  2469. }
  2470. pOut += w;
  2471. }
  2472. }
  2473. /*
  2474. * For RAW BW (YUV 4:0:0) images, data show up in 256 byte segments.
  2475. * The segments represent 4 squares of 8x8 pixels as follows:
  2476. *
  2477. * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
  2478. * 8 9 ... 15 72 73 ... 79 200 201 ... 207
  2479. * ... ... ...
  2480. * 56 57 ... 63 120 121 ... 127 248 249 ... 255
  2481. *
  2482. */
  2483. static void
  2484. yuv400raw_to_yuv400p(struct ov511_frame *frame,
  2485. unsigned char *pIn0, unsigned char *pOut0)
  2486. {
  2487. int x, y;
  2488. unsigned char *pIn, *pOut, *pOutLine;
  2489. /* Copy Y */
  2490. pIn = pIn0;
  2491. pOutLine = pOut0;
  2492. for (y = 0; y < frame->rawheight - 1; y += 8) {
  2493. pOut = pOutLine;
  2494. for (x = 0; x < frame->rawwidth - 1; x += 8) {
  2495. make_8x8(pIn, pOut, frame->rawwidth);
  2496. pIn += 64;
  2497. pOut += 8;
  2498. }
  2499. pOutLine += 8 * frame->rawwidth;
  2500. }
  2501. }
  2502. /*
  2503. * For YUV 4:2:0 images, the data show up in 384 byte segments.
  2504. * The first 64 bytes of each segment are U, the next 64 are V. The U and
  2505. * V are arranged as follows:
  2506. *
  2507. * 0 1 ... 7
  2508. * 8 9 ... 15
  2509. * ...
  2510. * 56 57 ... 63
  2511. *
  2512. * U and V are shipped at half resolution (1 U,V sample -> one 2x2 block).
  2513. *
  2514. * The next 256 bytes are full resolution Y data and represent 4 squares
  2515. * of 8x8 pixels as follows:
  2516. *
  2517. * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
  2518. * 8 9 ... 15 72 73 ... 79 200 201 ... 207
  2519. * ... ... ...
  2520. * 56 57 ... 63 120 121 ... 127 ... 248 249 ... 255
  2521. *
  2522. * Note that the U and V data in one segment represent a 16 x 16 pixel
  2523. * area, but the Y data represent a 32 x 8 pixel area. If the width is not an
  2524. * even multiple of 32, the extra 8x8 blocks within a 32x8 block belong to the
  2525. * next horizontal stripe.
  2526. *
  2527. * If dumppix module param is set, _parse_data just dumps the incoming segments,
  2528. * verbatim, in order, into the frame. When used with vidcat -f ppm -s 640x480
  2529. * this puts the data on the standard output and can be analyzed with the
  2530. * parseppm.c utility I wrote. That's a much faster way for figuring out how
  2531. * these data are scrambled.
  2532. */
  2533. /* Converts from raw, uncompressed segments at pIn0 to a YUV420P frame at pOut0.
  2534. *
  2535. * FIXME: Currently only handles width and height that are multiples of 16
  2536. */
  2537. static void
  2538. yuv420raw_to_yuv420p(struct ov511_frame *frame,
  2539. unsigned char *pIn0, unsigned char *pOut0)
  2540. {
  2541. int k, x, y;
  2542. unsigned char *pIn, *pOut, *pOutLine;
  2543. const unsigned int a = frame->rawwidth * frame->rawheight;
  2544. const unsigned int w = frame->rawwidth / 2;
  2545. /* Copy U and V */
  2546. pIn = pIn0;
  2547. pOutLine = pOut0 + a;
  2548. for (y = 0; y < frame->rawheight - 1; y += 16) {
  2549. pOut = pOutLine;
  2550. for (x = 0; x < frame->rawwidth - 1; x += 16) {
  2551. make_8x8(pIn, pOut, w);
  2552. make_8x8(pIn + 64, pOut + a/4, w);
  2553. pIn += 384;
  2554. pOut += 8;
  2555. }
  2556. pOutLine += 8 * w;
  2557. }
  2558. /* Copy Y */
  2559. pIn = pIn0 + 128;
  2560. pOutLine = pOut0;
  2561. k = 0;
  2562. for (y = 0; y < frame->rawheight - 1; y += 8) {
  2563. pOut = pOutLine;
  2564. for (x = 0; x < frame->rawwidth - 1; x += 8) {
  2565. make_8x8(pIn, pOut, frame->rawwidth);
  2566. pIn += 64;
  2567. pOut += 8;
  2568. if ((++k) > 3) {
  2569. k = 0;
  2570. pIn += 128;
  2571. }
  2572. }
  2573. pOutLine += 8 * frame->rawwidth;
  2574. }
  2575. }
  2576. /**********************************************************************
  2577. *
  2578. * Decompression
  2579. *
  2580. **********************************************************************/
  2581. /* Chooses a decompression module, locks it, and sets ov->decomp_ops
  2582. * accordingly. Returns -ENXIO if decompressor is not available, otherwise
  2583. * returns 0 if no other error.
  2584. */
  2585. static int
  2586. request_decompressor(struct usb_ov511 *ov)
  2587. {
  2588. if (!ov)
  2589. return -ENODEV;
  2590. if (ov->decomp_ops) {
  2591. err("ERROR: Decompressor already requested!");
  2592. return -EINVAL;
  2593. }
  2594. lock_kernel();
  2595. /* Try to get MMX, and fall back on no-MMX if necessary */
  2596. if (ov->bclass == BCL_OV511) {
  2597. if (ov511_mmx_decomp_ops) {
  2598. PDEBUG(3, "Using OV511 MMX decompressor");
  2599. ov->decomp_ops = ov511_mmx_decomp_ops;
  2600. } else if (ov511_decomp_ops) {
  2601. PDEBUG(3, "Using OV511 decompressor");
  2602. ov->decomp_ops = ov511_decomp_ops;
  2603. } else {
  2604. err("No decompressor available");
  2605. }
  2606. } else if (ov->bclass == BCL_OV518) {
  2607. if (ov518_mmx_decomp_ops) {
  2608. PDEBUG(3, "Using OV518 MMX decompressor");
  2609. ov->decomp_ops = ov518_mmx_decomp_ops;
  2610. } else if (ov518_decomp_ops) {
  2611. PDEBUG(3, "Using OV518 decompressor");
  2612. ov->decomp_ops = ov518_decomp_ops;
  2613. } else {
  2614. err("No decompressor available");
  2615. }
  2616. } else {
  2617. err("Unknown bridge");
  2618. }
  2619. if (!ov->decomp_ops)
  2620. goto nosys;
  2621. if (!ov->decomp_ops->owner) {
  2622. ov->decomp_ops = NULL;
  2623. goto nosys;
  2624. }
  2625. if (!try_module_get(ov->decomp_ops->owner))
  2626. goto nosys;
  2627. unlock_kernel();
  2628. return 0;
  2629. nosys:
  2630. unlock_kernel();
  2631. return -ENOSYS;
  2632. }
  2633. /* Unlocks decompression module and nulls ov->decomp_ops. Safe to call even
  2634. * if ov->decomp_ops is NULL.
  2635. */
  2636. static void
  2637. release_decompressor(struct usb_ov511 *ov)
  2638. {
  2639. int released = 0; /* Did we actually do anything? */
  2640. if (!ov)
  2641. return;
  2642. lock_kernel();
  2643. if (ov->decomp_ops) {
  2644. module_put(ov->decomp_ops->owner);
  2645. released = 1;
  2646. }
  2647. ov->decomp_ops = NULL;
  2648. unlock_kernel();
  2649. if (released)
  2650. PDEBUG(3, "Decompressor released");
  2651. }
  2652. static void
  2653. decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
  2654. unsigned char *pIn0, unsigned char *pOut0)
  2655. {
  2656. if (!ov->decomp_ops)
  2657. if (request_decompressor(ov))
  2658. return;
  2659. PDEBUG(4, "Decompressing %d bytes", frame->bytes_recvd);
  2660. if (frame->format == VIDEO_PALETTE_GREY
  2661. && ov->decomp_ops->decomp_400) {
  2662. int ret = ov->decomp_ops->decomp_400(
  2663. pIn0,
  2664. pOut0,
  2665. frame->compbuf,
  2666. frame->rawwidth,
  2667. frame->rawheight,
  2668. frame->bytes_recvd);
  2669. PDEBUG(4, "DEBUG: decomp_400 returned %d", ret);
  2670. } else if (frame->format != VIDEO_PALETTE_GREY
  2671. && ov->decomp_ops->decomp_420) {
  2672. int ret = ov->decomp_ops->decomp_420(
  2673. pIn0,
  2674. pOut0,
  2675. frame->compbuf,
  2676. frame->rawwidth,
  2677. frame->rawheight,
  2678. frame->bytes_recvd);
  2679. PDEBUG(4, "DEBUG: decomp_420 returned %d", ret);
  2680. } else {
  2681. err("Decompressor does not support this format");
  2682. }
  2683. }
  2684. /**********************************************************************
  2685. *
  2686. * Format conversion
  2687. *
  2688. **********************************************************************/
  2689. /* Fuses even and odd fields together, and doubles width.
  2690. * INPUT: an odd field followed by an even field at pIn0, in YUV planar format
  2691. * OUTPUT: a normal YUV planar image, with correct aspect ratio
  2692. */
  2693. static void
  2694. deinterlace(struct ov511_frame *frame, int rawformat,
  2695. unsigned char *pIn0, unsigned char *pOut0)
  2696. {
  2697. const int fieldheight = frame->rawheight / 2;
  2698. const int fieldpix = fieldheight * frame->rawwidth;
  2699. const int w = frame->width;
  2700. int x, y;
  2701. unsigned char *pInEven, *pInOdd, *pOut;
  2702. PDEBUG(5, "fieldheight=%d", fieldheight);
  2703. if (frame->rawheight != frame->height) {
  2704. err("invalid height");
  2705. return;
  2706. }
  2707. if ((frame->rawwidth * 2) != frame->width) {
  2708. err("invalid width");
  2709. return;
  2710. }
  2711. /* Y */
  2712. pInOdd = pIn0;
  2713. pInEven = pInOdd + fieldpix;
  2714. pOut = pOut0;
  2715. for (y = 0; y < fieldheight; y++) {
  2716. for (x = 0; x < frame->rawwidth; x++) {
  2717. *pOut = *pInEven;
  2718. *(pOut+1) = *pInEven++;
  2719. *(pOut+w) = *pInOdd;
  2720. *(pOut+w+1) = *pInOdd++;
  2721. pOut += 2;
  2722. }
  2723. pOut += w;
  2724. }
  2725. if (rawformat == RAWFMT_YUV420) {
  2726. /* U */
  2727. pInOdd = pIn0 + fieldpix * 2;
  2728. pInEven = pInOdd + fieldpix / 4;
  2729. for (y = 0; y < fieldheight / 2; y++) {
  2730. for (x = 0; x < frame->rawwidth / 2; x++) {
  2731. *pOut = *pInEven;
  2732. *(pOut+1) = *pInEven++;
  2733. *(pOut+w/2) = *pInOdd;
  2734. *(pOut+w/2+1) = *pInOdd++;
  2735. pOut += 2;
  2736. }
  2737. pOut += w/2;
  2738. }
  2739. /* V */
  2740. pInOdd = pIn0 + fieldpix * 2 + fieldpix / 2;
  2741. pInEven = pInOdd + fieldpix / 4;
  2742. for (y = 0; y < fieldheight / 2; y++) {
  2743. for (x = 0; x < frame->rawwidth / 2; x++) {
  2744. *pOut = *pInEven;
  2745. *(pOut+1) = *pInEven++;
  2746. *(pOut+w/2) = *pInOdd;
  2747. *(pOut+w/2+1) = *pInOdd++;
  2748. pOut += 2;
  2749. }
  2750. pOut += w/2;
  2751. }
  2752. }
  2753. }
  2754. static void
  2755. ov51x_postprocess_grey(struct usb_ov511 *ov, struct ov511_frame *frame)
  2756. {
  2757. /* Deinterlace frame, if necessary */
  2758. if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
  2759. if (frame->compressed)
  2760. decompress(ov, frame, frame->rawdata,
  2761. frame->tempdata);
  2762. else
  2763. yuv400raw_to_yuv400p(frame, frame->rawdata,
  2764. frame->tempdata);
  2765. deinterlace(frame, RAWFMT_YUV400, frame->tempdata,
  2766. frame->data);
  2767. } else {
  2768. if (frame->compressed)
  2769. decompress(ov, frame, frame->rawdata,
  2770. frame->data);
  2771. else
  2772. yuv400raw_to_yuv400p(frame, frame->rawdata,
  2773. frame->data);
  2774. }
  2775. }
  2776. /* Process raw YUV420 data into standard YUV420P */
  2777. static void
  2778. ov51x_postprocess_yuv420(struct usb_ov511 *ov, struct ov511_frame *frame)
  2779. {
  2780. /* Deinterlace frame, if necessary */
  2781. if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
  2782. if (frame->compressed)
  2783. decompress(ov, frame, frame->rawdata, frame->tempdata);
  2784. else
  2785. yuv420raw_to_yuv420p(frame, frame->rawdata,
  2786. frame->tempdata);
  2787. deinterlace(frame, RAWFMT_YUV420, frame->tempdata,
  2788. frame->data);
  2789. } else {
  2790. if (frame->compressed)
  2791. decompress(ov, frame, frame->rawdata, frame->data);
  2792. else
  2793. yuv420raw_to_yuv420p(frame, frame->rawdata,
  2794. frame->data);
  2795. }
  2796. }
  2797. /* Post-processes the specified frame. This consists of:
  2798. * 1. Decompress frame, if necessary
  2799. * 2. Deinterlace frame and scale to proper size, if necessary
  2800. * 3. Convert from YUV planar to destination format, if necessary
  2801. * 4. Fix the RGB offset, if necessary
  2802. */
  2803. static void
  2804. ov51x_postprocess(struct usb_ov511 *ov, struct ov511_frame *frame)
  2805. {
  2806. if (dumppix) {
  2807. memset(frame->data, 0,
  2808. MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
  2809. PDEBUG(4, "Dumping %d bytes", frame->bytes_recvd);
  2810. memcpy(frame->data, frame->rawdata, frame->bytes_recvd);
  2811. } else {
  2812. switch (frame->format) {
  2813. case VIDEO_PALETTE_GREY:
  2814. ov51x_postprocess_grey(ov, frame);
  2815. break;
  2816. case VIDEO_PALETTE_YUV420:
  2817. case VIDEO_PALETTE_YUV420P:
  2818. ov51x_postprocess_yuv420(ov, frame);
  2819. break;
  2820. default:
  2821. err("Cannot convert data to %s",
  2822. symbolic(v4l1_plist, frame->format));
  2823. }
  2824. }
  2825. }
  2826. /**********************************************************************
  2827. *
  2828. * OV51x data transfer, IRQ handler
  2829. *
  2830. **********************************************************************/
  2831. static inline void
  2832. ov511_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
  2833. {
  2834. int num, offset;
  2835. int pnum = in[ov->packet_size - 1]; /* Get packet number */
  2836. int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
  2837. struct ov511_frame *frame = &ov->frame[ov->curframe];
  2838. struct timeval *ts;
  2839. /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
  2840. * byte non-zero. The EOF packet has image width/height in the
  2841. * 10th and 11th bytes. The 9th byte is given as follows:
  2842. *
  2843. * bit 7: EOF
  2844. * 6: compression enabled
  2845. * 5: 422/420/400 modes
  2846. * 4: 422/420/400 modes
  2847. * 3: 1
  2848. * 2: snapshot button on
  2849. * 1: snapshot frame
  2850. * 0: even/odd field
  2851. */
  2852. if (printph) {
  2853. info("ph(%3d): %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x",
  2854. pnum, in[0], in[1], in[2], in[3], in[4], in[5], in[6],
  2855. in[7], in[8], in[9], in[10], in[11]);
  2856. }
  2857. /* Check for SOF/EOF packet */
  2858. if ((in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) ||
  2859. (~in[8] & 0x08))
  2860. goto check_middle;
  2861. /* Frame end */
  2862. if (in[8] & 0x80) {
  2863. ts = (struct timeval *)(frame->data
  2864. + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
  2865. do_gettimeofday(ts);
  2866. /* Get the actual frame size from the EOF header */
  2867. frame->rawwidth = ((int)(in[9]) + 1) * 8;
  2868. frame->rawheight = ((int)(in[10]) + 1) * 8;
  2869. PDEBUG(4, "Frame end, frame=%d, pnum=%d, w=%d, h=%d, recvd=%d",
  2870. ov->curframe, pnum, frame->rawwidth, frame->rawheight,
  2871. frame->bytes_recvd);
  2872. /* Validate the header data */
  2873. RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
  2874. RESTRICT_TO_RANGE(frame->rawheight, ov->minheight,
  2875. ov->maxheight);
  2876. /* Don't allow byte count to exceed buffer size */
  2877. RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
  2878. if (frame->scanstate == STATE_LINES) {
  2879. int nextf;
  2880. frame->grabstate = FRAME_DONE;
  2881. wake_up_interruptible(&frame->wq);
  2882. /* If next frame is ready or grabbing,
  2883. * point to it */
  2884. nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
  2885. if (ov->frame[nextf].grabstate == FRAME_READY
  2886. || ov->frame[nextf].grabstate == FRAME_GRABBING) {
  2887. ov->curframe = nextf;
  2888. ov->frame[nextf].scanstate = STATE_SCANNING;
  2889. } else {
  2890. if (frame->grabstate == FRAME_DONE) {
  2891. PDEBUG(4, "** Frame done **");
  2892. } else {
  2893. PDEBUG(4, "Frame not ready? state = %d",
  2894. ov->frame[nextf].grabstate);
  2895. }
  2896. ov->curframe = -1;
  2897. }
  2898. } else {
  2899. PDEBUG(5, "Frame done, but not scanning");
  2900. }
  2901. /* Image corruption caused by misplaced frame->segment = 0
  2902. * fixed by carlosf@conectiva.com.br
  2903. */
  2904. } else {
  2905. /* Frame start */
  2906. PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
  2907. /* Check to see if it's a snapshot frame */
  2908. /* FIXME?? Should the snapshot reset go here? Performance? */
  2909. if (in[8] & 0x02) {
  2910. frame->snapshot = 1;
  2911. PDEBUG(3, "snapshot detected");
  2912. }
  2913. frame->scanstate = STATE_LINES;
  2914. frame->bytes_recvd = 0;
  2915. frame->compressed = in[8] & 0x40;
  2916. }
  2917. check_middle:
  2918. /* Are we in a frame? */
  2919. if (frame->scanstate != STATE_LINES) {
  2920. PDEBUG(5, "Not in a frame; packet skipped");
  2921. return;
  2922. }
  2923. /* If frame start, skip header */
  2924. if (frame->bytes_recvd == 0)
  2925. offset = 9;
  2926. else
  2927. offset = 0;
  2928. num = n - offset - 1;
  2929. /* Dump all data exactly as received */
  2930. if (dumppix == 2) {
  2931. frame->bytes_recvd += n - 1;
  2932. if (frame->bytes_recvd <= max_raw)
  2933. memcpy(frame->rawdata + frame->bytes_recvd - (n - 1),
  2934. in, n - 1);
  2935. else
  2936. PDEBUG(3, "Raw data buffer overrun!! (%d)",
  2937. frame->bytes_recvd - max_raw);
  2938. } else if (!frame->compressed && !remove_zeros) {
  2939. frame->bytes_recvd += num;
  2940. if (frame->bytes_recvd <= max_raw)
  2941. memcpy(frame->rawdata + frame->bytes_recvd - num,
  2942. in + offset, num);
  2943. else
  2944. PDEBUG(3, "Raw data buffer overrun!! (%d)",
  2945. frame->bytes_recvd - max_raw);
  2946. } else { /* Remove all-zero FIFO lines (aligned 32-byte blocks) */
  2947. int b, read = 0, allzero, copied = 0;
  2948. if (offset) {
  2949. frame->bytes_recvd += 32 - offset; // Bytes out
  2950. memcpy(frame->rawdata, in + offset, 32 - offset);
  2951. read += 32;
  2952. }
  2953. while (read < n - 1) {
  2954. allzero = 1;
  2955. for (b = 0; b < 32; b++) {
  2956. if (in[read + b]) {
  2957. allzero = 0;
  2958. break;
  2959. }
  2960. }
  2961. if (allzero) {
  2962. /* Don't copy it */
  2963. } else {
  2964. if (frame->bytes_recvd + copied + 32 <= max_raw)
  2965. {
  2966. memcpy(frame->rawdata
  2967. + frame->bytes_recvd + copied,
  2968. in + read, 32);
  2969. copied += 32;
  2970. } else {
  2971. PDEBUG(3, "Raw data buffer overrun!!");
  2972. }
  2973. }
  2974. read += 32;
  2975. }
  2976. frame->bytes_recvd += copied;
  2977. }
  2978. }
  2979. static inline void
  2980. ov518_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
  2981. {
  2982. int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
  2983. struct ov511_frame *frame = &ov->frame[ov->curframe];
  2984. struct timeval *ts;
  2985. /* Don't copy the packet number byte */
  2986. if (ov->packet_numbering)
  2987. --n;
  2988. /* A false positive here is likely, until OVT gives me
  2989. * the definitive SOF/EOF format */
  2990. if ((!(in[0] | in[1] | in[2] | in[3] | in[5])) && in[6]) {
  2991. if (printph) {
  2992. info("ph: %2x %2x %2x %2x %2x %2x %2x %2x", in[0],
  2993. in[1], in[2], in[3], in[4], in[5], in[6], in[7]);
  2994. }
  2995. if (frame->scanstate == STATE_LINES) {
  2996. PDEBUG(4, "Detected frame end/start");
  2997. goto eof;
  2998. } else { //scanstate == STATE_SCANNING
  2999. /* Frame start */
  3000. PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
  3001. goto sof;
  3002. }
  3003. } else {
  3004. goto check_middle;
  3005. }
  3006. eof:
  3007. ts = (struct timeval *)(frame->data
  3008. + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
  3009. do_gettimeofday(ts);
  3010. PDEBUG(4, "Frame end, curframe = %d, hw=%d, vw=%d, recvd=%d",
  3011. ov->curframe,
  3012. (int)(in[9]), (int)(in[10]), frame->bytes_recvd);
  3013. // FIXME: Since we don't know the header formats yet,
  3014. // there is no way to know what the actual image size is
  3015. frame->rawwidth = frame->width;
  3016. frame->rawheight = frame->height;
  3017. /* Validate the header data */
  3018. RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
  3019. RESTRICT_TO_RANGE(frame->rawheight, ov->minheight, ov->maxheight);
  3020. /* Don't allow byte count to exceed buffer size */
  3021. RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
  3022. if (frame->scanstate == STATE_LINES) {
  3023. int nextf;
  3024. frame->grabstate = FRAME_DONE;
  3025. wake_up_interruptible(&frame->wq);
  3026. /* If next frame is ready or grabbing,
  3027. * point to it */
  3028. nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
  3029. if (ov->frame[nextf].grabstate == FRAME_READY
  3030. || ov->frame[nextf].grabstate == FRAME_GRABBING) {
  3031. ov->curframe = nextf;
  3032. ov->frame[nextf].scanstate = STATE_SCANNING;
  3033. frame = &ov->frame[nextf];
  3034. } else {
  3035. if (frame->grabstate == FRAME_DONE) {
  3036. PDEBUG(4, "** Frame done **");
  3037. } else {
  3038. PDEBUG(4, "Frame not ready? state = %d",
  3039. ov->frame[nextf].grabstate);
  3040. }
  3041. ov->curframe = -1;
  3042. PDEBUG(4, "SOF dropped (no active frame)");
  3043. return; /* Nowhere to store this frame */
  3044. }
  3045. }
  3046. sof:
  3047. PDEBUG(4, "Starting capture on frame %d", frame->framenum);
  3048. // Snapshot not reverse-engineered yet.
  3049. #if 0
  3050. /* Check to see if it's a snapshot frame */
  3051. /* FIXME?? Should the snapshot reset go here? Performance? */
  3052. if (in[8] & 0x02) {
  3053. frame->snapshot = 1;
  3054. PDEBUG(3, "snapshot detected");
  3055. }
  3056. #endif
  3057. frame->scanstate = STATE_LINES;
  3058. frame->bytes_recvd = 0;
  3059. frame->compressed = 1;
  3060. check_middle:
  3061. /* Are we in a frame? */
  3062. if (frame->scanstate != STATE_LINES) {
  3063. PDEBUG(4, "scanstate: no SOF yet");
  3064. return;
  3065. }
  3066. /* Dump all data exactly as received */
  3067. if (dumppix == 2) {
  3068. frame->bytes_recvd += n;
  3069. if (frame->bytes_recvd <= max_raw)
  3070. memcpy(frame->rawdata + frame->bytes_recvd - n, in, n);
  3071. else
  3072. PDEBUG(3, "Raw data buffer overrun!! (%d)",
  3073. frame->bytes_recvd - max_raw);
  3074. } else {
  3075. /* All incoming data are divided into 8-byte segments. If the
  3076. * segment contains all zero bytes, it must be skipped. These
  3077. * zero-segments allow the OV518 to mainain a constant data rate
  3078. * regardless of the effectiveness of the compression. Segments
  3079. * are aligned relative to the beginning of each isochronous
  3080. * packet. The first segment in each image is a header (the
  3081. * decompressor skips it later).
  3082. */
  3083. int b, read = 0, allzero, copied = 0;
  3084. while (read < n) {
  3085. allzero = 1;
  3086. for (b = 0; b < 8; b++) {
  3087. if (in[read + b]) {
  3088. allzero = 0;
  3089. break;
  3090. }
  3091. }
  3092. if (allzero) {
  3093. /* Don't copy it */
  3094. } else {
  3095. if (frame->bytes_recvd + copied + 8 <= max_raw)
  3096. {
  3097. memcpy(frame->rawdata
  3098. + frame->bytes_recvd + copied,
  3099. in + read, 8);
  3100. copied += 8;
  3101. } else {
  3102. PDEBUG(3, "Raw data buffer overrun!!");
  3103. }
  3104. }
  3105. read += 8;
  3106. }
  3107. frame->bytes_recvd += copied;
  3108. }
  3109. }
  3110. static void
  3111. ov51x_isoc_irq(struct urb *urb, struct pt_regs *regs)
  3112. {
  3113. int i;
  3114. struct usb_ov511 *ov;
  3115. struct ov511_sbuf *sbuf;
  3116. if (!urb->context) {
  3117. PDEBUG(4, "no context");
  3118. return;
  3119. }
  3120. sbuf = urb->context;
  3121. ov = sbuf->ov;
  3122. if (!ov || !ov->dev || !ov->user) {
  3123. PDEBUG(4, "no device, or not open");
  3124. return;
  3125. }
  3126. if (!ov->streaming) {
  3127. PDEBUG(4, "hmmm... not streaming, but got interrupt");
  3128. return;
  3129. }
  3130. if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
  3131. PDEBUG(4, "URB unlinked");
  3132. return;
  3133. }
  3134. if (urb->status != -EINPROGRESS && urb->status != 0) {
  3135. err("ERROR: urb->status=%d: %s", urb->status,
  3136. symbolic(urb_errlist, urb->status));
  3137. }
  3138. /* Copy the data received into our frame buffer */
  3139. PDEBUG(5, "sbuf[%d]: Moving %d packets", sbuf->n,
  3140. urb->number_of_packets);
  3141. for (i = 0; i < urb->number_of_packets; i++) {
  3142. /* Warning: Don't call *_move_data() if no frame active! */
  3143. if (ov->curframe >= 0) {
  3144. int n = urb->iso_frame_desc[i].actual_length;
  3145. int st = urb->iso_frame_desc[i].status;
  3146. unsigned char *cdata;
  3147. urb->iso_frame_desc[i].actual_length = 0;
  3148. urb->iso_frame_desc[i].status = 0;
  3149. cdata = urb->transfer_buffer
  3150. + urb->iso_frame_desc[i].offset;
  3151. if (!n) {
  3152. PDEBUG(4, "Zero-length packet");
  3153. continue;
  3154. }
  3155. if (st)
  3156. PDEBUG(2, "data error: [%d] len=%d, status=%d",
  3157. i, n, st);
  3158. if (ov->bclass == BCL_OV511)
  3159. ov511_move_data(ov, cdata, n);
  3160. else if (ov->bclass == BCL_OV518)
  3161. ov518_move_data(ov, cdata, n);
  3162. else
  3163. err("Unknown bridge device (%d)", ov->bridge);
  3164. } else if (waitqueue_active(&ov->wq)) {
  3165. wake_up_interruptible(&ov->wq);
  3166. }
  3167. }
  3168. /* Resubmit this URB */
  3169. urb->dev = ov->dev;
  3170. if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
  3171. err("usb_submit_urb() ret %d", i);
  3172. return;
  3173. }
  3174. /****************************************************************************
  3175. *
  3176. * Stream initialization and termination
  3177. *
  3178. ***************************************************************************/
  3179. static int
  3180. ov51x_init_isoc(struct usb_ov511 *ov)
  3181. {
  3182. struct urb *urb;
  3183. int fx, err, n, size;
  3184. PDEBUG(3, "*** Initializing capture ***");
  3185. ov->curframe = -1;
  3186. if (ov->bridge == BRG_OV511) {
  3187. if (cams == 1)
  3188. size = 993;
  3189. else if (cams == 2)
  3190. size = 513;
  3191. else if (cams == 3 || cams == 4)
  3192. size = 257;
  3193. else {
  3194. err("\"cams\" parameter too high!");
  3195. return -1;
  3196. }
  3197. } else if (ov->bridge == BRG_OV511PLUS) {
  3198. if (cams == 1)
  3199. size = 961;
  3200. else if (cams == 2)
  3201. size = 513;
  3202. else if (cams == 3 || cams == 4)
  3203. size = 257;
  3204. else if (cams >= 5 && cams <= 8)
  3205. size = 129;
  3206. else if (cams >= 9 && cams <= 31)
  3207. size = 33;
  3208. else {
  3209. err("\"cams\" parameter too high!");
  3210. return -1;
  3211. }
  3212. } else if (ov->bclass == BCL_OV518) {
  3213. if (cams == 1)
  3214. size = 896;
  3215. else if (cams == 2)
  3216. size = 512;
  3217. else if (cams == 3 || cams == 4)
  3218. size = 256;
  3219. else if (cams >= 5 && cams <= 8)
  3220. size = 128;
  3221. else {
  3222. err("\"cams\" parameter too high!");
  3223. return -1;
  3224. }
  3225. } else {
  3226. err("invalid bridge type");
  3227. return -1;
  3228. }
  3229. // FIXME: OV518 is hardcoded to 15 FPS (alternate 5) for now
  3230. if (ov->bclass == BCL_OV518) {
  3231. if (packetsize == -1) {
  3232. ov518_set_packet_size(ov, 640);
  3233. } else {
  3234. info("Forcing packet size to %d", packetsize);
  3235. ov518_set_packet_size(ov, packetsize);
  3236. }
  3237. } else {
  3238. if (packetsize == -1) {
  3239. ov511_set_packet_size(ov, size);
  3240. } else {
  3241. info("Forcing packet size to %d", packetsize);
  3242. ov511_set_packet_size(ov, packetsize);
  3243. }
  3244. }
  3245. for (n = 0; n < OV511_NUMSBUF; n++) {
  3246. urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  3247. if (!urb) {
  3248. err("init isoc: usb_alloc_urb ret. NULL");
  3249. return -ENOMEM;
  3250. }
  3251. ov->sbuf[n].urb = urb;
  3252. urb->dev = ov->dev;
  3253. urb->context = &ov->sbuf[n];
  3254. urb->pipe = usb_rcvisocpipe(ov->dev, OV511_ENDPOINT_ADDRESS);
  3255. urb->transfer_flags = URB_ISO_ASAP;
  3256. urb->transfer_buffer = ov->sbuf[n].data;
  3257. urb->complete = ov51x_isoc_irq;
  3258. urb->number_of_packets = FRAMES_PER_DESC;
  3259. urb->transfer_buffer_length = ov->packet_size * FRAMES_PER_DESC;
  3260. urb->interval = 1;
  3261. for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
  3262. urb->iso_frame_desc[fx].offset = ov->packet_size * fx;
  3263. urb->iso_frame_desc[fx].length = ov->packet_size;
  3264. }
  3265. }
  3266. ov->streaming = 1;
  3267. for (n = 0; n < OV511_NUMSBUF; n++) {
  3268. ov->sbuf[n].urb->dev = ov->dev;
  3269. err = usb_submit_urb(ov->sbuf[n].urb, GFP_KERNEL);
  3270. if (err) {
  3271. err("init isoc: usb_submit_urb(%d) ret %d", n, err);
  3272. return err;
  3273. }
  3274. }
  3275. return 0;
  3276. }
  3277. static void
  3278. ov51x_unlink_isoc(struct usb_ov511 *ov)
  3279. {
  3280. int n;
  3281. /* Unschedule all of the iso td's */
  3282. for (n = OV511_NUMSBUF - 1; n >= 0; n--) {
  3283. if (ov->sbuf[n].urb) {
  3284. usb_kill_urb(ov->sbuf[n].urb);
  3285. usb_free_urb(ov->sbuf[n].urb);
  3286. ov->sbuf[n].urb = NULL;
  3287. }
  3288. }
  3289. }
  3290. static void
  3291. ov51x_stop_isoc(struct usb_ov511 *ov)
  3292. {
  3293. if (!ov->streaming || !ov->dev)
  3294. return;
  3295. PDEBUG(3, "*** Stopping capture ***");
  3296. if (ov->bclass == BCL_OV518)
  3297. ov518_set_packet_size(ov, 0);
  3298. else
  3299. ov511_set_packet_size(ov, 0);
  3300. ov->streaming = 0;
  3301. ov51x_unlink_isoc(ov);
  3302. }
  3303. static int
  3304. ov51x_new_frame(struct usb_ov511 *ov, int framenum)
  3305. {
  3306. struct ov511_frame *frame;
  3307. int newnum;
  3308. PDEBUG(4, "ov->curframe = %d, framenum = %d", ov->curframe, framenum);
  3309. if (!ov->dev)
  3310. return -1;
  3311. /* If we're not grabbing a frame right now and the other frame is */
  3312. /* ready to be grabbed into, then use it instead */
  3313. if (ov->curframe == -1) {
  3314. newnum = (framenum - 1 + OV511_NUMFRAMES) % OV511_NUMFRAMES;
  3315. if (ov->frame[newnum].grabstate == FRAME_READY)
  3316. framenum = newnum;
  3317. } else
  3318. return 0;
  3319. frame = &ov->frame[framenum];
  3320. PDEBUG(4, "framenum = %d, width = %d, height = %d", framenum,
  3321. frame->width, frame->height);
  3322. frame->grabstate = FRAME_GRABBING;
  3323. frame->scanstate = STATE_SCANNING;
  3324. frame->snapshot = 0;
  3325. ov->curframe = framenum;
  3326. /* Make sure it's not too big */
  3327. if (frame->width > ov->maxwidth)
  3328. frame->width = ov->maxwidth;
  3329. frame->width &= ~7L; /* Multiple of 8 */
  3330. if (frame->height > ov->maxheight)
  3331. frame->height = ov->maxheight;
  3332. frame->height &= ~3L; /* Multiple of 4 */
  3333. return 0;
  3334. }
  3335. /****************************************************************************
  3336. *
  3337. * Buffer management
  3338. *
  3339. ***************************************************************************/
  3340. /*
  3341. * - You must acquire buf_lock before entering this function.
  3342. * - Because this code will free any non-null pointer, you must be sure to null
  3343. * them if you explicitly free them somewhere else!
  3344. */
  3345. static void
  3346. ov51x_do_dealloc(struct usb_ov511 *ov)
  3347. {
  3348. int i;
  3349. PDEBUG(4, "entered");
  3350. if (ov->fbuf) {
  3351. rvfree(ov->fbuf, OV511_NUMFRAMES
  3352. * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
  3353. ov->fbuf = NULL;
  3354. }
  3355. vfree(ov->rawfbuf);
  3356. ov->rawfbuf = NULL;
  3357. vfree(ov->tempfbuf);
  3358. ov->tempfbuf = NULL;
  3359. for (i = 0; i < OV511_NUMSBUF; i++) {
  3360. kfree(ov->sbuf[i].data);
  3361. ov->sbuf[i].data = NULL;
  3362. }
  3363. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3364. ov->frame[i].data = NULL;
  3365. ov->frame[i].rawdata = NULL;
  3366. ov->frame[i].tempdata = NULL;
  3367. if (ov->frame[i].compbuf) {
  3368. free_page((unsigned long) ov->frame[i].compbuf);
  3369. ov->frame[i].compbuf = NULL;
  3370. }
  3371. }
  3372. PDEBUG(4, "buffer memory deallocated");
  3373. ov->buf_state = BUF_NOT_ALLOCATED;
  3374. PDEBUG(4, "leaving");
  3375. }
  3376. static int
  3377. ov51x_alloc(struct usb_ov511 *ov)
  3378. {
  3379. int i;
  3380. const int w = ov->maxwidth;
  3381. const int h = ov->maxheight;
  3382. const int data_bufsize = OV511_NUMFRAMES * MAX_DATA_SIZE(w, h);
  3383. const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h);
  3384. PDEBUG(4, "entered");
  3385. down(&ov->buf_lock);
  3386. if (ov->buf_state == BUF_ALLOCATED)
  3387. goto out;
  3388. ov->fbuf = rvmalloc(data_bufsize);
  3389. if (!ov->fbuf)
  3390. goto error;
  3391. ov->rawfbuf = vmalloc(raw_bufsize);
  3392. if (!ov->rawfbuf)
  3393. goto error;
  3394. memset(ov->rawfbuf, 0, raw_bufsize);
  3395. ov->tempfbuf = vmalloc(raw_bufsize);
  3396. if (!ov->tempfbuf)
  3397. goto error;
  3398. memset(ov->tempfbuf, 0, raw_bufsize);
  3399. for (i = 0; i < OV511_NUMSBUF; i++) {
  3400. ov->sbuf[i].data = kmalloc(FRAMES_PER_DESC *
  3401. MAX_FRAME_SIZE_PER_DESC, GFP_KERNEL);
  3402. if (!ov->sbuf[i].data)
  3403. goto error;
  3404. PDEBUG(4, "sbuf[%d] @ %p", i, ov->sbuf[i].data);
  3405. }
  3406. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3407. ov->frame[i].data = ov->fbuf + i * MAX_DATA_SIZE(w, h);
  3408. ov->frame[i].rawdata = ov->rawfbuf
  3409. + i * MAX_RAW_DATA_SIZE(w, h);
  3410. ov->frame[i].tempdata = ov->tempfbuf
  3411. + i * MAX_RAW_DATA_SIZE(w, h);
  3412. ov->frame[i].compbuf =
  3413. (unsigned char *) __get_free_page(GFP_KERNEL);
  3414. if (!ov->frame[i].compbuf)
  3415. goto error;
  3416. PDEBUG(4, "frame[%d] @ %p", i, ov->frame[i].data);
  3417. }
  3418. ov->buf_state = BUF_ALLOCATED;
  3419. out:
  3420. up(&ov->buf_lock);
  3421. PDEBUG(4, "leaving");
  3422. return 0;
  3423. error:
  3424. ov51x_do_dealloc(ov);
  3425. up(&ov->buf_lock);
  3426. PDEBUG(4, "errored");
  3427. return -ENOMEM;
  3428. }
  3429. static void
  3430. ov51x_dealloc(struct usb_ov511 *ov)
  3431. {
  3432. PDEBUG(4, "entered");
  3433. down(&ov->buf_lock);
  3434. ov51x_do_dealloc(ov);
  3435. up(&ov->buf_lock);
  3436. PDEBUG(4, "leaving");
  3437. }
  3438. /****************************************************************************
  3439. *
  3440. * V4L 1 API
  3441. *
  3442. ***************************************************************************/
  3443. static int
  3444. ov51x_v4l1_open(struct inode *inode, struct file *file)
  3445. {
  3446. struct video_device *vdev = video_devdata(file);
  3447. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3448. int err, i;
  3449. PDEBUG(4, "opening");
  3450. down(&ov->lock);
  3451. err = -EBUSY;
  3452. if (ov->user)
  3453. goto out;
  3454. ov->sub_flag = 0;
  3455. /* In case app doesn't set them... */
  3456. err = ov51x_set_default_params(ov);
  3457. if (err < 0)
  3458. goto out;
  3459. /* Make sure frames are reset */
  3460. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3461. ov->frame[i].grabstate = FRAME_UNUSED;
  3462. ov->frame[i].bytes_read = 0;
  3463. }
  3464. /* If compression is on, make sure now that a
  3465. * decompressor can be loaded */
  3466. if (ov->compress && !ov->decomp_ops) {
  3467. err = request_decompressor(ov);
  3468. if (err && !dumppix)
  3469. goto out;
  3470. }
  3471. err = ov51x_alloc(ov);
  3472. if (err < 0)
  3473. goto out;
  3474. err = ov51x_init_isoc(ov);
  3475. if (err) {
  3476. ov51x_dealloc(ov);
  3477. goto out;
  3478. }
  3479. ov->user++;
  3480. file->private_data = vdev;
  3481. if (ov->led_policy == LED_AUTO)
  3482. ov51x_led_control(ov, 1);
  3483. out:
  3484. up(&ov->lock);
  3485. return err;
  3486. }
  3487. static int
  3488. ov51x_v4l1_close(struct inode *inode, struct file *file)
  3489. {
  3490. struct video_device *vdev = file->private_data;
  3491. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3492. PDEBUG(4, "ov511_close");
  3493. down(&ov->lock);
  3494. ov->user--;
  3495. ov51x_stop_isoc(ov);
  3496. release_decompressor(ov);
  3497. if (ov->led_policy == LED_AUTO)
  3498. ov51x_led_control(ov, 0);
  3499. if (ov->dev)
  3500. ov51x_dealloc(ov);
  3501. up(&ov->lock);
  3502. /* Device unplugged while open. Only a minimum of unregistration is done
  3503. * here; the disconnect callback already did the rest. */
  3504. if (!ov->dev) {
  3505. down(&ov->cbuf_lock);
  3506. kfree(ov->cbuf);
  3507. ov->cbuf = NULL;
  3508. up(&ov->cbuf_lock);
  3509. ov51x_dealloc(ov);
  3510. kfree(ov);
  3511. ov = NULL;
  3512. }
  3513. file->private_data = NULL;
  3514. return 0;
  3515. }
  3516. /* Do not call this function directly! */
  3517. static int
  3518. ov51x_v4l1_ioctl_internal(struct inode *inode, struct file *file,
  3519. unsigned int cmd, void *arg)
  3520. {
  3521. struct video_device *vdev = file->private_data;
  3522. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3523. PDEBUG(5, "IOCtl: 0x%X", cmd);
  3524. if (!ov->dev)
  3525. return -EIO;
  3526. switch (cmd) {
  3527. case VIDIOCGCAP:
  3528. {
  3529. struct video_capability *b = arg;
  3530. PDEBUG(4, "VIDIOCGCAP");
  3531. memset(b, 0, sizeof(struct video_capability));
  3532. sprintf(b->name, "%s USB Camera",
  3533. symbolic(brglist, ov->bridge));
  3534. b->type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE;
  3535. b->channels = ov->num_inputs;
  3536. b->audios = 0;
  3537. b->maxwidth = ov->maxwidth;
  3538. b->maxheight = ov->maxheight;
  3539. b->minwidth = ov->minwidth;
  3540. b->minheight = ov->minheight;
  3541. return 0;
  3542. }
  3543. case VIDIOCGCHAN:
  3544. {
  3545. struct video_channel *v = arg;
  3546. PDEBUG(4, "VIDIOCGCHAN");
  3547. if ((unsigned)(v->channel) >= ov->num_inputs) {
  3548. err("Invalid channel (%d)", v->channel);
  3549. return -EINVAL;
  3550. }
  3551. v->norm = ov->norm;
  3552. v->type = VIDEO_TYPE_CAMERA;
  3553. v->flags = 0;
  3554. // v->flags |= (ov->has_decoder) ? VIDEO_VC_NORM : 0;
  3555. v->tuners = 0;
  3556. decoder_get_input_name(ov, v->channel, v->name);
  3557. return 0;
  3558. }
  3559. case VIDIOCSCHAN:
  3560. {
  3561. struct video_channel *v = arg;
  3562. int err;
  3563. PDEBUG(4, "VIDIOCSCHAN");
  3564. /* Make sure it's not a camera */
  3565. if (!ov->has_decoder) {
  3566. if (v->channel == 0)
  3567. return 0;
  3568. else
  3569. return -EINVAL;
  3570. }
  3571. if (v->norm != VIDEO_MODE_PAL &&
  3572. v->norm != VIDEO_MODE_NTSC &&
  3573. v->norm != VIDEO_MODE_SECAM &&
  3574. v->norm != VIDEO_MODE_AUTO) {
  3575. err("Invalid norm (%d)", v->norm);
  3576. return -EINVAL;
  3577. }
  3578. if ((unsigned)(v->channel) >= ov->num_inputs) {
  3579. err("Invalid channel (%d)", v->channel);
  3580. return -EINVAL;
  3581. }
  3582. err = decoder_set_input(ov, v->channel);
  3583. if (err)
  3584. return err;
  3585. err = decoder_set_norm(ov, v->norm);
  3586. if (err)
  3587. return err;
  3588. return 0;
  3589. }
  3590. case VIDIOCGPICT:
  3591. {
  3592. struct video_picture *p = arg;
  3593. PDEBUG(4, "VIDIOCGPICT");
  3594. memset(p, 0, sizeof(struct video_picture));
  3595. if (sensor_get_picture(ov, p))
  3596. return -EIO;
  3597. /* Can we get these from frame[0]? -claudio? */
  3598. p->depth = ov->frame[0].depth;
  3599. p->palette = ov->frame[0].format;
  3600. return 0;
  3601. }
  3602. case VIDIOCSPICT:
  3603. {
  3604. struct video_picture *p = arg;
  3605. int i, rc;
  3606. PDEBUG(4, "VIDIOCSPICT");
  3607. if (!get_depth(p->palette))
  3608. return -EINVAL;
  3609. if (sensor_set_picture(ov, p))
  3610. return -EIO;
  3611. if (force_palette && p->palette != force_palette) {
  3612. info("Palette rejected (%s)",
  3613. symbolic(v4l1_plist, p->palette));
  3614. return -EINVAL;
  3615. }
  3616. // FIXME: Format should be independent of frames
  3617. if (p->palette != ov->frame[0].format) {
  3618. PDEBUG(4, "Detected format change");
  3619. rc = ov51x_wait_frames_inactive(ov);
  3620. if (rc)
  3621. return rc;
  3622. mode_init_regs(ov, ov->frame[0].width,
  3623. ov->frame[0].height, p->palette, ov->sub_flag);
  3624. }
  3625. PDEBUG(4, "Setting depth=%d, palette=%s",
  3626. p->depth, symbolic(v4l1_plist, p->palette));
  3627. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3628. ov->frame[i].depth = p->depth;
  3629. ov->frame[i].format = p->palette;
  3630. }
  3631. return 0;
  3632. }
  3633. case VIDIOCGCAPTURE:
  3634. {
  3635. int *vf = arg;
  3636. PDEBUG(4, "VIDIOCGCAPTURE");
  3637. ov->sub_flag = *vf;
  3638. return 0;
  3639. }
  3640. case VIDIOCSCAPTURE:
  3641. {
  3642. struct video_capture *vc = arg;
  3643. PDEBUG(4, "VIDIOCSCAPTURE");
  3644. if (vc->flags)
  3645. return -EINVAL;
  3646. if (vc->decimation)
  3647. return -EINVAL;
  3648. vc->x &= ~3L;
  3649. vc->y &= ~1L;
  3650. vc->y &= ~31L;
  3651. if (vc->width == 0)
  3652. vc->width = 32;
  3653. vc->height /= 16;
  3654. vc->height *= 16;
  3655. if (vc->height == 0)
  3656. vc->height = 16;
  3657. ov->subx = vc->x;
  3658. ov->suby = vc->y;
  3659. ov->subw = vc->width;
  3660. ov->subh = vc->height;
  3661. return 0;
  3662. }
  3663. case VIDIOCSWIN:
  3664. {
  3665. struct video_window *vw = arg;
  3666. int i, rc;
  3667. PDEBUG(4, "VIDIOCSWIN: %dx%d", vw->width, vw->height);
  3668. #if 0
  3669. if (vw->flags)
  3670. return -EINVAL;
  3671. if (vw->clipcount)
  3672. return -EINVAL;
  3673. if (vw->height != ov->maxheight)
  3674. return -EINVAL;
  3675. if (vw->width != ov->maxwidth)
  3676. return -EINVAL;
  3677. #endif
  3678. rc = ov51x_wait_frames_inactive(ov);
  3679. if (rc)
  3680. return rc;
  3681. rc = mode_init_regs(ov, vw->width, vw->height,
  3682. ov->frame[0].format, ov->sub_flag);
  3683. if (rc < 0)
  3684. return rc;
  3685. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3686. ov->frame[i].width = vw->width;
  3687. ov->frame[i].height = vw->height;
  3688. }
  3689. return 0;
  3690. }
  3691. case VIDIOCGWIN:
  3692. {
  3693. struct video_window *vw = arg;
  3694. memset(vw, 0, sizeof(struct video_window));
  3695. vw->x = 0; /* FIXME */
  3696. vw->y = 0;
  3697. vw->width = ov->frame[0].width;
  3698. vw->height = ov->frame[0].height;
  3699. vw->flags = 30;
  3700. PDEBUG(4, "VIDIOCGWIN: %dx%d", vw->width, vw->height);
  3701. return 0;
  3702. }
  3703. case VIDIOCGMBUF:
  3704. {
  3705. struct video_mbuf *vm = arg;
  3706. int i;
  3707. PDEBUG(4, "VIDIOCGMBUF");
  3708. memset(vm, 0, sizeof(struct video_mbuf));
  3709. vm->size = OV511_NUMFRAMES
  3710. * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
  3711. vm->frames = OV511_NUMFRAMES;
  3712. vm->offsets[0] = 0;
  3713. for (i = 1; i < OV511_NUMFRAMES; i++) {
  3714. vm->offsets[i] = vm->offsets[i-1]
  3715. + MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
  3716. }
  3717. return 0;
  3718. }
  3719. case VIDIOCMCAPTURE:
  3720. {
  3721. struct video_mmap *vm = arg;
  3722. int rc, depth;
  3723. unsigned int f = vm->frame;
  3724. PDEBUG(4, "VIDIOCMCAPTURE: frame: %d, %dx%d, %s", f, vm->width,
  3725. vm->height, symbolic(v4l1_plist, vm->format));
  3726. depth = get_depth(vm->format);
  3727. if (!depth) {
  3728. PDEBUG(2, "VIDIOCMCAPTURE: invalid format (%s)",
  3729. symbolic(v4l1_plist, vm->format));
  3730. return -EINVAL;
  3731. }
  3732. if (f >= OV511_NUMFRAMES) {
  3733. err("VIDIOCMCAPTURE: invalid frame (%d)", f);
  3734. return -EINVAL;
  3735. }
  3736. if (vm->width > ov->maxwidth
  3737. || vm->height > ov->maxheight) {
  3738. err("VIDIOCMCAPTURE: requested dimensions too big");
  3739. return -EINVAL;
  3740. }
  3741. if (ov->frame[f].grabstate == FRAME_GRABBING) {
  3742. PDEBUG(4, "VIDIOCMCAPTURE: already grabbing");
  3743. return -EBUSY;
  3744. }
  3745. if (force_palette && (vm->format != force_palette)) {
  3746. PDEBUG(2, "palette rejected (%s)",
  3747. symbolic(v4l1_plist, vm->format));
  3748. return -EINVAL;
  3749. }
  3750. if ((ov->frame[f].width != vm->width) ||
  3751. (ov->frame[f].height != vm->height) ||
  3752. (ov->frame[f].format != vm->format) ||
  3753. (ov->frame[f].sub_flag != ov->sub_flag) ||
  3754. (ov->frame[f].depth != depth)) {
  3755. PDEBUG(4, "VIDIOCMCAPTURE: change in image parameters");
  3756. rc = ov51x_wait_frames_inactive(ov);
  3757. if (rc)
  3758. return rc;
  3759. rc = mode_init_regs(ov, vm->width, vm->height,
  3760. vm->format, ov->sub_flag);
  3761. #if 0
  3762. if (rc < 0) {
  3763. PDEBUG(1, "Got error while initializing regs ");
  3764. return ret;
  3765. }
  3766. #endif
  3767. ov->frame[f].width = vm->width;
  3768. ov->frame[f].height = vm->height;
  3769. ov->frame[f].format = vm->format;
  3770. ov->frame[f].sub_flag = ov->sub_flag;
  3771. ov->frame[f].depth = depth;
  3772. }
  3773. /* Mark it as ready */
  3774. ov->frame[f].grabstate = FRAME_READY;
  3775. PDEBUG(4, "VIDIOCMCAPTURE: renewing frame %d", f);
  3776. return ov51x_new_frame(ov, f);
  3777. }
  3778. case VIDIOCSYNC:
  3779. {
  3780. unsigned int fnum = *((unsigned int *) arg);
  3781. struct ov511_frame *frame;
  3782. int rc;
  3783. if (fnum >= OV511_NUMFRAMES) {
  3784. err("VIDIOCSYNC: invalid frame (%d)", fnum);
  3785. return -EINVAL;
  3786. }
  3787. frame = &ov->frame[fnum];
  3788. PDEBUG(4, "syncing to frame %d, grabstate = %d", fnum,
  3789. frame->grabstate);
  3790. switch (frame->grabstate) {
  3791. case FRAME_UNUSED:
  3792. return -EINVAL;
  3793. case FRAME_READY:
  3794. case FRAME_GRABBING:
  3795. case FRAME_ERROR:
  3796. redo:
  3797. if (!ov->dev)
  3798. return -EIO;
  3799. rc = wait_event_interruptible(frame->wq,
  3800. (frame->grabstate == FRAME_DONE)
  3801. || (frame->grabstate == FRAME_ERROR));
  3802. if (rc)
  3803. return rc;
  3804. if (frame->grabstate == FRAME_ERROR) {
  3805. if ((rc = ov51x_new_frame(ov, fnum)) < 0)
  3806. return rc;
  3807. goto redo;
  3808. }
  3809. /* Fall through */
  3810. case FRAME_DONE:
  3811. if (ov->snap_enabled && !frame->snapshot) {
  3812. if ((rc = ov51x_new_frame(ov, fnum)) < 0)
  3813. return rc;
  3814. goto redo;
  3815. }
  3816. frame->grabstate = FRAME_UNUSED;
  3817. /* Reset the hardware snapshot button */
  3818. /* FIXME - Is this the best place for this? */
  3819. if ((ov->snap_enabled) && (frame->snapshot)) {
  3820. frame->snapshot = 0;
  3821. ov51x_clear_snapshot(ov);
  3822. }
  3823. /* Decompression, format conversion, etc... */
  3824. ov51x_postprocess(ov, frame);
  3825. break;
  3826. } /* end switch */
  3827. return 0;
  3828. }
  3829. case VIDIOCGFBUF:
  3830. {
  3831. struct video_buffer *vb = arg;
  3832. PDEBUG(4, "VIDIOCGFBUF");
  3833. memset(vb, 0, sizeof(struct video_buffer));
  3834. return 0;
  3835. }
  3836. case VIDIOCGUNIT:
  3837. {
  3838. struct video_unit *vu = arg;
  3839. PDEBUG(4, "VIDIOCGUNIT");
  3840. memset(vu, 0, sizeof(struct video_unit));
  3841. vu->video = ov->vdev->minor;
  3842. vu->vbi = VIDEO_NO_UNIT;
  3843. vu->radio = VIDEO_NO_UNIT;
  3844. vu->audio = VIDEO_NO_UNIT;
  3845. vu->teletext = VIDEO_NO_UNIT;
  3846. return 0;
  3847. }
  3848. case OV511IOC_WI2C:
  3849. {
  3850. struct ov511_i2c_struct *w = arg;
  3851. return i2c_w_slave(ov, w->slave, w->reg, w->value, w->mask);
  3852. }
  3853. case OV511IOC_RI2C:
  3854. {
  3855. struct ov511_i2c_struct *r = arg;
  3856. int rc;
  3857. rc = i2c_r_slave(ov, r->slave, r->reg);
  3858. if (rc < 0)
  3859. return rc;
  3860. r->value = rc;
  3861. return 0;
  3862. }
  3863. default:
  3864. PDEBUG(3, "Unsupported IOCtl: 0x%X", cmd);
  3865. return -ENOIOCTLCMD;
  3866. } /* end switch */
  3867. return 0;
  3868. }
  3869. static int
  3870. ov51x_v4l1_ioctl(struct inode *inode, struct file *file,
  3871. unsigned int cmd, unsigned long arg)
  3872. {
  3873. struct video_device *vdev = file->private_data;
  3874. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3875. int rc;
  3876. if (down_interruptible(&ov->lock))
  3877. return -EINTR;
  3878. rc = video_usercopy(inode, file, cmd, arg, ov51x_v4l1_ioctl_internal);
  3879. up(&ov->lock);
  3880. return rc;
  3881. }
  3882. static ssize_t
  3883. ov51x_v4l1_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
  3884. {
  3885. struct video_device *vdev = file->private_data;
  3886. int noblock = file->f_flags&O_NONBLOCK;
  3887. unsigned long count = cnt;
  3888. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3889. int i, rc = 0, frmx = -1;
  3890. struct ov511_frame *frame;
  3891. if (down_interruptible(&ov->lock))
  3892. return -EINTR;
  3893. PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
  3894. if (!vdev || !buf) {
  3895. rc = -EFAULT;
  3896. goto error;
  3897. }
  3898. if (!ov->dev) {
  3899. rc = -EIO;
  3900. goto error;
  3901. }
  3902. // FIXME: Only supports two frames
  3903. /* See if a frame is completed, then use it. */
  3904. if (ov->frame[0].grabstate >= FRAME_DONE) /* _DONE or _ERROR */
  3905. frmx = 0;
  3906. else if (ov->frame[1].grabstate >= FRAME_DONE)/* _DONE or _ERROR */
  3907. frmx = 1;
  3908. /* If nonblocking we return immediately */
  3909. if (noblock && (frmx == -1)) {
  3910. rc = -EAGAIN;
  3911. goto error;
  3912. }
  3913. /* If no FRAME_DONE, look for a FRAME_GRABBING state. */
  3914. /* See if a frame is in process (grabbing), then use it. */
  3915. if (frmx == -1) {
  3916. if (ov->frame[0].grabstate == FRAME_GRABBING)
  3917. frmx = 0;
  3918. else if (ov->frame[1].grabstate == FRAME_GRABBING)
  3919. frmx = 1;
  3920. }
  3921. /* If no frame is active, start one. */
  3922. if (frmx == -1) {
  3923. if ((rc = ov51x_new_frame(ov, frmx = 0))) {
  3924. err("read: ov51x_new_frame error");
  3925. goto error;
  3926. }
  3927. }
  3928. frame = &ov->frame[frmx];
  3929. restart:
  3930. if (!ov->dev) {
  3931. rc = -EIO;
  3932. goto error;
  3933. }
  3934. /* Wait while we're grabbing the image */
  3935. PDEBUG(4, "Waiting image grabbing");
  3936. rc = wait_event_interruptible(frame->wq,
  3937. (frame->grabstate == FRAME_DONE)
  3938. || (frame->grabstate == FRAME_ERROR));
  3939. if (rc)
  3940. goto error;
  3941. PDEBUG(4, "Got image, frame->grabstate = %d", frame->grabstate);
  3942. PDEBUG(4, "bytes_recvd = %d", frame->bytes_recvd);
  3943. if (frame->grabstate == FRAME_ERROR) {
  3944. frame->bytes_read = 0;
  3945. err("** ick! ** Errored frame %d", ov->curframe);
  3946. if (ov51x_new_frame(ov, frmx)) {
  3947. err("read: ov51x_new_frame error");
  3948. goto error;
  3949. }
  3950. goto restart;
  3951. }
  3952. /* Repeat until we get a snapshot frame */
  3953. if (ov->snap_enabled)
  3954. PDEBUG(4, "Waiting snapshot frame");
  3955. if (ov->snap_enabled && !frame->snapshot) {
  3956. frame->bytes_read = 0;
  3957. if ((rc = ov51x_new_frame(ov, frmx))) {
  3958. err("read: ov51x_new_frame error");
  3959. goto error;
  3960. }
  3961. goto restart;
  3962. }
  3963. /* Clear the snapshot */
  3964. if (ov->snap_enabled && frame->snapshot) {
  3965. frame->snapshot = 0;
  3966. ov51x_clear_snapshot(ov);
  3967. }
  3968. /* Decompression, format conversion, etc... */
  3969. ov51x_postprocess(ov, frame);
  3970. PDEBUG(4, "frmx=%d, bytes_read=%ld, length=%ld", frmx,
  3971. frame->bytes_read,
  3972. get_frame_length(frame));
  3973. /* copy bytes to user space; we allow for partials reads */
  3974. // if ((count + frame->bytes_read)
  3975. // > get_frame_length((struct ov511_frame *)frame))
  3976. // count = frame->scanlength - frame->bytes_read;
  3977. /* FIXME - count hardwired to be one frame... */
  3978. count = get_frame_length(frame);
  3979. PDEBUG(4, "Copy to user space: %ld bytes", count);
  3980. if ((i = copy_to_user(buf, frame->data + frame->bytes_read, count))) {
  3981. PDEBUG(4, "Copy failed! %d bytes not copied", i);
  3982. rc = -EFAULT;
  3983. goto error;
  3984. }
  3985. frame->bytes_read += count;
  3986. PDEBUG(4, "{copy} count used=%ld, new bytes_read=%ld",
  3987. count, frame->bytes_read);
  3988. /* If all data have been read... */
  3989. if (frame->bytes_read
  3990. >= get_frame_length(frame)) {
  3991. frame->bytes_read = 0;
  3992. // FIXME: Only supports two frames
  3993. /* Mark it as available to be used again. */
  3994. ov->frame[frmx].grabstate = FRAME_UNUSED;
  3995. if ((rc = ov51x_new_frame(ov, !frmx))) {
  3996. err("ov51x_new_frame returned error");
  3997. goto error;
  3998. }
  3999. }
  4000. PDEBUG(4, "read finished, returning %ld (sweet)", count);
  4001. up(&ov->lock);
  4002. return count;
  4003. error:
  4004. up(&ov->lock);
  4005. return rc;
  4006. }
  4007. static int
  4008. ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
  4009. {
  4010. struct video_device *vdev = file->private_data;
  4011. unsigned long start = vma->vm_start;
  4012. unsigned long size = vma->vm_end - vma->vm_start;
  4013. struct usb_ov511 *ov = video_get_drvdata(vdev);
  4014. unsigned long page, pos;
  4015. if (ov->dev == NULL)
  4016. return -EIO;
  4017. PDEBUG(4, "mmap: %ld (%lX) bytes", size, size);
  4018. if (size > (((OV511_NUMFRAMES
  4019. * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight)
  4020. + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
  4021. return -EINVAL;
  4022. if (down_interruptible(&ov->lock))
  4023. return -EINTR;
  4024. pos = (unsigned long)ov->fbuf;
  4025. while (size > 0) {
  4026. page = vmalloc_to_pfn((void *)pos);
  4027. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  4028. up(&ov->lock);
  4029. return -EAGAIN;
  4030. }
  4031. start += PAGE_SIZE;
  4032. pos += PAGE_SIZE;
  4033. if (size > PAGE_SIZE)
  4034. size -= PAGE_SIZE;
  4035. else
  4036. size = 0;
  4037. }
  4038. up(&ov->lock);
  4039. return 0;
  4040. }
  4041. static struct file_operations ov511_fops = {
  4042. .owner = THIS_MODULE,
  4043. .open = ov51x_v4l1_open,
  4044. .release = ov51x_v4l1_close,
  4045. .read = ov51x_v4l1_read,
  4046. .mmap = ov51x_v4l1_mmap,
  4047. .ioctl = ov51x_v4l1_ioctl,
  4048. .llseek = no_llseek,
  4049. };
  4050. static struct video_device vdev_template = {
  4051. .owner = THIS_MODULE,
  4052. .name = "OV511 USB Camera",
  4053. .type = VID_TYPE_CAPTURE,
  4054. .hardware = VID_HARDWARE_OV511,
  4055. .fops = &ov511_fops,
  4056. .release = video_device_release,
  4057. .minor = -1,
  4058. };
  4059. /****************************************************************************
  4060. *
  4061. * OV511 and sensor configuration
  4062. *
  4063. ***************************************************************************/
  4064. /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
  4065. * the same register settings as the OV7610, since they are very similar.
  4066. */
  4067. static int
  4068. ov7xx0_configure(struct usb_ov511 *ov)
  4069. {
  4070. int i, success;
  4071. int rc;
  4072. /* Lawrence Glaister <lg@jfm.bc.ca> reports:
  4073. *
  4074. * Register 0x0f in the 7610 has the following effects:
  4075. *
  4076. * 0x85 (AEC method 1): Best overall, good contrast range
  4077. * 0x45 (AEC method 2): Very overexposed
  4078. * 0xa5 (spec sheet default): Ok, but the black level is
  4079. * shifted resulting in loss of contrast
  4080. * 0x05 (old driver setting): very overexposed, too much
  4081. * contrast
  4082. */
  4083. static struct ov511_regvals aRegvalsNorm7610[] = {
  4084. { OV511_I2C_BUS, 0x10, 0xff },
  4085. { OV511_I2C_BUS, 0x16, 0x06 },
  4086. { OV511_I2C_BUS, 0x28, 0x24 },
  4087. { OV511_I2C_BUS, 0x2b, 0xac },
  4088. { OV511_I2C_BUS, 0x12, 0x00 },
  4089. { OV511_I2C_BUS, 0x38, 0x81 },
  4090. { OV511_I2C_BUS, 0x28, 0x24 }, /* 0c */
  4091. { OV511_I2C_BUS, 0x0f, 0x85 }, /* lg's setting */
  4092. { OV511_I2C_BUS, 0x15, 0x01 },
  4093. { OV511_I2C_BUS, 0x20, 0x1c },
  4094. { OV511_I2C_BUS, 0x23, 0x2a },
  4095. { OV511_I2C_BUS, 0x24, 0x10 },
  4096. { OV511_I2C_BUS, 0x25, 0x8a },
  4097. { OV511_I2C_BUS, 0x26, 0xa2 },
  4098. { OV511_I2C_BUS, 0x27, 0xc2 },
  4099. { OV511_I2C_BUS, 0x2a, 0x04 },
  4100. { OV511_I2C_BUS, 0x2c, 0xfe },
  4101. { OV511_I2C_BUS, 0x2d, 0x93 },
  4102. { OV511_I2C_BUS, 0x30, 0x71 },
  4103. { OV511_I2C_BUS, 0x31, 0x60 },
  4104. { OV511_I2C_BUS, 0x32, 0x26 },
  4105. { OV511_I2C_BUS, 0x33, 0x20 },
  4106. { OV511_I2C_BUS, 0x34, 0x48 },
  4107. { OV511_I2C_BUS, 0x12, 0x24 },
  4108. { OV511_I2C_BUS, 0x11, 0x01 },
  4109. { OV511_I2C_BUS, 0x0c, 0x24 },
  4110. { OV511_I2C_BUS, 0x0d, 0x24 },
  4111. { OV511_DONE_BUS, 0x0, 0x00 },
  4112. };
  4113. static struct ov511_regvals aRegvalsNorm7620[] = {
  4114. { OV511_I2C_BUS, 0x00, 0x00 },
  4115. { OV511_I2C_BUS, 0x01, 0x80 },
  4116. { OV511_I2C_BUS, 0x02, 0x80 },
  4117. { OV511_I2C_BUS, 0x03, 0xc0 },
  4118. { OV511_I2C_BUS, 0x06, 0x60 },
  4119. { OV511_I2C_BUS, 0x07, 0x00 },
  4120. { OV511_I2C_BUS, 0x0c, 0x24 },
  4121. { OV511_I2C_BUS, 0x0c, 0x24 },
  4122. { OV511_I2C_BUS, 0x0d, 0x24 },
  4123. { OV511_I2C_BUS, 0x11, 0x01 },
  4124. { OV511_I2C_BUS, 0x12, 0x24 },
  4125. { OV511_I2C_BUS, 0x13, 0x01 },
  4126. { OV511_I2C_BUS, 0x14, 0x84 },
  4127. { OV511_I2C_BUS, 0x15, 0x01 },
  4128. { OV511_I2C_BUS, 0x16, 0x03 },
  4129. { OV511_I2C_BUS, 0x17, 0x2f },
  4130. { OV511_I2C_BUS, 0x18, 0xcf },
  4131. { OV511_I2C_BUS, 0x19, 0x06 },
  4132. { OV511_I2C_BUS, 0x1a, 0xf5 },
  4133. { OV511_I2C_BUS, 0x1b, 0x00 },
  4134. { OV511_I2C_BUS, 0x20, 0x18 },
  4135. { OV511_I2C_BUS, 0x21, 0x80 },
  4136. { OV511_I2C_BUS, 0x22, 0x80 },
  4137. { OV511_I2C_BUS, 0x23, 0x00 },
  4138. { OV511_I2C_BUS, 0x26, 0xa2 },
  4139. { OV511_I2C_BUS, 0x27, 0xea },
  4140. { OV511_I2C_BUS, 0x28, 0x20 },
  4141. { OV511_I2C_BUS, 0x29, 0x00 },
  4142. { OV511_I2C_BUS, 0x2a, 0x10 },
  4143. { OV511_I2C_BUS, 0x2b, 0x00 },
  4144. { OV511_I2C_BUS, 0x2c, 0x88 },
  4145. { OV511_I2C_BUS, 0x2d, 0x91 },
  4146. { OV511_I2C_BUS, 0x2e, 0x80 },
  4147. { OV511_I2C_BUS, 0x2f, 0x44 },
  4148. { OV511_I2C_BUS, 0x60, 0x27 },
  4149. { OV511_I2C_BUS, 0x61, 0x02 },
  4150. { OV511_I2C_BUS, 0x62, 0x5f },
  4151. { OV511_I2C_BUS, 0x63, 0xd5 },
  4152. { OV511_I2C_BUS, 0x64, 0x57 },
  4153. { OV511_I2C_BUS, 0x65, 0x83 },
  4154. { OV511_I2C_BUS, 0x66, 0x55 },
  4155. { OV511_I2C_BUS, 0x67, 0x92 },
  4156. { OV511_I2C_BUS, 0x68, 0xcf },
  4157. { OV511_I2C_BUS, 0x69, 0x76 },
  4158. { OV511_I2C_BUS, 0x6a, 0x22 },
  4159. { OV511_I2C_BUS, 0x6b, 0x00 },
  4160. { OV511_I2C_BUS, 0x6c, 0x02 },
  4161. { OV511_I2C_BUS, 0x6d, 0x44 },
  4162. { OV511_I2C_BUS, 0x6e, 0x80 },
  4163. { OV511_I2C_BUS, 0x6f, 0x1d },
  4164. { OV511_I2C_BUS, 0x70, 0x8b },
  4165. { OV511_I2C_BUS, 0x71, 0x00 },
  4166. { OV511_I2C_BUS, 0x72, 0x14 },
  4167. { OV511_I2C_BUS, 0x73, 0x54 },
  4168. { OV511_I2C_BUS, 0x74, 0x00 },
  4169. { OV511_I2C_BUS, 0x75, 0x8e },
  4170. { OV511_I2C_BUS, 0x76, 0x00 },
  4171. { OV511_I2C_BUS, 0x77, 0xff },
  4172. { OV511_I2C_BUS, 0x78, 0x80 },
  4173. { OV511_I2C_BUS, 0x79, 0x80 },
  4174. { OV511_I2C_BUS, 0x7a, 0x80 },
  4175. { OV511_I2C_BUS, 0x7b, 0xe2 },
  4176. { OV511_I2C_BUS, 0x7c, 0x00 },
  4177. { OV511_DONE_BUS, 0x0, 0x00 },
  4178. };
  4179. PDEBUG(4, "starting configuration");
  4180. /* This looks redundant, but is necessary for WebCam 3 */
  4181. ov->primary_i2c_slave = OV7xx0_SID;
  4182. if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
  4183. return -1;
  4184. if (init_ov_sensor(ov) >= 0) {
  4185. PDEBUG(1, "OV7xx0 sensor initalized (method 1)");
  4186. } else {
  4187. /* Reset the 76xx */
  4188. if (i2c_w(ov, 0x12, 0x80) < 0)
  4189. return -1;
  4190. /* Wait for it to initialize */
  4191. msleep(150);
  4192. i = 0;
  4193. success = 0;
  4194. while (i <= i2c_detect_tries) {
  4195. if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
  4196. (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
  4197. success = 1;
  4198. break;
  4199. } else {
  4200. i++;
  4201. }
  4202. }
  4203. // Was (i == i2c_detect_tries) previously. This obviously used to always report
  4204. // success. Whether anyone actually depended on that bug is unknown
  4205. if ((i >= i2c_detect_tries) && (success == 0)) {
  4206. err("Failed to read sensor ID. You might not have an");
  4207. err("OV7610/20, or it may be not responding. Report");
  4208. err("this to " EMAIL);
  4209. err("This is only a warning. You can attempt to use");
  4210. err("your camera anyway");
  4211. // Only issue a warning for now
  4212. // return -1;
  4213. } else {
  4214. PDEBUG(1, "OV7xx0 initialized (method 2, %dx)", i+1);
  4215. }
  4216. }
  4217. /* Detect sensor (sub)type */
  4218. rc = i2c_r(ov, OV7610_REG_COM_I);
  4219. if (rc < 0) {
  4220. err("Error detecting sensor type");
  4221. return -1;
  4222. } else if ((rc & 3) == 3) {
  4223. info("Sensor is an OV7610");
  4224. ov->sensor = SEN_OV7610;
  4225. } else if ((rc & 3) == 1) {
  4226. /* I don't know what's different about the 76BE yet. */
  4227. if (i2c_r(ov, 0x15) & 1)
  4228. info("Sensor is an OV7620AE");
  4229. else
  4230. info("Sensor is an OV76BE");
  4231. /* OV511+ will return all zero isoc data unless we
  4232. * configure the sensor as a 7620. Someone needs to
  4233. * find the exact reg. setting that causes this. */
  4234. if (ov->bridge == BRG_OV511PLUS) {
  4235. info("Enabling 511+/7620AE workaround");
  4236. ov->sensor = SEN_OV7620;
  4237. } else {
  4238. ov->sensor = SEN_OV76BE;
  4239. }
  4240. } else if ((rc & 3) == 0) {
  4241. info("Sensor is an OV7620");
  4242. ov->sensor = SEN_OV7620;
  4243. } else {
  4244. err("Unknown image sensor version: %d", rc & 3);
  4245. return -1;
  4246. }
  4247. if (ov->sensor == SEN_OV7620) {
  4248. PDEBUG(4, "Writing 7620 registers");
  4249. if (write_regvals(ov, aRegvalsNorm7620))
  4250. return -1;
  4251. } else {
  4252. PDEBUG(4, "Writing 7610 registers");
  4253. if (write_regvals(ov, aRegvalsNorm7610))
  4254. return -1;
  4255. }
  4256. /* Set sensor-specific vars */
  4257. ov->maxwidth = 640;
  4258. ov->maxheight = 480;
  4259. ov->minwidth = 64;
  4260. ov->minheight = 48;
  4261. // FIXME: These do not match the actual settings yet
  4262. ov->brightness = 0x80 << 8;
  4263. ov->contrast = 0x80 << 8;
  4264. ov->colour = 0x80 << 8;
  4265. ov->hue = 0x80 << 8;
  4266. return 0;
  4267. }
  4268. /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
  4269. static int
  4270. ov6xx0_configure(struct usb_ov511 *ov)
  4271. {
  4272. int rc;
  4273. static struct ov511_regvals aRegvalsNorm6x20[] = {
  4274. { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
  4275. { OV511_I2C_BUS, 0x11, 0x01 },
  4276. { OV511_I2C_BUS, 0x03, 0x60 },
  4277. { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
  4278. { OV511_I2C_BUS, 0x07, 0xa8 },
  4279. /* The ratio of 0x0c and 0x0d controls the white point */
  4280. { OV511_I2C_BUS, 0x0c, 0x24 },
  4281. { OV511_I2C_BUS, 0x0d, 0x24 },
  4282. { OV511_I2C_BUS, 0x0f, 0x15 }, /* COMS */
  4283. { OV511_I2C_BUS, 0x10, 0x75 }, /* AEC Exposure time */
  4284. { OV511_I2C_BUS, 0x12, 0x24 }, /* Enable AGC */
  4285. { OV511_I2C_BUS, 0x14, 0x04 },
  4286. /* 0x16: 0x06 helps frame stability with moving objects */
  4287. { OV511_I2C_BUS, 0x16, 0x06 },
  4288. // { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
  4289. { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
  4290. /* 0x28: 0x05 Selects RGB format if RGB on */
  4291. { OV511_I2C_BUS, 0x28, 0x05 },
  4292. { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
  4293. // { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
  4294. { OV511_I2C_BUS, 0x2d, 0x99 },
  4295. { OV511_I2C_BUS, 0x33, 0xa0 }, /* Color Processing Parameter */
  4296. { OV511_I2C_BUS, 0x34, 0xd2 }, /* Max A/D range */
  4297. { OV511_I2C_BUS, 0x38, 0x8b },
  4298. { OV511_I2C_BUS, 0x39, 0x40 },
  4299. { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
  4300. { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
  4301. { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
  4302. { OV511_I2C_BUS, 0x3d, 0x80 },
  4303. /* These next two registers (0x4a, 0x4b) are undocumented. They
  4304. * control the color balance */
  4305. { OV511_I2C_BUS, 0x4a, 0x80 },
  4306. { OV511_I2C_BUS, 0x4b, 0x80 },
  4307. { OV511_I2C_BUS, 0x4d, 0xd2 }, /* This reduces noise a bit */
  4308. { OV511_I2C_BUS, 0x4e, 0xc1 },
  4309. { OV511_I2C_BUS, 0x4f, 0x04 },
  4310. // Do 50-53 have any effect?
  4311. // Toggle 0x12[2] off and on here?
  4312. { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
  4313. };
  4314. static struct ov511_regvals aRegvalsNorm6x30[] = {
  4315. /*OK*/ { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
  4316. { OV511_I2C_BUS, 0x11, 0x00 },
  4317. /*OK*/ { OV511_I2C_BUS, 0x03, 0x60 },
  4318. /*0A?*/ { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
  4319. { OV511_I2C_BUS, 0x07, 0xa8 },
  4320. /* The ratio of 0x0c and 0x0d controls the white point */
  4321. /*OK*/ { OV511_I2C_BUS, 0x0c, 0x24 },
  4322. /*OK*/ { OV511_I2C_BUS, 0x0d, 0x24 },
  4323. /*A*/ { OV511_I2C_BUS, 0x0e, 0x20 },
  4324. // /*04?*/ { OV511_I2C_BUS, 0x14, 0x80 },
  4325. { OV511_I2C_BUS, 0x16, 0x03 },
  4326. // /*OK*/ { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
  4327. // 21 & 22? The suggested values look wrong. Go with default
  4328. /*A*/ { OV511_I2C_BUS, 0x23, 0xc0 },
  4329. /*A*/ { OV511_I2C_BUS, 0x25, 0x9a }, // Check this against default
  4330. // /*OK*/ { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
  4331. /* 0x28: 0x05 Selects RGB format if RGB on */
  4332. // /*04?*/ { OV511_I2C_BUS, 0x28, 0x05 },
  4333. // /*04?*/ { OV511_I2C_BUS, 0x28, 0x45 }, // DEBUG: Tristate UV bus
  4334. /*OK*/ { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
  4335. // /*OK*/ { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
  4336. { OV511_I2C_BUS, 0x2d, 0x99 },
  4337. // /*A*/ { OV511_I2C_BUS, 0x33, 0x26 }, // Reserved bits on 6620
  4338. // /*d2?*/ { OV511_I2C_BUS, 0x34, 0x03 }, /* Max A/D range */
  4339. // /*8b?*/ { OV511_I2C_BUS, 0x38, 0x83 },
  4340. // /*40?*/ { OV511_I2C_BUS, 0x39, 0xc0 }, // 6630 adds bit 7
  4341. // { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
  4342. // { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
  4343. // { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
  4344. { OV511_I2C_BUS, 0x3d, 0x80 },
  4345. // /*A*/ { OV511_I2C_BUS, 0x3f, 0x0e },
  4346. /* These next two registers (0x4a, 0x4b) are undocumented. They
  4347. * control the color balance */
  4348. // /*OK?*/ { OV511_I2C_BUS, 0x4a, 0x80 }, // Check these
  4349. // /*OK?*/ { OV511_I2C_BUS, 0x4b, 0x80 },
  4350. { OV511_I2C_BUS, 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
  4351. /*c1?*/ { OV511_I2C_BUS, 0x4e, 0x40 },
  4352. /* UV average mode, color killer: strongest */
  4353. { OV511_I2C_BUS, 0x4f, 0x07 },
  4354. { OV511_I2C_BUS, 0x54, 0x23 }, /* Max AGC gain: 18dB */
  4355. { OV511_I2C_BUS, 0x57, 0x81 }, /* (default) */
  4356. { OV511_I2C_BUS, 0x59, 0x01 }, /* AGC dark current comp: +1 */
  4357. { OV511_I2C_BUS, 0x5a, 0x2c }, /* (undocumented) */
  4358. { OV511_I2C_BUS, 0x5b, 0x0f }, /* AWB chrominance levels */
  4359. // { OV511_I2C_BUS, 0x5c, 0x10 },
  4360. { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
  4361. };
  4362. PDEBUG(4, "starting sensor configuration");
  4363. if (init_ov_sensor(ov) < 0) {
  4364. err("Failed to read sensor ID. You might not have an OV6xx0,");
  4365. err("or it may be not responding. Report this to " EMAIL);
  4366. return -1;
  4367. } else {
  4368. PDEBUG(1, "OV6xx0 sensor detected");
  4369. }
  4370. /* Detect sensor (sub)type */
  4371. rc = i2c_r(ov, OV7610_REG_COM_I);
  4372. if (rc < 0) {
  4373. err("Error detecting sensor type");
  4374. return -1;
  4375. }
  4376. if ((rc & 3) == 0) {
  4377. ov->sensor = SEN_OV6630;
  4378. info("Sensor is an OV6630");
  4379. } else if ((rc & 3) == 1) {
  4380. ov->sensor = SEN_OV6620;
  4381. info("Sensor is an OV6620");
  4382. } else if ((rc & 3) == 2) {
  4383. ov->sensor = SEN_OV6630;
  4384. info("Sensor is an OV6630AE");
  4385. } else if ((rc & 3) == 3) {
  4386. ov->sensor = SEN_OV6630;
  4387. info("Sensor is an OV6630AF");
  4388. }
  4389. /* Set sensor-specific vars */
  4390. ov->maxwidth = 352;
  4391. ov->maxheight = 288;
  4392. ov->minwidth = 64;
  4393. ov->minheight = 48;
  4394. // FIXME: These do not match the actual settings yet
  4395. ov->brightness = 0x80 << 8;
  4396. ov->contrast = 0x80 << 8;
  4397. ov->colour = 0x80 << 8;
  4398. ov->hue = 0x80 << 8;
  4399. if (ov->sensor == SEN_OV6620) {
  4400. PDEBUG(4, "Writing 6x20 registers");
  4401. if (write_regvals(ov, aRegvalsNorm6x20))
  4402. return -1;
  4403. } else {
  4404. PDEBUG(4, "Writing 6x30 registers");
  4405. if (write_regvals(ov, aRegvalsNorm6x30))
  4406. return -1;
  4407. }
  4408. return 0;
  4409. }
  4410. /* This initializes the KS0127 and KS0127B video decoders. */
  4411. static int
  4412. ks0127_configure(struct usb_ov511 *ov)
  4413. {
  4414. int rc;
  4415. // FIXME: I don't know how to sync or reset it yet
  4416. #if 0
  4417. if (ov51x_init_ks_sensor(ov) < 0) {
  4418. err("Failed to initialize the KS0127");
  4419. return -1;
  4420. } else {
  4421. PDEBUG(1, "KS012x(B) sensor detected");
  4422. }
  4423. #endif
  4424. /* Detect decoder subtype */
  4425. rc = i2c_r(ov, 0x00);
  4426. if (rc < 0) {
  4427. err("Error detecting sensor type");
  4428. return -1;
  4429. } else if (rc & 0x08) {
  4430. rc = i2c_r(ov, 0x3d);
  4431. if (rc < 0) {
  4432. err("Error detecting sensor type");
  4433. return -1;
  4434. } else if ((rc & 0x0f) == 0) {
  4435. info("Sensor is a KS0127");
  4436. ov->sensor = SEN_KS0127;
  4437. } else if ((rc & 0x0f) == 9) {
  4438. info("Sensor is a KS0127B Rev. A");
  4439. ov->sensor = SEN_KS0127B;
  4440. }
  4441. } else {
  4442. err("Error: Sensor is an unsupported KS0122");
  4443. return -1;
  4444. }
  4445. /* Set sensor-specific vars */
  4446. ov->maxwidth = 640;
  4447. ov->maxheight = 480;
  4448. ov->minwidth = 64;
  4449. ov->minheight = 48;
  4450. // FIXME: These do not match the actual settings yet
  4451. ov->brightness = 0x80 << 8;
  4452. ov->contrast = 0x80 << 8;
  4453. ov->colour = 0x80 << 8;
  4454. ov->hue = 0x80 << 8;
  4455. /* This device is not supported yet. Bail out now... */
  4456. err("This sensor is not supported yet.");
  4457. return -1;
  4458. return 0;
  4459. }
  4460. /* This initializes the SAA7111A video decoder. */
  4461. static int
  4462. saa7111a_configure(struct usb_ov511 *ov)
  4463. {
  4464. int rc;
  4465. /* Since there is no register reset command, all registers must be
  4466. * written, otherwise gives erratic results */
  4467. static struct ov511_regvals aRegvalsNormSAA7111A[] = {
  4468. { OV511_I2C_BUS, 0x06, 0xce },
  4469. { OV511_I2C_BUS, 0x07, 0x00 },
  4470. { OV511_I2C_BUS, 0x10, 0x44 }, /* YUV422, 240/286 lines */
  4471. { OV511_I2C_BUS, 0x0e, 0x01 }, /* NTSC M or PAL BGHI */
  4472. { OV511_I2C_BUS, 0x00, 0x00 },
  4473. { OV511_I2C_BUS, 0x01, 0x00 },
  4474. { OV511_I2C_BUS, 0x03, 0x23 },
  4475. { OV511_I2C_BUS, 0x04, 0x00 },
  4476. { OV511_I2C_BUS, 0x05, 0x00 },
  4477. { OV511_I2C_BUS, 0x08, 0xc8 }, /* Auto field freq */
  4478. { OV511_I2C_BUS, 0x09, 0x01 }, /* Chrom. trap off, APER=0.25 */
  4479. { OV511_I2C_BUS, 0x0a, 0x80 }, /* BRIG=128 */
  4480. { OV511_I2C_BUS, 0x0b, 0x40 }, /* CONT=1.0 */
  4481. { OV511_I2C_BUS, 0x0c, 0x40 }, /* SATN=1.0 */
  4482. { OV511_I2C_BUS, 0x0d, 0x00 }, /* HUE=0 */
  4483. { OV511_I2C_BUS, 0x0f, 0x00 },
  4484. { OV511_I2C_BUS, 0x11, 0x0c },
  4485. { OV511_I2C_BUS, 0x12, 0x00 },
  4486. { OV511_I2C_BUS, 0x13, 0x00 },
  4487. { OV511_I2C_BUS, 0x14, 0x00 },
  4488. { OV511_I2C_BUS, 0x15, 0x00 },
  4489. { OV511_I2C_BUS, 0x16, 0x00 },
  4490. { OV511_I2C_BUS, 0x17, 0x00 },
  4491. { OV511_I2C_BUS, 0x02, 0xc0 }, /* Composite input 0 */
  4492. { OV511_DONE_BUS, 0x0, 0x00 },
  4493. };
  4494. // FIXME: I don't know how to sync or reset it yet
  4495. #if 0
  4496. if (ov51x_init_saa_sensor(ov) < 0) {
  4497. err("Failed to initialize the SAA7111A");
  4498. return -1;
  4499. } else {
  4500. PDEBUG(1, "SAA7111A sensor detected");
  4501. }
  4502. #endif
  4503. /* 640x480 not supported with PAL */
  4504. if (ov->pal) {
  4505. ov->maxwidth = 320;
  4506. ov->maxheight = 240; /* Even field only */
  4507. } else {
  4508. ov->maxwidth = 640;
  4509. ov->maxheight = 480; /* Even/Odd fields */
  4510. }
  4511. ov->minwidth = 320;
  4512. ov->minheight = 240; /* Even field only */
  4513. ov->has_decoder = 1;
  4514. ov->num_inputs = 8;
  4515. ov->norm = VIDEO_MODE_AUTO;
  4516. ov->stop_during_set = 0; /* Decoder guarantees stable image */
  4517. /* Decoder doesn't change these values, so we use these instead of
  4518. * acutally reading the registers (which doesn't work) */
  4519. ov->brightness = 0x80 << 8;
  4520. ov->contrast = 0x40 << 9;
  4521. ov->colour = 0x40 << 9;
  4522. ov->hue = 32768;
  4523. PDEBUG(4, "Writing SAA7111A registers");
  4524. if (write_regvals(ov, aRegvalsNormSAA7111A))
  4525. return -1;
  4526. /* Detect version of decoder. This must be done after writing the
  4527. * initial regs or the decoder will lock up. */
  4528. rc = i2c_r(ov, 0x00);
  4529. if (rc < 0) {
  4530. err("Error detecting sensor version");
  4531. return -1;
  4532. } else {
  4533. info("Sensor is an SAA7111A (version 0x%x)", rc);
  4534. ov->sensor = SEN_SAA7111A;
  4535. }
  4536. // FIXME: Fix this for OV518(+)
  4537. /* Latch to negative edge of clock. Otherwise, we get incorrect
  4538. * colors and jitter in the digital signal. */
  4539. if (ov->bclass == BCL_OV511)
  4540. reg_w(ov, 0x11, 0x00);
  4541. else
  4542. warn("SAA7111A not yet supported with OV518/OV518+");
  4543. return 0;
  4544. }
  4545. /* This initializes the OV511/OV511+ and the sensor */
  4546. static int
  4547. ov511_configure(struct usb_ov511 *ov)
  4548. {
  4549. static struct ov511_regvals aRegvalsInit511[] = {
  4550. { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
  4551. { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
  4552. { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
  4553. { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
  4554. { OV511_REG_BUS, R51x_SYS_RESET, 0x3f },
  4555. { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
  4556. { OV511_REG_BUS, R51x_SYS_RESET, 0x3d },
  4557. { OV511_DONE_BUS, 0x0, 0x00},
  4558. };
  4559. static struct ov511_regvals aRegvalsNorm511[] = {
  4560. { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0x01 },
  4561. { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
  4562. { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
  4563. { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
  4564. { OV511_REG_BUS, R511_FIFO_OPTS, 0x1f },
  4565. { OV511_REG_BUS, R511_COMP_EN, 0x00 },
  4566. { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
  4567. { OV511_DONE_BUS, 0x0, 0x00 },
  4568. };
  4569. static struct ov511_regvals aRegvalsNorm511Plus[] = {
  4570. { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0xff },
  4571. { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
  4572. { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
  4573. { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
  4574. { OV511_REG_BUS, R511_FIFO_OPTS, 0xff },
  4575. { OV511_REG_BUS, R511_COMP_EN, 0x00 },
  4576. { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
  4577. { OV511_DONE_BUS, 0x0, 0x00 },
  4578. };
  4579. PDEBUG(4, "");
  4580. ov->customid = reg_r(ov, R511_SYS_CUST_ID);
  4581. if (ov->customid < 0) {
  4582. err("Unable to read camera bridge registers");
  4583. goto error;
  4584. }
  4585. PDEBUG (1, "CustomID = %d", ov->customid);
  4586. ov->desc = symbolic(camlist, ov->customid);
  4587. info("model: %s", ov->desc);
  4588. if (0 == strcmp(ov->desc, NOT_DEFINED_STR)) {
  4589. err("Camera type (%d) not recognized", ov->customid);
  4590. err("Please notify " EMAIL " of the name,");
  4591. err("manufacturer, model, and this number of your camera.");
  4592. err("Also include the output of the detection process.");
  4593. }
  4594. if (ov->customid == 70) /* USB Life TV (PAL/SECAM) */
  4595. ov->pal = 1;
  4596. if (write_regvals(ov, aRegvalsInit511))
  4597. goto error;
  4598. if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
  4599. ov51x_led_control(ov, 0);
  4600. /* The OV511+ has undocumented bits in the flow control register.
  4601. * Setting it to 0xff fixes the corruption with moving objects. */
  4602. if (ov->bridge == BRG_OV511) {
  4603. if (write_regvals(ov, aRegvalsNorm511))
  4604. goto error;
  4605. } else if (ov->bridge == BRG_OV511PLUS) {
  4606. if (write_regvals(ov, aRegvalsNorm511Plus))
  4607. goto error;
  4608. } else {
  4609. err("Invalid bridge");
  4610. }
  4611. if (ov511_init_compression(ov))
  4612. goto error;
  4613. ov->packet_numbering = 1;
  4614. ov511_set_packet_size(ov, 0);
  4615. ov->snap_enabled = snapshot;
  4616. /* Test for 7xx0 */
  4617. PDEBUG(3, "Testing for 0V7xx0");
  4618. ov->primary_i2c_slave = OV7xx0_SID;
  4619. if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
  4620. goto error;
  4621. if (i2c_w(ov, 0x12, 0x80) < 0) {
  4622. /* Test for 6xx0 */
  4623. PDEBUG(3, "Testing for 0V6xx0");
  4624. ov->primary_i2c_slave = OV6xx0_SID;
  4625. if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
  4626. goto error;
  4627. if (i2c_w(ov, 0x12, 0x80) < 0) {
  4628. /* Test for 8xx0 */
  4629. PDEBUG(3, "Testing for 0V8xx0");
  4630. ov->primary_i2c_slave = OV8xx0_SID;
  4631. if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
  4632. goto error;
  4633. if (i2c_w(ov, 0x12, 0x80) < 0) {
  4634. /* Test for SAA7111A */
  4635. PDEBUG(3, "Testing for SAA7111A");
  4636. ov->primary_i2c_slave = SAA7111A_SID;
  4637. if (ov51x_set_slave_ids(ov, SAA7111A_SID) < 0)
  4638. goto error;
  4639. if (i2c_w(ov, 0x0d, 0x00) < 0) {
  4640. /* Test for KS0127 */
  4641. PDEBUG(3, "Testing for KS0127");
  4642. ov->primary_i2c_slave = KS0127_SID;
  4643. if (ov51x_set_slave_ids(ov, KS0127_SID) < 0)
  4644. goto error;
  4645. if (i2c_w(ov, 0x10, 0x00) < 0) {
  4646. err("Can't determine sensor slave IDs");
  4647. goto error;
  4648. } else {
  4649. if (ks0127_configure(ov) < 0) {
  4650. err("Failed to configure KS0127");
  4651. goto error;
  4652. }
  4653. }
  4654. } else {
  4655. if (saa7111a_configure(ov) < 0) {
  4656. err("Failed to configure SAA7111A");
  4657. goto error;
  4658. }
  4659. }
  4660. } else {
  4661. err("Detected unsupported OV8xx0 sensor");
  4662. goto error;
  4663. }
  4664. } else {
  4665. if (ov6xx0_configure(ov) < 0) {
  4666. err("Failed to configure OV6xx0");
  4667. goto error;
  4668. }
  4669. }
  4670. } else {
  4671. if (ov7xx0_configure(ov) < 0) {
  4672. err("Failed to configure OV7xx0");
  4673. goto error;
  4674. }
  4675. }
  4676. return 0;
  4677. error:
  4678. err("OV511 Config failed");
  4679. return -EBUSY;
  4680. }
  4681. /* This initializes the OV518/OV518+ and the sensor */
  4682. static int
  4683. ov518_configure(struct usb_ov511 *ov)
  4684. {
  4685. /* For 518 and 518+ */
  4686. static struct ov511_regvals aRegvalsInit518[] = {
  4687. { OV511_REG_BUS, R51x_SYS_RESET, 0x40 },
  4688. { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
  4689. { OV511_REG_BUS, R51x_SYS_RESET, 0x3e },
  4690. { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
  4691. { OV511_REG_BUS, R51x_SYS_RESET, 0x00 },
  4692. { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
  4693. { OV511_REG_BUS, 0x46, 0x00 },
  4694. { OV511_REG_BUS, 0x5d, 0x03 },
  4695. { OV511_DONE_BUS, 0x0, 0x00},
  4696. };
  4697. static struct ov511_regvals aRegvalsNorm518[] = {
  4698. { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
  4699. { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
  4700. { OV511_REG_BUS, 0x31, 0x0f },
  4701. { OV511_REG_BUS, 0x5d, 0x03 },
  4702. { OV511_REG_BUS, 0x24, 0x9f },
  4703. { OV511_REG_BUS, 0x25, 0x90 },
  4704. { OV511_REG_BUS, 0x20, 0x00 },
  4705. { OV511_REG_BUS, 0x51, 0x04 },
  4706. { OV511_REG_BUS, 0x71, 0x19 },
  4707. { OV511_DONE_BUS, 0x0, 0x00 },
  4708. };
  4709. static struct ov511_regvals aRegvalsNorm518Plus[] = {
  4710. { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
  4711. { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
  4712. { OV511_REG_BUS, 0x31, 0x0f },
  4713. { OV511_REG_BUS, 0x5d, 0x03 },
  4714. { OV511_REG_BUS, 0x24, 0x9f },
  4715. { OV511_REG_BUS, 0x25, 0x90 },
  4716. { OV511_REG_BUS, 0x20, 0x60 },
  4717. { OV511_REG_BUS, 0x51, 0x02 },
  4718. { OV511_REG_BUS, 0x71, 0x19 },
  4719. { OV511_REG_BUS, 0x40, 0xff },
  4720. { OV511_REG_BUS, 0x41, 0x42 },
  4721. { OV511_REG_BUS, 0x46, 0x00 },
  4722. { OV511_REG_BUS, 0x33, 0x04 },
  4723. { OV511_REG_BUS, 0x21, 0x19 },
  4724. { OV511_REG_BUS, 0x3f, 0x10 },
  4725. { OV511_DONE_BUS, 0x0, 0x00 },
  4726. };
  4727. PDEBUG(4, "");
  4728. /* First 5 bits of custom ID reg are a revision ID on OV518 */
  4729. info("Device revision %d", 0x1F & reg_r(ov, R511_SYS_CUST_ID));
  4730. /* Give it the default description */
  4731. ov->desc = symbolic(camlist, 0);
  4732. if (write_regvals(ov, aRegvalsInit518))
  4733. goto error;
  4734. /* Set LED GPIO pin to output mode */
  4735. if (reg_w_mask(ov, 0x57, 0x00, 0x02) < 0)
  4736. goto error;
  4737. /* LED is off by default with OV518; have to explicitly turn it on */
  4738. if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
  4739. ov51x_led_control(ov, 0);
  4740. else
  4741. ov51x_led_control(ov, 1);
  4742. /* Don't require compression if dumppix is enabled; otherwise it's
  4743. * required. OV518 has no uncompressed mode, to save RAM. */
  4744. if (!dumppix && !ov->compress) {
  4745. ov->compress = 1;
  4746. warn("Compression required with OV518...enabling");
  4747. }
  4748. if (ov->bridge == BRG_OV518) {
  4749. if (write_regvals(ov, aRegvalsNorm518))
  4750. goto error;
  4751. } else if (ov->bridge == BRG_OV518PLUS) {
  4752. if (write_regvals(ov, aRegvalsNorm518Plus))
  4753. goto error;
  4754. } else {
  4755. err("Invalid bridge");
  4756. }
  4757. if (reg_w(ov, 0x2f, 0x80) < 0)
  4758. goto error;
  4759. if (ov518_init_compression(ov))
  4760. goto error;
  4761. if (ov->bridge == BRG_OV518)
  4762. {
  4763. struct usb_interface *ifp;
  4764. struct usb_host_interface *alt;
  4765. __u16 mxps = 0;
  4766. ifp = usb_ifnum_to_if(ov->dev, 0);
  4767. if (ifp) {
  4768. alt = usb_altnum_to_altsetting(ifp, 7);
  4769. if (alt)
  4770. mxps = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
  4771. }
  4772. /* Some OV518s have packet numbering by default, some don't */
  4773. if (mxps == 897)
  4774. ov->packet_numbering = 1;
  4775. else
  4776. ov->packet_numbering = 0;
  4777. } else {
  4778. /* OV518+ has packet numbering turned on by default */
  4779. ov->packet_numbering = 1;
  4780. }
  4781. ov518_set_packet_size(ov, 0);
  4782. ov->snap_enabled = snapshot;
  4783. /* Test for 76xx */
  4784. ov->primary_i2c_slave = OV7xx0_SID;
  4785. if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
  4786. goto error;
  4787. /* The OV518 must be more aggressive about sensor detection since
  4788. * I2C write will never fail if the sensor is not present. We have
  4789. * to try to initialize the sensor to detect its presence */
  4790. if (init_ov_sensor(ov) < 0) {
  4791. /* Test for 6xx0 */
  4792. ov->primary_i2c_slave = OV6xx0_SID;
  4793. if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
  4794. goto error;
  4795. if (init_ov_sensor(ov) < 0) {
  4796. /* Test for 8xx0 */
  4797. ov->primary_i2c_slave = OV8xx0_SID;
  4798. if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
  4799. goto error;
  4800. if (init_ov_sensor(ov) < 0) {
  4801. err("Can't determine sensor slave IDs");
  4802. goto error;
  4803. } else {
  4804. err("Detected unsupported OV8xx0 sensor");
  4805. goto error;
  4806. }
  4807. } else {
  4808. if (ov6xx0_configure(ov) < 0) {
  4809. err("Failed to configure OV6xx0");
  4810. goto error;
  4811. }
  4812. }
  4813. } else {
  4814. if (ov7xx0_configure(ov) < 0) {
  4815. err("Failed to configure OV7xx0");
  4816. goto error;
  4817. }
  4818. }
  4819. ov->maxwidth = 352;
  4820. ov->maxheight = 288;
  4821. // The OV518 cannot go as low as the sensor can
  4822. ov->minwidth = 160;
  4823. ov->minheight = 120;
  4824. return 0;
  4825. error:
  4826. err("OV518 Config failed");
  4827. return -EBUSY;
  4828. }
  4829. /****************************************************************************
  4830. * sysfs
  4831. ***************************************************************************/
  4832. static inline struct usb_ov511 *cd_to_ov(struct class_device *cd)
  4833. {
  4834. struct video_device *vdev = to_video_device(cd);
  4835. return video_get_drvdata(vdev);
  4836. }
  4837. static ssize_t show_custom_id(struct class_device *cd, char *buf)
  4838. {
  4839. struct usb_ov511 *ov = cd_to_ov(cd);
  4840. return sprintf(buf, "%d\n", ov->customid);
  4841. }
  4842. static CLASS_DEVICE_ATTR(custom_id, S_IRUGO, show_custom_id, NULL);
  4843. static ssize_t show_model(struct class_device *cd, char *buf)
  4844. {
  4845. struct usb_ov511 *ov = cd_to_ov(cd);
  4846. return sprintf(buf, "%s\n", ov->desc);
  4847. }
  4848. static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  4849. static ssize_t show_bridge(struct class_device *cd, char *buf)
  4850. {
  4851. struct usb_ov511 *ov = cd_to_ov(cd);
  4852. return sprintf(buf, "%s\n", symbolic(brglist, ov->bridge));
  4853. }
  4854. static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_bridge, NULL);
  4855. static ssize_t show_sensor(struct class_device *cd, char *buf)
  4856. {
  4857. struct usb_ov511 *ov = cd_to_ov(cd);
  4858. return sprintf(buf, "%s\n", symbolic(senlist, ov->sensor));
  4859. }
  4860. static CLASS_DEVICE_ATTR(sensor, S_IRUGO, show_sensor, NULL);
  4861. static ssize_t show_brightness(struct class_device *cd, char *buf)
  4862. {
  4863. struct usb_ov511 *ov = cd_to_ov(cd);
  4864. unsigned short x;
  4865. if (!ov->dev)
  4866. return -ENODEV;
  4867. sensor_get_brightness(ov, &x);
  4868. return sprintf(buf, "%d\n", x >> 8);
  4869. }
  4870. static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
  4871. static ssize_t show_saturation(struct class_device *cd, char *buf)
  4872. {
  4873. struct usb_ov511 *ov = cd_to_ov(cd);
  4874. unsigned short x;
  4875. if (!ov->dev)
  4876. return -ENODEV;
  4877. sensor_get_saturation(ov, &x);
  4878. return sprintf(buf, "%d\n", x >> 8);
  4879. }
  4880. static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
  4881. static ssize_t show_contrast(struct class_device *cd, char *buf)
  4882. {
  4883. struct usb_ov511 *ov = cd_to_ov(cd);
  4884. unsigned short x;
  4885. if (!ov->dev)
  4886. return -ENODEV;
  4887. sensor_get_contrast(ov, &x);
  4888. return sprintf(buf, "%d\n", x >> 8);
  4889. }
  4890. static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
  4891. static ssize_t show_hue(struct class_device *cd, char *buf)
  4892. {
  4893. struct usb_ov511 *ov = cd_to_ov(cd);
  4894. unsigned short x;
  4895. if (!ov->dev)
  4896. return -ENODEV;
  4897. sensor_get_hue(ov, &x);
  4898. return sprintf(buf, "%d\n", x >> 8);
  4899. }
  4900. static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
  4901. static ssize_t show_exposure(struct class_device *cd, char *buf)
  4902. {
  4903. struct usb_ov511 *ov = cd_to_ov(cd);
  4904. unsigned char exp;
  4905. if (!ov->dev)
  4906. return -ENODEV;
  4907. sensor_get_exposure(ov, &exp);
  4908. return sprintf(buf, "%d\n", exp >> 8);
  4909. }
  4910. static CLASS_DEVICE_ATTR(exposure, S_IRUGO, show_exposure, NULL);
  4911. static void ov_create_sysfs(struct video_device *vdev)
  4912. {
  4913. video_device_create_file(vdev, &class_device_attr_custom_id);
  4914. video_device_create_file(vdev, &class_device_attr_model);
  4915. video_device_create_file(vdev, &class_device_attr_bridge);
  4916. video_device_create_file(vdev, &class_device_attr_sensor);
  4917. video_device_create_file(vdev, &class_device_attr_brightness);
  4918. video_device_create_file(vdev, &class_device_attr_saturation);
  4919. video_device_create_file(vdev, &class_device_attr_contrast);
  4920. video_device_create_file(vdev, &class_device_attr_hue);
  4921. video_device_create_file(vdev, &class_device_attr_exposure);
  4922. }
  4923. /****************************************************************************
  4924. * USB routines
  4925. ***************************************************************************/
  4926. static int
  4927. ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  4928. {
  4929. struct usb_device *dev = interface_to_usbdev(intf);
  4930. struct usb_interface_descriptor *idesc;
  4931. struct usb_ov511 *ov;
  4932. int i;
  4933. PDEBUG(1, "probing for device...");
  4934. /* We don't handle multi-config cameras */
  4935. if (dev->descriptor.bNumConfigurations != 1)
  4936. return -ENODEV;
  4937. idesc = &intf->cur_altsetting->desc;
  4938. if (idesc->bInterfaceClass != 0xFF)
  4939. return -ENODEV;
  4940. if (idesc->bInterfaceSubClass != 0x00)
  4941. return -ENODEV;
  4942. if ((ov = kmalloc(sizeof(*ov), GFP_KERNEL)) == NULL) {
  4943. err("couldn't kmalloc ov struct");
  4944. goto error_out;
  4945. }
  4946. memset(ov, 0, sizeof(*ov));
  4947. ov->dev = dev;
  4948. ov->iface = idesc->bInterfaceNumber;
  4949. ov->led_policy = led;
  4950. ov->compress = compress;
  4951. ov->lightfreq = lightfreq;
  4952. ov->num_inputs = 1; /* Video decoder init functs. change this */
  4953. ov->stop_during_set = !fastset;
  4954. ov->backlight = backlight;
  4955. ov->mirror = mirror;
  4956. ov->auto_brt = autobright;
  4957. ov->auto_gain = autogain;
  4958. ov->auto_exp = autoexp;
  4959. switch (le16_to_cpu(dev->descriptor.idProduct)) {
  4960. case PROD_OV511:
  4961. ov->bridge = BRG_OV511;
  4962. ov->bclass = BCL_OV511;
  4963. break;
  4964. case PROD_OV511PLUS:
  4965. ov->bridge = BRG_OV511PLUS;
  4966. ov->bclass = BCL_OV511;
  4967. break;
  4968. case PROD_OV518:
  4969. ov->bridge = BRG_OV518;
  4970. ov->bclass = BCL_OV518;
  4971. break;
  4972. case PROD_OV518PLUS:
  4973. ov->bridge = BRG_OV518PLUS;
  4974. ov->bclass = BCL_OV518;
  4975. break;
  4976. case PROD_ME2CAM:
  4977. if (le16_to_cpu(dev->descriptor.idVendor) != VEND_MATTEL)
  4978. goto error;
  4979. ov->bridge = BRG_OV511PLUS;
  4980. ov->bclass = BCL_OV511;
  4981. break;
  4982. default:
  4983. err("Unknown product ID 0x%04x", le16_to_cpu(dev->descriptor.idProduct));
  4984. goto error;
  4985. }
  4986. info("USB %s video device found", symbolic(brglist, ov->bridge));
  4987. init_waitqueue_head(&ov->wq);
  4988. init_MUTEX(&ov->lock); /* to 1 == available */
  4989. init_MUTEX(&ov->buf_lock);
  4990. init_MUTEX(&ov->param_lock);
  4991. init_MUTEX(&ov->i2c_lock);
  4992. init_MUTEX(&ov->cbuf_lock);
  4993. ov->buf_state = BUF_NOT_ALLOCATED;
  4994. if (usb_make_path(dev, ov->usb_path, OV511_USB_PATH_LEN) < 0) {
  4995. err("usb_make_path error");
  4996. goto error;
  4997. }
  4998. /* Allocate control transfer buffer. */
  4999. /* Must be kmalloc()'ed, for DMA compatibility */
  5000. ov->cbuf = kmalloc(OV511_CBUF_SIZE, GFP_KERNEL);
  5001. if (!ov->cbuf)
  5002. goto error;
  5003. if (ov->bclass == BCL_OV518) {
  5004. if (ov518_configure(ov) < 0)
  5005. goto error;
  5006. } else {
  5007. if (ov511_configure(ov) < 0)
  5008. goto error;
  5009. }
  5010. for (i = 0; i < OV511_NUMFRAMES; i++) {
  5011. ov->frame[i].framenum = i;
  5012. init_waitqueue_head(&ov->frame[i].wq);
  5013. }
  5014. for (i = 0; i < OV511_NUMSBUF; i++) {
  5015. ov->sbuf[i].ov = ov;
  5016. spin_lock_init(&ov->sbuf[i].lock);
  5017. ov->sbuf[i].n = i;
  5018. }
  5019. /* Unnecessary? (This is done on open(). Need to make sure variables
  5020. * are properly initialized without this before removing it, though). */
  5021. if (ov51x_set_default_params(ov) < 0)
  5022. goto error;
  5023. #ifdef OV511_DEBUG
  5024. if (dump_bridge) {
  5025. if (ov->bclass == BCL_OV511)
  5026. ov511_dump_regs(ov);
  5027. else
  5028. ov518_dump_regs(ov);
  5029. }
  5030. #endif
  5031. ov->vdev = video_device_alloc();
  5032. if (!ov->vdev)
  5033. goto error;
  5034. memcpy(ov->vdev, &vdev_template, sizeof(*ov->vdev));
  5035. ov->vdev->dev = &dev->dev;
  5036. video_set_drvdata(ov->vdev, ov);
  5037. for (i = 0; i < OV511_MAX_UNIT_VIDEO; i++) {
  5038. /* Minor 0 cannot be specified; assume user wants autodetect */
  5039. if (unit_video[i] == 0)
  5040. break;
  5041. if (video_register_device(ov->vdev, VFL_TYPE_GRABBER,
  5042. unit_video[i]) >= 0) {
  5043. break;
  5044. }
  5045. }
  5046. /* Use the next available one */
  5047. if ((ov->vdev->minor == -1) &&
  5048. video_register_device(ov->vdev, VFL_TYPE_GRABBER, -1) < 0) {
  5049. err("video_register_device failed");
  5050. goto error;
  5051. }
  5052. info("Device at %s registered to minor %d", ov->usb_path,
  5053. ov->vdev->minor);
  5054. usb_set_intfdata(intf, ov);
  5055. ov_create_sysfs(ov->vdev);
  5056. return 0;
  5057. error:
  5058. if (ov->vdev) {
  5059. if (-1 == ov->vdev->minor)
  5060. video_device_release(ov->vdev);
  5061. else
  5062. video_unregister_device(ov->vdev);
  5063. ov->vdev = NULL;
  5064. }
  5065. if (ov->cbuf) {
  5066. down(&ov->cbuf_lock);
  5067. kfree(ov->cbuf);
  5068. ov->cbuf = NULL;
  5069. up(&ov->cbuf_lock);
  5070. }
  5071. kfree(ov);
  5072. ov = NULL;
  5073. error_out:
  5074. err("Camera initialization failed");
  5075. return -EIO;
  5076. }
  5077. static void
  5078. ov51x_disconnect(struct usb_interface *intf)
  5079. {
  5080. struct usb_ov511 *ov = usb_get_intfdata(intf);
  5081. int n;
  5082. PDEBUG(3, "");
  5083. usb_set_intfdata (intf, NULL);
  5084. if (!ov)
  5085. return;
  5086. if (ov->vdev)
  5087. video_unregister_device(ov->vdev);
  5088. for (n = 0; n < OV511_NUMFRAMES; n++)
  5089. ov->frame[n].grabstate = FRAME_ERROR;
  5090. ov->curframe = -1;
  5091. /* This will cause the process to request another frame */
  5092. for (n = 0; n < OV511_NUMFRAMES; n++)
  5093. wake_up_interruptible(&ov->frame[n].wq);
  5094. wake_up_interruptible(&ov->wq);
  5095. ov->streaming = 0;
  5096. ov51x_unlink_isoc(ov);
  5097. ov->dev = NULL;
  5098. /* Free the memory */
  5099. if (ov && !ov->user) {
  5100. down(&ov->cbuf_lock);
  5101. kfree(ov->cbuf);
  5102. ov->cbuf = NULL;
  5103. up(&ov->cbuf_lock);
  5104. ov51x_dealloc(ov);
  5105. kfree(ov);
  5106. ov = NULL;
  5107. }
  5108. PDEBUG(3, "Disconnect complete");
  5109. }
  5110. static struct usb_driver ov511_driver = {
  5111. .owner = THIS_MODULE,
  5112. .name = "ov511",
  5113. .id_table = device_table,
  5114. .probe = ov51x_probe,
  5115. .disconnect = ov51x_disconnect
  5116. };
  5117. /****************************************************************************
  5118. *
  5119. * Module routines
  5120. *
  5121. ***************************************************************************/
  5122. /* Returns 0 for success */
  5123. int
  5124. ov511_register_decomp_module(int ver, struct ov51x_decomp_ops *ops, int ov518,
  5125. int mmx)
  5126. {
  5127. if (ver != DECOMP_INTERFACE_VER) {
  5128. err("Decompression module has incompatible");
  5129. err("interface version %d", ver);
  5130. err("Interface version %d is required", DECOMP_INTERFACE_VER);
  5131. return -EINVAL;
  5132. }
  5133. if (!ops)
  5134. return -EFAULT;
  5135. if (mmx && !ov51x_mmx_available) {
  5136. err("MMX not available on this system or kernel");
  5137. return -EINVAL;
  5138. }
  5139. lock_kernel();
  5140. if (ov518) {
  5141. if (mmx) {
  5142. if (ov518_mmx_decomp_ops)
  5143. goto err_in_use;
  5144. else
  5145. ov518_mmx_decomp_ops = ops;
  5146. } else {
  5147. if (ov518_decomp_ops)
  5148. goto err_in_use;
  5149. else
  5150. ov518_decomp_ops = ops;
  5151. }
  5152. } else {
  5153. if (mmx) {
  5154. if (ov511_mmx_decomp_ops)
  5155. goto err_in_use;
  5156. else
  5157. ov511_mmx_decomp_ops = ops;
  5158. } else {
  5159. if (ov511_decomp_ops)
  5160. goto err_in_use;
  5161. else
  5162. ov511_decomp_ops = ops;
  5163. }
  5164. }
  5165. unlock_kernel();
  5166. return 0;
  5167. err_in_use:
  5168. unlock_kernel();
  5169. return -EBUSY;
  5170. }
  5171. void
  5172. ov511_deregister_decomp_module(int ov518, int mmx)
  5173. {
  5174. lock_kernel();
  5175. if (ov518) {
  5176. if (mmx)
  5177. ov518_mmx_decomp_ops = NULL;
  5178. else
  5179. ov518_decomp_ops = NULL;
  5180. } else {
  5181. if (mmx)
  5182. ov511_mmx_decomp_ops = NULL;
  5183. else
  5184. ov511_decomp_ops = NULL;
  5185. }
  5186. unlock_kernel();
  5187. }
  5188. static int __init
  5189. usb_ov511_init(void)
  5190. {
  5191. int retval;
  5192. retval = usb_register(&ov511_driver);
  5193. if (retval)
  5194. goto out;
  5195. info(DRIVER_VERSION " : " DRIVER_DESC);
  5196. out:
  5197. return retval;
  5198. }
  5199. static void __exit
  5200. usb_ov511_exit(void)
  5201. {
  5202. usb_deregister(&ov511_driver);
  5203. info("driver deregistered");
  5204. }
  5205. module_init(usb_ov511_init);
  5206. module_exit(usb_ov511_exit);
  5207. EXPORT_SYMBOL(ov511_register_decomp_module);
  5208. EXPORT_SYMBOL(ov511_deregister_decomp_module);