soc_camera.c 28 KB

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