meye.c 52 KB

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