sn9c102_core.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919
  1. /***************************************************************************
  2. * V4L2 driver for SN9C10x PC Camera Controllers *
  3. * *
  4. * Copyright (C) 2004-2006 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 SN9C10x PC Camera Controllers"
  43. #define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
  44. #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
  45. #define SN9C102_MODULE_LICENSE "GPL"
  46. #define SN9C102_MODULE_VERSION "1:1.27"
  47. #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 27)
  48. /*****************************************************************************/
  49. MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
  50. MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
  51. MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
  52. MODULE_VERSION(SN9C102_MODULE_VERSION);
  53. MODULE_LICENSE(SN9C102_MODULE_LICENSE);
  54. static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
  55. module_param_array(video_nr, short, NULL, 0444);
  56. MODULE_PARM_DESC(video_nr,
  57. "\n<-1|n[,...]> Specify V4L2 minor mode number."
  58. "\n -1 = use next available (default)"
  59. "\n n = use minor number n (integer >= 0)"
  60. "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
  61. " cameras this way."
  62. "\nFor example:"
  63. "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
  64. "\nthe second camera and use auto for the first"
  65. "\none and for every other camera."
  66. "\n");
  67. static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
  68. SN9C102_FORCE_MUNMAP};
  69. module_param_array(force_munmap, bool, NULL, 0444);
  70. MODULE_PARM_DESC(force_munmap,
  71. "\n<0|1[,...]> Force the application to unmap previously"
  72. "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
  73. "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
  74. "\nthis feature. This parameter is specific for each"
  75. "\ndetected camera."
  76. "\n 0 = do not force memory unmapping"
  77. "\n 1 = force memory unmapping (save memory)"
  78. "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
  79. "\n");
  80. static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
  81. SN9C102_FRAME_TIMEOUT};
  82. module_param_array(frame_timeout, uint, NULL, 0644);
  83. MODULE_PARM_DESC(frame_timeout,
  84. "\n<n[,...]> Timeout for a video frame in seconds."
  85. "\nThis parameter is specific for each detected camera."
  86. "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
  87. "\n");
  88. #ifdef SN9C102_DEBUG
  89. static unsigned short debug = SN9C102_DEBUG_LEVEL;
  90. module_param(debug, ushort, 0644);
  91. MODULE_PARM_DESC(debug,
  92. "\n<n> Debugging information level, from 0 to 3:"
  93. "\n0 = none (use carefully)"
  94. "\n1 = critical errors"
  95. "\n2 = significant informations"
  96. "\n3 = more verbose messages"
  97. "\nLevel 3 is useful for testing only, when only "
  98. "one device is used."
  99. "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
  100. "\n");
  101. #endif
  102. /*****************************************************************************/
  103. static sn9c102_sof_header_t sn9c102_sof_header[] = {
  104. {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
  105. {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
  106. };
  107. static sn9c103_sof_header_t sn9c103_sof_header[] = {
  108. {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20},
  109. };
  110. static sn9c102_eof_header_t sn9c102_eof_header[] = {
  111. {0x00, 0x00, 0x00, 0x00},
  112. {0x40, 0x00, 0x00, 0x00},
  113. {0x80, 0x00, 0x00, 0x00},
  114. {0xc0, 0x00, 0x00, 0x00},
  115. };
  116. /*****************************************************************************/
  117. static u32
  118. sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
  119. enum sn9c102_io_method io)
  120. {
  121. struct v4l2_pix_format* p = &(cam->sensor.pix_format);
  122. struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
  123. const size_t imagesize = cam->module_param.force_munmap ||
  124. io == IO_READ ?
  125. (p->width * p->height * p->priv) / 8 :
  126. (r->width * r->height * p->priv) / 8;
  127. void* buff = NULL;
  128. u32 i;
  129. if (count > SN9C102_MAX_FRAMES)
  130. count = SN9C102_MAX_FRAMES;
  131. cam->nbuffers = count;
  132. while (cam->nbuffers > 0) {
  133. if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
  134. break;
  135. cam->nbuffers--;
  136. }
  137. for (i = 0; i < cam->nbuffers; i++) {
  138. cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
  139. cam->frame[i].buf.index = i;
  140. cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
  141. cam->frame[i].buf.length = imagesize;
  142. cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  143. cam->frame[i].buf.sequence = 0;
  144. cam->frame[i].buf.field = V4L2_FIELD_NONE;
  145. cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
  146. cam->frame[i].buf.flags = 0;
  147. }
  148. return cam->nbuffers;
  149. }
  150. static void sn9c102_release_buffers(struct sn9c102_device* cam)
  151. {
  152. if (cam->nbuffers) {
  153. vfree(cam->frame[0].bufmem);
  154. cam->nbuffers = 0;
  155. }
  156. cam->frame_current = NULL;
  157. }
  158. static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
  159. {
  160. u32 i;
  161. INIT_LIST_HEAD(&cam->inqueue);
  162. INIT_LIST_HEAD(&cam->outqueue);
  163. for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
  164. cam->frame[i].state = F_UNUSED;
  165. cam->frame[i].buf.bytesused = 0;
  166. }
  167. }
  168. static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
  169. {
  170. struct sn9c102_frame_t *i;
  171. list_for_each_entry(i, &cam->outqueue, frame) {
  172. i->state = F_QUEUED;
  173. list_add(&i->frame, &cam->inqueue);
  174. }
  175. INIT_LIST_HEAD(&cam->outqueue);
  176. }
  177. static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
  178. {
  179. unsigned long lock_flags;
  180. u32 i;
  181. for (i = 0; i < cam->nbuffers; i++)
  182. if (cam->frame[i].state == F_UNUSED) {
  183. cam->frame[i].state = F_QUEUED;
  184. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  185. list_add_tail(&cam->frame[i].frame, &cam->inqueue);
  186. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  187. }
  188. }
  189. /*****************************************************************************/
  190. int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
  191. {
  192. struct usb_device* udev = cam->usbdev;
  193. int i, res;
  194. if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
  195. return -1;
  196. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  197. index, 0, buff, sizeof(buff),
  198. SN9C102_CTRL_TIMEOUT*sizeof(buff));
  199. if (res < 0) {
  200. DBG(3, "Failed to write registers (index 0x%02X, error %d)",
  201. index, res);
  202. return -1;
  203. }
  204. for (i = 0; i < sizeof(buff); i++)
  205. cam->reg[index+i] = buff[i];
  206. return 0;
  207. }
  208. int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
  209. {
  210. struct usb_device* udev = cam->usbdev;
  211. u8* buff = cam->control_buffer;
  212. int res;
  213. if (index >= ARRAY_SIZE(cam->reg))
  214. return -1;
  215. *buff = value;
  216. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  217. index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
  218. if (res < 0) {
  219. DBG(3, "Failed to write a register (value 0x%02X, index "
  220. "0x%02X, error %d)", value, index, res);
  221. return -1;
  222. }
  223. cam->reg[index] = value;
  224. return 0;
  225. }
  226. /* NOTE: reading some registers always returns 0 */
  227. static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
  228. {
  229. struct usb_device* udev = cam->usbdev;
  230. u8* buff = cam->control_buffer;
  231. int res;
  232. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
  233. index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
  234. if (res < 0)
  235. DBG(3, "Failed to read a register (index 0x%02X, error %d)",
  236. index, res);
  237. return (res >= 0) ? (int)(*buff) : -1;
  238. }
  239. int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
  240. {
  241. if (index >= ARRAY_SIZE(cam->reg))
  242. return -1;
  243. return cam->reg[index];
  244. }
  245. static int
  246. sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
  247. {
  248. int i, r;
  249. for (i = 1; i <= 5; i++) {
  250. r = sn9c102_read_reg(cam, 0x08);
  251. if (r < 0)
  252. return -EIO;
  253. if (r & 0x04)
  254. return 0;
  255. if (sensor->frequency & SN9C102_I2C_400KHZ)
  256. udelay(5*16);
  257. else
  258. udelay(16*16);
  259. }
  260. return -EBUSY;
  261. }
  262. static int
  263. sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
  264. struct sn9c102_sensor* sensor)
  265. {
  266. int r;
  267. r = sn9c102_read_reg(cam, 0x08);
  268. return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
  269. }
  270. static int
  271. sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
  272. struct sn9c102_sensor* sensor)
  273. {
  274. int r;
  275. r = sn9c102_read_reg(cam, 0x08);
  276. return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
  277. }
  278. int
  279. sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
  280. struct sn9c102_sensor* sensor, u8 data0, u8 data1,
  281. u8 n, u8 buffer[])
  282. {
  283. struct usb_device* udev = cam->usbdev;
  284. u8* data = cam->control_buffer;
  285. int err = 0, res;
  286. /* Write cycle */
  287. data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
  288. ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
  289. data[1] = data0; /* I2C slave id */
  290. data[2] = data1; /* address */
  291. data[7] = 0x10;
  292. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  293. 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
  294. if (res < 0)
  295. err += res;
  296. err += sn9c102_i2c_wait(cam, sensor);
  297. /* Read cycle - n bytes */
  298. data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
  299. ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
  300. (n << 4) | 0x02;
  301. data[1] = data0;
  302. data[7] = 0x10;
  303. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  304. 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
  305. if (res < 0)
  306. err += res;
  307. err += sn9c102_i2c_wait(cam, sensor);
  308. /* The first read byte will be placed in data[4] */
  309. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
  310. 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
  311. if (res < 0)
  312. err += res;
  313. err += sn9c102_i2c_detect_read_error(cam, sensor);
  314. PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
  315. data[4]);
  316. if (err) {
  317. DBG(3, "I2C read failed for %s image sensor", sensor->name);
  318. return -1;
  319. }
  320. if (buffer)
  321. memcpy(buffer, data, sizeof(buffer));
  322. return (int)data[4];
  323. }
  324. int
  325. sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
  326. struct sn9c102_sensor* sensor, u8 n, u8 data0,
  327. u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
  328. {
  329. struct usb_device* udev = cam->usbdev;
  330. u8* data = cam->control_buffer;
  331. int err = 0, res;
  332. /* Write cycle. It usually is address + value */
  333. data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
  334. ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
  335. | ((n - 1) << 4);
  336. data[1] = data0;
  337. data[2] = data1;
  338. data[3] = data2;
  339. data[4] = data3;
  340. data[5] = data4;
  341. data[6] = data5;
  342. data[7] = 0x14;
  343. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
  344. 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
  345. if (res < 0)
  346. err += res;
  347. err += sn9c102_i2c_wait(cam, sensor);
  348. err += sn9c102_i2c_detect_write_error(cam, sensor);
  349. if (err)
  350. DBG(3, "I2C write failed for %s image sensor", sensor->name);
  351. PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
  352. "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
  353. n, data0, data1, data2, data3, data4, data5);
  354. return err ? -1 : 0;
  355. }
  356. int
  357. sn9c102_i2c_try_read(struct sn9c102_device* cam,
  358. struct sn9c102_sensor* sensor, u8 address)
  359. {
  360. return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
  361. address, 1, NULL);
  362. }
  363. int
  364. sn9c102_i2c_try_write(struct sn9c102_device* cam,
  365. struct sn9c102_sensor* sensor, u8 address, u8 value)
  366. {
  367. return sn9c102_i2c_try_raw_write(cam, sensor, 3,
  368. sensor->i2c_slave_id, address,
  369. value, 0, 0, 0);
  370. }
  371. int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
  372. {
  373. return sn9c102_i2c_try_read(cam, &cam->sensor, address);
  374. }
  375. int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
  376. {
  377. return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
  378. }
  379. /*****************************************************************************/
  380. static void*
  381. sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
  382. {
  383. size_t soflen = 0, i;
  384. u8 j, n = 0;
  385. switch (cam->bridge) {
  386. case BRIDGE_SN9C101:
  387. case BRIDGE_SN9C102:
  388. soflen = sizeof(sn9c102_sof_header_t);
  389. n = sizeof(sn9c102_sof_header) / soflen;
  390. break;
  391. case BRIDGE_SN9C103:
  392. soflen = sizeof(sn9c103_sof_header_t);
  393. n = sizeof(sn9c103_sof_header) / soflen;
  394. }
  395. for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
  396. for (j = 0; j < n; j++)
  397. /* The invariable part of the header is 6 bytes long */
  398. if ((cam->bridge != BRIDGE_SN9C103 &&
  399. !memcmp(mem + i, sn9c102_sof_header[j], 6)) ||
  400. (cam->bridge == BRIDGE_SN9C103 &&
  401. !memcmp(mem + i, sn9c103_sof_header[j], 6))) {
  402. memcpy(cam->sof_header, mem + i, soflen);
  403. /* Skip the header */
  404. return mem + i + soflen;
  405. }
  406. return NULL;
  407. }
  408. static void*
  409. sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
  410. {
  411. size_t eoflen = sizeof(sn9c102_eof_header_t), i;
  412. unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
  413. if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
  414. return NULL; /* EOF header does not exist in compressed data */
  415. for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
  416. for (j = 0; j < n; j++)
  417. if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
  418. return mem + i;
  419. return NULL;
  420. }
  421. static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
  422. {
  423. struct sn9c102_device* cam = urb->context;
  424. struct sn9c102_frame_t** f;
  425. size_t imagesize, soflen;
  426. u8 i;
  427. int err = 0;
  428. if (urb->status == -ENOENT)
  429. return;
  430. f = &cam->frame_current;
  431. if (cam->stream == STREAM_INTERRUPT) {
  432. cam->stream = STREAM_OFF;
  433. if ((*f))
  434. (*f)->state = F_QUEUED;
  435. DBG(3, "Stream interrupted");
  436. wake_up(&cam->wait_stream);
  437. }
  438. if (cam->state & DEV_DISCONNECTED)
  439. return;
  440. if (cam->state & DEV_MISCONFIGURED) {
  441. wake_up_interruptible(&cam->wait_frame);
  442. return;
  443. }
  444. if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
  445. goto resubmit_urb;
  446. if (!(*f))
  447. (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
  448. frame);
  449. imagesize = (cam->sensor.pix_format.width *
  450. cam->sensor.pix_format.height *
  451. cam->sensor.pix_format.priv) / 8;
  452. soflen = (cam->bridge) == BRIDGE_SN9C103 ?
  453. sizeof(sn9c103_sof_header_t) :
  454. sizeof(sn9c102_sof_header_t);
  455. for (i = 0; i < urb->number_of_packets; i++) {
  456. unsigned int img, len, status;
  457. void *pos, *sof, *eof;
  458. len = urb->iso_frame_desc[i].actual_length;
  459. status = urb->iso_frame_desc[i].status;
  460. pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
  461. if (status) {
  462. DBG(3, "Error in isochronous frame");
  463. (*f)->state = F_ERROR;
  464. continue;
  465. }
  466. PDBGG("Isochrnous frame: length %u, #%u i", len, i);
  467. redo:
  468. sof = sn9c102_find_sof_header(cam, pos, len);
  469. if (likely(!sof)) {
  470. eof = sn9c102_find_eof_header(cam, pos, len);
  471. if ((*f)->state == F_GRABBING) {
  472. end_of_frame:
  473. img = len;
  474. if (eof)
  475. img = (eof > pos) ? eof - pos - 1 : 0;
  476. if ((*f)->buf.bytesused+img > imagesize) {
  477. u32 b;
  478. b = (*f)->buf.bytesused + img -
  479. imagesize;
  480. img = imagesize - (*f)->buf.bytesused;
  481. DBG(3, "Expected EOF not found: "
  482. "video frame cut");
  483. if (eof)
  484. DBG(3, "Exceeded limit: +%u "
  485. "bytes", (unsigned)(b));
  486. }
  487. memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
  488. img);
  489. if ((*f)->buf.bytesused == 0)
  490. do_gettimeofday(&(*f)->buf.timestamp);
  491. (*f)->buf.bytesused += img;
  492. if ((*f)->buf.bytesused == imagesize ||
  493. (cam->sensor.pix_format.pixelformat ==
  494. V4L2_PIX_FMT_SN9C10X && eof)) {
  495. u32 b;
  496. b = (*f)->buf.bytesused;
  497. (*f)->state = F_DONE;
  498. (*f)->buf.sequence= ++cam->frame_count;
  499. spin_lock(&cam->queue_lock);
  500. list_move_tail(&(*f)->frame,
  501. &cam->outqueue);
  502. if (!list_empty(&cam->inqueue))
  503. (*f) = list_entry(
  504. cam->inqueue.next,
  505. struct sn9c102_frame_t,
  506. frame );
  507. else
  508. (*f) = NULL;
  509. spin_unlock(&cam->queue_lock);
  510. memcpy(cam->sysfs.frame_header,
  511. cam->sof_header, soflen);
  512. DBG(3, "Video frame captured: %lu "
  513. "bytes", (unsigned long)(b));
  514. if (!(*f))
  515. goto resubmit_urb;
  516. } else if (eof) {
  517. (*f)->state = F_ERROR;
  518. DBG(3, "Not expected EOF after %lu "
  519. "bytes of image data",
  520. (unsigned long)
  521. ((*f)->buf.bytesused));
  522. }
  523. if (sof) /* (1) */
  524. goto start_of_frame;
  525. } else if (eof) {
  526. DBG(3, "EOF without SOF");
  527. continue;
  528. } else {
  529. PDBGG("Ignoring pointless isochronous frame");
  530. continue;
  531. }
  532. } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
  533. start_of_frame:
  534. (*f)->state = F_GRABBING;
  535. (*f)->buf.bytesused = 0;
  536. len -= (sof - pos);
  537. pos = sof;
  538. DBG(3, "SOF detected: new video frame");
  539. if (len)
  540. goto redo;
  541. } else if ((*f)->state == F_GRABBING) {
  542. eof = sn9c102_find_eof_header(cam, pos, len);
  543. if (eof && eof < sof)
  544. goto end_of_frame; /* (1) */
  545. else {
  546. if (cam->sensor.pix_format.pixelformat ==
  547. V4L2_PIX_FMT_SN9C10X) {
  548. eof = sof - soflen;
  549. goto end_of_frame;
  550. } else {
  551. DBG(3, "SOF before expected EOF after "
  552. "%lu bytes of image data",
  553. (unsigned long)
  554. ((*f)->buf.bytesused));
  555. goto start_of_frame;
  556. }
  557. }
  558. }
  559. }
  560. resubmit_urb:
  561. urb->dev = cam->usbdev;
  562. err = usb_submit_urb(urb, GFP_ATOMIC);
  563. if (err < 0 && err != -EPERM) {
  564. cam->state |= DEV_MISCONFIGURED;
  565. DBG(1, "usb_submit_urb() failed");
  566. }
  567. wake_up_interruptible(&cam->wait_frame);
  568. }
  569. static int sn9c102_start_transfer(struct sn9c102_device* cam)
  570. {
  571. struct usb_device *udev = cam->usbdev;
  572. struct urb* urb;
  573. const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512,
  574. 680, 800, 900, 1023};
  575. const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512,
  576. 680, 800, 900, 1003};
  577. const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ?
  578. sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] :
  579. sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
  580. s8 i, j;
  581. int err = 0;
  582. for (i = 0; i < SN9C102_URBS; i++) {
  583. cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
  584. GFP_KERNEL);
  585. if (!cam->transfer_buffer[i]) {
  586. err = -ENOMEM;
  587. DBG(1, "Not enough memory");
  588. goto free_buffers;
  589. }
  590. }
  591. for (i = 0; i < SN9C102_URBS; i++) {
  592. urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
  593. cam->urb[i] = urb;
  594. if (!urb) {
  595. err = -ENOMEM;
  596. DBG(1, "usb_alloc_urb() failed");
  597. goto free_urbs;
  598. }
  599. urb->dev = udev;
  600. urb->context = cam;
  601. urb->pipe = usb_rcvisocpipe(udev, 1);
  602. urb->transfer_flags = URB_ISO_ASAP;
  603. urb->number_of_packets = SN9C102_ISO_PACKETS;
  604. urb->complete = sn9c102_urb_complete;
  605. urb->transfer_buffer = cam->transfer_buffer[i];
  606. urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
  607. urb->interval = 1;
  608. for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
  609. urb->iso_frame_desc[j].offset = psz * j;
  610. urb->iso_frame_desc[j].length = psz;
  611. }
  612. }
  613. /* Enable video */
  614. if (!(cam->reg[0x01] & 0x04)) {
  615. err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
  616. if (err) {
  617. err = -EIO;
  618. DBG(1, "I/O hardware error");
  619. goto free_urbs;
  620. }
  621. }
  622. err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
  623. if (err) {
  624. DBG(1, "usb_set_interface() failed");
  625. goto free_urbs;
  626. }
  627. cam->frame_current = NULL;
  628. for (i = 0; i < SN9C102_URBS; i++) {
  629. err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
  630. if (err) {
  631. for (j = i-1; j >= 0; j--)
  632. usb_kill_urb(cam->urb[j]);
  633. DBG(1, "usb_submit_urb() failed, error %d", err);
  634. goto free_urbs;
  635. }
  636. }
  637. return 0;
  638. free_urbs:
  639. for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
  640. usb_free_urb(cam->urb[i]);
  641. free_buffers:
  642. for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
  643. kfree(cam->transfer_buffer[i]);
  644. return err;
  645. }
  646. static int sn9c102_stop_transfer(struct sn9c102_device* cam)
  647. {
  648. struct usb_device *udev = cam->usbdev;
  649. s8 i;
  650. int err = 0;
  651. if (cam->state & DEV_DISCONNECTED)
  652. return 0;
  653. for (i = SN9C102_URBS-1; i >= 0; i--) {
  654. usb_kill_urb(cam->urb[i]);
  655. usb_free_urb(cam->urb[i]);
  656. kfree(cam->transfer_buffer[i]);
  657. }
  658. err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
  659. if (err)
  660. DBG(3, "usb_set_interface() failed");
  661. return err;
  662. }
  663. static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
  664. {
  665. long timeout;
  666. cam->stream = STREAM_INTERRUPT;
  667. timeout = wait_event_timeout(cam->wait_stream,
  668. (cam->stream == STREAM_OFF) ||
  669. (cam->state & DEV_DISCONNECTED),
  670. SN9C102_URB_TIMEOUT);
  671. if (cam->state & DEV_DISCONNECTED)
  672. return -ENODEV;
  673. else if (cam->stream != STREAM_OFF) {
  674. cam->state |= DEV_MISCONFIGURED;
  675. DBG(1, "URB timeout reached. The camera is misconfigured. "
  676. "To use it, close and open /dev/video%d again.",
  677. cam->v4ldev->minor);
  678. return -EIO;
  679. }
  680. return 0;
  681. }
  682. /*****************************************************************************/
  683. #ifdef CONFIG_VIDEO_ADV_DEBUG
  684. static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
  685. {
  686. char str[5];
  687. char* endp;
  688. unsigned long val;
  689. if (len < 4) {
  690. strncpy(str, buff, len);
  691. str[len+1] = '\0';
  692. } else {
  693. strncpy(str, buff, 4);
  694. str[4] = '\0';
  695. }
  696. val = simple_strtoul(str, &endp, 0);
  697. *count = 0;
  698. if (val <= 0xff)
  699. *count = (ssize_t)(endp - str);
  700. if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
  701. *count += 1;
  702. return (u8)val;
  703. }
  704. /*
  705. NOTE 1: being inside one of the following methods implies that the v4l
  706. device exists for sure (see kobjects and reference counters)
  707. NOTE 2: buffers are PAGE_SIZE long
  708. */
  709. static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
  710. {
  711. struct sn9c102_device* cam;
  712. ssize_t count;
  713. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  714. return -ERESTARTSYS;
  715. cam = video_get_drvdata(to_video_device(cd));
  716. if (!cam) {
  717. mutex_unlock(&sn9c102_sysfs_lock);
  718. return -ENODEV;
  719. }
  720. count = sprintf(buf, "%u\n", cam->sysfs.reg);
  721. mutex_unlock(&sn9c102_sysfs_lock);
  722. return count;
  723. }
  724. static ssize_t
  725. sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
  726. {
  727. struct sn9c102_device* cam;
  728. u8 index;
  729. ssize_t count;
  730. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  731. return -ERESTARTSYS;
  732. cam = video_get_drvdata(to_video_device(cd));
  733. if (!cam) {
  734. mutex_unlock(&sn9c102_sysfs_lock);
  735. return -ENODEV;
  736. }
  737. index = sn9c102_strtou8(buf, len, &count);
  738. if (index > 0x1f || !count) {
  739. mutex_unlock(&sn9c102_sysfs_lock);
  740. return -EINVAL;
  741. }
  742. cam->sysfs.reg = index;
  743. DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
  744. DBG(3, "Written bytes: %zd", count);
  745. mutex_unlock(&sn9c102_sysfs_lock);
  746. return count;
  747. }
  748. static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
  749. {
  750. struct sn9c102_device* cam;
  751. ssize_t count;
  752. int val;
  753. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  754. return -ERESTARTSYS;
  755. cam = video_get_drvdata(to_video_device(cd));
  756. if (!cam) {
  757. mutex_unlock(&sn9c102_sysfs_lock);
  758. return -ENODEV;
  759. }
  760. if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
  761. mutex_unlock(&sn9c102_sysfs_lock);
  762. return -EIO;
  763. }
  764. count = sprintf(buf, "%d\n", val);
  765. DBG(3, "Read bytes: %zd", count);
  766. mutex_unlock(&sn9c102_sysfs_lock);
  767. return count;
  768. }
  769. static ssize_t
  770. sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
  771. {
  772. struct sn9c102_device* cam;
  773. u8 value;
  774. ssize_t count;
  775. int err;
  776. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  777. return -ERESTARTSYS;
  778. cam = video_get_drvdata(to_video_device(cd));
  779. if (!cam) {
  780. mutex_unlock(&sn9c102_sysfs_lock);
  781. return -ENODEV;
  782. }
  783. value = sn9c102_strtou8(buf, len, &count);
  784. if (!count) {
  785. mutex_unlock(&sn9c102_sysfs_lock);
  786. return -EINVAL;
  787. }
  788. err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
  789. if (err) {
  790. mutex_unlock(&sn9c102_sysfs_lock);
  791. return -EIO;
  792. }
  793. DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
  794. cam->sysfs.reg, value);
  795. DBG(3, "Written bytes: %zd", count);
  796. mutex_unlock(&sn9c102_sysfs_lock);
  797. return count;
  798. }
  799. static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
  800. {
  801. struct sn9c102_device* cam;
  802. ssize_t count;
  803. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  804. return -ERESTARTSYS;
  805. cam = video_get_drvdata(to_video_device(cd));
  806. if (!cam) {
  807. mutex_unlock(&sn9c102_sysfs_lock);
  808. return -ENODEV;
  809. }
  810. count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
  811. DBG(3, "Read bytes: %zd", count);
  812. mutex_unlock(&sn9c102_sysfs_lock);
  813. return count;
  814. }
  815. static ssize_t
  816. sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
  817. {
  818. struct sn9c102_device* cam;
  819. u8 index;
  820. ssize_t count;
  821. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  822. return -ERESTARTSYS;
  823. cam = video_get_drvdata(to_video_device(cd));
  824. if (!cam) {
  825. mutex_unlock(&sn9c102_sysfs_lock);
  826. return -ENODEV;
  827. }
  828. index = sn9c102_strtou8(buf, len, &count);
  829. if (!count) {
  830. mutex_unlock(&sn9c102_sysfs_lock);
  831. return -EINVAL;
  832. }
  833. cam->sysfs.i2c_reg = index;
  834. DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
  835. DBG(3, "Written bytes: %zd", count);
  836. mutex_unlock(&sn9c102_sysfs_lock);
  837. return count;
  838. }
  839. static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
  840. {
  841. struct sn9c102_device* cam;
  842. ssize_t count;
  843. int val;
  844. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  845. return -ERESTARTSYS;
  846. cam = video_get_drvdata(to_video_device(cd));
  847. if (!cam) {
  848. mutex_unlock(&sn9c102_sysfs_lock);
  849. return -ENODEV;
  850. }
  851. if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
  852. mutex_unlock(&sn9c102_sysfs_lock);
  853. return -ENOSYS;
  854. }
  855. if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
  856. mutex_unlock(&sn9c102_sysfs_lock);
  857. return -EIO;
  858. }
  859. count = sprintf(buf, "%d\n", val);
  860. DBG(3, "Read bytes: %zd", count);
  861. mutex_unlock(&sn9c102_sysfs_lock);
  862. return count;
  863. }
  864. static ssize_t
  865. sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
  866. {
  867. struct sn9c102_device* cam;
  868. u8 value;
  869. ssize_t count;
  870. int err;
  871. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  872. return -ERESTARTSYS;
  873. cam = video_get_drvdata(to_video_device(cd));
  874. if (!cam) {
  875. mutex_unlock(&sn9c102_sysfs_lock);
  876. return -ENODEV;
  877. }
  878. if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
  879. mutex_unlock(&sn9c102_sysfs_lock);
  880. return -ENOSYS;
  881. }
  882. value = sn9c102_strtou8(buf, len, &count);
  883. if (!count) {
  884. mutex_unlock(&sn9c102_sysfs_lock);
  885. return -EINVAL;
  886. }
  887. err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
  888. if (err) {
  889. mutex_unlock(&sn9c102_sysfs_lock);
  890. return -EIO;
  891. }
  892. DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
  893. cam->sysfs.i2c_reg, value);
  894. DBG(3, "Written bytes: %zd", count);
  895. mutex_unlock(&sn9c102_sysfs_lock);
  896. return count;
  897. }
  898. static ssize_t
  899. sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
  900. {
  901. struct sn9c102_device* cam;
  902. enum sn9c102_bridge bridge;
  903. ssize_t res = 0;
  904. u8 value;
  905. ssize_t count;
  906. if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
  907. return -ERESTARTSYS;
  908. cam = video_get_drvdata(to_video_device(cd));
  909. if (!cam) {
  910. mutex_unlock(&sn9c102_sysfs_lock);
  911. return -ENODEV;
  912. }
  913. bridge = cam->bridge;
  914. mutex_unlock(&sn9c102_sysfs_lock);
  915. value = sn9c102_strtou8(buf, len, &count);
  916. if (!count)
  917. return -EINVAL;
  918. switch (bridge) {
  919. case BRIDGE_SN9C101:
  920. case BRIDGE_SN9C102:
  921. if (value > 0x0f)
  922. return -EINVAL;
  923. if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
  924. res = sn9c102_store_val(cd, buf, len);
  925. break;
  926. case BRIDGE_SN9C103:
  927. if (value > 0x7f)
  928. return -EINVAL;
  929. if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
  930. res = sn9c102_store_val(cd, buf, len);
  931. break;
  932. }
  933. return res;
  934. }
  935. static ssize_t
  936. sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
  937. {
  938. ssize_t res = 0;
  939. u8 value;
  940. ssize_t count;
  941. value = sn9c102_strtou8(buf, len, &count);
  942. if (!count || value > 0x7f)
  943. return -EINVAL;
  944. if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
  945. res = sn9c102_store_val(cd, buf, len);
  946. return res;
  947. }
  948. static ssize_t
  949. sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
  950. {
  951. ssize_t res = 0;
  952. u8 value;
  953. ssize_t count;
  954. value = sn9c102_strtou8(buf, len, &count);
  955. if (!count || value > 0x7f)
  956. return -EINVAL;
  957. if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
  958. res = sn9c102_store_val(cd, buf, len);
  959. return res;
  960. }
  961. static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
  962. {
  963. struct sn9c102_device* cam;
  964. ssize_t count;
  965. cam = video_get_drvdata(to_video_device(cd));
  966. if (!cam)
  967. return -ENODEV;
  968. count = sizeof(cam->sysfs.frame_header);
  969. memcpy(buf, cam->sysfs.frame_header, count);
  970. DBG(3, "Frame header, read bytes: %zd", count);
  971. return count;
  972. }
  973. static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
  974. sn9c102_show_reg, sn9c102_store_reg);
  975. static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
  976. sn9c102_show_val, sn9c102_store_val);
  977. static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
  978. sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
  979. static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
  980. sn9c102_show_i2c_val, sn9c102_store_i2c_val);
  981. static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
  982. static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
  983. static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
  984. static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
  985. sn9c102_show_frame_header, NULL);
  986. static void sn9c102_create_sysfs(struct sn9c102_device* cam)
  987. {
  988. struct video_device *v4ldev = cam->v4ldev;
  989. video_device_create_file(v4ldev, &class_device_attr_reg);
  990. video_device_create_file(v4ldev, &class_device_attr_val);
  991. video_device_create_file(v4ldev, &class_device_attr_frame_header);
  992. if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
  993. video_device_create_file(v4ldev, &class_device_attr_green);
  994. else if (cam->bridge == BRIDGE_SN9C103) {
  995. video_device_create_file(v4ldev, &class_device_attr_blue);
  996. video_device_create_file(v4ldev, &class_device_attr_red);
  997. }
  998. if (cam->sensor.sysfs_ops) {
  999. video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
  1000. video_device_create_file(v4ldev, &class_device_attr_i2c_val);
  1001. }
  1002. }
  1003. #endif /* CONFIG_VIDEO_ADV_DEBUG */
  1004. /*****************************************************************************/
  1005. static int
  1006. sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
  1007. {
  1008. int err = 0;
  1009. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  1010. err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
  1011. else
  1012. err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
  1013. return err ? -EIO : 0;
  1014. }
  1015. static int
  1016. sn9c102_set_compression(struct sn9c102_device* cam,
  1017. struct v4l2_jpegcompression* compression)
  1018. {
  1019. int err = 0;
  1020. if (compression->quality == 0)
  1021. err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
  1022. else if (compression->quality == 1)
  1023. err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
  1024. return err ? -EIO : 0;
  1025. }
  1026. static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
  1027. {
  1028. u8 r = 0;
  1029. int err = 0;
  1030. if (scale == 1)
  1031. r = cam->reg[0x18] & 0xcf;
  1032. else if (scale == 2) {
  1033. r = cam->reg[0x18] & 0xcf;
  1034. r |= 0x10;
  1035. } else if (scale == 4)
  1036. r = cam->reg[0x18] | 0x20;
  1037. err += sn9c102_write_reg(cam, r, 0x18);
  1038. if (err)
  1039. return -EIO;
  1040. PDBGG("Scaling factor: %u", scale);
  1041. return 0;
  1042. }
  1043. static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
  1044. {
  1045. struct sn9c102_sensor* s = &cam->sensor;
  1046. u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
  1047. v_start = (u8)(rect->top - s->cropcap.bounds.top),
  1048. h_size = (u8)(rect->width / 16),
  1049. v_size = (u8)(rect->height / 16);
  1050. int err = 0;
  1051. err += sn9c102_write_reg(cam, h_start, 0x12);
  1052. err += sn9c102_write_reg(cam, v_start, 0x13);
  1053. err += sn9c102_write_reg(cam, h_size, 0x15);
  1054. err += sn9c102_write_reg(cam, v_size, 0x16);
  1055. if (err)
  1056. return -EIO;
  1057. PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
  1058. "%u %u %u %u", h_start, v_start, h_size, v_size);
  1059. return 0;
  1060. }
  1061. static int sn9c102_init(struct sn9c102_device* cam)
  1062. {
  1063. struct sn9c102_sensor* s = &cam->sensor;
  1064. struct v4l2_control ctrl;
  1065. struct v4l2_queryctrl *qctrl;
  1066. struct v4l2_rect* rect;
  1067. u8 i = 0;
  1068. int err = 0;
  1069. if (!(cam->state & DEV_INITIALIZED)) {
  1070. init_waitqueue_head(&cam->open);
  1071. qctrl = s->qctrl;
  1072. rect = &(s->cropcap.defrect);
  1073. } else { /* use current values */
  1074. qctrl = s->_qctrl;
  1075. rect = &(s->_rect);
  1076. }
  1077. err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
  1078. err += sn9c102_set_crop(cam, rect);
  1079. if (err)
  1080. return err;
  1081. if (s->init) {
  1082. err = s->init(cam);
  1083. if (err) {
  1084. DBG(3, "Sensor initialization failed");
  1085. return err;
  1086. }
  1087. }
  1088. if (!(cam->state & DEV_INITIALIZED))
  1089. cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
  1090. else
  1091. err += sn9c102_set_compression(cam, &cam->compression);
  1092. err += sn9c102_set_pix_format(cam, &s->pix_format);
  1093. if (s->set_pix_format)
  1094. err += s->set_pix_format(cam, &s->pix_format);
  1095. if (err)
  1096. return err;
  1097. if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
  1098. DBG(3, "Compressed video format is active, quality %d",
  1099. cam->compression.quality);
  1100. else
  1101. DBG(3, "Uncompressed video format is active");
  1102. if (s->set_crop)
  1103. if ((err = s->set_crop(cam, rect))) {
  1104. DBG(3, "set_crop() failed");
  1105. return err;
  1106. }
  1107. if (s->set_ctrl) {
  1108. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1109. if (s->qctrl[i].id != 0 &&
  1110. !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
  1111. ctrl.id = s->qctrl[i].id;
  1112. ctrl.value = qctrl[i].default_value;
  1113. err = s->set_ctrl(cam, &ctrl);
  1114. if (err) {
  1115. DBG(3, "Set %s control failed",
  1116. s->qctrl[i].name);
  1117. return err;
  1118. }
  1119. DBG(3, "Image sensor supports '%s' control",
  1120. s->qctrl[i].name);
  1121. }
  1122. }
  1123. if (!(cam->state & DEV_INITIALIZED)) {
  1124. mutex_init(&cam->fileop_mutex);
  1125. spin_lock_init(&cam->queue_lock);
  1126. init_waitqueue_head(&cam->wait_frame);
  1127. init_waitqueue_head(&cam->wait_stream);
  1128. cam->nreadbuffers = 2;
  1129. memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
  1130. memcpy(&(s->_rect), &(s->cropcap.defrect),
  1131. sizeof(struct v4l2_rect));
  1132. cam->state |= DEV_INITIALIZED;
  1133. }
  1134. DBG(2, "Initialization succeeded");
  1135. return 0;
  1136. }
  1137. static void sn9c102_release_resources(struct sn9c102_device* cam)
  1138. {
  1139. mutex_lock(&sn9c102_sysfs_lock);
  1140. DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
  1141. video_set_drvdata(cam->v4ldev, NULL);
  1142. video_unregister_device(cam->v4ldev);
  1143. usb_put_dev(cam->usbdev);
  1144. mutex_unlock(&sn9c102_sysfs_lock);
  1145. kfree(cam->control_buffer);
  1146. }
  1147. /*****************************************************************************/
  1148. static int sn9c102_open(struct inode* inode, struct file* filp)
  1149. {
  1150. struct sn9c102_device* cam;
  1151. int err = 0;
  1152. /*
  1153. This is the only safe way to prevent race conditions with
  1154. disconnect
  1155. */
  1156. if (!down_read_trylock(&sn9c102_disconnect))
  1157. return -ERESTARTSYS;
  1158. cam = video_get_drvdata(video_devdata(filp));
  1159. if (mutex_lock_interruptible(&cam->dev_mutex)) {
  1160. up_read(&sn9c102_disconnect);
  1161. return -ERESTARTSYS;
  1162. }
  1163. if (cam->users) {
  1164. DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
  1165. if ((filp->f_flags & O_NONBLOCK) ||
  1166. (filp->f_flags & O_NDELAY)) {
  1167. err = -EWOULDBLOCK;
  1168. goto out;
  1169. }
  1170. mutex_unlock(&cam->dev_mutex);
  1171. err = wait_event_interruptible_exclusive(cam->open,
  1172. cam->state & DEV_DISCONNECTED
  1173. || !cam->users);
  1174. if (err) {
  1175. up_read(&sn9c102_disconnect);
  1176. return err;
  1177. }
  1178. if (cam->state & DEV_DISCONNECTED) {
  1179. up_read(&sn9c102_disconnect);
  1180. return -ENODEV;
  1181. }
  1182. mutex_lock(&cam->dev_mutex);
  1183. }
  1184. if (cam->state & DEV_MISCONFIGURED) {
  1185. err = sn9c102_init(cam);
  1186. if (err) {
  1187. DBG(1, "Initialization failed again. "
  1188. "I will retry on next open().");
  1189. goto out;
  1190. }
  1191. cam->state &= ~DEV_MISCONFIGURED;
  1192. }
  1193. if ((err = sn9c102_start_transfer(cam)))
  1194. goto out;
  1195. filp->private_data = cam;
  1196. cam->users++;
  1197. cam->io = IO_NONE;
  1198. cam->stream = STREAM_OFF;
  1199. cam->nbuffers = 0;
  1200. cam->frame_count = 0;
  1201. sn9c102_empty_framequeues(cam);
  1202. DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
  1203. out:
  1204. mutex_unlock(&cam->dev_mutex);
  1205. up_read(&sn9c102_disconnect);
  1206. return err;
  1207. }
  1208. static int sn9c102_release(struct inode* inode, struct file* filp)
  1209. {
  1210. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  1211. mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
  1212. sn9c102_stop_transfer(cam);
  1213. sn9c102_release_buffers(cam);
  1214. if (cam->state & DEV_DISCONNECTED) {
  1215. sn9c102_release_resources(cam);
  1216. mutex_unlock(&cam->dev_mutex);
  1217. kfree(cam);
  1218. return 0;
  1219. }
  1220. cam->users--;
  1221. wake_up_interruptible_nr(&cam->open, 1);
  1222. DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
  1223. mutex_unlock(&cam->dev_mutex);
  1224. return 0;
  1225. }
  1226. static ssize_t
  1227. sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
  1228. {
  1229. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  1230. struct sn9c102_frame_t* f, * i;
  1231. unsigned long lock_flags;
  1232. long timeout;
  1233. int err = 0;
  1234. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1235. return -ERESTARTSYS;
  1236. if (cam->state & DEV_DISCONNECTED) {
  1237. DBG(1, "Device not present");
  1238. mutex_unlock(&cam->fileop_mutex);
  1239. return -ENODEV;
  1240. }
  1241. if (cam->state & DEV_MISCONFIGURED) {
  1242. DBG(1, "The camera is misconfigured. Close and open it "
  1243. "again.");
  1244. mutex_unlock(&cam->fileop_mutex);
  1245. return -EIO;
  1246. }
  1247. if (cam->io == IO_MMAP) {
  1248. DBG(3, "Close and open the device again to choose "
  1249. "the read method");
  1250. mutex_unlock(&cam->fileop_mutex);
  1251. return -EINVAL;
  1252. }
  1253. if (cam->io == IO_NONE) {
  1254. if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
  1255. DBG(1, "read() failed, not enough memory");
  1256. mutex_unlock(&cam->fileop_mutex);
  1257. return -ENOMEM;
  1258. }
  1259. cam->io = IO_READ;
  1260. cam->stream = STREAM_ON;
  1261. }
  1262. if (list_empty(&cam->inqueue)) {
  1263. if (!list_empty(&cam->outqueue))
  1264. sn9c102_empty_framequeues(cam);
  1265. sn9c102_queue_unusedframes(cam);
  1266. }
  1267. if (!count) {
  1268. mutex_unlock(&cam->fileop_mutex);
  1269. return 0;
  1270. }
  1271. if (list_empty(&cam->outqueue)) {
  1272. if (filp->f_flags & O_NONBLOCK) {
  1273. mutex_unlock(&cam->fileop_mutex);
  1274. return -EAGAIN;
  1275. }
  1276. timeout = wait_event_interruptible_timeout
  1277. ( cam->wait_frame,
  1278. (!list_empty(&cam->outqueue)) ||
  1279. (cam->state & DEV_DISCONNECTED) ||
  1280. (cam->state & DEV_MISCONFIGURED),
  1281. cam->module_param.frame_timeout *
  1282. 1000 * msecs_to_jiffies(1) );
  1283. if (timeout < 0) {
  1284. mutex_unlock(&cam->fileop_mutex);
  1285. return timeout;
  1286. }
  1287. if (cam->state & DEV_DISCONNECTED) {
  1288. mutex_unlock(&cam->fileop_mutex);
  1289. return -ENODEV;
  1290. }
  1291. if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
  1292. mutex_unlock(&cam->fileop_mutex);
  1293. return -EIO;
  1294. }
  1295. }
  1296. f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
  1297. if (count > f->buf.bytesused)
  1298. count = f->buf.bytesused;
  1299. if (copy_to_user(buf, f->bufmem, count)) {
  1300. err = -EFAULT;
  1301. goto exit;
  1302. }
  1303. *f_pos += count;
  1304. exit:
  1305. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1306. list_for_each_entry(i, &cam->outqueue, frame)
  1307. i->state = F_UNUSED;
  1308. INIT_LIST_HEAD(&cam->outqueue);
  1309. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1310. sn9c102_queue_unusedframes(cam);
  1311. PDBGG("Frame #%lu, bytes read: %zu",
  1312. (unsigned long)f->buf.index, count);
  1313. mutex_unlock(&cam->fileop_mutex);
  1314. return count;
  1315. }
  1316. static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
  1317. {
  1318. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  1319. struct sn9c102_frame_t* f;
  1320. unsigned long lock_flags;
  1321. unsigned int mask = 0;
  1322. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1323. return POLLERR;
  1324. if (cam->state & DEV_DISCONNECTED) {
  1325. DBG(1, "Device not present");
  1326. goto error;
  1327. }
  1328. if (cam->state & DEV_MISCONFIGURED) {
  1329. DBG(1, "The camera is misconfigured. Close and open it "
  1330. "again.");
  1331. goto error;
  1332. }
  1333. if (cam->io == IO_NONE) {
  1334. if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
  1335. IO_READ)) {
  1336. DBG(1, "poll() failed, not enough memory");
  1337. goto error;
  1338. }
  1339. cam->io = IO_READ;
  1340. cam->stream = STREAM_ON;
  1341. }
  1342. if (cam->io == IO_READ) {
  1343. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1344. list_for_each_entry(f, &cam->outqueue, frame)
  1345. f->state = F_UNUSED;
  1346. INIT_LIST_HEAD(&cam->outqueue);
  1347. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1348. sn9c102_queue_unusedframes(cam);
  1349. }
  1350. poll_wait(filp, &cam->wait_frame, wait);
  1351. if (!list_empty(&cam->outqueue))
  1352. mask |= POLLIN | POLLRDNORM;
  1353. mutex_unlock(&cam->fileop_mutex);
  1354. return mask;
  1355. error:
  1356. mutex_unlock(&cam->fileop_mutex);
  1357. return POLLERR;
  1358. }
  1359. static void sn9c102_vm_open(struct vm_area_struct* vma)
  1360. {
  1361. struct sn9c102_frame_t* f = vma->vm_private_data;
  1362. f->vma_use_count++;
  1363. }
  1364. static void sn9c102_vm_close(struct vm_area_struct* vma)
  1365. {
  1366. /* NOTE: buffers are not freed here */
  1367. struct sn9c102_frame_t* f = vma->vm_private_data;
  1368. f->vma_use_count--;
  1369. }
  1370. static struct vm_operations_struct sn9c102_vm_ops = {
  1371. .open = sn9c102_vm_open,
  1372. .close = sn9c102_vm_close,
  1373. };
  1374. static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
  1375. {
  1376. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  1377. unsigned long size = vma->vm_end - vma->vm_start,
  1378. start = vma->vm_start;
  1379. void *pos;
  1380. u32 i;
  1381. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1382. return -ERESTARTSYS;
  1383. if (cam->state & DEV_DISCONNECTED) {
  1384. DBG(1, "Device not present");
  1385. mutex_unlock(&cam->fileop_mutex);
  1386. return -ENODEV;
  1387. }
  1388. if (cam->state & DEV_MISCONFIGURED) {
  1389. DBG(1, "The camera is misconfigured. Close and open it "
  1390. "again.");
  1391. mutex_unlock(&cam->fileop_mutex);
  1392. return -EIO;
  1393. }
  1394. if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
  1395. size != PAGE_ALIGN(cam->frame[0].buf.length)) {
  1396. mutex_unlock(&cam->fileop_mutex);
  1397. return -EINVAL;
  1398. }
  1399. for (i = 0; i < cam->nbuffers; i++) {
  1400. if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
  1401. break;
  1402. }
  1403. if (i == cam->nbuffers) {
  1404. mutex_unlock(&cam->fileop_mutex);
  1405. return -EINVAL;
  1406. }
  1407. vma->vm_flags |= VM_IO;
  1408. vma->vm_flags |= VM_RESERVED;
  1409. pos = cam->frame[i].bufmem;
  1410. while (size > 0) { /* size is page-aligned */
  1411. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  1412. mutex_unlock(&cam->fileop_mutex);
  1413. return -EAGAIN;
  1414. }
  1415. start += PAGE_SIZE;
  1416. pos += PAGE_SIZE;
  1417. size -= PAGE_SIZE;
  1418. }
  1419. vma->vm_ops = &sn9c102_vm_ops;
  1420. vma->vm_private_data = &cam->frame[i];
  1421. sn9c102_vm_open(vma);
  1422. mutex_unlock(&cam->fileop_mutex);
  1423. return 0;
  1424. }
  1425. /*****************************************************************************/
  1426. static int
  1427. sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
  1428. {
  1429. struct v4l2_capability cap = {
  1430. .driver = "sn9c102",
  1431. .version = SN9C102_MODULE_VERSION_CODE,
  1432. .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  1433. V4L2_CAP_STREAMING,
  1434. };
  1435. strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
  1436. if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
  1437. strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
  1438. sizeof(cap.bus_info));
  1439. if (copy_to_user(arg, &cap, sizeof(cap)))
  1440. return -EFAULT;
  1441. return 0;
  1442. }
  1443. static int
  1444. sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
  1445. {
  1446. struct v4l2_input i;
  1447. if (copy_from_user(&i, arg, sizeof(i)))
  1448. return -EFAULT;
  1449. if (i.index)
  1450. return -EINVAL;
  1451. memset(&i, 0, sizeof(i));
  1452. strcpy(i.name, "Camera");
  1453. i.type = V4L2_INPUT_TYPE_CAMERA;
  1454. if (copy_to_user(arg, &i, sizeof(i)))
  1455. return -EFAULT;
  1456. return 0;
  1457. }
  1458. static int
  1459. sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
  1460. {
  1461. int index = 0;
  1462. if (copy_to_user(arg, &index, sizeof(index)))
  1463. return -EFAULT;
  1464. return 0;
  1465. }
  1466. static int
  1467. sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
  1468. {
  1469. int index;
  1470. if (copy_from_user(&index, arg, sizeof(index)))
  1471. return -EFAULT;
  1472. if (index != 0)
  1473. return -EINVAL;
  1474. return 0;
  1475. }
  1476. static int
  1477. sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
  1478. {
  1479. struct sn9c102_sensor* s = &cam->sensor;
  1480. struct v4l2_queryctrl qc;
  1481. u8 i;
  1482. if (copy_from_user(&qc, arg, sizeof(qc)))
  1483. return -EFAULT;
  1484. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1485. if (qc.id && qc.id == s->qctrl[i].id) {
  1486. memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
  1487. if (copy_to_user(arg, &qc, sizeof(qc)))
  1488. return -EFAULT;
  1489. return 0;
  1490. }
  1491. return -EINVAL;
  1492. }
  1493. static int
  1494. sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
  1495. {
  1496. struct sn9c102_sensor* s = &cam->sensor;
  1497. struct v4l2_control ctrl;
  1498. int err = 0;
  1499. u8 i;
  1500. if (!s->get_ctrl && !s->set_ctrl)
  1501. return -EINVAL;
  1502. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1503. return -EFAULT;
  1504. if (!s->get_ctrl) {
  1505. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1506. if (ctrl.id && ctrl.id == s->qctrl[i].id) {
  1507. ctrl.value = s->_qctrl[i].default_value;
  1508. goto exit;
  1509. }
  1510. return -EINVAL;
  1511. } else
  1512. err = s->get_ctrl(cam, &ctrl);
  1513. exit:
  1514. if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
  1515. return -EFAULT;
  1516. return err;
  1517. }
  1518. static int
  1519. sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
  1520. {
  1521. struct sn9c102_sensor* s = &cam->sensor;
  1522. struct v4l2_control ctrl;
  1523. u8 i;
  1524. int err = 0;
  1525. if (!s->set_ctrl)
  1526. return -EINVAL;
  1527. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1528. return -EFAULT;
  1529. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1530. if (ctrl.id == s->qctrl[i].id) {
  1531. if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
  1532. return -EINVAL;
  1533. if (ctrl.value < s->qctrl[i].minimum ||
  1534. ctrl.value > s->qctrl[i].maximum)
  1535. return -ERANGE;
  1536. ctrl.value -= ctrl.value % s->qctrl[i].step;
  1537. break;
  1538. }
  1539. if ((err = s->set_ctrl(cam, &ctrl)))
  1540. return err;
  1541. s->_qctrl[i].default_value = ctrl.value;
  1542. PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
  1543. (unsigned long)ctrl.id, (unsigned long)ctrl.value);
  1544. return 0;
  1545. }
  1546. static int
  1547. sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
  1548. {
  1549. struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
  1550. cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1551. cc->pixelaspect.numerator = 1;
  1552. cc->pixelaspect.denominator = 1;
  1553. if (copy_to_user(arg, cc, sizeof(*cc)))
  1554. return -EFAULT;
  1555. return 0;
  1556. }
  1557. static int
  1558. sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
  1559. {
  1560. struct sn9c102_sensor* s = &cam->sensor;
  1561. struct v4l2_crop crop = {
  1562. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1563. };
  1564. memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
  1565. if (copy_to_user(arg, &crop, sizeof(crop)))
  1566. return -EFAULT;
  1567. return 0;
  1568. }
  1569. static int
  1570. sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
  1571. {
  1572. struct sn9c102_sensor* s = &cam->sensor;
  1573. struct v4l2_crop crop;
  1574. struct v4l2_rect* rect;
  1575. struct v4l2_rect* bounds = &(s->cropcap.bounds);
  1576. struct v4l2_pix_format* pix_format = &(s->pix_format);
  1577. u8 scale;
  1578. const enum sn9c102_stream_state stream = cam->stream;
  1579. const u32 nbuffers = cam->nbuffers;
  1580. u32 i;
  1581. int err = 0;
  1582. if (copy_from_user(&crop, arg, sizeof(crop)))
  1583. return -EFAULT;
  1584. rect = &(crop.c);
  1585. if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1586. return -EINVAL;
  1587. if (cam->module_param.force_munmap)
  1588. for (i = 0; i < cam->nbuffers; i++)
  1589. if (cam->frame[i].vma_use_count) {
  1590. DBG(3, "VIDIOC_S_CROP failed. "
  1591. "Unmap the buffers first.");
  1592. return -EINVAL;
  1593. }
  1594. /* Preserve R,G or B origin */
  1595. rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
  1596. rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
  1597. if (rect->width < 16)
  1598. rect->width = 16;
  1599. if (rect->height < 16)
  1600. rect->height = 16;
  1601. if (rect->width > bounds->width)
  1602. rect->width = bounds->width;
  1603. if (rect->height > bounds->height)
  1604. rect->height = bounds->height;
  1605. if (rect->left < bounds->left)
  1606. rect->left = bounds->left;
  1607. if (rect->top < bounds->top)
  1608. rect->top = bounds->top;
  1609. if (rect->left + rect->width > bounds->left + bounds->width)
  1610. rect->left = bounds->left+bounds->width - rect->width;
  1611. if (rect->top + rect->height > bounds->top + bounds->height)
  1612. rect->top = bounds->top+bounds->height - rect->height;
  1613. rect->width &= ~15L;
  1614. rect->height &= ~15L;
  1615. if (SN9C102_PRESERVE_IMGSCALE) {
  1616. /* Calculate the actual scaling factor */
  1617. u32 a, b;
  1618. a = rect->width * rect->height;
  1619. b = pix_format->width * pix_format->height;
  1620. scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
  1621. } else
  1622. scale = 1;
  1623. if (cam->stream == STREAM_ON)
  1624. if ((err = sn9c102_stream_interrupt(cam)))
  1625. return err;
  1626. if (copy_to_user(arg, &crop, sizeof(crop))) {
  1627. cam->stream = stream;
  1628. return -EFAULT;
  1629. }
  1630. if (cam->module_param.force_munmap || cam->io == IO_READ)
  1631. sn9c102_release_buffers(cam);
  1632. err = sn9c102_set_crop(cam, rect);
  1633. if (s->set_crop)
  1634. err += s->set_crop(cam, rect);
  1635. err += sn9c102_set_scale(cam, scale);
  1636. if (err) { /* atomic, no rollback in ioctl() */
  1637. cam->state |= DEV_MISCONFIGURED;
  1638. DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
  1639. "use the camera, close and open /dev/video%d again.",
  1640. cam->v4ldev->minor);
  1641. return -EIO;
  1642. }
  1643. s->pix_format.width = rect->width/scale;
  1644. s->pix_format.height = rect->height/scale;
  1645. memcpy(&(s->_rect), rect, sizeof(*rect));
  1646. if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
  1647. nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
  1648. cam->state |= DEV_MISCONFIGURED;
  1649. DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
  1650. "use the camera, close and open /dev/video%d again.",
  1651. cam->v4ldev->minor);
  1652. return -ENOMEM;
  1653. }
  1654. if (cam->io == IO_READ)
  1655. sn9c102_empty_framequeues(cam);
  1656. else if (cam->module_param.force_munmap)
  1657. sn9c102_requeue_outqueue(cam);
  1658. cam->stream = stream;
  1659. return 0;
  1660. }
  1661. static int
  1662. sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
  1663. {
  1664. struct v4l2_fmtdesc fmtd;
  1665. if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
  1666. return -EFAULT;
  1667. if (fmtd.index == 0) {
  1668. strcpy(fmtd.description, "bayer rgb");
  1669. fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
  1670. } else if (fmtd.index == 1) {
  1671. strcpy(fmtd.description, "compressed");
  1672. fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
  1673. fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
  1674. } else
  1675. return -EINVAL;
  1676. fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1677. memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
  1678. if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
  1679. return -EFAULT;
  1680. return 0;
  1681. }
  1682. static int
  1683. sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
  1684. {
  1685. struct v4l2_format format;
  1686. struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
  1687. if (copy_from_user(&format, arg, sizeof(format)))
  1688. return -EFAULT;
  1689. if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1690. return -EINVAL;
  1691. pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
  1692. ? 0 : (pfmt->width * pfmt->priv) / 8;
  1693. pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
  1694. pfmt->field = V4L2_FIELD_NONE;
  1695. memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
  1696. if (copy_to_user(arg, &format, sizeof(format)))
  1697. return -EFAULT;
  1698. return 0;
  1699. }
  1700. static int
  1701. sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
  1702. void __user * arg)
  1703. {
  1704. struct sn9c102_sensor* s = &cam->sensor;
  1705. struct v4l2_format format;
  1706. struct v4l2_pix_format* pix;
  1707. struct v4l2_pix_format* pfmt = &(s->pix_format);
  1708. struct v4l2_rect* bounds = &(s->cropcap.bounds);
  1709. struct v4l2_rect rect;
  1710. u8 scale;
  1711. const enum sn9c102_stream_state stream = cam->stream;
  1712. const u32 nbuffers = cam->nbuffers;
  1713. u32 i;
  1714. int err = 0;
  1715. if (copy_from_user(&format, arg, sizeof(format)))
  1716. return -EFAULT;
  1717. pix = &(format.fmt.pix);
  1718. if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1719. return -EINVAL;
  1720. memcpy(&rect, &(s->_rect), sizeof(rect));
  1721. { /* calculate the actual scaling factor */
  1722. u32 a, b;
  1723. a = rect.width * rect.height;
  1724. b = pix->width * pix->height;
  1725. scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
  1726. }
  1727. rect.width = scale * pix->width;
  1728. rect.height = scale * pix->height;
  1729. if (rect.width < 16)
  1730. rect.width = 16;
  1731. if (rect.height < 16)
  1732. rect.height = 16;
  1733. if (rect.width > bounds->left + bounds->width - rect.left)
  1734. rect.width = bounds->left + bounds->width - rect.left;
  1735. if (rect.height > bounds->top + bounds->height - rect.top)
  1736. rect.height = bounds->top + bounds->height - rect.top;
  1737. rect.width &= ~15L;
  1738. rect.height &= ~15L;
  1739. { /* adjust the scaling factor */
  1740. u32 a, b;
  1741. a = rect.width * rect.height;
  1742. b = pix->width * pix->height;
  1743. scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
  1744. }
  1745. pix->width = rect.width / scale;
  1746. pix->height = rect.height / scale;
  1747. if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
  1748. pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
  1749. pix->pixelformat = pfmt->pixelformat;
  1750. pix->priv = pfmt->priv; /* bpp */
  1751. pix->colorspace = pfmt->colorspace;
  1752. pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  1753. ? 0 : (pix->width * pix->priv) / 8;
  1754. pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
  1755. pix->field = V4L2_FIELD_NONE;
  1756. if (cmd == VIDIOC_TRY_FMT) {
  1757. if (copy_to_user(arg, &format, sizeof(format)))
  1758. return -EFAULT;
  1759. return 0;
  1760. }
  1761. if (cam->module_param.force_munmap)
  1762. for (i = 0; i < cam->nbuffers; i++)
  1763. if (cam->frame[i].vma_use_count) {
  1764. DBG(3, "VIDIOC_S_FMT failed. Unmap the "
  1765. "buffers first.");
  1766. return -EINVAL;
  1767. }
  1768. if (cam->stream == STREAM_ON)
  1769. if ((err = sn9c102_stream_interrupt(cam)))
  1770. return err;
  1771. if (copy_to_user(arg, &format, sizeof(format))) {
  1772. cam->stream = stream;
  1773. return -EFAULT;
  1774. }
  1775. if (cam->module_param.force_munmap || cam->io == IO_READ)
  1776. sn9c102_release_buffers(cam);
  1777. err += sn9c102_set_pix_format(cam, pix);
  1778. err += sn9c102_set_crop(cam, &rect);
  1779. if (s->set_pix_format)
  1780. err += s->set_pix_format(cam, pix);
  1781. if (s->set_crop)
  1782. err += s->set_crop(cam, &rect);
  1783. err += sn9c102_set_scale(cam, scale);
  1784. if (err) { /* atomic, no rollback in ioctl() */
  1785. cam->state |= DEV_MISCONFIGURED;
  1786. DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
  1787. "use the camera, close and open /dev/video%d again.",
  1788. cam->v4ldev->minor);
  1789. return -EIO;
  1790. }
  1791. memcpy(pfmt, pix, sizeof(*pix));
  1792. memcpy(&(s->_rect), &rect, sizeof(rect));
  1793. if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
  1794. nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
  1795. cam->state |= DEV_MISCONFIGURED;
  1796. DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
  1797. "use the camera, close and open /dev/video%d again.",
  1798. cam->v4ldev->minor);
  1799. return -ENOMEM;
  1800. }
  1801. if (cam->io == IO_READ)
  1802. sn9c102_empty_framequeues(cam);
  1803. else if (cam->module_param.force_munmap)
  1804. sn9c102_requeue_outqueue(cam);
  1805. cam->stream = stream;
  1806. return 0;
  1807. }
  1808. static int
  1809. sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
  1810. {
  1811. if (copy_to_user(arg, &cam->compression,
  1812. sizeof(cam->compression)))
  1813. return -EFAULT;
  1814. return 0;
  1815. }
  1816. static int
  1817. sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
  1818. {
  1819. struct v4l2_jpegcompression jc;
  1820. const enum sn9c102_stream_state stream = cam->stream;
  1821. int err = 0;
  1822. if (copy_from_user(&jc, arg, sizeof(jc)))
  1823. return -EFAULT;
  1824. if (jc.quality != 0 && jc.quality != 1)
  1825. return -EINVAL;
  1826. if (cam->stream == STREAM_ON)
  1827. if ((err = sn9c102_stream_interrupt(cam)))
  1828. return err;
  1829. err += sn9c102_set_compression(cam, &jc);
  1830. if (err) { /* atomic, no rollback in ioctl() */
  1831. cam->state |= DEV_MISCONFIGURED;
  1832. DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
  1833. "problems. To use the camera, close and open "
  1834. "/dev/video%d again.", cam->v4ldev->minor);
  1835. return -EIO;
  1836. }
  1837. cam->compression.quality = jc.quality;
  1838. cam->stream = stream;
  1839. return 0;
  1840. }
  1841. static int
  1842. sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
  1843. {
  1844. struct v4l2_requestbuffers rb;
  1845. u32 i;
  1846. int err;
  1847. if (copy_from_user(&rb, arg, sizeof(rb)))
  1848. return -EFAULT;
  1849. if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1850. rb.memory != V4L2_MEMORY_MMAP)
  1851. return -EINVAL;
  1852. if (cam->io == IO_READ) {
  1853. DBG(3, "Close and open the device again to choose the mmap "
  1854. "I/O method");
  1855. return -EINVAL;
  1856. }
  1857. for (i = 0; i < cam->nbuffers; i++)
  1858. if (cam->frame[i].vma_use_count) {
  1859. DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
  1860. "still mapped.");
  1861. return -EINVAL;
  1862. }
  1863. if (cam->stream == STREAM_ON)
  1864. if ((err = sn9c102_stream_interrupt(cam)))
  1865. return err;
  1866. sn9c102_empty_framequeues(cam);
  1867. sn9c102_release_buffers(cam);
  1868. if (rb.count)
  1869. rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
  1870. if (copy_to_user(arg, &rb, sizeof(rb))) {
  1871. sn9c102_release_buffers(cam);
  1872. cam->io = IO_NONE;
  1873. return -EFAULT;
  1874. }
  1875. cam->io = rb.count ? IO_MMAP : IO_NONE;
  1876. return 0;
  1877. }
  1878. static int
  1879. sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
  1880. {
  1881. struct v4l2_buffer b;
  1882. if (copy_from_user(&b, arg, sizeof(b)))
  1883. return -EFAULT;
  1884. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1885. b.index >= cam->nbuffers || cam->io != IO_MMAP)
  1886. return -EINVAL;
  1887. memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
  1888. if (cam->frame[b.index].vma_use_count)
  1889. b.flags |= V4L2_BUF_FLAG_MAPPED;
  1890. if (cam->frame[b.index].state == F_DONE)
  1891. b.flags |= V4L2_BUF_FLAG_DONE;
  1892. else if (cam->frame[b.index].state != F_UNUSED)
  1893. b.flags |= V4L2_BUF_FLAG_QUEUED;
  1894. if (copy_to_user(arg, &b, sizeof(b)))
  1895. return -EFAULT;
  1896. return 0;
  1897. }
  1898. static int
  1899. sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
  1900. {
  1901. struct v4l2_buffer b;
  1902. unsigned long lock_flags;
  1903. if (copy_from_user(&b, arg, sizeof(b)))
  1904. return -EFAULT;
  1905. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1906. b.index >= cam->nbuffers || cam->io != IO_MMAP)
  1907. return -EINVAL;
  1908. if (cam->frame[b.index].state != F_UNUSED)
  1909. return -EINVAL;
  1910. cam->frame[b.index].state = F_QUEUED;
  1911. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1912. list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
  1913. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1914. PDBGG("Frame #%lu queued", (unsigned long)b.index);
  1915. return 0;
  1916. }
  1917. static int
  1918. sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
  1919. void __user * arg)
  1920. {
  1921. struct v4l2_buffer b;
  1922. struct sn9c102_frame_t *f;
  1923. unsigned long lock_flags;
  1924. long timeout;
  1925. if (copy_from_user(&b, arg, sizeof(b)))
  1926. return -EFAULT;
  1927. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
  1928. return -EINVAL;
  1929. if (list_empty(&cam->outqueue)) {
  1930. if (cam->stream == STREAM_OFF)
  1931. return -EINVAL;
  1932. if (filp->f_flags & O_NONBLOCK)
  1933. return -EAGAIN;
  1934. timeout = wait_event_interruptible_timeout
  1935. ( cam->wait_frame,
  1936. (!list_empty(&cam->outqueue)) ||
  1937. (cam->state & DEV_DISCONNECTED) ||
  1938. (cam->state & DEV_MISCONFIGURED),
  1939. cam->module_param.frame_timeout *
  1940. 1000 * msecs_to_jiffies(1) );
  1941. if (timeout < 0)
  1942. return timeout;
  1943. if (cam->state & DEV_DISCONNECTED)
  1944. return -ENODEV;
  1945. if (!timeout || (cam->state & DEV_MISCONFIGURED))
  1946. return -EIO;
  1947. }
  1948. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1949. f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
  1950. list_del(cam->outqueue.next);
  1951. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1952. f->state = F_UNUSED;
  1953. memcpy(&b, &f->buf, sizeof(b));
  1954. if (f->vma_use_count)
  1955. b.flags |= V4L2_BUF_FLAG_MAPPED;
  1956. if (copy_to_user(arg, &b, sizeof(b)))
  1957. return -EFAULT;
  1958. PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
  1959. return 0;
  1960. }
  1961. static int
  1962. sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
  1963. {
  1964. int type;
  1965. if (copy_from_user(&type, arg, sizeof(type)))
  1966. return -EFAULT;
  1967. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
  1968. return -EINVAL;
  1969. if (list_empty(&cam->inqueue))
  1970. return -EINVAL;
  1971. cam->stream = STREAM_ON;
  1972. DBG(3, "Stream on");
  1973. return 0;
  1974. }
  1975. static int
  1976. sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
  1977. {
  1978. int type, err;
  1979. if (copy_from_user(&type, arg, sizeof(type)))
  1980. return -EFAULT;
  1981. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
  1982. return -EINVAL;
  1983. if (cam->stream == STREAM_ON)
  1984. if ((err = sn9c102_stream_interrupt(cam)))
  1985. return err;
  1986. sn9c102_empty_framequeues(cam);
  1987. DBG(3, "Stream off");
  1988. return 0;
  1989. }
  1990. static int
  1991. sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
  1992. {
  1993. struct v4l2_streamparm sp;
  1994. if (copy_from_user(&sp, arg, sizeof(sp)))
  1995. return -EFAULT;
  1996. if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1997. return -EINVAL;
  1998. sp.parm.capture.extendedmode = 0;
  1999. sp.parm.capture.readbuffers = cam->nreadbuffers;
  2000. if (copy_to_user(arg, &sp, sizeof(sp)))
  2001. return -EFAULT;
  2002. return 0;
  2003. }
  2004. static int
  2005. sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
  2006. {
  2007. struct v4l2_streamparm sp;
  2008. if (copy_from_user(&sp, arg, sizeof(sp)))
  2009. return -EFAULT;
  2010. if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2011. return -EINVAL;
  2012. sp.parm.capture.extendedmode = 0;
  2013. if (sp.parm.capture.readbuffers == 0)
  2014. sp.parm.capture.readbuffers = cam->nreadbuffers;
  2015. if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
  2016. sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
  2017. if (copy_to_user(arg, &sp, sizeof(sp)))
  2018. return -EFAULT;
  2019. cam->nreadbuffers = sp.parm.capture.readbuffers;
  2020. return 0;
  2021. }
  2022. static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
  2023. unsigned int cmd, void __user * arg)
  2024. {
  2025. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  2026. switch (cmd) {
  2027. case VIDIOC_QUERYCAP:
  2028. return sn9c102_vidioc_querycap(cam, arg);
  2029. case VIDIOC_ENUMINPUT:
  2030. return sn9c102_vidioc_enuminput(cam, arg);
  2031. case VIDIOC_G_INPUT:
  2032. return sn9c102_vidioc_g_input(cam, arg);
  2033. case VIDIOC_S_INPUT:
  2034. return sn9c102_vidioc_s_input(cam, arg);
  2035. case VIDIOC_QUERYCTRL:
  2036. return sn9c102_vidioc_query_ctrl(cam, arg);
  2037. case VIDIOC_G_CTRL:
  2038. return sn9c102_vidioc_g_ctrl(cam, arg);
  2039. case VIDIOC_S_CTRL_OLD:
  2040. case VIDIOC_S_CTRL:
  2041. return sn9c102_vidioc_s_ctrl(cam, arg);
  2042. case VIDIOC_CROPCAP_OLD:
  2043. case VIDIOC_CROPCAP:
  2044. return sn9c102_vidioc_cropcap(cam, arg);
  2045. case VIDIOC_G_CROP:
  2046. return sn9c102_vidioc_g_crop(cam, arg);
  2047. case VIDIOC_S_CROP:
  2048. return sn9c102_vidioc_s_crop(cam, arg);
  2049. case VIDIOC_ENUM_FMT:
  2050. return sn9c102_vidioc_enum_fmt(cam, arg);
  2051. case VIDIOC_G_FMT:
  2052. return sn9c102_vidioc_g_fmt(cam, arg);
  2053. case VIDIOC_TRY_FMT:
  2054. case VIDIOC_S_FMT:
  2055. return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
  2056. case VIDIOC_G_JPEGCOMP:
  2057. return sn9c102_vidioc_g_jpegcomp(cam, arg);
  2058. case VIDIOC_S_JPEGCOMP:
  2059. return sn9c102_vidioc_s_jpegcomp(cam, arg);
  2060. case VIDIOC_REQBUFS:
  2061. return sn9c102_vidioc_reqbufs(cam, arg);
  2062. case VIDIOC_QUERYBUF:
  2063. return sn9c102_vidioc_querybuf(cam, arg);
  2064. case VIDIOC_QBUF:
  2065. return sn9c102_vidioc_qbuf(cam, arg);
  2066. case VIDIOC_DQBUF:
  2067. return sn9c102_vidioc_dqbuf(cam, filp, arg);
  2068. case VIDIOC_STREAMON:
  2069. return sn9c102_vidioc_streamon(cam, arg);
  2070. case VIDIOC_STREAMOFF:
  2071. return sn9c102_vidioc_streamoff(cam, arg);
  2072. case VIDIOC_G_PARM:
  2073. return sn9c102_vidioc_g_parm(cam, arg);
  2074. case VIDIOC_S_PARM_OLD:
  2075. case VIDIOC_S_PARM:
  2076. return sn9c102_vidioc_s_parm(cam, arg);
  2077. case VIDIOC_G_STD:
  2078. case VIDIOC_S_STD:
  2079. case VIDIOC_QUERYSTD:
  2080. case VIDIOC_ENUMSTD:
  2081. case VIDIOC_QUERYMENU:
  2082. return -EINVAL;
  2083. default:
  2084. return -EINVAL;
  2085. }
  2086. }
  2087. static int sn9c102_ioctl(struct inode* inode, struct file* filp,
  2088. unsigned int cmd, unsigned long arg)
  2089. {
  2090. struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
  2091. int err = 0;
  2092. if (mutex_lock_interruptible(&cam->fileop_mutex))
  2093. return -ERESTARTSYS;
  2094. if (cam->state & DEV_DISCONNECTED) {
  2095. DBG(1, "Device not present");
  2096. mutex_unlock(&cam->fileop_mutex);
  2097. return -ENODEV;
  2098. }
  2099. if (cam->state & DEV_MISCONFIGURED) {
  2100. DBG(1, "The camera is misconfigured. Close and open it "
  2101. "again.");
  2102. mutex_unlock(&cam->fileop_mutex);
  2103. return -EIO;
  2104. }
  2105. V4LDBG(3, "sn9c102", cmd);
  2106. err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
  2107. mutex_unlock(&cam->fileop_mutex);
  2108. return err;
  2109. }
  2110. /*****************************************************************************/
  2111. static struct file_operations sn9c102_fops = {
  2112. .owner = THIS_MODULE,
  2113. .open = sn9c102_open,
  2114. .release = sn9c102_release,
  2115. .ioctl = sn9c102_ioctl,
  2116. .read = sn9c102_read,
  2117. .poll = sn9c102_poll,
  2118. .mmap = sn9c102_mmap,
  2119. .llseek = no_llseek,
  2120. };
  2121. /*****************************************************************************/
  2122. /* It exists a single interface only. We do not need to validate anything. */
  2123. static int
  2124. sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
  2125. {
  2126. struct usb_device *udev = interface_to_usbdev(intf);
  2127. struct sn9c102_device* cam;
  2128. static unsigned int dev_nr = 0;
  2129. unsigned int i;
  2130. int err = 0, r;
  2131. if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
  2132. return -ENOMEM;
  2133. cam->usbdev = udev;
  2134. if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
  2135. DBG(1, "kmalloc() failed");
  2136. err = -ENOMEM;
  2137. goto fail;
  2138. }
  2139. if (!(cam->v4ldev = video_device_alloc())) {
  2140. DBG(1, "video_device_alloc() failed");
  2141. err = -ENOMEM;
  2142. goto fail;
  2143. }
  2144. mutex_init(&cam->dev_mutex);
  2145. r = sn9c102_read_reg(cam, 0x00);
  2146. if (r < 0 || r != 0x10) {
  2147. DBG(1, "Sorry, this is not a SN9C10x based camera "
  2148. "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
  2149. err = -ENODEV;
  2150. goto fail;
  2151. }
  2152. cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ?
  2153. BRIDGE_SN9C103 : BRIDGE_SN9C102;
  2154. switch (cam->bridge) {
  2155. case BRIDGE_SN9C101:
  2156. case BRIDGE_SN9C102:
  2157. DBG(2, "SN9C10[12] PC Camera Controller detected "
  2158. "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
  2159. break;
  2160. case BRIDGE_SN9C103:
  2161. DBG(2, "SN9C103 PC Camera Controller detected "
  2162. "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
  2163. break;
  2164. }
  2165. for (i = 0; sn9c102_sensor_table[i]; i++) {
  2166. err = sn9c102_sensor_table[i](cam);
  2167. if (!err)
  2168. break;
  2169. }
  2170. if (!err) {
  2171. DBG(2, "%s image sensor detected", cam->sensor.name);
  2172. DBG(3, "Support for %s maintained by %s",
  2173. cam->sensor.name, cam->sensor.maintainer);
  2174. } else {
  2175. DBG(1, "No supported image sensor detected");
  2176. err = -ENODEV;
  2177. goto fail;
  2178. }
  2179. if (sn9c102_init(cam)) {
  2180. DBG(1, "Initialization failed. I will retry on open().");
  2181. cam->state |= DEV_MISCONFIGURED;
  2182. }
  2183. strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
  2184. cam->v4ldev->owner = THIS_MODULE;
  2185. cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
  2186. cam->v4ldev->hardware = 0;
  2187. cam->v4ldev->fops = &sn9c102_fops;
  2188. cam->v4ldev->minor = video_nr[dev_nr];
  2189. cam->v4ldev->release = video_device_release;
  2190. video_set_drvdata(cam->v4ldev, cam);
  2191. mutex_lock(&cam->dev_mutex);
  2192. err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
  2193. video_nr[dev_nr]);
  2194. if (err) {
  2195. DBG(1, "V4L2 device registration failed");
  2196. if (err == -ENFILE && video_nr[dev_nr] == -1)
  2197. DBG(1, "Free /dev/videoX node not found");
  2198. video_nr[dev_nr] = -1;
  2199. dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
  2200. mutex_unlock(&cam->dev_mutex);
  2201. goto fail;
  2202. }
  2203. DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
  2204. cam->module_param.force_munmap = force_munmap[dev_nr];
  2205. cam->module_param.frame_timeout = frame_timeout[dev_nr];
  2206. dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
  2207. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2208. sn9c102_create_sysfs(cam);
  2209. DBG(2, "Optional device control through 'sysfs' interface ready");
  2210. #endif
  2211. usb_set_intfdata(intf, cam);
  2212. mutex_unlock(&cam->dev_mutex);
  2213. return 0;
  2214. fail:
  2215. if (cam) {
  2216. kfree(cam->control_buffer);
  2217. if (cam->v4ldev)
  2218. video_device_release(cam->v4ldev);
  2219. kfree(cam);
  2220. }
  2221. return err;
  2222. }
  2223. static void sn9c102_usb_disconnect(struct usb_interface* intf)
  2224. {
  2225. struct sn9c102_device* cam = usb_get_intfdata(intf);
  2226. if (!cam)
  2227. return;
  2228. down_write(&sn9c102_disconnect);
  2229. mutex_lock(&cam->dev_mutex);
  2230. DBG(2, "Disconnecting %s...", cam->v4ldev->name);
  2231. wake_up_interruptible_all(&cam->open);
  2232. if (cam->users) {
  2233. DBG(2, "Device /dev/video%d is open! Deregistration and "
  2234. "memory deallocation are deferred on close.",
  2235. cam->v4ldev->minor);
  2236. cam->state |= DEV_MISCONFIGURED;
  2237. sn9c102_stop_transfer(cam);
  2238. cam->state |= DEV_DISCONNECTED;
  2239. wake_up_interruptible(&cam->wait_frame);
  2240. wake_up(&cam->wait_stream);
  2241. usb_get_dev(cam->usbdev);
  2242. } else {
  2243. cam->state |= DEV_DISCONNECTED;
  2244. sn9c102_release_resources(cam);
  2245. }
  2246. mutex_unlock(&cam->dev_mutex);
  2247. if (!cam->users)
  2248. kfree(cam);
  2249. up_write(&sn9c102_disconnect);
  2250. }
  2251. static struct usb_driver sn9c102_usb_driver = {
  2252. .name = "sn9c102",
  2253. .id_table = sn9c102_id_table,
  2254. .probe = sn9c102_usb_probe,
  2255. .disconnect = sn9c102_usb_disconnect,
  2256. };
  2257. /*****************************************************************************/
  2258. static int __init sn9c102_module_init(void)
  2259. {
  2260. int err = 0;
  2261. KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
  2262. KDBG(3, SN9C102_MODULE_AUTHOR);
  2263. if ((err = usb_register(&sn9c102_usb_driver)))
  2264. KDBG(1, "usb_register() failed");
  2265. return err;
  2266. }
  2267. static void __exit sn9c102_module_exit(void)
  2268. {
  2269. usb_deregister(&sn9c102_usb_driver);
  2270. }
  2271. module_init(sn9c102_module_init);
  2272. module_exit(sn9c102_module_exit);