meye.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. /*
  2. * Motion Eye video4linux driver for Sony Vaio PictureBook
  3. *
  4. * Copyright (C) 2001-2004 Stelian Pop <stelian@popies.net>
  5. *
  6. * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
  7. *
  8. * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
  9. *
  10. * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
  11. *
  12. * Some parts borrowed from various video4linux drivers, especially
  13. * bttv-driver.c and zoran.c, see original files for credits.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/pci.h>
  31. #include <linux/init.h>
  32. #include <linux/videodev.h>
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/io.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/dma-mapping.h>
  41. #include "meye.h"
  42. #include <linux/meye.h>
  43. MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
  44. MODULE_DESCRIPTION("v4l2 driver for the MotionEye camera");
  45. MODULE_LICENSE("GPL");
  46. MODULE_VERSION(MEYE_DRIVER_VERSION);
  47. /* number of grab buffers */
  48. static unsigned int gbuffers = 2;
  49. module_param(gbuffers, int, 0444);
  50. MODULE_PARM_DESC(gbuffers, "number of capture buffers, default is 2 (32 max)");
  51. /* size of a grab buffer */
  52. static unsigned int gbufsize = MEYE_MAX_BUFSIZE;
  53. module_param(gbufsize, int, 0444);
  54. MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 614400"
  55. " (will be rounded up to a page multiple)");
  56. /* /dev/videoX registration number */
  57. static int video_nr = -1;
  58. module_param(video_nr, int, 0444);
  59. MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
  60. /* driver structure - only one possible */
  61. static struct meye meye;
  62. /****************************************************************************/
  63. /* Memory allocation routines (stolen from bttv-driver.c) */
  64. /****************************************************************************/
  65. static void *rvmalloc(unsigned long size)
  66. {
  67. void *mem;
  68. unsigned long adr;
  69. size = PAGE_ALIGN(size);
  70. mem = vmalloc_32(size);
  71. if (mem) {
  72. memset(mem, 0, size);
  73. adr = (unsigned long) mem;
  74. while (size > 0) {
  75. SetPageReserved(vmalloc_to_page((void *)adr));
  76. adr += PAGE_SIZE;
  77. size -= PAGE_SIZE;
  78. }
  79. }
  80. return mem;
  81. }
  82. static void rvfree(void * mem, unsigned long size)
  83. {
  84. unsigned long adr;
  85. if (mem) {
  86. adr = (unsigned long) mem;
  87. while ((long) size > 0) {
  88. ClearPageReserved(vmalloc_to_page((void *)adr));
  89. adr += PAGE_SIZE;
  90. size -= PAGE_SIZE;
  91. }
  92. vfree(mem);
  93. }
  94. }
  95. /*
  96. * return a page table pointing to N pages of locked memory
  97. *
  98. * NOTE: The meye device expects DMA addresses on 32 bits, we build
  99. * a table of 1024 entries = 4 bytes * 1024 = 4096 bytes.
  100. */
  101. static int ptable_alloc(void)
  102. {
  103. u32 *pt;
  104. int i;
  105. memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
  106. /* give only 32 bit DMA addresses */
  107. if (dma_set_mask(&meye.mchip_dev->dev, DMA_32BIT_MASK))
  108. return -1;
  109. meye.mchip_ptable_toc = dma_alloc_coherent(&meye.mchip_dev->dev,
  110. PAGE_SIZE,
  111. &meye.mchip_dmahandle,
  112. GFP_KERNEL);
  113. if (!meye.mchip_ptable_toc) {
  114. meye.mchip_dmahandle = 0;
  115. return -1;
  116. }
  117. pt = meye.mchip_ptable_toc;
  118. for (i = 0; i < MCHIP_NB_PAGES; i++) {
  119. dma_addr_t dma;
  120. meye.mchip_ptable[i] = dma_alloc_coherent(&meye.mchip_dev->dev,
  121. PAGE_SIZE,
  122. &dma,
  123. GFP_KERNEL);
  124. if (!meye.mchip_ptable[i]) {
  125. int j;
  126. pt = meye.mchip_ptable_toc;
  127. for (j = 0; j < i; ++j) {
  128. dma = (dma_addr_t) *pt;
  129. dma_free_coherent(&meye.mchip_dev->dev,
  130. PAGE_SIZE,
  131. meye.mchip_ptable[j], dma);
  132. pt++;
  133. }
  134. dma_free_coherent(&meye.mchip_dev->dev,
  135. PAGE_SIZE,
  136. meye.mchip_ptable_toc,
  137. meye.mchip_dmahandle);
  138. meye.mchip_ptable_toc = NULL;
  139. meye.mchip_dmahandle = 0;
  140. return -1;
  141. }
  142. *pt = (u32) dma;
  143. pt++;
  144. }
  145. return 0;
  146. }
  147. static void ptable_free(void)
  148. {
  149. u32 *pt;
  150. int i;
  151. pt = meye.mchip_ptable_toc;
  152. for (i = 0; i < MCHIP_NB_PAGES; i++) {
  153. dma_addr_t dma = (dma_addr_t) *pt;
  154. if (meye.mchip_ptable[i])
  155. dma_free_coherent(&meye.mchip_dev->dev,
  156. PAGE_SIZE,
  157. meye.mchip_ptable[i], dma);
  158. pt++;
  159. }
  160. if (meye.mchip_ptable_toc)
  161. dma_free_coherent(&meye.mchip_dev->dev,
  162. PAGE_SIZE,
  163. meye.mchip_ptable_toc,
  164. meye.mchip_dmahandle);
  165. memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
  166. meye.mchip_ptable_toc = NULL;
  167. meye.mchip_dmahandle = 0;
  168. }
  169. /* copy data from ptable into buf */
  170. static void ptable_copy(u8 *buf, int start, int size, int pt_pages)
  171. {
  172. int i;
  173. for (i = 0; i < (size / PAGE_SIZE) * PAGE_SIZE; i += PAGE_SIZE) {
  174. memcpy(buf + i, meye.mchip_ptable[start++], PAGE_SIZE);
  175. if (start >= pt_pages)
  176. start = 0;
  177. }
  178. memcpy(buf + i, meye.mchip_ptable[start], size % PAGE_SIZE);
  179. }
  180. /****************************************************************************/
  181. /* JPEG tables at different qualities to load into the VRJ chip */
  182. /****************************************************************************/
  183. /* return a set of quantisation tables based on a quality from 1 to 10 */
  184. static u16 *jpeg_quantisation_tables(int *length, int quality)
  185. {
  186. static u16 jpeg_tables[][70] = { {
  187. 0xdbff, 0x4300, 0xff00, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  188. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  189. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  190. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  191. 0xffff, 0xffff, 0xffff,
  192. 0xdbff, 0x4300, 0xff01, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  193. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  194. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  195. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  196. 0xffff, 0xffff, 0xffff,
  197. },
  198. {
  199. 0xdbff, 0x4300, 0x5000, 0x3c37, 0x3c46, 0x5032, 0x4146, 0x5a46,
  200. 0x5055, 0x785f, 0x82c8, 0x6e78, 0x786e, 0xaff5, 0x91b9, 0xffc8,
  201. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  202. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  203. 0xffff, 0xffff, 0xffff,
  204. 0xdbff, 0x4300, 0x5501, 0x5a5a, 0x6978, 0xeb78, 0x8282, 0xffeb,
  205. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  206. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  207. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  208. 0xffff, 0xffff, 0xffff,
  209. },
  210. {
  211. 0xdbff, 0x4300, 0x2800, 0x1e1c, 0x1e23, 0x2819, 0x2123, 0x2d23,
  212. 0x282b, 0x3c30, 0x4164, 0x373c, 0x3c37, 0x587b, 0x495d, 0x9164,
  213. 0x9980, 0x8f96, 0x8c80, 0xa08a, 0xe6b4, 0xa0c3, 0xdaaa, 0x8aad,
  214. 0xc88c, 0xcbff, 0xeeda, 0xfff5, 0xffff, 0xc19b, 0xffff, 0xfaff,
  215. 0xe6ff, 0xfffd, 0xfff8,
  216. 0xdbff, 0x4300, 0x2b01, 0x2d2d, 0x353c, 0x763c, 0x4141, 0xf876,
  217. 0x8ca5, 0xf8a5, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
  218. 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
  219. 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
  220. 0xf8f8, 0xf8f8, 0xfff8,
  221. },
  222. {
  223. 0xdbff, 0x4300, 0x1b00, 0x1412, 0x1417, 0x1b11, 0x1617, 0x1e17,
  224. 0x1b1c, 0x2820, 0x2b42, 0x2528, 0x2825, 0x3a51, 0x303d, 0x6042,
  225. 0x6555, 0x5f64, 0x5d55, 0x6a5b, 0x9978, 0x6a81, 0x9071, 0x5b73,
  226. 0x855d, 0x86b5, 0x9e90, 0xaba3, 0xabad, 0x8067, 0xc9bc, 0xa6ba,
  227. 0x99c7, 0xaba8, 0xffa4,
  228. 0xdbff, 0x4300, 0x1c01, 0x1e1e, 0x2328, 0x4e28, 0x2b2b, 0xa44e,
  229. 0x5d6e, 0xa46e, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
  230. 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
  231. 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
  232. 0xa4a4, 0xa4a4, 0xffa4,
  233. },
  234. {
  235. 0xdbff, 0x4300, 0x1400, 0x0f0e, 0x0f12, 0x140d, 0x1012, 0x1712,
  236. 0x1415, 0x1e18, 0x2132, 0x1c1e, 0x1e1c, 0x2c3d, 0x242e, 0x4932,
  237. 0x4c40, 0x474b, 0x4640, 0x5045, 0x735a, 0x5062, 0x6d55, 0x4556,
  238. 0x6446, 0x6588, 0x776d, 0x817b, 0x8182, 0x604e, 0x978d, 0x7d8c,
  239. 0x7396, 0x817e, 0xff7c,
  240. 0xdbff, 0x4300, 0x1501, 0x1717, 0x1a1e, 0x3b1e, 0x2121, 0x7c3b,
  241. 0x4653, 0x7c53, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
  242. 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
  243. 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
  244. 0x7c7c, 0x7c7c, 0xff7c,
  245. },
  246. {
  247. 0xdbff, 0x4300, 0x1000, 0x0c0b, 0x0c0e, 0x100a, 0x0d0e, 0x120e,
  248. 0x1011, 0x1813, 0x1a28, 0x1618, 0x1816, 0x2331, 0x1d25, 0x3a28,
  249. 0x3d33, 0x393c, 0x3833, 0x4037, 0x5c48, 0x404e, 0x5744, 0x3745,
  250. 0x5038, 0x516d, 0x5f57, 0x6762, 0x6768, 0x4d3e, 0x7971, 0x6470,
  251. 0x5c78, 0x6765, 0xff63,
  252. 0xdbff, 0x4300, 0x1101, 0x1212, 0x1518, 0x2f18, 0x1a1a, 0x632f,
  253. 0x3842, 0x6342, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
  254. 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
  255. 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
  256. 0x6363, 0x6363, 0xff63,
  257. },
  258. {
  259. 0xdbff, 0x4300, 0x0d00, 0x0a09, 0x0a0b, 0x0d08, 0x0a0b, 0x0e0b,
  260. 0x0d0e, 0x130f, 0x1520, 0x1213, 0x1312, 0x1c27, 0x171e, 0x2e20,
  261. 0x3129, 0x2e30, 0x2d29, 0x332c, 0x4a3a, 0x333e, 0x4636, 0x2c37,
  262. 0x402d, 0x4157, 0x4c46, 0x524e, 0x5253, 0x3e32, 0x615a, 0x505a,
  263. 0x4a60, 0x5251, 0xff4f,
  264. 0xdbff, 0x4300, 0x0e01, 0x0e0e, 0x1113, 0x2613, 0x1515, 0x4f26,
  265. 0x2d35, 0x4f35, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
  266. 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
  267. 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
  268. 0x4f4f, 0x4f4f, 0xff4f,
  269. },
  270. {
  271. 0xdbff, 0x4300, 0x0a00, 0x0707, 0x0708, 0x0a06, 0x0808, 0x0b08,
  272. 0x0a0a, 0x0e0b, 0x1018, 0x0d0e, 0x0e0d, 0x151d, 0x1116, 0x2318,
  273. 0x251f, 0x2224, 0x221f, 0x2621, 0x372b, 0x262f, 0x3429, 0x2129,
  274. 0x3022, 0x3141, 0x3934, 0x3e3b, 0x3e3e, 0x2e25, 0x4944, 0x3c43,
  275. 0x3748, 0x3e3d, 0xff3b,
  276. 0xdbff, 0x4300, 0x0a01, 0x0b0b, 0x0d0e, 0x1c0e, 0x1010, 0x3b1c,
  277. 0x2228, 0x3b28, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
  278. 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
  279. 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
  280. 0x3b3b, 0x3b3b, 0xff3b,
  281. },
  282. {
  283. 0xdbff, 0x4300, 0x0600, 0x0504, 0x0506, 0x0604, 0x0506, 0x0706,
  284. 0x0607, 0x0a08, 0x0a10, 0x090a, 0x0a09, 0x0e14, 0x0c0f, 0x1710,
  285. 0x1814, 0x1718, 0x1614, 0x1a16, 0x251d, 0x1a1f, 0x231b, 0x161c,
  286. 0x2016, 0x202c, 0x2623, 0x2927, 0x292a, 0x1f19, 0x302d, 0x282d,
  287. 0x2530, 0x2928, 0xff28,
  288. 0xdbff, 0x4300, 0x0701, 0x0707, 0x080a, 0x130a, 0x0a0a, 0x2813,
  289. 0x161a, 0x281a, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
  290. 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
  291. 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
  292. 0x2828, 0x2828, 0xff28,
  293. },
  294. {
  295. 0xdbff, 0x4300, 0x0300, 0x0202, 0x0203, 0x0302, 0x0303, 0x0403,
  296. 0x0303, 0x0504, 0x0508, 0x0405, 0x0504, 0x070a, 0x0607, 0x0c08,
  297. 0x0c0a, 0x0b0c, 0x0b0a, 0x0d0b, 0x120e, 0x0d10, 0x110e, 0x0b0e,
  298. 0x100b, 0x1016, 0x1311, 0x1514, 0x1515, 0x0f0c, 0x1817, 0x1416,
  299. 0x1218, 0x1514, 0xff14,
  300. 0xdbff, 0x4300, 0x0301, 0x0404, 0x0405, 0x0905, 0x0505, 0x1409,
  301. 0x0b0d, 0x140d, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
  302. 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
  303. 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
  304. 0x1414, 0x1414, 0xff14,
  305. },
  306. {
  307. 0xdbff, 0x4300, 0x0100, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  308. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  309. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  310. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  311. 0x0101, 0x0101, 0xff01,
  312. 0xdbff, 0x4300, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  313. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  314. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  315. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  316. 0x0101, 0x0101, 0xff01,
  317. } };
  318. if (quality < 0 || quality > 10) {
  319. printk(KERN_WARNING
  320. "meye: invalid quality level %d - using 8\n", quality);
  321. quality = 8;
  322. }
  323. *length = ARRAY_SIZE(jpeg_tables[quality]);
  324. return jpeg_tables[quality];
  325. }
  326. /* return a generic set of huffman tables */
  327. static u16 *jpeg_huffman_tables(int *length)
  328. {
  329. static u16 tables[] = {
  330. 0xC4FF, 0xB500, 0x0010, 0x0102, 0x0303, 0x0402, 0x0503, 0x0405,
  331. 0x0004, 0x0100, 0x017D, 0x0302, 0x0400, 0x0511, 0x2112, 0x4131,
  332. 0x1306, 0x6151, 0x2207, 0x1471, 0x8132, 0xA191, 0x2308, 0xB142,
  333. 0x15C1, 0xD152, 0x24F0, 0x6233, 0x8272, 0x0A09, 0x1716, 0x1918,
  334. 0x251A, 0x2726, 0x2928, 0x342A, 0x3635, 0x3837, 0x3A39, 0x4443,
  335. 0x4645, 0x4847, 0x4A49, 0x5453, 0x5655, 0x5857, 0x5A59, 0x6463,
  336. 0x6665, 0x6867, 0x6A69, 0x7473, 0x7675, 0x7877, 0x7A79, 0x8483,
  337. 0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998, 0xA29A,
  338. 0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6, 0xB9B8,
  339. 0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4, 0xD7D6,
  340. 0xD9D8, 0xE1DA, 0xE3E2, 0xE5E4, 0xE7E6, 0xE9E8, 0xF1EA, 0xF3F2,
  341. 0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
  342. 0xC4FF, 0xB500, 0x0011, 0x0102, 0x0402, 0x0304, 0x0704, 0x0405,
  343. 0x0004, 0x0201, 0x0077, 0x0201, 0x1103, 0x0504, 0x3121, 0x1206,
  344. 0x5141, 0x6107, 0x1371, 0x3222, 0x0881, 0x4214, 0xA191, 0xC1B1,
  345. 0x2309, 0x5233, 0x15F0, 0x7262, 0x0AD1, 0x2416, 0xE134, 0xF125,
  346. 0x1817, 0x1A19, 0x2726, 0x2928, 0x352A, 0x3736, 0x3938, 0x433A,
  347. 0x4544, 0x4746, 0x4948, 0x534A, 0x5554, 0x5756, 0x5958, 0x635A,
  348. 0x6564, 0x6766, 0x6968, 0x736A, 0x7574, 0x7776, 0x7978, 0x827A,
  349. 0x8483, 0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998,
  350. 0xA29A, 0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6,
  351. 0xB9B8, 0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4,
  352. 0xD7D6, 0xD9D8, 0xE2DA, 0xE4E3, 0xE6E5, 0xE8E7, 0xEAE9, 0xF3F2,
  353. 0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
  354. 0xC4FF, 0x1F00, 0x0000, 0x0501, 0x0101, 0x0101, 0x0101, 0x0000,
  355. 0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
  356. 0xFF0B,
  357. 0xC4FF, 0x1F00, 0x0001, 0x0103, 0x0101, 0x0101, 0x0101, 0x0101,
  358. 0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
  359. 0xFF0B
  360. };
  361. *length = ARRAY_SIZE(tables);
  362. return tables;
  363. }
  364. /****************************************************************************/
  365. /* MCHIP low-level functions */
  366. /****************************************************************************/
  367. /* returns the horizontal capture size */
  368. static inline int mchip_hsize(void)
  369. {
  370. return meye.params.subsample ? 320 : 640;
  371. }
  372. /* returns the vertical capture size */
  373. static inline int mchip_vsize(void)
  374. {
  375. return meye.params.subsample ? 240 : 480;
  376. }
  377. /* waits for a register to be available */
  378. static void mchip_sync(int reg)
  379. {
  380. u32 status;
  381. int i;
  382. if (reg == MCHIP_MM_FIFO_DATA) {
  383. for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
  384. status = readl(meye.mchip_mmregs +
  385. MCHIP_MM_FIFO_STATUS);
  386. if (!(status & MCHIP_MM_FIFO_WAIT)) {
  387. printk(KERN_WARNING "meye: fifo not ready\n");
  388. return;
  389. }
  390. if (status & MCHIP_MM_FIFO_READY)
  391. return;
  392. udelay(1);
  393. }
  394. } else if (reg > 0x80) {
  395. u32 mask = (reg < 0x100) ? MCHIP_HIC_STATUS_MCC_RDY
  396. : MCHIP_HIC_STATUS_VRJ_RDY;
  397. for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
  398. status = readl(meye.mchip_mmregs + MCHIP_HIC_STATUS);
  399. if (status & mask)
  400. return;
  401. udelay(1);
  402. }
  403. } else
  404. return;
  405. printk(KERN_WARNING
  406. "meye: mchip_sync() timeout on reg 0x%x status=0x%x\n",
  407. reg, status);
  408. }
  409. /* sets a value into the register */
  410. static inline void mchip_set(int reg, u32 v)
  411. {
  412. mchip_sync(reg);
  413. writel(v, meye.mchip_mmregs + reg);
  414. }
  415. /* get the register value */
  416. static inline u32 mchip_read(int reg)
  417. {
  418. mchip_sync(reg);
  419. return readl(meye.mchip_mmregs + reg);
  420. }
  421. /* wait for a register to become a particular value */
  422. static inline int mchip_delay(u32 reg, u32 v)
  423. {
  424. int n = 10;
  425. while (--n && mchip_read(reg) != v)
  426. udelay(1);
  427. return n;
  428. }
  429. /* setup subsampling */
  430. static void mchip_subsample(void)
  431. {
  432. mchip_set(MCHIP_MCC_R_SAMPLING, meye.params.subsample);
  433. mchip_set(MCHIP_MCC_R_XRANGE, mchip_hsize());
  434. mchip_set(MCHIP_MCC_R_YRANGE, mchip_vsize());
  435. mchip_set(MCHIP_MCC_B_XRANGE, mchip_hsize());
  436. mchip_set(MCHIP_MCC_B_YRANGE, mchip_vsize());
  437. mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
  438. }
  439. /* set the framerate into the mchip */
  440. static void mchip_set_framerate(void)
  441. {
  442. mchip_set(MCHIP_HIC_S_RATE, meye.params.framerate);
  443. }
  444. /* load some huffman and quantisation tables into the VRJ chip ready
  445. for JPEG compression */
  446. static void mchip_load_tables(void)
  447. {
  448. int i;
  449. int length;
  450. u16 *tables;
  451. tables = jpeg_huffman_tables(&length);
  452. for (i = 0; i < length; i++)
  453. writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
  454. tables = jpeg_quantisation_tables(&length, meye.params.quality);
  455. for (i = 0; i < length; i++)
  456. writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
  457. }
  458. /* setup the VRJ parameters in the chip */
  459. static void mchip_vrj_setup(u8 mode)
  460. {
  461. mchip_set(MCHIP_VRJ_BUS_MODE, 5);
  462. mchip_set(MCHIP_VRJ_SIGNAL_ACTIVE_LEVEL, 0x1f);
  463. mchip_set(MCHIP_VRJ_PDAT_USE, 1);
  464. mchip_set(MCHIP_VRJ_IRQ_FLAG, 0xa0);
  465. mchip_set(MCHIP_VRJ_MODE_SPECIFY, mode);
  466. mchip_set(MCHIP_VRJ_NUM_LINES, mchip_vsize());
  467. mchip_set(MCHIP_VRJ_NUM_PIXELS, mchip_hsize());
  468. mchip_set(MCHIP_VRJ_NUM_COMPONENTS, 0x1b);
  469. mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_LO, 0xFFFF);
  470. mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_HI, 0xFFFF);
  471. mchip_set(MCHIP_VRJ_COMP_DATA_FORMAT, 0xC);
  472. mchip_set(MCHIP_VRJ_RESTART_INTERVAL, 0);
  473. mchip_set(MCHIP_VRJ_SOF1, 0x601);
  474. mchip_set(MCHIP_VRJ_SOF2, 0x1502);
  475. mchip_set(MCHIP_VRJ_SOF3, 0x1503);
  476. mchip_set(MCHIP_VRJ_SOF4, 0x1596);
  477. mchip_set(MCHIP_VRJ_SOS, 0x0ed0);
  478. mchip_load_tables();
  479. }
  480. /* sets the DMA parameters into the chip */
  481. static void mchip_dma_setup(dma_addr_t dma_addr)
  482. {
  483. int i;
  484. mchip_set(MCHIP_MM_PT_ADDR, (u32)dma_addr);
  485. for (i = 0; i < 4; i++)
  486. mchip_set(MCHIP_MM_FIR(i), 0);
  487. meye.mchip_fnum = 0;
  488. }
  489. /* setup for DMA transfers - also zeros the framebuffer */
  490. static int mchip_dma_alloc(void)
  491. {
  492. if (!meye.mchip_dmahandle)
  493. if (ptable_alloc())
  494. return -1;
  495. return 0;
  496. }
  497. /* frees the DMA buffer */
  498. static void mchip_dma_free(void)
  499. {
  500. if (meye.mchip_dmahandle) {
  501. mchip_dma_setup(0);
  502. ptable_free();
  503. }
  504. }
  505. /* stop any existing HIC action and wait for any dma to complete then
  506. reset the dma engine */
  507. static void mchip_hic_stop(void)
  508. {
  509. int i, j;
  510. meye.mchip_mode = MCHIP_HIC_MODE_NOOP;
  511. if (!(mchip_read(MCHIP_HIC_STATUS) & MCHIP_HIC_STATUS_BUSY))
  512. return;
  513. for (i = 0; i < 20; ++i) {
  514. mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_STOP);
  515. mchip_delay(MCHIP_HIC_CMD, 0);
  516. for (j = 0; j < 100; ++j) {
  517. if (mchip_delay(MCHIP_HIC_STATUS,
  518. MCHIP_HIC_STATUS_IDLE))
  519. return;
  520. msleep(1);
  521. }
  522. printk(KERN_ERR "meye: need to reset HIC!\n");
  523. mchip_set(MCHIP_HIC_CTL, MCHIP_HIC_CTL_SOFT_RESET);
  524. msleep(250);
  525. }
  526. printk(KERN_ERR "meye: resetting HIC hanged!\n");
  527. }
  528. /****************************************************************************/
  529. /* MCHIP frame processing functions */
  530. /****************************************************************************/
  531. /* get the next ready frame from the dma engine */
  532. static u32 mchip_get_frame(void)
  533. {
  534. u32 v;
  535. v = mchip_read(MCHIP_MM_FIR(meye.mchip_fnum));
  536. return v;
  537. }
  538. /* frees the current frame from the dma engine */
  539. static void mchip_free_frame(void)
  540. {
  541. mchip_set(MCHIP_MM_FIR(meye.mchip_fnum), 0);
  542. meye.mchip_fnum++;
  543. meye.mchip_fnum %= 4;
  544. }
  545. /* read one frame from the framebuffer assuming it was captured using
  546. a uncompressed transfer */
  547. static void mchip_cont_read_frame(u32 v, u8 *buf, int size)
  548. {
  549. int pt_id;
  550. pt_id = (v >> 17) & 0x3FF;
  551. ptable_copy(buf, pt_id, size, MCHIP_NB_PAGES);
  552. }
  553. /* read a compressed frame from the framebuffer */
  554. static int mchip_comp_read_frame(u32 v, u8 *buf, int size)
  555. {
  556. int pt_start, pt_end, trailer;
  557. int fsize;
  558. int i;
  559. pt_start = (v >> 19) & 0xFF;
  560. pt_end = (v >> 11) & 0xFF;
  561. trailer = (v >> 1) & 0x3FF;
  562. if (pt_end < pt_start)
  563. fsize = (MCHIP_NB_PAGES_MJPEG - pt_start) * PAGE_SIZE +
  564. pt_end * PAGE_SIZE + trailer * 4;
  565. else
  566. fsize = (pt_end - pt_start) * PAGE_SIZE + trailer * 4;
  567. if (fsize > size) {
  568. printk(KERN_WARNING "meye: oversized compressed frame %d\n",
  569. fsize);
  570. return -1;
  571. }
  572. ptable_copy(buf, pt_start, fsize, MCHIP_NB_PAGES_MJPEG);
  573. #ifdef MEYE_JPEG_CORRECTION
  574. /* Some mchip generated jpeg frames are incorrect. In most
  575. * (all ?) of those cases, the final EOI (0xff 0xd9) marker
  576. * is not present at the end of the frame.
  577. *
  578. * Since adding the final marker is not enough to restore
  579. * the jpeg integrity, we drop the frame.
  580. */
  581. for (i = fsize - 1; i > 0 && buf[i] == 0xff; i--) ;
  582. if (i < 2 || buf[i - 1] != 0xff || buf[i] != 0xd9)
  583. return -1;
  584. #endif
  585. return fsize;
  586. }
  587. /* take a picture into SDRAM */
  588. static void mchip_take_picture(void)
  589. {
  590. int i;
  591. mchip_hic_stop();
  592. mchip_subsample();
  593. mchip_dma_setup(meye.mchip_dmahandle);
  594. mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_CAP);
  595. mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
  596. mchip_delay(MCHIP_HIC_CMD, 0);
  597. for (i = 0; i < 100; ++i) {
  598. if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
  599. break;
  600. msleep(1);
  601. }
  602. }
  603. /* dma a previously taken picture into a buffer */
  604. static void mchip_get_picture(u8 *buf, int bufsize)
  605. {
  606. u32 v;
  607. int i;
  608. mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_OUT);
  609. mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
  610. mchip_delay(MCHIP_HIC_CMD, 0);
  611. for (i = 0; i < 100; ++i) {
  612. if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
  613. break;
  614. msleep(1);
  615. }
  616. for (i = 0; i < 4; ++i) {
  617. v = mchip_get_frame();
  618. if (v & MCHIP_MM_FIR_RDY) {
  619. mchip_cont_read_frame(v, buf, bufsize);
  620. break;
  621. }
  622. mchip_free_frame();
  623. }
  624. }
  625. /* start continuous dma capture */
  626. static void mchip_continuous_start(void)
  627. {
  628. mchip_hic_stop();
  629. mchip_subsample();
  630. mchip_set_framerate();
  631. mchip_dma_setup(meye.mchip_dmahandle);
  632. meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
  633. mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_CONT_OUT);
  634. mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
  635. mchip_delay(MCHIP_HIC_CMD, 0);
  636. }
  637. /* compress one frame into a buffer */
  638. static int mchip_compress_frame(u8 *buf, int bufsize)
  639. {
  640. u32 v;
  641. int len = -1, i;
  642. mchip_vrj_setup(0x3f);
  643. udelay(50);
  644. mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_COMP);
  645. mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
  646. mchip_delay(MCHIP_HIC_CMD, 0);
  647. for (i = 0; i < 100; ++i) {
  648. if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
  649. break;
  650. msleep(1);
  651. }
  652. for (i = 0; i < 4; ++i) {
  653. v = mchip_get_frame();
  654. if (v & MCHIP_MM_FIR_RDY) {
  655. len = mchip_comp_read_frame(v, buf, bufsize);
  656. break;
  657. }
  658. mchip_free_frame();
  659. }
  660. return len;
  661. }
  662. #if 0
  663. /* uncompress one image into a buffer */
  664. static int mchip_uncompress_frame(u8 *img, int imgsize, u8 *buf, int bufsize)
  665. {
  666. mchip_vrj_setup(0x3f);
  667. udelay(50);
  668. mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_DECOMP);
  669. mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
  670. mchip_delay(MCHIP_HIC_CMD, 0);
  671. return mchip_comp_read_frame(buf, bufsize);
  672. }
  673. #endif
  674. /* start continuous compressed capture */
  675. static void mchip_cont_compression_start(void)
  676. {
  677. mchip_hic_stop();
  678. mchip_vrj_setup(0x3f);
  679. mchip_subsample();
  680. mchip_set_framerate();
  681. mchip_dma_setup(meye.mchip_dmahandle);
  682. meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
  683. mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_CONT_COMP);
  684. mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
  685. mchip_delay(MCHIP_HIC_CMD, 0);
  686. }
  687. /****************************************************************************/
  688. /* Interrupt handling */
  689. /****************************************************************************/
  690. static irqreturn_t meye_irq(int irq, void *dev_id)
  691. {
  692. u32 v;
  693. int reqnr;
  694. static int sequence;
  695. v = mchip_read(MCHIP_MM_INTA);
  696. if (meye.mchip_mode != MCHIP_HIC_MODE_CONT_OUT &&
  697. meye.mchip_mode != MCHIP_HIC_MODE_CONT_COMP)
  698. return IRQ_NONE;
  699. again:
  700. v = mchip_get_frame();
  701. if (!(v & MCHIP_MM_FIR_RDY))
  702. return IRQ_HANDLED;
  703. if (meye.mchip_mode == MCHIP_HIC_MODE_CONT_OUT) {
  704. if (kfifo_get(meye.grabq, (unsigned char *)&reqnr,
  705. sizeof(int)) != sizeof(int)) {
  706. mchip_free_frame();
  707. return IRQ_HANDLED;
  708. }
  709. mchip_cont_read_frame(v, meye.grab_fbuffer + gbufsize * reqnr,
  710. mchip_hsize() * mchip_vsize() * 2);
  711. meye.grab_buffer[reqnr].size = mchip_hsize() * mchip_vsize() * 2;
  712. meye.grab_buffer[reqnr].state = MEYE_BUF_DONE;
  713. do_gettimeofday(&meye.grab_buffer[reqnr].timestamp);
  714. meye.grab_buffer[reqnr].sequence = sequence++;
  715. kfifo_put(meye.doneq, (unsigned char *)&reqnr, sizeof(int));
  716. wake_up_interruptible(&meye.proc_list);
  717. } else {
  718. int size;
  719. size = mchip_comp_read_frame(v, meye.grab_temp, gbufsize);
  720. if (size == -1) {
  721. mchip_free_frame();
  722. goto again;
  723. }
  724. if (kfifo_get(meye.grabq, (unsigned char *)&reqnr,
  725. sizeof(int)) != sizeof(int)) {
  726. mchip_free_frame();
  727. goto again;
  728. }
  729. memcpy(meye.grab_fbuffer + gbufsize * reqnr, meye.grab_temp,
  730. size);
  731. meye.grab_buffer[reqnr].size = size;
  732. meye.grab_buffer[reqnr].state = MEYE_BUF_DONE;
  733. do_gettimeofday(&meye.grab_buffer[reqnr].timestamp);
  734. meye.grab_buffer[reqnr].sequence = sequence++;
  735. kfifo_put(meye.doneq, (unsigned char *)&reqnr, sizeof(int));
  736. wake_up_interruptible(&meye.proc_list);
  737. }
  738. mchip_free_frame();
  739. goto again;
  740. }
  741. /****************************************************************************/
  742. /* video4linux integration */
  743. /****************************************************************************/
  744. static int meye_open(struct inode *inode, struct file *file)
  745. {
  746. int i, err;
  747. lock_kernel();
  748. err = video_exclusive_open(inode, file);
  749. if (err < 0) {
  750. unlock_kernel();
  751. return err;
  752. }
  753. mchip_hic_stop();
  754. if (mchip_dma_alloc()) {
  755. printk(KERN_ERR "meye: mchip framebuffer allocation failed\n");
  756. video_exclusive_release(inode, file);
  757. unlock_kernel();
  758. return -ENOBUFS;
  759. }
  760. for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
  761. meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
  762. kfifo_reset(meye.grabq);
  763. kfifo_reset(meye.doneq);
  764. unlock_kernel();
  765. return 0;
  766. }
  767. static int meye_release(struct inode *inode, struct file *file)
  768. {
  769. mchip_hic_stop();
  770. mchip_dma_free();
  771. video_exclusive_release(inode, file);
  772. return 0;
  773. }
  774. static int meyeioc_g_params(struct meye_params *p)
  775. {
  776. *p = meye.params;
  777. return 0;
  778. }
  779. static int meyeioc_s_params(struct meye_params *jp)
  780. {
  781. if (jp->subsample > 1)
  782. return -EINVAL;
  783. if (jp->quality > 10)
  784. return -EINVAL;
  785. if (jp->sharpness > 63 || jp->agc > 63 || jp->picture > 63)
  786. return -EINVAL;
  787. if (jp->framerate > 31)
  788. return -EINVAL;
  789. mutex_lock(&meye.lock);
  790. if (meye.params.subsample != jp->subsample ||
  791. meye.params.quality != jp->quality)
  792. mchip_hic_stop(); /* need restart */
  793. meye.params = *jp;
  794. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERASHARPNESS,
  795. meye.params.sharpness);
  796. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAAGC,
  797. meye.params.agc);
  798. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAPICTURE,
  799. meye.params.picture);
  800. mutex_unlock(&meye.lock);
  801. return 0;
  802. }
  803. static int meyeioc_qbuf_capt(int *nb)
  804. {
  805. if (!meye.grab_fbuffer)
  806. return -EINVAL;
  807. if (*nb >= gbuffers)
  808. return -EINVAL;
  809. if (*nb < 0) {
  810. /* stop capture */
  811. mchip_hic_stop();
  812. return 0;
  813. }
  814. if (meye.grab_buffer[*nb].state != MEYE_BUF_UNUSED)
  815. return -EBUSY;
  816. mutex_lock(&meye.lock);
  817. if (meye.mchip_mode != MCHIP_HIC_MODE_CONT_COMP)
  818. mchip_cont_compression_start();
  819. meye.grab_buffer[*nb].state = MEYE_BUF_USING;
  820. kfifo_put(meye.grabq, (unsigned char *)nb, sizeof(int));
  821. mutex_unlock(&meye.lock);
  822. return 0;
  823. }
  824. static int meyeioc_sync(struct file *file, void *fh, int *i)
  825. {
  826. int unused;
  827. if (*i < 0 || *i >= gbuffers)
  828. return -EINVAL;
  829. mutex_lock(&meye.lock);
  830. switch (meye.grab_buffer[*i].state) {
  831. case MEYE_BUF_UNUSED:
  832. mutex_unlock(&meye.lock);
  833. return -EINVAL;
  834. case MEYE_BUF_USING:
  835. if (file->f_flags & O_NONBLOCK) {
  836. mutex_unlock(&meye.lock);
  837. return -EAGAIN;
  838. }
  839. if (wait_event_interruptible(meye.proc_list,
  840. (meye.grab_buffer[*i].state != MEYE_BUF_USING))) {
  841. mutex_unlock(&meye.lock);
  842. return -EINTR;
  843. }
  844. /* fall through */
  845. case MEYE_BUF_DONE:
  846. meye.grab_buffer[*i].state = MEYE_BUF_UNUSED;
  847. kfifo_get(meye.doneq, (unsigned char *)&unused, sizeof(int));
  848. }
  849. *i = meye.grab_buffer[*i].size;
  850. mutex_unlock(&meye.lock);
  851. return 0;
  852. }
  853. static int meyeioc_stillcapt(void)
  854. {
  855. if (!meye.grab_fbuffer)
  856. return -EINVAL;
  857. if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
  858. return -EBUSY;
  859. mutex_lock(&meye.lock);
  860. meye.grab_buffer[0].state = MEYE_BUF_USING;
  861. mchip_take_picture();
  862. mchip_get_picture(meye.grab_fbuffer,
  863. mchip_hsize() * mchip_vsize() * 2);
  864. meye.grab_buffer[0].state = MEYE_BUF_DONE;
  865. mutex_unlock(&meye.lock);
  866. return 0;
  867. }
  868. static int meyeioc_stilljcapt(int *len)
  869. {
  870. if (!meye.grab_fbuffer)
  871. return -EINVAL;
  872. if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
  873. return -EBUSY;
  874. mutex_lock(&meye.lock);
  875. meye.grab_buffer[0].state = MEYE_BUF_USING;
  876. *len = -1;
  877. while (*len == -1) {
  878. mchip_take_picture();
  879. *len = mchip_compress_frame(meye.grab_fbuffer, gbufsize);
  880. }
  881. meye.grab_buffer[0].state = MEYE_BUF_DONE;
  882. mutex_unlock(&meye.lock);
  883. return 0;
  884. }
  885. static int vidioc_querycap(struct file *file, void *fh,
  886. struct v4l2_capability *cap)
  887. {
  888. memset(cap, 0, sizeof(*cap));
  889. strcpy(cap->driver, "meye");
  890. strcpy(cap->card, "meye");
  891. sprintf(cap->bus_info, "PCI:%s", pci_name(meye.mchip_dev));
  892. cap->version = (MEYE_DRIVER_MAJORVERSION << 8) +
  893. MEYE_DRIVER_MINORVERSION;
  894. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  895. V4L2_CAP_STREAMING;
  896. return 0;
  897. }
  898. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  899. {
  900. if (i->index != 0)
  901. return -EINVAL;
  902. memset(i, 0, sizeof(*i));
  903. i->index = 0;
  904. strcpy(i->name, "Camera");
  905. i->type = V4L2_INPUT_TYPE_CAMERA;
  906. return 0;
  907. }
  908. static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
  909. {
  910. *i = 0;
  911. return 0;
  912. }
  913. static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
  914. {
  915. if (i != 0)
  916. return -EINVAL;
  917. return 0;
  918. }
  919. static int vidioc_queryctrl(struct file *file, void *fh,
  920. struct v4l2_queryctrl *c)
  921. {
  922. switch (c->id) {
  923. case V4L2_CID_BRIGHTNESS:
  924. c->type = V4L2_CTRL_TYPE_INTEGER;
  925. strcpy(c->name, "Brightness");
  926. c->minimum = 0;
  927. c->maximum = 63;
  928. c->step = 1;
  929. c->default_value = 32;
  930. c->flags = 0;
  931. break;
  932. case V4L2_CID_HUE:
  933. c->type = V4L2_CTRL_TYPE_INTEGER;
  934. strcpy(c->name, "Hue");
  935. c->minimum = 0;
  936. c->maximum = 63;
  937. c->step = 1;
  938. c->default_value = 32;
  939. c->flags = 0;
  940. break;
  941. case V4L2_CID_CONTRAST:
  942. c->type = V4L2_CTRL_TYPE_INTEGER;
  943. strcpy(c->name, "Contrast");
  944. c->minimum = 0;
  945. c->maximum = 63;
  946. c->step = 1;
  947. c->default_value = 32;
  948. c->flags = 0;
  949. break;
  950. case V4L2_CID_SATURATION:
  951. c->type = V4L2_CTRL_TYPE_INTEGER;
  952. strcpy(c->name, "Saturation");
  953. c->minimum = 0;
  954. c->maximum = 63;
  955. c->step = 1;
  956. c->default_value = 32;
  957. c->flags = 0;
  958. break;
  959. case V4L2_CID_AGC:
  960. c->type = V4L2_CTRL_TYPE_INTEGER;
  961. strcpy(c->name, "Agc");
  962. c->minimum = 0;
  963. c->maximum = 63;
  964. c->step = 1;
  965. c->default_value = 48;
  966. c->flags = 0;
  967. break;
  968. case V4L2_CID_MEYE_SHARPNESS:
  969. case V4L2_CID_SHARPNESS:
  970. c->type = V4L2_CTRL_TYPE_INTEGER;
  971. strcpy(c->name, "Sharpness");
  972. c->minimum = 0;
  973. c->maximum = 63;
  974. c->step = 1;
  975. c->default_value = 32;
  976. /* Continue to report legacy private SHARPNESS ctrl but
  977. * say it is disabled in preference to ctrl in the spec
  978. */
  979. c->flags = (c->id == V4L2_CID_SHARPNESS) ? 0 :
  980. V4L2_CTRL_FLAG_DISABLED;
  981. break;
  982. case V4L2_CID_PICTURE:
  983. c->type = V4L2_CTRL_TYPE_INTEGER;
  984. strcpy(c->name, "Picture");
  985. c->minimum = 0;
  986. c->maximum = 63;
  987. c->step = 1;
  988. c->default_value = 0;
  989. c->flags = 0;
  990. break;
  991. case V4L2_CID_JPEGQUAL:
  992. c->type = V4L2_CTRL_TYPE_INTEGER;
  993. strcpy(c->name, "JPEG quality");
  994. c->minimum = 0;
  995. c->maximum = 10;
  996. c->step = 1;
  997. c->default_value = 8;
  998. c->flags = 0;
  999. break;
  1000. case V4L2_CID_FRAMERATE:
  1001. c->type = V4L2_CTRL_TYPE_INTEGER;
  1002. strcpy(c->name, "Framerate");
  1003. c->minimum = 0;
  1004. c->maximum = 31;
  1005. c->step = 1;
  1006. c->default_value = 0;
  1007. c->flags = 0;
  1008. break;
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. return 0;
  1013. }
  1014. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  1015. {
  1016. mutex_lock(&meye.lock);
  1017. switch (c->id) {
  1018. case V4L2_CID_BRIGHTNESS:
  1019. sony_pic_camera_command(
  1020. SONY_PIC_COMMAND_SETCAMERABRIGHTNESS, c->value);
  1021. meye.picture.brightness = c->value << 10;
  1022. break;
  1023. case V4L2_CID_HUE:
  1024. sony_pic_camera_command(
  1025. SONY_PIC_COMMAND_SETCAMERAHUE, c->value);
  1026. meye.picture.hue = c->value << 10;
  1027. break;
  1028. case V4L2_CID_CONTRAST:
  1029. sony_pic_camera_command(
  1030. SONY_PIC_COMMAND_SETCAMERACONTRAST, c->value);
  1031. meye.picture.contrast = c->value << 10;
  1032. break;
  1033. case V4L2_CID_SATURATION:
  1034. sony_pic_camera_command(
  1035. SONY_PIC_COMMAND_SETCAMERACOLOR, c->value);
  1036. meye.picture.colour = c->value << 10;
  1037. break;
  1038. case V4L2_CID_AGC:
  1039. sony_pic_camera_command(
  1040. SONY_PIC_COMMAND_SETCAMERAAGC, c->value);
  1041. meye.params.agc = c->value;
  1042. break;
  1043. case V4L2_CID_SHARPNESS:
  1044. case V4L2_CID_MEYE_SHARPNESS:
  1045. sony_pic_camera_command(
  1046. SONY_PIC_COMMAND_SETCAMERASHARPNESS, c->value);
  1047. meye.params.sharpness = c->value;
  1048. break;
  1049. case V4L2_CID_PICTURE:
  1050. sony_pic_camera_command(
  1051. SONY_PIC_COMMAND_SETCAMERAPICTURE, c->value);
  1052. meye.params.picture = c->value;
  1053. break;
  1054. case V4L2_CID_JPEGQUAL:
  1055. meye.params.quality = c->value;
  1056. break;
  1057. case V4L2_CID_FRAMERATE:
  1058. meye.params.framerate = c->value;
  1059. break;
  1060. default:
  1061. mutex_unlock(&meye.lock);
  1062. return -EINVAL;
  1063. }
  1064. mutex_unlock(&meye.lock);
  1065. return 0;
  1066. }
  1067. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  1068. {
  1069. mutex_lock(&meye.lock);
  1070. switch (c->id) {
  1071. case V4L2_CID_BRIGHTNESS:
  1072. c->value = meye.picture.brightness >> 10;
  1073. break;
  1074. case V4L2_CID_HUE:
  1075. c->value = meye.picture.hue >> 10;
  1076. break;
  1077. case V4L2_CID_CONTRAST:
  1078. c->value = meye.picture.contrast >> 10;
  1079. break;
  1080. case V4L2_CID_SATURATION:
  1081. c->value = meye.picture.colour >> 10;
  1082. break;
  1083. case V4L2_CID_AGC:
  1084. c->value = meye.params.agc;
  1085. break;
  1086. case V4L2_CID_SHARPNESS:
  1087. case V4L2_CID_MEYE_SHARPNESS:
  1088. c->value = meye.params.sharpness;
  1089. break;
  1090. case V4L2_CID_PICTURE:
  1091. c->value = meye.params.picture;
  1092. break;
  1093. case V4L2_CID_JPEGQUAL:
  1094. c->value = meye.params.quality;
  1095. break;
  1096. case V4L2_CID_FRAMERATE:
  1097. c->value = meye.params.framerate;
  1098. break;
  1099. default:
  1100. mutex_unlock(&meye.lock);
  1101. return -EINVAL;
  1102. }
  1103. mutex_unlock(&meye.lock);
  1104. return 0;
  1105. }
  1106. static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
  1107. struct v4l2_fmtdesc *f)
  1108. {
  1109. if (f->index > 1)
  1110. return -EINVAL;
  1111. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1112. return -EINVAL;
  1113. if (f->index == 0) {
  1114. /* standard YUV 422 capture */
  1115. memset(f, 0, sizeof(*f));
  1116. f->index = 0;
  1117. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1118. f->flags = 0;
  1119. strcpy(f->description, "YUV422");
  1120. f->pixelformat = V4L2_PIX_FMT_YUYV;
  1121. } else {
  1122. /* compressed MJPEG capture */
  1123. memset(f, 0, sizeof(*f));
  1124. f->index = 1;
  1125. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1126. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  1127. strcpy(f->description, "MJPEG");
  1128. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  1129. }
  1130. return 0;
  1131. }
  1132. static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
  1133. struct v4l2_format *f)
  1134. {
  1135. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1136. return -EINVAL;
  1137. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
  1138. f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1139. return -EINVAL;
  1140. if (f->fmt.pix.field != V4L2_FIELD_ANY &&
  1141. f->fmt.pix.field != V4L2_FIELD_NONE)
  1142. return -EINVAL;
  1143. f->fmt.pix.field = V4L2_FIELD_NONE;
  1144. if (f->fmt.pix.width <= 320) {
  1145. f->fmt.pix.width = 320;
  1146. f->fmt.pix.height = 240;
  1147. } else {
  1148. f->fmt.pix.width = 640;
  1149. f->fmt.pix.height = 480;
  1150. }
  1151. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1152. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1153. f->fmt.pix.bytesperline;
  1154. f->fmt.pix.colorspace = 0;
  1155. f->fmt.pix.priv = 0;
  1156. return 0;
  1157. }
  1158. static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
  1159. struct v4l2_format *f)
  1160. {
  1161. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1162. return -EINVAL;
  1163. memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
  1164. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1165. switch (meye.mchip_mode) {
  1166. case MCHIP_HIC_MODE_CONT_OUT:
  1167. default:
  1168. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  1169. break;
  1170. case MCHIP_HIC_MODE_CONT_COMP:
  1171. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
  1172. break;
  1173. }
  1174. f->fmt.pix.field = V4L2_FIELD_NONE;
  1175. f->fmt.pix.width = mchip_hsize();
  1176. f->fmt.pix.height = mchip_vsize();
  1177. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1178. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1179. f->fmt.pix.bytesperline;
  1180. f->fmt.pix.colorspace = 0;
  1181. f->fmt.pix.priv = 0;
  1182. return 0;
  1183. }
  1184. static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
  1185. struct v4l2_format *f)
  1186. {
  1187. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1188. return -EINVAL;
  1189. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
  1190. f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1191. return -EINVAL;
  1192. if (f->fmt.pix.field != V4L2_FIELD_ANY &&
  1193. f->fmt.pix.field != V4L2_FIELD_NONE)
  1194. return -EINVAL;
  1195. f->fmt.pix.field = V4L2_FIELD_NONE;
  1196. mutex_lock(&meye.lock);
  1197. if (f->fmt.pix.width <= 320) {
  1198. f->fmt.pix.width = 320;
  1199. f->fmt.pix.height = 240;
  1200. meye.params.subsample = 1;
  1201. } else {
  1202. f->fmt.pix.width = 640;
  1203. f->fmt.pix.height = 480;
  1204. meye.params.subsample = 0;
  1205. }
  1206. switch (f->fmt.pix.pixelformat) {
  1207. case V4L2_PIX_FMT_YUYV:
  1208. meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
  1209. break;
  1210. case V4L2_PIX_FMT_MJPEG:
  1211. meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
  1212. break;
  1213. }
  1214. mutex_unlock(&meye.lock);
  1215. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1216. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1217. f->fmt.pix.bytesperline;
  1218. f->fmt.pix.colorspace = 0;
  1219. f->fmt.pix.priv = 0;
  1220. return 0;
  1221. }
  1222. static int vidioc_reqbufs(struct file *file, void *fh,
  1223. struct v4l2_requestbuffers *req)
  1224. {
  1225. int i;
  1226. if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1227. return -EINVAL;
  1228. if (req->memory != V4L2_MEMORY_MMAP)
  1229. return -EINVAL;
  1230. if (meye.grab_fbuffer && req->count == gbuffers) {
  1231. /* already allocated, no modifications */
  1232. return 0;
  1233. }
  1234. mutex_lock(&meye.lock);
  1235. if (meye.grab_fbuffer) {
  1236. for (i = 0; i < gbuffers; i++)
  1237. if (meye.vma_use_count[i]) {
  1238. mutex_unlock(&meye.lock);
  1239. return -EINVAL;
  1240. }
  1241. rvfree(meye.grab_fbuffer, gbuffers * gbufsize);
  1242. meye.grab_fbuffer = NULL;
  1243. }
  1244. gbuffers = max(2, min((int)req->count, MEYE_MAX_BUFNBRS));
  1245. req->count = gbuffers;
  1246. meye.grab_fbuffer = rvmalloc(gbuffers * gbufsize);
  1247. if (!meye.grab_fbuffer) {
  1248. printk(KERN_ERR "meye: v4l framebuffer allocation"
  1249. " failed\n");
  1250. mutex_unlock(&meye.lock);
  1251. return -ENOMEM;
  1252. }
  1253. for (i = 0; i < gbuffers; i++)
  1254. meye.vma_use_count[i] = 0;
  1255. mutex_unlock(&meye.lock);
  1256. return 0;
  1257. }
  1258. static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1259. {
  1260. int index = buf->index;
  1261. if (index < 0 || index >= gbuffers)
  1262. return -EINVAL;
  1263. memset(buf, 0, sizeof(*buf));
  1264. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1265. buf->index = index;
  1266. buf->bytesused = meye.grab_buffer[index].size;
  1267. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1268. if (meye.grab_buffer[index].state == MEYE_BUF_USING)
  1269. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1270. if (meye.grab_buffer[index].state == MEYE_BUF_DONE)
  1271. buf->flags |= V4L2_BUF_FLAG_DONE;
  1272. buf->field = V4L2_FIELD_NONE;
  1273. buf->timestamp = meye.grab_buffer[index].timestamp;
  1274. buf->sequence = meye.grab_buffer[index].sequence;
  1275. buf->memory = V4L2_MEMORY_MMAP;
  1276. buf->m.offset = index * gbufsize;
  1277. buf->length = gbufsize;
  1278. return 0;
  1279. }
  1280. static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1281. {
  1282. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1283. return -EINVAL;
  1284. if (buf->memory != V4L2_MEMORY_MMAP)
  1285. return -EINVAL;
  1286. if (buf->index < 0 || buf->index >= gbuffers)
  1287. return -EINVAL;
  1288. if (meye.grab_buffer[buf->index].state != MEYE_BUF_UNUSED)
  1289. return -EINVAL;
  1290. mutex_lock(&meye.lock);
  1291. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1292. buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1293. meye.grab_buffer[buf->index].state = MEYE_BUF_USING;
  1294. kfifo_put(meye.grabq, (unsigned char *)&buf->index, sizeof(int));
  1295. mutex_unlock(&meye.lock);
  1296. return 0;
  1297. }
  1298. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1299. {
  1300. int reqnr;
  1301. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1302. return -EINVAL;
  1303. if (buf->memory != V4L2_MEMORY_MMAP)
  1304. return -EINVAL;
  1305. mutex_lock(&meye.lock);
  1306. if (kfifo_len(meye.doneq) == 0 && file->f_flags & O_NONBLOCK) {
  1307. mutex_unlock(&meye.lock);
  1308. return -EAGAIN;
  1309. }
  1310. if (wait_event_interruptible(meye.proc_list,
  1311. kfifo_len(meye.doneq) != 0) < 0) {
  1312. mutex_unlock(&meye.lock);
  1313. return -EINTR;
  1314. }
  1315. if (!kfifo_get(meye.doneq, (unsigned char *)&reqnr,
  1316. sizeof(int))) {
  1317. mutex_unlock(&meye.lock);
  1318. return -EBUSY;
  1319. }
  1320. if (meye.grab_buffer[reqnr].state != MEYE_BUF_DONE) {
  1321. mutex_unlock(&meye.lock);
  1322. return -EINVAL;
  1323. }
  1324. buf->index = reqnr;
  1325. buf->bytesused = meye.grab_buffer[reqnr].size;
  1326. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1327. buf->field = V4L2_FIELD_NONE;
  1328. buf->timestamp = meye.grab_buffer[reqnr].timestamp;
  1329. buf->sequence = meye.grab_buffer[reqnr].sequence;
  1330. buf->memory = V4L2_MEMORY_MMAP;
  1331. buf->m.offset = reqnr * gbufsize;
  1332. buf->length = gbufsize;
  1333. meye.grab_buffer[reqnr].state = MEYE_BUF_UNUSED;
  1334. mutex_unlock(&meye.lock);
  1335. return 0;
  1336. }
  1337. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1338. {
  1339. mutex_lock(&meye.lock);
  1340. switch (meye.mchip_mode) {
  1341. case MCHIP_HIC_MODE_CONT_OUT:
  1342. mchip_continuous_start();
  1343. break;
  1344. case MCHIP_HIC_MODE_CONT_COMP:
  1345. mchip_cont_compression_start();
  1346. break;
  1347. default:
  1348. mutex_unlock(&meye.lock);
  1349. return -EINVAL;
  1350. }
  1351. mutex_unlock(&meye.lock);
  1352. return 0;
  1353. }
  1354. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  1355. {
  1356. mutex_lock(&meye.lock);
  1357. mchip_hic_stop();
  1358. kfifo_reset(meye.grabq);
  1359. kfifo_reset(meye.doneq);
  1360. for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
  1361. meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
  1362. mutex_unlock(&meye.lock);
  1363. return 0;
  1364. }
  1365. static int vidioc_default(struct file *file, void *fh, int cmd, void *arg)
  1366. {
  1367. switch (cmd) {
  1368. case MEYEIOC_G_PARAMS:
  1369. return meyeioc_g_params((struct meye_params *) arg);
  1370. case MEYEIOC_S_PARAMS:
  1371. return meyeioc_s_params((struct meye_params *) arg);
  1372. case MEYEIOC_QBUF_CAPT:
  1373. return meyeioc_qbuf_capt((int *) arg);
  1374. case MEYEIOC_SYNC:
  1375. return meyeioc_sync(file, fh, (int *) arg);
  1376. case MEYEIOC_STILLCAPT:
  1377. return meyeioc_stillcapt();
  1378. case MEYEIOC_STILLJCAPT:
  1379. return meyeioc_stilljcapt((int *) arg);
  1380. default:
  1381. return -EINVAL;
  1382. }
  1383. }
  1384. static unsigned int meye_poll(struct file *file, poll_table *wait)
  1385. {
  1386. unsigned int res = 0;
  1387. mutex_lock(&meye.lock);
  1388. poll_wait(file, &meye.proc_list, wait);
  1389. if (kfifo_len(meye.doneq))
  1390. res = POLLIN | POLLRDNORM;
  1391. mutex_unlock(&meye.lock);
  1392. return res;
  1393. }
  1394. static void meye_vm_open(struct vm_area_struct *vma)
  1395. {
  1396. long idx = (long)vma->vm_private_data;
  1397. meye.vma_use_count[idx]++;
  1398. }
  1399. static void meye_vm_close(struct vm_area_struct *vma)
  1400. {
  1401. long idx = (long)vma->vm_private_data;
  1402. meye.vma_use_count[idx]--;
  1403. }
  1404. static struct vm_operations_struct meye_vm_ops = {
  1405. .open = meye_vm_open,
  1406. .close = meye_vm_close,
  1407. };
  1408. static int meye_mmap(struct file *file, struct vm_area_struct *vma)
  1409. {
  1410. unsigned long start = vma->vm_start;
  1411. unsigned long size = vma->vm_end - vma->vm_start;
  1412. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1413. unsigned long page, pos;
  1414. mutex_lock(&meye.lock);
  1415. if (size > gbuffers * gbufsize) {
  1416. mutex_unlock(&meye.lock);
  1417. return -EINVAL;
  1418. }
  1419. if (!meye.grab_fbuffer) {
  1420. int i;
  1421. /* lazy allocation */
  1422. meye.grab_fbuffer = rvmalloc(gbuffers*gbufsize);
  1423. if (!meye.grab_fbuffer) {
  1424. printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
  1425. mutex_unlock(&meye.lock);
  1426. return -ENOMEM;
  1427. }
  1428. for (i = 0; i < gbuffers; i++)
  1429. meye.vma_use_count[i] = 0;
  1430. }
  1431. pos = (unsigned long)meye.grab_fbuffer + offset;
  1432. while (size > 0) {
  1433. page = vmalloc_to_pfn((void *)pos);
  1434. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  1435. mutex_unlock(&meye.lock);
  1436. return -EAGAIN;
  1437. }
  1438. start += PAGE_SIZE;
  1439. pos += PAGE_SIZE;
  1440. if (size > PAGE_SIZE)
  1441. size -= PAGE_SIZE;
  1442. else
  1443. size = 0;
  1444. }
  1445. vma->vm_ops = &meye_vm_ops;
  1446. vma->vm_flags &= ~VM_IO; /* not I/O memory */
  1447. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  1448. vma->vm_private_data = (void *) (offset / gbufsize);
  1449. meye_vm_open(vma);
  1450. mutex_unlock(&meye.lock);
  1451. return 0;
  1452. }
  1453. static const struct file_operations meye_fops = {
  1454. .owner = THIS_MODULE,
  1455. .open = meye_open,
  1456. .release = meye_release,
  1457. .mmap = meye_mmap,
  1458. .ioctl = video_ioctl2,
  1459. #ifdef CONFIG_COMPAT
  1460. .compat_ioctl = v4l_compat_ioctl32,
  1461. #endif
  1462. .poll = meye_poll,
  1463. .llseek = no_llseek,
  1464. };
  1465. static const struct v4l2_ioctl_ops meye_ioctl_ops = {
  1466. .vidioc_querycap = vidioc_querycap,
  1467. .vidioc_enum_input = vidioc_enum_input,
  1468. .vidioc_g_input = vidioc_g_input,
  1469. .vidioc_s_input = vidioc_s_input,
  1470. .vidioc_queryctrl = vidioc_queryctrl,
  1471. .vidioc_s_ctrl = vidioc_s_ctrl,
  1472. .vidioc_g_ctrl = vidioc_g_ctrl,
  1473. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1474. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1475. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1476. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1477. .vidioc_reqbufs = vidioc_reqbufs,
  1478. .vidioc_querybuf = vidioc_querybuf,
  1479. .vidioc_qbuf = vidioc_qbuf,
  1480. .vidioc_dqbuf = vidioc_dqbuf,
  1481. .vidioc_streamon = vidioc_streamon,
  1482. .vidioc_streamoff = vidioc_streamoff,
  1483. .vidioc_default = vidioc_default,
  1484. };
  1485. static struct video_device meye_template = {
  1486. .name = "meye",
  1487. .fops = &meye_fops,
  1488. .ioctl_ops = &meye_ioctl_ops,
  1489. .release = video_device_release,
  1490. .minor = -1,
  1491. };
  1492. #ifdef CONFIG_PM
  1493. static int meye_suspend(struct pci_dev *pdev, pm_message_t state)
  1494. {
  1495. pci_save_state(pdev);
  1496. meye.pm_mchip_mode = meye.mchip_mode;
  1497. mchip_hic_stop();
  1498. mchip_set(MCHIP_MM_INTA, 0x0);
  1499. return 0;
  1500. }
  1501. static int meye_resume(struct pci_dev *pdev)
  1502. {
  1503. pci_restore_state(pdev);
  1504. pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
  1505. mchip_delay(MCHIP_HIC_CMD, 0);
  1506. mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
  1507. msleep(1);
  1508. mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
  1509. msleep(1);
  1510. mchip_set(MCHIP_MM_PCI_MODE, 5);
  1511. msleep(1);
  1512. mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
  1513. switch (meye.pm_mchip_mode) {
  1514. case MCHIP_HIC_MODE_CONT_OUT:
  1515. mchip_continuous_start();
  1516. break;
  1517. case MCHIP_HIC_MODE_CONT_COMP:
  1518. mchip_cont_compression_start();
  1519. break;
  1520. }
  1521. return 0;
  1522. }
  1523. #endif
  1524. static int __devinit meye_probe(struct pci_dev *pcidev,
  1525. const struct pci_device_id *ent)
  1526. {
  1527. int ret = -EBUSY;
  1528. unsigned long mchip_adr;
  1529. if (meye.mchip_dev != NULL) {
  1530. printk(KERN_ERR "meye: only one device allowed!\n");
  1531. goto outnotdev;
  1532. }
  1533. meye.mchip_dev = pcidev;
  1534. meye.video_dev = video_device_alloc();
  1535. if (!meye.video_dev) {
  1536. printk(KERN_ERR "meye: video_device_alloc() failed!\n");
  1537. goto outnotdev;
  1538. }
  1539. ret = -ENOMEM;
  1540. meye.grab_temp = vmalloc(MCHIP_NB_PAGES_MJPEG * PAGE_SIZE);
  1541. if (!meye.grab_temp) {
  1542. printk(KERN_ERR "meye: grab buffer allocation failed\n");
  1543. goto outvmalloc;
  1544. }
  1545. spin_lock_init(&meye.grabq_lock);
  1546. meye.grabq = kfifo_alloc(sizeof(int) * MEYE_MAX_BUFNBRS, GFP_KERNEL,
  1547. &meye.grabq_lock);
  1548. if (IS_ERR(meye.grabq)) {
  1549. printk(KERN_ERR "meye: fifo allocation failed\n");
  1550. goto outkfifoalloc1;
  1551. }
  1552. spin_lock_init(&meye.doneq_lock);
  1553. meye.doneq = kfifo_alloc(sizeof(int) * MEYE_MAX_BUFNBRS, GFP_KERNEL,
  1554. &meye.doneq_lock);
  1555. if (IS_ERR(meye.doneq)) {
  1556. printk(KERN_ERR "meye: fifo allocation failed\n");
  1557. goto outkfifoalloc2;
  1558. }
  1559. memcpy(meye.video_dev, &meye_template, sizeof(meye_template));
  1560. meye.video_dev->parent = &meye.mchip_dev->dev;
  1561. ret = -EIO;
  1562. if ((ret = sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 1))) {
  1563. printk(KERN_ERR "meye: unable to power on the camera\n");
  1564. printk(KERN_ERR "meye: did you enable the camera in "
  1565. "sonypi using the module options ?\n");
  1566. goto outsonypienable;
  1567. }
  1568. if ((ret = pci_enable_device(meye.mchip_dev))) {
  1569. printk(KERN_ERR "meye: pci_enable_device failed\n");
  1570. goto outenabledev;
  1571. }
  1572. mchip_adr = pci_resource_start(meye.mchip_dev,0);
  1573. if (!mchip_adr) {
  1574. printk(KERN_ERR "meye: mchip has no device base address\n");
  1575. goto outregions;
  1576. }
  1577. if (!request_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1578. pci_resource_len(meye.mchip_dev, 0),
  1579. "meye")) {
  1580. printk(KERN_ERR "meye: request_mem_region failed\n");
  1581. goto outregions;
  1582. }
  1583. meye.mchip_mmregs = ioremap(mchip_adr, MCHIP_MM_REGS);
  1584. if (!meye.mchip_mmregs) {
  1585. printk(KERN_ERR "meye: ioremap failed\n");
  1586. goto outremap;
  1587. }
  1588. meye.mchip_irq = pcidev->irq;
  1589. if (request_irq(meye.mchip_irq, meye_irq,
  1590. IRQF_DISABLED | IRQF_SHARED, "meye", meye_irq)) {
  1591. printk(KERN_ERR "meye: request_irq failed\n");
  1592. goto outreqirq;
  1593. }
  1594. pci_write_config_byte(meye.mchip_dev, PCI_CACHE_LINE_SIZE, 8);
  1595. pci_write_config_byte(meye.mchip_dev, PCI_LATENCY_TIMER, 64);
  1596. pci_set_master(meye.mchip_dev);
  1597. /* Ask the camera to perform a soft reset. */
  1598. pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
  1599. mchip_delay(MCHIP_HIC_CMD, 0);
  1600. mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
  1601. msleep(1);
  1602. mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
  1603. msleep(1);
  1604. mchip_set(MCHIP_MM_PCI_MODE, 5);
  1605. msleep(1);
  1606. mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
  1607. if (video_register_device(meye.video_dev, VFL_TYPE_GRABBER,
  1608. video_nr) < 0) {
  1609. printk(KERN_ERR "meye: video_register_device failed\n");
  1610. goto outvideoreg;
  1611. }
  1612. mutex_init(&meye.lock);
  1613. init_waitqueue_head(&meye.proc_list);
  1614. meye.picture.depth = 16;
  1615. meye.picture.palette = VIDEO_PALETTE_YUV422;
  1616. meye.picture.brightness = 32 << 10;
  1617. meye.picture.hue = 32 << 10;
  1618. meye.picture.colour = 32 << 10;
  1619. meye.picture.contrast = 32 << 10;
  1620. meye.picture.whiteness = 0;
  1621. meye.params.subsample = 0;
  1622. meye.params.quality = 8;
  1623. meye.params.sharpness = 32;
  1624. meye.params.agc = 48;
  1625. meye.params.picture = 0;
  1626. meye.params.framerate = 0;
  1627. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERABRIGHTNESS, 32);
  1628. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAHUE, 32);
  1629. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERACOLOR, 32);
  1630. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERACONTRAST, 32);
  1631. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERASHARPNESS, 32);
  1632. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAPICTURE, 0);
  1633. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAAGC, 48);
  1634. printk(KERN_INFO "meye: Motion Eye Camera Driver v%s.\n",
  1635. MEYE_DRIVER_VERSION);
  1636. printk(KERN_INFO "meye: mchip KL5A72002 rev. %d, base %lx, irq %d\n",
  1637. meye.mchip_dev->revision, mchip_adr, meye.mchip_irq);
  1638. return 0;
  1639. outvideoreg:
  1640. free_irq(meye.mchip_irq, meye_irq);
  1641. outreqirq:
  1642. iounmap(meye.mchip_mmregs);
  1643. outremap:
  1644. release_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1645. pci_resource_len(meye.mchip_dev, 0));
  1646. outregions:
  1647. pci_disable_device(meye.mchip_dev);
  1648. outenabledev:
  1649. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
  1650. outsonypienable:
  1651. kfifo_free(meye.doneq);
  1652. outkfifoalloc2:
  1653. kfifo_free(meye.grabq);
  1654. outkfifoalloc1:
  1655. vfree(meye.grab_temp);
  1656. outvmalloc:
  1657. video_device_release(meye.video_dev);
  1658. outnotdev:
  1659. return ret;
  1660. }
  1661. static void __devexit meye_remove(struct pci_dev *pcidev)
  1662. {
  1663. video_unregister_device(meye.video_dev);
  1664. mchip_hic_stop();
  1665. mchip_dma_free();
  1666. /* disable interrupts */
  1667. mchip_set(MCHIP_MM_INTA, 0x0);
  1668. free_irq(meye.mchip_irq, meye_irq);
  1669. iounmap(meye.mchip_mmregs);
  1670. release_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1671. pci_resource_len(meye.mchip_dev, 0));
  1672. pci_disable_device(meye.mchip_dev);
  1673. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
  1674. kfifo_free(meye.doneq);
  1675. kfifo_free(meye.grabq);
  1676. vfree(meye.grab_temp);
  1677. if (meye.grab_fbuffer) {
  1678. rvfree(meye.grab_fbuffer, gbuffers*gbufsize);
  1679. meye.grab_fbuffer = NULL;
  1680. }
  1681. printk(KERN_INFO "meye: removed\n");
  1682. }
  1683. static struct pci_device_id meye_pci_tbl[] = {
  1684. { PCI_VENDOR_ID_KAWASAKI, PCI_DEVICE_ID_MCHIP_KL5A72002,
  1685. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  1686. { }
  1687. };
  1688. MODULE_DEVICE_TABLE(pci, meye_pci_tbl);
  1689. static struct pci_driver meye_driver = {
  1690. .name = "meye",
  1691. .id_table = meye_pci_tbl,
  1692. .probe = meye_probe,
  1693. .remove = __devexit_p(meye_remove),
  1694. #ifdef CONFIG_PM
  1695. .suspend = meye_suspend,
  1696. .resume = meye_resume,
  1697. #endif
  1698. };
  1699. static int __init meye_init(void)
  1700. {
  1701. gbuffers = max(2, min((int)gbuffers, MEYE_MAX_BUFNBRS));
  1702. if (gbufsize < 0 || gbufsize > MEYE_MAX_BUFSIZE)
  1703. gbufsize = MEYE_MAX_BUFSIZE;
  1704. gbufsize = PAGE_ALIGN(gbufsize);
  1705. printk(KERN_INFO "meye: using %d buffers with %dk (%dk total) "
  1706. "for capture\n",
  1707. gbuffers,
  1708. gbufsize / 1024, gbuffers * gbufsize / 1024);
  1709. return pci_register_driver(&meye_driver);
  1710. }
  1711. static void __exit meye_exit(void)
  1712. {
  1713. pci_unregister_driver(&meye_driver);
  1714. }
  1715. module_init(meye_init);
  1716. module_exit(meye_exit);