et61x251_core.c 64 KB

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