meye.c 51 KB

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