et61x251_core.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706
  1. /***************************************************************************
  2. * V4L2 driver for ET61X[12]51 PC Camera Controllers *
  3. * *
  4. * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the Free Software *
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
  19. ***************************************************************************/
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/version.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/param.h>
  26. #include <linux/errno.h>
  27. #include <linux/slab.h>
  28. #include <linux/device.h>
  29. #include <linux/fs.h>
  30. #include <linux/delay.h>
  31. #include <linux/compiler.h>
  32. #include <linux/ioctl.h>
  33. #include <linux/poll.h>
  34. #include <linux/stat.h>
  35. #include <linux/mm.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/page-flags.h>
  38. #include <media/v4l2-ioctl.h>
  39. #include <asm/byteorder.h>
  40. #include <asm/page.h>
  41. #include <asm/uaccess.h>
  42. #include "et61x251.h"
  43. /*****************************************************************************/
  44. #define ET61X251_MODULE_NAME "V4L2 driver for ET61X[12]51 " \
  45. "PC Camera Controllers"
  46. #define ET61X251_MODULE_AUTHOR "(C) 2006-2007 Luca Risolia"
  47. #define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
  48. #define ET61X251_MODULE_LICENSE "GPL"
  49. #define ET61X251_MODULE_VERSION "1.1.10"
  50. /*****************************************************************************/
  51. MODULE_DEVICE_TABLE(usb, et61x251_id_table);
  52. MODULE_AUTHOR(ET61X251_MODULE_AUTHOR " " ET61X251_AUTHOR_EMAIL);
  53. MODULE_DESCRIPTION(ET61X251_MODULE_NAME);
  54. MODULE_VERSION(ET61X251_MODULE_VERSION);
  55. MODULE_LICENSE(ET61X251_MODULE_LICENSE);
  56. static short video_nr[] = {[0 ... ET61X251_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 "
  63. __MODULE_STRING(ET61X251_MAX_DEVICES) " cameras this way."
  64. "\nFor example:"
  65. "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
  66. "\nthe second registered camera and use auto for the first"
  67. "\none and for every other camera."
  68. "\n");
  69. static short force_munmap[] = {[0 ... ET61X251_MAX_DEVICES-1] =
  70. ET61X251_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(ET61X251_FORCE_MUNMAP)"."
  81. "\n");
  82. static unsigned int frame_timeout[] = {[0 ... ET61X251_MAX_DEVICES-1] =
  83. ET61X251_FRAME_TIMEOUT};
  84. module_param_array(frame_timeout, uint, NULL, 0644);
  85. MODULE_PARM_DESC(frame_timeout,
  86. "\n<n[,...]> Timeout for a video frame in seconds."
  87. "\nThis parameter is specific for each detected camera."
  88. "\nDefault value is "
  89. __MODULE_STRING(ET61X251_FRAME_TIMEOUT)"."
  90. "\n");
  91. #ifdef ET61X251_DEBUG
  92. static unsigned short debug = ET61X251_DEBUG_LEVEL;
  93. module_param(debug, ushort, 0644);
  94. MODULE_PARM_DESC(debug,
  95. "\n<n> Debugging information level, from 0 to 3:"
  96. "\n0 = none (use carefully)"
  97. "\n1 = critical errors"
  98. "\n2 = significant informations"
  99. "\n3 = more verbose messages"
  100. "\nLevel 3 is useful for testing only, when only "
  101. "one device is used."
  102. "\nDefault value is "__MODULE_STRING(ET61X251_DEBUG_LEVEL)"."
  103. "\n");
  104. #endif
  105. /*****************************************************************************/
  106. static u32
  107. et61x251_request_buffers(struct et61x251_device* cam, u32 count,
  108. enum et61x251_io_method io)
  109. {
  110. struct v4l2_pix_format* p = &(cam->sensor.pix_format);
  111. struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
  112. const size_t imagesize = cam->module_param.force_munmap ||
  113. io == IO_READ ?
  114. (p->width * p->height * p->priv) / 8 :
  115. (r->width * r->height * p->priv) / 8;
  116. void* buff = NULL;
  117. u32 i;
  118. if (count > ET61X251_MAX_FRAMES)
  119. count = ET61X251_MAX_FRAMES;
  120. cam->nbuffers = count;
  121. while (cam->nbuffers > 0) {
  122. if ((buff = vmalloc_32_user(cam->nbuffers *
  123. PAGE_ALIGN(imagesize))))
  124. break;
  125. cam->nbuffers--;
  126. }
  127. for (i = 0; i < cam->nbuffers; i++) {
  128. cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
  129. cam->frame[i].buf.index = i;
  130. cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
  131. cam->frame[i].buf.length = imagesize;
  132. cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  133. cam->frame[i].buf.sequence = 0;
  134. cam->frame[i].buf.field = V4L2_FIELD_NONE;
  135. cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
  136. cam->frame[i].buf.flags = 0;
  137. }
  138. return cam->nbuffers;
  139. }
  140. static void et61x251_release_buffers(struct et61x251_device* cam)
  141. {
  142. if (cam->nbuffers) {
  143. vfree(cam->frame[0].bufmem);
  144. cam->nbuffers = 0;
  145. }
  146. cam->frame_current = NULL;
  147. }
  148. static void et61x251_empty_framequeues(struct et61x251_device* cam)
  149. {
  150. u32 i;
  151. INIT_LIST_HEAD(&cam->inqueue);
  152. INIT_LIST_HEAD(&cam->outqueue);
  153. for (i = 0; i < ET61X251_MAX_FRAMES; i++) {
  154. cam->frame[i].state = F_UNUSED;
  155. cam->frame[i].buf.bytesused = 0;
  156. }
  157. }
  158. static void et61x251_requeue_outqueue(struct et61x251_device* cam)
  159. {
  160. struct et61x251_frame_t *i;
  161. list_for_each_entry(i, &cam->outqueue, frame) {
  162. i->state = F_QUEUED;
  163. list_add(&i->frame, &cam->inqueue);
  164. }
  165. INIT_LIST_HEAD(&cam->outqueue);
  166. }
  167. static void et61x251_queue_unusedframes(struct et61x251_device* cam)
  168. {
  169. unsigned long lock_flags;
  170. u32 i;
  171. for (i = 0; i < cam->nbuffers; i++)
  172. if (cam->frame[i].state == F_UNUSED) {
  173. cam->frame[i].state = F_QUEUED;
  174. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  175. list_add_tail(&cam->frame[i].frame, &cam->inqueue);
  176. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  177. }
  178. }
  179. /*****************************************************************************/
  180. int et61x251_write_reg(struct et61x251_device* cam, u8 value, u16 index)
  181. {
  182. struct usb_device* udev = cam->usbdev;
  183. u8* buff = cam->control_buffer;
  184. int res;
  185. *buff = value;
  186. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
  187. 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
  188. if (res < 0) {
  189. DBG(3, "Failed to write a register (value 0x%02X, index "
  190. "0x%02X, error %d)", value, index, res);
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. static int et61x251_read_reg(struct et61x251_device* cam, u16 index)
  196. {
  197. struct usb_device* udev = cam->usbdev;
  198. u8* buff = cam->control_buffer;
  199. int res;
  200. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
  201. 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
  202. if (res < 0)
  203. DBG(3, "Failed to read a register (index 0x%02X, error %d)",
  204. index, res);
  205. return (res >= 0) ? (int)(*buff) : -1;
  206. }
  207. static int
  208. et61x251_i2c_wait(struct et61x251_device* cam,
  209. const struct et61x251_sensor* sensor)
  210. {
  211. int i, r;
  212. for (i = 1; i <= 8; i++) {
  213. if (sensor->interface == ET61X251_I2C_3WIRES) {
  214. r = et61x251_read_reg(cam, 0x8e);
  215. if (!(r & 0x02) && (r >= 0))
  216. return 0;
  217. } else {
  218. r = et61x251_read_reg(cam, 0x8b);
  219. if (!(r & 0x01) && (r >= 0))
  220. return 0;
  221. }
  222. if (r < 0)
  223. return -EIO;
  224. udelay(8*8); /* minimum for sensors at 400kHz */
  225. }
  226. return -EBUSY;
  227. }
  228. int
  229. et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
  230. u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
  231. u8 data8, u8 address)
  232. {
  233. struct usb_device* udev = cam->usbdev;
  234. u8* data = cam->control_buffer;
  235. int err = 0, res;
  236. data[0] = data2;
  237. data[1] = data3;
  238. data[2] = data4;
  239. data[3] = data5;
  240. data[4] = data6;
  241. data[5] = data7;
  242. data[6] = data8;
  243. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
  244. 0, 0x81, data, n-1, ET61X251_CTRL_TIMEOUT);
  245. if (res < 0)
  246. err += res;
  247. data[0] = address;
  248. data[1] = cam->sensor.i2c_slave_id;
  249. data[2] = cam->sensor.rsta | 0x02 | (n << 4);
  250. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
  251. 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
  252. if (res < 0)
  253. err += res;
  254. /* Start writing through the serial interface */
  255. data[0] = data1;
  256. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
  257. 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
  258. if (res < 0)
  259. err += res;
  260. err += et61x251_i2c_wait(cam, &cam->sensor);
  261. if (err)
  262. DBG(3, "I2C raw write failed for %s image sensor",
  263. cam->sensor.name);
  264. PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
  265. "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
  266. " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n, address,
  267. data1, data2, data3, data4, data5, data6, data7, data8);
  268. return err ? -1 : 0;
  269. }
  270. /*****************************************************************************/
  271. static void et61x251_urb_complete(struct urb *urb)
  272. {
  273. struct et61x251_device* cam = urb->context;
  274. struct et61x251_frame_t** f;
  275. size_t imagesize;
  276. u8 i;
  277. int err = 0;
  278. if (urb->status == -ENOENT)
  279. return;
  280. f = &cam->frame_current;
  281. if (cam->stream == STREAM_INTERRUPT) {
  282. cam->stream = STREAM_OFF;
  283. if ((*f))
  284. (*f)->state = F_QUEUED;
  285. DBG(3, "Stream interrupted");
  286. wake_up(&cam->wait_stream);
  287. }
  288. if (cam->state & DEV_DISCONNECTED)
  289. return;
  290. if (cam->state & DEV_MISCONFIGURED) {
  291. wake_up_interruptible(&cam->wait_frame);
  292. return;
  293. }
  294. if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
  295. goto resubmit_urb;
  296. if (!(*f))
  297. (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
  298. frame);
  299. imagesize = (cam->sensor.pix_format.width *
  300. cam->sensor.pix_format.height *
  301. cam->sensor.pix_format.priv) / 8;
  302. for (i = 0; i < urb->number_of_packets; i++) {
  303. unsigned int len, status;
  304. void *pos;
  305. u8* b1, * b2, sof;
  306. const u8 VOID_BYTES = 6;
  307. size_t imglen;
  308. len = urb->iso_frame_desc[i].actual_length;
  309. status = urb->iso_frame_desc[i].status;
  310. pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
  311. if (status) {
  312. DBG(3, "Error in isochronous frame");
  313. (*f)->state = F_ERROR;
  314. continue;
  315. }
  316. b1 = pos++;
  317. b2 = pos++;
  318. sof = ((*b1 & 0x3f) == 63);
  319. imglen = ((*b1 & 0xc0) << 2) | *b2;
  320. PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
  321. len, i, imglen);
  322. if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
  323. start_of_frame:
  324. if (sof) {
  325. (*f)->state = F_GRABBING;
  326. (*f)->buf.bytesused = 0;
  327. do_gettimeofday(&(*f)->buf.timestamp);
  328. pos += 22;
  329. DBG(3, "SOF detected: new video frame");
  330. }
  331. if ((*f)->state == F_GRABBING) {
  332. if (sof && (*f)->buf.bytesused) {
  333. if (cam->sensor.pix_format.pixelformat ==
  334. V4L2_PIX_FMT_ET61X251)
  335. goto end_of_frame;
  336. else {
  337. DBG(3, "Not expected SOF detected "
  338. "after %lu bytes",
  339. (unsigned long)(*f)->buf.bytesused);
  340. (*f)->state = F_ERROR;
  341. continue;
  342. }
  343. }
  344. if ((*f)->buf.bytesused + imglen > imagesize) {
  345. DBG(3, "Video frame size exceeded");
  346. (*f)->state = F_ERROR;
  347. continue;
  348. }
  349. pos += VOID_BYTES;
  350. memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, imglen);
  351. (*f)->buf.bytesused += imglen;
  352. if ((*f)->buf.bytesused == imagesize) {
  353. u32 b;
  354. end_of_frame:
  355. b = (*f)->buf.bytesused;
  356. (*f)->state = F_DONE;
  357. (*f)->buf.sequence= ++cam->frame_count;
  358. spin_lock(&cam->queue_lock);
  359. list_move_tail(&(*f)->frame, &cam->outqueue);
  360. if (!list_empty(&cam->inqueue))
  361. (*f) = list_entry(cam->inqueue.next,
  362. struct et61x251_frame_t,
  363. frame);
  364. else
  365. (*f) = NULL;
  366. spin_unlock(&cam->queue_lock);
  367. DBG(3, "Video frame captured: : %lu bytes",
  368. (unsigned long)(b));
  369. if (!(*f))
  370. goto resubmit_urb;
  371. if (sof &&
  372. cam->sensor.pix_format.pixelformat ==
  373. V4L2_PIX_FMT_ET61X251)
  374. goto start_of_frame;
  375. }
  376. }
  377. }
  378. resubmit_urb:
  379. urb->dev = cam->usbdev;
  380. err = usb_submit_urb(urb, GFP_ATOMIC);
  381. if (err < 0 && err != -EPERM) {
  382. cam->state |= DEV_MISCONFIGURED;
  383. DBG(1, "usb_submit_urb() failed");
  384. }
  385. wake_up_interruptible(&cam->wait_frame);
  386. }
  387. static int et61x251_start_transfer(struct et61x251_device* cam)
  388. {
  389. struct usb_device *udev = cam->usbdev;
  390. struct urb* urb;
  391. struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
  392. usb_ifnum_to_if(udev, 0),
  393. ET61X251_ALTERNATE_SETTING);
  394. const unsigned int psz = le16_to_cpu(altsetting->
  395. endpoint[0].desc.wMaxPacketSize);
  396. s8 i, j;
  397. int err = 0;
  398. for (i = 0; i < ET61X251_URBS; i++) {
  399. cam->transfer_buffer[i] = kzalloc(ET61X251_ISO_PACKETS * psz,
  400. GFP_KERNEL);
  401. if (!cam->transfer_buffer[i]) {
  402. err = -ENOMEM;
  403. DBG(1, "Not enough memory");
  404. goto free_buffers;
  405. }
  406. }
  407. for (i = 0; i < ET61X251_URBS; i++) {
  408. urb = usb_alloc_urb(ET61X251_ISO_PACKETS, GFP_KERNEL);
  409. cam->urb[i] = urb;
  410. if (!urb) {
  411. err = -ENOMEM;
  412. DBG(1, "usb_alloc_urb() failed");
  413. goto free_urbs;
  414. }
  415. urb->dev = udev;
  416. urb->context = cam;
  417. urb->pipe = usb_rcvisocpipe(udev, 1);
  418. urb->transfer_flags = URB_ISO_ASAP;
  419. urb->number_of_packets = ET61X251_ISO_PACKETS;
  420. urb->complete = et61x251_urb_complete;
  421. urb->transfer_buffer = cam->transfer_buffer[i];
  422. urb->transfer_buffer_length = psz * ET61X251_ISO_PACKETS;
  423. urb->interval = 1;
  424. for (j = 0; j < ET61X251_ISO_PACKETS; j++) {
  425. urb->iso_frame_desc[j].offset = psz * j;
  426. urb->iso_frame_desc[j].length = psz;
  427. }
  428. }
  429. err = et61x251_write_reg(cam, 0x01, 0x03);
  430. err = et61x251_write_reg(cam, 0x00, 0x03);
  431. err = et61x251_write_reg(cam, 0x08, 0x03);
  432. if (err) {
  433. err = -EIO;
  434. DBG(1, "I/O hardware error");
  435. goto free_urbs;
  436. }
  437. err = usb_set_interface(udev, 0, ET61X251_ALTERNATE_SETTING);
  438. if (err) {
  439. DBG(1, "usb_set_interface() failed");
  440. goto free_urbs;
  441. }
  442. cam->frame_current = NULL;
  443. for (i = 0; i < ET61X251_URBS; i++) {
  444. err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
  445. if (err) {
  446. for (j = i-1; j >= 0; j--)
  447. usb_kill_urb(cam->urb[j]);
  448. DBG(1, "usb_submit_urb() failed, error %d", err);
  449. goto free_urbs;
  450. }
  451. }
  452. return 0;
  453. free_urbs:
  454. for (i = 0; (i < ET61X251_URBS) && cam->urb[i]; i++)
  455. usb_free_urb(cam->urb[i]);
  456. free_buffers:
  457. for (i = 0; (i < ET61X251_URBS) && cam->transfer_buffer[i]; i++)
  458. kfree(cam->transfer_buffer[i]);
  459. return err;
  460. }
  461. static int et61x251_stop_transfer(struct et61x251_device* cam)
  462. {
  463. struct usb_device *udev = cam->usbdev;
  464. s8 i;
  465. int err = 0;
  466. if (cam->state & DEV_DISCONNECTED)
  467. return 0;
  468. for (i = ET61X251_URBS-1; i >= 0; i--) {
  469. usb_kill_urb(cam->urb[i]);
  470. usb_free_urb(cam->urb[i]);
  471. kfree(cam->transfer_buffer[i]);
  472. }
  473. err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
  474. if (err)
  475. DBG(3, "usb_set_interface() failed");
  476. return err;
  477. }
  478. static int et61x251_stream_interrupt(struct et61x251_device* cam)
  479. {
  480. long timeout;
  481. cam->stream = STREAM_INTERRUPT;
  482. timeout = wait_event_timeout(cam->wait_stream,
  483. (cam->stream == STREAM_OFF) ||
  484. (cam->state & DEV_DISCONNECTED),
  485. ET61X251_URB_TIMEOUT);
  486. if (cam->state & DEV_DISCONNECTED)
  487. return -ENODEV;
  488. else if (cam->stream != STREAM_OFF) {
  489. cam->state |= DEV_MISCONFIGURED;
  490. DBG(1, "URB timeout reached. The camera is misconfigured. To "
  491. "use it, close and open %s again.",
  492. video_device_node_name(cam->v4ldev));
  493. return -EIO;
  494. }
  495. return 0;
  496. }
  497. /*****************************************************************************/
  498. #ifdef CONFIG_VIDEO_ADV_DEBUG
  499. static int et61x251_i2c_try_read(struct et61x251_device* cam,
  500. const struct et61x251_sensor* sensor,
  501. u8 address)
  502. {
  503. struct usb_device* udev = cam->usbdev;
  504. u8* data = cam->control_buffer;
  505. int err = 0, res;
  506. data[0] = address;
  507. data[1] = cam->sensor.i2c_slave_id;
  508. data[2] = cam->sensor.rsta | 0x10;
  509. data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
  510. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
  511. 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
  512. if (res < 0)
  513. err += res;
  514. err += et61x251_i2c_wait(cam, sensor);
  515. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
  516. 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
  517. if (res < 0)
  518. err += res;
  519. if (err)
  520. DBG(3, "I2C read failed for %s image sensor", sensor->name);
  521. PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
  522. return err ? -1 : (int)data[0];
  523. }
  524. static int et61x251_i2c_try_write(struct et61x251_device* cam,
  525. const struct et61x251_sensor* sensor,
  526. u8 address, u8 value)
  527. {
  528. struct usb_device* udev = cam->usbdev;
  529. u8* data = cam->control_buffer;
  530. int err = 0, res;
  531. data[0] = address;
  532. data[1] = cam->sensor.i2c_slave_id;
  533. data[2] = cam->sensor.rsta | 0x12;
  534. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
  535. 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
  536. if (res < 0)
  537. err += res;
  538. data[0] = value;
  539. res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
  540. 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
  541. if (res < 0)
  542. err += res;
  543. err += et61x251_i2c_wait(cam, sensor);
  544. if (err)
  545. DBG(3, "I2C write failed for %s image sensor", sensor->name);
  546. PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
  547. return err ? -1 : 0;
  548. }
  549. static int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
  550. {
  551. return et61x251_i2c_try_read(cam, &cam->sensor, address);
  552. }
  553. static int et61x251_i2c_write(struct et61x251_device* cam,
  554. u8 address, u8 value)
  555. {
  556. return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
  557. }
  558. static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
  559. {
  560. char str[5];
  561. char* endp;
  562. unsigned long val;
  563. if (len < 4) {
  564. strncpy(str, buff, len);
  565. str[len] = '\0';
  566. } else {
  567. strncpy(str, buff, 4);
  568. str[4] = '\0';
  569. }
  570. val = simple_strtoul(str, &endp, 0);
  571. *count = 0;
  572. if (val <= 0xff)
  573. *count = (ssize_t)(endp - str);
  574. if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
  575. *count += 1;
  576. return (u8)val;
  577. }
  578. /*
  579. NOTE 1: being inside one of the following methods implies that the v4l
  580. device exists for sure (see kobjects and reference counters)
  581. NOTE 2: buffers are PAGE_SIZE long
  582. */
  583. static ssize_t et61x251_show_reg(struct device* cd,
  584. struct device_attribute *attr, char* buf)
  585. {
  586. struct et61x251_device* cam;
  587. ssize_t count;
  588. if (mutex_lock_interruptible(&et61x251_sysfs_lock))
  589. return -ERESTARTSYS;
  590. cam = video_get_drvdata(to_video_device(cd));
  591. if (!cam) {
  592. mutex_unlock(&et61x251_sysfs_lock);
  593. return -ENODEV;
  594. }
  595. count = sprintf(buf, "%u\n", cam->sysfs.reg);
  596. mutex_unlock(&et61x251_sysfs_lock);
  597. return count;
  598. }
  599. static ssize_t
  600. et61x251_store_reg(struct device* cd,
  601. struct device_attribute *attr, const char* buf, size_t len)
  602. {
  603. struct et61x251_device* cam;
  604. u8 index;
  605. ssize_t count;
  606. if (mutex_lock_interruptible(&et61x251_sysfs_lock))
  607. return -ERESTARTSYS;
  608. cam = video_get_drvdata(to_video_device(cd));
  609. if (!cam) {
  610. mutex_unlock(&et61x251_sysfs_lock);
  611. return -ENODEV;
  612. }
  613. index = et61x251_strtou8(buf, len, &count);
  614. if (index > 0x8e || !count) {
  615. mutex_unlock(&et61x251_sysfs_lock);
  616. return -EINVAL;
  617. }
  618. cam->sysfs.reg = index;
  619. DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
  620. DBG(3, "Written bytes: %zd", count);
  621. mutex_unlock(&et61x251_sysfs_lock);
  622. return count;
  623. }
  624. static ssize_t et61x251_show_val(struct device* cd,
  625. struct device_attribute *attr, char* buf)
  626. {
  627. struct et61x251_device* cam;
  628. ssize_t count;
  629. int val;
  630. if (mutex_lock_interruptible(&et61x251_sysfs_lock))
  631. return -ERESTARTSYS;
  632. cam = video_get_drvdata(to_video_device(cd));
  633. if (!cam) {
  634. mutex_unlock(&et61x251_sysfs_lock);
  635. return -ENODEV;
  636. }
  637. if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
  638. mutex_unlock(&et61x251_sysfs_lock);
  639. return -EIO;
  640. }
  641. count = sprintf(buf, "%d\n", val);
  642. DBG(3, "Read bytes: %zd", count);
  643. mutex_unlock(&et61x251_sysfs_lock);
  644. return count;
  645. }
  646. static ssize_t
  647. et61x251_store_val(struct device* cd, struct device_attribute *attr,
  648. const char* buf, size_t len)
  649. {
  650. struct et61x251_device* cam;
  651. u8 value;
  652. ssize_t count;
  653. int err;
  654. if (mutex_lock_interruptible(&et61x251_sysfs_lock))
  655. return -ERESTARTSYS;
  656. cam = video_get_drvdata(to_video_device(cd));
  657. if (!cam) {
  658. mutex_unlock(&et61x251_sysfs_lock);
  659. return -ENODEV;
  660. }
  661. value = et61x251_strtou8(buf, len, &count);
  662. if (!count) {
  663. mutex_unlock(&et61x251_sysfs_lock);
  664. return -EINVAL;
  665. }
  666. err = et61x251_write_reg(cam, value, cam->sysfs.reg);
  667. if (err) {
  668. mutex_unlock(&et61x251_sysfs_lock);
  669. return -EIO;
  670. }
  671. DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
  672. cam->sysfs.reg, value);
  673. DBG(3, "Written bytes: %zd", count);
  674. mutex_unlock(&et61x251_sysfs_lock);
  675. return count;
  676. }
  677. static ssize_t et61x251_show_i2c_reg(struct device* cd,
  678. struct device_attribute *attr, char* buf)
  679. {
  680. struct et61x251_device* cam;
  681. ssize_t count;
  682. if (mutex_lock_interruptible(&et61x251_sysfs_lock))
  683. return -ERESTARTSYS;
  684. cam = video_get_drvdata(to_video_device(cd));
  685. if (!cam) {
  686. mutex_unlock(&et61x251_sysfs_lock);
  687. return -ENODEV;
  688. }
  689. count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
  690. DBG(3, "Read bytes: %zd", count);
  691. mutex_unlock(&et61x251_sysfs_lock);
  692. return count;
  693. }
  694. static ssize_t
  695. et61x251_store_i2c_reg(struct device* cd, struct device_attribute *attr,
  696. const char* buf, size_t len)
  697. {
  698. struct et61x251_device* cam;
  699. u8 index;
  700. ssize_t count;
  701. if (mutex_lock_interruptible(&et61x251_sysfs_lock))
  702. return -ERESTARTSYS;
  703. cam = video_get_drvdata(to_video_device(cd));
  704. if (!cam) {
  705. mutex_unlock(&et61x251_sysfs_lock);
  706. return -ENODEV;
  707. }
  708. index = et61x251_strtou8(buf, len, &count);
  709. if (!count) {
  710. mutex_unlock(&et61x251_sysfs_lock);
  711. return -EINVAL;
  712. }
  713. cam->sysfs.i2c_reg = index;
  714. DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
  715. DBG(3, "Written bytes: %zd", count);
  716. mutex_unlock(&et61x251_sysfs_lock);
  717. return count;
  718. }
  719. static ssize_t et61x251_show_i2c_val(struct device* cd,
  720. struct device_attribute *attr, char* buf)
  721. {
  722. struct et61x251_device* cam;
  723. ssize_t count;
  724. int val;
  725. if (mutex_lock_interruptible(&et61x251_sysfs_lock))
  726. return -ERESTARTSYS;
  727. cam = video_get_drvdata(to_video_device(cd));
  728. if (!cam) {
  729. mutex_unlock(&et61x251_sysfs_lock);
  730. return -ENODEV;
  731. }
  732. if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
  733. mutex_unlock(&et61x251_sysfs_lock);
  734. return -ENOSYS;
  735. }
  736. if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
  737. mutex_unlock(&et61x251_sysfs_lock);
  738. return -EIO;
  739. }
  740. count = sprintf(buf, "%d\n", val);
  741. DBG(3, "Read bytes: %zd", count);
  742. mutex_unlock(&et61x251_sysfs_lock);
  743. return count;
  744. }
  745. static ssize_t
  746. et61x251_store_i2c_val(struct device* cd, struct device_attribute *attr,
  747. const char* buf, size_t len)
  748. {
  749. struct et61x251_device* cam;
  750. u8 value;
  751. ssize_t count;
  752. int err;
  753. if (mutex_lock_interruptible(&et61x251_sysfs_lock))
  754. return -ERESTARTSYS;
  755. cam = video_get_drvdata(to_video_device(cd));
  756. if (!cam) {
  757. mutex_unlock(&et61x251_sysfs_lock);
  758. return -ENODEV;
  759. }
  760. if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
  761. mutex_unlock(&et61x251_sysfs_lock);
  762. return -ENOSYS;
  763. }
  764. value = et61x251_strtou8(buf, len, &count);
  765. if (!count) {
  766. mutex_unlock(&et61x251_sysfs_lock);
  767. return -EINVAL;
  768. }
  769. err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
  770. if (err) {
  771. mutex_unlock(&et61x251_sysfs_lock);
  772. return -EIO;
  773. }
  774. DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
  775. cam->sysfs.i2c_reg, value);
  776. DBG(3, "Written bytes: %zd", count);
  777. mutex_unlock(&et61x251_sysfs_lock);
  778. return count;
  779. }
  780. static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
  781. et61x251_show_reg, et61x251_store_reg);
  782. static DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
  783. et61x251_show_val, et61x251_store_val);
  784. static DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
  785. et61x251_show_i2c_reg, et61x251_store_i2c_reg);
  786. static DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
  787. et61x251_show_i2c_val, et61x251_store_i2c_val);
  788. static int et61x251_create_sysfs(struct et61x251_device* cam)
  789. {
  790. struct device *classdev = &(cam->v4ldev->dev);
  791. int err = 0;
  792. if ((err = device_create_file(classdev, &dev_attr_reg)))
  793. goto err_out;
  794. if ((err = device_create_file(classdev, &dev_attr_val)))
  795. goto err_reg;
  796. if (cam->sensor.sysfs_ops) {
  797. if ((err = device_create_file(classdev, &dev_attr_i2c_reg)))
  798. goto err_val;
  799. if ((err = device_create_file(classdev, &dev_attr_i2c_val)))
  800. goto err_i2c_reg;
  801. }
  802. err_i2c_reg:
  803. if (cam->sensor.sysfs_ops)
  804. device_remove_file(classdev, &dev_attr_i2c_reg);
  805. err_val:
  806. device_remove_file(classdev, &dev_attr_val);
  807. err_reg:
  808. device_remove_file(classdev, &dev_attr_reg);
  809. err_out:
  810. return err;
  811. }
  812. #endif /* CONFIG_VIDEO_ADV_DEBUG */
  813. /*****************************************************************************/
  814. static int
  815. et61x251_set_pix_format(struct et61x251_device* cam,
  816. struct v4l2_pix_format* pix)
  817. {
  818. int r, err = 0;
  819. if ((r = et61x251_read_reg(cam, 0x12)) < 0)
  820. err += r;
  821. if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
  822. err += et61x251_write_reg(cam, r & 0xfd, 0x12);
  823. else
  824. err += et61x251_write_reg(cam, r | 0x02, 0x12);
  825. return err ? -EIO : 0;
  826. }
  827. static int
  828. et61x251_set_compression(struct et61x251_device* cam,
  829. struct v4l2_jpegcompression* compression)
  830. {
  831. int r, err = 0;
  832. if ((r = et61x251_read_reg(cam, 0x12)) < 0)
  833. err += r;
  834. if (compression->quality == 0)
  835. err += et61x251_write_reg(cam, r & 0xfb, 0x12);
  836. else
  837. err += et61x251_write_reg(cam, r | 0x04, 0x12);
  838. return err ? -EIO : 0;
  839. }
  840. static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
  841. {
  842. int r = 0, err = 0;
  843. r = et61x251_read_reg(cam, 0x12);
  844. if (r < 0)
  845. err += r;
  846. if (scale == 1)
  847. err += et61x251_write_reg(cam, r & ~0x01, 0x12);
  848. else if (scale == 2)
  849. err += et61x251_write_reg(cam, r | 0x01, 0x12);
  850. if (err)
  851. return -EIO;
  852. PDBGG("Scaling factor: %u", scale);
  853. return 0;
  854. }
  855. static int
  856. et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
  857. {
  858. struct et61x251_sensor* s = &cam->sensor;
  859. u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
  860. s->active_pixel.left),
  861. fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
  862. s->active_pixel.top),
  863. fmw_length = (u16)(rect->width),
  864. fmw_height = (u16)(rect->height);
  865. int err = 0;
  866. err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
  867. err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
  868. err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
  869. err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
  870. err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
  871. | ((fmw_length & 0x300) >> 4)
  872. | ((fmw_height & 0x300) >> 2), 0x6d);
  873. if (err)
  874. return -EIO;
  875. PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
  876. fmw_sx, fmw_sy, fmw_length, fmw_height);
  877. return 0;
  878. }
  879. static int et61x251_init(struct et61x251_device* cam)
  880. {
  881. struct et61x251_sensor* s = &cam->sensor;
  882. struct v4l2_control ctrl;
  883. struct v4l2_queryctrl *qctrl;
  884. struct v4l2_rect* rect;
  885. u8 i = 0;
  886. int err = 0;
  887. if (!(cam->state & DEV_INITIALIZED)) {
  888. mutex_init(&cam->open_mutex);
  889. init_waitqueue_head(&cam->wait_open);
  890. qctrl = s->qctrl;
  891. rect = &(s->cropcap.defrect);
  892. cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
  893. } else { /* use current values */
  894. qctrl = s->_qctrl;
  895. rect = &(s->_rect);
  896. }
  897. err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
  898. err += et61x251_set_crop(cam, rect);
  899. if (err)
  900. return err;
  901. if (s->init) {
  902. err = s->init(cam);
  903. if (err) {
  904. DBG(3, "Sensor initialization failed");
  905. return err;
  906. }
  907. }
  908. err += et61x251_set_compression(cam, &cam->compression);
  909. err += et61x251_set_pix_format(cam, &s->pix_format);
  910. if (s->set_pix_format)
  911. err += s->set_pix_format(cam, &s->pix_format);
  912. if (err)
  913. return err;
  914. if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
  915. DBG(3, "Compressed video format is active, quality %d",
  916. cam->compression.quality);
  917. else
  918. DBG(3, "Uncompressed video format is active");
  919. if (s->set_crop)
  920. if ((err = s->set_crop(cam, rect))) {
  921. DBG(3, "set_crop() failed");
  922. return err;
  923. }
  924. if (s->set_ctrl) {
  925. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  926. if (s->qctrl[i].id != 0 &&
  927. !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
  928. ctrl.id = s->qctrl[i].id;
  929. ctrl.value = qctrl[i].default_value;
  930. err = s->set_ctrl(cam, &ctrl);
  931. if (err) {
  932. DBG(3, "Set %s control failed",
  933. s->qctrl[i].name);
  934. return err;
  935. }
  936. DBG(3, "Image sensor supports '%s' control",
  937. s->qctrl[i].name);
  938. }
  939. }
  940. if (!(cam->state & DEV_INITIALIZED)) {
  941. mutex_init(&cam->fileop_mutex);
  942. spin_lock_init(&cam->queue_lock);
  943. init_waitqueue_head(&cam->wait_frame);
  944. init_waitqueue_head(&cam->wait_stream);
  945. cam->nreadbuffers = 2;
  946. memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
  947. memcpy(&(s->_rect), &(s->cropcap.defrect),
  948. sizeof(struct v4l2_rect));
  949. cam->state |= DEV_INITIALIZED;
  950. }
  951. DBG(2, "Initialization succeeded");
  952. return 0;
  953. }
  954. /*****************************************************************************/
  955. static void et61x251_release_resources(struct kref *kref)
  956. {
  957. struct et61x251_device *cam;
  958. mutex_lock(&et61x251_sysfs_lock);
  959. cam = container_of(kref, struct et61x251_device, kref);
  960. DBG(2, "V4L2 device %s deregistered",
  961. video_device_node_name(cam->v4ldev));
  962. video_set_drvdata(cam->v4ldev, NULL);
  963. video_unregister_device(cam->v4ldev);
  964. usb_put_dev(cam->usbdev);
  965. kfree(cam->control_buffer);
  966. kfree(cam);
  967. mutex_unlock(&et61x251_sysfs_lock);
  968. }
  969. static int et61x251_open(struct file *filp)
  970. {
  971. struct et61x251_device* cam;
  972. int err = 0;
  973. if (!down_read_trylock(&et61x251_dev_lock))
  974. return -ERESTARTSYS;
  975. cam = video_drvdata(filp);
  976. if (wait_for_completion_interruptible(&cam->probe)) {
  977. up_read(&et61x251_dev_lock);
  978. return -ERESTARTSYS;
  979. }
  980. kref_get(&cam->kref);
  981. if (mutex_lock_interruptible(&cam->open_mutex)) {
  982. kref_put(&cam->kref, et61x251_release_resources);
  983. up_read(&et61x251_dev_lock);
  984. return -ERESTARTSYS;
  985. }
  986. if (cam->state & DEV_DISCONNECTED) {
  987. DBG(1, "Device not present");
  988. err = -ENODEV;
  989. goto out;
  990. }
  991. if (cam->users) {
  992. DBG(2, "Device %s is already in use",
  993. video_device_node_name(cam->v4ldev));
  994. DBG(3, "Simultaneous opens are not supported");
  995. if ((filp->f_flags & O_NONBLOCK) ||
  996. (filp->f_flags & O_NDELAY)) {
  997. err = -EWOULDBLOCK;
  998. goto out;
  999. }
  1000. DBG(2, "A blocking open() has been requested. Wait for the "
  1001. "device to be released...");
  1002. up_read(&et61x251_dev_lock);
  1003. err = wait_event_interruptible_exclusive(cam->wait_open,
  1004. (cam->state & DEV_DISCONNECTED)
  1005. || !cam->users);
  1006. down_read(&et61x251_dev_lock);
  1007. if (err)
  1008. goto out;
  1009. if (cam->state & DEV_DISCONNECTED) {
  1010. err = -ENODEV;
  1011. goto out;
  1012. }
  1013. }
  1014. if (cam->state & DEV_MISCONFIGURED) {
  1015. err = et61x251_init(cam);
  1016. if (err) {
  1017. DBG(1, "Initialization failed again. "
  1018. "I will retry on next open().");
  1019. goto out;
  1020. }
  1021. cam->state &= ~DEV_MISCONFIGURED;
  1022. }
  1023. if ((err = et61x251_start_transfer(cam)))
  1024. goto out;
  1025. filp->private_data = cam;
  1026. cam->users++;
  1027. cam->io = IO_NONE;
  1028. cam->stream = STREAM_OFF;
  1029. cam->nbuffers = 0;
  1030. cam->frame_count = 0;
  1031. et61x251_empty_framequeues(cam);
  1032. DBG(3, "Video device %s is open",
  1033. video_device_node_name(cam->v4ldev));
  1034. out:
  1035. mutex_unlock(&cam->open_mutex);
  1036. if (err)
  1037. kref_put(&cam->kref, et61x251_release_resources);
  1038. up_read(&et61x251_dev_lock);
  1039. return err;
  1040. }
  1041. static int et61x251_release(struct file *filp)
  1042. {
  1043. struct et61x251_device* cam;
  1044. down_write(&et61x251_dev_lock);
  1045. cam = video_drvdata(filp);
  1046. et61x251_stop_transfer(cam);
  1047. et61x251_release_buffers(cam);
  1048. cam->users--;
  1049. wake_up_interruptible_nr(&cam->wait_open, 1);
  1050. DBG(3, "Video device %s closed",
  1051. video_device_node_name(cam->v4ldev));
  1052. kref_put(&cam->kref, et61x251_release_resources);
  1053. up_write(&et61x251_dev_lock);
  1054. return 0;
  1055. }
  1056. static ssize_t
  1057. et61x251_read(struct file* filp, char __user * buf,
  1058. size_t count, loff_t* f_pos)
  1059. {
  1060. struct et61x251_device *cam = video_drvdata(filp);
  1061. struct et61x251_frame_t* f, * i;
  1062. unsigned long lock_flags;
  1063. long timeout;
  1064. int err = 0;
  1065. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1066. return -ERESTARTSYS;
  1067. if (cam->state & DEV_DISCONNECTED) {
  1068. DBG(1, "Device not present");
  1069. mutex_unlock(&cam->fileop_mutex);
  1070. return -ENODEV;
  1071. }
  1072. if (cam->state & DEV_MISCONFIGURED) {
  1073. DBG(1, "The camera is misconfigured. Close and open it "
  1074. "again.");
  1075. mutex_unlock(&cam->fileop_mutex);
  1076. return -EIO;
  1077. }
  1078. if (cam->io == IO_MMAP) {
  1079. DBG(3, "Close and open the device again to choose the read "
  1080. "method");
  1081. mutex_unlock(&cam->fileop_mutex);
  1082. return -EBUSY;
  1083. }
  1084. if (cam->io == IO_NONE) {
  1085. if (!et61x251_request_buffers(cam, cam->nreadbuffers,
  1086. IO_READ)) {
  1087. DBG(1, "read() failed, not enough memory");
  1088. mutex_unlock(&cam->fileop_mutex);
  1089. return -ENOMEM;
  1090. }
  1091. cam->io = IO_READ;
  1092. cam->stream = STREAM_ON;
  1093. }
  1094. if (list_empty(&cam->inqueue)) {
  1095. if (!list_empty(&cam->outqueue))
  1096. et61x251_empty_framequeues(cam);
  1097. et61x251_queue_unusedframes(cam);
  1098. }
  1099. if (!count) {
  1100. mutex_unlock(&cam->fileop_mutex);
  1101. return 0;
  1102. }
  1103. if (list_empty(&cam->outqueue)) {
  1104. if (filp->f_flags & O_NONBLOCK) {
  1105. mutex_unlock(&cam->fileop_mutex);
  1106. return -EAGAIN;
  1107. }
  1108. timeout = wait_event_interruptible_timeout
  1109. ( cam->wait_frame,
  1110. (!list_empty(&cam->outqueue)) ||
  1111. (cam->state & DEV_DISCONNECTED) ||
  1112. (cam->state & DEV_MISCONFIGURED),
  1113. msecs_to_jiffies(
  1114. cam->module_param.frame_timeout * 1000
  1115. )
  1116. );
  1117. if (timeout < 0) {
  1118. mutex_unlock(&cam->fileop_mutex);
  1119. return timeout;
  1120. }
  1121. if (cam->state & DEV_DISCONNECTED) {
  1122. mutex_unlock(&cam->fileop_mutex);
  1123. return -ENODEV;
  1124. }
  1125. if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
  1126. mutex_unlock(&cam->fileop_mutex);
  1127. return -EIO;
  1128. }
  1129. }
  1130. f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
  1131. if (count > f->buf.bytesused)
  1132. count = f->buf.bytesused;
  1133. if (copy_to_user(buf, f->bufmem, count)) {
  1134. err = -EFAULT;
  1135. goto exit;
  1136. }
  1137. *f_pos += count;
  1138. exit:
  1139. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1140. list_for_each_entry(i, &cam->outqueue, frame)
  1141. i->state = F_UNUSED;
  1142. INIT_LIST_HEAD(&cam->outqueue);
  1143. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1144. et61x251_queue_unusedframes(cam);
  1145. PDBGG("Frame #%lu, bytes read: %zu",
  1146. (unsigned long)f->buf.index, count);
  1147. mutex_unlock(&cam->fileop_mutex);
  1148. return err ? err : count;
  1149. }
  1150. static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
  1151. {
  1152. struct et61x251_device *cam = video_drvdata(filp);
  1153. struct et61x251_frame_t* f;
  1154. unsigned long lock_flags;
  1155. unsigned int mask = 0;
  1156. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1157. return POLLERR;
  1158. if (cam->state & DEV_DISCONNECTED) {
  1159. DBG(1, "Device not present");
  1160. goto error;
  1161. }
  1162. if (cam->state & DEV_MISCONFIGURED) {
  1163. DBG(1, "The camera is misconfigured. Close and open it "
  1164. "again.");
  1165. goto error;
  1166. }
  1167. if (cam->io == IO_NONE) {
  1168. if (!et61x251_request_buffers(cam, cam->nreadbuffers,
  1169. IO_READ)) {
  1170. DBG(1, "poll() failed, not enough memory");
  1171. goto error;
  1172. }
  1173. cam->io = IO_READ;
  1174. cam->stream = STREAM_ON;
  1175. }
  1176. if (cam->io == IO_READ) {
  1177. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1178. list_for_each_entry(f, &cam->outqueue, frame)
  1179. f->state = F_UNUSED;
  1180. INIT_LIST_HEAD(&cam->outqueue);
  1181. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1182. et61x251_queue_unusedframes(cam);
  1183. }
  1184. poll_wait(filp, &cam->wait_frame, wait);
  1185. if (!list_empty(&cam->outqueue))
  1186. mask |= POLLIN | POLLRDNORM;
  1187. mutex_unlock(&cam->fileop_mutex);
  1188. return mask;
  1189. error:
  1190. mutex_unlock(&cam->fileop_mutex);
  1191. return POLLERR;
  1192. }
  1193. static void et61x251_vm_open(struct vm_area_struct* vma)
  1194. {
  1195. struct et61x251_frame_t* f = vma->vm_private_data;
  1196. f->vma_use_count++;
  1197. }
  1198. static void et61x251_vm_close(struct vm_area_struct* vma)
  1199. {
  1200. /* NOTE: buffers are not freed here */
  1201. struct et61x251_frame_t* f = vma->vm_private_data;
  1202. f->vma_use_count--;
  1203. }
  1204. static const struct vm_operations_struct et61x251_vm_ops = {
  1205. .open = et61x251_vm_open,
  1206. .close = et61x251_vm_close,
  1207. };
  1208. static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
  1209. {
  1210. struct et61x251_device *cam = video_drvdata(filp);
  1211. unsigned long size = vma->vm_end - vma->vm_start,
  1212. start = vma->vm_start;
  1213. void *pos;
  1214. u32 i;
  1215. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1216. return -ERESTARTSYS;
  1217. if (cam->state & DEV_DISCONNECTED) {
  1218. DBG(1, "Device not present");
  1219. mutex_unlock(&cam->fileop_mutex);
  1220. return -ENODEV;
  1221. }
  1222. if (cam->state & DEV_MISCONFIGURED) {
  1223. DBG(1, "The camera is misconfigured. Close and open it "
  1224. "again.");
  1225. mutex_unlock(&cam->fileop_mutex);
  1226. return -EIO;
  1227. }
  1228. if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
  1229. mutex_unlock(&cam->fileop_mutex);
  1230. return -EACCES;
  1231. }
  1232. if (cam->io != IO_MMAP ||
  1233. size != PAGE_ALIGN(cam->frame[0].buf.length)) {
  1234. mutex_unlock(&cam->fileop_mutex);
  1235. return -EINVAL;
  1236. }
  1237. for (i = 0; i < cam->nbuffers; i++) {
  1238. if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
  1239. break;
  1240. }
  1241. if (i == cam->nbuffers) {
  1242. mutex_unlock(&cam->fileop_mutex);
  1243. return -EINVAL;
  1244. }
  1245. vma->vm_flags |= VM_IO;
  1246. vma->vm_flags |= VM_RESERVED;
  1247. pos = cam->frame[i].bufmem;
  1248. while (size > 0) { /* size is page-aligned */
  1249. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  1250. mutex_unlock(&cam->fileop_mutex);
  1251. return -EAGAIN;
  1252. }
  1253. start += PAGE_SIZE;
  1254. pos += PAGE_SIZE;
  1255. size -= PAGE_SIZE;
  1256. }
  1257. vma->vm_ops = &et61x251_vm_ops;
  1258. vma->vm_private_data = &cam->frame[i];
  1259. et61x251_vm_open(vma);
  1260. mutex_unlock(&cam->fileop_mutex);
  1261. return 0;
  1262. }
  1263. /*****************************************************************************/
  1264. static int
  1265. et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
  1266. {
  1267. struct v4l2_capability cap = {
  1268. .driver = "et61x251",
  1269. .version = LINUX_VERSION_CODE,
  1270. .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  1271. V4L2_CAP_STREAMING,
  1272. };
  1273. strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
  1274. if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
  1275. strlcpy(cap.bus_info, dev_name(&cam->usbdev->dev),
  1276. sizeof(cap.bus_info));
  1277. if (copy_to_user(arg, &cap, sizeof(cap)))
  1278. return -EFAULT;
  1279. return 0;
  1280. }
  1281. static int
  1282. et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
  1283. {
  1284. struct v4l2_input i;
  1285. if (copy_from_user(&i, arg, sizeof(i)))
  1286. return -EFAULT;
  1287. if (i.index)
  1288. return -EINVAL;
  1289. memset(&i, 0, sizeof(i));
  1290. strcpy(i.name, "Camera");
  1291. i.type = V4L2_INPUT_TYPE_CAMERA;
  1292. i.capabilities = V4L2_IN_CAP_STD;
  1293. if (copy_to_user(arg, &i, sizeof(i)))
  1294. return -EFAULT;
  1295. return 0;
  1296. }
  1297. static int
  1298. et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
  1299. {
  1300. int index = 0;
  1301. if (copy_to_user(arg, &index, sizeof(index)))
  1302. return -EFAULT;
  1303. return 0;
  1304. }
  1305. static int
  1306. et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
  1307. {
  1308. int index;
  1309. if (copy_from_user(&index, arg, sizeof(index)))
  1310. return -EFAULT;
  1311. if (index != 0)
  1312. return -EINVAL;
  1313. return 0;
  1314. }
  1315. static int
  1316. et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
  1317. {
  1318. struct et61x251_sensor* s = &cam->sensor;
  1319. struct v4l2_queryctrl qc;
  1320. u8 i;
  1321. if (copy_from_user(&qc, arg, sizeof(qc)))
  1322. return -EFAULT;
  1323. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1324. if (qc.id && qc.id == s->qctrl[i].id) {
  1325. memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
  1326. if (copy_to_user(arg, &qc, sizeof(qc)))
  1327. return -EFAULT;
  1328. return 0;
  1329. }
  1330. return -EINVAL;
  1331. }
  1332. static int
  1333. et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
  1334. {
  1335. struct et61x251_sensor* s = &cam->sensor;
  1336. struct v4l2_control ctrl;
  1337. int err = 0;
  1338. u8 i;
  1339. if (!s->get_ctrl && !s->set_ctrl)
  1340. return -EINVAL;
  1341. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1342. return -EFAULT;
  1343. if (!s->get_ctrl) {
  1344. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
  1345. if (ctrl.id == s->qctrl[i].id) {
  1346. ctrl.value = s->_qctrl[i].default_value;
  1347. goto exit;
  1348. }
  1349. return -EINVAL;
  1350. } else
  1351. err = s->get_ctrl(cam, &ctrl);
  1352. exit:
  1353. if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
  1354. return -EFAULT;
  1355. return err;
  1356. }
  1357. static int
  1358. et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
  1359. {
  1360. struct et61x251_sensor* s = &cam->sensor;
  1361. struct v4l2_control ctrl;
  1362. u8 i;
  1363. int err = 0;
  1364. if (!s->set_ctrl)
  1365. return -EINVAL;
  1366. if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
  1367. return -EFAULT;
  1368. for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) {
  1369. if (ctrl.id == s->qctrl[i].id) {
  1370. if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
  1371. return -EINVAL;
  1372. if (ctrl.value < s->qctrl[i].minimum ||
  1373. ctrl.value > s->qctrl[i].maximum)
  1374. return -ERANGE;
  1375. ctrl.value -= ctrl.value % s->qctrl[i].step;
  1376. break;
  1377. }
  1378. }
  1379. if (i == ARRAY_SIZE(s->qctrl))
  1380. return -EINVAL;
  1381. if ((err = s->set_ctrl(cam, &ctrl)))
  1382. return err;
  1383. s->_qctrl[i].default_value = ctrl.value;
  1384. return 0;
  1385. }
  1386. static int
  1387. et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
  1388. {
  1389. struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
  1390. cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1391. cc->pixelaspect.numerator = 1;
  1392. cc->pixelaspect.denominator = 1;
  1393. if (copy_to_user(arg, cc, sizeof(*cc)))
  1394. return -EFAULT;
  1395. return 0;
  1396. }
  1397. static int
  1398. et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
  1399. {
  1400. struct et61x251_sensor* s = &cam->sensor;
  1401. struct v4l2_crop crop = {
  1402. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1403. };
  1404. memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
  1405. if (copy_to_user(arg, &crop, sizeof(crop)))
  1406. return -EFAULT;
  1407. return 0;
  1408. }
  1409. static int
  1410. et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
  1411. {
  1412. struct et61x251_sensor* s = &cam->sensor;
  1413. struct v4l2_crop crop;
  1414. struct v4l2_rect* rect;
  1415. struct v4l2_rect* bounds = &(s->cropcap.bounds);
  1416. struct v4l2_pix_format* pix_format = &(s->pix_format);
  1417. u8 scale;
  1418. const enum et61x251_stream_state stream = cam->stream;
  1419. const u32 nbuffers = cam->nbuffers;
  1420. u32 i;
  1421. int err = 0;
  1422. if (copy_from_user(&crop, arg, sizeof(crop)))
  1423. return -EFAULT;
  1424. rect = &(crop.c);
  1425. if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1426. return -EINVAL;
  1427. if (cam->module_param.force_munmap)
  1428. for (i = 0; i < cam->nbuffers; i++)
  1429. if (cam->frame[i].vma_use_count) {
  1430. DBG(3, "VIDIOC_S_CROP failed. "
  1431. "Unmap the buffers first.");
  1432. return -EBUSY;
  1433. }
  1434. /* Preserve R,G or B origin */
  1435. rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
  1436. rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
  1437. if (rect->width < 16)
  1438. rect->width = 16;
  1439. if (rect->height < 16)
  1440. rect->height = 16;
  1441. if (rect->width > bounds->width)
  1442. rect->width = bounds->width;
  1443. if (rect->height > bounds->height)
  1444. rect->height = bounds->height;
  1445. if (rect->left < bounds->left)
  1446. rect->left = bounds->left;
  1447. if (rect->top < bounds->top)
  1448. rect->top = bounds->top;
  1449. if (rect->left + rect->width > bounds->left + bounds->width)
  1450. rect->left = bounds->left+bounds->width - rect->width;
  1451. if (rect->top + rect->height > bounds->top + bounds->height)
  1452. rect->top = bounds->top+bounds->height - rect->height;
  1453. rect->width &= ~15L;
  1454. rect->height &= ~15L;
  1455. if (ET61X251_PRESERVE_IMGSCALE) {
  1456. /* Calculate the actual scaling factor */
  1457. u32 a, b;
  1458. a = rect->width * rect->height;
  1459. b = pix_format->width * pix_format->height;
  1460. scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
  1461. } else
  1462. scale = 1;
  1463. if (cam->stream == STREAM_ON)
  1464. if ((err = et61x251_stream_interrupt(cam)))
  1465. return err;
  1466. if (copy_to_user(arg, &crop, sizeof(crop))) {
  1467. cam->stream = stream;
  1468. return -EFAULT;
  1469. }
  1470. if (cam->module_param.force_munmap || cam->io == IO_READ)
  1471. et61x251_release_buffers(cam);
  1472. err = et61x251_set_crop(cam, rect);
  1473. if (s->set_crop)
  1474. err += s->set_crop(cam, rect);
  1475. err += et61x251_set_scale(cam, scale);
  1476. if (err) { /* atomic, no rollback in ioctl() */
  1477. cam->state |= DEV_MISCONFIGURED;
  1478. DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
  1479. "use the camera, close and open %s again.",
  1480. video_device_node_name(cam->v4ldev));
  1481. return -EIO;
  1482. }
  1483. s->pix_format.width = rect->width/scale;
  1484. s->pix_format.height = rect->height/scale;
  1485. memcpy(&(s->_rect), rect, sizeof(*rect));
  1486. if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
  1487. nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
  1488. cam->state |= DEV_MISCONFIGURED;
  1489. DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
  1490. "use the camera, close and open %s again.",
  1491. video_device_node_name(cam->v4ldev));
  1492. return -ENOMEM;
  1493. }
  1494. if (cam->io == IO_READ)
  1495. et61x251_empty_framequeues(cam);
  1496. else if (cam->module_param.force_munmap)
  1497. et61x251_requeue_outqueue(cam);
  1498. cam->stream = stream;
  1499. return 0;
  1500. }
  1501. static int
  1502. et61x251_vidioc_enum_framesizes(struct et61x251_device* cam, void __user * arg)
  1503. {
  1504. struct v4l2_frmsizeenum frmsize;
  1505. if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
  1506. return -EFAULT;
  1507. if (frmsize.index != 0)
  1508. return -EINVAL;
  1509. if (frmsize.pixel_format != V4L2_PIX_FMT_ET61X251 &&
  1510. frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
  1511. return -EINVAL;
  1512. frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1513. frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
  1514. frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
  1515. frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
  1516. frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
  1517. memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
  1518. if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
  1519. return -EFAULT;
  1520. return 0;
  1521. }
  1522. static int
  1523. et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
  1524. {
  1525. struct v4l2_fmtdesc fmtd;
  1526. if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
  1527. return -EFAULT;
  1528. if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1529. return -EINVAL;
  1530. if (fmtd.index == 0) {
  1531. strcpy(fmtd.description, "bayer rgb");
  1532. fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
  1533. } else if (fmtd.index == 1) {
  1534. strcpy(fmtd.description, "compressed");
  1535. fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
  1536. fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
  1537. } else
  1538. return -EINVAL;
  1539. fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1540. memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
  1541. if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
  1542. return -EFAULT;
  1543. return 0;
  1544. }
  1545. static int
  1546. et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
  1547. {
  1548. struct v4l2_format format;
  1549. struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
  1550. if (copy_from_user(&format, arg, sizeof(format)))
  1551. return -EFAULT;
  1552. if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1553. return -EINVAL;
  1554. pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_ET61X251) ?
  1555. 0 : V4L2_COLORSPACE_SRGB;
  1556. pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
  1557. ? 0 : (pfmt->width * pfmt->priv) / 8;
  1558. pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
  1559. pfmt->field = V4L2_FIELD_NONE;
  1560. memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
  1561. if (copy_to_user(arg, &format, sizeof(format)))
  1562. return -EFAULT;
  1563. return 0;
  1564. }
  1565. static int
  1566. et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
  1567. void __user * arg)
  1568. {
  1569. struct et61x251_sensor* s = &cam->sensor;
  1570. struct v4l2_format format;
  1571. struct v4l2_pix_format* pix;
  1572. struct v4l2_pix_format* pfmt = &(s->pix_format);
  1573. struct v4l2_rect* bounds = &(s->cropcap.bounds);
  1574. struct v4l2_rect rect;
  1575. u8 scale;
  1576. const enum et61x251_stream_state stream = cam->stream;
  1577. const u32 nbuffers = cam->nbuffers;
  1578. u32 i;
  1579. int err = 0;
  1580. if (copy_from_user(&format, arg, sizeof(format)))
  1581. return -EFAULT;
  1582. pix = &(format.fmt.pix);
  1583. if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1584. return -EINVAL;
  1585. memcpy(&rect, &(s->_rect), sizeof(rect));
  1586. { /* calculate the actual scaling factor */
  1587. u32 a, b;
  1588. a = rect.width * rect.height;
  1589. b = pix->width * pix->height;
  1590. scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
  1591. }
  1592. rect.width = scale * pix->width;
  1593. rect.height = scale * pix->height;
  1594. if (rect.width < 16)
  1595. rect.width = 16;
  1596. if (rect.height < 16)
  1597. rect.height = 16;
  1598. if (rect.width > bounds->left + bounds->width - rect.left)
  1599. rect.width = bounds->left + bounds->width - rect.left;
  1600. if (rect.height > bounds->top + bounds->height - rect.top)
  1601. rect.height = bounds->top + bounds->height - rect.top;
  1602. rect.width &= ~15L;
  1603. rect.height &= ~15L;
  1604. { /* adjust the scaling factor */
  1605. u32 a, b;
  1606. a = rect.width * rect.height;
  1607. b = pix->width * pix->height;
  1608. scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
  1609. }
  1610. pix->width = rect.width / scale;
  1611. pix->height = rect.height / scale;
  1612. if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
  1613. pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
  1614. pix->pixelformat = pfmt->pixelformat;
  1615. pix->priv = pfmt->priv; /* bpp */
  1616. pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_ET61X251) ?
  1617. 0 : V4L2_COLORSPACE_SRGB;
  1618. pix->colorspace = pfmt->colorspace;
  1619. pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
  1620. ? 0 : (pix->width * pix->priv) / 8;
  1621. pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
  1622. pix->field = V4L2_FIELD_NONE;
  1623. if (cmd == VIDIOC_TRY_FMT) {
  1624. if (copy_to_user(arg, &format, sizeof(format)))
  1625. return -EFAULT;
  1626. return 0;
  1627. }
  1628. if (cam->module_param.force_munmap)
  1629. for (i = 0; i < cam->nbuffers; i++)
  1630. if (cam->frame[i].vma_use_count) {
  1631. DBG(3, "VIDIOC_S_FMT failed. "
  1632. "Unmap the buffers first.");
  1633. return -EBUSY;
  1634. }
  1635. if (cam->stream == STREAM_ON)
  1636. if ((err = et61x251_stream_interrupt(cam)))
  1637. return err;
  1638. if (copy_to_user(arg, &format, sizeof(format))) {
  1639. cam->stream = stream;
  1640. return -EFAULT;
  1641. }
  1642. if (cam->module_param.force_munmap || cam->io == IO_READ)
  1643. et61x251_release_buffers(cam);
  1644. err += et61x251_set_pix_format(cam, pix);
  1645. err += et61x251_set_crop(cam, &rect);
  1646. if (s->set_pix_format)
  1647. err += s->set_pix_format(cam, pix);
  1648. if (s->set_crop)
  1649. err += s->set_crop(cam, &rect);
  1650. err += et61x251_set_scale(cam, scale);
  1651. if (err) { /* atomic, no rollback in ioctl() */
  1652. cam->state |= DEV_MISCONFIGURED;
  1653. DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
  1654. "use the camera, close and open %s again.",
  1655. video_device_node_name(cam->v4ldev));
  1656. return -EIO;
  1657. }
  1658. memcpy(pfmt, pix, sizeof(*pix));
  1659. memcpy(&(s->_rect), &rect, sizeof(rect));
  1660. if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
  1661. nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
  1662. cam->state |= DEV_MISCONFIGURED;
  1663. DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
  1664. "use the camera, close and open %s again.",
  1665. video_device_node_name(cam->v4ldev));
  1666. return -ENOMEM;
  1667. }
  1668. if (cam->io == IO_READ)
  1669. et61x251_empty_framequeues(cam);
  1670. else if (cam->module_param.force_munmap)
  1671. et61x251_requeue_outqueue(cam);
  1672. cam->stream = stream;
  1673. return 0;
  1674. }
  1675. static int
  1676. et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
  1677. {
  1678. if (copy_to_user(arg, &cam->compression,
  1679. sizeof(cam->compression)))
  1680. return -EFAULT;
  1681. return 0;
  1682. }
  1683. static int
  1684. et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
  1685. {
  1686. struct v4l2_jpegcompression jc;
  1687. const enum et61x251_stream_state stream = cam->stream;
  1688. int err = 0;
  1689. if (copy_from_user(&jc, arg, sizeof(jc)))
  1690. return -EFAULT;
  1691. if (jc.quality != 0 && jc.quality != 1)
  1692. return -EINVAL;
  1693. if (cam->stream == STREAM_ON)
  1694. if ((err = et61x251_stream_interrupt(cam)))
  1695. return err;
  1696. err += et61x251_set_compression(cam, &jc);
  1697. if (err) { /* atomic, no rollback in ioctl() */
  1698. cam->state |= DEV_MISCONFIGURED;
  1699. DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
  1700. "problems. To use the camera, close and open "
  1701. "%s again.", video_device_node_name(cam->v4ldev));
  1702. return -EIO;
  1703. }
  1704. cam->compression.quality = jc.quality;
  1705. cam->stream = stream;
  1706. return 0;
  1707. }
  1708. static int
  1709. et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
  1710. {
  1711. struct v4l2_requestbuffers rb;
  1712. u32 i;
  1713. int err;
  1714. if (copy_from_user(&rb, arg, sizeof(rb)))
  1715. return -EFAULT;
  1716. if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1717. rb.memory != V4L2_MEMORY_MMAP)
  1718. return -EINVAL;
  1719. if (cam->io == IO_READ) {
  1720. DBG(3, "Close and open the device again to choose the mmap "
  1721. "I/O method");
  1722. return -EBUSY;
  1723. }
  1724. for (i = 0; i < cam->nbuffers; i++)
  1725. if (cam->frame[i].vma_use_count) {
  1726. DBG(3, "VIDIOC_REQBUFS failed. "
  1727. "Previous buffers are still mapped.");
  1728. return -EBUSY;
  1729. }
  1730. if (cam->stream == STREAM_ON)
  1731. if ((err = et61x251_stream_interrupt(cam)))
  1732. return err;
  1733. et61x251_empty_framequeues(cam);
  1734. et61x251_release_buffers(cam);
  1735. if (rb.count)
  1736. rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
  1737. if (copy_to_user(arg, &rb, sizeof(rb))) {
  1738. et61x251_release_buffers(cam);
  1739. cam->io = IO_NONE;
  1740. return -EFAULT;
  1741. }
  1742. cam->io = rb.count ? IO_MMAP : IO_NONE;
  1743. return 0;
  1744. }
  1745. static int
  1746. et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
  1747. {
  1748. struct v4l2_buffer b;
  1749. if (copy_from_user(&b, arg, sizeof(b)))
  1750. return -EFAULT;
  1751. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1752. b.index >= cam->nbuffers || cam->io != IO_MMAP)
  1753. return -EINVAL;
  1754. memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
  1755. if (cam->frame[b.index].vma_use_count)
  1756. b.flags |= V4L2_BUF_FLAG_MAPPED;
  1757. if (cam->frame[b.index].state == F_DONE)
  1758. b.flags |= V4L2_BUF_FLAG_DONE;
  1759. else if (cam->frame[b.index].state != F_UNUSED)
  1760. b.flags |= V4L2_BUF_FLAG_QUEUED;
  1761. if (copy_to_user(arg, &b, sizeof(b)))
  1762. return -EFAULT;
  1763. return 0;
  1764. }
  1765. static int
  1766. et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
  1767. {
  1768. struct v4l2_buffer b;
  1769. unsigned long lock_flags;
  1770. if (copy_from_user(&b, arg, sizeof(b)))
  1771. return -EFAULT;
  1772. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1773. b.index >= cam->nbuffers || cam->io != IO_MMAP)
  1774. return -EINVAL;
  1775. if (cam->frame[b.index].state != F_UNUSED)
  1776. return -EINVAL;
  1777. cam->frame[b.index].state = F_QUEUED;
  1778. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1779. list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
  1780. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1781. PDBGG("Frame #%lu queued", (unsigned long)b.index);
  1782. return 0;
  1783. }
  1784. static int
  1785. et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
  1786. void __user * arg)
  1787. {
  1788. struct v4l2_buffer b;
  1789. struct et61x251_frame_t *f;
  1790. unsigned long lock_flags;
  1791. long timeout;
  1792. if (copy_from_user(&b, arg, sizeof(b)))
  1793. return -EFAULT;
  1794. if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
  1795. return -EINVAL;
  1796. if (list_empty(&cam->outqueue)) {
  1797. if (cam->stream == STREAM_OFF)
  1798. return -EINVAL;
  1799. if (filp->f_flags & O_NONBLOCK)
  1800. return -EAGAIN;
  1801. timeout = wait_event_interruptible_timeout
  1802. ( cam->wait_frame,
  1803. (!list_empty(&cam->outqueue)) ||
  1804. (cam->state & DEV_DISCONNECTED) ||
  1805. (cam->state & DEV_MISCONFIGURED),
  1806. cam->module_param.frame_timeout *
  1807. 1000 * msecs_to_jiffies(1) );
  1808. if (timeout < 0)
  1809. return timeout;
  1810. if (cam->state & DEV_DISCONNECTED)
  1811. return -ENODEV;
  1812. if (!timeout || (cam->state & DEV_MISCONFIGURED))
  1813. return -EIO;
  1814. }
  1815. spin_lock_irqsave(&cam->queue_lock, lock_flags);
  1816. f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
  1817. list_del(cam->outqueue.next);
  1818. spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
  1819. f->state = F_UNUSED;
  1820. memcpy(&b, &f->buf, sizeof(b));
  1821. if (f->vma_use_count)
  1822. b.flags |= V4L2_BUF_FLAG_MAPPED;
  1823. if (copy_to_user(arg, &b, sizeof(b)))
  1824. return -EFAULT;
  1825. PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
  1826. return 0;
  1827. }
  1828. static int
  1829. et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
  1830. {
  1831. int type;
  1832. if (copy_from_user(&type, arg, sizeof(type)))
  1833. return -EFAULT;
  1834. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
  1835. return -EINVAL;
  1836. cam->stream = STREAM_ON;
  1837. DBG(3, "Stream on");
  1838. return 0;
  1839. }
  1840. static int
  1841. et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
  1842. {
  1843. int type, err;
  1844. if (copy_from_user(&type, arg, sizeof(type)))
  1845. return -EFAULT;
  1846. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
  1847. return -EINVAL;
  1848. if (cam->stream == STREAM_ON)
  1849. if ((err = et61x251_stream_interrupt(cam)))
  1850. return err;
  1851. et61x251_empty_framequeues(cam);
  1852. DBG(3, "Stream off");
  1853. return 0;
  1854. }
  1855. static int
  1856. et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
  1857. {
  1858. struct v4l2_streamparm sp;
  1859. if (copy_from_user(&sp, arg, sizeof(sp)))
  1860. return -EFAULT;
  1861. if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1862. return -EINVAL;
  1863. sp.parm.capture.extendedmode = 0;
  1864. sp.parm.capture.readbuffers = cam->nreadbuffers;
  1865. if (copy_to_user(arg, &sp, sizeof(sp)))
  1866. return -EFAULT;
  1867. return 0;
  1868. }
  1869. static int
  1870. et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
  1871. {
  1872. struct v4l2_streamparm sp;
  1873. if (copy_from_user(&sp, arg, sizeof(sp)))
  1874. return -EFAULT;
  1875. if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1876. return -EINVAL;
  1877. sp.parm.capture.extendedmode = 0;
  1878. if (sp.parm.capture.readbuffers == 0)
  1879. sp.parm.capture.readbuffers = cam->nreadbuffers;
  1880. if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
  1881. sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
  1882. if (copy_to_user(arg, &sp, sizeof(sp)))
  1883. return -EFAULT;
  1884. cam->nreadbuffers = sp.parm.capture.readbuffers;
  1885. return 0;
  1886. }
  1887. static long et61x251_ioctl_v4l2(struct file *filp,
  1888. unsigned int cmd, void __user *arg)
  1889. {
  1890. struct et61x251_device *cam = video_drvdata(filp);
  1891. switch (cmd) {
  1892. case VIDIOC_QUERYCAP:
  1893. return et61x251_vidioc_querycap(cam, arg);
  1894. case VIDIOC_ENUMINPUT:
  1895. return et61x251_vidioc_enuminput(cam, arg);
  1896. case VIDIOC_G_INPUT:
  1897. return et61x251_vidioc_g_input(cam, arg);
  1898. case VIDIOC_S_INPUT:
  1899. return et61x251_vidioc_s_input(cam, arg);
  1900. case VIDIOC_QUERYCTRL:
  1901. return et61x251_vidioc_query_ctrl(cam, arg);
  1902. case VIDIOC_G_CTRL:
  1903. return et61x251_vidioc_g_ctrl(cam, arg);
  1904. case VIDIOC_S_CTRL:
  1905. return et61x251_vidioc_s_ctrl(cam, arg);
  1906. case VIDIOC_CROPCAP:
  1907. return et61x251_vidioc_cropcap(cam, arg);
  1908. case VIDIOC_G_CROP:
  1909. return et61x251_vidioc_g_crop(cam, arg);
  1910. case VIDIOC_S_CROP:
  1911. return et61x251_vidioc_s_crop(cam, arg);
  1912. case VIDIOC_ENUM_FMT:
  1913. return et61x251_vidioc_enum_fmt(cam, arg);
  1914. case VIDIOC_G_FMT:
  1915. return et61x251_vidioc_g_fmt(cam, arg);
  1916. case VIDIOC_TRY_FMT:
  1917. case VIDIOC_S_FMT:
  1918. return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
  1919. case VIDIOC_ENUM_FRAMESIZES:
  1920. return et61x251_vidioc_enum_framesizes(cam, arg);
  1921. case VIDIOC_G_JPEGCOMP:
  1922. return et61x251_vidioc_g_jpegcomp(cam, arg);
  1923. case VIDIOC_S_JPEGCOMP:
  1924. return et61x251_vidioc_s_jpegcomp(cam, arg);
  1925. case VIDIOC_REQBUFS:
  1926. return et61x251_vidioc_reqbufs(cam, arg);
  1927. case VIDIOC_QUERYBUF:
  1928. return et61x251_vidioc_querybuf(cam, arg);
  1929. case VIDIOC_QBUF:
  1930. return et61x251_vidioc_qbuf(cam, arg);
  1931. case VIDIOC_DQBUF:
  1932. return et61x251_vidioc_dqbuf(cam, filp, arg);
  1933. case VIDIOC_STREAMON:
  1934. return et61x251_vidioc_streamon(cam, arg);
  1935. case VIDIOC_STREAMOFF:
  1936. return et61x251_vidioc_streamoff(cam, arg);
  1937. case VIDIOC_G_PARM:
  1938. return et61x251_vidioc_g_parm(cam, arg);
  1939. case VIDIOC_S_PARM:
  1940. return et61x251_vidioc_s_parm(cam, arg);
  1941. default:
  1942. return -ENOTTY;
  1943. }
  1944. }
  1945. static long et61x251_ioctl(struct file *filp,
  1946. unsigned int cmd, unsigned long arg)
  1947. {
  1948. struct et61x251_device *cam = video_drvdata(filp);
  1949. long err = 0;
  1950. if (mutex_lock_interruptible(&cam->fileop_mutex))
  1951. return -ERESTARTSYS;
  1952. if (cam->state & DEV_DISCONNECTED) {
  1953. DBG(1, "Device not present");
  1954. mutex_unlock(&cam->fileop_mutex);
  1955. return -ENODEV;
  1956. }
  1957. if (cam->state & DEV_MISCONFIGURED) {
  1958. DBG(1, "The camera is misconfigured. Close and open it "
  1959. "again.");
  1960. mutex_unlock(&cam->fileop_mutex);
  1961. return -EIO;
  1962. }
  1963. V4LDBG(3, "et61x251", cmd);
  1964. err = et61x251_ioctl_v4l2(filp, cmd, (void __user *)arg);
  1965. mutex_unlock(&cam->fileop_mutex);
  1966. return err;
  1967. }
  1968. static const struct v4l2_file_operations et61x251_fops = {
  1969. .owner = THIS_MODULE,
  1970. .open = et61x251_open,
  1971. .release = et61x251_release,
  1972. .unlocked_ioctl = et61x251_ioctl,
  1973. .read = et61x251_read,
  1974. .poll = et61x251_poll,
  1975. .mmap = et61x251_mmap,
  1976. };
  1977. /*****************************************************************************/
  1978. /* It exists a single interface only. We do not need to validate anything. */
  1979. static int
  1980. et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
  1981. {
  1982. struct usb_device *udev = interface_to_usbdev(intf);
  1983. struct et61x251_device* cam;
  1984. static unsigned int dev_nr;
  1985. unsigned int i;
  1986. int err = 0;
  1987. if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
  1988. return -ENOMEM;
  1989. cam->usbdev = udev;
  1990. if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
  1991. DBG(1, "kmalloc() failed");
  1992. err = -ENOMEM;
  1993. goto fail;
  1994. }
  1995. if (!(cam->v4ldev = video_device_alloc())) {
  1996. DBG(1, "video_device_alloc() failed");
  1997. err = -ENOMEM;
  1998. goto fail;
  1999. }
  2000. DBG(2, "ET61X[12]51 PC Camera Controller detected "
  2001. "(vid/pid 0x%04X:0x%04X)",id->idVendor, id->idProduct);
  2002. for (i = 0; et61x251_sensor_table[i]; i++) {
  2003. err = et61x251_sensor_table[i](cam);
  2004. if (!err)
  2005. break;
  2006. }
  2007. if (!err)
  2008. DBG(2, "%s image sensor detected", cam->sensor.name);
  2009. else {
  2010. DBG(1, "No supported image sensor detected");
  2011. err = -ENODEV;
  2012. goto fail;
  2013. }
  2014. if (et61x251_init(cam)) {
  2015. DBG(1, "Initialization failed. I will retry on open().");
  2016. cam->state |= DEV_MISCONFIGURED;
  2017. }
  2018. strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
  2019. cam->v4ldev->fops = &et61x251_fops;
  2020. cam->v4ldev->release = video_device_release;
  2021. cam->v4ldev->parent = &udev->dev;
  2022. video_set_drvdata(cam->v4ldev, cam);
  2023. init_completion(&cam->probe);
  2024. err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
  2025. video_nr[dev_nr]);
  2026. if (err) {
  2027. DBG(1, "V4L2 device registration failed");
  2028. if (err == -ENFILE && video_nr[dev_nr] == -1)
  2029. DBG(1, "Free /dev/videoX node not found");
  2030. video_nr[dev_nr] = -1;
  2031. dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
  2032. complete_all(&cam->probe);
  2033. goto fail;
  2034. }
  2035. DBG(2, "V4L2 device registered as %s",
  2036. video_device_node_name(cam->v4ldev));
  2037. cam->module_param.force_munmap = force_munmap[dev_nr];
  2038. cam->module_param.frame_timeout = frame_timeout[dev_nr];
  2039. dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
  2040. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2041. err = et61x251_create_sysfs(cam);
  2042. if (!err)
  2043. DBG(2, "Optional device control through 'sysfs' "
  2044. "interface ready");
  2045. else
  2046. DBG(2, "Failed to create 'sysfs' interface for optional "
  2047. "device controlling. Error #%d", err);
  2048. #else
  2049. DBG(2, "Optional device control through 'sysfs' interface disabled");
  2050. DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
  2051. "configuration option to enable it.");
  2052. #endif
  2053. usb_set_intfdata(intf, cam);
  2054. kref_init(&cam->kref);
  2055. usb_get_dev(cam->usbdev);
  2056. complete_all(&cam->probe);
  2057. return 0;
  2058. fail:
  2059. if (cam) {
  2060. kfree(cam->control_buffer);
  2061. if (cam->v4ldev)
  2062. video_device_release(cam->v4ldev);
  2063. kfree(cam);
  2064. }
  2065. return err;
  2066. }
  2067. static void et61x251_usb_disconnect(struct usb_interface* intf)
  2068. {
  2069. struct et61x251_device* cam;
  2070. down_write(&et61x251_dev_lock);
  2071. cam = usb_get_intfdata(intf);
  2072. DBG(2, "Disconnecting %s...", cam->v4ldev->name);
  2073. if (cam->users) {
  2074. DBG(2, "Device %s is open! Deregistration and memory "
  2075. "deallocation are deferred.",
  2076. video_device_node_name(cam->v4ldev));
  2077. cam->state |= DEV_MISCONFIGURED;
  2078. et61x251_stop_transfer(cam);
  2079. cam->state |= DEV_DISCONNECTED;
  2080. wake_up_interruptible(&cam->wait_frame);
  2081. wake_up(&cam->wait_stream);
  2082. } else
  2083. cam->state |= DEV_DISCONNECTED;
  2084. wake_up_interruptible_all(&cam->wait_open);
  2085. kref_put(&cam->kref, et61x251_release_resources);
  2086. up_write(&et61x251_dev_lock);
  2087. }
  2088. static struct usb_driver et61x251_usb_driver = {
  2089. .name = "et61x251",
  2090. .id_table = et61x251_id_table,
  2091. .probe = et61x251_usb_probe,
  2092. .disconnect = et61x251_usb_disconnect,
  2093. };
  2094. /*****************************************************************************/
  2095. static int __init et61x251_module_init(void)
  2096. {
  2097. int err = 0;
  2098. KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
  2099. KDBG(3, ET61X251_MODULE_AUTHOR);
  2100. if ((err = usb_register(&et61x251_usb_driver)))
  2101. KDBG(1, "usb_register() failed");
  2102. return err;
  2103. }
  2104. static void __exit et61x251_module_exit(void)
  2105. {
  2106. usb_deregister(&et61x251_usb_driver);
  2107. }
  2108. module_init(et61x251_module_init);
  2109. module_exit(et61x251_module_exit);