mcam-core.c 40 KB

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