soc_camera.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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 = pix->field;
  235. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  236. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  237. f->type);
  238. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  239. icd->width, icd->height);
  240. /* set physical bus parameters */
  241. return ici->ops->set_bus_param(icd, pix->pixelformat);
  242. }
  243. static int soc_camera_open(struct file *file)
  244. {
  245. struct video_device *vdev;
  246. struct soc_camera_device *icd;
  247. struct soc_camera_host *ici;
  248. struct soc_camera_file *icf;
  249. int ret;
  250. icf = vmalloc(sizeof(*icf));
  251. if (!icf)
  252. return -ENOMEM;
  253. /*
  254. * It is safe to dereference these pointers now as long as a user has
  255. * the video device open - we are protected by the held cdev reference.
  256. */
  257. vdev = video_devdata(file);
  258. icd = container_of(vdev->parent, struct soc_camera_device, dev);
  259. ici = to_soc_camera_host(icd->dev.parent);
  260. if (!try_module_get(icd->ops->owner)) {
  261. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  262. ret = -EINVAL;
  263. goto emgd;
  264. }
  265. if (!try_module_get(ici->ops->owner)) {
  266. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  267. ret = -EINVAL;
  268. goto emgi;
  269. }
  270. /* Protect against icd->remove() until we module_get() both drivers. */
  271. mutex_lock(&icd->video_lock);
  272. icf->icd = icd;
  273. icd->use_count++;
  274. /* Now we really have to activate the camera */
  275. if (icd->use_count == 1) {
  276. struct v4l2_format f = {
  277. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  278. .fmt.pix = {
  279. .width = DEFAULT_WIDTH,
  280. .height = DEFAULT_HEIGHT,
  281. .field = V4L2_FIELD_ANY,
  282. },
  283. };
  284. ret = soc_camera_init_user_formats(icd);
  285. if (ret < 0)
  286. goto eiufmt;
  287. ret = ici->ops->add(icd);
  288. if (ret < 0) {
  289. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  290. goto eiciadd;
  291. }
  292. f.fmt.pix.pixelformat = icd->current_fmt->fourcc;
  293. f.fmt.pix.colorspace = icd->current_fmt->colorspace;
  294. /* Try to configure with default parameters */
  295. ret = soc_camera_set_fmt(icf, &f);
  296. if (ret < 0)
  297. goto esfmt;
  298. }
  299. mutex_unlock(&icd->video_lock);
  300. file->private_data = icf;
  301. dev_dbg(&icd->dev, "camera device open\n");
  302. ici->ops->init_videobuf(&icf->vb_vidq, icd);
  303. return 0;
  304. /*
  305. * First three errors are entered with the .video_lock held
  306. * and use_count == 1
  307. */
  308. esfmt:
  309. ici->ops->remove(icd);
  310. eiciadd:
  311. soc_camera_free_user_formats(icd);
  312. eiufmt:
  313. icd->use_count--;
  314. mutex_unlock(&icd->video_lock);
  315. module_put(ici->ops->owner);
  316. emgi:
  317. module_put(icd->ops->owner);
  318. emgd:
  319. vfree(icf);
  320. return ret;
  321. }
  322. static int soc_camera_close(struct file *file)
  323. {
  324. struct soc_camera_file *icf = file->private_data;
  325. struct soc_camera_device *icd = icf->icd;
  326. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  327. struct video_device *vdev = icd->vdev;
  328. mutex_lock(&icd->video_lock);
  329. icd->use_count--;
  330. if (!icd->use_count) {
  331. ici->ops->remove(icd);
  332. soc_camera_free_user_formats(icd);
  333. }
  334. mutex_unlock(&icd->video_lock);
  335. module_put(icd->ops->owner);
  336. module_put(ici->ops->owner);
  337. vfree(icf);
  338. dev_dbg(vdev->parent, "camera device close\n");
  339. return 0;
  340. }
  341. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  342. size_t count, loff_t *ppos)
  343. {
  344. struct soc_camera_file *icf = file->private_data;
  345. struct soc_camera_device *icd = icf->icd;
  346. struct video_device *vdev = icd->vdev;
  347. int err = -EINVAL;
  348. dev_err(vdev->parent, "camera device read not implemented\n");
  349. return err;
  350. }
  351. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  352. {
  353. struct soc_camera_file *icf = file->private_data;
  354. struct soc_camera_device *icd = icf->icd;
  355. int err;
  356. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  357. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  358. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  359. (unsigned long)vma->vm_start,
  360. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  361. err);
  362. return err;
  363. }
  364. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  365. {
  366. struct soc_camera_file *icf = file->private_data;
  367. struct soc_camera_device *icd = icf->icd;
  368. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  369. if (list_empty(&icf->vb_vidq.stream)) {
  370. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  371. return POLLERR;
  372. }
  373. return ici->ops->poll(file, pt);
  374. }
  375. static struct v4l2_file_operations soc_camera_fops = {
  376. .owner = THIS_MODULE,
  377. .open = soc_camera_open,
  378. .release = soc_camera_close,
  379. .ioctl = video_ioctl2,
  380. .read = soc_camera_read,
  381. .mmap = soc_camera_mmap,
  382. .poll = soc_camera_poll,
  383. };
  384. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  385. struct v4l2_format *f)
  386. {
  387. struct soc_camera_file *icf = file->private_data;
  388. struct soc_camera_device *icd = icf->icd;
  389. int ret;
  390. WARN_ON(priv != file->private_data);
  391. mutex_lock(&icf->vb_vidq.vb_lock);
  392. if (videobuf_queue_is_busy(&icf->vb_vidq)) {
  393. dev_err(&icd->dev, "S_FMT denied: queue busy\n");
  394. ret = -EBUSY;
  395. goto unlock;
  396. }
  397. ret = soc_camera_set_fmt(icf, f);
  398. unlock:
  399. mutex_unlock(&icf->vb_vidq.vb_lock);
  400. return ret;
  401. }
  402. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  403. struct v4l2_fmtdesc *f)
  404. {
  405. struct soc_camera_file *icf = file->private_data;
  406. struct soc_camera_device *icd = icf->icd;
  407. const struct soc_camera_data_format *format;
  408. WARN_ON(priv != file->private_data);
  409. if (f->index >= icd->num_user_formats)
  410. return -EINVAL;
  411. format = icd->user_formats[f->index].host_fmt;
  412. strlcpy(f->description, format->name, sizeof(f->description));
  413. f->pixelformat = format->fourcc;
  414. return 0;
  415. }
  416. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  417. struct v4l2_format *f)
  418. {
  419. struct soc_camera_file *icf = file->private_data;
  420. struct soc_camera_device *icd = icf->icd;
  421. struct v4l2_pix_format *pix = &f->fmt.pix;
  422. WARN_ON(priv != file->private_data);
  423. pix->width = icd->width;
  424. pix->height = icd->height;
  425. pix->field = icf->vb_vidq.field;
  426. pix->pixelformat = icd->current_fmt->fourcc;
  427. pix->bytesperline = pix->width *
  428. DIV_ROUND_UP(icd->current_fmt->depth, 8);
  429. pix->sizeimage = pix->height * pix->bytesperline;
  430. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  431. icd->current_fmt->fourcc);
  432. return 0;
  433. }
  434. static int soc_camera_querycap(struct file *file, void *priv,
  435. struct v4l2_capability *cap)
  436. {
  437. struct soc_camera_file *icf = file->private_data;
  438. struct soc_camera_device *icd = icf->icd;
  439. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  440. WARN_ON(priv != file->private_data);
  441. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  442. return ici->ops->querycap(ici, cap);
  443. }
  444. static int soc_camera_streamon(struct file *file, void *priv,
  445. enum v4l2_buf_type i)
  446. {
  447. struct soc_camera_file *icf = file->private_data;
  448. struct soc_camera_device *icd = icf->icd;
  449. int ret;
  450. WARN_ON(priv != file->private_data);
  451. dev_dbg(&icd->dev, "%s\n", __func__);
  452. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  453. return -EINVAL;
  454. mutex_lock(&icd->video_lock);
  455. icd->ops->start_capture(icd);
  456. /* This calls buf_queue from host driver's videobuf_queue_ops */
  457. ret = videobuf_streamon(&icf->vb_vidq);
  458. mutex_unlock(&icd->video_lock);
  459. return ret;
  460. }
  461. static int soc_camera_streamoff(struct file *file, void *priv,
  462. enum v4l2_buf_type i)
  463. {
  464. struct soc_camera_file *icf = file->private_data;
  465. struct soc_camera_device *icd = icf->icd;
  466. WARN_ON(priv != file->private_data);
  467. dev_dbg(&icd->dev, "%s\n", __func__);
  468. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  469. return -EINVAL;
  470. mutex_lock(&icd->video_lock);
  471. /* This calls buf_release from host driver's videobuf_queue_ops for all
  472. * remaining buffers. When the last buffer is freed, stop capture */
  473. videobuf_streamoff(&icf->vb_vidq);
  474. icd->ops->stop_capture(icd);
  475. mutex_unlock(&icd->video_lock);
  476. return 0;
  477. }
  478. static int soc_camera_queryctrl(struct file *file, void *priv,
  479. struct v4l2_queryctrl *qc)
  480. {
  481. struct soc_camera_file *icf = file->private_data;
  482. struct soc_camera_device *icd = icf->icd;
  483. int i;
  484. WARN_ON(priv != file->private_data);
  485. if (!qc->id)
  486. return -EINVAL;
  487. for (i = 0; i < icd->ops->num_controls; i++)
  488. if (qc->id == icd->ops->controls[i].id) {
  489. memcpy(qc, &(icd->ops->controls[i]),
  490. sizeof(*qc));
  491. return 0;
  492. }
  493. return -EINVAL;
  494. }
  495. static int soc_camera_g_ctrl(struct file *file, void *priv,
  496. struct v4l2_control *ctrl)
  497. {
  498. struct soc_camera_file *icf = file->private_data;
  499. struct soc_camera_device *icd = icf->icd;
  500. WARN_ON(priv != file->private_data);
  501. switch (ctrl->id) {
  502. case V4L2_CID_GAIN:
  503. if (icd->gain == (unsigned short)~0)
  504. return -EINVAL;
  505. ctrl->value = icd->gain;
  506. return 0;
  507. case V4L2_CID_EXPOSURE:
  508. if (icd->exposure == (unsigned short)~0)
  509. return -EINVAL;
  510. ctrl->value = icd->exposure;
  511. return 0;
  512. }
  513. if (icd->ops->get_control)
  514. return icd->ops->get_control(icd, ctrl);
  515. return -EINVAL;
  516. }
  517. static int soc_camera_s_ctrl(struct file *file, void *priv,
  518. struct v4l2_control *ctrl)
  519. {
  520. struct soc_camera_file *icf = file->private_data;
  521. struct soc_camera_device *icd = icf->icd;
  522. WARN_ON(priv != file->private_data);
  523. if (icd->ops->set_control)
  524. return icd->ops->set_control(icd, ctrl);
  525. return -EINVAL;
  526. }
  527. static int soc_camera_cropcap(struct file *file, void *fh,
  528. struct v4l2_cropcap *a)
  529. {
  530. struct soc_camera_file *icf = file->private_data;
  531. struct soc_camera_device *icd = icf->icd;
  532. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  533. a->bounds.left = icd->x_min;
  534. a->bounds.top = icd->y_min;
  535. a->bounds.width = icd->width_max;
  536. a->bounds.height = icd->height_max;
  537. a->defrect.left = icd->x_min;
  538. a->defrect.top = icd->y_min;
  539. a->defrect.width = DEFAULT_WIDTH;
  540. a->defrect.height = DEFAULT_HEIGHT;
  541. a->pixelaspect.numerator = 1;
  542. a->pixelaspect.denominator = 1;
  543. return 0;
  544. }
  545. static int soc_camera_g_crop(struct file *file, void *fh,
  546. struct v4l2_crop *a)
  547. {
  548. struct soc_camera_file *icf = file->private_data;
  549. struct soc_camera_device *icd = icf->icd;
  550. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  551. a->c.left = icd->x_current;
  552. a->c.top = icd->y_current;
  553. a->c.width = icd->width;
  554. a->c.height = icd->height;
  555. return 0;
  556. }
  557. static int soc_camera_s_crop(struct file *file, void *fh,
  558. struct v4l2_crop *a)
  559. {
  560. struct soc_camera_file *icf = file->private_data;
  561. struct soc_camera_device *icd = icf->icd;
  562. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  563. int ret;
  564. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  565. return -EINVAL;
  566. /* Cropping is allowed during a running capture, guard consistency */
  567. mutex_lock(&icf->vb_vidq.vb_lock);
  568. ret = ici->ops->set_crop(icd, &a->c);
  569. if (!ret) {
  570. icd->width = a->c.width;
  571. icd->height = a->c.height;
  572. icd->x_current = a->c.left;
  573. icd->y_current = a->c.top;
  574. }
  575. mutex_unlock(&icf->vb_vidq.vb_lock);
  576. return ret;
  577. }
  578. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  579. struct v4l2_dbg_chip_ident *id)
  580. {
  581. struct soc_camera_file *icf = file->private_data;
  582. struct soc_camera_device *icd = icf->icd;
  583. if (!icd->ops->get_chip_id)
  584. return -EINVAL;
  585. return icd->ops->get_chip_id(icd, id);
  586. }
  587. #ifdef CONFIG_VIDEO_ADV_DEBUG
  588. static int soc_camera_g_register(struct file *file, void *fh,
  589. struct v4l2_dbg_register *reg)
  590. {
  591. struct soc_camera_file *icf = file->private_data;
  592. struct soc_camera_device *icd = icf->icd;
  593. if (!icd->ops->get_register)
  594. return -EINVAL;
  595. return icd->ops->get_register(icd, reg);
  596. }
  597. static int soc_camera_s_register(struct file *file, void *fh,
  598. struct v4l2_dbg_register *reg)
  599. {
  600. struct soc_camera_file *icf = file->private_data;
  601. struct soc_camera_device *icd = icf->icd;
  602. if (!icd->ops->set_register)
  603. return -EINVAL;
  604. return icd->ops->set_register(icd, reg);
  605. }
  606. #endif
  607. static int device_register_link(struct soc_camera_device *icd)
  608. {
  609. int ret = device_register(&icd->dev);
  610. if (ret < 0) {
  611. /* Prevent calling device_unregister() */
  612. icd->dev.parent = NULL;
  613. dev_err(&icd->dev, "Cannot register device: %d\n", ret);
  614. /* Even if probe() was unsuccessful for all registered drivers,
  615. * device_register() returns 0, and we add the link, just to
  616. * document this camera's control device */
  617. } else if (icd->control)
  618. /* Have to sysfs_remove_link() before device_unregister()? */
  619. if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
  620. "control"))
  621. dev_warn(&icd->dev,
  622. "Failed creating the control symlink\n");
  623. return ret;
  624. }
  625. /* So far this function cannot fail */
  626. static void scan_add_host(struct soc_camera_host *ici)
  627. {
  628. struct soc_camera_device *icd;
  629. mutex_lock(&list_lock);
  630. list_for_each_entry(icd, &devices, list) {
  631. if (icd->iface == ici->nr) {
  632. icd->dev.parent = &ici->dev;
  633. device_register_link(icd);
  634. }
  635. }
  636. mutex_unlock(&list_lock);
  637. }
  638. /* return: 0 if no match found or a match found and
  639. * device_register() successful, error code otherwise */
  640. static int scan_add_device(struct soc_camera_device *icd)
  641. {
  642. struct soc_camera_host *ici;
  643. int ret = 0;
  644. mutex_lock(&list_lock);
  645. list_add_tail(&icd->list, &devices);
  646. /* Watch out for class_for_each_device / class_find_device API by
  647. * Dave Young <hidave.darkstar@gmail.com> */
  648. list_for_each_entry(ici, &hosts, list) {
  649. if (icd->iface == ici->nr) {
  650. ret = 1;
  651. icd->dev.parent = &ici->dev;
  652. break;
  653. }
  654. }
  655. mutex_unlock(&list_lock);
  656. if (ret)
  657. ret = device_register_link(icd);
  658. return ret;
  659. }
  660. static int soc_camera_probe(struct device *dev)
  661. {
  662. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  663. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  664. int ret;
  665. /*
  666. * Possible race scenario:
  667. * modprobe <camera-host-driver> triggers __func__
  668. * at this moment respective <camera-sensor-driver> gets rmmod'ed
  669. * to protect take module references.
  670. */
  671. if (!try_module_get(icd->ops->owner)) {
  672. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  673. ret = -EINVAL;
  674. goto emgd;
  675. }
  676. if (!try_module_get(ici->ops->owner)) {
  677. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  678. ret = -EINVAL;
  679. goto emgi;
  680. }
  681. mutex_lock(&icd->video_lock);
  682. /* We only call ->add() here to activate and probe the camera.
  683. * We shall ->remove() and deactivate it immediately afterwards. */
  684. ret = ici->ops->add(icd);
  685. if (ret < 0)
  686. goto eiadd;
  687. ret = icd->ops->probe(icd);
  688. if (ret >= 0) {
  689. const struct v4l2_queryctrl *qctrl;
  690. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  691. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  692. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  693. icd->exposure = qctrl ? qctrl->default_value :
  694. (unsigned short)~0;
  695. }
  696. ici->ops->remove(icd);
  697. eiadd:
  698. mutex_unlock(&icd->video_lock);
  699. module_put(ici->ops->owner);
  700. emgi:
  701. module_put(icd->ops->owner);
  702. emgd:
  703. return ret;
  704. }
  705. /* This is called on device_unregister, which only means we have to disconnect
  706. * from the host, but not remove ourselves from the device list */
  707. static int soc_camera_remove(struct device *dev)
  708. {
  709. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  710. if (icd->ops->remove)
  711. icd->ops->remove(icd);
  712. return 0;
  713. }
  714. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  715. {
  716. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  717. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  718. int ret = 0;
  719. if (ici->ops->suspend)
  720. ret = ici->ops->suspend(icd, state);
  721. return ret;
  722. }
  723. static int soc_camera_resume(struct device *dev)
  724. {
  725. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  726. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  727. int ret = 0;
  728. if (ici->ops->resume)
  729. ret = ici->ops->resume(icd);
  730. return ret;
  731. }
  732. static struct bus_type soc_camera_bus_type = {
  733. .name = "soc-camera",
  734. .probe = soc_camera_probe,
  735. .remove = soc_camera_remove,
  736. .suspend = soc_camera_suspend,
  737. .resume = soc_camera_resume,
  738. };
  739. static struct device_driver ic_drv = {
  740. .name = "camera",
  741. .bus = &soc_camera_bus_type,
  742. .owner = THIS_MODULE,
  743. };
  744. static void dummy_release(struct device *dev)
  745. {
  746. }
  747. int soc_camera_host_register(struct soc_camera_host *ici)
  748. {
  749. int ret;
  750. struct soc_camera_host *ix;
  751. if (!ici || !ici->ops ||
  752. !ici->ops->try_fmt ||
  753. !ici->ops->set_fmt ||
  754. !ici->ops->set_crop ||
  755. !ici->ops->set_bus_param ||
  756. !ici->ops->querycap ||
  757. !ici->ops->init_videobuf ||
  758. !ici->ops->reqbufs ||
  759. !ici->ops->add ||
  760. !ici->ops->remove ||
  761. !ici->ops->poll)
  762. return -EINVAL;
  763. /* Number might be equal to the platform device ID */
  764. dev_set_name(&ici->dev, "camera_host%d", ici->nr);
  765. mutex_lock(&list_lock);
  766. list_for_each_entry(ix, &hosts, list) {
  767. if (ix->nr == ici->nr) {
  768. mutex_unlock(&list_lock);
  769. return -EBUSY;
  770. }
  771. }
  772. list_add_tail(&ici->list, &hosts);
  773. mutex_unlock(&list_lock);
  774. ici->dev.release = dummy_release;
  775. ret = device_register(&ici->dev);
  776. if (ret)
  777. goto edevr;
  778. scan_add_host(ici);
  779. return 0;
  780. edevr:
  781. mutex_lock(&list_lock);
  782. list_del(&ici->list);
  783. mutex_unlock(&list_lock);
  784. return ret;
  785. }
  786. EXPORT_SYMBOL(soc_camera_host_register);
  787. /* Unregister all clients! */
  788. void soc_camera_host_unregister(struct soc_camera_host *ici)
  789. {
  790. struct soc_camera_device *icd;
  791. mutex_lock(&list_lock);
  792. list_del(&ici->list);
  793. list_for_each_entry(icd, &devices, list) {
  794. if (icd->dev.parent == &ici->dev) {
  795. device_unregister(&icd->dev);
  796. /* Not before device_unregister(), .remove
  797. * needs parent to call ici->ops->remove() */
  798. icd->dev.parent = NULL;
  799. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  800. }
  801. }
  802. mutex_unlock(&list_lock);
  803. device_unregister(&ici->dev);
  804. }
  805. EXPORT_SYMBOL(soc_camera_host_unregister);
  806. /* Image capture device */
  807. int soc_camera_device_register(struct soc_camera_device *icd)
  808. {
  809. struct soc_camera_device *ix;
  810. int num = -1, i;
  811. if (!icd || !icd->ops ||
  812. !icd->ops->probe ||
  813. !icd->ops->init ||
  814. !icd->ops->release ||
  815. !icd->ops->start_capture ||
  816. !icd->ops->stop_capture ||
  817. !icd->ops->set_crop ||
  818. !icd->ops->set_fmt ||
  819. !icd->ops->try_fmt ||
  820. !icd->ops->query_bus_param ||
  821. !icd->ops->set_bus_param)
  822. return -EINVAL;
  823. for (i = 0; i < 256 && num < 0; i++) {
  824. num = i;
  825. list_for_each_entry(ix, &devices, list) {
  826. if (ix->iface == icd->iface && ix->devnum == i) {
  827. num = -1;
  828. break;
  829. }
  830. }
  831. }
  832. if (num < 0)
  833. /* ok, we have 256 cameras on this host...
  834. * man, stay reasonable... */
  835. return -ENOMEM;
  836. icd->devnum = num;
  837. icd->dev.bus = &soc_camera_bus_type;
  838. dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
  839. icd->dev.release = dummy_release;
  840. icd->use_count = 0;
  841. icd->host_priv = NULL;
  842. mutex_init(&icd->video_lock);
  843. return scan_add_device(icd);
  844. }
  845. EXPORT_SYMBOL(soc_camera_device_register);
  846. void soc_camera_device_unregister(struct soc_camera_device *icd)
  847. {
  848. mutex_lock(&list_lock);
  849. list_del(&icd->list);
  850. /* The bus->remove will be eventually called */
  851. if (icd->dev.parent)
  852. device_unregister(&icd->dev);
  853. mutex_unlock(&list_lock);
  854. }
  855. EXPORT_SYMBOL(soc_camera_device_unregister);
  856. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  857. .vidioc_querycap = soc_camera_querycap,
  858. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  859. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  860. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  861. .vidioc_enum_input = soc_camera_enum_input,
  862. .vidioc_g_input = soc_camera_g_input,
  863. .vidioc_s_input = soc_camera_s_input,
  864. .vidioc_s_std = soc_camera_s_std,
  865. .vidioc_reqbufs = soc_camera_reqbufs,
  866. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  867. .vidioc_querybuf = soc_camera_querybuf,
  868. .vidioc_qbuf = soc_camera_qbuf,
  869. .vidioc_dqbuf = soc_camera_dqbuf,
  870. .vidioc_streamon = soc_camera_streamon,
  871. .vidioc_streamoff = soc_camera_streamoff,
  872. .vidioc_queryctrl = soc_camera_queryctrl,
  873. .vidioc_g_ctrl = soc_camera_g_ctrl,
  874. .vidioc_s_ctrl = soc_camera_s_ctrl,
  875. .vidioc_cropcap = soc_camera_cropcap,
  876. .vidioc_g_crop = soc_camera_g_crop,
  877. .vidioc_s_crop = soc_camera_s_crop,
  878. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  879. #ifdef CONFIG_VIDEO_ADV_DEBUG
  880. .vidioc_g_register = soc_camera_g_register,
  881. .vidioc_s_register = soc_camera_s_register,
  882. #endif
  883. };
  884. /*
  885. * Usually called from the struct soc_camera_ops .probe() method, i.e., from
  886. * soc_camera_probe() above with .video_lock held
  887. */
  888. int soc_camera_video_start(struct soc_camera_device *icd)
  889. {
  890. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  891. int err = -ENOMEM;
  892. struct video_device *vdev;
  893. if (!icd->dev.parent)
  894. return -ENODEV;
  895. vdev = video_device_alloc();
  896. if (!vdev)
  897. goto evidallocd;
  898. dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
  899. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  900. vdev->parent = &icd->dev;
  901. vdev->current_norm = V4L2_STD_UNKNOWN;
  902. vdev->fops = &soc_camera_fops;
  903. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  904. vdev->release = video_device_release;
  905. vdev->minor = -1;
  906. vdev->tvnorms = V4L2_STD_UNKNOWN,
  907. err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
  908. if (err < 0) {
  909. dev_err(vdev->parent, "video_register_device failed\n");
  910. goto evidregd;
  911. }
  912. icd->vdev = vdev;
  913. return 0;
  914. evidregd:
  915. video_device_release(vdev);
  916. evidallocd:
  917. return err;
  918. }
  919. EXPORT_SYMBOL(soc_camera_video_start);
  920. void soc_camera_video_stop(struct soc_camera_device *icd)
  921. {
  922. struct video_device *vdev = icd->vdev;
  923. dev_dbg(&icd->dev, "%s\n", __func__);
  924. if (!icd->dev.parent || !vdev)
  925. return;
  926. mutex_lock(&icd->video_lock);
  927. video_unregister_device(vdev);
  928. icd->vdev = NULL;
  929. mutex_unlock(&icd->video_lock);
  930. }
  931. EXPORT_SYMBOL(soc_camera_video_stop);
  932. static int __init soc_camera_init(void)
  933. {
  934. int ret = bus_register(&soc_camera_bus_type);
  935. if (ret)
  936. return ret;
  937. ret = driver_register(&ic_drv);
  938. if (ret)
  939. goto edrvr;
  940. return 0;
  941. edrvr:
  942. bus_unregister(&soc_camera_bus_type);
  943. return ret;
  944. }
  945. static void __exit soc_camera_exit(void)
  946. {
  947. driver_unregister(&ic_drv);
  948. bus_unregister(&soc_camera_bus_type);
  949. }
  950. module_init(soc_camera_init);
  951. module_exit(soc_camera_exit);
  952. MODULE_DESCRIPTION("Image capture bus driver");
  953. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  954. MODULE_LICENSE("GPL");