sn9c102_core.c 82 KB

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