meye.c 51 KB

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