sn9c102_core.c 83 KB

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