mcam-core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. /*
  2. * The Marvell camera core. This device appears in a number of settings,
  3. * so it needs platform-specific support outside of the core.
  4. *
  5. * Copyright 2011 Jonathan Corbet corbet@lwn.net
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/i2c.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/videodev2.h>
  15. #include <linux/slab.h>
  16. #include <media/v4l2-device.h>
  17. #include <media/v4l2-ioctl.h>
  18. #include <media/v4l2-chip-ident.h>
  19. #include <media/ov7670.h>
  20. #include <linux/device.h>
  21. #include <linux/wait.h>
  22. #include <linux/list.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/delay.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/io.h>
  29. #include "mcam-core.h"
  30. /*
  31. * Internal DMA buffer management. Since the controller cannot do S/G I/O,
  32. * we must have physically contiguous buffers to bring frames into.
  33. * These parameters control how many buffers we use, whether we
  34. * allocate them at load time (better chance of success, but nails down
  35. * memory) or when somebody tries to use the camera (riskier), and,
  36. * for load-time allocation, how big they should be.
  37. *
  38. * The controller can cycle through three buffers. We could use
  39. * more by flipping pointers around, but it probably makes little
  40. * sense.
  41. */
  42. static int alloc_bufs_at_read;
  43. module_param(alloc_bufs_at_read, bool, 0444);
  44. MODULE_PARM_DESC(alloc_bufs_at_read,
  45. "Non-zero value causes DMA buffers to be allocated when the "
  46. "video capture device is read, rather than at module load "
  47. "time. This saves memory, but decreases the chances of "
  48. "successfully getting those buffers.");
  49. static int n_dma_bufs = 3;
  50. module_param(n_dma_bufs, uint, 0644);
  51. MODULE_PARM_DESC(n_dma_bufs,
  52. "The number of DMA buffers to allocate. Can be either two "
  53. "(saves memory, makes timing tighter) or three.");
  54. static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
  55. module_param(dma_buf_size, uint, 0444);
  56. MODULE_PARM_DESC(dma_buf_size,
  57. "The size of the allocated DMA buffers. If actual operating "
  58. "parameters require larger buffers, an attempt to reallocate "
  59. "will be made.");
  60. static int min_buffers = 1;
  61. module_param(min_buffers, uint, 0644);
  62. MODULE_PARM_DESC(min_buffers,
  63. "The minimum number of streaming I/O buffers we are willing "
  64. "to work with.");
  65. static int max_buffers = 10;
  66. module_param(max_buffers, uint, 0644);
  67. MODULE_PARM_DESC(max_buffers,
  68. "The maximum number of streaming I/O buffers an application "
  69. "will be allowed to allocate. These buffers are big and live "
  70. "in vmalloc space.");
  71. static int flip;
  72. module_param(flip, bool, 0444);
  73. MODULE_PARM_DESC(flip,
  74. "If set, the sensor will be instructed to flip the image "
  75. "vertically.");
  76. /*
  77. * Status flags. Always manipulated with bit operations.
  78. */
  79. #define CF_BUF0_VALID 0 /* Buffers valid - first three */
  80. #define CF_BUF1_VALID 1
  81. #define CF_BUF2_VALID 2
  82. #define CF_DMA_ACTIVE 3 /* A frame is incoming */
  83. #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
  84. #define sensor_call(cam, o, f, args...) \
  85. v4l2_subdev_call(cam->sensor, o, f, ##args)
  86. static struct mcam_format_struct {
  87. __u8 *desc;
  88. __u32 pixelformat;
  89. int bpp; /* Bytes per pixel */
  90. enum v4l2_mbus_pixelcode mbus_code;
  91. } mcam_formats[] = {
  92. {
  93. .desc = "YUYV 4:2:2",
  94. .pixelformat = V4L2_PIX_FMT_YUYV,
  95. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  96. .bpp = 2,
  97. },
  98. {
  99. .desc = "RGB 444",
  100. .pixelformat = V4L2_PIX_FMT_RGB444,
  101. .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
  102. .bpp = 2,
  103. },
  104. {
  105. .desc = "RGB 565",
  106. .pixelformat = V4L2_PIX_FMT_RGB565,
  107. .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  108. .bpp = 2,
  109. },
  110. {
  111. .desc = "Raw RGB Bayer",
  112. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  113. .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
  114. .bpp = 1
  115. },
  116. };
  117. #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
  118. static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
  119. {
  120. unsigned i;
  121. for (i = 0; i < N_MCAM_FMTS; i++)
  122. if (mcam_formats[i].pixelformat == pixelformat)
  123. return mcam_formats + i;
  124. /* Not found? Then return the first format. */
  125. return mcam_formats;
  126. }
  127. /*
  128. * Start over with DMA buffers - dev_lock needed.
  129. */
  130. static void mcam_reset_buffers(struct mcam_camera *cam)
  131. {
  132. int i;
  133. cam->next_buf = -1;
  134. for (i = 0; i < cam->nbufs; i++)
  135. clear_bit(i, &cam->flags);
  136. cam->specframes = 0;
  137. }
  138. static inline int mcam_needs_config(struct mcam_camera *cam)
  139. {
  140. return test_bit(CF_CONFIG_NEEDED, &cam->flags);
  141. }
  142. static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
  143. {
  144. if (needed)
  145. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  146. else
  147. clear_bit(CF_CONFIG_NEEDED, &cam->flags);
  148. }
  149. /*
  150. * Debugging and related. FIXME these are broken
  151. */
  152. #define cam_err(cam, fmt, arg...) \
  153. dev_err((cam)->dev, fmt, ##arg);
  154. #define cam_warn(cam, fmt, arg...) \
  155. dev_warn((cam)->dev, fmt, ##arg);
  156. #define cam_dbg(cam, fmt, arg...) \
  157. dev_dbg((cam)->dev, fmt, ##arg);
  158. /* ------------------------------------------------------------------- */
  159. /*
  160. * Deal with the controller.
  161. */
  162. /*
  163. * Do everything we think we need to have the interface operating
  164. * according to the desired format.
  165. */
  166. static void mcam_ctlr_dma(struct mcam_camera *cam)
  167. {
  168. /*
  169. * Store the first two Y buffers (we aren't supporting
  170. * planar formats for now, so no UV bufs). Then either
  171. * set the third if it exists, or tell the controller
  172. * to just use two.
  173. */
  174. mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
  175. mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
  176. if (cam->nbufs > 2) {
  177. mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
  178. mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
  179. } else
  180. mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
  181. mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
  182. }
  183. static void mcam_ctlr_image(struct mcam_camera *cam)
  184. {
  185. int imgsz;
  186. struct v4l2_pix_format *fmt = &cam->pix_format;
  187. imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
  188. (fmt->bytesperline & IMGSZ_H_MASK);
  189. mcam_reg_write(cam, REG_IMGSIZE, imgsz);
  190. mcam_reg_write(cam, REG_IMGOFFSET, 0);
  191. /* YPITCH just drops the last two bits */
  192. mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
  193. IMGP_YP_MASK);
  194. /*
  195. * Tell the controller about the image format we are using.
  196. */
  197. switch (cam->pix_format.pixelformat) {
  198. case V4L2_PIX_FMT_YUYV:
  199. mcam_reg_write_mask(cam, REG_CTRL0,
  200. C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
  201. C0_DF_MASK);
  202. break;
  203. case V4L2_PIX_FMT_RGB444:
  204. mcam_reg_write_mask(cam, REG_CTRL0,
  205. C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
  206. C0_DF_MASK);
  207. /* Alpha value? */
  208. break;
  209. case V4L2_PIX_FMT_RGB565:
  210. mcam_reg_write_mask(cam, REG_CTRL0,
  211. C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
  212. C0_DF_MASK);
  213. break;
  214. default:
  215. cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
  216. break;
  217. }
  218. /*
  219. * Make sure it knows we want to use hsync/vsync.
  220. */
  221. mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
  222. C0_SIFM_MASK);
  223. }
  224. /*
  225. * Configure the controller for operation; caller holds the
  226. * device mutex.
  227. */
  228. static int mcam_ctlr_configure(struct mcam_camera *cam)
  229. {
  230. unsigned long flags;
  231. spin_lock_irqsave(&cam->dev_lock, flags);
  232. mcam_ctlr_dma(cam);
  233. mcam_ctlr_image(cam);
  234. mcam_set_config_needed(cam, 0);
  235. spin_unlock_irqrestore(&cam->dev_lock, flags);
  236. return 0;
  237. }
  238. static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
  239. {
  240. /*
  241. * Clear any pending interrupts, since we do not
  242. * expect to have I/O active prior to enabling.
  243. */
  244. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
  245. mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
  246. }
  247. static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
  248. {
  249. mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
  250. }
  251. /*
  252. * Make the controller start grabbing images. Everything must
  253. * be set up before doing this.
  254. */
  255. static void mcam_ctlr_start(struct mcam_camera *cam)
  256. {
  257. /* set_bit performs a read, so no other barrier should be
  258. needed here */
  259. mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
  260. }
  261. static void mcam_ctlr_stop(struct mcam_camera *cam)
  262. {
  263. mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  264. }
  265. static void mcam_ctlr_init(struct mcam_camera *cam)
  266. {
  267. unsigned long flags;
  268. spin_lock_irqsave(&cam->dev_lock, flags);
  269. /*
  270. * Make sure it's not powered down.
  271. */
  272. mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  273. /*
  274. * Turn off the enable bit. It sure should be off anyway,
  275. * but it's good to be sure.
  276. */
  277. mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  278. /*
  279. * Clock the sensor appropriately. Controller clock should
  280. * be 48MHz, sensor "typical" value is half that.
  281. */
  282. mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
  283. spin_unlock_irqrestore(&cam->dev_lock, flags);
  284. }
  285. /*
  286. * Stop the controller, and don't return until we're really sure that no
  287. * further DMA is going on.
  288. */
  289. static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
  290. {
  291. unsigned long flags;
  292. /*
  293. * Theory: stop the camera controller (whether it is operating
  294. * or not). Delay briefly just in case we race with the SOF
  295. * interrupt, then wait until no DMA is active.
  296. */
  297. spin_lock_irqsave(&cam->dev_lock, flags);
  298. mcam_ctlr_stop(cam);
  299. spin_unlock_irqrestore(&cam->dev_lock, flags);
  300. mdelay(1);
  301. wait_event_timeout(cam->iowait,
  302. !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
  303. if (test_bit(CF_DMA_ACTIVE, &cam->flags))
  304. cam_err(cam, "Timeout waiting for DMA to end\n");
  305. /* This would be bad news - what now? */
  306. spin_lock_irqsave(&cam->dev_lock, flags);
  307. cam->state = S_IDLE;
  308. mcam_ctlr_irq_disable(cam);
  309. spin_unlock_irqrestore(&cam->dev_lock, flags);
  310. }
  311. /*
  312. * Power up and down.
  313. */
  314. static void mcam_ctlr_power_up(struct mcam_camera *cam)
  315. {
  316. unsigned long flags;
  317. spin_lock_irqsave(&cam->dev_lock, flags);
  318. mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  319. cam->plat_power_up(cam);
  320. spin_unlock_irqrestore(&cam->dev_lock, flags);
  321. msleep(5); /* Just to be sure */
  322. }
  323. static void mcam_ctlr_power_down(struct mcam_camera *cam)
  324. {
  325. unsigned long flags;
  326. spin_lock_irqsave(&cam->dev_lock, flags);
  327. cam->plat_power_down(cam);
  328. mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
  329. spin_unlock_irqrestore(&cam->dev_lock, flags);
  330. }
  331. /* -------------------------------------------------------------------- */
  332. /*
  333. * Communications with the sensor.
  334. */
  335. static int __mcam_cam_reset(struct mcam_camera *cam)
  336. {
  337. return sensor_call(cam, core, reset, 0);
  338. }
  339. /*
  340. * We have found the sensor on the i2c. Let's try to have a
  341. * conversation.
  342. */
  343. static int mcam_cam_init(struct mcam_camera *cam)
  344. {
  345. struct v4l2_dbg_chip_ident chip;
  346. int ret;
  347. mutex_lock(&cam->s_mutex);
  348. if (cam->state != S_NOTREADY)
  349. cam_warn(cam, "Cam init with device in funky state %d",
  350. cam->state);
  351. ret = __mcam_cam_reset(cam);
  352. if (ret)
  353. goto out;
  354. chip.ident = V4L2_IDENT_NONE;
  355. chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
  356. chip.match.addr = cam->sensor_addr;
  357. ret = sensor_call(cam, core, g_chip_ident, &chip);
  358. if (ret)
  359. goto out;
  360. cam->sensor_type = chip.ident;
  361. if (cam->sensor_type != V4L2_IDENT_OV7670) {
  362. cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
  363. ret = -EINVAL;
  364. goto out;
  365. }
  366. /* Get/set parameters? */
  367. ret = 0;
  368. cam->state = S_IDLE;
  369. out:
  370. mcam_ctlr_power_down(cam);
  371. mutex_unlock(&cam->s_mutex);
  372. return ret;
  373. }
  374. /*
  375. * Configure the sensor to match the parameters we have. Caller should
  376. * hold s_mutex
  377. */
  378. static int mcam_cam_set_flip(struct mcam_camera *cam)
  379. {
  380. struct v4l2_control ctrl;
  381. memset(&ctrl, 0, sizeof(ctrl));
  382. ctrl.id = V4L2_CID_VFLIP;
  383. ctrl.value = flip;
  384. return sensor_call(cam, core, s_ctrl, &ctrl);
  385. }
  386. static int mcam_cam_configure(struct mcam_camera *cam)
  387. {
  388. struct v4l2_mbus_framefmt mbus_fmt;
  389. int ret;
  390. v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
  391. ret = sensor_call(cam, core, init, 0);
  392. if (ret == 0)
  393. ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
  394. /*
  395. * OV7670 does weird things if flip is set *before* format...
  396. */
  397. ret += mcam_cam_set_flip(cam);
  398. return ret;
  399. }
  400. /* -------------------------------------------------------------------- */
  401. /*
  402. * DMA buffer management. These functions need s_mutex held.
  403. */
  404. /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
  405. * does a get_free_pages() call, and we waste a good chunk of an orderN
  406. * allocation. Should try to allocate the whole set in one chunk.
  407. */
  408. static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
  409. {
  410. int i;
  411. mcam_set_config_needed(cam, 1);
  412. if (loadtime)
  413. cam->dma_buf_size = dma_buf_size;
  414. else
  415. cam->dma_buf_size = cam->pix_format.sizeimage;
  416. if (n_dma_bufs > 3)
  417. n_dma_bufs = 3;
  418. cam->nbufs = 0;
  419. for (i = 0; i < n_dma_bufs; i++) {
  420. cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
  421. cam->dma_buf_size, cam->dma_handles + i,
  422. GFP_KERNEL);
  423. if (cam->dma_bufs[i] == NULL) {
  424. cam_warn(cam, "Failed to allocate DMA buffer\n");
  425. break;
  426. }
  427. /* For debug, remove eventually */
  428. memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
  429. (cam->nbufs)++;
  430. }
  431. switch (cam->nbufs) {
  432. case 1:
  433. dma_free_coherent(cam->dev, cam->dma_buf_size,
  434. cam->dma_bufs[0], cam->dma_handles[0]);
  435. cam->nbufs = 0;
  436. case 0:
  437. cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
  438. return -ENOMEM;
  439. case 2:
  440. if (n_dma_bufs > 2)
  441. cam_warn(cam, "Will limp along with only 2 buffers\n");
  442. break;
  443. }
  444. return 0;
  445. }
  446. static void mcam_free_dma_bufs(struct mcam_camera *cam)
  447. {
  448. int i;
  449. for (i = 0; i < cam->nbufs; i++) {
  450. dma_free_coherent(cam->dev, cam->dma_buf_size,
  451. cam->dma_bufs[i], cam->dma_handles[i]);
  452. cam->dma_bufs[i] = NULL;
  453. }
  454. cam->nbufs = 0;
  455. }
  456. /* ----------------------------------------------------------------------- */
  457. /*
  458. * Here starts the V4L2 interface code.
  459. */
  460. /*
  461. * Read an image from the device.
  462. */
  463. static ssize_t mcam_deliver_buffer(struct mcam_camera *cam,
  464. char __user *buffer, size_t len, loff_t *pos)
  465. {
  466. int bufno;
  467. unsigned long flags;
  468. spin_lock_irqsave(&cam->dev_lock, flags);
  469. if (cam->next_buf < 0) {
  470. cam_err(cam, "deliver_buffer: No next buffer\n");
  471. spin_unlock_irqrestore(&cam->dev_lock, flags);
  472. return -EIO;
  473. }
  474. bufno = cam->next_buf;
  475. clear_bit(bufno, &cam->flags);
  476. if (++(cam->next_buf) >= cam->nbufs)
  477. cam->next_buf = 0;
  478. if (!test_bit(cam->next_buf, &cam->flags))
  479. cam->next_buf = -1;
  480. cam->specframes = 0;
  481. spin_unlock_irqrestore(&cam->dev_lock, flags);
  482. if (len > cam->pix_format.sizeimage)
  483. len = cam->pix_format.sizeimage;
  484. if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
  485. return -EFAULT;
  486. (*pos) += len;
  487. return len;
  488. }
  489. /*
  490. * Get everything ready, and start grabbing frames.
  491. */
  492. static int mcam_read_setup(struct mcam_camera *cam, enum mcam_state state)
  493. {
  494. int ret;
  495. unsigned long flags;
  496. /*
  497. * Configuration. If we still don't have DMA buffers,
  498. * make one last, desperate attempt.
  499. */
  500. if (cam->nbufs == 0)
  501. if (mcam_alloc_dma_bufs(cam, 0))
  502. return -ENOMEM;
  503. if (mcam_needs_config(cam)) {
  504. mcam_cam_configure(cam);
  505. ret = mcam_ctlr_configure(cam);
  506. if (ret)
  507. return ret;
  508. }
  509. /*
  510. * Turn it loose.
  511. */
  512. spin_lock_irqsave(&cam->dev_lock, flags);
  513. mcam_reset_buffers(cam);
  514. mcam_ctlr_irq_enable(cam);
  515. cam->state = state;
  516. mcam_ctlr_start(cam);
  517. spin_unlock_irqrestore(&cam->dev_lock, flags);
  518. return 0;
  519. }
  520. static ssize_t mcam_v4l_read(struct file *filp,
  521. char __user *buffer, size_t len, loff_t *pos)
  522. {
  523. struct mcam_camera *cam = filp->private_data;
  524. int ret = 0;
  525. /*
  526. * Perhaps we're in speculative read mode and already
  527. * have data?
  528. */
  529. mutex_lock(&cam->s_mutex);
  530. if (cam->state == S_SPECREAD) {
  531. if (cam->next_buf >= 0) {
  532. ret = mcam_deliver_buffer(cam, buffer, len, pos);
  533. if (ret != 0)
  534. goto out_unlock;
  535. }
  536. } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
  537. ret = -EIO;
  538. goto out_unlock;
  539. } else if (cam->state != S_IDLE) {
  540. ret = -EBUSY;
  541. goto out_unlock;
  542. }
  543. /*
  544. * v4l2: multiple processes can open the device, but only
  545. * one gets to grab data from it.
  546. */
  547. if (cam->owner && cam->owner != filp) {
  548. ret = -EBUSY;
  549. goto out_unlock;
  550. }
  551. cam->owner = filp;
  552. /*
  553. * Do setup if need be.
  554. */
  555. if (cam->state != S_SPECREAD) {
  556. ret = mcam_read_setup(cam, S_SINGLEREAD);
  557. if (ret)
  558. goto out_unlock;
  559. }
  560. /*
  561. * Wait for something to happen. This should probably
  562. * be interruptible (FIXME).
  563. */
  564. wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
  565. if (cam->next_buf < 0) {
  566. cam_err(cam, "read() operation timed out\n");
  567. mcam_ctlr_stop_dma(cam);
  568. ret = -EIO;
  569. goto out_unlock;
  570. }
  571. /*
  572. * Give them their data and we should be done.
  573. */
  574. ret = mcam_deliver_buffer(cam, buffer, len, pos);
  575. out_unlock:
  576. mutex_unlock(&cam->s_mutex);
  577. return ret;
  578. }
  579. /*
  580. * Streaming I/O support.
  581. */
  582. static int mcam_vidioc_streamon(struct file *filp, void *priv,
  583. enum v4l2_buf_type type)
  584. {
  585. struct mcam_camera *cam = filp->private_data;
  586. int ret = -EINVAL;
  587. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  588. goto out;
  589. mutex_lock(&cam->s_mutex);
  590. if (cam->state != S_IDLE || cam->n_sbufs == 0)
  591. goto out_unlock;
  592. cam->sequence = 0;
  593. ret = mcam_read_setup(cam, S_STREAMING);
  594. out_unlock:
  595. mutex_unlock(&cam->s_mutex);
  596. out:
  597. return ret;
  598. }
  599. static int mcam_vidioc_streamoff(struct file *filp, void *priv,
  600. enum v4l2_buf_type type)
  601. {
  602. struct mcam_camera *cam = filp->private_data;
  603. int ret = -EINVAL;
  604. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  605. goto out;
  606. mutex_lock(&cam->s_mutex);
  607. if (cam->state != S_STREAMING)
  608. goto out_unlock;
  609. mcam_ctlr_stop_dma(cam);
  610. ret = 0;
  611. out_unlock:
  612. mutex_unlock(&cam->s_mutex);
  613. out:
  614. return ret;
  615. }
  616. static int mcam_setup_siobuf(struct mcam_camera *cam, int index)
  617. {
  618. struct mcam_sio_buffer *buf = cam->sb_bufs + index;
  619. INIT_LIST_HEAD(&buf->list);
  620. buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
  621. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  622. if (buf->buffer == NULL)
  623. return -ENOMEM;
  624. buf->mapcount = 0;
  625. buf->cam = cam;
  626. buf->v4lbuf.index = index;
  627. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  628. buf->v4lbuf.field = V4L2_FIELD_NONE;
  629. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  630. /*
  631. * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
  632. * just uses the length times the index, but the spec warns
  633. * against doing just that - vma merging problems. So we
  634. * leave a gap between each pair of buffers.
  635. */
  636. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  637. return 0;
  638. }
  639. static int mcam_free_sio_buffers(struct mcam_camera *cam)
  640. {
  641. int i;
  642. /*
  643. * If any buffers are mapped, we cannot free them at all.
  644. */
  645. for (i = 0; i < cam->n_sbufs; i++)
  646. if (cam->sb_bufs[i].mapcount > 0)
  647. return -EBUSY;
  648. /*
  649. * OK, let's do it.
  650. */
  651. for (i = 0; i < cam->n_sbufs; i++)
  652. vfree(cam->sb_bufs[i].buffer);
  653. cam->n_sbufs = 0;
  654. kfree(cam->sb_bufs);
  655. cam->sb_bufs = NULL;
  656. INIT_LIST_HEAD(&cam->sb_avail);
  657. INIT_LIST_HEAD(&cam->sb_full);
  658. return 0;
  659. }
  660. static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
  661. struct v4l2_requestbuffers *req)
  662. {
  663. struct mcam_camera *cam = filp->private_data;
  664. int ret = 0; /* Silence warning */
  665. /*
  666. * Make sure it's something we can do. User pointers could be
  667. * implemented without great pain, but that's not been done yet.
  668. */
  669. if (req->memory != V4L2_MEMORY_MMAP)
  670. return -EINVAL;
  671. /*
  672. * If they ask for zero buffers, they really want us to stop streaming
  673. * (if it's happening) and free everything. Should we check owner?
  674. */
  675. mutex_lock(&cam->s_mutex);
  676. if (req->count == 0) {
  677. if (cam->state == S_STREAMING)
  678. mcam_ctlr_stop_dma(cam);
  679. ret = mcam_free_sio_buffers(cam);
  680. goto out;
  681. }
  682. /*
  683. * Device needs to be idle and working. We *could* try to do the
  684. * right thing in S_SPECREAD by shutting things down, but it
  685. * probably doesn't matter.
  686. */
  687. if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
  688. ret = -EBUSY;
  689. goto out;
  690. }
  691. cam->owner = filp;
  692. if (req->count < min_buffers)
  693. req->count = min_buffers;
  694. else if (req->count > max_buffers)
  695. req->count = max_buffers;
  696. if (cam->n_sbufs > 0) {
  697. ret = mcam_free_sio_buffers(cam);
  698. if (ret)
  699. goto out;
  700. }
  701. cam->sb_bufs = kzalloc(req->count*sizeof(struct mcam_sio_buffer),
  702. GFP_KERNEL);
  703. if (cam->sb_bufs == NULL) {
  704. ret = -ENOMEM;
  705. goto out;
  706. }
  707. for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
  708. ret = mcam_setup_siobuf(cam, cam->n_sbufs);
  709. if (ret)
  710. break;
  711. }
  712. if (cam->n_sbufs == 0) /* no luck at all - ret already set */
  713. kfree(cam->sb_bufs);
  714. req->count = cam->n_sbufs; /* In case of partial success */
  715. out:
  716. mutex_unlock(&cam->s_mutex);
  717. return ret;
  718. }
  719. static int mcam_vidioc_querybuf(struct file *filp, void *priv,
  720. struct v4l2_buffer *buf)
  721. {
  722. struct mcam_camera *cam = filp->private_data;
  723. int ret = -EINVAL;
  724. mutex_lock(&cam->s_mutex);
  725. if (buf->index >= cam->n_sbufs)
  726. goto out;
  727. *buf = cam->sb_bufs[buf->index].v4lbuf;
  728. ret = 0;
  729. out:
  730. mutex_unlock(&cam->s_mutex);
  731. return ret;
  732. }
  733. static int mcam_vidioc_qbuf(struct file *filp, void *priv,
  734. struct v4l2_buffer *buf)
  735. {
  736. struct mcam_camera *cam = filp->private_data;
  737. struct mcam_sio_buffer *sbuf;
  738. int ret = -EINVAL;
  739. unsigned long flags;
  740. mutex_lock(&cam->s_mutex);
  741. if (buf->index >= cam->n_sbufs)
  742. goto out;
  743. sbuf = cam->sb_bufs + buf->index;
  744. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
  745. ret = 0; /* Already queued?? */
  746. goto out;
  747. }
  748. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
  749. /* Spec doesn't say anything, seems appropriate tho */
  750. ret = -EBUSY;
  751. goto out;
  752. }
  753. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  754. spin_lock_irqsave(&cam->dev_lock, flags);
  755. list_add(&sbuf->list, &cam->sb_avail);
  756. spin_unlock_irqrestore(&cam->dev_lock, flags);
  757. ret = 0;
  758. out:
  759. mutex_unlock(&cam->s_mutex);
  760. return ret;
  761. }
  762. static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
  763. struct v4l2_buffer *buf)
  764. {
  765. struct mcam_camera *cam = filp->private_data;
  766. struct mcam_sio_buffer *sbuf;
  767. int ret = -EINVAL;
  768. unsigned long flags;
  769. mutex_lock(&cam->s_mutex);
  770. if (cam->state != S_STREAMING)
  771. goto out_unlock;
  772. if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
  773. ret = -EAGAIN;
  774. goto out_unlock;
  775. }
  776. while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
  777. mutex_unlock(&cam->s_mutex);
  778. if (wait_event_interruptible(cam->iowait,
  779. !list_empty(&cam->sb_full))) {
  780. ret = -ERESTARTSYS;
  781. goto out;
  782. }
  783. mutex_lock(&cam->s_mutex);
  784. }
  785. if (cam->state != S_STREAMING)
  786. ret = -EINTR;
  787. else {
  788. spin_lock_irqsave(&cam->dev_lock, flags);
  789. /* Should probably recheck !list_empty() here */
  790. sbuf = list_entry(cam->sb_full.next,
  791. struct mcam_sio_buffer, list);
  792. list_del_init(&sbuf->list);
  793. spin_unlock_irqrestore(&cam->dev_lock, flags);
  794. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  795. *buf = sbuf->v4lbuf;
  796. ret = 0;
  797. }
  798. out_unlock:
  799. mutex_unlock(&cam->s_mutex);
  800. out:
  801. return ret;
  802. }
  803. static void mcam_v4l_vm_open(struct vm_area_struct *vma)
  804. {
  805. struct mcam_sio_buffer *sbuf = vma->vm_private_data;
  806. /*
  807. * Locking: done under mmap_sem, so we don't need to
  808. * go back to the camera lock here.
  809. */
  810. sbuf->mapcount++;
  811. }
  812. static void mcam_v4l_vm_close(struct vm_area_struct *vma)
  813. {
  814. struct mcam_sio_buffer *sbuf = vma->vm_private_data;
  815. mutex_lock(&sbuf->cam->s_mutex);
  816. sbuf->mapcount--;
  817. /* Docs say we should stop I/O too... */
  818. if (sbuf->mapcount == 0)
  819. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  820. mutex_unlock(&sbuf->cam->s_mutex);
  821. }
  822. static const struct vm_operations_struct mcam_v4l_vm_ops = {
  823. .open = mcam_v4l_vm_open,
  824. .close = mcam_v4l_vm_close
  825. };
  826. static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
  827. {
  828. struct mcam_camera *cam = filp->private_data;
  829. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  830. int ret = -EINVAL;
  831. int i;
  832. struct mcam_sio_buffer *sbuf = NULL;
  833. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  834. return -EINVAL;
  835. /*
  836. * Find the buffer they are looking for.
  837. */
  838. mutex_lock(&cam->s_mutex);
  839. for (i = 0; i < cam->n_sbufs; i++)
  840. if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
  841. sbuf = cam->sb_bufs + i;
  842. break;
  843. }
  844. if (sbuf == NULL)
  845. goto out;
  846. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  847. if (ret)
  848. goto out;
  849. vma->vm_flags |= VM_DONTEXPAND;
  850. vma->vm_private_data = sbuf;
  851. vma->vm_ops = &mcam_v4l_vm_ops;
  852. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  853. mcam_v4l_vm_open(vma);
  854. ret = 0;
  855. out:
  856. mutex_unlock(&cam->s_mutex);
  857. return ret;
  858. }
  859. static int mcam_v4l_open(struct file *filp)
  860. {
  861. struct mcam_camera *cam = video_drvdata(filp);
  862. filp->private_data = cam;
  863. mutex_lock(&cam->s_mutex);
  864. if (cam->users == 0) {
  865. mcam_ctlr_power_up(cam);
  866. __mcam_cam_reset(cam);
  867. mcam_set_config_needed(cam, 1);
  868. /* FIXME make sure this is complete */
  869. }
  870. (cam->users)++;
  871. mutex_unlock(&cam->s_mutex);
  872. return 0;
  873. }
  874. static int mcam_v4l_release(struct file *filp)
  875. {
  876. struct mcam_camera *cam = filp->private_data;
  877. mutex_lock(&cam->s_mutex);
  878. (cam->users)--;
  879. if (filp == cam->owner) {
  880. mcam_ctlr_stop_dma(cam);
  881. mcam_free_sio_buffers(cam);
  882. cam->owner = NULL;
  883. }
  884. if (cam->users == 0) {
  885. mcam_ctlr_power_down(cam);
  886. if (alloc_bufs_at_read)
  887. mcam_free_dma_bufs(cam);
  888. }
  889. mutex_unlock(&cam->s_mutex);
  890. return 0;
  891. }
  892. static unsigned int mcam_v4l_poll(struct file *filp,
  893. struct poll_table_struct *pt)
  894. {
  895. struct mcam_camera *cam = filp->private_data;
  896. poll_wait(filp, &cam->iowait, pt);
  897. if (cam->next_buf >= 0)
  898. return POLLIN | POLLRDNORM;
  899. return 0;
  900. }
  901. static int mcam_vidioc_queryctrl(struct file *filp, void *priv,
  902. struct v4l2_queryctrl *qc)
  903. {
  904. struct mcam_camera *cam = priv;
  905. int ret;
  906. mutex_lock(&cam->s_mutex);
  907. ret = sensor_call(cam, core, queryctrl, qc);
  908. mutex_unlock(&cam->s_mutex);
  909. return ret;
  910. }
  911. static int mcam_vidioc_g_ctrl(struct file *filp, void *priv,
  912. struct v4l2_control *ctrl)
  913. {
  914. struct mcam_camera *cam = priv;
  915. int ret;
  916. mutex_lock(&cam->s_mutex);
  917. ret = sensor_call(cam, core, g_ctrl, ctrl);
  918. mutex_unlock(&cam->s_mutex);
  919. return ret;
  920. }
  921. static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
  922. struct v4l2_control *ctrl)
  923. {
  924. struct mcam_camera *cam = priv;
  925. int ret;
  926. mutex_lock(&cam->s_mutex);
  927. ret = sensor_call(cam, core, s_ctrl, ctrl);
  928. mutex_unlock(&cam->s_mutex);
  929. return ret;
  930. }
  931. static int mcam_vidioc_querycap(struct file *file, void *priv,
  932. struct v4l2_capability *cap)
  933. {
  934. strcpy(cap->driver, "marvell_ccic");
  935. strcpy(cap->card, "marvell_ccic");
  936. cap->version = 1;
  937. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  938. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  939. return 0;
  940. }
  941. /*
  942. * The default format we use until somebody says otherwise.
  943. */
  944. static const struct v4l2_pix_format mcam_def_pix_format = {
  945. .width = VGA_WIDTH,
  946. .height = VGA_HEIGHT,
  947. .pixelformat = V4L2_PIX_FMT_YUYV,
  948. .field = V4L2_FIELD_NONE,
  949. .bytesperline = VGA_WIDTH*2,
  950. .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
  951. };
  952. static const enum v4l2_mbus_pixelcode mcam_def_mbus_code =
  953. V4L2_MBUS_FMT_YUYV8_2X8;
  954. static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
  955. void *priv, struct v4l2_fmtdesc *fmt)
  956. {
  957. if (fmt->index >= N_MCAM_FMTS)
  958. return -EINVAL;
  959. strlcpy(fmt->description, mcam_formats[fmt->index].desc,
  960. sizeof(fmt->description));
  961. fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
  962. return 0;
  963. }
  964. static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
  965. struct v4l2_format *fmt)
  966. {
  967. struct mcam_camera *cam = priv;
  968. struct mcam_format_struct *f;
  969. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  970. struct v4l2_mbus_framefmt mbus_fmt;
  971. int ret;
  972. f = mcam_find_format(pix->pixelformat);
  973. pix->pixelformat = f->pixelformat;
  974. v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
  975. mutex_lock(&cam->s_mutex);
  976. ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
  977. mutex_unlock(&cam->s_mutex);
  978. v4l2_fill_pix_format(pix, &mbus_fmt);
  979. pix->bytesperline = pix->width * f->bpp;
  980. pix->sizeimage = pix->height * pix->bytesperline;
  981. return ret;
  982. }
  983. static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
  984. struct v4l2_format *fmt)
  985. {
  986. struct mcam_camera *cam = priv;
  987. struct mcam_format_struct *f;
  988. int ret;
  989. /*
  990. * Can't do anything if the device is not idle
  991. * Also can't if there are streaming buffers in place.
  992. */
  993. if (cam->state != S_IDLE || cam->n_sbufs > 0)
  994. return -EBUSY;
  995. f = mcam_find_format(fmt->fmt.pix.pixelformat);
  996. /*
  997. * See if the formatting works in principle.
  998. */
  999. ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
  1000. if (ret)
  1001. return ret;
  1002. /*
  1003. * Now we start to change things for real, so let's do it
  1004. * under lock.
  1005. */
  1006. mutex_lock(&cam->s_mutex);
  1007. cam->pix_format = fmt->fmt.pix;
  1008. cam->mbus_code = f->mbus_code;
  1009. /*
  1010. * Make sure we have appropriate DMA buffers.
  1011. */
  1012. ret = -ENOMEM;
  1013. if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
  1014. mcam_free_dma_bufs(cam);
  1015. if (cam->nbufs == 0) {
  1016. if (mcam_alloc_dma_bufs(cam, 0))
  1017. goto out;
  1018. }
  1019. /*
  1020. * It looks like this might work, so let's program the sensor.
  1021. */
  1022. ret = mcam_cam_configure(cam);
  1023. if (!ret)
  1024. ret = mcam_ctlr_configure(cam);
  1025. out:
  1026. mutex_unlock(&cam->s_mutex);
  1027. return ret;
  1028. }
  1029. /*
  1030. * Return our stored notion of how the camera is/should be configured.
  1031. * The V4l2 spec wants us to be smarter, and actually get this from
  1032. * the camera (and not mess with it at open time). Someday.
  1033. */
  1034. static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
  1035. struct v4l2_format *f)
  1036. {
  1037. struct mcam_camera *cam = priv;
  1038. f->fmt.pix = cam->pix_format;
  1039. return 0;
  1040. }
  1041. /*
  1042. * We only have one input - the sensor - so minimize the nonsense here.
  1043. */
  1044. static int mcam_vidioc_enum_input(struct file *filp, void *priv,
  1045. struct v4l2_input *input)
  1046. {
  1047. if (input->index != 0)
  1048. return -EINVAL;
  1049. input->type = V4L2_INPUT_TYPE_CAMERA;
  1050. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  1051. strcpy(input->name, "Camera");
  1052. return 0;
  1053. }
  1054. static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  1055. {
  1056. *i = 0;
  1057. return 0;
  1058. }
  1059. static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  1060. {
  1061. if (i != 0)
  1062. return -EINVAL;
  1063. return 0;
  1064. }
  1065. /* from vivi.c */
  1066. static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  1067. {
  1068. return 0;
  1069. }
  1070. /*
  1071. * G/S_PARM. Most of this is done by the sensor, but we are
  1072. * the level which controls the number of read buffers.
  1073. */
  1074. static int mcam_vidioc_g_parm(struct file *filp, void *priv,
  1075. struct v4l2_streamparm *parms)
  1076. {
  1077. struct mcam_camera *cam = priv;
  1078. int ret;
  1079. mutex_lock(&cam->s_mutex);
  1080. ret = sensor_call(cam, video, g_parm, parms);
  1081. mutex_unlock(&cam->s_mutex);
  1082. parms->parm.capture.readbuffers = n_dma_bufs;
  1083. return ret;
  1084. }
  1085. static int mcam_vidioc_s_parm(struct file *filp, void *priv,
  1086. struct v4l2_streamparm *parms)
  1087. {
  1088. struct mcam_camera *cam = priv;
  1089. int ret;
  1090. mutex_lock(&cam->s_mutex);
  1091. ret = sensor_call(cam, video, s_parm, parms);
  1092. mutex_unlock(&cam->s_mutex);
  1093. parms->parm.capture.readbuffers = n_dma_bufs;
  1094. return ret;
  1095. }
  1096. static int mcam_vidioc_g_chip_ident(struct file *file, void *priv,
  1097. struct v4l2_dbg_chip_ident *chip)
  1098. {
  1099. struct mcam_camera *cam = priv;
  1100. chip->ident = V4L2_IDENT_NONE;
  1101. chip->revision = 0;
  1102. if (v4l2_chip_match_host(&chip->match)) {
  1103. chip->ident = cam->chip_id;
  1104. return 0;
  1105. }
  1106. return sensor_call(cam, core, g_chip_ident, chip);
  1107. }
  1108. static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
  1109. struct v4l2_frmsizeenum *sizes)
  1110. {
  1111. struct mcam_camera *cam = priv;
  1112. int ret;
  1113. mutex_lock(&cam->s_mutex);
  1114. ret = sensor_call(cam, video, enum_framesizes, sizes);
  1115. mutex_unlock(&cam->s_mutex);
  1116. return ret;
  1117. }
  1118. static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
  1119. struct v4l2_frmivalenum *interval)
  1120. {
  1121. struct mcam_camera *cam = priv;
  1122. int ret;
  1123. mutex_lock(&cam->s_mutex);
  1124. ret = sensor_call(cam, video, enum_frameintervals, interval);
  1125. mutex_unlock(&cam->s_mutex);
  1126. return ret;
  1127. }
  1128. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1129. static int mcam_vidioc_g_register(struct file *file, void *priv,
  1130. struct v4l2_dbg_register *reg)
  1131. {
  1132. struct mcam_camera *cam = priv;
  1133. if (v4l2_chip_match_host(&reg->match)) {
  1134. reg->val = mcam_reg_read(cam, reg->reg);
  1135. reg->size = 4;
  1136. return 0;
  1137. }
  1138. return sensor_call(cam, core, g_register, reg);
  1139. }
  1140. static int mcam_vidioc_s_register(struct file *file, void *priv,
  1141. struct v4l2_dbg_register *reg)
  1142. {
  1143. struct mcam_camera *cam = priv;
  1144. if (v4l2_chip_match_host(&reg->match)) {
  1145. mcam_reg_write(cam, reg->reg, reg->val);
  1146. return 0;
  1147. }
  1148. return sensor_call(cam, core, s_register, reg);
  1149. }
  1150. #endif
  1151. /*
  1152. * This template device holds all of those v4l2 methods; we
  1153. * clone it for specific real devices.
  1154. */
  1155. static const struct v4l2_file_operations mcam_v4l_fops = {
  1156. .owner = THIS_MODULE,
  1157. .open = mcam_v4l_open,
  1158. .release = mcam_v4l_release,
  1159. .read = mcam_v4l_read,
  1160. .poll = mcam_v4l_poll,
  1161. .mmap = mcam_v4l_mmap,
  1162. .unlocked_ioctl = video_ioctl2,
  1163. };
  1164. static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
  1165. .vidioc_querycap = mcam_vidioc_querycap,
  1166. .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
  1167. .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
  1168. .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
  1169. .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
  1170. .vidioc_enum_input = mcam_vidioc_enum_input,
  1171. .vidioc_g_input = mcam_vidioc_g_input,
  1172. .vidioc_s_input = mcam_vidioc_s_input,
  1173. .vidioc_s_std = mcam_vidioc_s_std,
  1174. .vidioc_reqbufs = mcam_vidioc_reqbufs,
  1175. .vidioc_querybuf = mcam_vidioc_querybuf,
  1176. .vidioc_qbuf = mcam_vidioc_qbuf,
  1177. .vidioc_dqbuf = mcam_vidioc_dqbuf,
  1178. .vidioc_streamon = mcam_vidioc_streamon,
  1179. .vidioc_streamoff = mcam_vidioc_streamoff,
  1180. .vidioc_queryctrl = mcam_vidioc_queryctrl,
  1181. .vidioc_g_ctrl = mcam_vidioc_g_ctrl,
  1182. .vidioc_s_ctrl = mcam_vidioc_s_ctrl,
  1183. .vidioc_g_parm = mcam_vidioc_g_parm,
  1184. .vidioc_s_parm = mcam_vidioc_s_parm,
  1185. .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
  1186. .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
  1187. .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident,
  1188. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1189. .vidioc_g_register = mcam_vidioc_g_register,
  1190. .vidioc_s_register = mcam_vidioc_s_register,
  1191. #endif
  1192. };
  1193. static struct video_device mcam_v4l_template = {
  1194. .name = "mcam",
  1195. .tvnorms = V4L2_STD_NTSC_M,
  1196. .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
  1197. .fops = &mcam_v4l_fops,
  1198. .ioctl_ops = &mcam_v4l_ioctl_ops,
  1199. .release = video_device_release_empty,
  1200. };
  1201. /* ---------------------------------------------------------------------- */
  1202. /*
  1203. * Interrupt handler stuff
  1204. */
  1205. static void mcam_frame_tasklet(unsigned long data)
  1206. {
  1207. struct mcam_camera *cam = (struct mcam_camera *) data;
  1208. int i;
  1209. unsigned long flags;
  1210. struct mcam_sio_buffer *sbuf;
  1211. spin_lock_irqsave(&cam->dev_lock, flags);
  1212. for (i = 0; i < cam->nbufs; i++) {
  1213. int bufno = cam->next_buf;
  1214. if (bufno < 0) { /* "will never happen" */
  1215. cam_err(cam, "No valid bufs in tasklet!\n");
  1216. break;
  1217. }
  1218. if (++(cam->next_buf) >= cam->nbufs)
  1219. cam->next_buf = 0;
  1220. if (!test_bit(bufno, &cam->flags))
  1221. continue;
  1222. if (list_empty(&cam->sb_avail))
  1223. break; /* Leave it valid, hope for better later */
  1224. clear_bit(bufno, &cam->flags);
  1225. sbuf = list_entry(cam->sb_avail.next,
  1226. struct mcam_sio_buffer, list);
  1227. /*
  1228. * Drop the lock during the big copy. This *should* be safe...
  1229. */
  1230. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1231. memcpy(sbuf->buffer, cam->dma_bufs[bufno],
  1232. cam->pix_format.sizeimage);
  1233. sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
  1234. sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
  1235. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1236. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1237. spin_lock_irqsave(&cam->dev_lock, flags);
  1238. list_move_tail(&sbuf->list, &cam->sb_full);
  1239. }
  1240. if (!list_empty(&cam->sb_full))
  1241. wake_up(&cam->iowait);
  1242. spin_unlock_irqrestore(&cam->dev_lock, flags);
  1243. }
  1244. static void mcam_frame_complete(struct mcam_camera *cam, int frame)
  1245. {
  1246. /*
  1247. * Basic frame housekeeping.
  1248. */
  1249. if (test_bit(frame, &cam->flags) && printk_ratelimit())
  1250. cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
  1251. set_bit(frame, &cam->flags);
  1252. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1253. if (cam->next_buf < 0)
  1254. cam->next_buf = frame;
  1255. cam->buf_seq[frame] = ++(cam->sequence);
  1256. switch (cam->state) {
  1257. /*
  1258. * If in single read mode, try going speculative.
  1259. */
  1260. case S_SINGLEREAD:
  1261. cam->state = S_SPECREAD;
  1262. cam->specframes = 0;
  1263. wake_up(&cam->iowait);
  1264. break;
  1265. /*
  1266. * If we are already doing speculative reads, and nobody is
  1267. * reading them, just stop.
  1268. */
  1269. case S_SPECREAD:
  1270. if (++(cam->specframes) >= cam->nbufs) {
  1271. mcam_ctlr_stop(cam);
  1272. mcam_ctlr_irq_disable(cam);
  1273. cam->state = S_IDLE;
  1274. }
  1275. wake_up(&cam->iowait);
  1276. break;
  1277. /*
  1278. * For the streaming case, we defer the real work to the
  1279. * camera tasklet.
  1280. *
  1281. * FIXME: if the application is not consuming the buffers,
  1282. * we should eventually put things on hold and restart in
  1283. * vidioc_dqbuf().
  1284. */
  1285. case S_STREAMING:
  1286. tasklet_schedule(&cam->s_tasklet);
  1287. break;
  1288. default:
  1289. cam_err(cam, "Frame interrupt in non-operational state\n");
  1290. break;
  1291. }
  1292. }
  1293. int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
  1294. {
  1295. unsigned int frame, handled = 0;
  1296. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1297. /*
  1298. * Handle any frame completions. There really should
  1299. * not be more than one of these, or we have fallen
  1300. * far behind.
  1301. */
  1302. for (frame = 0; frame < cam->nbufs; frame++)
  1303. if (irqs & (IRQ_EOF0 << frame)) {
  1304. mcam_frame_complete(cam, frame);
  1305. handled = 1;
  1306. }
  1307. /*
  1308. * If a frame starts, note that we have DMA active. This
  1309. * code assumes that we won't get multiple frame interrupts
  1310. * at once; may want to rethink that.
  1311. */
  1312. if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
  1313. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1314. handled = 1;
  1315. }
  1316. return handled;
  1317. }
  1318. /*
  1319. * Registration and such.
  1320. */
  1321. static struct ov7670_config sensor_cfg = {
  1322. /*
  1323. * Exclude QCIF mode, because it only captures a tiny portion
  1324. * of the sensor FOV
  1325. */
  1326. .min_width = 320,
  1327. .min_height = 240,
  1328. };
  1329. int mccic_register(struct mcam_camera *cam)
  1330. {
  1331. struct i2c_board_info ov7670_info = {
  1332. .type = "ov7670",
  1333. .addr = 0x42,
  1334. .platform_data = &sensor_cfg,
  1335. };
  1336. int ret;
  1337. /*
  1338. * Register with V4L
  1339. */
  1340. ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
  1341. if (ret)
  1342. return ret;
  1343. mutex_init(&cam->s_mutex);
  1344. cam->state = S_NOTREADY;
  1345. mcam_set_config_needed(cam, 1);
  1346. init_waitqueue_head(&cam->iowait);
  1347. cam->pix_format = mcam_def_pix_format;
  1348. cam->mbus_code = mcam_def_mbus_code;
  1349. INIT_LIST_HEAD(&cam->dev_list);
  1350. INIT_LIST_HEAD(&cam->sb_avail);
  1351. INIT_LIST_HEAD(&cam->sb_full);
  1352. tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam);
  1353. mcam_ctlr_init(cam);
  1354. /*
  1355. * Try to find the sensor.
  1356. */
  1357. sensor_cfg.clock_speed = cam->clock_speed;
  1358. sensor_cfg.use_smbus = cam->use_smbus;
  1359. cam->sensor_addr = ov7670_info.addr;
  1360. cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
  1361. &cam->i2c_adapter, &ov7670_info, NULL);
  1362. if (cam->sensor == NULL) {
  1363. ret = -ENODEV;
  1364. goto out_unregister;
  1365. }
  1366. ret = mcam_cam_init(cam);
  1367. if (ret)
  1368. goto out_unregister;
  1369. /*
  1370. * Get the v4l2 setup done.
  1371. */
  1372. mutex_lock(&cam->s_mutex);
  1373. cam->vdev = mcam_v4l_template;
  1374. cam->vdev.debug = 0;
  1375. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1376. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1377. if (ret)
  1378. goto out;
  1379. video_set_drvdata(&cam->vdev, cam);
  1380. /*
  1381. * If so requested, try to get our DMA buffers now.
  1382. */
  1383. if (!alloc_bufs_at_read) {
  1384. if (mcam_alloc_dma_bufs(cam, 1))
  1385. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1386. " will try again later.");
  1387. }
  1388. out:
  1389. mutex_unlock(&cam->s_mutex);
  1390. return ret;
  1391. out_unregister:
  1392. v4l2_device_unregister(&cam->v4l2_dev);
  1393. return ret;
  1394. }
  1395. void mccic_shutdown(struct mcam_camera *cam)
  1396. {
  1397. if (cam->users > 0)
  1398. cam_warn(cam, "Removing a device with users!\n");
  1399. if (cam->n_sbufs > 0)
  1400. /* What if they are still mapped? Shouldn't be, but... */
  1401. mcam_free_sio_buffers(cam);
  1402. mcam_ctlr_stop_dma(cam);
  1403. mcam_ctlr_power_down(cam);
  1404. mcam_free_dma_bufs(cam);
  1405. video_unregister_device(&cam->vdev);
  1406. v4l2_device_unregister(&cam->v4l2_dev);
  1407. }
  1408. /*
  1409. * Power management
  1410. */
  1411. #ifdef CONFIG_PM
  1412. void mccic_suspend(struct mcam_camera *cam)
  1413. {
  1414. enum mcam_state cstate = cam->state;
  1415. mcam_ctlr_stop_dma(cam);
  1416. mcam_ctlr_power_down(cam);
  1417. cam->state = cstate;
  1418. }
  1419. int mccic_resume(struct mcam_camera *cam)
  1420. {
  1421. int ret = 0;
  1422. mutex_lock(&cam->s_mutex);
  1423. if (cam->users > 0) {
  1424. mcam_ctlr_power_up(cam);
  1425. __mcam_cam_reset(cam);
  1426. } else {
  1427. mcam_ctlr_power_down(cam);
  1428. }
  1429. mutex_unlock(&cam->s_mutex);
  1430. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  1431. if (cam->state == S_SPECREAD)
  1432. cam->state = S_IDLE; /* Don't bother restarting */
  1433. else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
  1434. ret = mcam_read_setup(cam, cam->state);
  1435. return ret;
  1436. }
  1437. #endif /* CONFIG_PM */