meye.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  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/gfp.h>
  34. #include <linux/videodev2.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-fh.h>
  39. #include <media/v4l2-event.h>
  40. #include <linux/uaccess.h>
  41. #include <asm/io.h>
  42. #include <linux/delay.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/vmalloc.h>
  45. #include <linux/dma-mapping.h>
  46. #include "meye.h"
  47. #include <linux/meye.h>
  48. MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
  49. MODULE_DESCRIPTION("v4l2 driver for the MotionEye camera");
  50. MODULE_LICENSE("GPL");
  51. MODULE_VERSION(MEYE_DRIVER_VERSION);
  52. /* number of grab buffers */
  53. static unsigned int gbuffers = 2;
  54. module_param(gbuffers, int, 0444);
  55. MODULE_PARM_DESC(gbuffers, "number of capture buffers, default is 2 (32 max)");
  56. /* size of a grab buffer */
  57. static unsigned int gbufsize = MEYE_MAX_BUFSIZE;
  58. module_param(gbufsize, int, 0444);
  59. MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 614400 (will be rounded up to a page multiple)");
  60. /* /dev/videoX registration number */
  61. static int video_nr = -1;
  62. module_param(video_nr, int, 0444);
  63. MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
  64. /* driver structure - only one possible */
  65. static struct meye meye;
  66. /****************************************************************************/
  67. /* Memory allocation routines (stolen from bttv-driver.c) */
  68. /****************************************************************************/
  69. static void *rvmalloc(unsigned long size)
  70. {
  71. void *mem;
  72. unsigned long adr;
  73. size = PAGE_ALIGN(size);
  74. mem = vmalloc_32(size);
  75. if (mem) {
  76. memset(mem, 0, size);
  77. adr = (unsigned long) mem;
  78. while (size > 0) {
  79. SetPageReserved(vmalloc_to_page((void *)adr));
  80. adr += PAGE_SIZE;
  81. size -= PAGE_SIZE;
  82. }
  83. }
  84. return mem;
  85. }
  86. static void rvfree(void * mem, unsigned long size)
  87. {
  88. unsigned long adr;
  89. if (mem) {
  90. adr = (unsigned long) mem;
  91. while ((long) size > 0) {
  92. ClearPageReserved(vmalloc_to_page((void *)adr));
  93. adr += PAGE_SIZE;
  94. size -= PAGE_SIZE;
  95. }
  96. vfree(mem);
  97. }
  98. }
  99. /*
  100. * return a page table pointing to N pages of locked memory
  101. *
  102. * NOTE: The meye device expects DMA addresses on 32 bits, we build
  103. * a table of 1024 entries = 4 bytes * 1024 = 4096 bytes.
  104. */
  105. static int ptable_alloc(void)
  106. {
  107. u32 *pt;
  108. int i;
  109. memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
  110. /* give only 32 bit DMA addresses */
  111. if (dma_set_mask(&meye.mchip_dev->dev, DMA_BIT_MASK(32)))
  112. return -1;
  113. meye.mchip_ptable_toc = dma_alloc_coherent(&meye.mchip_dev->dev,
  114. PAGE_SIZE,
  115. &meye.mchip_dmahandle,
  116. GFP_KERNEL);
  117. if (!meye.mchip_ptable_toc) {
  118. meye.mchip_dmahandle = 0;
  119. return -1;
  120. }
  121. pt = meye.mchip_ptable_toc;
  122. for (i = 0; i < MCHIP_NB_PAGES; i++) {
  123. dma_addr_t dma;
  124. meye.mchip_ptable[i] = dma_alloc_coherent(&meye.mchip_dev->dev,
  125. PAGE_SIZE,
  126. &dma,
  127. GFP_KERNEL);
  128. if (!meye.mchip_ptable[i]) {
  129. int j;
  130. pt = meye.mchip_ptable_toc;
  131. for (j = 0; j < i; ++j) {
  132. dma = (dma_addr_t) *pt;
  133. dma_free_coherent(&meye.mchip_dev->dev,
  134. PAGE_SIZE,
  135. meye.mchip_ptable[j], dma);
  136. pt++;
  137. }
  138. dma_free_coherent(&meye.mchip_dev->dev,
  139. PAGE_SIZE,
  140. meye.mchip_ptable_toc,
  141. meye.mchip_dmahandle);
  142. meye.mchip_ptable_toc = NULL;
  143. meye.mchip_dmahandle = 0;
  144. return -1;
  145. }
  146. *pt = (u32) dma;
  147. pt++;
  148. }
  149. return 0;
  150. }
  151. static void ptable_free(void)
  152. {
  153. u32 *pt;
  154. int i;
  155. pt = meye.mchip_ptable_toc;
  156. for (i = 0; i < MCHIP_NB_PAGES; i++) {
  157. dma_addr_t dma = (dma_addr_t) *pt;
  158. if (meye.mchip_ptable[i])
  159. dma_free_coherent(&meye.mchip_dev->dev,
  160. PAGE_SIZE,
  161. meye.mchip_ptable[i], dma);
  162. pt++;
  163. }
  164. if (meye.mchip_ptable_toc)
  165. dma_free_coherent(&meye.mchip_dev->dev,
  166. PAGE_SIZE,
  167. meye.mchip_ptable_toc,
  168. meye.mchip_dmahandle);
  169. memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
  170. meye.mchip_ptable_toc = NULL;
  171. meye.mchip_dmahandle = 0;
  172. }
  173. /* copy data from ptable into buf */
  174. static void ptable_copy(u8 *buf, int start, int size, int pt_pages)
  175. {
  176. int i;
  177. for (i = 0; i < (size / PAGE_SIZE) * PAGE_SIZE; i += PAGE_SIZE) {
  178. memcpy(buf + i, meye.mchip_ptable[start++], PAGE_SIZE);
  179. if (start >= pt_pages)
  180. start = 0;
  181. }
  182. memcpy(buf + i, meye.mchip_ptable[start], size % PAGE_SIZE);
  183. }
  184. /****************************************************************************/
  185. /* JPEG tables at different qualities to load into the VRJ chip */
  186. /****************************************************************************/
  187. /* return a set of quantisation tables based on a quality from 1 to 10 */
  188. static u16 *jpeg_quantisation_tables(int *length, int quality)
  189. {
  190. static u16 jpeg_tables[][70] = { {
  191. 0xdbff, 0x4300, 0xff00, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  192. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  193. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  194. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  195. 0xffff, 0xffff, 0xffff,
  196. 0xdbff, 0x4300, 0xff01, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  197. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  198. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  199. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  200. 0xffff, 0xffff, 0xffff,
  201. },
  202. {
  203. 0xdbff, 0x4300, 0x5000, 0x3c37, 0x3c46, 0x5032, 0x4146, 0x5a46,
  204. 0x5055, 0x785f, 0x82c8, 0x6e78, 0x786e, 0xaff5, 0x91b9, 0xffc8,
  205. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  206. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  207. 0xffff, 0xffff, 0xffff,
  208. 0xdbff, 0x4300, 0x5501, 0x5a5a, 0x6978, 0xeb78, 0x8282, 0xffeb,
  209. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  210. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  211. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  212. 0xffff, 0xffff, 0xffff,
  213. },
  214. {
  215. 0xdbff, 0x4300, 0x2800, 0x1e1c, 0x1e23, 0x2819, 0x2123, 0x2d23,
  216. 0x282b, 0x3c30, 0x4164, 0x373c, 0x3c37, 0x587b, 0x495d, 0x9164,
  217. 0x9980, 0x8f96, 0x8c80, 0xa08a, 0xe6b4, 0xa0c3, 0xdaaa, 0x8aad,
  218. 0xc88c, 0xcbff, 0xeeda, 0xfff5, 0xffff, 0xc19b, 0xffff, 0xfaff,
  219. 0xe6ff, 0xfffd, 0xfff8,
  220. 0xdbff, 0x4300, 0x2b01, 0x2d2d, 0x353c, 0x763c, 0x4141, 0xf876,
  221. 0x8ca5, 0xf8a5, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
  222. 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
  223. 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
  224. 0xf8f8, 0xf8f8, 0xfff8,
  225. },
  226. {
  227. 0xdbff, 0x4300, 0x1b00, 0x1412, 0x1417, 0x1b11, 0x1617, 0x1e17,
  228. 0x1b1c, 0x2820, 0x2b42, 0x2528, 0x2825, 0x3a51, 0x303d, 0x6042,
  229. 0x6555, 0x5f64, 0x5d55, 0x6a5b, 0x9978, 0x6a81, 0x9071, 0x5b73,
  230. 0x855d, 0x86b5, 0x9e90, 0xaba3, 0xabad, 0x8067, 0xc9bc, 0xa6ba,
  231. 0x99c7, 0xaba8, 0xffa4,
  232. 0xdbff, 0x4300, 0x1c01, 0x1e1e, 0x2328, 0x4e28, 0x2b2b, 0xa44e,
  233. 0x5d6e, 0xa46e, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
  234. 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
  235. 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
  236. 0xa4a4, 0xa4a4, 0xffa4,
  237. },
  238. {
  239. 0xdbff, 0x4300, 0x1400, 0x0f0e, 0x0f12, 0x140d, 0x1012, 0x1712,
  240. 0x1415, 0x1e18, 0x2132, 0x1c1e, 0x1e1c, 0x2c3d, 0x242e, 0x4932,
  241. 0x4c40, 0x474b, 0x4640, 0x5045, 0x735a, 0x5062, 0x6d55, 0x4556,
  242. 0x6446, 0x6588, 0x776d, 0x817b, 0x8182, 0x604e, 0x978d, 0x7d8c,
  243. 0x7396, 0x817e, 0xff7c,
  244. 0xdbff, 0x4300, 0x1501, 0x1717, 0x1a1e, 0x3b1e, 0x2121, 0x7c3b,
  245. 0x4653, 0x7c53, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
  246. 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
  247. 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
  248. 0x7c7c, 0x7c7c, 0xff7c,
  249. },
  250. {
  251. 0xdbff, 0x4300, 0x1000, 0x0c0b, 0x0c0e, 0x100a, 0x0d0e, 0x120e,
  252. 0x1011, 0x1813, 0x1a28, 0x1618, 0x1816, 0x2331, 0x1d25, 0x3a28,
  253. 0x3d33, 0x393c, 0x3833, 0x4037, 0x5c48, 0x404e, 0x5744, 0x3745,
  254. 0x5038, 0x516d, 0x5f57, 0x6762, 0x6768, 0x4d3e, 0x7971, 0x6470,
  255. 0x5c78, 0x6765, 0xff63,
  256. 0xdbff, 0x4300, 0x1101, 0x1212, 0x1518, 0x2f18, 0x1a1a, 0x632f,
  257. 0x3842, 0x6342, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
  258. 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
  259. 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
  260. 0x6363, 0x6363, 0xff63,
  261. },
  262. {
  263. 0xdbff, 0x4300, 0x0d00, 0x0a09, 0x0a0b, 0x0d08, 0x0a0b, 0x0e0b,
  264. 0x0d0e, 0x130f, 0x1520, 0x1213, 0x1312, 0x1c27, 0x171e, 0x2e20,
  265. 0x3129, 0x2e30, 0x2d29, 0x332c, 0x4a3a, 0x333e, 0x4636, 0x2c37,
  266. 0x402d, 0x4157, 0x4c46, 0x524e, 0x5253, 0x3e32, 0x615a, 0x505a,
  267. 0x4a60, 0x5251, 0xff4f,
  268. 0xdbff, 0x4300, 0x0e01, 0x0e0e, 0x1113, 0x2613, 0x1515, 0x4f26,
  269. 0x2d35, 0x4f35, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
  270. 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
  271. 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
  272. 0x4f4f, 0x4f4f, 0xff4f,
  273. },
  274. {
  275. 0xdbff, 0x4300, 0x0a00, 0x0707, 0x0708, 0x0a06, 0x0808, 0x0b08,
  276. 0x0a0a, 0x0e0b, 0x1018, 0x0d0e, 0x0e0d, 0x151d, 0x1116, 0x2318,
  277. 0x251f, 0x2224, 0x221f, 0x2621, 0x372b, 0x262f, 0x3429, 0x2129,
  278. 0x3022, 0x3141, 0x3934, 0x3e3b, 0x3e3e, 0x2e25, 0x4944, 0x3c43,
  279. 0x3748, 0x3e3d, 0xff3b,
  280. 0xdbff, 0x4300, 0x0a01, 0x0b0b, 0x0d0e, 0x1c0e, 0x1010, 0x3b1c,
  281. 0x2228, 0x3b28, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
  282. 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
  283. 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
  284. 0x3b3b, 0x3b3b, 0xff3b,
  285. },
  286. {
  287. 0xdbff, 0x4300, 0x0600, 0x0504, 0x0506, 0x0604, 0x0506, 0x0706,
  288. 0x0607, 0x0a08, 0x0a10, 0x090a, 0x0a09, 0x0e14, 0x0c0f, 0x1710,
  289. 0x1814, 0x1718, 0x1614, 0x1a16, 0x251d, 0x1a1f, 0x231b, 0x161c,
  290. 0x2016, 0x202c, 0x2623, 0x2927, 0x292a, 0x1f19, 0x302d, 0x282d,
  291. 0x2530, 0x2928, 0xff28,
  292. 0xdbff, 0x4300, 0x0701, 0x0707, 0x080a, 0x130a, 0x0a0a, 0x2813,
  293. 0x161a, 0x281a, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
  294. 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
  295. 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
  296. 0x2828, 0x2828, 0xff28,
  297. },
  298. {
  299. 0xdbff, 0x4300, 0x0300, 0x0202, 0x0203, 0x0302, 0x0303, 0x0403,
  300. 0x0303, 0x0504, 0x0508, 0x0405, 0x0504, 0x070a, 0x0607, 0x0c08,
  301. 0x0c0a, 0x0b0c, 0x0b0a, 0x0d0b, 0x120e, 0x0d10, 0x110e, 0x0b0e,
  302. 0x100b, 0x1016, 0x1311, 0x1514, 0x1515, 0x0f0c, 0x1817, 0x1416,
  303. 0x1218, 0x1514, 0xff14,
  304. 0xdbff, 0x4300, 0x0301, 0x0404, 0x0405, 0x0905, 0x0505, 0x1409,
  305. 0x0b0d, 0x140d, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
  306. 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
  307. 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
  308. 0x1414, 0x1414, 0xff14,
  309. },
  310. {
  311. 0xdbff, 0x4300, 0x0100, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  312. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  313. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  314. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  315. 0x0101, 0x0101, 0xff01,
  316. 0xdbff, 0x4300, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  317. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  318. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  319. 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
  320. 0x0101, 0x0101, 0xff01,
  321. } };
  322. if (quality < 0 || quality > 10) {
  323. printk(KERN_WARNING
  324. "meye: invalid quality level %d - using 8\n", quality);
  325. quality = 8;
  326. }
  327. *length = ARRAY_SIZE(jpeg_tables[quality]);
  328. return jpeg_tables[quality];
  329. }
  330. /* return a generic set of huffman tables */
  331. static u16 *jpeg_huffman_tables(int *length)
  332. {
  333. static u16 tables[] = {
  334. 0xC4FF, 0xB500, 0x0010, 0x0102, 0x0303, 0x0402, 0x0503, 0x0405,
  335. 0x0004, 0x0100, 0x017D, 0x0302, 0x0400, 0x0511, 0x2112, 0x4131,
  336. 0x1306, 0x6151, 0x2207, 0x1471, 0x8132, 0xA191, 0x2308, 0xB142,
  337. 0x15C1, 0xD152, 0x24F0, 0x6233, 0x8272, 0x0A09, 0x1716, 0x1918,
  338. 0x251A, 0x2726, 0x2928, 0x342A, 0x3635, 0x3837, 0x3A39, 0x4443,
  339. 0x4645, 0x4847, 0x4A49, 0x5453, 0x5655, 0x5857, 0x5A59, 0x6463,
  340. 0x6665, 0x6867, 0x6A69, 0x7473, 0x7675, 0x7877, 0x7A79, 0x8483,
  341. 0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998, 0xA29A,
  342. 0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6, 0xB9B8,
  343. 0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4, 0xD7D6,
  344. 0xD9D8, 0xE1DA, 0xE3E2, 0xE5E4, 0xE7E6, 0xE9E8, 0xF1EA, 0xF3F2,
  345. 0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
  346. 0xC4FF, 0xB500, 0x0011, 0x0102, 0x0402, 0x0304, 0x0704, 0x0405,
  347. 0x0004, 0x0201, 0x0077, 0x0201, 0x1103, 0x0504, 0x3121, 0x1206,
  348. 0x5141, 0x6107, 0x1371, 0x3222, 0x0881, 0x4214, 0xA191, 0xC1B1,
  349. 0x2309, 0x5233, 0x15F0, 0x7262, 0x0AD1, 0x2416, 0xE134, 0xF125,
  350. 0x1817, 0x1A19, 0x2726, 0x2928, 0x352A, 0x3736, 0x3938, 0x433A,
  351. 0x4544, 0x4746, 0x4948, 0x534A, 0x5554, 0x5756, 0x5958, 0x635A,
  352. 0x6564, 0x6766, 0x6968, 0x736A, 0x7574, 0x7776, 0x7978, 0x827A,
  353. 0x8483, 0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998,
  354. 0xA29A, 0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6,
  355. 0xB9B8, 0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4,
  356. 0xD7D6, 0xD9D8, 0xE2DA, 0xE4E3, 0xE6E5, 0xE8E7, 0xEAE9, 0xF3F2,
  357. 0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
  358. 0xC4FF, 0x1F00, 0x0000, 0x0501, 0x0101, 0x0101, 0x0101, 0x0000,
  359. 0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
  360. 0xFF0B,
  361. 0xC4FF, 0x1F00, 0x0001, 0x0103, 0x0101, 0x0101, 0x0101, 0x0101,
  362. 0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
  363. 0xFF0B
  364. };
  365. *length = ARRAY_SIZE(tables);
  366. return tables;
  367. }
  368. /****************************************************************************/
  369. /* MCHIP low-level functions */
  370. /****************************************************************************/
  371. /* returns the horizontal capture size */
  372. static inline int mchip_hsize(void)
  373. {
  374. return meye.params.subsample ? 320 : 640;
  375. }
  376. /* returns the vertical capture size */
  377. static inline int mchip_vsize(void)
  378. {
  379. return meye.params.subsample ? 240 : 480;
  380. }
  381. /* waits for a register to be available */
  382. static void mchip_sync(int reg)
  383. {
  384. u32 status;
  385. int i;
  386. if (reg == MCHIP_MM_FIFO_DATA) {
  387. for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
  388. status = readl(meye.mchip_mmregs +
  389. MCHIP_MM_FIFO_STATUS);
  390. if (!(status & MCHIP_MM_FIFO_WAIT)) {
  391. printk(KERN_WARNING "meye: fifo not ready\n");
  392. return;
  393. }
  394. if (status & MCHIP_MM_FIFO_READY)
  395. return;
  396. udelay(1);
  397. }
  398. } else if (reg > 0x80) {
  399. u32 mask = (reg < 0x100) ? MCHIP_HIC_STATUS_MCC_RDY
  400. : MCHIP_HIC_STATUS_VRJ_RDY;
  401. for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
  402. status = readl(meye.mchip_mmregs + MCHIP_HIC_STATUS);
  403. if (status & mask)
  404. return;
  405. udelay(1);
  406. }
  407. } else
  408. return;
  409. printk(KERN_WARNING
  410. "meye: mchip_sync() timeout on reg 0x%x status=0x%x\n",
  411. reg, status);
  412. }
  413. /* sets a value into the register */
  414. static inline void mchip_set(int reg, u32 v)
  415. {
  416. mchip_sync(reg);
  417. writel(v, meye.mchip_mmregs + reg);
  418. }
  419. /* get the register value */
  420. static inline u32 mchip_read(int reg)
  421. {
  422. mchip_sync(reg);
  423. return readl(meye.mchip_mmregs + reg);
  424. }
  425. /* wait for a register to become a particular value */
  426. static inline int mchip_delay(u32 reg, u32 v)
  427. {
  428. int n = 10;
  429. while (--n && mchip_read(reg) != v)
  430. udelay(1);
  431. return n;
  432. }
  433. /* setup subsampling */
  434. static void mchip_subsample(void)
  435. {
  436. mchip_set(MCHIP_MCC_R_SAMPLING, meye.params.subsample);
  437. mchip_set(MCHIP_MCC_R_XRANGE, mchip_hsize());
  438. mchip_set(MCHIP_MCC_R_YRANGE, mchip_vsize());
  439. mchip_set(MCHIP_MCC_B_XRANGE, mchip_hsize());
  440. mchip_set(MCHIP_MCC_B_YRANGE, mchip_vsize());
  441. mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
  442. }
  443. /* set the framerate into the mchip */
  444. static void mchip_set_framerate(void)
  445. {
  446. mchip_set(MCHIP_HIC_S_RATE, meye.params.framerate);
  447. }
  448. /* load some huffman and quantisation tables into the VRJ chip ready
  449. for JPEG compression */
  450. static void mchip_load_tables(void)
  451. {
  452. int i;
  453. int length;
  454. u16 *tables;
  455. tables = jpeg_huffman_tables(&length);
  456. for (i = 0; i < length; i++)
  457. writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
  458. tables = jpeg_quantisation_tables(&length, meye.params.quality);
  459. for (i = 0; i < length; i++)
  460. writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
  461. }
  462. /* setup the VRJ parameters in the chip */
  463. static void mchip_vrj_setup(u8 mode)
  464. {
  465. mchip_set(MCHIP_VRJ_BUS_MODE, 5);
  466. mchip_set(MCHIP_VRJ_SIGNAL_ACTIVE_LEVEL, 0x1f);
  467. mchip_set(MCHIP_VRJ_PDAT_USE, 1);
  468. mchip_set(MCHIP_VRJ_IRQ_FLAG, 0xa0);
  469. mchip_set(MCHIP_VRJ_MODE_SPECIFY, mode);
  470. mchip_set(MCHIP_VRJ_NUM_LINES, mchip_vsize());
  471. mchip_set(MCHIP_VRJ_NUM_PIXELS, mchip_hsize());
  472. mchip_set(MCHIP_VRJ_NUM_COMPONENTS, 0x1b);
  473. mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_LO, 0xFFFF);
  474. mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_HI, 0xFFFF);
  475. mchip_set(MCHIP_VRJ_COMP_DATA_FORMAT, 0xC);
  476. mchip_set(MCHIP_VRJ_RESTART_INTERVAL, 0);
  477. mchip_set(MCHIP_VRJ_SOF1, 0x601);
  478. mchip_set(MCHIP_VRJ_SOF2, 0x1502);
  479. mchip_set(MCHIP_VRJ_SOF3, 0x1503);
  480. mchip_set(MCHIP_VRJ_SOF4, 0x1596);
  481. mchip_set(MCHIP_VRJ_SOS, 0x0ed0);
  482. mchip_load_tables();
  483. }
  484. /* sets the DMA parameters into the chip */
  485. static void mchip_dma_setup(dma_addr_t dma_addr)
  486. {
  487. int i;
  488. mchip_set(MCHIP_MM_PT_ADDR, (u32)dma_addr);
  489. for (i = 0; i < 4; i++)
  490. mchip_set(MCHIP_MM_FIR(i), 0);
  491. meye.mchip_fnum = 0;
  492. }
  493. /* setup for DMA transfers - also zeros the framebuffer */
  494. static int mchip_dma_alloc(void)
  495. {
  496. if (!meye.mchip_dmahandle)
  497. if (ptable_alloc())
  498. return -1;
  499. return 0;
  500. }
  501. /* frees the DMA buffer */
  502. static void mchip_dma_free(void)
  503. {
  504. if (meye.mchip_dmahandle) {
  505. mchip_dma_setup(0);
  506. ptable_free();
  507. }
  508. }
  509. /* stop any existing HIC action and wait for any dma to complete then
  510. reset the dma engine */
  511. static void mchip_hic_stop(void)
  512. {
  513. int i, j;
  514. meye.mchip_mode = MCHIP_HIC_MODE_NOOP;
  515. if (!(mchip_read(MCHIP_HIC_STATUS) & MCHIP_HIC_STATUS_BUSY))
  516. return;
  517. for (i = 0; i < 20; ++i) {
  518. mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_STOP);
  519. mchip_delay(MCHIP_HIC_CMD, 0);
  520. for (j = 0; j < 100; ++j) {
  521. if (mchip_delay(MCHIP_HIC_STATUS,
  522. MCHIP_HIC_STATUS_IDLE))
  523. return;
  524. msleep(1);
  525. }
  526. printk(KERN_ERR "meye: need to reset HIC!\n");
  527. mchip_set(MCHIP_HIC_CTL, MCHIP_HIC_CTL_SOFT_RESET);
  528. msleep(250);
  529. }
  530. printk(KERN_ERR "meye: resetting HIC hanged!\n");
  531. }
  532. /****************************************************************************/
  533. /* MCHIP frame processing functions */
  534. /****************************************************************************/
  535. /* get the next ready frame from the dma engine */
  536. static u32 mchip_get_frame(void)
  537. {
  538. return mchip_read(MCHIP_MM_FIR(meye.mchip_fnum));
  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. v4l2_get_timestamp(&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. v4l2_get_timestamp(&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 v4l2_fh_open(file);
  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 v4l2_fh_release(file);
  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->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  893. V4L2_CAP_STREAMING;
  894. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  895. return 0;
  896. }
  897. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  898. {
  899. if (i->index != 0)
  900. return -EINVAL;
  901. strcpy(i->name, "Camera");
  902. i->type = V4L2_INPUT_TYPE_CAMERA;
  903. return 0;
  904. }
  905. static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
  906. {
  907. *i = 0;
  908. return 0;
  909. }
  910. static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
  911. {
  912. if (i != 0)
  913. return -EINVAL;
  914. return 0;
  915. }
  916. static int meye_s_ctrl(struct v4l2_ctrl *ctrl)
  917. {
  918. mutex_lock(&meye.lock);
  919. switch (ctrl->id) {
  920. case V4L2_CID_BRIGHTNESS:
  921. sony_pic_camera_command(
  922. SONY_PIC_COMMAND_SETCAMERABRIGHTNESS, ctrl->val);
  923. meye.brightness = ctrl->val << 10;
  924. break;
  925. case V4L2_CID_HUE:
  926. sony_pic_camera_command(
  927. SONY_PIC_COMMAND_SETCAMERAHUE, ctrl->val);
  928. meye.hue = ctrl->val << 10;
  929. break;
  930. case V4L2_CID_CONTRAST:
  931. sony_pic_camera_command(
  932. SONY_PIC_COMMAND_SETCAMERACONTRAST, ctrl->val);
  933. meye.contrast = ctrl->val << 10;
  934. break;
  935. case V4L2_CID_SATURATION:
  936. sony_pic_camera_command(
  937. SONY_PIC_COMMAND_SETCAMERACOLOR, ctrl->val);
  938. meye.colour = ctrl->val << 10;
  939. break;
  940. case V4L2_CID_MEYE_AGC:
  941. sony_pic_camera_command(
  942. SONY_PIC_COMMAND_SETCAMERAAGC, ctrl->val);
  943. meye.params.agc = ctrl->val;
  944. break;
  945. case V4L2_CID_SHARPNESS:
  946. sony_pic_camera_command(
  947. SONY_PIC_COMMAND_SETCAMERASHARPNESS, ctrl->val);
  948. meye.params.sharpness = ctrl->val;
  949. break;
  950. case V4L2_CID_MEYE_PICTURE:
  951. sony_pic_camera_command(
  952. SONY_PIC_COMMAND_SETCAMERAPICTURE, ctrl->val);
  953. meye.params.picture = ctrl->val;
  954. break;
  955. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  956. meye.params.quality = ctrl->val;
  957. break;
  958. case V4L2_CID_MEYE_FRAMERATE:
  959. meye.params.framerate = ctrl->val;
  960. break;
  961. default:
  962. mutex_unlock(&meye.lock);
  963. return -EINVAL;
  964. }
  965. mutex_unlock(&meye.lock);
  966. return 0;
  967. }
  968. static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
  969. struct v4l2_fmtdesc *f)
  970. {
  971. if (f->index > 1)
  972. return -EINVAL;
  973. if (f->index == 0) {
  974. /* standard YUV 422 capture */
  975. f->flags = 0;
  976. strcpy(f->description, "YUV422");
  977. f->pixelformat = V4L2_PIX_FMT_YUYV;
  978. } else {
  979. /* compressed MJPEG capture */
  980. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  981. strcpy(f->description, "MJPEG");
  982. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  983. }
  984. return 0;
  985. }
  986. static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
  987. struct v4l2_format *f)
  988. {
  989. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
  990. f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  991. return -EINVAL;
  992. if (f->fmt.pix.field != V4L2_FIELD_ANY &&
  993. f->fmt.pix.field != V4L2_FIELD_NONE)
  994. return -EINVAL;
  995. f->fmt.pix.field = V4L2_FIELD_NONE;
  996. if (f->fmt.pix.width <= 320) {
  997. f->fmt.pix.width = 320;
  998. f->fmt.pix.height = 240;
  999. } else {
  1000. f->fmt.pix.width = 640;
  1001. f->fmt.pix.height = 480;
  1002. }
  1003. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1004. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1005. f->fmt.pix.bytesperline;
  1006. f->fmt.pix.colorspace = 0;
  1007. return 0;
  1008. }
  1009. static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
  1010. struct v4l2_format *f)
  1011. {
  1012. switch (meye.mchip_mode) {
  1013. case MCHIP_HIC_MODE_CONT_OUT:
  1014. default:
  1015. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  1016. break;
  1017. case MCHIP_HIC_MODE_CONT_COMP:
  1018. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
  1019. break;
  1020. }
  1021. f->fmt.pix.field = V4L2_FIELD_NONE;
  1022. f->fmt.pix.width = mchip_hsize();
  1023. f->fmt.pix.height = mchip_vsize();
  1024. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1025. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1026. f->fmt.pix.bytesperline;
  1027. return 0;
  1028. }
  1029. static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
  1030. struct v4l2_format *f)
  1031. {
  1032. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
  1033. f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1034. return -EINVAL;
  1035. if (f->fmt.pix.field != V4L2_FIELD_ANY &&
  1036. f->fmt.pix.field != V4L2_FIELD_NONE)
  1037. return -EINVAL;
  1038. f->fmt.pix.field = V4L2_FIELD_NONE;
  1039. mutex_lock(&meye.lock);
  1040. if (f->fmt.pix.width <= 320) {
  1041. f->fmt.pix.width = 320;
  1042. f->fmt.pix.height = 240;
  1043. meye.params.subsample = 1;
  1044. } else {
  1045. f->fmt.pix.width = 640;
  1046. f->fmt.pix.height = 480;
  1047. meye.params.subsample = 0;
  1048. }
  1049. switch (f->fmt.pix.pixelformat) {
  1050. case V4L2_PIX_FMT_YUYV:
  1051. meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
  1052. break;
  1053. case V4L2_PIX_FMT_MJPEG:
  1054. meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
  1055. break;
  1056. }
  1057. mutex_unlock(&meye.lock);
  1058. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1059. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1060. f->fmt.pix.bytesperline;
  1061. f->fmt.pix.colorspace = 0;
  1062. return 0;
  1063. }
  1064. static int vidioc_reqbufs(struct file *file, void *fh,
  1065. struct v4l2_requestbuffers *req)
  1066. {
  1067. int i;
  1068. if (req->memory != V4L2_MEMORY_MMAP)
  1069. return -EINVAL;
  1070. if (meye.grab_fbuffer && req->count == gbuffers) {
  1071. /* already allocated, no modifications */
  1072. return 0;
  1073. }
  1074. mutex_lock(&meye.lock);
  1075. if (meye.grab_fbuffer) {
  1076. for (i = 0; i < gbuffers; i++)
  1077. if (meye.vma_use_count[i]) {
  1078. mutex_unlock(&meye.lock);
  1079. return -EINVAL;
  1080. }
  1081. rvfree(meye.grab_fbuffer, gbuffers * gbufsize);
  1082. meye.grab_fbuffer = NULL;
  1083. }
  1084. gbuffers = max(2, min((int)req->count, MEYE_MAX_BUFNBRS));
  1085. req->count = gbuffers;
  1086. meye.grab_fbuffer = rvmalloc(gbuffers * gbufsize);
  1087. if (!meye.grab_fbuffer) {
  1088. printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
  1089. mutex_unlock(&meye.lock);
  1090. return -ENOMEM;
  1091. }
  1092. for (i = 0; i < gbuffers; i++)
  1093. meye.vma_use_count[i] = 0;
  1094. mutex_unlock(&meye.lock);
  1095. return 0;
  1096. }
  1097. static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1098. {
  1099. unsigned int index = buf->index;
  1100. if (index >= gbuffers)
  1101. return -EINVAL;
  1102. buf->bytesused = meye.grab_buffer[index].size;
  1103. buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1104. if (meye.grab_buffer[index].state == MEYE_BUF_USING)
  1105. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1106. if (meye.grab_buffer[index].state == MEYE_BUF_DONE)
  1107. buf->flags |= V4L2_BUF_FLAG_DONE;
  1108. buf->field = V4L2_FIELD_NONE;
  1109. buf->timestamp = meye.grab_buffer[index].timestamp;
  1110. buf->sequence = meye.grab_buffer[index].sequence;
  1111. buf->memory = V4L2_MEMORY_MMAP;
  1112. buf->m.offset = index * gbufsize;
  1113. buf->length = gbufsize;
  1114. return 0;
  1115. }
  1116. static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1117. {
  1118. if (buf->memory != V4L2_MEMORY_MMAP)
  1119. return -EINVAL;
  1120. if (buf->index >= gbuffers)
  1121. return -EINVAL;
  1122. if (meye.grab_buffer[buf->index].state != MEYE_BUF_UNUSED)
  1123. return -EINVAL;
  1124. mutex_lock(&meye.lock);
  1125. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1126. buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1127. meye.grab_buffer[buf->index].state = MEYE_BUF_USING;
  1128. kfifo_in_locked(&meye.grabq, (unsigned char *)&buf->index,
  1129. sizeof(int), &meye.grabq_lock);
  1130. mutex_unlock(&meye.lock);
  1131. return 0;
  1132. }
  1133. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1134. {
  1135. int reqnr;
  1136. if (buf->memory != V4L2_MEMORY_MMAP)
  1137. return -EINVAL;
  1138. mutex_lock(&meye.lock);
  1139. if (kfifo_len(&meye.doneq) == 0 && file->f_flags & O_NONBLOCK) {
  1140. mutex_unlock(&meye.lock);
  1141. return -EAGAIN;
  1142. }
  1143. if (wait_event_interruptible(meye.proc_list,
  1144. kfifo_len(&meye.doneq) != 0) < 0) {
  1145. mutex_unlock(&meye.lock);
  1146. return -EINTR;
  1147. }
  1148. if (!kfifo_out_locked(&meye.doneq, (unsigned char *)&reqnr,
  1149. sizeof(int), &meye.doneq_lock)) {
  1150. mutex_unlock(&meye.lock);
  1151. return -EBUSY;
  1152. }
  1153. if (meye.grab_buffer[reqnr].state != MEYE_BUF_DONE) {
  1154. mutex_unlock(&meye.lock);
  1155. return -EINVAL;
  1156. }
  1157. buf->index = reqnr;
  1158. buf->bytesused = meye.grab_buffer[reqnr].size;
  1159. buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1160. buf->field = V4L2_FIELD_NONE;
  1161. buf->timestamp = meye.grab_buffer[reqnr].timestamp;
  1162. buf->sequence = meye.grab_buffer[reqnr].sequence;
  1163. buf->memory = V4L2_MEMORY_MMAP;
  1164. buf->m.offset = reqnr * gbufsize;
  1165. buf->length = gbufsize;
  1166. meye.grab_buffer[reqnr].state = MEYE_BUF_UNUSED;
  1167. mutex_unlock(&meye.lock);
  1168. return 0;
  1169. }
  1170. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1171. {
  1172. mutex_lock(&meye.lock);
  1173. switch (meye.mchip_mode) {
  1174. case MCHIP_HIC_MODE_CONT_OUT:
  1175. mchip_continuous_start();
  1176. break;
  1177. case MCHIP_HIC_MODE_CONT_COMP:
  1178. mchip_cont_compression_start();
  1179. break;
  1180. default:
  1181. mutex_unlock(&meye.lock);
  1182. return -EINVAL;
  1183. }
  1184. mutex_unlock(&meye.lock);
  1185. return 0;
  1186. }
  1187. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  1188. {
  1189. mutex_lock(&meye.lock);
  1190. mchip_hic_stop();
  1191. kfifo_reset(&meye.grabq);
  1192. kfifo_reset(&meye.doneq);
  1193. for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
  1194. meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
  1195. mutex_unlock(&meye.lock);
  1196. return 0;
  1197. }
  1198. static long vidioc_default(struct file *file, void *fh, bool valid_prio,
  1199. unsigned int cmd, void *arg)
  1200. {
  1201. switch (cmd) {
  1202. case MEYEIOC_G_PARAMS:
  1203. return meyeioc_g_params((struct meye_params *) arg);
  1204. case MEYEIOC_S_PARAMS:
  1205. return meyeioc_s_params((struct meye_params *) arg);
  1206. case MEYEIOC_QBUF_CAPT:
  1207. return meyeioc_qbuf_capt((int *) arg);
  1208. case MEYEIOC_SYNC:
  1209. return meyeioc_sync(file, fh, (int *) arg);
  1210. case MEYEIOC_STILLCAPT:
  1211. return meyeioc_stillcapt();
  1212. case MEYEIOC_STILLJCAPT:
  1213. return meyeioc_stilljcapt((int *) arg);
  1214. default:
  1215. return -ENOTTY;
  1216. }
  1217. }
  1218. static unsigned int meye_poll(struct file *file, poll_table *wait)
  1219. {
  1220. unsigned int res = v4l2_ctrl_poll(file, wait);
  1221. mutex_lock(&meye.lock);
  1222. poll_wait(file, &meye.proc_list, wait);
  1223. if (kfifo_len(&meye.doneq))
  1224. res |= POLLIN | POLLRDNORM;
  1225. mutex_unlock(&meye.lock);
  1226. return res;
  1227. }
  1228. static void meye_vm_open(struct vm_area_struct *vma)
  1229. {
  1230. long idx = (long)vma->vm_private_data;
  1231. meye.vma_use_count[idx]++;
  1232. }
  1233. static void meye_vm_close(struct vm_area_struct *vma)
  1234. {
  1235. long idx = (long)vma->vm_private_data;
  1236. meye.vma_use_count[idx]--;
  1237. }
  1238. static const struct vm_operations_struct meye_vm_ops = {
  1239. .open = meye_vm_open,
  1240. .close = meye_vm_close,
  1241. };
  1242. static int meye_mmap(struct file *file, struct vm_area_struct *vma)
  1243. {
  1244. unsigned long start = vma->vm_start;
  1245. unsigned long size = vma->vm_end - vma->vm_start;
  1246. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1247. unsigned long page, pos;
  1248. mutex_lock(&meye.lock);
  1249. if (size > gbuffers * gbufsize) {
  1250. mutex_unlock(&meye.lock);
  1251. return -EINVAL;
  1252. }
  1253. if (!meye.grab_fbuffer) {
  1254. int i;
  1255. /* lazy allocation */
  1256. meye.grab_fbuffer = rvmalloc(gbuffers*gbufsize);
  1257. if (!meye.grab_fbuffer) {
  1258. printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
  1259. mutex_unlock(&meye.lock);
  1260. return -ENOMEM;
  1261. }
  1262. for (i = 0; i < gbuffers; i++)
  1263. meye.vma_use_count[i] = 0;
  1264. }
  1265. pos = (unsigned long)meye.grab_fbuffer + offset;
  1266. while (size > 0) {
  1267. page = vmalloc_to_pfn((void *)pos);
  1268. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  1269. mutex_unlock(&meye.lock);
  1270. return -EAGAIN;
  1271. }
  1272. start += PAGE_SIZE;
  1273. pos += PAGE_SIZE;
  1274. if (size > PAGE_SIZE)
  1275. size -= PAGE_SIZE;
  1276. else
  1277. size = 0;
  1278. }
  1279. vma->vm_ops = &meye_vm_ops;
  1280. vma->vm_flags &= ~VM_IO; /* not I/O memory */
  1281. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1282. vma->vm_private_data = (void *) (offset / gbufsize);
  1283. meye_vm_open(vma);
  1284. mutex_unlock(&meye.lock);
  1285. return 0;
  1286. }
  1287. static const struct v4l2_file_operations meye_fops = {
  1288. .owner = THIS_MODULE,
  1289. .open = meye_open,
  1290. .release = meye_release,
  1291. .mmap = meye_mmap,
  1292. .unlocked_ioctl = video_ioctl2,
  1293. .poll = meye_poll,
  1294. };
  1295. static const struct v4l2_ioctl_ops meye_ioctl_ops = {
  1296. .vidioc_querycap = vidioc_querycap,
  1297. .vidioc_enum_input = vidioc_enum_input,
  1298. .vidioc_g_input = vidioc_g_input,
  1299. .vidioc_s_input = vidioc_s_input,
  1300. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1301. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1302. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1303. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1304. .vidioc_reqbufs = vidioc_reqbufs,
  1305. .vidioc_querybuf = vidioc_querybuf,
  1306. .vidioc_qbuf = vidioc_qbuf,
  1307. .vidioc_dqbuf = vidioc_dqbuf,
  1308. .vidioc_streamon = vidioc_streamon,
  1309. .vidioc_streamoff = vidioc_streamoff,
  1310. .vidioc_log_status = v4l2_ctrl_log_status,
  1311. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1312. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1313. .vidioc_default = vidioc_default,
  1314. };
  1315. static struct video_device meye_template = {
  1316. .name = "meye",
  1317. .fops = &meye_fops,
  1318. .ioctl_ops = &meye_ioctl_ops,
  1319. .release = video_device_release_empty,
  1320. };
  1321. static const struct v4l2_ctrl_ops meye_ctrl_ops = {
  1322. .s_ctrl = meye_s_ctrl,
  1323. };
  1324. #ifdef CONFIG_PM
  1325. static int meye_suspend(struct pci_dev *pdev, pm_message_t state)
  1326. {
  1327. pci_save_state(pdev);
  1328. meye.pm_mchip_mode = meye.mchip_mode;
  1329. mchip_hic_stop();
  1330. mchip_set(MCHIP_MM_INTA, 0x0);
  1331. return 0;
  1332. }
  1333. static int meye_resume(struct pci_dev *pdev)
  1334. {
  1335. pci_restore_state(pdev);
  1336. pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
  1337. mchip_delay(MCHIP_HIC_CMD, 0);
  1338. mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
  1339. msleep(1);
  1340. mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
  1341. msleep(1);
  1342. mchip_set(MCHIP_MM_PCI_MODE, 5);
  1343. msleep(1);
  1344. mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
  1345. switch (meye.pm_mchip_mode) {
  1346. case MCHIP_HIC_MODE_CONT_OUT:
  1347. mchip_continuous_start();
  1348. break;
  1349. case MCHIP_HIC_MODE_CONT_COMP:
  1350. mchip_cont_compression_start();
  1351. break;
  1352. }
  1353. return 0;
  1354. }
  1355. #endif
  1356. static int meye_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
  1357. {
  1358. static const struct v4l2_ctrl_config ctrl_agc = {
  1359. .id = V4L2_CID_MEYE_AGC,
  1360. .type = V4L2_CTRL_TYPE_INTEGER,
  1361. .ops = &meye_ctrl_ops,
  1362. .name = "AGC",
  1363. .max = 63,
  1364. .step = 1,
  1365. .def = 48,
  1366. .flags = V4L2_CTRL_FLAG_SLIDER,
  1367. };
  1368. static const struct v4l2_ctrl_config ctrl_picture = {
  1369. .id = V4L2_CID_MEYE_PICTURE,
  1370. .type = V4L2_CTRL_TYPE_INTEGER,
  1371. .ops = &meye_ctrl_ops,
  1372. .name = "Picture",
  1373. .max = 63,
  1374. .step = 1,
  1375. };
  1376. static const struct v4l2_ctrl_config ctrl_framerate = {
  1377. .id = V4L2_CID_MEYE_FRAMERATE,
  1378. .type = V4L2_CTRL_TYPE_INTEGER,
  1379. .ops = &meye_ctrl_ops,
  1380. .name = "Framerate",
  1381. .max = 31,
  1382. .step = 1,
  1383. };
  1384. struct v4l2_device *v4l2_dev = &meye.v4l2_dev;
  1385. int ret = -EBUSY;
  1386. unsigned long mchip_adr;
  1387. if (meye.mchip_dev != NULL) {
  1388. printk(KERN_ERR "meye: only one device allowed!\n");
  1389. return ret;
  1390. }
  1391. ret = v4l2_device_register(&pcidev->dev, v4l2_dev);
  1392. if (ret < 0) {
  1393. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  1394. return ret;
  1395. }
  1396. ret = -ENOMEM;
  1397. meye.mchip_dev = pcidev;
  1398. meye.grab_temp = vmalloc(MCHIP_NB_PAGES_MJPEG * PAGE_SIZE);
  1399. if (!meye.grab_temp) {
  1400. v4l2_err(v4l2_dev, "grab buffer allocation failed\n");
  1401. goto outvmalloc;
  1402. }
  1403. spin_lock_init(&meye.grabq_lock);
  1404. if (kfifo_alloc(&meye.grabq, sizeof(int) * MEYE_MAX_BUFNBRS,
  1405. GFP_KERNEL)) {
  1406. v4l2_err(v4l2_dev, "fifo allocation failed\n");
  1407. goto outkfifoalloc1;
  1408. }
  1409. spin_lock_init(&meye.doneq_lock);
  1410. if (kfifo_alloc(&meye.doneq, sizeof(int) * MEYE_MAX_BUFNBRS,
  1411. GFP_KERNEL)) {
  1412. v4l2_err(v4l2_dev, "fifo allocation failed\n");
  1413. goto outkfifoalloc2;
  1414. }
  1415. meye.vdev = meye_template;
  1416. meye.vdev.v4l2_dev = &meye.v4l2_dev;
  1417. ret = -EIO;
  1418. if ((ret = sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 1))) {
  1419. v4l2_err(v4l2_dev, "meye: unable to power on the camera\n");
  1420. v4l2_err(v4l2_dev, "meye: did you enable the camera in sonypi using the module options ?\n");
  1421. goto outsonypienable;
  1422. }
  1423. if ((ret = pci_enable_device(meye.mchip_dev))) {
  1424. v4l2_err(v4l2_dev, "meye: pci_enable_device failed\n");
  1425. goto outenabledev;
  1426. }
  1427. mchip_adr = pci_resource_start(meye.mchip_dev,0);
  1428. if (!mchip_adr) {
  1429. v4l2_err(v4l2_dev, "meye: mchip has no device base address\n");
  1430. goto outregions;
  1431. }
  1432. if (!request_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1433. pci_resource_len(meye.mchip_dev, 0),
  1434. "meye")) {
  1435. v4l2_err(v4l2_dev, "meye: request_mem_region failed\n");
  1436. goto outregions;
  1437. }
  1438. meye.mchip_mmregs = ioremap(mchip_adr, MCHIP_MM_REGS);
  1439. if (!meye.mchip_mmregs) {
  1440. v4l2_err(v4l2_dev, "meye: ioremap failed\n");
  1441. goto outremap;
  1442. }
  1443. meye.mchip_irq = pcidev->irq;
  1444. if (request_irq(meye.mchip_irq, meye_irq,
  1445. IRQF_SHARED, "meye", meye_irq)) {
  1446. v4l2_err(v4l2_dev, "request_irq failed\n");
  1447. goto outreqirq;
  1448. }
  1449. pci_write_config_byte(meye.mchip_dev, PCI_CACHE_LINE_SIZE, 8);
  1450. pci_write_config_byte(meye.mchip_dev, PCI_LATENCY_TIMER, 64);
  1451. pci_set_master(meye.mchip_dev);
  1452. /* Ask the camera to perform a soft reset. */
  1453. pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
  1454. mchip_delay(MCHIP_HIC_CMD, 0);
  1455. mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
  1456. msleep(1);
  1457. mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
  1458. msleep(1);
  1459. mchip_set(MCHIP_MM_PCI_MODE, 5);
  1460. msleep(1);
  1461. mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
  1462. mutex_init(&meye.lock);
  1463. init_waitqueue_head(&meye.proc_list);
  1464. v4l2_ctrl_handler_init(&meye.hdl, 3);
  1465. v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
  1466. V4L2_CID_BRIGHTNESS, 0, 63, 1, 32);
  1467. v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
  1468. V4L2_CID_HUE, 0, 63, 1, 32);
  1469. v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
  1470. V4L2_CID_CONTRAST, 0, 63, 1, 32);
  1471. v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
  1472. V4L2_CID_SATURATION, 0, 63, 1, 32);
  1473. v4l2_ctrl_new_custom(&meye.hdl, &ctrl_agc, NULL);
  1474. v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
  1475. V4L2_CID_SHARPNESS, 0, 63, 1, 32);
  1476. v4l2_ctrl_new_custom(&meye.hdl, &ctrl_picture, NULL);
  1477. v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
  1478. V4L2_CID_JPEG_COMPRESSION_QUALITY, 0, 10, 1, 8);
  1479. v4l2_ctrl_new_custom(&meye.hdl, &ctrl_framerate, NULL);
  1480. if (meye.hdl.error) {
  1481. v4l2_err(v4l2_dev, "couldn't register controls\n");
  1482. goto outvideoreg;
  1483. }
  1484. v4l2_ctrl_handler_setup(&meye.hdl);
  1485. meye.vdev.ctrl_handler = &meye.hdl;
  1486. if (video_register_device(&meye.vdev, VFL_TYPE_GRABBER,
  1487. video_nr) < 0) {
  1488. v4l2_err(v4l2_dev, "video_register_device failed\n");
  1489. goto outvideoreg;
  1490. }
  1491. v4l2_info(v4l2_dev, "Motion Eye Camera Driver v%s.\n",
  1492. MEYE_DRIVER_VERSION);
  1493. v4l2_info(v4l2_dev, "mchip KL5A72002 rev. %d, base %lx, irq %d\n",
  1494. meye.mchip_dev->revision, mchip_adr, meye.mchip_irq);
  1495. return 0;
  1496. outvideoreg:
  1497. v4l2_ctrl_handler_free(&meye.hdl);
  1498. free_irq(meye.mchip_irq, meye_irq);
  1499. outreqirq:
  1500. iounmap(meye.mchip_mmregs);
  1501. outremap:
  1502. release_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1503. pci_resource_len(meye.mchip_dev, 0));
  1504. outregions:
  1505. pci_disable_device(meye.mchip_dev);
  1506. outenabledev:
  1507. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
  1508. outsonypienable:
  1509. kfifo_free(&meye.doneq);
  1510. outkfifoalloc2:
  1511. kfifo_free(&meye.grabq);
  1512. outkfifoalloc1:
  1513. vfree(meye.grab_temp);
  1514. outvmalloc:
  1515. return ret;
  1516. }
  1517. static void meye_remove(struct pci_dev *pcidev)
  1518. {
  1519. video_unregister_device(&meye.vdev);
  1520. mchip_hic_stop();
  1521. mchip_dma_free();
  1522. /* disable interrupts */
  1523. mchip_set(MCHIP_MM_INTA, 0x0);
  1524. free_irq(meye.mchip_irq, meye_irq);
  1525. iounmap(meye.mchip_mmregs);
  1526. release_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1527. pci_resource_len(meye.mchip_dev, 0));
  1528. pci_disable_device(meye.mchip_dev);
  1529. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
  1530. kfifo_free(&meye.doneq);
  1531. kfifo_free(&meye.grabq);
  1532. vfree(meye.grab_temp);
  1533. if (meye.grab_fbuffer) {
  1534. rvfree(meye.grab_fbuffer, gbuffers*gbufsize);
  1535. meye.grab_fbuffer = NULL;
  1536. }
  1537. printk(KERN_INFO "meye: removed\n");
  1538. }
  1539. static struct pci_device_id meye_pci_tbl[] = {
  1540. { PCI_VDEVICE(KAWASAKI, PCI_DEVICE_ID_MCHIP_KL5A72002), 0 },
  1541. { }
  1542. };
  1543. MODULE_DEVICE_TABLE(pci, meye_pci_tbl);
  1544. static struct pci_driver meye_driver = {
  1545. .name = "meye",
  1546. .id_table = meye_pci_tbl,
  1547. .probe = meye_probe,
  1548. .remove = meye_remove,
  1549. #ifdef CONFIG_PM
  1550. .suspend = meye_suspend,
  1551. .resume = meye_resume,
  1552. #endif
  1553. };
  1554. static int __init meye_init(void)
  1555. {
  1556. gbuffers = max(2, min((int)gbuffers, MEYE_MAX_BUFNBRS));
  1557. if (gbufsize > MEYE_MAX_BUFSIZE)
  1558. gbufsize = MEYE_MAX_BUFSIZE;
  1559. gbufsize = PAGE_ALIGN(gbufsize);
  1560. printk(KERN_INFO "meye: using %d buffers with %dk (%dk total) for capture\n",
  1561. gbuffers,
  1562. gbufsize / 1024, gbuffers * gbufsize / 1024);
  1563. return pci_register_driver(&meye_driver);
  1564. }
  1565. static void __exit meye_exit(void)
  1566. {
  1567. pci_unregister_driver(&meye_driver);
  1568. }
  1569. module_init(meye_init);
  1570. module_exit(meye_exit);