meye.c 51 KB

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