mcam-core.c 40 KB

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