soc_camera.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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-dev.h>
  27. #include <media/soc_camera.h>
  28. static LIST_HEAD(hosts);
  29. static LIST_HEAD(devices);
  30. static DEFINE_MUTEX(list_lock);
  31. static DEFINE_MUTEX(video_lock);
  32. const static struct soc_camera_data_format*
  33. format_by_fourcc(struct soc_camera_device *icd, unsigned int fourcc)
  34. {
  35. unsigned int i;
  36. for (i = 0; i < icd->num_formats; i++)
  37. if (icd->formats[i].fourcc == fourcc)
  38. return icd->formats + i;
  39. return NULL;
  40. }
  41. static int soc_camera_try_fmt_cap(struct file *file, void *priv,
  42. struct v4l2_format *f)
  43. {
  44. struct soc_camera_file *icf = file->private_data;
  45. struct soc_camera_device *icd = icf->icd;
  46. struct soc_camera_host *ici =
  47. to_soc_camera_host(icd->dev.parent);
  48. enum v4l2_field field;
  49. const struct soc_camera_data_format *fmt;
  50. int ret;
  51. WARN_ON(priv != file->private_data);
  52. fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
  53. if (!fmt) {
  54. dev_dbg(&icd->dev, "invalid format 0x%08x\n",
  55. f->fmt.pix.pixelformat);
  56. return -EINVAL;
  57. }
  58. dev_dbg(&icd->dev, "fmt: 0x%08x\n", fmt->fourcc);
  59. field = f->fmt.pix.field;
  60. if (field == V4L2_FIELD_ANY) {
  61. field = V4L2_FIELD_NONE;
  62. } else if (V4L2_FIELD_NONE != field) {
  63. dev_err(&icd->dev, "Field type invalid.\n");
  64. return -EINVAL;
  65. }
  66. /* test physical bus parameters */
  67. ret = ici->ops->try_bus_param(icd, f->fmt.pix.pixelformat);
  68. if (ret)
  69. return ret;
  70. /* limit format to hardware capabilities */
  71. ret = ici->ops->try_fmt_cap(icd, f);
  72. /* calculate missing fields */
  73. f->fmt.pix.field = field;
  74. f->fmt.pix.bytesperline =
  75. (f->fmt.pix.width * fmt->depth) >> 3;
  76. f->fmt.pix.sizeimage =
  77. f->fmt.pix.height * f->fmt.pix.bytesperline;
  78. return ret;
  79. }
  80. static int soc_camera_enum_input(struct file *file, void *priv,
  81. struct v4l2_input *inp)
  82. {
  83. if (inp->index != 0)
  84. return -EINVAL;
  85. inp->type = V4L2_INPUT_TYPE_CAMERA;
  86. inp->std = V4L2_STD_UNKNOWN;
  87. strcpy(inp->name, "Camera");
  88. return 0;
  89. }
  90. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  91. {
  92. *i = 0;
  93. return 0;
  94. }
  95. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  96. {
  97. if (i > 0)
  98. return -EINVAL;
  99. return 0;
  100. }
  101. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
  102. {
  103. return 0;
  104. }
  105. static int soc_camera_reqbufs(struct file *file, void *priv,
  106. struct v4l2_requestbuffers *p)
  107. {
  108. int ret;
  109. struct soc_camera_file *icf = file->private_data;
  110. struct soc_camera_device *icd = icf->icd;
  111. struct soc_camera_host *ici =
  112. to_soc_camera_host(icd->dev.parent);
  113. WARN_ON(priv != file->private_data);
  114. dev_dbg(&icd->dev, "%s: %d\n", __FUNCTION__, p->memory);
  115. ret = videobuf_reqbufs(&icf->vb_vidq, p);
  116. if (ret < 0)
  117. return ret;
  118. return ici->ops->reqbufs(icf, p);
  119. return ret;
  120. }
  121. static int soc_camera_querybuf(struct file *file, void *priv,
  122. struct v4l2_buffer *p)
  123. {
  124. struct soc_camera_file *icf = file->private_data;
  125. WARN_ON(priv != file->private_data);
  126. return videobuf_querybuf(&icf->vb_vidq, p);
  127. }
  128. static int soc_camera_qbuf(struct file *file, void *priv,
  129. struct v4l2_buffer *p)
  130. {
  131. struct soc_camera_file *icf = file->private_data;
  132. WARN_ON(priv != file->private_data);
  133. return videobuf_qbuf(&icf->vb_vidq, p);
  134. }
  135. static int soc_camera_dqbuf(struct file *file, void *priv,
  136. struct v4l2_buffer *p)
  137. {
  138. struct soc_camera_file *icf = file->private_data;
  139. WARN_ON(priv != file->private_data);
  140. return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
  141. }
  142. static int soc_camera_open(struct inode *inode, struct file *file)
  143. {
  144. struct video_device *vdev;
  145. struct soc_camera_device *icd;
  146. struct soc_camera_host *ici;
  147. struct soc_camera_file *icf;
  148. spinlock_t *lock;
  149. int ret;
  150. icf = vmalloc(sizeof(*icf));
  151. if (!icf)
  152. return -ENOMEM;
  153. /* Protect against icd->remove() until we module_get() both drivers. */
  154. mutex_lock(&video_lock);
  155. vdev = video_devdata(file);
  156. icd = container_of(vdev->dev, struct soc_camera_device, dev);
  157. ici = to_soc_camera_host(icd->dev.parent);
  158. if (!try_module_get(icd->ops->owner)) {
  159. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  160. ret = -EINVAL;
  161. goto emgd;
  162. }
  163. if (!try_module_get(ici->ops->owner)) {
  164. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  165. ret = -EINVAL;
  166. goto emgi;
  167. }
  168. icf->icd = icd;
  169. icf->lock = ici->ops->spinlock_alloc(icf);
  170. if (!icf->lock) {
  171. ret = -ENOMEM;
  172. goto esla;
  173. }
  174. icd->use_count++;
  175. /* Now we really have to activate the camera */
  176. if (icd->use_count == 1) {
  177. ret = ici->ops->add(icd);
  178. if (ret < 0) {
  179. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  180. icd->use_count--;
  181. goto eiciadd;
  182. }
  183. }
  184. mutex_unlock(&video_lock);
  185. file->private_data = icf;
  186. dev_dbg(&icd->dev, "camera device open\n");
  187. /* We must pass NULL as dev pointer, then all pci_* dma operations
  188. * transform to normal dma_* ones. */
  189. videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, icf->lock,
  190. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  191. ici->msize, icd);
  192. return 0;
  193. /* All errors are entered with the video_lock held */
  194. eiciadd:
  195. lock = icf->lock;
  196. icf->lock = NULL;
  197. if (ici->ops->spinlock_free)
  198. ici->ops->spinlock_free(lock);
  199. esla:
  200. module_put(ici->ops->owner);
  201. emgi:
  202. module_put(icd->ops->owner);
  203. emgd:
  204. mutex_unlock(&video_lock);
  205. vfree(icf);
  206. return ret;
  207. }
  208. static int soc_camera_close(struct inode *inode, struct file *file)
  209. {
  210. struct soc_camera_file *icf = file->private_data;
  211. struct soc_camera_device *icd = icf->icd;
  212. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  213. struct video_device *vdev = icd->vdev;
  214. spinlock_t *lock = icf->lock;
  215. mutex_lock(&video_lock);
  216. icd->use_count--;
  217. if (!icd->use_count)
  218. ici->ops->remove(icd);
  219. icf->lock = NULL;
  220. if (ici->ops->spinlock_free)
  221. ici->ops->spinlock_free(lock);
  222. module_put(icd->ops->owner);
  223. module_put(ici->ops->owner);
  224. mutex_unlock(&video_lock);
  225. vfree(icf);
  226. dev_dbg(vdev->dev, "camera device close\n");
  227. return 0;
  228. }
  229. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  230. size_t count, loff_t *ppos)
  231. {
  232. struct soc_camera_file *icf = file->private_data;
  233. struct soc_camera_device *icd = icf->icd;
  234. struct video_device *vdev = icd->vdev;
  235. int err = -EINVAL;
  236. dev_err(vdev->dev, "camera device read not implemented\n");
  237. return err;
  238. }
  239. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  240. {
  241. struct soc_camera_file *icf = file->private_data;
  242. struct soc_camera_device *icd = icf->icd;
  243. int err;
  244. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  245. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  246. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  247. (unsigned long)vma->vm_start,
  248. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  249. err);
  250. return err;
  251. }
  252. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  253. {
  254. struct soc_camera_file *icf = file->private_data;
  255. struct soc_camera_device *icd = icf->icd;
  256. struct soc_camera_host *ici =
  257. to_soc_camera_host(icd->dev.parent);
  258. if (list_empty(&icf->vb_vidq.stream)) {
  259. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  260. return POLLERR;
  261. }
  262. return ici->ops->poll(file, pt);
  263. }
  264. static struct file_operations soc_camera_fops = {
  265. .owner = THIS_MODULE,
  266. .open = soc_camera_open,
  267. .release = soc_camera_close,
  268. .ioctl = video_ioctl2,
  269. .read = soc_camera_read,
  270. .mmap = soc_camera_mmap,
  271. .poll = soc_camera_poll,
  272. .llseek = no_llseek,
  273. };
  274. static int soc_camera_s_fmt_cap(struct file *file, void *priv,
  275. struct v4l2_format *f)
  276. {
  277. struct soc_camera_file *icf = file->private_data;
  278. struct soc_camera_device *icd = icf->icd;
  279. struct soc_camera_host *ici =
  280. to_soc_camera_host(icd->dev.parent);
  281. int ret;
  282. struct v4l2_rect rect;
  283. const static struct soc_camera_data_format *data_fmt;
  284. WARN_ON(priv != file->private_data);
  285. data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
  286. if (!data_fmt)
  287. return -EINVAL;
  288. /* buswidth may be further adjusted by the ici */
  289. icd->buswidth = data_fmt->depth;
  290. ret = soc_camera_try_fmt_cap(file, icf, f);
  291. if (ret < 0)
  292. return ret;
  293. rect.left = icd->x_current;
  294. rect.top = icd->y_current;
  295. rect.width = f->fmt.pix.width;
  296. rect.height = f->fmt.pix.height;
  297. ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
  298. if (ret < 0)
  299. return ret;
  300. icd->current_fmt = data_fmt;
  301. icd->width = rect.width;
  302. icd->height = rect.height;
  303. icf->vb_vidq.field = f->fmt.pix.field;
  304. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
  305. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  306. f->type);
  307. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  308. icd->width, icd->height);
  309. /* set physical bus parameters */
  310. return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
  311. }
  312. static int soc_camera_enum_fmt_cap(struct file *file, void *priv,
  313. struct v4l2_fmtdesc *f)
  314. {
  315. struct soc_camera_file *icf = file->private_data;
  316. struct soc_camera_device *icd = icf->icd;
  317. const struct soc_camera_data_format *format;
  318. WARN_ON(priv != file->private_data);
  319. if (f->index >= icd->num_formats)
  320. return -EINVAL;
  321. format = &icd->formats[f->index];
  322. strlcpy(f->description, format->name, sizeof(f->description));
  323. f->pixelformat = format->fourcc;
  324. return 0;
  325. }
  326. static int soc_camera_g_fmt_cap(struct file *file, void *priv,
  327. struct v4l2_format *f)
  328. {
  329. struct soc_camera_file *icf = file->private_data;
  330. struct soc_camera_device *icd = icf->icd;
  331. WARN_ON(priv != file->private_data);
  332. f->fmt.pix.width = icd->width;
  333. f->fmt.pix.height = icd->height;
  334. f->fmt.pix.field = icf->vb_vidq.field;
  335. f->fmt.pix.pixelformat = icd->current_fmt->fourcc;
  336. f->fmt.pix.bytesperline =
  337. (f->fmt.pix.width * icd->current_fmt->depth) >> 3;
  338. f->fmt.pix.sizeimage =
  339. f->fmt.pix.height * f->fmt.pix.bytesperline;
  340. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  341. icd->current_fmt->fourcc);
  342. return 0;
  343. }
  344. static int soc_camera_querycap(struct file *file, void *priv,
  345. struct v4l2_capability *cap)
  346. {
  347. struct soc_camera_file *icf = file->private_data;
  348. struct soc_camera_device *icd = icf->icd;
  349. struct soc_camera_host *ici =
  350. to_soc_camera_host(icd->dev.parent);
  351. WARN_ON(priv != file->private_data);
  352. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  353. return ici->ops->querycap(ici, cap);
  354. }
  355. static int soc_camera_streamon(struct file *file, void *priv,
  356. enum v4l2_buf_type i)
  357. {
  358. struct soc_camera_file *icf = file->private_data;
  359. struct soc_camera_device *icd = icf->icd;
  360. WARN_ON(priv != file->private_data);
  361. dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
  362. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  363. return -EINVAL;
  364. icd->ops->start_capture(icd);
  365. /* This calls buf_queue from host driver's videobuf_queue_ops */
  366. return videobuf_streamon(&icf->vb_vidq);
  367. }
  368. static int soc_camera_streamoff(struct file *file, void *priv,
  369. enum v4l2_buf_type i)
  370. {
  371. struct soc_camera_file *icf = file->private_data;
  372. struct soc_camera_device *icd = icf->icd;
  373. WARN_ON(priv != file->private_data);
  374. dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
  375. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  376. return -EINVAL;
  377. /* This calls buf_release from host driver's videobuf_queue_ops for all
  378. * remaining buffers. When the last buffer is freed, stop capture */
  379. videobuf_streamoff(&icf->vb_vidq);
  380. icd->ops->stop_capture(icd);
  381. return 0;
  382. }
  383. static int soc_camera_queryctrl(struct file *file, void *priv,
  384. struct v4l2_queryctrl *qc)
  385. {
  386. struct soc_camera_file *icf = file->private_data;
  387. struct soc_camera_device *icd = icf->icd;
  388. int i;
  389. WARN_ON(priv != file->private_data);
  390. if (!qc->id)
  391. return -EINVAL;
  392. for (i = 0; i < icd->ops->num_controls; i++)
  393. if (qc->id == icd->ops->controls[i].id) {
  394. memcpy(qc, &(icd->ops->controls[i]),
  395. sizeof(*qc));
  396. return 0;
  397. }
  398. return -EINVAL;
  399. }
  400. static int soc_camera_g_ctrl(struct file *file, void *priv,
  401. struct v4l2_control *ctrl)
  402. {
  403. struct soc_camera_file *icf = file->private_data;
  404. struct soc_camera_device *icd = icf->icd;
  405. WARN_ON(priv != file->private_data);
  406. switch (ctrl->id) {
  407. case V4L2_CID_GAIN:
  408. if (icd->gain == (unsigned short)~0)
  409. return -EINVAL;
  410. ctrl->value = icd->gain;
  411. return 0;
  412. case V4L2_CID_EXPOSURE:
  413. if (icd->exposure == (unsigned short)~0)
  414. return -EINVAL;
  415. ctrl->value = icd->exposure;
  416. return 0;
  417. }
  418. if (icd->ops->get_control)
  419. return icd->ops->get_control(icd, ctrl);
  420. return -EINVAL;
  421. }
  422. static int soc_camera_s_ctrl(struct file *file, void *priv,
  423. struct v4l2_control *ctrl)
  424. {
  425. struct soc_camera_file *icf = file->private_data;
  426. struct soc_camera_device *icd = icf->icd;
  427. WARN_ON(priv != file->private_data);
  428. if (icd->ops->set_control)
  429. return icd->ops->set_control(icd, ctrl);
  430. return -EINVAL;
  431. }
  432. static int soc_camera_cropcap(struct file *file, void *fh,
  433. struct v4l2_cropcap *a)
  434. {
  435. struct soc_camera_file *icf = file->private_data;
  436. struct soc_camera_device *icd = icf->icd;
  437. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  438. a->bounds.left = icd->x_min;
  439. a->bounds.top = icd->y_min;
  440. a->bounds.width = icd->width_max;
  441. a->bounds.height = icd->height_max;
  442. a->defrect.left = icd->x_min;
  443. a->defrect.top = icd->y_min;
  444. a->defrect.width = 640;
  445. a->defrect.height = 480;
  446. a->pixelaspect.numerator = 1;
  447. a->pixelaspect.denominator = 1;
  448. return 0;
  449. }
  450. static int soc_camera_g_crop(struct file *file, void *fh,
  451. struct v4l2_crop *a)
  452. {
  453. struct soc_camera_file *icf = file->private_data;
  454. struct soc_camera_device *icd = icf->icd;
  455. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  456. a->c.left = icd->x_current;
  457. a->c.top = icd->y_current;
  458. a->c.width = icd->width;
  459. a->c.height = icd->height;
  460. return 0;
  461. }
  462. static int soc_camera_s_crop(struct file *file, void *fh,
  463. struct v4l2_crop *a)
  464. {
  465. struct soc_camera_file *icf = file->private_data;
  466. struct soc_camera_device *icd = icf->icd;
  467. struct soc_camera_host *ici =
  468. to_soc_camera_host(icd->dev.parent);
  469. int ret;
  470. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  471. return -EINVAL;
  472. ret = ici->ops->set_fmt_cap(icd, 0, &a->c);
  473. if (!ret) {
  474. icd->width = a->c.width;
  475. icd->height = a->c.height;
  476. icd->x_current = a->c.left;
  477. icd->y_current = a->c.top;
  478. }
  479. return ret;
  480. }
  481. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  482. struct v4l2_chip_ident *id)
  483. {
  484. struct soc_camera_file *icf = file->private_data;
  485. struct soc_camera_device *icd = icf->icd;
  486. if (!icd->ops->get_chip_id)
  487. return -EINVAL;
  488. return icd->ops->get_chip_id(icd, id);
  489. }
  490. #ifdef CONFIG_VIDEO_ADV_DEBUG
  491. static int soc_camera_g_register(struct file *file, void *fh,
  492. struct v4l2_register *reg)
  493. {
  494. struct soc_camera_file *icf = file->private_data;
  495. struct soc_camera_device *icd = icf->icd;
  496. if (!icd->ops->get_register)
  497. return -EINVAL;
  498. return icd->ops->get_register(icd, reg);
  499. }
  500. static int soc_camera_s_register(struct file *file, void *fh,
  501. struct v4l2_register *reg)
  502. {
  503. struct soc_camera_file *icf = file->private_data;
  504. struct soc_camera_device *icd = icf->icd;
  505. if (!icd->ops->set_register)
  506. return -EINVAL;
  507. return icd->ops->set_register(icd, reg);
  508. }
  509. #endif
  510. static int device_register_link(struct soc_camera_device *icd)
  511. {
  512. int ret = device_register(&icd->dev);
  513. if (ret < 0) {
  514. /* Prevent calling device_unregister() */
  515. icd->dev.parent = NULL;
  516. dev_err(&icd->dev, "Cannot register device: %d\n", ret);
  517. /* Even if probe() was unsuccessful for all registered drivers,
  518. * device_register() returns 0, and we add the link, just to
  519. * document this camera's control device */
  520. } else if (icd->control)
  521. /* Have to sysfs_remove_link() before device_unregister()? */
  522. if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
  523. "control"))
  524. dev_warn(&icd->dev,
  525. "Failed creating the control symlink\n");
  526. return ret;
  527. }
  528. /* So far this function cannot fail */
  529. static void scan_add_host(struct soc_camera_host *ici)
  530. {
  531. struct soc_camera_device *icd;
  532. mutex_lock(&list_lock);
  533. list_for_each_entry(icd, &devices, list) {
  534. if (icd->iface == ici->nr) {
  535. icd->dev.parent = &ici->dev;
  536. device_register_link(icd);
  537. }
  538. }
  539. mutex_unlock(&list_lock);
  540. }
  541. /* return: 0 if no match found or a match found and
  542. * device_register() successful, error code otherwise */
  543. static int scan_add_device(struct soc_camera_device *icd)
  544. {
  545. struct soc_camera_host *ici;
  546. int ret = 0;
  547. mutex_lock(&list_lock);
  548. list_add_tail(&icd->list, &devices);
  549. /* Watch out for class_for_each_device / class_find_device API by
  550. * Dave Young <hidave.darkstar@gmail.com> */
  551. list_for_each_entry(ici, &hosts, list) {
  552. if (icd->iface == ici->nr) {
  553. ret = 1;
  554. icd->dev.parent = &ici->dev;
  555. break;
  556. }
  557. }
  558. mutex_unlock(&list_lock);
  559. if (ret)
  560. ret = device_register_link(icd);
  561. return ret;
  562. }
  563. static int soc_camera_probe(struct device *dev)
  564. {
  565. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  566. struct soc_camera_host *ici =
  567. to_soc_camera_host(icd->dev.parent);
  568. int ret;
  569. if (!icd->ops->probe)
  570. return -ENODEV;
  571. /* We only call ->add() here to activate and probe the camera.
  572. * We shall ->remove() and deactivate it immediately afterwards. */
  573. ret = ici->ops->add(icd);
  574. if (ret < 0)
  575. return ret;
  576. ret = icd->ops->probe(icd);
  577. if (ret >= 0) {
  578. const struct v4l2_queryctrl *qctrl;
  579. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  580. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  581. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  582. icd->exposure = qctrl ? qctrl->default_value :
  583. (unsigned short)~0;
  584. }
  585. ici->ops->remove(icd);
  586. return ret;
  587. }
  588. /* This is called on device_unregister, which only means we have to disconnect
  589. * from the host, but not remove ourselves from the device list */
  590. static int soc_camera_remove(struct device *dev)
  591. {
  592. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  593. if (icd->ops->remove)
  594. icd->ops->remove(icd);
  595. return 0;
  596. }
  597. static struct bus_type soc_camera_bus_type = {
  598. .name = "soc-camera",
  599. .probe = soc_camera_probe,
  600. .remove = soc_camera_remove,
  601. };
  602. static struct device_driver ic_drv = {
  603. .name = "camera",
  604. .bus = &soc_camera_bus_type,
  605. .owner = THIS_MODULE,
  606. };
  607. /*
  608. * Image capture host - this is a host device, not a bus device, so,
  609. * no bus reference, no probing.
  610. */
  611. static struct class soc_camera_host_class = {
  612. .owner = THIS_MODULE,
  613. .name = "camera_host",
  614. };
  615. static void dummy_release(struct device *dev)
  616. {
  617. }
  618. static spinlock_t *spinlock_alloc(struct soc_camera_file *icf)
  619. {
  620. spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
  621. if (lock)
  622. spin_lock_init(lock);
  623. return lock;
  624. }
  625. static void spinlock_free(spinlock_t *lock)
  626. {
  627. kfree(lock);
  628. }
  629. int soc_camera_host_register(struct soc_camera_host *ici)
  630. {
  631. int ret;
  632. struct soc_camera_host *ix;
  633. if (!ici->vbq_ops || !ici->ops->add || !ici->ops->remove)
  634. return -EINVAL;
  635. /* Number might be equal to the platform device ID */
  636. sprintf(ici->dev.bus_id, "camera_host%d", ici->nr);
  637. ici->dev.class = &soc_camera_host_class;
  638. mutex_lock(&list_lock);
  639. list_for_each_entry(ix, &hosts, list) {
  640. if (ix->nr == ici->nr) {
  641. mutex_unlock(&list_lock);
  642. return -EBUSY;
  643. }
  644. }
  645. list_add_tail(&ici->list, &hosts);
  646. mutex_unlock(&list_lock);
  647. ici->dev.release = dummy_release;
  648. ret = device_register(&ici->dev);
  649. if (ret)
  650. goto edevr;
  651. if (!ici->ops->spinlock_alloc) {
  652. ici->ops->spinlock_alloc = spinlock_alloc;
  653. ici->ops->spinlock_free = spinlock_free;
  654. }
  655. scan_add_host(ici);
  656. return 0;
  657. edevr:
  658. mutex_lock(&list_lock);
  659. list_del(&ici->list);
  660. mutex_unlock(&list_lock);
  661. return ret;
  662. }
  663. EXPORT_SYMBOL(soc_camera_host_register);
  664. /* Unregister all clients! */
  665. void soc_camera_host_unregister(struct soc_camera_host *ici)
  666. {
  667. struct soc_camera_device *icd;
  668. mutex_lock(&list_lock);
  669. list_del(&ici->list);
  670. list_for_each_entry(icd, &devices, list) {
  671. if (icd->dev.parent == &ici->dev) {
  672. device_unregister(&icd->dev);
  673. /* Not before device_unregister(), .remove
  674. * needs parent to call ici->ops->remove() */
  675. icd->dev.parent = NULL;
  676. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  677. }
  678. }
  679. mutex_unlock(&list_lock);
  680. device_unregister(&ici->dev);
  681. }
  682. EXPORT_SYMBOL(soc_camera_host_unregister);
  683. /* Image capture device */
  684. int soc_camera_device_register(struct soc_camera_device *icd)
  685. {
  686. struct soc_camera_device *ix;
  687. int num = -1, i;
  688. if (!icd)
  689. return -EINVAL;
  690. for (i = 0; i < 256 && num < 0; i++) {
  691. num = i;
  692. list_for_each_entry(ix, &devices, list) {
  693. if (ix->iface == icd->iface && ix->devnum == i) {
  694. num = -1;
  695. break;
  696. }
  697. }
  698. }
  699. if (num < 0)
  700. /* ok, we have 256 cameras on this host...
  701. * man, stay reasonable... */
  702. return -ENOMEM;
  703. icd->devnum = num;
  704. icd->dev.bus = &soc_camera_bus_type;
  705. snprintf(icd->dev.bus_id, sizeof(icd->dev.bus_id),
  706. "%u-%u", icd->iface, icd->devnum);
  707. icd->dev.release = dummy_release;
  708. return scan_add_device(icd);
  709. }
  710. EXPORT_SYMBOL(soc_camera_device_register);
  711. void soc_camera_device_unregister(struct soc_camera_device *icd)
  712. {
  713. mutex_lock(&list_lock);
  714. list_del(&icd->list);
  715. /* The bus->remove will be eventually called */
  716. if (icd->dev.parent)
  717. device_unregister(&icd->dev);
  718. mutex_unlock(&list_lock);
  719. }
  720. EXPORT_SYMBOL(soc_camera_device_unregister);
  721. int soc_camera_video_start(struct soc_camera_device *icd)
  722. {
  723. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  724. int err = -ENOMEM;
  725. struct video_device *vdev;
  726. if (!icd->dev.parent)
  727. return -ENODEV;
  728. vdev = video_device_alloc();
  729. if (!vdev)
  730. goto evidallocd;
  731. dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
  732. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  733. /* Maybe better &ici->dev */
  734. vdev->dev = &icd->dev;
  735. vdev->type = VID_TYPE_CAPTURE;
  736. vdev->current_norm = V4L2_STD_UNKNOWN;
  737. vdev->fops = &soc_camera_fops;
  738. vdev->release = video_device_release;
  739. vdev->minor = -1;
  740. vdev->tvnorms = V4L2_STD_UNKNOWN,
  741. vdev->vidioc_querycap = soc_camera_querycap;
  742. vdev->vidioc_g_fmt_cap = soc_camera_g_fmt_cap;
  743. vdev->vidioc_enum_fmt_cap = soc_camera_enum_fmt_cap;
  744. vdev->vidioc_s_fmt_cap = soc_camera_s_fmt_cap;
  745. vdev->vidioc_enum_input = soc_camera_enum_input;
  746. vdev->vidioc_g_input = soc_camera_g_input;
  747. vdev->vidioc_s_input = soc_camera_s_input;
  748. vdev->vidioc_s_std = soc_camera_s_std;
  749. vdev->vidioc_reqbufs = soc_camera_reqbufs;
  750. vdev->vidioc_try_fmt_cap = soc_camera_try_fmt_cap;
  751. vdev->vidioc_querybuf = soc_camera_querybuf;
  752. vdev->vidioc_qbuf = soc_camera_qbuf;
  753. vdev->vidioc_dqbuf = soc_camera_dqbuf;
  754. vdev->vidioc_streamon = soc_camera_streamon;
  755. vdev->vidioc_streamoff = soc_camera_streamoff;
  756. vdev->vidioc_queryctrl = soc_camera_queryctrl;
  757. vdev->vidioc_g_ctrl = soc_camera_g_ctrl;
  758. vdev->vidioc_s_ctrl = soc_camera_s_ctrl;
  759. vdev->vidioc_cropcap = soc_camera_cropcap;
  760. vdev->vidioc_g_crop = soc_camera_g_crop;
  761. vdev->vidioc_s_crop = soc_camera_s_crop;
  762. vdev->vidioc_g_chip_ident = soc_camera_g_chip_ident;
  763. #ifdef CONFIG_VIDEO_ADV_DEBUG
  764. vdev->vidioc_g_register = soc_camera_g_register;
  765. vdev->vidioc_s_register = soc_camera_s_register;
  766. #endif
  767. icd->current_fmt = &icd->formats[0];
  768. err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
  769. if (err < 0) {
  770. dev_err(vdev->dev, "video_register_device failed\n");
  771. goto evidregd;
  772. }
  773. icd->vdev = vdev;
  774. return 0;
  775. evidregd:
  776. video_device_release(vdev);
  777. evidallocd:
  778. return err;
  779. }
  780. EXPORT_SYMBOL(soc_camera_video_start);
  781. void soc_camera_video_stop(struct soc_camera_device *icd)
  782. {
  783. struct video_device *vdev = icd->vdev;
  784. dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
  785. if (!icd->dev.parent || !vdev)
  786. return;
  787. mutex_lock(&video_lock);
  788. video_unregister_device(vdev);
  789. icd->vdev = NULL;
  790. mutex_unlock(&video_lock);
  791. }
  792. EXPORT_SYMBOL(soc_camera_video_stop);
  793. static int __init soc_camera_init(void)
  794. {
  795. int ret = bus_register(&soc_camera_bus_type);
  796. if (ret)
  797. return ret;
  798. ret = driver_register(&ic_drv);
  799. if (ret)
  800. goto edrvr;
  801. ret = class_register(&soc_camera_host_class);
  802. if (ret)
  803. goto eclr;
  804. return 0;
  805. eclr:
  806. driver_unregister(&ic_drv);
  807. edrvr:
  808. bus_unregister(&soc_camera_bus_type);
  809. return ret;
  810. }
  811. static void __exit soc_camera_exit(void)
  812. {
  813. class_unregister(&soc_camera_host_class);
  814. driver_unregister(&ic_drv);
  815. bus_unregister(&soc_camera_bus_type);
  816. }
  817. module_init(soc_camera_init);
  818. module_exit(soc_camera_exit);
  819. MODULE_DESCRIPTION("Image capture bus driver");
  820. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  821. MODULE_LICENSE("GPL");