sn9c102_core.c 69 KB

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