ov511.c 142 KB

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