meye.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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_BIT_MASK(32)))
  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 file *file)
  745. {
  746. int i;
  747. if (test_and_set_bit(0, &meye.in_use))
  748. return -EBUSY;
  749. mchip_hic_stop();
  750. if (mchip_dma_alloc()) {
  751. printk(KERN_ERR "meye: mchip framebuffer allocation failed\n");
  752. clear_bit(0, &meye.in_use);
  753. return -ENOBUFS;
  754. }
  755. for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
  756. meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
  757. kfifo_reset(meye.grabq);
  758. kfifo_reset(meye.doneq);
  759. return 0;
  760. }
  761. static int meye_release(struct file *file)
  762. {
  763. mchip_hic_stop();
  764. mchip_dma_free();
  765. clear_bit(0, &meye.in_use);
  766. return 0;
  767. }
  768. static int meyeioc_g_params(struct meye_params *p)
  769. {
  770. *p = meye.params;
  771. return 0;
  772. }
  773. static int meyeioc_s_params(struct meye_params *jp)
  774. {
  775. if (jp->subsample > 1)
  776. return -EINVAL;
  777. if (jp->quality > 10)
  778. return -EINVAL;
  779. if (jp->sharpness > 63 || jp->agc > 63 || jp->picture > 63)
  780. return -EINVAL;
  781. if (jp->framerate > 31)
  782. return -EINVAL;
  783. mutex_lock(&meye.lock);
  784. if (meye.params.subsample != jp->subsample ||
  785. meye.params.quality != jp->quality)
  786. mchip_hic_stop(); /* need restart */
  787. meye.params = *jp;
  788. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERASHARPNESS,
  789. meye.params.sharpness);
  790. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAAGC,
  791. meye.params.agc);
  792. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAPICTURE,
  793. meye.params.picture);
  794. mutex_unlock(&meye.lock);
  795. return 0;
  796. }
  797. static int meyeioc_qbuf_capt(int *nb)
  798. {
  799. if (!meye.grab_fbuffer)
  800. return -EINVAL;
  801. if (*nb >= gbuffers)
  802. return -EINVAL;
  803. if (*nb < 0) {
  804. /* stop capture */
  805. mchip_hic_stop();
  806. return 0;
  807. }
  808. if (meye.grab_buffer[*nb].state != MEYE_BUF_UNUSED)
  809. return -EBUSY;
  810. mutex_lock(&meye.lock);
  811. if (meye.mchip_mode != MCHIP_HIC_MODE_CONT_COMP)
  812. mchip_cont_compression_start();
  813. meye.grab_buffer[*nb].state = MEYE_BUF_USING;
  814. kfifo_put(meye.grabq, (unsigned char *)nb, sizeof(int));
  815. mutex_unlock(&meye.lock);
  816. return 0;
  817. }
  818. static int meyeioc_sync(struct file *file, void *fh, int *i)
  819. {
  820. int unused;
  821. if (*i < 0 || *i >= gbuffers)
  822. return -EINVAL;
  823. mutex_lock(&meye.lock);
  824. switch (meye.grab_buffer[*i].state) {
  825. case MEYE_BUF_UNUSED:
  826. mutex_unlock(&meye.lock);
  827. return -EINVAL;
  828. case MEYE_BUF_USING:
  829. if (file->f_flags & O_NONBLOCK) {
  830. mutex_unlock(&meye.lock);
  831. return -EAGAIN;
  832. }
  833. if (wait_event_interruptible(meye.proc_list,
  834. (meye.grab_buffer[*i].state != MEYE_BUF_USING))) {
  835. mutex_unlock(&meye.lock);
  836. return -EINTR;
  837. }
  838. /* fall through */
  839. case MEYE_BUF_DONE:
  840. meye.grab_buffer[*i].state = MEYE_BUF_UNUSED;
  841. kfifo_get(meye.doneq, (unsigned char *)&unused, sizeof(int));
  842. }
  843. *i = meye.grab_buffer[*i].size;
  844. mutex_unlock(&meye.lock);
  845. return 0;
  846. }
  847. static int meyeioc_stillcapt(void)
  848. {
  849. if (!meye.grab_fbuffer)
  850. return -EINVAL;
  851. if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
  852. return -EBUSY;
  853. mutex_lock(&meye.lock);
  854. meye.grab_buffer[0].state = MEYE_BUF_USING;
  855. mchip_take_picture();
  856. mchip_get_picture(meye.grab_fbuffer,
  857. mchip_hsize() * mchip_vsize() * 2);
  858. meye.grab_buffer[0].state = MEYE_BUF_DONE;
  859. mutex_unlock(&meye.lock);
  860. return 0;
  861. }
  862. static int meyeioc_stilljcapt(int *len)
  863. {
  864. if (!meye.grab_fbuffer)
  865. return -EINVAL;
  866. if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
  867. return -EBUSY;
  868. mutex_lock(&meye.lock);
  869. meye.grab_buffer[0].state = MEYE_BUF_USING;
  870. *len = -1;
  871. while (*len == -1) {
  872. mchip_take_picture();
  873. *len = mchip_compress_frame(meye.grab_fbuffer, gbufsize);
  874. }
  875. meye.grab_buffer[0].state = MEYE_BUF_DONE;
  876. mutex_unlock(&meye.lock);
  877. return 0;
  878. }
  879. static int vidioc_querycap(struct file *file, void *fh,
  880. struct v4l2_capability *cap)
  881. {
  882. strcpy(cap->driver, "meye");
  883. strcpy(cap->card, "meye");
  884. sprintf(cap->bus_info, "PCI:%s", pci_name(meye.mchip_dev));
  885. cap->version = (MEYE_DRIVER_MAJORVERSION << 8) +
  886. MEYE_DRIVER_MINORVERSION;
  887. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  888. V4L2_CAP_STREAMING;
  889. return 0;
  890. }
  891. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  892. {
  893. if (i->index != 0)
  894. return -EINVAL;
  895. strcpy(i->name, "Camera");
  896. i->type = V4L2_INPUT_TYPE_CAMERA;
  897. return 0;
  898. }
  899. static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
  900. {
  901. *i = 0;
  902. return 0;
  903. }
  904. static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
  905. {
  906. if (i != 0)
  907. return -EINVAL;
  908. return 0;
  909. }
  910. static int vidioc_queryctrl(struct file *file, void *fh,
  911. struct v4l2_queryctrl *c)
  912. {
  913. switch (c->id) {
  914. case V4L2_CID_BRIGHTNESS:
  915. c->type = V4L2_CTRL_TYPE_INTEGER;
  916. strcpy(c->name, "Brightness");
  917. c->minimum = 0;
  918. c->maximum = 63;
  919. c->step = 1;
  920. c->default_value = 32;
  921. c->flags = 0;
  922. break;
  923. case V4L2_CID_HUE:
  924. c->type = V4L2_CTRL_TYPE_INTEGER;
  925. strcpy(c->name, "Hue");
  926. c->minimum = 0;
  927. c->maximum = 63;
  928. c->step = 1;
  929. c->default_value = 32;
  930. c->flags = 0;
  931. break;
  932. case V4L2_CID_CONTRAST:
  933. c->type = V4L2_CTRL_TYPE_INTEGER;
  934. strcpy(c->name, "Contrast");
  935. c->minimum = 0;
  936. c->maximum = 63;
  937. c->step = 1;
  938. c->default_value = 32;
  939. c->flags = 0;
  940. break;
  941. case V4L2_CID_SATURATION:
  942. c->type = V4L2_CTRL_TYPE_INTEGER;
  943. strcpy(c->name, "Saturation");
  944. c->minimum = 0;
  945. c->maximum = 63;
  946. c->step = 1;
  947. c->default_value = 32;
  948. c->flags = 0;
  949. break;
  950. case V4L2_CID_AGC:
  951. c->type = V4L2_CTRL_TYPE_INTEGER;
  952. strcpy(c->name, "Agc");
  953. c->minimum = 0;
  954. c->maximum = 63;
  955. c->step = 1;
  956. c->default_value = 48;
  957. c->flags = 0;
  958. break;
  959. case V4L2_CID_MEYE_SHARPNESS:
  960. case V4L2_CID_SHARPNESS:
  961. c->type = V4L2_CTRL_TYPE_INTEGER;
  962. strcpy(c->name, "Sharpness");
  963. c->minimum = 0;
  964. c->maximum = 63;
  965. c->step = 1;
  966. c->default_value = 32;
  967. /* Continue to report legacy private SHARPNESS ctrl but
  968. * say it is disabled in preference to ctrl in the spec
  969. */
  970. c->flags = (c->id == V4L2_CID_SHARPNESS) ? 0 :
  971. V4L2_CTRL_FLAG_DISABLED;
  972. break;
  973. case V4L2_CID_PICTURE:
  974. c->type = V4L2_CTRL_TYPE_INTEGER;
  975. strcpy(c->name, "Picture");
  976. c->minimum = 0;
  977. c->maximum = 63;
  978. c->step = 1;
  979. c->default_value = 0;
  980. c->flags = 0;
  981. break;
  982. case V4L2_CID_JPEGQUAL:
  983. c->type = V4L2_CTRL_TYPE_INTEGER;
  984. strcpy(c->name, "JPEG quality");
  985. c->minimum = 0;
  986. c->maximum = 10;
  987. c->step = 1;
  988. c->default_value = 8;
  989. c->flags = 0;
  990. break;
  991. case V4L2_CID_FRAMERATE:
  992. c->type = V4L2_CTRL_TYPE_INTEGER;
  993. strcpy(c->name, "Framerate");
  994. c->minimum = 0;
  995. c->maximum = 31;
  996. c->step = 1;
  997. c->default_value = 0;
  998. c->flags = 0;
  999. break;
  1000. default:
  1001. return -EINVAL;
  1002. }
  1003. return 0;
  1004. }
  1005. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  1006. {
  1007. mutex_lock(&meye.lock);
  1008. switch (c->id) {
  1009. case V4L2_CID_BRIGHTNESS:
  1010. sony_pic_camera_command(
  1011. SONY_PIC_COMMAND_SETCAMERABRIGHTNESS, c->value);
  1012. meye.picture.brightness = c->value << 10;
  1013. break;
  1014. case V4L2_CID_HUE:
  1015. sony_pic_camera_command(
  1016. SONY_PIC_COMMAND_SETCAMERAHUE, c->value);
  1017. meye.picture.hue = c->value << 10;
  1018. break;
  1019. case V4L2_CID_CONTRAST:
  1020. sony_pic_camera_command(
  1021. SONY_PIC_COMMAND_SETCAMERACONTRAST, c->value);
  1022. meye.picture.contrast = c->value << 10;
  1023. break;
  1024. case V4L2_CID_SATURATION:
  1025. sony_pic_camera_command(
  1026. SONY_PIC_COMMAND_SETCAMERACOLOR, c->value);
  1027. meye.picture.colour = c->value << 10;
  1028. break;
  1029. case V4L2_CID_AGC:
  1030. sony_pic_camera_command(
  1031. SONY_PIC_COMMAND_SETCAMERAAGC, c->value);
  1032. meye.params.agc = c->value;
  1033. break;
  1034. case V4L2_CID_SHARPNESS:
  1035. case V4L2_CID_MEYE_SHARPNESS:
  1036. sony_pic_camera_command(
  1037. SONY_PIC_COMMAND_SETCAMERASHARPNESS, c->value);
  1038. meye.params.sharpness = c->value;
  1039. break;
  1040. case V4L2_CID_PICTURE:
  1041. sony_pic_camera_command(
  1042. SONY_PIC_COMMAND_SETCAMERAPICTURE, c->value);
  1043. meye.params.picture = c->value;
  1044. break;
  1045. case V4L2_CID_JPEGQUAL:
  1046. meye.params.quality = c->value;
  1047. break;
  1048. case V4L2_CID_FRAMERATE:
  1049. meye.params.framerate = c->value;
  1050. break;
  1051. default:
  1052. mutex_unlock(&meye.lock);
  1053. return -EINVAL;
  1054. }
  1055. mutex_unlock(&meye.lock);
  1056. return 0;
  1057. }
  1058. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  1059. {
  1060. mutex_lock(&meye.lock);
  1061. switch (c->id) {
  1062. case V4L2_CID_BRIGHTNESS:
  1063. c->value = meye.picture.brightness >> 10;
  1064. break;
  1065. case V4L2_CID_HUE:
  1066. c->value = meye.picture.hue >> 10;
  1067. break;
  1068. case V4L2_CID_CONTRAST:
  1069. c->value = meye.picture.contrast >> 10;
  1070. break;
  1071. case V4L2_CID_SATURATION:
  1072. c->value = meye.picture.colour >> 10;
  1073. break;
  1074. case V4L2_CID_AGC:
  1075. c->value = meye.params.agc;
  1076. break;
  1077. case V4L2_CID_SHARPNESS:
  1078. case V4L2_CID_MEYE_SHARPNESS:
  1079. c->value = meye.params.sharpness;
  1080. break;
  1081. case V4L2_CID_PICTURE:
  1082. c->value = meye.params.picture;
  1083. break;
  1084. case V4L2_CID_JPEGQUAL:
  1085. c->value = meye.params.quality;
  1086. break;
  1087. case V4L2_CID_FRAMERATE:
  1088. c->value = meye.params.framerate;
  1089. break;
  1090. default:
  1091. mutex_unlock(&meye.lock);
  1092. return -EINVAL;
  1093. }
  1094. mutex_unlock(&meye.lock);
  1095. return 0;
  1096. }
  1097. static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
  1098. struct v4l2_fmtdesc *f)
  1099. {
  1100. if (f->index > 1)
  1101. return -EINVAL;
  1102. if (f->index == 0) {
  1103. /* standard YUV 422 capture */
  1104. f->flags = 0;
  1105. strcpy(f->description, "YUV422");
  1106. f->pixelformat = V4L2_PIX_FMT_YUYV;
  1107. } else {
  1108. /* compressed MJPEG capture */
  1109. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  1110. strcpy(f->description, "MJPEG");
  1111. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  1112. }
  1113. return 0;
  1114. }
  1115. static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
  1116. struct v4l2_format *f)
  1117. {
  1118. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
  1119. f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1120. return -EINVAL;
  1121. if (f->fmt.pix.field != V4L2_FIELD_ANY &&
  1122. f->fmt.pix.field != V4L2_FIELD_NONE)
  1123. return -EINVAL;
  1124. f->fmt.pix.field = V4L2_FIELD_NONE;
  1125. if (f->fmt.pix.width <= 320) {
  1126. f->fmt.pix.width = 320;
  1127. f->fmt.pix.height = 240;
  1128. } else {
  1129. f->fmt.pix.width = 640;
  1130. f->fmt.pix.height = 480;
  1131. }
  1132. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1133. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1134. f->fmt.pix.bytesperline;
  1135. f->fmt.pix.colorspace = 0;
  1136. f->fmt.pix.priv = 0;
  1137. return 0;
  1138. }
  1139. static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
  1140. struct v4l2_format *f)
  1141. {
  1142. switch (meye.mchip_mode) {
  1143. case MCHIP_HIC_MODE_CONT_OUT:
  1144. default:
  1145. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  1146. break;
  1147. case MCHIP_HIC_MODE_CONT_COMP:
  1148. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
  1149. break;
  1150. }
  1151. f->fmt.pix.field = V4L2_FIELD_NONE;
  1152. f->fmt.pix.width = mchip_hsize();
  1153. f->fmt.pix.height = mchip_vsize();
  1154. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1155. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1156. f->fmt.pix.bytesperline;
  1157. return 0;
  1158. }
  1159. static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
  1160. struct v4l2_format *f)
  1161. {
  1162. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
  1163. f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1164. return -EINVAL;
  1165. if (f->fmt.pix.field != V4L2_FIELD_ANY &&
  1166. f->fmt.pix.field != V4L2_FIELD_NONE)
  1167. return -EINVAL;
  1168. f->fmt.pix.field = V4L2_FIELD_NONE;
  1169. mutex_lock(&meye.lock);
  1170. if (f->fmt.pix.width <= 320) {
  1171. f->fmt.pix.width = 320;
  1172. f->fmt.pix.height = 240;
  1173. meye.params.subsample = 1;
  1174. } else {
  1175. f->fmt.pix.width = 640;
  1176. f->fmt.pix.height = 480;
  1177. meye.params.subsample = 0;
  1178. }
  1179. switch (f->fmt.pix.pixelformat) {
  1180. case V4L2_PIX_FMT_YUYV:
  1181. meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
  1182. break;
  1183. case V4L2_PIX_FMT_MJPEG:
  1184. meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
  1185. break;
  1186. }
  1187. mutex_unlock(&meye.lock);
  1188. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  1189. f->fmt.pix.sizeimage = f->fmt.pix.height *
  1190. f->fmt.pix.bytesperline;
  1191. f->fmt.pix.colorspace = 0;
  1192. f->fmt.pix.priv = 0;
  1193. return 0;
  1194. }
  1195. static int vidioc_reqbufs(struct file *file, void *fh,
  1196. struct v4l2_requestbuffers *req)
  1197. {
  1198. int i;
  1199. if (req->memory != V4L2_MEMORY_MMAP)
  1200. return -EINVAL;
  1201. if (meye.grab_fbuffer && req->count == gbuffers) {
  1202. /* already allocated, no modifications */
  1203. return 0;
  1204. }
  1205. mutex_lock(&meye.lock);
  1206. if (meye.grab_fbuffer) {
  1207. for (i = 0; i < gbuffers; i++)
  1208. if (meye.vma_use_count[i]) {
  1209. mutex_unlock(&meye.lock);
  1210. return -EINVAL;
  1211. }
  1212. rvfree(meye.grab_fbuffer, gbuffers * gbufsize);
  1213. meye.grab_fbuffer = NULL;
  1214. }
  1215. gbuffers = max(2, min((int)req->count, MEYE_MAX_BUFNBRS));
  1216. req->count = gbuffers;
  1217. meye.grab_fbuffer = rvmalloc(gbuffers * gbufsize);
  1218. if (!meye.grab_fbuffer) {
  1219. printk(KERN_ERR "meye: v4l framebuffer allocation"
  1220. " failed\n");
  1221. mutex_unlock(&meye.lock);
  1222. return -ENOMEM;
  1223. }
  1224. for (i = 0; i < gbuffers; i++)
  1225. meye.vma_use_count[i] = 0;
  1226. mutex_unlock(&meye.lock);
  1227. return 0;
  1228. }
  1229. static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1230. {
  1231. unsigned int index = buf->index;
  1232. if (index >= gbuffers)
  1233. return -EINVAL;
  1234. buf->bytesused = meye.grab_buffer[index].size;
  1235. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1236. if (meye.grab_buffer[index].state == MEYE_BUF_USING)
  1237. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1238. if (meye.grab_buffer[index].state == MEYE_BUF_DONE)
  1239. buf->flags |= V4L2_BUF_FLAG_DONE;
  1240. buf->field = V4L2_FIELD_NONE;
  1241. buf->timestamp = meye.grab_buffer[index].timestamp;
  1242. buf->sequence = meye.grab_buffer[index].sequence;
  1243. buf->memory = V4L2_MEMORY_MMAP;
  1244. buf->m.offset = index * gbufsize;
  1245. buf->length = gbufsize;
  1246. return 0;
  1247. }
  1248. static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1249. {
  1250. if (buf->memory != V4L2_MEMORY_MMAP)
  1251. return -EINVAL;
  1252. if (buf->index >= gbuffers)
  1253. return -EINVAL;
  1254. if (meye.grab_buffer[buf->index].state != MEYE_BUF_UNUSED)
  1255. return -EINVAL;
  1256. mutex_lock(&meye.lock);
  1257. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1258. buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1259. meye.grab_buffer[buf->index].state = MEYE_BUF_USING;
  1260. kfifo_put(meye.grabq, (unsigned char *)&buf->index, sizeof(int));
  1261. mutex_unlock(&meye.lock);
  1262. return 0;
  1263. }
  1264. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1265. {
  1266. int reqnr;
  1267. if (buf->memory != V4L2_MEMORY_MMAP)
  1268. return -EINVAL;
  1269. mutex_lock(&meye.lock);
  1270. if (kfifo_len(meye.doneq) == 0 && file->f_flags & O_NONBLOCK) {
  1271. mutex_unlock(&meye.lock);
  1272. return -EAGAIN;
  1273. }
  1274. if (wait_event_interruptible(meye.proc_list,
  1275. kfifo_len(meye.doneq) != 0) < 0) {
  1276. mutex_unlock(&meye.lock);
  1277. return -EINTR;
  1278. }
  1279. if (!kfifo_get(meye.doneq, (unsigned char *)&reqnr,
  1280. sizeof(int))) {
  1281. mutex_unlock(&meye.lock);
  1282. return -EBUSY;
  1283. }
  1284. if (meye.grab_buffer[reqnr].state != MEYE_BUF_DONE) {
  1285. mutex_unlock(&meye.lock);
  1286. return -EINVAL;
  1287. }
  1288. buf->index = reqnr;
  1289. buf->bytesused = meye.grab_buffer[reqnr].size;
  1290. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1291. buf->field = V4L2_FIELD_NONE;
  1292. buf->timestamp = meye.grab_buffer[reqnr].timestamp;
  1293. buf->sequence = meye.grab_buffer[reqnr].sequence;
  1294. buf->memory = V4L2_MEMORY_MMAP;
  1295. buf->m.offset = reqnr * gbufsize;
  1296. buf->length = gbufsize;
  1297. meye.grab_buffer[reqnr].state = MEYE_BUF_UNUSED;
  1298. mutex_unlock(&meye.lock);
  1299. return 0;
  1300. }
  1301. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1302. {
  1303. mutex_lock(&meye.lock);
  1304. switch (meye.mchip_mode) {
  1305. case MCHIP_HIC_MODE_CONT_OUT:
  1306. mchip_continuous_start();
  1307. break;
  1308. case MCHIP_HIC_MODE_CONT_COMP:
  1309. mchip_cont_compression_start();
  1310. break;
  1311. default:
  1312. mutex_unlock(&meye.lock);
  1313. return -EINVAL;
  1314. }
  1315. mutex_unlock(&meye.lock);
  1316. return 0;
  1317. }
  1318. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  1319. {
  1320. mutex_lock(&meye.lock);
  1321. mchip_hic_stop();
  1322. kfifo_reset(meye.grabq);
  1323. kfifo_reset(meye.doneq);
  1324. for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
  1325. meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
  1326. mutex_unlock(&meye.lock);
  1327. return 0;
  1328. }
  1329. static long vidioc_default(struct file *file, void *fh, int cmd, void *arg)
  1330. {
  1331. switch (cmd) {
  1332. case MEYEIOC_G_PARAMS:
  1333. return meyeioc_g_params((struct meye_params *) arg);
  1334. case MEYEIOC_S_PARAMS:
  1335. return meyeioc_s_params((struct meye_params *) arg);
  1336. case MEYEIOC_QBUF_CAPT:
  1337. return meyeioc_qbuf_capt((int *) arg);
  1338. case MEYEIOC_SYNC:
  1339. return meyeioc_sync(file, fh, (int *) arg);
  1340. case MEYEIOC_STILLCAPT:
  1341. return meyeioc_stillcapt();
  1342. case MEYEIOC_STILLJCAPT:
  1343. return meyeioc_stilljcapt((int *) arg);
  1344. default:
  1345. return -EINVAL;
  1346. }
  1347. }
  1348. static unsigned int meye_poll(struct file *file, poll_table *wait)
  1349. {
  1350. unsigned int res = 0;
  1351. mutex_lock(&meye.lock);
  1352. poll_wait(file, &meye.proc_list, wait);
  1353. if (kfifo_len(meye.doneq))
  1354. res = POLLIN | POLLRDNORM;
  1355. mutex_unlock(&meye.lock);
  1356. return res;
  1357. }
  1358. static void meye_vm_open(struct vm_area_struct *vma)
  1359. {
  1360. long idx = (long)vma->vm_private_data;
  1361. meye.vma_use_count[idx]++;
  1362. }
  1363. static void meye_vm_close(struct vm_area_struct *vma)
  1364. {
  1365. long idx = (long)vma->vm_private_data;
  1366. meye.vma_use_count[idx]--;
  1367. }
  1368. static struct vm_operations_struct meye_vm_ops = {
  1369. .open = meye_vm_open,
  1370. .close = meye_vm_close,
  1371. };
  1372. static int meye_mmap(struct file *file, struct vm_area_struct *vma)
  1373. {
  1374. unsigned long start = vma->vm_start;
  1375. unsigned long size = vma->vm_end - vma->vm_start;
  1376. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1377. unsigned long page, pos;
  1378. mutex_lock(&meye.lock);
  1379. if (size > gbuffers * gbufsize) {
  1380. mutex_unlock(&meye.lock);
  1381. return -EINVAL;
  1382. }
  1383. if (!meye.grab_fbuffer) {
  1384. int i;
  1385. /* lazy allocation */
  1386. meye.grab_fbuffer = rvmalloc(gbuffers*gbufsize);
  1387. if (!meye.grab_fbuffer) {
  1388. printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
  1389. mutex_unlock(&meye.lock);
  1390. return -ENOMEM;
  1391. }
  1392. for (i = 0; i < gbuffers; i++)
  1393. meye.vma_use_count[i] = 0;
  1394. }
  1395. pos = (unsigned long)meye.grab_fbuffer + offset;
  1396. while (size > 0) {
  1397. page = vmalloc_to_pfn((void *)pos);
  1398. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  1399. mutex_unlock(&meye.lock);
  1400. return -EAGAIN;
  1401. }
  1402. start += PAGE_SIZE;
  1403. pos += PAGE_SIZE;
  1404. if (size > PAGE_SIZE)
  1405. size -= PAGE_SIZE;
  1406. else
  1407. size = 0;
  1408. }
  1409. vma->vm_ops = &meye_vm_ops;
  1410. vma->vm_flags &= ~VM_IO; /* not I/O memory */
  1411. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  1412. vma->vm_private_data = (void *) (offset / gbufsize);
  1413. meye_vm_open(vma);
  1414. mutex_unlock(&meye.lock);
  1415. return 0;
  1416. }
  1417. static const struct v4l2_file_operations meye_fops = {
  1418. .owner = THIS_MODULE,
  1419. .open = meye_open,
  1420. .release = meye_release,
  1421. .mmap = meye_mmap,
  1422. .ioctl = video_ioctl2,
  1423. .poll = meye_poll,
  1424. };
  1425. static const struct v4l2_ioctl_ops meye_ioctl_ops = {
  1426. .vidioc_querycap = vidioc_querycap,
  1427. .vidioc_enum_input = vidioc_enum_input,
  1428. .vidioc_g_input = vidioc_g_input,
  1429. .vidioc_s_input = vidioc_s_input,
  1430. .vidioc_queryctrl = vidioc_queryctrl,
  1431. .vidioc_s_ctrl = vidioc_s_ctrl,
  1432. .vidioc_g_ctrl = vidioc_g_ctrl,
  1433. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1434. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1435. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1436. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1437. .vidioc_reqbufs = vidioc_reqbufs,
  1438. .vidioc_querybuf = vidioc_querybuf,
  1439. .vidioc_qbuf = vidioc_qbuf,
  1440. .vidioc_dqbuf = vidioc_dqbuf,
  1441. .vidioc_streamon = vidioc_streamon,
  1442. .vidioc_streamoff = vidioc_streamoff,
  1443. .vidioc_default = vidioc_default,
  1444. };
  1445. static struct video_device meye_template = {
  1446. .name = "meye",
  1447. .fops = &meye_fops,
  1448. .ioctl_ops = &meye_ioctl_ops,
  1449. .release = video_device_release,
  1450. .minor = -1,
  1451. };
  1452. #ifdef CONFIG_PM
  1453. static int meye_suspend(struct pci_dev *pdev, pm_message_t state)
  1454. {
  1455. pci_save_state(pdev);
  1456. meye.pm_mchip_mode = meye.mchip_mode;
  1457. mchip_hic_stop();
  1458. mchip_set(MCHIP_MM_INTA, 0x0);
  1459. return 0;
  1460. }
  1461. static int meye_resume(struct pci_dev *pdev)
  1462. {
  1463. pci_restore_state(pdev);
  1464. pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
  1465. mchip_delay(MCHIP_HIC_CMD, 0);
  1466. mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
  1467. msleep(1);
  1468. mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
  1469. msleep(1);
  1470. mchip_set(MCHIP_MM_PCI_MODE, 5);
  1471. msleep(1);
  1472. mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
  1473. switch (meye.pm_mchip_mode) {
  1474. case MCHIP_HIC_MODE_CONT_OUT:
  1475. mchip_continuous_start();
  1476. break;
  1477. case MCHIP_HIC_MODE_CONT_COMP:
  1478. mchip_cont_compression_start();
  1479. break;
  1480. }
  1481. return 0;
  1482. }
  1483. #endif
  1484. static int __devinit meye_probe(struct pci_dev *pcidev,
  1485. const struct pci_device_id *ent)
  1486. {
  1487. int ret = -EBUSY;
  1488. unsigned long mchip_adr;
  1489. if (meye.mchip_dev != NULL) {
  1490. printk(KERN_ERR "meye: only one device allowed!\n");
  1491. goto outnotdev;
  1492. }
  1493. ret = -ENOMEM;
  1494. meye.mchip_dev = pcidev;
  1495. meye.video_dev = video_device_alloc();
  1496. if (!meye.video_dev) {
  1497. printk(KERN_ERR "meye: video_device_alloc() failed!\n");
  1498. goto outnotdev;
  1499. }
  1500. meye.grab_temp = vmalloc(MCHIP_NB_PAGES_MJPEG * PAGE_SIZE);
  1501. if (!meye.grab_temp) {
  1502. printk(KERN_ERR "meye: grab buffer allocation failed\n");
  1503. goto outvmalloc;
  1504. }
  1505. spin_lock_init(&meye.grabq_lock);
  1506. meye.grabq = kfifo_alloc(sizeof(int) * MEYE_MAX_BUFNBRS, GFP_KERNEL,
  1507. &meye.grabq_lock);
  1508. if (IS_ERR(meye.grabq)) {
  1509. printk(KERN_ERR "meye: fifo allocation failed\n");
  1510. goto outkfifoalloc1;
  1511. }
  1512. spin_lock_init(&meye.doneq_lock);
  1513. meye.doneq = kfifo_alloc(sizeof(int) * MEYE_MAX_BUFNBRS, GFP_KERNEL,
  1514. &meye.doneq_lock);
  1515. if (IS_ERR(meye.doneq)) {
  1516. printk(KERN_ERR "meye: fifo allocation failed\n");
  1517. goto outkfifoalloc2;
  1518. }
  1519. memcpy(meye.video_dev, &meye_template, sizeof(meye_template));
  1520. meye.video_dev->parent = &meye.mchip_dev->dev;
  1521. ret = -EIO;
  1522. if ((ret = sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 1))) {
  1523. printk(KERN_ERR "meye: unable to power on the camera\n");
  1524. printk(KERN_ERR "meye: did you enable the camera in "
  1525. "sonypi using the module options ?\n");
  1526. goto outsonypienable;
  1527. }
  1528. if ((ret = pci_enable_device(meye.mchip_dev))) {
  1529. printk(KERN_ERR "meye: pci_enable_device failed\n");
  1530. goto outenabledev;
  1531. }
  1532. mchip_adr = pci_resource_start(meye.mchip_dev,0);
  1533. if (!mchip_adr) {
  1534. printk(KERN_ERR "meye: mchip has no device base address\n");
  1535. goto outregions;
  1536. }
  1537. if (!request_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1538. pci_resource_len(meye.mchip_dev, 0),
  1539. "meye")) {
  1540. printk(KERN_ERR "meye: request_mem_region failed\n");
  1541. goto outregions;
  1542. }
  1543. meye.mchip_mmregs = ioremap(mchip_adr, MCHIP_MM_REGS);
  1544. if (!meye.mchip_mmregs) {
  1545. printk(KERN_ERR "meye: ioremap failed\n");
  1546. goto outremap;
  1547. }
  1548. meye.mchip_irq = pcidev->irq;
  1549. if (request_irq(meye.mchip_irq, meye_irq,
  1550. IRQF_DISABLED | IRQF_SHARED, "meye", meye_irq)) {
  1551. printk(KERN_ERR "meye: request_irq failed\n");
  1552. goto outreqirq;
  1553. }
  1554. pci_write_config_byte(meye.mchip_dev, PCI_CACHE_LINE_SIZE, 8);
  1555. pci_write_config_byte(meye.mchip_dev, PCI_LATENCY_TIMER, 64);
  1556. pci_set_master(meye.mchip_dev);
  1557. /* Ask the camera to perform a soft reset. */
  1558. pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
  1559. mchip_delay(MCHIP_HIC_CMD, 0);
  1560. mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
  1561. msleep(1);
  1562. mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
  1563. msleep(1);
  1564. mchip_set(MCHIP_MM_PCI_MODE, 5);
  1565. msleep(1);
  1566. mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
  1567. if (video_register_device(meye.video_dev, VFL_TYPE_GRABBER,
  1568. video_nr) < 0) {
  1569. printk(KERN_ERR "meye: video_register_device failed\n");
  1570. goto outvideoreg;
  1571. }
  1572. mutex_init(&meye.lock);
  1573. init_waitqueue_head(&meye.proc_list);
  1574. meye.picture.depth = 16;
  1575. meye.picture.palette = VIDEO_PALETTE_YUV422;
  1576. meye.picture.brightness = 32 << 10;
  1577. meye.picture.hue = 32 << 10;
  1578. meye.picture.colour = 32 << 10;
  1579. meye.picture.contrast = 32 << 10;
  1580. meye.picture.whiteness = 0;
  1581. meye.params.subsample = 0;
  1582. meye.params.quality = 8;
  1583. meye.params.sharpness = 32;
  1584. meye.params.agc = 48;
  1585. meye.params.picture = 0;
  1586. meye.params.framerate = 0;
  1587. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERABRIGHTNESS, 32);
  1588. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAHUE, 32);
  1589. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERACOLOR, 32);
  1590. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERACONTRAST, 32);
  1591. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERASHARPNESS, 32);
  1592. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAPICTURE, 0);
  1593. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAAGC, 48);
  1594. printk(KERN_INFO "meye: Motion Eye Camera Driver v%s.\n",
  1595. MEYE_DRIVER_VERSION);
  1596. printk(KERN_INFO "meye: mchip KL5A72002 rev. %d, base %lx, irq %d\n",
  1597. meye.mchip_dev->revision, mchip_adr, meye.mchip_irq);
  1598. return 0;
  1599. outvideoreg:
  1600. free_irq(meye.mchip_irq, meye_irq);
  1601. outreqirq:
  1602. iounmap(meye.mchip_mmregs);
  1603. outremap:
  1604. release_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1605. pci_resource_len(meye.mchip_dev, 0));
  1606. outregions:
  1607. pci_disable_device(meye.mchip_dev);
  1608. outenabledev:
  1609. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
  1610. outsonypienable:
  1611. kfifo_free(meye.doneq);
  1612. outkfifoalloc2:
  1613. kfifo_free(meye.grabq);
  1614. outkfifoalloc1:
  1615. vfree(meye.grab_temp);
  1616. outvmalloc:
  1617. video_device_release(meye.video_dev);
  1618. outnotdev:
  1619. return ret;
  1620. }
  1621. static void __devexit meye_remove(struct pci_dev *pcidev)
  1622. {
  1623. video_unregister_device(meye.video_dev);
  1624. mchip_hic_stop();
  1625. mchip_dma_free();
  1626. /* disable interrupts */
  1627. mchip_set(MCHIP_MM_INTA, 0x0);
  1628. free_irq(meye.mchip_irq, meye_irq);
  1629. iounmap(meye.mchip_mmregs);
  1630. release_mem_region(pci_resource_start(meye.mchip_dev, 0),
  1631. pci_resource_len(meye.mchip_dev, 0));
  1632. pci_disable_device(meye.mchip_dev);
  1633. sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
  1634. kfifo_free(meye.doneq);
  1635. kfifo_free(meye.grabq);
  1636. vfree(meye.grab_temp);
  1637. if (meye.grab_fbuffer) {
  1638. rvfree(meye.grab_fbuffer, gbuffers*gbufsize);
  1639. meye.grab_fbuffer = NULL;
  1640. }
  1641. printk(KERN_INFO "meye: removed\n");
  1642. }
  1643. static struct pci_device_id meye_pci_tbl[] = {
  1644. { PCI_VENDOR_ID_KAWASAKI, PCI_DEVICE_ID_MCHIP_KL5A72002,
  1645. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  1646. { }
  1647. };
  1648. MODULE_DEVICE_TABLE(pci, meye_pci_tbl);
  1649. static struct pci_driver meye_driver = {
  1650. .name = "meye",
  1651. .id_table = meye_pci_tbl,
  1652. .probe = meye_probe,
  1653. .remove = __devexit_p(meye_remove),
  1654. #ifdef CONFIG_PM
  1655. .suspend = meye_suspend,
  1656. .resume = meye_resume,
  1657. #endif
  1658. };
  1659. static int __init meye_init(void)
  1660. {
  1661. gbuffers = max(2, min((int)gbuffers, MEYE_MAX_BUFNBRS));
  1662. if (gbufsize < 0 || gbufsize > MEYE_MAX_BUFSIZE)
  1663. gbufsize = MEYE_MAX_BUFSIZE;
  1664. gbufsize = PAGE_ALIGN(gbufsize);
  1665. printk(KERN_INFO "meye: using %d buffers with %dk (%dk total) "
  1666. "for capture\n",
  1667. gbuffers,
  1668. gbufsize / 1024, gbuffers * gbufsize / 1024);
  1669. return pci_register_driver(&meye_driver);
  1670. }
  1671. static void __exit meye_exit(void)
  1672. {
  1673. pci_unregister_driver(&meye_driver);
  1674. }
  1675. module_init(meye_init);
  1676. module_exit(meye_exit);