soc_camera.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. * camera image capture (abstract) bus driver
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  5. *
  6. * This driver provides an interface between platform-specific camera
  7. * busses and camera devices. It should be used if the camera is
  8. * connected not over a "proper" bus like PCI or USB, but over a
  9. * special bus, like, for example, the Quick Capture interface on PXA270
  10. * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
  11. * It can handle multiple cameras and / or multiple busses, which can
  12. * be used, e.g., in stereo-vision applications.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/device.h>
  21. #include <linux/list.h>
  22. #include <linux/err.h>
  23. #include <linux/mutex.h>
  24. #include <linux/vmalloc.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <media/v4l2-dev.h>
  28. #include <media/videobuf-core.h>
  29. #include <media/soc_camera.h>
  30. /* Default to VGA resolution */
  31. #define DEFAULT_WIDTH 640
  32. #define DEFAULT_HEIGHT 480
  33. static LIST_HEAD(hosts);
  34. static LIST_HEAD(devices);
  35. static DEFINE_MUTEX(list_lock);
  36. const struct soc_camera_data_format *soc_camera_format_by_fourcc(
  37. struct soc_camera_device *icd, unsigned int fourcc)
  38. {
  39. unsigned int i;
  40. for (i = 0; i < icd->num_formats; i++)
  41. if (icd->formats[i].fourcc == fourcc)
  42. return icd->formats + i;
  43. return NULL;
  44. }
  45. EXPORT_SYMBOL(soc_camera_format_by_fourcc);
  46. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  47. struct soc_camera_device *icd, unsigned int fourcc)
  48. {
  49. unsigned int i;
  50. for (i = 0; i < icd->num_user_formats; i++)
  51. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  52. return icd->user_formats + i;
  53. return NULL;
  54. }
  55. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  56. /**
  57. * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  58. * @icl: camera platform parameters
  59. * @flags: flags to be inverted according to platform configuration
  60. * @return: resulting flags
  61. */
  62. unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
  63. unsigned long flags)
  64. {
  65. unsigned long f;
  66. /* If only one of the two polarities is supported, switch to the opposite */
  67. if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  68. f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
  69. if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
  70. flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
  71. }
  72. if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  73. f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
  74. if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
  75. flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
  76. }
  77. if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
  78. f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
  79. if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
  80. flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
  81. }
  82. return flags;
  83. }
  84. EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
  85. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  86. struct v4l2_format *f)
  87. {
  88. struct soc_camera_file *icf = file->private_data;
  89. struct soc_camera_device *icd = icf->icd;
  90. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  91. WARN_ON(priv != file->private_data);
  92. /* limit format to hardware capabilities */
  93. return ici->ops->try_fmt(icd, f);
  94. }
  95. static int soc_camera_enum_input(struct file *file, void *priv,
  96. struct v4l2_input *inp)
  97. {
  98. struct soc_camera_file *icf = file->private_data;
  99. struct soc_camera_device *icd = icf->icd;
  100. int ret = 0;
  101. if (inp->index != 0)
  102. return -EINVAL;
  103. if (icd->ops->enum_input)
  104. ret = icd->ops->enum_input(icd, inp);
  105. else {
  106. /* default is camera */
  107. inp->type = V4L2_INPUT_TYPE_CAMERA;
  108. inp->std = V4L2_STD_UNKNOWN;
  109. strcpy(inp->name, "Camera");
  110. }
  111. return ret;
  112. }
  113. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  114. {
  115. *i = 0;
  116. return 0;
  117. }
  118. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  119. {
  120. if (i > 0)
  121. return -EINVAL;
  122. return 0;
  123. }
  124. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
  125. {
  126. struct soc_camera_file *icf = file->private_data;
  127. struct soc_camera_device *icd = icf->icd;
  128. int ret = 0;
  129. if (icd->ops->set_std)
  130. ret = icd->ops->set_std(icd, a);
  131. return ret;
  132. }
  133. static int soc_camera_reqbufs(struct file *file, void *priv,
  134. struct v4l2_requestbuffers *p)
  135. {
  136. int ret;
  137. struct soc_camera_file *icf = file->private_data;
  138. struct soc_camera_device *icd = icf->icd;
  139. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  140. WARN_ON(priv != file->private_data);
  141. dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
  142. ret = videobuf_reqbufs(&icf->vb_vidq, p);
  143. if (ret < 0)
  144. return ret;
  145. return ici->ops->reqbufs(icf, p);
  146. }
  147. static int soc_camera_querybuf(struct file *file, void *priv,
  148. struct v4l2_buffer *p)
  149. {
  150. struct soc_camera_file *icf = file->private_data;
  151. WARN_ON(priv != file->private_data);
  152. return videobuf_querybuf(&icf->vb_vidq, p);
  153. }
  154. static int soc_camera_qbuf(struct file *file, void *priv,
  155. struct v4l2_buffer *p)
  156. {
  157. struct soc_camera_file *icf = file->private_data;
  158. WARN_ON(priv != file->private_data);
  159. return videobuf_qbuf(&icf->vb_vidq, p);
  160. }
  161. static int soc_camera_dqbuf(struct file *file, void *priv,
  162. struct v4l2_buffer *p)
  163. {
  164. struct soc_camera_file *icf = file->private_data;
  165. WARN_ON(priv != file->private_data);
  166. return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
  167. }
  168. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  169. {
  170. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  171. int i, fmts = 0;
  172. if (!ici->ops->get_formats)
  173. /*
  174. * Fallback mode - the host will have to serve all
  175. * sensor-provided formats one-to-one to the user
  176. */
  177. fmts = icd->num_formats;
  178. else
  179. /*
  180. * First pass - only count formats this host-sensor
  181. * configuration can provide
  182. */
  183. for (i = 0; i < icd->num_formats; i++)
  184. fmts += ici->ops->get_formats(icd, i, NULL);
  185. if (!fmts)
  186. return -ENXIO;
  187. icd->user_formats =
  188. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  189. if (!icd->user_formats)
  190. return -ENOMEM;
  191. icd->num_user_formats = fmts;
  192. fmts = 0;
  193. dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
  194. /* Second pass - actually fill data formats */
  195. for (i = 0; i < icd->num_formats; i++)
  196. if (!ici->ops->get_formats) {
  197. icd->user_formats[i].host_fmt = icd->formats + i;
  198. icd->user_formats[i].cam_fmt = icd->formats + i;
  199. icd->user_formats[i].buswidth = icd->formats[i].depth;
  200. } else {
  201. fmts += ici->ops->get_formats(icd, i,
  202. &icd->user_formats[fmts]);
  203. }
  204. icd->current_fmt = icd->user_formats[0].host_fmt;
  205. return 0;
  206. }
  207. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  208. {
  209. vfree(icd->user_formats);
  210. }
  211. /* Called with .vb_lock held */
  212. static int soc_camera_set_fmt(struct soc_camera_file *icf,
  213. struct v4l2_format *f)
  214. {
  215. struct soc_camera_device *icd = icf->icd;
  216. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  217. struct v4l2_pix_format *pix = &f->fmt.pix;
  218. int ret;
  219. /* We always call try_fmt() before set_fmt() or set_crop() */
  220. ret = ici->ops->try_fmt(icd, f);
  221. if (ret < 0)
  222. return ret;
  223. ret = ici->ops->set_fmt(icd, f);
  224. if (ret < 0) {
  225. return ret;
  226. } else if (!icd->current_fmt ||
  227. icd->current_fmt->fourcc != pix->pixelformat) {
  228. dev_err(&ici->dev,
  229. "Host driver hasn't set up current format correctly!\n");
  230. return -EINVAL;
  231. }
  232. icd->width = pix->width;
  233. icd->height = pix->height;
  234. icf->vb_vidq.field =
  235. icd->field = pix->field;
  236. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  237. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  238. f->type);
  239. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  240. icd->width, icd->height);
  241. /* set physical bus parameters */
  242. return ici->ops->set_bus_param(icd, pix->pixelformat);
  243. }
  244. static int soc_camera_open(struct file *file)
  245. {
  246. struct video_device *vdev;
  247. struct soc_camera_device *icd;
  248. struct soc_camera_host *ici;
  249. struct soc_camera_file *icf;
  250. int ret;
  251. icf = vmalloc(sizeof(*icf));
  252. if (!icf)
  253. return -ENOMEM;
  254. /*
  255. * It is safe to dereference these pointers now as long as a user has
  256. * the video device open - we are protected by the held cdev reference.
  257. */
  258. vdev = video_devdata(file);
  259. icd = container_of(vdev->parent, struct soc_camera_device, dev);
  260. ici = to_soc_camera_host(icd->dev.parent);
  261. if (!try_module_get(icd->ops->owner)) {
  262. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  263. ret = -EINVAL;
  264. goto emgd;
  265. }
  266. if (!try_module_get(ici->ops->owner)) {
  267. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  268. ret = -EINVAL;
  269. goto emgi;
  270. }
  271. /* Protect against icd->remove() until we module_get() both drivers. */
  272. mutex_lock(&icd->video_lock);
  273. icf->icd = icd;
  274. icd->use_count++;
  275. /* Now we really have to activate the camera */
  276. if (icd->use_count == 1) {
  277. /* Restore parameters before the last close() per V4L2 API */
  278. struct v4l2_format f = {
  279. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  280. .fmt.pix = {
  281. .width = icd->width,
  282. .height = icd->height,
  283. .field = icd->field,
  284. .pixelformat = icd->current_fmt->fourcc,
  285. .colorspace = icd->current_fmt->colorspace,
  286. },
  287. };
  288. ret = ici->ops->add(icd);
  289. if (ret < 0) {
  290. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  291. goto eiciadd;
  292. }
  293. /* Try to configure with default parameters */
  294. ret = soc_camera_set_fmt(icf, &f);
  295. if (ret < 0)
  296. goto esfmt;
  297. }
  298. mutex_unlock(&icd->video_lock);
  299. file->private_data = icf;
  300. dev_dbg(&icd->dev, "camera device open\n");
  301. ici->ops->init_videobuf(&icf->vb_vidq, icd);
  302. return 0;
  303. /*
  304. * First three errors are entered with the .video_lock held
  305. * and use_count == 1
  306. */
  307. esfmt:
  308. ici->ops->remove(icd);
  309. eiciadd:
  310. icd->use_count--;
  311. mutex_unlock(&icd->video_lock);
  312. module_put(ici->ops->owner);
  313. emgi:
  314. module_put(icd->ops->owner);
  315. emgd:
  316. vfree(icf);
  317. return ret;
  318. }
  319. static int soc_camera_close(struct file *file)
  320. {
  321. struct soc_camera_file *icf = file->private_data;
  322. struct soc_camera_device *icd = icf->icd;
  323. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  324. struct video_device *vdev = icd->vdev;
  325. mutex_lock(&icd->video_lock);
  326. icd->use_count--;
  327. if (!icd->use_count)
  328. ici->ops->remove(icd);
  329. mutex_unlock(&icd->video_lock);
  330. module_put(icd->ops->owner);
  331. module_put(ici->ops->owner);
  332. vfree(icf);
  333. dev_dbg(vdev->parent, "camera device close\n");
  334. return 0;
  335. }
  336. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  337. size_t count, loff_t *ppos)
  338. {
  339. struct soc_camera_file *icf = file->private_data;
  340. struct soc_camera_device *icd = icf->icd;
  341. struct video_device *vdev = icd->vdev;
  342. int err = -EINVAL;
  343. dev_err(vdev->parent, "camera device read not implemented\n");
  344. return err;
  345. }
  346. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  347. {
  348. struct soc_camera_file *icf = file->private_data;
  349. struct soc_camera_device *icd = icf->icd;
  350. int err;
  351. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  352. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  353. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  354. (unsigned long)vma->vm_start,
  355. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  356. err);
  357. return err;
  358. }
  359. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  360. {
  361. struct soc_camera_file *icf = file->private_data;
  362. struct soc_camera_device *icd = icf->icd;
  363. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  364. if (list_empty(&icf->vb_vidq.stream)) {
  365. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  366. return POLLERR;
  367. }
  368. return ici->ops->poll(file, pt);
  369. }
  370. static struct v4l2_file_operations soc_camera_fops = {
  371. .owner = THIS_MODULE,
  372. .open = soc_camera_open,
  373. .release = soc_camera_close,
  374. .ioctl = video_ioctl2,
  375. .read = soc_camera_read,
  376. .mmap = soc_camera_mmap,
  377. .poll = soc_camera_poll,
  378. };
  379. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  380. struct v4l2_format *f)
  381. {
  382. struct soc_camera_file *icf = file->private_data;
  383. struct soc_camera_device *icd = icf->icd;
  384. int ret;
  385. WARN_ON(priv != file->private_data);
  386. mutex_lock(&icf->vb_vidq.vb_lock);
  387. if (videobuf_queue_is_busy(&icf->vb_vidq)) {
  388. dev_err(&icd->dev, "S_FMT denied: queue busy\n");
  389. ret = -EBUSY;
  390. goto unlock;
  391. }
  392. ret = soc_camera_set_fmt(icf, f);
  393. unlock:
  394. mutex_unlock(&icf->vb_vidq.vb_lock);
  395. return ret;
  396. }
  397. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  398. struct v4l2_fmtdesc *f)
  399. {
  400. struct soc_camera_file *icf = file->private_data;
  401. struct soc_camera_device *icd = icf->icd;
  402. const struct soc_camera_data_format *format;
  403. WARN_ON(priv != file->private_data);
  404. if (f->index >= icd->num_user_formats)
  405. return -EINVAL;
  406. format = icd->user_formats[f->index].host_fmt;
  407. strlcpy(f->description, format->name, sizeof(f->description));
  408. f->pixelformat = format->fourcc;
  409. return 0;
  410. }
  411. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  412. struct v4l2_format *f)
  413. {
  414. struct soc_camera_file *icf = file->private_data;
  415. struct soc_camera_device *icd = icf->icd;
  416. struct v4l2_pix_format *pix = &f->fmt.pix;
  417. WARN_ON(priv != file->private_data);
  418. pix->width = icd->width;
  419. pix->height = icd->height;
  420. pix->field = icf->vb_vidq.field;
  421. pix->pixelformat = icd->current_fmt->fourcc;
  422. pix->bytesperline = pix->width *
  423. DIV_ROUND_UP(icd->current_fmt->depth, 8);
  424. pix->sizeimage = pix->height * pix->bytesperline;
  425. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  426. icd->current_fmt->fourcc);
  427. return 0;
  428. }
  429. static int soc_camera_querycap(struct file *file, void *priv,
  430. struct v4l2_capability *cap)
  431. {
  432. struct soc_camera_file *icf = file->private_data;
  433. struct soc_camera_device *icd = icf->icd;
  434. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  435. WARN_ON(priv != file->private_data);
  436. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  437. return ici->ops->querycap(ici, cap);
  438. }
  439. static int soc_camera_streamon(struct file *file, void *priv,
  440. enum v4l2_buf_type i)
  441. {
  442. struct soc_camera_file *icf = file->private_data;
  443. struct soc_camera_device *icd = icf->icd;
  444. int ret;
  445. WARN_ON(priv != file->private_data);
  446. dev_dbg(&icd->dev, "%s\n", __func__);
  447. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  448. return -EINVAL;
  449. mutex_lock(&icd->video_lock);
  450. icd->ops->start_capture(icd);
  451. /* This calls buf_queue from host driver's videobuf_queue_ops */
  452. ret = videobuf_streamon(&icf->vb_vidq);
  453. mutex_unlock(&icd->video_lock);
  454. return ret;
  455. }
  456. static int soc_camera_streamoff(struct file *file, void *priv,
  457. enum v4l2_buf_type i)
  458. {
  459. struct soc_camera_file *icf = file->private_data;
  460. struct soc_camera_device *icd = icf->icd;
  461. WARN_ON(priv != file->private_data);
  462. dev_dbg(&icd->dev, "%s\n", __func__);
  463. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  464. return -EINVAL;
  465. mutex_lock(&icd->video_lock);
  466. /* This calls buf_release from host driver's videobuf_queue_ops for all
  467. * remaining buffers. When the last buffer is freed, stop capture */
  468. videobuf_streamoff(&icf->vb_vidq);
  469. icd->ops->stop_capture(icd);
  470. mutex_unlock(&icd->video_lock);
  471. return 0;
  472. }
  473. static int soc_camera_queryctrl(struct file *file, void *priv,
  474. struct v4l2_queryctrl *qc)
  475. {
  476. struct soc_camera_file *icf = file->private_data;
  477. struct soc_camera_device *icd = icf->icd;
  478. int i;
  479. WARN_ON(priv != file->private_data);
  480. if (!qc->id)
  481. return -EINVAL;
  482. for (i = 0; i < icd->ops->num_controls; i++)
  483. if (qc->id == icd->ops->controls[i].id) {
  484. memcpy(qc, &(icd->ops->controls[i]),
  485. sizeof(*qc));
  486. return 0;
  487. }
  488. return -EINVAL;
  489. }
  490. static int soc_camera_g_ctrl(struct file *file, void *priv,
  491. struct v4l2_control *ctrl)
  492. {
  493. struct soc_camera_file *icf = file->private_data;
  494. struct soc_camera_device *icd = icf->icd;
  495. WARN_ON(priv != file->private_data);
  496. switch (ctrl->id) {
  497. case V4L2_CID_GAIN:
  498. if (icd->gain == (unsigned short)~0)
  499. return -EINVAL;
  500. ctrl->value = icd->gain;
  501. return 0;
  502. case V4L2_CID_EXPOSURE:
  503. if (icd->exposure == (unsigned short)~0)
  504. return -EINVAL;
  505. ctrl->value = icd->exposure;
  506. return 0;
  507. }
  508. if (icd->ops->get_control)
  509. return icd->ops->get_control(icd, ctrl);
  510. return -EINVAL;
  511. }
  512. static int soc_camera_s_ctrl(struct file *file, void *priv,
  513. struct v4l2_control *ctrl)
  514. {
  515. struct soc_camera_file *icf = file->private_data;
  516. struct soc_camera_device *icd = icf->icd;
  517. WARN_ON(priv != file->private_data);
  518. if (icd->ops->set_control)
  519. return icd->ops->set_control(icd, ctrl);
  520. return -EINVAL;
  521. }
  522. static int soc_camera_cropcap(struct file *file, void *fh,
  523. struct v4l2_cropcap *a)
  524. {
  525. struct soc_camera_file *icf = file->private_data;
  526. struct soc_camera_device *icd = icf->icd;
  527. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  528. a->bounds.left = icd->x_min;
  529. a->bounds.top = icd->y_min;
  530. a->bounds.width = icd->width_max;
  531. a->bounds.height = icd->height_max;
  532. a->defrect.left = icd->x_min;
  533. a->defrect.top = icd->y_min;
  534. a->defrect.width = DEFAULT_WIDTH;
  535. a->defrect.height = DEFAULT_HEIGHT;
  536. a->pixelaspect.numerator = 1;
  537. a->pixelaspect.denominator = 1;
  538. return 0;
  539. }
  540. static int soc_camera_g_crop(struct file *file, void *fh,
  541. struct v4l2_crop *a)
  542. {
  543. struct soc_camera_file *icf = file->private_data;
  544. struct soc_camera_device *icd = icf->icd;
  545. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  546. a->c.left = icd->x_current;
  547. a->c.top = icd->y_current;
  548. a->c.width = icd->width;
  549. a->c.height = icd->height;
  550. return 0;
  551. }
  552. static int soc_camera_s_crop(struct file *file, void *fh,
  553. struct v4l2_crop *a)
  554. {
  555. struct soc_camera_file *icf = file->private_data;
  556. struct soc_camera_device *icd = icf->icd;
  557. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  558. int ret;
  559. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  560. return -EINVAL;
  561. /* Cropping is allowed during a running capture, guard consistency */
  562. mutex_lock(&icf->vb_vidq.vb_lock);
  563. ret = ici->ops->set_crop(icd, &a->c);
  564. if (!ret) {
  565. icd->width = a->c.width;
  566. icd->height = a->c.height;
  567. icd->x_current = a->c.left;
  568. icd->y_current = a->c.top;
  569. }
  570. mutex_unlock(&icf->vb_vidq.vb_lock);
  571. return ret;
  572. }
  573. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  574. struct v4l2_dbg_chip_ident *id)
  575. {
  576. struct soc_camera_file *icf = file->private_data;
  577. struct soc_camera_device *icd = icf->icd;
  578. if (!icd->ops->get_chip_id)
  579. return -EINVAL;
  580. return icd->ops->get_chip_id(icd, id);
  581. }
  582. #ifdef CONFIG_VIDEO_ADV_DEBUG
  583. static int soc_camera_g_register(struct file *file, void *fh,
  584. struct v4l2_dbg_register *reg)
  585. {
  586. struct soc_camera_file *icf = file->private_data;
  587. struct soc_camera_device *icd = icf->icd;
  588. if (!icd->ops->get_register)
  589. return -EINVAL;
  590. return icd->ops->get_register(icd, reg);
  591. }
  592. static int soc_camera_s_register(struct file *file, void *fh,
  593. struct v4l2_dbg_register *reg)
  594. {
  595. struct soc_camera_file *icf = file->private_data;
  596. struct soc_camera_device *icd = icf->icd;
  597. if (!icd->ops->set_register)
  598. return -EINVAL;
  599. return icd->ops->set_register(icd, reg);
  600. }
  601. #endif
  602. static int device_register_link(struct soc_camera_device *icd)
  603. {
  604. int ret = dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
  605. if (!ret)
  606. ret = device_register(&icd->dev);
  607. if (ret < 0) {
  608. /* Prevent calling device_unregister() */
  609. icd->dev.parent = NULL;
  610. dev_err(&icd->dev, "Cannot register device: %d\n", ret);
  611. /* Even if probe() was unsuccessful for all registered drivers,
  612. * device_register() returns 0, and we add the link, just to
  613. * document this camera's control device */
  614. } else if (icd->control)
  615. /* Have to sysfs_remove_link() before device_unregister()? */
  616. if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
  617. "control"))
  618. dev_warn(&icd->dev,
  619. "Failed creating the control symlink\n");
  620. return ret;
  621. }
  622. /* So far this function cannot fail */
  623. static void scan_add_host(struct soc_camera_host *ici)
  624. {
  625. struct soc_camera_device *icd;
  626. mutex_lock(&list_lock);
  627. list_for_each_entry(icd, &devices, list) {
  628. if (icd->iface == ici->nr) {
  629. icd->dev.parent = &ici->dev;
  630. device_register_link(icd);
  631. }
  632. }
  633. mutex_unlock(&list_lock);
  634. }
  635. /* return: 0 if no match found or a match found and
  636. * device_register() successful, error code otherwise */
  637. static int scan_add_device(struct soc_camera_device *icd)
  638. {
  639. struct soc_camera_host *ici;
  640. int ret = 0;
  641. mutex_lock(&list_lock);
  642. list_add_tail(&icd->list, &devices);
  643. /* Watch out for class_for_each_device / class_find_device API by
  644. * Dave Young <hidave.darkstar@gmail.com> */
  645. list_for_each_entry(ici, &hosts, list) {
  646. if (icd->iface == ici->nr) {
  647. ret = 1;
  648. icd->dev.parent = &ici->dev;
  649. break;
  650. }
  651. }
  652. mutex_unlock(&list_lock);
  653. if (ret)
  654. ret = device_register_link(icd);
  655. return ret;
  656. }
  657. static int soc_camera_probe(struct device *dev)
  658. {
  659. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  660. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  661. int ret;
  662. /*
  663. * Possible race scenario:
  664. * modprobe <camera-host-driver> triggers __func__
  665. * at this moment respective <camera-sensor-driver> gets rmmod'ed
  666. * to protect take module references.
  667. */
  668. if (!try_module_get(icd->ops->owner)) {
  669. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  670. ret = -EINVAL;
  671. goto emgd;
  672. }
  673. if (!try_module_get(ici->ops->owner)) {
  674. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  675. ret = -EINVAL;
  676. goto emgi;
  677. }
  678. mutex_lock(&icd->video_lock);
  679. /* We only call ->add() here to activate and probe the camera.
  680. * We shall ->remove() and deactivate it immediately afterwards. */
  681. ret = ici->ops->add(icd);
  682. if (ret < 0)
  683. goto eiadd;
  684. ret = icd->ops->probe(icd);
  685. if (ret >= 0) {
  686. const struct v4l2_queryctrl *qctrl;
  687. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  688. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  689. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  690. icd->exposure = qctrl ? qctrl->default_value :
  691. (unsigned short)~0;
  692. ret = soc_camera_init_user_formats(icd);
  693. if (ret < 0)
  694. goto eiufmt;
  695. icd->height = DEFAULT_HEIGHT;
  696. icd->width = DEFAULT_WIDTH;
  697. icd->field = V4L2_FIELD_ANY;
  698. }
  699. eiufmt:
  700. ici->ops->remove(icd);
  701. eiadd:
  702. mutex_unlock(&icd->video_lock);
  703. module_put(ici->ops->owner);
  704. emgi:
  705. module_put(icd->ops->owner);
  706. emgd:
  707. return ret;
  708. }
  709. /* This is called on device_unregister, which only means we have to disconnect
  710. * from the host, but not remove ourselves from the device list */
  711. static int soc_camera_remove(struct device *dev)
  712. {
  713. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  714. if (icd->ops->remove)
  715. icd->ops->remove(icd);
  716. soc_camera_free_user_formats(icd);
  717. return 0;
  718. }
  719. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  720. {
  721. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  722. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  723. int ret = 0;
  724. if (ici->ops->suspend)
  725. ret = ici->ops->suspend(icd, state);
  726. return ret;
  727. }
  728. static int soc_camera_resume(struct device *dev)
  729. {
  730. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  731. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  732. int ret = 0;
  733. if (ici->ops->resume)
  734. ret = ici->ops->resume(icd);
  735. return ret;
  736. }
  737. static struct bus_type soc_camera_bus_type = {
  738. .name = "soc-camera",
  739. .probe = soc_camera_probe,
  740. .remove = soc_camera_remove,
  741. .suspend = soc_camera_suspend,
  742. .resume = soc_camera_resume,
  743. };
  744. static struct device_driver ic_drv = {
  745. .name = "camera",
  746. .bus = &soc_camera_bus_type,
  747. .owner = THIS_MODULE,
  748. };
  749. static void dummy_release(struct device *dev)
  750. {
  751. }
  752. int soc_camera_host_register(struct soc_camera_host *ici)
  753. {
  754. int ret;
  755. struct soc_camera_host *ix;
  756. if (!ici || !ici->ops ||
  757. !ici->ops->try_fmt ||
  758. !ici->ops->set_fmt ||
  759. !ici->ops->set_crop ||
  760. !ici->ops->set_bus_param ||
  761. !ici->ops->querycap ||
  762. !ici->ops->init_videobuf ||
  763. !ici->ops->reqbufs ||
  764. !ici->ops->add ||
  765. !ici->ops->remove ||
  766. !ici->ops->poll)
  767. return -EINVAL;
  768. /* Number might be equal to the platform device ID */
  769. dev_set_name(&ici->dev, "camera_host%d", ici->nr);
  770. mutex_lock(&list_lock);
  771. list_for_each_entry(ix, &hosts, list) {
  772. if (ix->nr == ici->nr) {
  773. mutex_unlock(&list_lock);
  774. return -EBUSY;
  775. }
  776. }
  777. list_add_tail(&ici->list, &hosts);
  778. mutex_unlock(&list_lock);
  779. ici->dev.release = dummy_release;
  780. ret = device_register(&ici->dev);
  781. if (ret)
  782. goto edevr;
  783. scan_add_host(ici);
  784. return 0;
  785. edevr:
  786. mutex_lock(&list_lock);
  787. list_del(&ici->list);
  788. mutex_unlock(&list_lock);
  789. return ret;
  790. }
  791. EXPORT_SYMBOL(soc_camera_host_register);
  792. /* Unregister all clients! */
  793. void soc_camera_host_unregister(struct soc_camera_host *ici)
  794. {
  795. struct soc_camera_device *icd;
  796. mutex_lock(&list_lock);
  797. list_del(&ici->list);
  798. list_for_each_entry(icd, &devices, list) {
  799. if (icd->dev.parent == &ici->dev) {
  800. device_unregister(&icd->dev);
  801. /* Not before device_unregister(), .remove
  802. * needs parent to call ici->ops->remove() */
  803. icd->dev.parent = NULL;
  804. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  805. }
  806. }
  807. mutex_unlock(&list_lock);
  808. device_unregister(&ici->dev);
  809. }
  810. EXPORT_SYMBOL(soc_camera_host_unregister);
  811. /* Image capture device */
  812. int soc_camera_device_register(struct soc_camera_device *icd)
  813. {
  814. struct soc_camera_device *ix;
  815. int num = -1, i;
  816. if (!icd || !icd->ops ||
  817. !icd->ops->probe ||
  818. !icd->ops->init ||
  819. !icd->ops->release ||
  820. !icd->ops->start_capture ||
  821. !icd->ops->stop_capture ||
  822. !icd->ops->set_crop ||
  823. !icd->ops->set_fmt ||
  824. !icd->ops->try_fmt ||
  825. !icd->ops->query_bus_param ||
  826. !icd->ops->set_bus_param)
  827. return -EINVAL;
  828. for (i = 0; i < 256 && num < 0; i++) {
  829. num = i;
  830. list_for_each_entry(ix, &devices, list) {
  831. if (ix->iface == icd->iface && ix->devnum == i) {
  832. num = -1;
  833. break;
  834. }
  835. }
  836. }
  837. if (num < 0)
  838. /* ok, we have 256 cameras on this host...
  839. * man, stay reasonable... */
  840. return -ENOMEM;
  841. icd->devnum = num;
  842. icd->dev.bus = &soc_camera_bus_type;
  843. icd->dev.release = dummy_release;
  844. icd->use_count = 0;
  845. icd->host_priv = NULL;
  846. mutex_init(&icd->video_lock);
  847. return scan_add_device(icd);
  848. }
  849. EXPORT_SYMBOL(soc_camera_device_register);
  850. void soc_camera_device_unregister(struct soc_camera_device *icd)
  851. {
  852. mutex_lock(&list_lock);
  853. list_del(&icd->list);
  854. /* The bus->remove will be eventually called */
  855. if (icd->dev.parent)
  856. device_unregister(&icd->dev);
  857. mutex_unlock(&list_lock);
  858. }
  859. EXPORT_SYMBOL(soc_camera_device_unregister);
  860. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  861. .vidioc_querycap = soc_camera_querycap,
  862. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  863. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  864. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  865. .vidioc_enum_input = soc_camera_enum_input,
  866. .vidioc_g_input = soc_camera_g_input,
  867. .vidioc_s_input = soc_camera_s_input,
  868. .vidioc_s_std = soc_camera_s_std,
  869. .vidioc_reqbufs = soc_camera_reqbufs,
  870. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  871. .vidioc_querybuf = soc_camera_querybuf,
  872. .vidioc_qbuf = soc_camera_qbuf,
  873. .vidioc_dqbuf = soc_camera_dqbuf,
  874. .vidioc_streamon = soc_camera_streamon,
  875. .vidioc_streamoff = soc_camera_streamoff,
  876. .vidioc_queryctrl = soc_camera_queryctrl,
  877. .vidioc_g_ctrl = soc_camera_g_ctrl,
  878. .vidioc_s_ctrl = soc_camera_s_ctrl,
  879. .vidioc_cropcap = soc_camera_cropcap,
  880. .vidioc_g_crop = soc_camera_g_crop,
  881. .vidioc_s_crop = soc_camera_s_crop,
  882. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  883. #ifdef CONFIG_VIDEO_ADV_DEBUG
  884. .vidioc_g_register = soc_camera_g_register,
  885. .vidioc_s_register = soc_camera_s_register,
  886. #endif
  887. };
  888. /*
  889. * Usually called from the struct soc_camera_ops .probe() method, i.e., from
  890. * soc_camera_probe() above with .video_lock held
  891. */
  892. int soc_camera_video_start(struct soc_camera_device *icd)
  893. {
  894. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  895. int err = -ENOMEM;
  896. struct video_device *vdev;
  897. if (!icd->dev.parent)
  898. return -ENODEV;
  899. vdev = video_device_alloc();
  900. if (!vdev)
  901. goto evidallocd;
  902. dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
  903. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  904. vdev->parent = &icd->dev;
  905. vdev->current_norm = V4L2_STD_UNKNOWN;
  906. vdev->fops = &soc_camera_fops;
  907. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  908. vdev->release = video_device_release;
  909. vdev->minor = -1;
  910. vdev->tvnorms = V4L2_STD_UNKNOWN,
  911. err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
  912. if (err < 0) {
  913. dev_err(vdev->parent, "video_register_device failed\n");
  914. goto evidregd;
  915. }
  916. icd->vdev = vdev;
  917. return 0;
  918. evidregd:
  919. video_device_release(vdev);
  920. evidallocd:
  921. return err;
  922. }
  923. EXPORT_SYMBOL(soc_camera_video_start);
  924. void soc_camera_video_stop(struct soc_camera_device *icd)
  925. {
  926. struct video_device *vdev = icd->vdev;
  927. dev_dbg(&icd->dev, "%s\n", __func__);
  928. if (!icd->dev.parent || !vdev)
  929. return;
  930. mutex_lock(&icd->video_lock);
  931. video_unregister_device(vdev);
  932. icd->vdev = NULL;
  933. mutex_unlock(&icd->video_lock);
  934. }
  935. EXPORT_SYMBOL(soc_camera_video_stop);
  936. static int __init soc_camera_init(void)
  937. {
  938. int ret = bus_register(&soc_camera_bus_type);
  939. if (ret)
  940. return ret;
  941. ret = driver_register(&ic_drv);
  942. if (ret)
  943. goto edrvr;
  944. return 0;
  945. edrvr:
  946. bus_unregister(&soc_camera_bus_type);
  947. return ret;
  948. }
  949. static void __exit soc_camera_exit(void)
  950. {
  951. driver_unregister(&ic_drv);
  952. bus_unregister(&soc_camera_bus_type);
  953. }
  954. module_init(soc_camera_init);
  955. module_exit(soc_camera_exit);
  956. MODULE_DESCRIPTION("Image capture bus driver");
  957. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  958. MODULE_LICENSE("GPL");