PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/media/video/meye.c

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