soc_camera.c 24 KB

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