meye.c 51 KB

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