soc_camera.c 24 KB

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