sn9c102_core.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411
  1. /***************************************************************************
  2. * V4L2 driver for SN9C1xx PC Camera Controllers *
  3. * *
  4. * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the Free Software *
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
  19. ***************************************************************************/
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/param.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include <linux/device.h>
  28. #include <linux/fs.h>
  29. #include <linux/delay.h>
  30. #include <linux/compiler.h>
  31. #include <linux/ioctl.h>
  32. #include <linux/poll.h>
  33. #include <linux/stat.h>
  34. #include <linux/mm.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/page-flags.h>
  37. #include <linux/byteorder/generic.h>
  38. #include <asm/page.h>
  39. #include <asm/uaccess.h>
  40. #include "sn9c102.h"
  41. /*****************************************************************************/
  42. #define SN9C102_MODULE_NAME "V4L2 driver for SN9C1xx PC Camera Controllers"
  43. #define SN9C102_MODULE_ALIAS "sn9c1xx"
  44. #define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia"
  45. #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
  46. #define SN9C102_MODULE_LICENSE "GPL"
  47. #define SN9C102_MODULE_VERSION "1:1.39"
  48. #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 39)
  49. /*****************************************************************************/
  50. MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
  51. MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
  52. MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
  53. MODULE_ALIAS(SN9C102_MODULE_ALIAS);
  54. MODULE_VERSION(SN9C102_MODULE_VERSION);
  55. MODULE_LICENSE(SN9C102_MODULE_LICENSE);
  56. static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
  57. module_param_array(video_nr, short, NULL, 0444);
  58. MODULE_PARM_DESC(video_nr,
  59. "\n<-1|n[,...]> Specify V4L2 minor mode number."
  60. "\n -1 = use next available (default)"
  61. "\n n = use minor number n (integer >= 0)"
  62. "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
  63. " cameras this way."
  64. "\nFor example:"
  65. "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
  66. "\nthe second camera and use auto for the first"
  67. "\none and for every other camera."
  68. "\n");
  69. static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
  70. SN9C102_FORCE_MUNMAP};
  71. module_param_array(force_munmap, bool, NULL, 0444);
  72. MODULE_PARM_DESC(force_munmap,
  73. "\n<0|1[,...]> Force the application to unmap previously"
  74. "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
  75. "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
  76. "\nthis feature. This parameter is specific for each"
  77. "\ndetected camera."
  78. "\n 0 = do not force memory unmapping"
  79. "\n 1 = force memory unmapping (save memory)"
  80. "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
  81. "\n");
  82. static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
  83. SN9C102_FRAME_TIMEOUT};
  84. module_param_array(frame_timeout, uint, NULL, 0644);
  85. MODULE_PARM_DESC(frame_timeout,
  86. "\n<0|n[,...]> Timeout for a video frame in seconds before"
  87. "\nreturning an I/O error; 0 for infinity."
  88. "\nThis parameter is specific for each detected camera."
  89. "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
  90. "\n");
  91. #ifdef SN9C102_DEBUG
  92. static unsigned short debug = SN9C102_DEBUG_LEVEL;
  93. module_param(debug, ushort, 0644);
  94. MODULE_PARM_DESC(debug,
  95. "\n<n> Debugging information level, from 0 to 3:"
  96. "\n0 = none (use carefully)"
  97. "\n1 = critical errors"
  98. "\n2 = significant informations"
  99. "\n3 = more verbose messages"
  100. "\nLevel 3 is useful for testing only."
  101. "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
  102. "\n");
  103. #endif
  104. /*****************************************************************************/
  105. static u32
  106. sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
  107. enum sn9c102_io_method io)
  108. {
  109. struct v4l2_pix_format* p = &(cam->sensor.pix_format);
  110. struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
  111. size_t imagesize = cam->module_param.force_munmap || io == IO_READ ?
  112. (p->width * p->height * p->priv) / 8 :
  113. (r->width * r->height * p->priv) / 8;
  114. void* buff = NULL;
  115. u32 i;
  116. if (count > SN9C102_MAX_FRAMES)
  117. count = SN9C102_MAX_FRAMES;
  118. if (cam->bridge == BRIDGE_SN9C105 || cam->bridge == BRIDGE_SN9C120)
  119. imagesize += 589 + 2; /* length of JPEG header + EOI marker */
  120. cam->nbuffers = count;
  121. while (cam->nbuffers > 0) {
  122. if ((buff = vmalloc_32_user(cam->nbuffers *
  123. PAGE_ALIGN(imagesize))))
  124. break;
  125. cam->nbuffers--;
  126. }
  127. for (i = 0; i < cam->nbuffers; i++) {
  128. cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
  129. cam->frame[i].buf.index = i;
  130. cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
  131. cam->frame[i].buf.length = imagesize;
  132. cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  133. cam->frame[i].buf.sequence = 0;
  134. cam->frame[i].buf.field = V4L2_FIELD_NONE;
  135. cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
  136. cam->frame[i].buf.flags = 0;
  137. }
  138. return cam->nbuffers;
  139. }
  140. static void sn9c102_release_buffers(struct sn9c102_device* cam)
  141. {
  142. if (cam->nbuffers) {
  143. vfree(cam->frame[0].bufmem);
  144. cam->nbuffers = 0;
  145. }
  146. cam->frame_current = NULL;
  147. }
  148. static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
  149. {
  150. u32 i;
  151. INIT_LIST_HEAD(&cam->inqueue);
  152. INIT_LIST_HEAD(&cam->outqueue);
  153. for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
  154. cam->frame[i].state = F_UNUSED;
  155. cam->frame[i].buf.bytesused = 0;
  156. }
  157. }
  158. static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
  159. {
  160. struct sn9c102_frame_t *i;
  161. list_for_each_entry(i, &cam->outqueue, frame) {
  162. i->state = F_QUEUED;
  163. list_add(&i->frame, &cam->inqueue);
  164. }
  165. INIT_LIST_HEAD(&cam->outqueue);
  166. }
  167. static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
  168. {
  169. unsigned long lock_flags;
  170. u32 i;
  171. for (i = 0; i < cam->nbuffers; i++)
  172. if (cam->frame[i].state == F_UNUSED) {
  173. cam->frame[i].state = F_QUEUED;
  174. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  175. list_add_tail(&cam->frame[i].frame, &cam->inqueue);
  176. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  177. }
  178. }
  179. /*****************************************************************************/
  180. /*
  181. * Write a sequence of count value/register pairs. Returns -1 after the
  182. * first failed write, or 0 for no errors.
  183. */
  184. int sn9c102_write_regs(struct sn9c102_device* cam, const u8 valreg[][2],
  185. int count)
  186. {
  187. struct usb_device* udev = cam->usbdev;
  188. u8* value = cam->control_buffer; /* Needed for DMA'able memory */
  189. int i, res;
  190. for (i = 0; i < count; i++) {
  191. u8 index = valreg[i][1];
  192. /*
  193. * index is a u8, so it must be <256 and can't be out of range.
  194. * If we put in a check anyway, gcc annoys us with a warning
  195. * that our check is useless. People get all uppity when they
  196. * see warnings in the kernel compile.
  197. */
  198. *value = valreg[i][0];
  199. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  200. 0x08, 0x41, index, 0,
  201. value, 1, SN9C102_CTRL_TIMEOUT);
  202. if (res < 0) {
  203. DBG(3, "Failed to write a register (value 0x%02X, "
  204. "index 0x%02X, error %d)", *value, index, res);
  205. return -1;
  206. }
  207. cam->reg[index] = *value;
  208. }
  209. return 0;
  210. }
  211. int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
  212. {
  213. struct usb_device* udev = cam->usbdev;
  214. u8* buff = cam->control_buffer;
  215. int res;
  216. if (index >= ARRAY_SIZE(cam->reg))
  217. return -1;
  218. *buff = value;
  219. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  220. index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
  221. if (res < 0) {
  222. DBG(3, "Failed to write a register (value 0x%02X, index "
  223. "0x%02X, error %d)", value, index, res);
  224. return -1;
  225. }
  226. cam->reg[index] = value;
  227. return 0;
  228. }
  229. /* NOTE: reading some registers always returns 0 */
  230. static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
  231. {
  232. struct usb_device* udev = cam->usbdev;
  233. u8* buff = cam->control_buffer;
  234. int res;
  235. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
  236. index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
  237. if (res < 0)
  238. DBG(3, "Failed to read a register (index 0x%02X, error %d)",
  239. index, res);
  240. return (res >= 0) ? (int)(*buff) : -1;
  241. }
  242. int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
  243. {
  244. if (index >= ARRAY_SIZE(cam->reg))
  245. return -1;
  246. return cam->reg[index];
  247. }
  248. static int
  249. sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
  250. {
  251. int i, r;
  252. for (i = 1; i <= 5; i++) {
  253. r = sn9c102_read_reg(cam, 0x08);
  254. if (r < 0)
  255. return -EIO;
  256. if (r & 0x04)
  257. return 0;
  258. if (sensor->frequency & SN9C102_I2C_400KHZ)
  259. udelay(5*16);
  260. else
  261. udelay(16*16);
  262. }
  263. return -EBUSY;
  264. }
  265. static int
  266. sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
  267. struct sn9c102_sensor* sensor)
  268. {
  269. int r , err = 0;
  270. r = sn9c102_read_reg(cam, 0x08);
  271. if (r < 0)
  272. err += r;
  273. if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
  274. if (!(r & 0x08))
  275. err += -1;
  276. } else {
  277. if (r & 0x08)
  278. err += -1;
  279. }
  280. return err ? -EIO : 0;
  281. }
  282. static int
  283. sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
  284. struct sn9c102_sensor* sensor)
  285. {
  286. int r;
  287. r = sn9c102_read_reg(cam, 0x08);
  288. return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
  289. }
  290. int
  291. sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
  292. struct sn9c102_sensor* sensor, u8 data0, u8 data1,
  293. u8 n, u8 buffer[])
  294. {
  295. struct usb_device* udev = cam->usbdev;
  296. u8* data = cam->control_buffer;
  297. int err = 0, res;
  298. /* Write cycle */
  299. data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
  300. ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
  301. data[1] = data0; /* I2C slave id */
  302. data[2] = data1; /* address */
  303. data[7] = 0x10;
  304. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  305. 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
  306. if (res < 0)
  307. err += res;
  308. err += sn9c102_i2c_wait(cam, sensor);
  309. /* Read cycle - n bytes */
  310. data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
  311. ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
  312. (n << 4) | 0x02;
  313. data[1] = data0;
  314. data[7] = 0x10;
  315. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  316. 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
  317. if (res < 0)
  318. err += res;
  319. err += sn9c102_i2c_wait(cam, sensor);
  320. /* The first read byte will be placed in data[4] */
  321. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
  322. 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
  323. if (res < 0)
  324. err += res;
  325. err += sn9c102_i2c_detect_read_error(cam, sensor);
  326. PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
  327. data[4]);
  328. if (err) {
  329. DBG(3, "I2C read failed for %s image sensor", sensor->name);
  330. return -1;
  331. }
  332. if (buffer)
  333. memcpy(buffer, data, sizeof(buffer));
  334. return (int)data[4];
  335. }
  336. int
  337. sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
  338. struct sn9c102_sensor* sensor, u8 n, u8 data0,
  339. u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
  340. {
  341. struct usb_device* udev = cam->usbdev;
  342. u8* data = cam->control_buffer;
  343. int err = 0, res;
  344. /* Write cycle. It usually is address + value */
  345. data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
  346. ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
  347. | ((n - 1) << 4);
  348. data[1] = data0;
  349. data[2] = data1;
  350. data[3] = data2;
  351. data[4] = data3;
  352. data[5] = data4;
  353. data[6] = data5;
  354. data[7] = 0x17;
  355. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  356. 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
  357. if (res < 0)
  358. err += res;
  359. err += sn9c102_i2c_wait(cam, sensor);
  360. err += sn9c102_i2c_detect_write_error(cam, sensor);
  361. if (err)
  362. DBG(3, "I2C write failed for %s image sensor", sensor->name);
  363. PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
  364. "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
  365. n, data0, data1, data2, data3, data4, data5);
  366. return err ? -1 : 0;
  367. }
  368. int
  369. sn9c102_i2c_try_read(struct sn9c102_device* cam,
  370. struct sn9c102_sensor* sensor, u8 address)
  371. {
  372. return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
  373. address, 1, NULL);
  374. }
  375. int
  376. sn9c102_i2c_try_write(struct sn9c102_device* cam,
  377. struct sn9c102_sensor* sensor, u8 address, u8 value)
  378. {
  379. return sn9c102_i2c_try_raw_write(cam, sensor, 3,
  380. sensor->i2c_slave_id, address,
  381. value, 0, 0, 0);
  382. }
  383. int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
  384. {
  385. return sn9c102_i2c_try_read(cam, &cam->sensor, address);
  386. }
  387. int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
  388. {
  389. return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
  390. }
  391. /*****************************************************************************/
  392. static size_t sn9c102_sof_length(struct sn9c102_device* cam)
  393. {
  394. switch (cam->bridge) {
  395. case BRIDGE_SN9C101:
  396. case BRIDGE_SN9C102:
  397. return 12;
  398. case BRIDGE_SN9C103:
  399. return 18;
  400. case BRIDGE_SN9C105:
  401. case BRIDGE_SN9C120:
  402. return 62;
  403. }
  404. return 0;
  405. }
  406. static void*
  407. sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
  408. {
  409. const char marker[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
  410. const char *m = mem;
  411. size_t soflen = 0, i, j;
  412. soflen = sn9c102_sof_length(cam);
  413. for (i = 0; i < len; i++) {
  414. size_t b;
  415. /* Read the variable part of the header */
  416. if (unlikely(cam->sof.bytesread >= sizeof(marker))) {
  417. cam->sof.header[cam->sof.bytesread] = *(m+i);
  418. if (++cam->sof.bytesread == soflen) {
  419. cam->sof.bytesread = 0;
  420. return mem + i;
  421. }
  422. continue;
  423. }
  424. /* Search for the SOF marker (fixed part) in the header */
  425. for (j = 0, b=cam->sof.bytesread; j+b < sizeof(marker); j++) {
  426. if (unlikely(i+j) == len)
  427. return NULL;
  428. if (*(m+i+j) == marker[cam->sof.bytesread]) {
  429. cam->sof.header[cam->sof.bytesread] = *(m+i+j);
  430. if (++cam->sof.bytesread == sizeof(marker)) {
  431. PDBGG("Bytes to analyze: %zd. SOF "
  432. "starts at byte #%zd", len, i);
  433. i += j+1;
  434. break;
  435. }
  436. } else {
  437. cam->sof.bytesread = 0;
  438. break;
  439. }
  440. }
  441. }
  442. return NULL;
  443. }
  444. static void*
  445. sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
  446. {
  447. char eof_header[4][4] = {
  448. {0x00, 0x00, 0x00, 0x00},
  449. {0x40, 0x00, 0x00, 0x00},
  450. {0x80, 0x00, 0x00, 0x00},
  451. {0xc0, 0x00, 0x00, 0x00},
  452. };
  453. size_t i, j;
  454. /* The EOF header does not exist in compressed data */
  455. if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
  456. cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
  457. return NULL;
  458. /*
  459. The EOF header might cross the packet boundary, but this is not a
  460. problem, since the end of a frame is determined by checking its size
  461. in the first place.
  462. */
  463. for (i = 0; (len >= 4) && (i <= len - 4); i++)
  464. for (j = 0; j < ARRAY_SIZE(eof_header); j++)
  465. if (!memcmp(mem + i, eof_header[j], 4))
  466. return mem + i;
  467. return NULL;
  468. }
  469. static void
  470. sn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
  471. {
  472. static u8 jpeg_header[589] = {
  473. 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,
  474. 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,
  475. 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,
  476. 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,
  477. 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,
  478. 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,
  479. 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,
  480. 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,
  481. 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,
  482. 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
  483. 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
  484. 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
  485. 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
  486. 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,
  487. 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  488. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  489. 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,
  490. 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  491. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
  492. 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,
  493. 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
  494. 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
  495. 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
  496. 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
  497. 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
  498. 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
  499. 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38,
  500. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
  501. 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
  502. 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
  503. 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
  504. 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
  505. 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
  506. 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
  507. 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
  508. 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
  509. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
  510. 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,
  511. 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
  512. 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
  513. 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  514. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
  515. 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
  516. 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,
  517. 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  518. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
  519. 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
  520. 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
  521. 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  522. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  523. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
  524. 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
  525. 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  526. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,
  527. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
  528. 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,
  529. 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,
  530. 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
  531. 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
  532. };
  533. u8 *pos = f->bufmem;
  534. memcpy(pos, jpeg_header, sizeof(jpeg_header));
  535. *(pos + 6) = 0x00;
  536. *(pos + 7 + 64) = 0x01;
  537. if (cam->compression.quality == 0) {
  538. memcpy(pos + 7, SN9C102_Y_QTABLE0, 64);
  539. memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64);
  540. } else if (cam->compression.quality == 1) {
  541. memcpy(pos + 7, SN9C102_Y_QTABLE1, 64);
  542. memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64);
  543. }
  544. *(pos + 564) = cam->sensor.pix_format.width & 0xFF;
  545. *(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF;
  546. *(pos + 562) = cam->sensor.pix_format.height & 0xFF;
  547. *(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF;
  548. *(pos + 567) = 0x21;
  549. f->buf.bytesused += sizeof(jpeg_header);
  550. }
  551. static void
  552. sn9c102_write_eoimarker(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
  553. {
  554. static const u8 eoi_marker[2] = {0xff, 0xd9};
  555. memcpy(f->bufmem + f->buf.bytesused, eoi_marker, sizeof(eoi_marker));
  556. f->buf.bytesused += sizeof(eoi_marker);
  557. }
  558. static void sn9c102_urb_complete(struct urb *urb)
  559. {
  560. struct sn9c102_device* cam = urb->context;
  561. struct sn9c102_frame_t** f;
  562. size_t imagesize, soflen;
  563. u8 i;
  564. int err = 0;
  565. if (urb->status == -ENOENT)
  566. return;
  567. f = &cam->frame_current;
  568. if (cam->stream == STREAM_INTERRUPT) {
  569. cam->stream = STREAM_OFF;
  570. if ((*f))
  571. (*f)->state = F_QUEUED;
  572. cam->sof.bytesread = 0;
  573. DBG(3, "Stream interrupted by application");
  574. wake_up(&cam->wait_stream);
  575. }
  576. if (cam->state & DEV_DISCONNECTED)
  577. return;
  578. if (cam->state & DEV_MISCONFIGURED) {
  579. wake_up_interruptible(&cam->wait_frame);
  580. return;
  581. }
  582. if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
  583. goto resubmit_urb;
  584. if (!(*f))
  585. (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
  586. frame);
  587. imagesize = (cam->sensor.pix_format.width *
  588. cam->sensor.pix_format.height *
  589. cam->sensor.pix_format.priv) / 8;
  590. if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
  591. imagesize += 589; /* length of jpeg header */
  592. soflen = sn9c102_sof_length(cam);
  593. for (i = 0; i < urb->number_of_packets; i++) {
  594. unsigned int img, len, status;
  595. void *pos, *sof, *eof;
  596. len = urb->iso_frame_desc[i].actual_length;
  597. status = urb->iso_frame_desc[i].status;
  598. pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
  599. if (status) {
  600. DBG(3, "Error in isochronous frame");
  601. (*f)->state = F_ERROR;
  602. cam->sof.bytesread = 0;
  603. continue;
  604. }
  605. PDBGG("Isochrnous frame: length %u, #%u i", len, i);
  606. redo:
  607. sof = sn9c102_find_sof_header(cam, pos, len);
  608. if (likely(!sof)) {
  609. eof = sn9c102_find_eof_header(cam, pos, len);
  610. if ((*f)->state == F_GRABBING) {
  611. end_of_frame:
  612. img = len;
  613. if (eof)
  614. img = (eof > pos) ? eof - pos - 1 : 0;
  615. if ((*f)->buf.bytesused + img > imagesize) {
  616. u32 b;
  617. b = (*f)->buf.bytesused + img -
  618. imagesize;
  619. img = imagesize - (*f)->buf.bytesused;
  620. PDBGG("Expected EOF not found: video "
  621. "frame cut");
  622. if (eof)
  623. DBG(3, "Exceeded limit: +%u "
  624. "bytes", (unsigned)(b));
  625. }
  626. memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
  627. img);
  628. if ((*f)->buf.bytesused == 0)
  629. do_gettimeofday(&(*f)->buf.timestamp);
  630. (*f)->buf.bytesused += img;
  631. if ((*f)->buf.bytesused == imagesize ||
  632. ((cam->sensor.pix_format.pixelformat ==
  633. V4L2_PIX_FMT_SN9C10X ||
  634. cam->sensor.pix_format.pixelformat ==
  635. V4L2_PIX_FMT_JPEG) && eof)) {
  636. u32 b;
  637. b = (*f)->buf.bytesused;
  638. (*f)->state = F_DONE;
  639. (*f)->buf.sequence= ++cam->frame_count;
  640. spin_lock(&cam->queue_lock);
  641. list_move_tail(&(*f)->frame,
  642. &cam->outqueue);
  643. if (!list_empty(&cam->inqueue))
  644. (*f) = list_entry(
  645. cam->inqueue.next,
  646. struct sn9c102_frame_t,
  647. frame );
  648. else
  649. (*f) = NULL;
  650. spin_unlock(&cam->queue_lock);
  651. memcpy(cam->sysfs.frame_header,
  652. cam->sof.header, soflen);
  653. DBG(3, "Video frame captured: %lu "
  654. "bytes", (unsigned long)(b));
  655. if (!(*f))
  656. goto resubmit_urb;
  657. } else if (eof) {
  658. (*f)->state = F_ERROR;
  659. DBG(3, "Not expected EOF after %lu "
  660. "bytes of image data",
  661. (unsigned long)
  662. ((*f)->buf.bytesused));
  663. }
  664. if (sof) /* (1) */
  665. goto start_of_frame;
  666. } else if (eof) {
  667. DBG(3, "EOF without SOF");
  668. continue;
  669. } else {
  670. PDBGG("Ignoring pointless isochronous frame");
  671. continue;
  672. }
  673. } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
  674. start_of_frame:
  675. (*f)->state = F_GRABBING;
  676. (*f)->buf.bytesused = 0;
  677. len -= (sof - pos);
  678. pos = sof;
  679. if (cam->sensor.pix_format.pixelformat ==
  680. V4L2_PIX_FMT_JPEG)
  681. sn9c102_write_jpegheader(cam, (*f));
  682. DBG(3, "SOF detected: new video frame");
  683. if (len)
  684. goto redo;
  685. } else if ((*f)->state == F_GRABBING) {
  686. eof = sn9c102_find_eof_header(cam, pos, len);
  687. if (eof && eof < sof)
  688. goto end_of_frame; /* (1) */
  689. else {
  690. if (cam->sensor.pix_format.pixelformat ==
  691. V4L2_PIX_FMT_SN9C10X ||
  692. cam->sensor.pix_format.pixelformat ==
  693. V4L2_PIX_FMT_JPEG) {
  694. if (sof - pos >= soflen) {
  695. eof = sof - soflen;
  696. } else { /* remove header */
  697. eof = pos;
  698. (*f)->buf.bytesused -=
  699. (soflen - (sof - pos));
  700. }
  701. goto end_of_frame;
  702. } else {
  703. DBG(3, "SOF before expected EOF after "
  704. "%lu bytes of image data",
  705. (unsigned long)
  706. ((*f)->buf.bytesused));
  707. goto start_of_frame;
  708. }
  709. }
  710. }
  711. }
  712. resubmit_urb:
  713. urb->dev = cam->usbdev;
  714. err = usb_submit_urb(urb, GFP_ATOMIC);
  715. if (err < 0 && err != -EPERM) {
  716. cam->state |= DEV_MISCONFIGURED;
  717. DBG(1, "usb_submit_urb() failed");
  718. }
  719. wake_up_interruptible(&cam->wait_frame);
  720. }
  721. static int sn9c102_start_transfer(struct sn9c102_device* cam)
  722. {
  723. struct usb_device *udev = cam->usbdev;
  724. struct urb* urb;
  725. struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
  726. usb_ifnum_to_if(udev, 0),
  727. SN9C102_ALTERNATE_SETTING);
  728. const unsigned int psz = le16_to_cpu(altsetting->
  729. endpoint[0].desc.wMaxPacketSize);
  730. s8 i, j;
  731. int err = 0;
  732. for (i = 0; i < SN9C102_URBS; i++) {
  733. cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
  734. GFP_KERNEL);
  735. if (!cam->transfer_buffer[i]) {
  736. err = -ENOMEM;
  737. DBG(1, "Not enough memory");
  738. goto free_buffers;
  739. }
  740. }
  741. for (i = 0; i < SN9C102_URBS; i++) {
  742. urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
  743. cam->urb[i] = urb;
  744. if (!urb) {
  745. err = -ENOMEM;
  746. DBG(1, "usb_alloc_urb() failed");
  747. goto free_urbs;
  748. }
  749. urb->dev = udev;
  750. urb->context = cam;
  751. urb->pipe = usb_rcvisocpipe(udev, 1);
  752. urb->transfer_flags = URB_ISO_ASAP;
  753. urb->number_of_packets = SN9C102_ISO_PACKETS;
  754. urb->complete = sn9c102_urb_complete;
  755. urb->transfer_buffer = cam->transfer_buffer[i];
  756. urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
  757. urb->interval = 1;
  758. for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
  759. urb->iso_frame_desc[j].offset = psz * j;
  760. urb->iso_frame_desc[j].length = psz;
  761. }
  762. }
  763. /* Enable video */
  764. if (!(cam->reg[0x01] & 0x04)) {
  765. err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
  766. if (err) {
  767. err = -EIO;
  768. DBG(1, "I/O hardware error");
  769. goto free_urbs;
  770. }
  771. }
  772. err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
  773. if (err) {
  774. DBG(1, "usb_set_interface() failed");
  775. goto free_urbs;
  776. }
  777. cam->frame_current = NULL;
  778. cam->sof.bytesread = 0;
  779. for (i = 0; i < SN9C102_URBS; i++) {
  780. err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
  781. if (err) {
  782. for (j = i-1; j >= 0; j--)
  783. usb_kill_urb(cam->urb[j]);
  784. DBG(1, "usb_submit_urb() failed, error %d", err);
  785. goto free_urbs;
  786. }
  787. }
  788. return 0;
  789. free_urbs:
  790. for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
  791. usb_free_urb(cam->urb[i]);
  792. free_buffers:
  793. for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
  794. kfree(cam->transfer_buffer[i]);
  795. return err;
  796. }
  797. static int sn9c102_stop_transfer(struct sn9c102_device* cam)
  798. {
  799. struct usb_device *udev = cam->usbdev;
  800. s8 i;
  801. int err = 0;
  802. if (cam->state & DEV_DISCONNECTED)
  803. return 0;
  804. for (i = SN9C102_URBS-1; i >= 0; i--) {
  805. usb_kill_urb(cam->urb[i]);
  806. usb_free_urb(cam->urb[i]);
  807. kfree(cam->transfer_buffer[i]);
  808. }
  809. err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
  810. if (err)
  811. DBG(3, "usb_set_interface() failed");
  812. return err;
  813. }
  814. static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
  815. {
  816. long timeout;
  817. cam->stream = STREAM_INTERRUPT;
  818. timeout = wait_event_timeout(cam->wait_stream,
  819. (cam->stream == STREAM_OFF) ||
  820. (cam->state & DEV_DISCONNECTED),
  821. SN9C102_URB_TIMEOUT);
  822. if (cam->state & DEV_DISCONNECTED)
  823. return -ENODEV;
  824. else if (cam->stream != STREAM_OFF) {
  825. cam->state |= DEV_MISCONFIGURED;
  826. DBG(1, "URB timeout reached. The camera is misconfigured. "
  827. "To use it, close and open /dev/video%d again.",
  828. cam->v4ldev->minor);
  829. return -EIO;
  830. }
  831. return 0;
  832. }
  833. /*****************************************************************************/
  834. #ifdef CONFIG_VIDEO_ADV_DEBUG
  835. static u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count)
  836. {
  837. char str[7];
  838. char* endp;
  839. unsigned long val;
  840. if (len < 6) {
  841. strncpy(str, buff, len);
  842. str[len] = '\0';
  843. } else {
  844. strncpy(str, buff, 6);
  845. str[6] = '\0';
  846. }
  847. val = simple_strtoul(str, &endp, 0);
  848. *count = 0;
  849. if (val <= 0xffff)
  850. *count = (ssize_t)(endp - str);
  851. if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
  852. *count += 1;
  853. return (u16)val;
  854. }
  855. /*
  856. NOTE 1: being inside one of the following methods implies that the v4l
  857. device exists for sure (see kobjects and reference counters)
  858. NOTE 2: buffers are PAGE_SIZE long
  859. */
  860. static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
  861. {
  862. struct sn9c102_device* cam;
  863. ssize_t count;
  864. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  865. return -ERESTARTSYS;
  866. cam = video_get_drvdata(container_of(cd, struct video_device,
  867. class_dev));
  868. if (!cam) {
  869. mutex_unlock(&sn9c102_sysfs_lock);
  870. return -ENODEV;
  871. }
  872. count = sprintf(buf, "%u\n", cam->sysfs.reg);
  873. mutex_unlock(&sn9c102_sysfs_lock);
  874. return count;
  875. }
  876. static ssize_t
  877. sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
  878. {
  879. struct sn9c102_device* cam;
  880. u16 index;
  881. ssize_t count;
  882. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  883. return -ERESTARTSYS;
  884. cam = video_get_drvdata(container_of(cd, struct video_device,
  885. class_dev));
  886. if (!cam) {
  887. mutex_unlock(&sn9c102_sysfs_lock);
  888. return -ENODEV;
  889. }
  890. index = sn9c102_strtou16(buf, len, &count);
  891. if (index >= ARRAY_SIZE(cam->reg) || !count) {
  892. mutex_unlock(&sn9c102_sysfs_lock);
  893. return -EINVAL;
  894. }
  895. cam->sysfs.reg = index;
  896. DBG(2, "Moved SN9C1XX register index to 0x%02X", cam->sysfs.reg);
  897. DBG(3, "Written bytes: %zd", count);
  898. mutex_unlock(&sn9c102_sysfs_lock);
  899. return count;
  900. }
  901. static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
  902. {
  903. struct sn9c102_device* cam;
  904. ssize_t count;
  905. int val;
  906. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  907. return -ERESTARTSYS;
  908. cam = video_get_drvdata(container_of(cd, struct video_device,
  909. class_dev));
  910. if (!cam) {
  911. mutex_unlock(&sn9c102_sysfs_lock);
  912. return -ENODEV;
  913. }
  914. if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
  915. mutex_unlock(&sn9c102_sysfs_lock);
  916. return -EIO;
  917. }
  918. count = sprintf(buf, "%d\n", val);
  919. DBG(3, "Read bytes: %zd, value: %d", count, val);
  920. mutex_unlock(&sn9c102_sysfs_lock);
  921. return count;
  922. }
  923. static ssize_t
  924. sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
  925. {
  926. struct sn9c102_device* cam;
  927. u16 value;
  928. ssize_t count;
  929. int err;
  930. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  931. return -ERESTARTSYS;
  932. cam = video_get_drvdata(container_of(cd, struct video_device,
  933. class_dev));
  934. if (!cam) {
  935. mutex_unlock(&sn9c102_sysfs_lock);
  936. return -ENODEV;
  937. }
  938. value = sn9c102_strtou16(buf, len, &count);
  939. if (!count) {
  940. mutex_unlock(&sn9c102_sysfs_lock);
  941. return -EINVAL;
  942. }
  943. err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
  944. if (err) {
  945. mutex_unlock(&sn9c102_sysfs_lock);
  946. return -EIO;
  947. }
  948. DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X",
  949. cam->sysfs.reg, value);
  950. DBG(3, "Written bytes: %zd", count);
  951. mutex_unlock(&sn9c102_sysfs_lock);
  952. return count;
  953. }
  954. static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
  955. {
  956. struct sn9c102_device* cam;
  957. ssize_t count;
  958. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  959. return -ERESTARTSYS;
  960. cam = video_get_drvdata(container_of(cd, struct video_device,
  961. class_dev));
  962. if (!cam) {
  963. mutex_unlock(&sn9c102_sysfs_lock);
  964. return -ENODEV;
  965. }
  966. count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
  967. DBG(3, "Read bytes: %zd", count);
  968. mutex_unlock(&sn9c102_sysfs_lock);
  969. return count;
  970. }
  971. static ssize_t
  972. sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
  973. {
  974. struct sn9c102_device* cam;
  975. u16 index;
  976. ssize_t count;
  977. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  978. return -ERESTARTSYS;
  979. cam = video_get_drvdata(container_of(cd, struct video_device,
  980. class_dev));
  981. if (!cam) {
  982. mutex_unlock(&sn9c102_sysfs_lock);
  983. return -ENODEV;
  984. }
  985. index = sn9c102_strtou16(buf, len, &count);
  986. if (!count) {
  987. mutex_unlock(&sn9c102_sysfs_lock);
  988. return -EINVAL;
  989. }
  990. cam->sysfs.i2c_reg = index;
  991. DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
  992. DBG(3, "Written bytes: %zd", count);
  993. mutex_unlock(&sn9c102_sysfs_lock);
  994. return count;
  995. }
  996. static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
  997. {
  998. struct sn9c102_device* cam;
  999. ssize_t count;
  1000. int val;
  1001. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  1002. return -ERESTARTSYS;
  1003. cam = video_get_drvdata(container_of(cd, struct video_device,
  1004. class_dev));
  1005. if (!cam) {
  1006. mutex_unlock(&sn9c102_sysfs_lock);
  1007. return -ENODEV;
  1008. }
  1009. if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
  1010. mutex_unlock(&sn9c102_sysfs_lock);
  1011. return -ENOSYS;
  1012. }
  1013. if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
  1014. mutex_unlock(&sn9c102_sysfs_lock);
  1015. return -EIO;
  1016. }
  1017. count = sprintf(buf, "%d\n", val);
  1018. DBG(3, "Read bytes: %zd, value: %d", count, val);
  1019. mutex_unlock(&sn9c102_sysfs_lock);
  1020. return count;
  1021. }
  1022. static ssize_t
  1023. sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
  1024. {
  1025. struct sn9c102_device* cam;
  1026. u16 value;
  1027. ssize_t count;
  1028. int err;
  1029. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  1030. return -ERESTARTSYS;
  1031. cam = video_get_drvdata(container_of(cd, struct video_device,
  1032. class_dev));
  1033. if (!cam) {
  1034. mutex_unlock(&sn9c102_sysfs_lock);
  1035. return -ENODEV;
  1036. }
  1037. if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
  1038. mutex_unlock(&sn9c102_sysfs_lock);
  1039. return -ENOSYS;
  1040. }
  1041. value = sn9c102_strtou16(buf, len, &count);
  1042. if (!count) {
  1043. mutex_unlock(&sn9c102_sysfs_lock);
  1044. return -EINVAL;
  1045. }
  1046. err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
  1047. if (err) {
  1048. mutex_unlock(&sn9c102_sysfs_lock);
  1049. return -EIO;
  1050. }
  1051. DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
  1052. cam->sysfs.i2c_reg, value);
  1053. DBG(3, "Written bytes: %zd", count);
  1054. mutex_unlock(&sn9c102_sysfs_lock);
  1055. return count;
  1056. }
  1057. static ssize_t
  1058. sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
  1059. {
  1060. struct sn9c102_device* cam;
  1061. enum sn9c102_bridge bridge;
  1062. ssize_t res = 0;
  1063. u16 value;
  1064. ssize_t count;
  1065. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  1066. return -ERESTARTSYS;
  1067. cam = video_get_drvdata(container_of(cd, struct video_device,
  1068. class_dev));
  1069. if (!cam) {
  1070. mutex_unlock(&sn9c102_sysfs_lock);
  1071. return -ENODEV;
  1072. }
  1073. bridge = cam->bridge;
  1074. mutex_unlock(&sn9c102_sysfs_lock);
  1075. value = sn9c102_strtou16(buf, len, &count);
  1076. if (!count)
  1077. return -EINVAL;
  1078. switch (bridge) {
  1079. case BRIDGE_SN9C101:
  1080. case BRIDGE_SN9C102:
  1081. if (value > 0x0f)
  1082. return -EINVAL;
  1083. if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
  1084. res = sn9c102_store_val(cd, buf, len);
  1085. break;
  1086. case BRIDGE_SN9C103:
  1087. case BRIDGE_SN9C105:
  1088. case BRIDGE_SN9C120:
  1089. if (value > 0x7f)
  1090. return -EINVAL;
  1091. if ((res = sn9c102_store_reg(cd, "0x07", 4)) >= 0)
  1092. res = sn9c102_store_val(cd, buf, len);
  1093. break;
  1094. }
  1095. return res;
  1096. }
  1097. static ssize_t
  1098. sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
  1099. {
  1100. ssize_t res = 0;
  1101. u16 value;
  1102. ssize_t count;
  1103. value = sn9c102_strtou16(buf, len, &count);
  1104. if (!count || value > 0x7f)
  1105. return -EINVAL;
  1106. if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
  1107. res = sn9c102_store_val(cd, buf, len);
  1108. return res;
  1109. }
  1110. static ssize_t
  1111. sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
  1112. {
  1113. ssize_t res = 0;
  1114. u16 value;
  1115. ssize_t count;
  1116. value = sn9c102_strtou16(buf, len, &count);
  1117. if (!count || value > 0x7f)
  1118. return -EINVAL;
  1119. if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
  1120. res = sn9c102_store_val(cd, buf, len);
  1121. return res;
  1122. }
  1123. static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
  1124. {
  1125. struct sn9c102_device* cam;
  1126. ssize_t count;
  1127. cam = video_get_drvdata(container_of(cd, struct video_device,
  1128. class_dev));
  1129. if (!cam)
  1130. return -ENODEV;
  1131. count = sizeof(cam->sysfs.frame_header);
  1132. memcpy(buf, cam->sysfs.frame_header, count);
  1133. DBG(3, "Frame header, read bytes: %zd", count);
  1134. return count;
  1135. }
  1136. static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
  1137. sn9c102_show_reg, sn9c102_store_reg);
  1138. static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
  1139. sn9c102_show_val, sn9c102_store_val);
  1140. static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
  1141. sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
  1142. static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
  1143. sn9c102_show_i2c_val, sn9c102_store_i2c_val);
  1144. static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
  1145. static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
  1146. static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
  1147. static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
  1148. sn9c102_show_frame_header, NULL);
  1149. static int sn9c102_create_sysfs(struct sn9c102_device* cam)
  1150. {
  1151. struct video_device *v4ldev = cam->v4ldev;
  1152. int err = 0;
  1153. if ((err = video_device_create_file(v4ldev, &class_device_attr_reg)))
  1154. goto err_out;
  1155. if ((err = video_device_create_file(v4ldev, &class_device_attr_val)))
  1156. goto err_reg;
  1157. if ((err = video_device_create_file(v4ldev,
  1158. &class_device_attr_frame_header)))
  1159. goto err_val;
  1160. if (cam->sensor.sysfs_ops) {
  1161. if ((err = video_device_create_file(v4ldev,
  1162. &class_device_attr_i2c_reg)))
  1163. goto err_frame_header;
  1164. if ((err = video_device_create_file(v4ldev,
  1165. &class_device_attr_i2c_val)))
  1166. goto err_i2c_reg;
  1167. }
  1168. if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
  1169. if ((err = video_device_create_file(v4ldev,
  1170. &class_device_attr_green)))
  1171. goto err_i2c_val;
  1172. } else {
  1173. if ((err = video_device_create_file(v4ldev,
  1174. &class_device_attr_blue)))
  1175. goto err_i2c_val;
  1176. if ((err = video_device_create_file(v4ldev,
  1177. &class_device_attr_red)))
  1178. goto err_blue;
  1179. }
  1180. return 0;
  1181. err_blue:
  1182. video_device_remove_file(v4ldev, &class_device_attr_blue);
  1183. err_i2c_val:
  1184. if (cam->sensor.sysfs_ops)
  1185. video_device_remove_file(v4ldev, &class_device_attr_i2c_val);
  1186. err_i2c_reg:
  1187. if (cam->sensor.sysfs_ops)
  1188. video_device_remove_file(v4ldev, &class_device_attr_i2c_reg);
  1189. err_frame_header:
  1190. video_device_remove_file(v4ldev, &class_device_attr_frame_header);
  1191. err_val:
  1192. video_device_remove_file(v4ldev, &class_device_attr_val);
  1193. err_reg:
  1194. video_device_remove_file(v4ldev, &class_device_attr_reg);
  1195. err_out:
  1196. return err;
  1197. }
  1198. #endif /* CONFIG_VIDEO_ADV_DEBUG */
  1199. /*****************************************************************************/
  1200. static int
  1201. sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
  1202. {
  1203. int err = 0;
  1204. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
  1205. pix->pixelformat == V4L2_PIX_FMT_JPEG) {
  1206. switch (cam->bridge) {
  1207. case BRIDGE_SN9C101:
  1208. case BRIDGE_SN9C102:
  1209. case BRIDGE_SN9C103:
  1210. err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
  1211. 0x18);
  1212. break;
  1213. case BRIDGE_SN9C105:
  1214. case BRIDGE_SN9C120:
  1215. err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
  1216. 0x18);
  1217. break;
  1218. }
  1219. } else {
  1220. switch (cam->bridge) {
  1221. case BRIDGE_SN9C101:
  1222. case BRIDGE_SN9C102:
  1223. case BRIDGE_SN9C103:
  1224. err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
  1225. 0x18);
  1226. break;
  1227. case BRIDGE_SN9C105:
  1228. case BRIDGE_SN9C120:
  1229. err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
  1230. 0x18);
  1231. break;
  1232. }
  1233. }
  1234. return err ? -EIO : 0;
  1235. }
  1236. static int
  1237. sn9c102_set_compression(struct sn9c102_device* cam,
  1238. struct v4l2_jpegcompression* compression)
  1239. {
  1240. int i, err = 0;
  1241. switch (cam->bridge) {
  1242. case BRIDGE_SN9C101:
  1243. case BRIDGE_SN9C102:
  1244. case BRIDGE_SN9C103:
  1245. if (compression->quality == 0)
  1246. err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01,
  1247. 0x17);
  1248. else if (compression->quality == 1)
  1249. err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe,
  1250. 0x17);
  1251. break;
  1252. case BRIDGE_SN9C105:
  1253. case BRIDGE_SN9C120:
  1254. if (compression->quality == 0) {
  1255. for (i = 0; i <= 63; i++) {
  1256. err += sn9c102_write_reg(cam,
  1257. SN9C102_Y_QTABLE1[i],
  1258. 0x100 + i);
  1259. err += sn9c102_write_reg(cam,
  1260. SN9C102_UV_QTABLE1[i],
  1261. 0x140 + i);
  1262. }
  1263. err += sn9c102_write_reg(cam, cam->reg[0x18] & 0xbf,
  1264. 0x18);
  1265. } else if (compression->quality == 1) {
  1266. for (i = 0; i <= 63; i++) {
  1267. err += sn9c102_write_reg(cam,
  1268. SN9C102_Y_QTABLE1[i],
  1269. 0x100 + i);
  1270. err += sn9c102_write_reg(cam,
  1271. SN9C102_UV_QTABLE1[i],
  1272. 0x140 + i);
  1273. }
  1274. err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x40,
  1275. 0x18);
  1276. }
  1277. break;
  1278. }
  1279. return err ? -EIO : 0;
  1280. }
  1281. static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
  1282. {
  1283. u8 r = 0;
  1284. int err = 0;
  1285. if (scale == 1)
  1286. r = cam->reg[0x18] & 0xcf;
  1287. else if (scale == 2) {
  1288. r = cam->reg[0x18] & 0xcf;
  1289. r |= 0x10;
  1290. } else if (scale == 4)
  1291. r = cam->reg[0x18] | 0x20;
  1292. err += sn9c102_write_reg(cam, r, 0x18);
  1293. if (err)
  1294. return -EIO;
  1295. PDBGG("Scaling factor: %u", scale);
  1296. return 0;
  1297. }
  1298. static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
  1299. {
  1300. struct sn9c102_sensor* s = &cam->sensor;
  1301. u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
  1302. v_start = (u8)(rect->top - s->cropcap.bounds.top),
  1303. h_size = (u8)(rect->width / 16),
  1304. v_size = (u8)(rect->height / 16);
  1305. int err = 0;
  1306. err += sn9c102_write_reg(cam, h_start, 0x12);
  1307. err += sn9c102_write_reg(cam, v_start, 0x13);
  1308. err += sn9c102_write_reg(cam, h_size, 0x15);
  1309. err += sn9c102_write_reg(cam, v_size, 0x16);
  1310. if (err)
  1311. return -EIO;
  1312. PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
  1313. "%u %u %u %u", h_start, v_start, h_size, v_size);
  1314. return 0;
  1315. }
  1316. static int sn9c102_init(struct sn9c102_device* cam)
  1317. {
  1318. struct sn9c102_sensor* s = &cam->sensor;
  1319. struct v4l2_control ctrl;
  1320. struct v4l2_queryctrl *qctrl;
  1321. struct v4l2_rect* rect;
  1322. u8 i = 0;
  1323. int err = 0;
  1324. if (!(cam->state & DEV_INITIALIZED)) {
  1325. init_waitqueue_head(&cam->open);
  1326. qctrl = s->qctrl;
  1327. rect = &(s->cropcap.defrect);
  1328. } else { /* use current values */
  1329. qctrl = s->_qctrl;
  1330. rect = &(s->_rect);
  1331. }
  1332. err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
  1333. err += sn9c102_set_crop(cam, rect);
  1334. if (err)
  1335. return err;
  1336. if (s->init) {
  1337. err = s->init(cam);
  1338. if (err) {
  1339. DBG(3, "Sensor initialization failed");
  1340. return err;
  1341. }
  1342. }
  1343. if (!(cam->state & DEV_INITIALIZED))
  1344. if (cam->bridge == BRIDGE_SN9C101 ||
  1345. cam->bridge == BRIDGE_SN9C102 ||
  1346. cam->bridge == BRIDGE_SN9C103) {
  1347. if (s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
  1348. s->pix_format.pixelformat= V4L2_PIX_FMT_SBGGR8;
  1349. cam->compression.quality = cam->reg[0x17] & 0x01 ?
  1350. 0 : 1;
  1351. } else {
  1352. if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
  1353. s->pix_format.pixelformat = V4L2_PIX_FMT_JPEG;
  1354. cam->compression.quality = cam->reg[0x18] & 0x40 ?
  1355. 0 : 1;
  1356. err += sn9c102_set_compression(cam, &cam->compression);
  1357. }
  1358. else
  1359. err += sn9c102_set_compression(cam, &cam->compression);
  1360. err += sn9c102_set_pix_format(cam, &s->pix_format);
  1361. if (s->set_pix_format)
  1362. err += s->set_pix_format(cam, &s->pix_format);
  1363. if (err)
  1364. return err;
  1365. if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
  1366. s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
  1367. DBG(3, "Compressed video format is active, quality %d",
  1368. cam->compression.quality);
  1369. else
  1370. DBG(3, "Uncompressed video format is active");
  1371. if (s->set_crop)
  1372. if ((err = s->set_crop(cam, rect))) {
  1373. DBG(3, "set_crop() failed");
  1374. return err;
  1375. }
  1376. if (s->set_ctrl) {
  1377. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1378. if (s->qctrl[i].id != 0 &&
  1379. !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
  1380. ctrl.id = s->qctrl[i].id;
  1381. ctrl.value = qctrl[i].default_value;
  1382. err = s->set_ctrl(cam, &ctrl);
  1383. if (err) {
  1384. DBG(3, "Set %s control failed",
  1385. s->qctrl[i].name);
  1386. return err;
  1387. }
  1388. DBG(3, "Image sensor supports '%s' control",
  1389. s->qctrl[i].name);
  1390. }
  1391. }
  1392. if (!(cam->state & DEV_INITIALIZED)) {
  1393. mutex_init(&cam->fileop_mutex);
  1394. spin_lock_init(&cam->queue_lock);
  1395. init_waitqueue_head(&cam->wait_frame);
  1396. init_waitqueue_head(&cam->wait_stream);
  1397. cam->nreadbuffers = 2;
  1398. memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
  1399. memcpy(&(s->_rect), &(s->cropcap.defrect),
  1400. sizeof(struct v4l2_rect));
  1401. cam->state |= DEV_INITIALIZED;
  1402. }
  1403. DBG(2, "Initialization succeeded");
  1404. return 0;
  1405. }
  1406. static void sn9c102_release_resources(struct sn9c102_device* cam)
  1407. {
  1408. mutex_lock(&sn9c102_sysfs_lock);
  1409. DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
  1410. video_set_drvdata(cam->v4ldev, NULL);
  1411. video_unregister_device(cam->v4ldev);
  1412. mutex_unlock(&sn9c102_sysfs_lock);
  1413. kfree(cam->control_buffer);
  1414. }
  1415. /*****************************************************************************/
  1416. static int sn9c102_open(struct inode* inode, struct file* filp)
  1417. {
  1418. struct sn9c102_device* cam;
  1419. int err = 0;
  1420. /*
  1421. This is the only safe way to prevent race conditions with
  1422. disconnect
  1423. */
  1424. if (!down_read_trylock(&sn9c102_disconnect))
  1425. return -ERESTARTSYS;
  1426. cam = video_get_drvdata(video_devdata(filp));
  1427. if (mutex_lock_interruptible(&cam->dev_mutex)) {
  1428. up_read(&sn9c102_disconnect);
  1429. return -ERESTARTSYS;
  1430. }
  1431. if (cam->users) {
  1432. DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
  1433. DBG(3, "Simultaneous opens are not supported");
  1434. if ((filp->f_flags & O_NONBLOCK) ||
  1435. (filp->f_flags & O_NDELAY)) {
  1436. err = -EWOULDBLOCK;
  1437. goto out;
  1438. }
  1439. mutex_unlock(&cam->dev_mutex);
  1440. err = wait_event_interruptible_exclusive(cam->open,
  1441. cam->state & DEV_DISCONNECTED
  1442. || !cam->users);
  1443. if (err) {
  1444. up_read(&sn9c102_disconnect);
  1445. return err;
  1446. }
  1447. if (cam->state & DEV_DISCONNECTED) {
  1448. up_read(&sn9c102_disconnect);
  1449. return -ENODEV;
  1450. }
  1451. mutex_lock(&cam->dev_mutex);
  1452. }
  1453. if (cam->state & DEV_MISCONFIGURED) {
  1454. err = sn9c102_init(cam);
  1455. if (err) {
  1456. DBG(1, "Initialization failed again. "
  1457. "I will retry on next open().");
  1458. goto out;
  1459. }
  1460. cam->state &= ~DEV_MISCONFIGURED;
  1461. }
  1462. if ((err = sn9c102_start_transfer(cam)))
  1463. goto out;
  1464. filp->private_data = cam;
  1465. cam->users++;
  1466. cam->io = IO_NONE;
  1467. cam->stream = STREAM_OFF;
  1468. cam->nbuffers = 0;
  1469. cam->frame_count = 0;
  1470. sn9c102_empty_framequeues(cam);
  1471. DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
  1472. out:
  1473. mutex_unlock(&cam->dev_mutex);
  1474. up_read(&sn9c102_disconnect);
  1475. return err;
  1476. }
  1477. static int sn9c102_release(struct inode* inode, struct file* filp)
  1478. {
  1479. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  1480. mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
  1481. sn9c102_stop_transfer(cam);
  1482. sn9c102_release_buffers(cam);
  1483. if (cam->state & DEV_DISCONNECTED) {
  1484. sn9c102_release_resources(cam);
  1485. usb_put_dev(cam->usbdev);
  1486. mutex_unlock(&cam->dev_mutex);
  1487. kfree(cam);
  1488. return 0;
  1489. }
  1490. cam->users--;
  1491. wake_up_interruptible_nr(&cam->open, 1);
  1492. DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
  1493. mutex_unlock(&cam->dev_mutex);
  1494. return 0;
  1495. }
  1496. static ssize_t
  1497. sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
  1498. {
  1499. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  1500. struct sn9c102_frame_t* f, * i;
  1501. unsigned long lock_flags;
  1502. long timeout;
  1503. int err = 0;
  1504. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1505. return -ERESTARTSYS;
  1506. if (cam->state & DEV_DISCONNECTED) {
  1507. DBG(1, "Device not present");
  1508. mutex_unlock(&cam->fileop_mutex);
  1509. return -ENODEV;
  1510. }
  1511. if (cam->state & DEV_MISCONFIGURED) {
  1512. DBG(1, "The camera is misconfigured. Close and open it "
  1513. "again.");
  1514. mutex_unlock(&cam->fileop_mutex);
  1515. return -EIO;
  1516. }
  1517. if (cam->io == IO_MMAP) {
  1518. DBG(3, "Close and open the device again to choose "
  1519. "the read method");
  1520. mutex_unlock(&cam->fileop_mutex);
  1521. return -EBUSY;
  1522. }
  1523. if (cam->io == IO_NONE) {
  1524. if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
  1525. DBG(1, "read() failed, not enough memory");
  1526. mutex_unlock(&cam->fileop_mutex);
  1527. return -ENOMEM;
  1528. }
  1529. cam->io = IO_READ;
  1530. cam->stream = STREAM_ON;
  1531. }
  1532. if (list_empty(&cam->inqueue)) {
  1533. if (!list_empty(&cam->outqueue))
  1534. sn9c102_empty_framequeues(cam);
  1535. sn9c102_queue_unusedframes(cam);
  1536. }
  1537. if (!count) {
  1538. mutex_unlock(&cam->fileop_mutex);
  1539. return 0;
  1540. }
  1541. if (list_empty(&cam->outqueue)) {
  1542. if (filp->f_flags & O_NONBLOCK) {
  1543. mutex_unlock(&cam->fileop_mutex);
  1544. return -EAGAIN;
  1545. }
  1546. if (!cam->module_param.frame_timeout) {
  1547. err = wait_event_interruptible
  1548. ( cam->wait_frame,
  1549. (!list_empty(&cam->outqueue)) ||
  1550. (cam->state & DEV_DISCONNECTED) ||
  1551. (cam->state & DEV_MISCONFIGURED) );
  1552. if (err) {
  1553. mutex_unlock(&cam->fileop_mutex);
  1554. return err;
  1555. }
  1556. } else {
  1557. timeout = wait_event_interruptible_timeout
  1558. ( cam->wait_frame,
  1559. (!list_empty(&cam->outqueue)) ||
  1560. (cam->state & DEV_DISCONNECTED) ||
  1561. (cam->state & DEV_MISCONFIGURED),
  1562. cam->module_param.frame_timeout *
  1563. 1000 * msecs_to_jiffies(1) );
  1564. if (timeout < 0) {
  1565. mutex_unlock(&cam->fileop_mutex);
  1566. return timeout;
  1567. } else if (timeout == 0 &&
  1568. !(cam->state & DEV_DISCONNECTED)) {
  1569. DBG(1, "Video frame timeout elapsed");
  1570. mutex_unlock(&cam->fileop_mutex);
  1571. return -EIO;
  1572. }
  1573. }
  1574. if (cam->state & DEV_DISCONNECTED) {
  1575. mutex_unlock(&cam->fileop_mutex);
  1576. return -ENODEV;
  1577. }
  1578. if (cam->state & DEV_MISCONFIGURED) {
  1579. mutex_unlock(&cam->fileop_mutex);
  1580. return -EIO;
  1581. }
  1582. }
  1583. f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
  1584. if (count > f->buf.bytesused)
  1585. count = f->buf.bytesused;
  1586. if (copy_to_user(buf, f->bufmem, count)) {
  1587. err = -EFAULT;
  1588. goto exit;
  1589. }
  1590. *f_pos += count;
  1591. exit:
  1592. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1593. list_for_each_entry(i, &cam->outqueue, frame)
  1594. i->state = F_UNUSED;
  1595. INIT_LIST_HEAD(&cam->outqueue);
  1596. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1597. sn9c102_queue_unusedframes(cam);
  1598. PDBGG("Frame #%lu, bytes read: %zu",
  1599. (unsigned long)f->buf.index, count);
  1600. mutex_unlock(&cam->fileop_mutex);
  1601. return count;
  1602. }
  1603. static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
  1604. {
  1605. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  1606. struct sn9c102_frame_t* f;
  1607. unsigned long lock_flags;
  1608. unsigned int mask = 0;
  1609. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1610. return POLLERR;
  1611. if (cam->state & DEV_DISCONNECTED) {
  1612. DBG(1, "Device not present");
  1613. goto error;
  1614. }
  1615. if (cam->state & DEV_MISCONFIGURED) {
  1616. DBG(1, "The camera is misconfigured. Close and open it "
  1617. "again.");
  1618. goto error;
  1619. }
  1620. if (cam->io == IO_NONE) {
  1621. if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
  1622. IO_READ)) {
  1623. DBG(1, "poll() failed, not enough memory");
  1624. goto error;
  1625. }
  1626. cam->io = IO_READ;
  1627. cam->stream = STREAM_ON;
  1628. }
  1629. if (cam->io == IO_READ) {
  1630. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1631. list_for_each_entry(f, &cam->outqueue, frame)
  1632. f->state = F_UNUSED;
  1633. INIT_LIST_HEAD(&cam->outqueue);
  1634. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1635. sn9c102_queue_unusedframes(cam);
  1636. }
  1637. poll_wait(filp, &cam->wait_frame, wait);
  1638. if (!list_empty(&cam->outqueue))
  1639. mask |= POLLIN | POLLRDNORM;
  1640. mutex_unlock(&cam->fileop_mutex);
  1641. return mask;
  1642. error:
  1643. mutex_unlock(&cam->fileop_mutex);
  1644. return POLLERR;
  1645. }
  1646. static void sn9c102_vm_open(struct vm_area_struct* vma)
  1647. {
  1648. struct sn9c102_frame_t* f = vma->vm_private_data;
  1649. f->vma_use_count++;
  1650. }
  1651. static void sn9c102_vm_close(struct vm_area_struct* vma)
  1652. {
  1653. /* NOTE: buffers are not freed here */
  1654. struct sn9c102_frame_t* f = vma->vm_private_data;
  1655. f->vma_use_count--;
  1656. }
  1657. static struct vm_operations_struct sn9c102_vm_ops = {
  1658. .open = sn9c102_vm_open,
  1659. .close = sn9c102_vm_close,
  1660. };
  1661. static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
  1662. {
  1663. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  1664. unsigned long size = vma->vm_end - vma->vm_start,
  1665. start = vma->vm_start;
  1666. void *pos;
  1667. u32 i;
  1668. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1669. return -ERESTARTSYS;
  1670. if (cam->state & DEV_DISCONNECTED) {
  1671. DBG(1, "Device not present");
  1672. mutex_unlock(&cam->fileop_mutex);
  1673. return -ENODEV;
  1674. }
  1675. if (cam->state & DEV_MISCONFIGURED) {
  1676. DBG(1, "The camera is misconfigured. Close and open it "
  1677. "again.");
  1678. mutex_unlock(&cam->fileop_mutex);
  1679. return -EIO;
  1680. }
  1681. if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
  1682. mutex_unlock(&cam->fileop_mutex);
  1683. return -EACCES;
  1684. }
  1685. if (cam->io != IO_MMAP ||
  1686. size != PAGE_ALIGN(cam->frame[0].buf.length)) {
  1687. mutex_unlock(&cam->fileop_mutex);
  1688. return -EINVAL;
  1689. }
  1690. for (i = 0; i < cam->nbuffers; i++) {
  1691. if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
  1692. break;
  1693. }
  1694. if (i == cam->nbuffers) {
  1695. mutex_unlock(&cam->fileop_mutex);
  1696. return -EINVAL;
  1697. }
  1698. vma->vm_flags |= VM_IO;
  1699. vma->vm_flags |= VM_RESERVED;
  1700. pos = cam->frame[i].bufmem;
  1701. while (size > 0) { /* size is page-aligned */
  1702. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  1703. mutex_unlock(&cam->fileop_mutex);
  1704. return -EAGAIN;
  1705. }
  1706. start += PAGE_SIZE;
  1707. pos += PAGE_SIZE;
  1708. size -= PAGE_SIZE;
  1709. }
  1710. vma->vm_ops = &sn9c102_vm_ops;
  1711. vma->vm_private_data = &cam->frame[i];
  1712. sn9c102_vm_open(vma);
  1713. mutex_unlock(&cam->fileop_mutex);
  1714. return 0;
  1715. }
  1716. /*****************************************************************************/
  1717. static int
  1718. sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
  1719. {
  1720. struct v4l2_capability cap = {
  1721. .driver = "sn9c102",
  1722. .version = SN9C102_MODULE_VERSION_CODE,
  1723. .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  1724. V4L2_CAP_STREAMING,
  1725. };
  1726. strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
  1727. if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
  1728. strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
  1729. sizeof(cap.bus_info));
  1730. if (copy_to_user(arg, &cap, sizeof(cap)))
  1731. return -EFAULT;
  1732. return 0;
  1733. }
  1734. static int
  1735. sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
  1736. {
  1737. struct v4l2_input i;
  1738. if (copy_from_user(&i, arg, sizeof(i)))
  1739. return -EFAULT;
  1740. if (i.index)
  1741. return -EINVAL;
  1742. memset(&i, 0, sizeof(i));
  1743. strcpy(i.name, "Camera");
  1744. i.type = V4L2_INPUT_TYPE_CAMERA;
  1745. if (copy_to_user(arg, &i, sizeof(i)))
  1746. return -EFAULT;
  1747. return 0;
  1748. }
  1749. static int
  1750. sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
  1751. {
  1752. int index = 0;
  1753. if (copy_to_user(arg, &index, sizeof(index)))
  1754. return -EFAULT;
  1755. return 0;
  1756. }
  1757. static int
  1758. sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
  1759. {
  1760. int index;
  1761. if (copy_from_user(&index, arg, sizeof(index)))
  1762. return -EFAULT;
  1763. if (index != 0)
  1764. return -EINVAL;
  1765. return 0;
  1766. }
  1767. static int
  1768. sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
  1769. {
  1770. struct sn9c102_sensor* s = &cam->sensor;
  1771. struct v4l2_queryctrl qc;
  1772. u8 i;
  1773. if (copy_from_user(&qc, arg, sizeof(qc)))
  1774. return -EFAULT;
  1775. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1776. if (qc.id && qc.id == s->qctrl[i].id) {
  1777. memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
  1778. if (copy_to_user(arg, &qc, sizeof(qc)))
  1779. return -EFAULT;
  1780. return 0;
  1781. }
  1782. return -EINVAL;
  1783. }
  1784. static int
  1785. sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
  1786. {
  1787. struct sn9c102_sensor* s = &cam->sensor;
  1788. struct v4l2_control ctrl;
  1789. int err = 0;
  1790. u8 i;
  1791. if (!s->get_ctrl && !s->set_ctrl)
  1792. return -EINVAL;
  1793. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1794. return -EFAULT;
  1795. if (!s->get_ctrl) {
  1796. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1797. if (ctrl.id && ctrl.id == s->qctrl[i].id) {
  1798. ctrl.value = s->_qctrl[i].default_value;
  1799. goto exit;
  1800. }
  1801. return -EINVAL;
  1802. } else
  1803. err = s->get_ctrl(cam, &ctrl);
  1804. exit:
  1805. if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
  1806. return -EFAULT;
  1807. PDBGG("VIDIOC_G_CTRL: id %lu, value %lu",
  1808. (unsigned long)ctrl.id, (unsigned long)ctrl.value);
  1809. return err;
  1810. }
  1811. static int
  1812. sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
  1813. {
  1814. struct sn9c102_sensor* s = &cam->sensor;
  1815. struct v4l2_control ctrl;
  1816. u8 i;
  1817. int err = 0;
  1818. if (!s->set_ctrl)
  1819. return -EINVAL;
  1820. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1821. return -EFAULT;
  1822. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1823. if (ctrl.id == s->qctrl[i].id) {
  1824. if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
  1825. return -EINVAL;
  1826. if (ctrl.value < s->qctrl[i].minimum ||
  1827. ctrl.value > s->qctrl[i].maximum)
  1828. return -ERANGE;
  1829. ctrl.value -= ctrl.value % s->qctrl[i].step;
  1830. break;
  1831. }
  1832. if ((err = s->set_ctrl(cam, &ctrl)))
  1833. return err;
  1834. s->_qctrl[i].default_value = ctrl.value;
  1835. PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
  1836. (unsigned long)ctrl.id, (unsigned long)ctrl.value);
  1837. return 0;
  1838. }
  1839. static int
  1840. sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
  1841. {
  1842. struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
  1843. cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1844. cc->pixelaspect.numerator = 1;
  1845. cc->pixelaspect.denominator = 1;
  1846. if (copy_to_user(arg, cc, sizeof(*cc)))
  1847. return -EFAULT;
  1848. return 0;
  1849. }
  1850. static int
  1851. sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
  1852. {
  1853. struct sn9c102_sensor* s = &cam->sensor;
  1854. struct v4l2_crop crop = {
  1855. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1856. };
  1857. memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
  1858. if (copy_to_user(arg, &crop, sizeof(crop)))
  1859. return -EFAULT;
  1860. return 0;
  1861. }
  1862. static int
  1863. sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
  1864. {
  1865. struct sn9c102_sensor* s = &cam->sensor;
  1866. struct v4l2_crop crop;
  1867. struct v4l2_rect* rect;
  1868. struct v4l2_rect* bounds = &(s->cropcap.bounds);
  1869. struct v4l2_pix_format* pix_format = &(s->pix_format);
  1870. u8 scale;
  1871. const enum sn9c102_stream_state stream = cam->stream;
  1872. const u32 nbuffers = cam->nbuffers;
  1873. u32 i;
  1874. int err = 0;
  1875. if (copy_from_user(&crop, arg, sizeof(crop)))
  1876. return -EFAULT;
  1877. rect = &(crop.c);
  1878. if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1879. return -EINVAL;
  1880. if (cam->module_param.force_munmap)
  1881. for (i = 0; i < cam->nbuffers; i++)
  1882. if (cam->frame[i].vma_use_count) {
  1883. DBG(3, "VIDIOC_S_CROP failed. "
  1884. "Unmap the buffers first.");
  1885. return -EBUSY;
  1886. }
  1887. /* Preserve R,G or B origin */
  1888. rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
  1889. rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
  1890. if (rect->width < 16)
  1891. rect->width = 16;
  1892. if (rect->height < 16)
  1893. rect->height = 16;
  1894. if (rect->width > bounds->width)
  1895. rect->width = bounds->width;
  1896. if (rect->height > bounds->height)
  1897. rect->height = bounds->height;
  1898. if (rect->left < bounds->left)
  1899. rect->left = bounds->left;
  1900. if (rect->top < bounds->top)
  1901. rect->top = bounds->top;
  1902. if (rect->left + rect->width > bounds->left + bounds->width)
  1903. rect->left = bounds->left+bounds->width - rect->width;
  1904. if (rect->top + rect->height > bounds->top + bounds->height)
  1905. rect->top = bounds->top+bounds->height - rect->height;
  1906. rect->width &= ~15L;
  1907. rect->height &= ~15L;
  1908. if (SN9C102_PRESERVE_IMGSCALE) {
  1909. /* Calculate the actual scaling factor */
  1910. u32 a, b;
  1911. a = rect->width * rect->height;
  1912. b = pix_format->width * pix_format->height;
  1913. scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
  1914. } else
  1915. scale = 1;
  1916. if (cam->stream == STREAM_ON)
  1917. if ((err = sn9c102_stream_interrupt(cam)))
  1918. return err;
  1919. if (copy_to_user(arg, &crop, sizeof(crop))) {
  1920. cam->stream = stream;
  1921. return -EFAULT;
  1922. }
  1923. if (cam->module_param.force_munmap || cam->io == IO_READ)
  1924. sn9c102_release_buffers(cam);
  1925. err = sn9c102_set_crop(cam, rect);
  1926. if (s->set_crop)
  1927. err += s->set_crop(cam, rect);
  1928. err += sn9c102_set_scale(cam, scale);
  1929. if (err) { /* atomic, no rollback in ioctl() */
  1930. cam->state |= DEV_MISCONFIGURED;
  1931. DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
  1932. "use the camera, close and open /dev/video%d again.",
  1933. cam->v4ldev->minor);
  1934. return -EIO;
  1935. }
  1936. s->pix_format.width = rect->width/scale;
  1937. s->pix_format.height = rect->height/scale;
  1938. memcpy(&(s->_rect), rect, sizeof(*rect));
  1939. if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
  1940. nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
  1941. cam->state |= DEV_MISCONFIGURED;
  1942. DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
  1943. "use the camera, close and open /dev/video%d again.",
  1944. cam->v4ldev->minor);
  1945. return -ENOMEM;
  1946. }
  1947. if (cam->io == IO_READ)
  1948. sn9c102_empty_framequeues(cam);
  1949. else if (cam->module_param.force_munmap)
  1950. sn9c102_requeue_outqueue(cam);
  1951. cam->stream = stream;
  1952. return 0;
  1953. }
  1954. static int
  1955. sn9c102_vidioc_enum_framesizes(struct sn9c102_device* cam, void __user * arg)
  1956. {
  1957. struct v4l2_frmsizeenum frmsize;
  1958. if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
  1959. return -EFAULT;
  1960. if (frmsize.index != 0)
  1961. return -EINVAL;
  1962. switch (cam->bridge) {
  1963. case BRIDGE_SN9C101:
  1964. case BRIDGE_SN9C102:
  1965. case BRIDGE_SN9C103:
  1966. if (frmsize.pixel_format != V4L2_PIX_FMT_SN9C10X &&
  1967. frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
  1968. return -EINVAL;
  1969. case BRIDGE_SN9C105:
  1970. case BRIDGE_SN9C120:
  1971. if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG &&
  1972. frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
  1973. return -EINVAL;
  1974. }
  1975. frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1976. frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
  1977. frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
  1978. frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
  1979. frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
  1980. memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
  1981. if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
  1982. return -EFAULT;
  1983. return 0;
  1984. }
  1985. static int
  1986. sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
  1987. {
  1988. struct v4l2_fmtdesc fmtd;
  1989. if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
  1990. return -EFAULT;
  1991. if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1992. return -EINVAL;
  1993. if (fmtd.index == 0) {
  1994. strcpy(fmtd.description, "bayer rgb");
  1995. fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
  1996. } else if (fmtd.index == 1) {
  1997. switch (cam->bridge) {
  1998. case BRIDGE_SN9C101:
  1999. case BRIDGE_SN9C102:
  2000. case BRIDGE_SN9C103:
  2001. strcpy(fmtd.description, "compressed");
  2002. fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
  2003. break;
  2004. case BRIDGE_SN9C105:
  2005. case BRIDGE_SN9C120:
  2006. strcpy(fmtd.description, "JPEG");
  2007. fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
  2008. break;
  2009. }
  2010. fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
  2011. } else
  2012. return -EINVAL;
  2013. fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2014. memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
  2015. if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
  2016. return -EFAULT;
  2017. return 0;
  2018. }
  2019. static int
  2020. sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
  2021. {
  2022. struct v4l2_format format;
  2023. struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
  2024. if (copy_from_user(&format, arg, sizeof(format)))
  2025. return -EFAULT;
  2026. if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2027. return -EINVAL;
  2028. pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_JPEG) ?
  2029. V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
  2030. pfmt->bytesperline = (pfmt->pixelformat == V4L2_PIX_FMT_SN9C10X ||
  2031. pfmt->pixelformat == V4L2_PIX_FMT_JPEG)
  2032. ? 0 : (pfmt->width * pfmt->priv) / 8;
  2033. pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
  2034. pfmt->field = V4L2_FIELD_NONE;
  2035. memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
  2036. if (copy_to_user(arg, &format, sizeof(format)))
  2037. return -EFAULT;
  2038. return 0;
  2039. }
  2040. static int
  2041. sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
  2042. void __user * arg)
  2043. {
  2044. struct sn9c102_sensor* s = &cam->sensor;
  2045. struct v4l2_format format;
  2046. struct v4l2_pix_format* pix;
  2047. struct v4l2_pix_format* pfmt = &(s->pix_format);
  2048. struct v4l2_rect* bounds = &(s->cropcap.bounds);
  2049. struct v4l2_rect rect;
  2050. u8 scale;
  2051. const enum sn9c102_stream_state stream = cam->stream;
  2052. const u32 nbuffers = cam->nbuffers;
  2053. u32 i;
  2054. int err = 0;
  2055. if (copy_from_user(&format, arg, sizeof(format)))
  2056. return -EFAULT;
  2057. pix = &(format.fmt.pix);
  2058. if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2059. return -EINVAL;
  2060. memcpy(&rect, &(s->_rect), sizeof(rect));
  2061. { /* calculate the actual scaling factor */
  2062. u32 a, b;
  2063. a = rect.width * rect.height;
  2064. b = pix->width * pix->height;
  2065. scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
  2066. }
  2067. rect.width = scale * pix->width;
  2068. rect.height = scale * pix->height;
  2069. if (rect.width < 16)
  2070. rect.width = 16;
  2071. if (rect.height < 16)
  2072. rect.height = 16;
  2073. if (rect.width > bounds->left + bounds->width - rect.left)
  2074. rect.width = bounds->left + bounds->width - rect.left;
  2075. if (rect.height > bounds->top + bounds->height - rect.top)
  2076. rect.height = bounds->top + bounds->height - rect.top;
  2077. rect.width &= ~15L;
  2078. rect.height &= ~15L;
  2079. { /* adjust the scaling factor */
  2080. u32 a, b;
  2081. a = rect.width * rect.height;
  2082. b = pix->width * pix->height;
  2083. scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
  2084. }
  2085. pix->width = rect.width / scale;
  2086. pix->height = rect.height / scale;
  2087. switch (cam->bridge) {
  2088. case BRIDGE_SN9C101:
  2089. case BRIDGE_SN9C102:
  2090. case BRIDGE_SN9C103:
  2091. if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
  2092. pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
  2093. pix->pixelformat = pfmt->pixelformat;
  2094. break;
  2095. case BRIDGE_SN9C105:
  2096. case BRIDGE_SN9C120:
  2097. if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
  2098. pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
  2099. pix->pixelformat = pfmt->pixelformat;
  2100. break;
  2101. }
  2102. pix->priv = pfmt->priv; /* bpp */
  2103. pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_JPEG) ?
  2104. V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
  2105. pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
  2106. pix->pixelformat == V4L2_PIX_FMT_JPEG)
  2107. ? 0 : (pix->width * pix->priv) / 8;
  2108. pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
  2109. pix->field = V4L2_FIELD_NONE;
  2110. if (cmd == VIDIOC_TRY_FMT) {
  2111. if (copy_to_user(arg, &format, sizeof(format)))
  2112. return -EFAULT;
  2113. return 0;
  2114. }
  2115. if (cam->module_param.force_munmap)
  2116. for (i = 0; i < cam->nbuffers; i++)
  2117. if (cam->frame[i].vma_use_count) {
  2118. DBG(3, "VIDIOC_S_FMT failed. Unmap the "
  2119. "buffers first.");
  2120. return -EBUSY;
  2121. }
  2122. if (cam->stream == STREAM_ON)
  2123. if ((err = sn9c102_stream_interrupt(cam)))
  2124. return err;
  2125. if (copy_to_user(arg, &format, sizeof(format))) {
  2126. cam->stream = stream;
  2127. return -EFAULT;
  2128. }
  2129. if (cam->module_param.force_munmap || cam->io == IO_READ)
  2130. sn9c102_release_buffers(cam);
  2131. err += sn9c102_set_pix_format(cam, pix);
  2132. err += sn9c102_set_crop(cam, &rect);
  2133. if (s->set_pix_format)
  2134. err += s->set_pix_format(cam, pix);
  2135. if (s->set_crop)
  2136. err += s->set_crop(cam, &rect);
  2137. err += sn9c102_set_scale(cam, scale);
  2138. if (err) { /* atomic, no rollback in ioctl() */
  2139. cam->state |= DEV_MISCONFIGURED;
  2140. DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
  2141. "use the camera, close and open /dev/video%d again.",
  2142. cam->v4ldev->minor);
  2143. return -EIO;
  2144. }
  2145. memcpy(pfmt, pix, sizeof(*pix));
  2146. memcpy(&(s->_rect), &rect, sizeof(rect));
  2147. if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
  2148. nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
  2149. cam->state |= DEV_MISCONFIGURED;
  2150. DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
  2151. "use the camera, close and open /dev/video%d again.",
  2152. cam->v4ldev->minor);
  2153. return -ENOMEM;
  2154. }
  2155. if (cam->io == IO_READ)
  2156. sn9c102_empty_framequeues(cam);
  2157. else if (cam->module_param.force_munmap)
  2158. sn9c102_requeue_outqueue(cam);
  2159. cam->stream = stream;
  2160. return 0;
  2161. }
  2162. static int
  2163. sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
  2164. {
  2165. if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
  2166. return -EFAULT;
  2167. return 0;
  2168. }
  2169. static int
  2170. sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
  2171. {
  2172. struct v4l2_jpegcompression jc;
  2173. const enum sn9c102_stream_state stream = cam->stream;
  2174. int err = 0;
  2175. if (copy_from_user(&jc, arg, sizeof(jc)))
  2176. return -EFAULT;
  2177. if (jc.quality != 0 && jc.quality != 1)
  2178. return -EINVAL;
  2179. if (cam->stream == STREAM_ON)
  2180. if ((err = sn9c102_stream_interrupt(cam)))
  2181. return err;
  2182. err += sn9c102_set_compression(cam, &jc);
  2183. if (err) { /* atomic, no rollback in ioctl() */
  2184. cam->state |= DEV_MISCONFIGURED;
  2185. DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
  2186. "problems. To use the camera, close and open "
  2187. "/dev/video%d again.", cam->v4ldev->minor);
  2188. return -EIO;
  2189. }
  2190. cam->compression.quality = jc.quality;
  2191. cam->stream = stream;
  2192. return 0;
  2193. }
  2194. static int
  2195. sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
  2196. {
  2197. struct v4l2_requestbuffers rb;
  2198. u32 i;
  2199. int err;
  2200. if (copy_from_user(&rb, arg, sizeof(rb)))
  2201. return -EFAULT;
  2202. if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2203. rb.memory != V4L2_MEMORY_MMAP)
  2204. return -EINVAL;
  2205. if (cam->io == IO_READ) {
  2206. DBG(3, "Close and open the device again to choose the mmap "
  2207. "I/O method");
  2208. return -EBUSY;
  2209. }
  2210. for (i = 0; i < cam->nbuffers; i++)
  2211. if (cam->frame[i].vma_use_count) {
  2212. DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
  2213. "still mapped.");
  2214. return -EBUSY;
  2215. }
  2216. if (cam->stream == STREAM_ON)
  2217. if ((err = sn9c102_stream_interrupt(cam)))
  2218. return err;
  2219. sn9c102_empty_framequeues(cam);
  2220. sn9c102_release_buffers(cam);
  2221. if (rb.count)
  2222. rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
  2223. if (copy_to_user(arg, &rb, sizeof(rb))) {
  2224. sn9c102_release_buffers(cam);
  2225. cam->io = IO_NONE;
  2226. return -EFAULT;
  2227. }
  2228. cam->io = rb.count ? IO_MMAP : IO_NONE;
  2229. return 0;
  2230. }
  2231. static int
  2232. sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
  2233. {
  2234. struct v4l2_buffer b;
  2235. if (copy_from_user(&b, arg, sizeof(b)))
  2236. return -EFAULT;
  2237. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2238. b.index >= cam->nbuffers || cam->io != IO_MMAP)
  2239. return -EINVAL;
  2240. memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
  2241. if (cam->frame[b.index].vma_use_count)
  2242. b.flags |= V4L2_BUF_FLAG_MAPPED;
  2243. if (cam->frame[b.index].state == F_DONE)
  2244. b.flags |= V4L2_BUF_FLAG_DONE;
  2245. else if (cam->frame[b.index].state != F_UNUSED)
  2246. b.flags |= V4L2_BUF_FLAG_QUEUED;
  2247. if (copy_to_user(arg, &b, sizeof(b)))
  2248. return -EFAULT;
  2249. return 0;
  2250. }
  2251. static int
  2252. sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
  2253. {
  2254. struct v4l2_buffer b;
  2255. unsigned long lock_flags;
  2256. if (copy_from_user(&b, arg, sizeof(b)))
  2257. return -EFAULT;
  2258. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2259. b.index >= cam->nbuffers || cam->io != IO_MMAP)
  2260. return -EINVAL;
  2261. if (cam->frame[b.index].state != F_UNUSED)
  2262. return -EINVAL;
  2263. cam->frame[b.index].state = F_QUEUED;
  2264. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  2265. list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
  2266. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  2267. PDBGG("Frame #%lu queued", (unsigned long)b.index);
  2268. return 0;
  2269. }
  2270. static int
  2271. sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
  2272. void __user * arg)
  2273. {
  2274. struct v4l2_buffer b;
  2275. struct sn9c102_frame_t *f;
  2276. unsigned long lock_flags;
  2277. long timeout;
  2278. int err = 0;
  2279. if (copy_from_user(&b, arg, sizeof(b)))
  2280. return -EFAULT;
  2281. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
  2282. return -EINVAL;
  2283. if (list_empty(&cam->outqueue)) {
  2284. if (cam->stream == STREAM_OFF)
  2285. return -EINVAL;
  2286. if (filp->f_flags & O_NONBLOCK)
  2287. return -EAGAIN;
  2288. if (!cam->module_param.frame_timeout) {
  2289. err = wait_event_interruptible
  2290. ( cam->wait_frame,
  2291. (!list_empty(&cam->outqueue)) ||
  2292. (cam->state & DEV_DISCONNECTED) ||
  2293. (cam->state & DEV_MISCONFIGURED) );
  2294. if (err)
  2295. return err;
  2296. } else {
  2297. timeout = wait_event_interruptible_timeout
  2298. ( cam->wait_frame,
  2299. (!list_empty(&cam->outqueue)) ||
  2300. (cam->state & DEV_DISCONNECTED) ||
  2301. (cam->state & DEV_MISCONFIGURED),
  2302. cam->module_param.frame_timeout *
  2303. 1000 * msecs_to_jiffies(1) );
  2304. if (timeout < 0)
  2305. return timeout;
  2306. else if (timeout == 0 &&
  2307. !(cam->state & DEV_DISCONNECTED)) {
  2308. DBG(1, "Video frame timeout elapsed");
  2309. return -EIO;
  2310. }
  2311. }
  2312. if (cam->state & DEV_DISCONNECTED)
  2313. return -ENODEV;
  2314. if (cam->state & DEV_MISCONFIGURED)
  2315. return -EIO;
  2316. }
  2317. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  2318. f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
  2319. list_del(cam->outqueue.next);
  2320. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  2321. f->state = F_UNUSED;
  2322. memcpy(&b, &f->buf, sizeof(b));
  2323. if (f->vma_use_count)
  2324. b.flags |= V4L2_BUF_FLAG_MAPPED;
  2325. if (copy_to_user(arg, &b, sizeof(b)))
  2326. return -EFAULT;
  2327. PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
  2328. return 0;
  2329. }
  2330. static int
  2331. sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
  2332. {
  2333. int type;
  2334. if (copy_from_user(&type, arg, sizeof(type)))
  2335. return -EFAULT;
  2336. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
  2337. return -EINVAL;
  2338. cam->stream = STREAM_ON;
  2339. DBG(3, "Stream on");
  2340. return 0;
  2341. }
  2342. static int
  2343. sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
  2344. {
  2345. int type, err;
  2346. if (copy_from_user(&type, arg, sizeof(type)))
  2347. return -EFAULT;
  2348. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
  2349. return -EINVAL;
  2350. if (cam->stream == STREAM_ON)
  2351. if ((err = sn9c102_stream_interrupt(cam)))
  2352. return err;
  2353. sn9c102_empty_framequeues(cam);
  2354. DBG(3, "Stream off");
  2355. return 0;
  2356. }
  2357. static int
  2358. sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
  2359. {
  2360. struct v4l2_streamparm sp;
  2361. if (copy_from_user(&sp, arg, sizeof(sp)))
  2362. return -EFAULT;
  2363. if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2364. return -EINVAL;
  2365. sp.parm.capture.extendedmode = 0;
  2366. sp.parm.capture.readbuffers = cam->nreadbuffers;
  2367. if (copy_to_user(arg, &sp, sizeof(sp)))
  2368. return -EFAULT;
  2369. return 0;
  2370. }
  2371. static int
  2372. sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
  2373. {
  2374. struct v4l2_streamparm sp;
  2375. if (copy_from_user(&sp, arg, sizeof(sp)))
  2376. return -EFAULT;
  2377. if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2378. return -EINVAL;
  2379. sp.parm.capture.extendedmode = 0;
  2380. if (sp.parm.capture.readbuffers == 0)
  2381. sp.parm.capture.readbuffers = cam->nreadbuffers;
  2382. if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
  2383. sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
  2384. if (copy_to_user(arg, &sp, sizeof(sp)))
  2385. return -EFAULT;
  2386. cam->nreadbuffers = sp.parm.capture.readbuffers;
  2387. return 0;
  2388. }
  2389. static int
  2390. sn9c102_vidioc_enumaudio(struct sn9c102_device* cam, void __user * arg)
  2391. {
  2392. struct v4l2_audio audio;
  2393. if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
  2394. return -EINVAL;
  2395. if (copy_from_user(&audio, arg, sizeof(audio)))
  2396. return -EFAULT;
  2397. if (audio.index != 0)
  2398. return -EINVAL;
  2399. strcpy(audio.name, "Microphone");
  2400. audio.capability = 0;
  2401. audio.mode = 0;
  2402. if (copy_to_user(arg, &audio, sizeof(audio)))
  2403. return -EFAULT;
  2404. return 0;
  2405. }
  2406. static int
  2407. sn9c102_vidioc_g_audio(struct sn9c102_device* cam, void __user * arg)
  2408. {
  2409. struct v4l2_audio audio;
  2410. if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
  2411. return -EINVAL;
  2412. if (copy_from_user(&audio, arg, sizeof(audio)))
  2413. return -EFAULT;
  2414. memset(&audio, 0, sizeof(audio));
  2415. strcpy(audio.name, "Microphone");
  2416. if (copy_to_user(arg, &audio, sizeof(audio)))
  2417. return -EFAULT;
  2418. return 0;
  2419. }
  2420. static int
  2421. sn9c102_vidioc_s_audio(struct sn9c102_device* cam, void __user * arg)
  2422. {
  2423. struct v4l2_audio audio;
  2424. if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
  2425. return -EINVAL;
  2426. if (copy_from_user(&audio, arg, sizeof(audio)))
  2427. return -EFAULT;
  2428. if (audio.index != 0)
  2429. return -EINVAL;
  2430. return 0;
  2431. }
  2432. static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
  2433. unsigned int cmd, void __user * arg)
  2434. {
  2435. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  2436. switch (cmd) {
  2437. case VIDIOC_QUERYCAP:
  2438. return sn9c102_vidioc_querycap(cam, arg);
  2439. case VIDIOC_ENUMINPUT:
  2440. return sn9c102_vidioc_enuminput(cam, arg);
  2441. case VIDIOC_G_INPUT:
  2442. return sn9c102_vidioc_g_input(cam, arg);
  2443. case VIDIOC_S_INPUT:
  2444. return sn9c102_vidioc_s_input(cam, arg);
  2445. case VIDIOC_QUERYCTRL:
  2446. return sn9c102_vidioc_query_ctrl(cam, arg);
  2447. case VIDIOC_G_CTRL:
  2448. return sn9c102_vidioc_g_ctrl(cam, arg);
  2449. case VIDIOC_S_CTRL:
  2450. return sn9c102_vidioc_s_ctrl(cam, arg);
  2451. case VIDIOC_CROPCAP:
  2452. return sn9c102_vidioc_cropcap(cam, arg);
  2453. case VIDIOC_G_CROP:
  2454. return sn9c102_vidioc_g_crop(cam, arg);
  2455. case VIDIOC_S_CROP:
  2456. return sn9c102_vidioc_s_crop(cam, arg);
  2457. case VIDIOC_ENUM_FRAMESIZES:
  2458. return sn9c102_vidioc_enum_framesizes(cam, arg);
  2459. case VIDIOC_ENUM_FMT:
  2460. return sn9c102_vidioc_enum_fmt(cam, arg);
  2461. case VIDIOC_G_FMT:
  2462. return sn9c102_vidioc_g_fmt(cam, arg);
  2463. case VIDIOC_TRY_FMT:
  2464. case VIDIOC_S_FMT:
  2465. return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
  2466. case VIDIOC_G_JPEGCOMP:
  2467. return sn9c102_vidioc_g_jpegcomp(cam, arg);
  2468. case VIDIOC_S_JPEGCOMP:
  2469. return sn9c102_vidioc_s_jpegcomp(cam, arg);
  2470. case VIDIOC_REQBUFS:
  2471. return sn9c102_vidioc_reqbufs(cam, arg);
  2472. case VIDIOC_QUERYBUF:
  2473. return sn9c102_vidioc_querybuf(cam, arg);
  2474. case VIDIOC_QBUF:
  2475. return sn9c102_vidioc_qbuf(cam, arg);
  2476. case VIDIOC_DQBUF:
  2477. return sn9c102_vidioc_dqbuf(cam, filp, arg);
  2478. case VIDIOC_STREAMON:
  2479. return sn9c102_vidioc_streamon(cam, arg);
  2480. case VIDIOC_STREAMOFF:
  2481. return sn9c102_vidioc_streamoff(cam, arg);
  2482. case VIDIOC_G_PARM:
  2483. return sn9c102_vidioc_g_parm(cam, arg);
  2484. case VIDIOC_S_PARM:
  2485. return sn9c102_vidioc_s_parm(cam, arg);
  2486. case VIDIOC_ENUMAUDIO:
  2487. return sn9c102_vidioc_enumaudio(cam, arg);
  2488. case VIDIOC_G_AUDIO:
  2489. return sn9c102_vidioc_g_audio(cam, arg);
  2490. case VIDIOC_S_AUDIO:
  2491. return sn9c102_vidioc_s_audio(cam, arg);
  2492. case VIDIOC_G_STD:
  2493. case VIDIOC_S_STD:
  2494. case VIDIOC_QUERYSTD:
  2495. case VIDIOC_ENUMSTD:
  2496. case VIDIOC_QUERYMENU:
  2497. case VIDIOC_ENUM_FRAMEINTERVALS:
  2498. return -EINVAL;
  2499. default:
  2500. return -EINVAL;
  2501. }
  2502. }
  2503. static int sn9c102_ioctl(struct inode* inode, struct file* filp,
  2504. unsigned int cmd, unsigned long arg)
  2505. {
  2506. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  2507. int err = 0;
  2508. if (mutex_lock_interruptible(&cam->fileop_mutex))
  2509. return -ERESTARTSYS;
  2510. if (cam->state & DEV_DISCONNECTED) {
  2511. DBG(1, "Device not present");
  2512. mutex_unlock(&cam->fileop_mutex);
  2513. return -ENODEV;
  2514. }
  2515. if (cam->state & DEV_MISCONFIGURED) {
  2516. DBG(1, "The camera is misconfigured. Close and open it "
  2517. "again.");
  2518. mutex_unlock(&cam->fileop_mutex);
  2519. return -EIO;
  2520. }
  2521. V4LDBG(3, "sn9c102", cmd);
  2522. err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
  2523. mutex_unlock(&cam->fileop_mutex);
  2524. return err;
  2525. }
  2526. /*****************************************************************************/
  2527. static const struct file_operations sn9c102_fops = {
  2528. .owner = THIS_MODULE,
  2529. .open = sn9c102_open,
  2530. .release = sn9c102_release,
  2531. .ioctl = sn9c102_ioctl,
  2532. .compat_ioctl = v4l_compat_ioctl32,
  2533. .read = sn9c102_read,
  2534. .poll = sn9c102_poll,
  2535. .mmap = sn9c102_mmap,
  2536. .llseek = no_llseek,
  2537. };
  2538. /*****************************************************************************/
  2539. /* It exists a single interface only. We do not need to validate anything. */
  2540. static int
  2541. sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
  2542. {
  2543. struct usb_device *udev = interface_to_usbdev(intf);
  2544. struct sn9c102_device* cam;
  2545. static unsigned int dev_nr = 0;
  2546. unsigned int i;
  2547. int err = 0, r;
  2548. if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
  2549. return -ENOMEM;
  2550. cam->usbdev = udev;
  2551. if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
  2552. DBG(1, "kzalloc() failed");
  2553. err = -ENOMEM;
  2554. goto fail;
  2555. }
  2556. if (!(cam->v4ldev = video_device_alloc())) {
  2557. DBG(1, "video_device_alloc() failed");
  2558. err = -ENOMEM;
  2559. goto fail;
  2560. }
  2561. mutex_init(&cam->dev_mutex);
  2562. r = sn9c102_read_reg(cam, 0x00);
  2563. if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) {
  2564. DBG(1, "Sorry, this is not a SN9C1xx-based camera "
  2565. "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  2566. err = -ENODEV;
  2567. goto fail;
  2568. }
  2569. cam->bridge = id->driver_info;
  2570. switch (cam->bridge) {
  2571. case BRIDGE_SN9C101:
  2572. case BRIDGE_SN9C102:
  2573. DBG(2, "SN9C10[12] PC Camera Controller detected "
  2574. "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  2575. break;
  2576. case BRIDGE_SN9C103:
  2577. DBG(2, "SN9C103 PC Camera Controller detected "
  2578. "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  2579. break;
  2580. case BRIDGE_SN9C105:
  2581. DBG(2, "SN9C105 PC Camera Controller detected "
  2582. "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  2583. break;
  2584. case BRIDGE_SN9C120:
  2585. DBG(2, "SN9C120 PC Camera Controller detected "
  2586. "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  2587. break;
  2588. }
  2589. for (i = 0; sn9c102_sensor_table[i]; i++) {
  2590. err = sn9c102_sensor_table[i](cam);
  2591. if (!err)
  2592. break;
  2593. }
  2594. if (!err) {
  2595. DBG(2, "%s image sensor detected", cam->sensor.name);
  2596. DBG(3, "Support for %s maintained by %s",
  2597. cam->sensor.name, cam->sensor.maintainer);
  2598. } else {
  2599. DBG(1, "No supported image sensor detected");
  2600. err = -ENODEV;
  2601. goto fail;
  2602. }
  2603. if (!(cam->bridge & cam->sensor.supported_bridge)) {
  2604. DBG(1, "Bridge not supported");
  2605. err = -ENODEV;
  2606. goto fail;
  2607. }
  2608. if (sn9c102_init(cam)) {
  2609. DBG(1, "Initialization failed. I will retry on open().");
  2610. cam->state |= DEV_MISCONFIGURED;
  2611. }
  2612. strcpy(cam->v4ldev->name, "SN9C1xx PC Camera");
  2613. cam->v4ldev->owner = THIS_MODULE;
  2614. cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
  2615. cam->v4ldev->hardware = 0;
  2616. cam->v4ldev->fops = &sn9c102_fops;
  2617. cam->v4ldev->minor = video_nr[dev_nr];
  2618. cam->v4ldev->release = video_device_release;
  2619. video_set_drvdata(cam->v4ldev, cam);
  2620. mutex_lock(&cam->dev_mutex);
  2621. err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
  2622. video_nr[dev_nr]);
  2623. if (err) {
  2624. DBG(1, "V4L2 device registration failed");
  2625. if (err == -ENFILE && video_nr[dev_nr] == -1)
  2626. DBG(1, "Free /dev/videoX node not found");
  2627. video_nr[dev_nr] = -1;
  2628. dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
  2629. mutex_unlock(&cam->dev_mutex);
  2630. goto fail;
  2631. }
  2632. DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
  2633. cam->module_param.force_munmap = force_munmap[dev_nr];
  2634. cam->module_param.frame_timeout = frame_timeout[dev_nr];
  2635. dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
  2636. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2637. err = sn9c102_create_sysfs(cam);
  2638. if (!err)
  2639. DBG(2, "Optional device control through 'sysfs' "
  2640. "interface ready");
  2641. else
  2642. DBG(2, "Failed to create optional 'sysfs' interface for "
  2643. "device controlling. Error #%d", err);
  2644. #else
  2645. DBG(2, "Optional device control through 'sysfs' interface disabled");
  2646. DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
  2647. "configuration option to enable it.");
  2648. #endif
  2649. usb_set_intfdata(intf, cam);
  2650. mutex_unlock(&cam->dev_mutex);
  2651. return 0;
  2652. fail:
  2653. if (cam) {
  2654. kfree(cam->control_buffer);
  2655. if (cam->v4ldev)
  2656. video_device_release(cam->v4ldev);
  2657. kfree(cam);
  2658. }
  2659. return err;
  2660. }
  2661. static void sn9c102_usb_disconnect(struct usb_interface* intf)
  2662. {
  2663. struct sn9c102_device* cam = usb_get_intfdata(intf);
  2664. if (!cam)
  2665. return;
  2666. down_write(&sn9c102_disconnect);
  2667. mutex_lock(&cam->dev_mutex);
  2668. DBG(2, "Disconnecting %s...", cam->v4ldev->name);
  2669. wake_up_interruptible_all(&cam->open);
  2670. if (cam->users) {
  2671. DBG(2, "Device /dev/video%d is open! Deregistration and "
  2672. "memory deallocation are deferred on close.",
  2673. cam->v4ldev->minor);
  2674. cam->state |= DEV_MISCONFIGURED;
  2675. sn9c102_stop_transfer(cam);
  2676. cam->state |= DEV_DISCONNECTED;
  2677. wake_up_interruptible(&cam->wait_frame);
  2678. wake_up(&cam->wait_stream);
  2679. usb_get_dev(cam->usbdev);
  2680. } else {
  2681. cam->state |= DEV_DISCONNECTED;
  2682. sn9c102_release_resources(cam);
  2683. }
  2684. mutex_unlock(&cam->dev_mutex);
  2685. if (!cam->users)
  2686. kfree(cam);
  2687. up_write(&sn9c102_disconnect);
  2688. }
  2689. static struct usb_driver sn9c102_usb_driver = {
  2690. .name = "sn9c102",
  2691. .id_table = sn9c102_id_table,
  2692. .probe = sn9c102_usb_probe,
  2693. .disconnect = sn9c102_usb_disconnect,
  2694. };
  2695. /*****************************************************************************/
  2696. static int __init sn9c102_module_init(void)
  2697. {
  2698. int err = 0;
  2699. KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
  2700. KDBG(3, SN9C102_MODULE_AUTHOR);
  2701. if ((err = usb_register(&sn9c102_usb_driver)))
  2702. KDBG(1, "usb_register() failed");
  2703. return err;
  2704. }
  2705. static void __exit sn9c102_module_exit(void)
  2706. {
  2707. usb_deregister(&sn9c102_usb_driver);
  2708. }
  2709. module_init(sn9c102_module_init);
  2710. module_exit(sn9c102_module_exit);