ov511.c 140 KB

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