meye.c 52 KB

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