meye.c 49 KB

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