sn9c102_core.c 68 KB

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