soc_camera.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. /* test physical bus parameters */
  67. ret = ici->try_bus_param(icd, f->fmt.pix.pixelformat);
  68. if (ret)
  69. return ret;
  70. /* limit format to hardware capabilities */
  71. ret = ici->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->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. 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->owner)) {
  163. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  164. ret = -EINVAL;
  165. goto emgi;
  166. }
  167. icd->use_count++;
  168. icf->icd = icd;
  169. /* Now we really have to activate the camera */
  170. if (icd->use_count == 1) {
  171. ret = ici->add(icd);
  172. if (ret < 0) {
  173. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  174. icd->use_count--;
  175. goto eiciadd;
  176. }
  177. }
  178. mutex_unlock(&video_lock);
  179. file->private_data = icf;
  180. dev_dbg(&icd->dev, "camera device open\n");
  181. /* We must pass NULL as dev pointer, then all pci_* dma operations
  182. * transform to normal dma_* ones. Do we need an irqlock? */
  183. videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, NULL,
  184. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  185. ici->msize, icd);
  186. return 0;
  187. /* All errors are entered with the video_lock held */
  188. eiciadd:
  189. module_put(ici->owner);
  190. emgi:
  191. module_put(icd->ops->owner);
  192. emgd:
  193. mutex_unlock(&video_lock);
  194. vfree(icf);
  195. return ret;
  196. }
  197. static int soc_camera_close(struct inode *inode, struct file *file)
  198. {
  199. struct soc_camera_file *icf = file->private_data;
  200. struct soc_camera_device *icd = icf->icd;
  201. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  202. struct video_device *vdev = icd->vdev;
  203. mutex_lock(&video_lock);
  204. icd->use_count--;
  205. if (!icd->use_count)
  206. ici->remove(icd);
  207. module_put(icd->ops->owner);
  208. module_put(ici->owner);
  209. mutex_unlock(&video_lock);
  210. vfree(file->private_data);
  211. dev_dbg(vdev->dev, "camera device close\n");
  212. return 0;
  213. }
  214. static int soc_camera_read(struct file *file, char __user *buf,
  215. size_t count, loff_t *ppos)
  216. {
  217. struct soc_camera_file *icf = file->private_data;
  218. struct soc_camera_device *icd = icf->icd;
  219. struct video_device *vdev = icd->vdev;
  220. int err = -EINVAL;
  221. dev_err(vdev->dev, "camera device read not implemented\n");
  222. return err;
  223. }
  224. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  225. {
  226. struct soc_camera_file *icf = file->private_data;
  227. struct soc_camera_device *icd = icf->icd;
  228. int err;
  229. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  230. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  231. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  232. (unsigned long)vma->vm_start,
  233. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  234. err);
  235. return err;
  236. }
  237. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  238. {
  239. struct soc_camera_file *icf = file->private_data;
  240. struct soc_camera_device *icd = icf->icd;
  241. struct soc_camera_host *ici =
  242. to_soc_camera_host(icd->dev.parent);
  243. if (list_empty(&icf->vb_vidq.stream)) {
  244. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  245. return POLLERR;
  246. }
  247. return ici->poll(file, pt);
  248. }
  249. static struct file_operations soc_camera_fops = {
  250. .owner = THIS_MODULE,
  251. .open = soc_camera_open,
  252. .release = soc_camera_close,
  253. .ioctl = video_ioctl2,
  254. .read = soc_camera_read,
  255. .mmap = soc_camera_mmap,
  256. .poll = soc_camera_poll,
  257. .llseek = no_llseek,
  258. };
  259. static int soc_camera_s_fmt_cap(struct file *file, void *priv,
  260. struct v4l2_format *f)
  261. {
  262. struct soc_camera_file *icf = file->private_data;
  263. struct soc_camera_device *icd = icf->icd;
  264. struct soc_camera_host *ici =
  265. to_soc_camera_host(icd->dev.parent);
  266. int ret;
  267. struct v4l2_rect rect;
  268. const static struct soc_camera_data_format *data_fmt;
  269. WARN_ON(priv != file->private_data);
  270. data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
  271. if (!data_fmt)
  272. return -EINVAL;
  273. /* buswidth may be further adjusted by the ici */
  274. icd->buswidth = data_fmt->depth;
  275. ret = soc_camera_try_fmt_cap(file, icf, f);
  276. if (ret < 0)
  277. return ret;
  278. rect.left = icd->x_current;
  279. rect.top = icd->y_current;
  280. rect.width = f->fmt.pix.width;
  281. rect.height = f->fmt.pix.height;
  282. ret = ici->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
  283. if (ret < 0)
  284. return ret;
  285. icd->current_fmt = data_fmt;
  286. icd->width = rect.width;
  287. icd->height = rect.height;
  288. icf->vb_vidq.field = f->fmt.pix.field;
  289. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
  290. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  291. f->type);
  292. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  293. icd->width, icd->height);
  294. /* set physical bus parameters */
  295. return ici->set_bus_param(icd, f->fmt.pix.pixelformat);
  296. }
  297. static int soc_camera_enum_fmt_cap(struct file *file, void *priv,
  298. struct v4l2_fmtdesc *f)
  299. {
  300. struct soc_camera_file *icf = file->private_data;
  301. struct soc_camera_device *icd = icf->icd;
  302. const struct soc_camera_data_format *format;
  303. WARN_ON(priv != file->private_data);
  304. if (f->index >= icd->ops->num_formats)
  305. return -EINVAL;
  306. format = &icd->ops->formats[f->index];
  307. strlcpy(f->description, format->name, sizeof(f->description));
  308. f->pixelformat = format->fourcc;
  309. return 0;
  310. }
  311. static int soc_camera_g_fmt_cap(struct file *file, void *priv,
  312. struct v4l2_format *f)
  313. {
  314. struct soc_camera_file *icf = file->private_data;
  315. struct soc_camera_device *icd = icf->icd;
  316. WARN_ON(priv != file->private_data);
  317. f->fmt.pix.width = icd->width;
  318. f->fmt.pix.height = icd->height;
  319. f->fmt.pix.field = icf->vb_vidq.field;
  320. f->fmt.pix.pixelformat = icd->current_fmt->fourcc;
  321. f->fmt.pix.bytesperline =
  322. (f->fmt.pix.width * icd->current_fmt->depth) >> 3;
  323. f->fmt.pix.sizeimage =
  324. f->fmt.pix.height * f->fmt.pix.bytesperline;
  325. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  326. icd->current_fmt->fourcc);
  327. return 0;
  328. }
  329. static int soc_camera_querycap(struct file *file, void *priv,
  330. struct v4l2_capability *cap)
  331. {
  332. struct soc_camera_file *icf = file->private_data;
  333. struct soc_camera_device *icd = icf->icd;
  334. struct soc_camera_host *ici =
  335. to_soc_camera_host(icd->dev.parent);
  336. WARN_ON(priv != file->private_data);
  337. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  338. return ici->querycap(ici, cap);
  339. }
  340. static int soc_camera_streamon(struct file *file, void *priv,
  341. enum v4l2_buf_type i)
  342. {
  343. struct soc_camera_file *icf = file->private_data;
  344. struct soc_camera_device *icd = icf->icd;
  345. WARN_ON(priv != file->private_data);
  346. dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
  347. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  348. return -EINVAL;
  349. icd->ops->start_capture(icd);
  350. /* This calls buf_queue from host driver's videobuf_queue_ops */
  351. return videobuf_streamon(&icf->vb_vidq);
  352. }
  353. static int soc_camera_streamoff(struct file *file, void *priv,
  354. enum v4l2_buf_type i)
  355. {
  356. struct soc_camera_file *icf = file->private_data;
  357. struct soc_camera_device *icd = icf->icd;
  358. WARN_ON(priv != file->private_data);
  359. dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
  360. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  361. return -EINVAL;
  362. /* This calls buf_release from host driver's videobuf_queue_ops for all
  363. * remaining buffers. When the last buffer is freed, stop capture */
  364. videobuf_streamoff(&icf->vb_vidq);
  365. icd->ops->stop_capture(icd);
  366. return 0;
  367. }
  368. static int soc_camera_queryctrl(struct file *file, void *priv,
  369. struct v4l2_queryctrl *qc)
  370. {
  371. struct soc_camera_file *icf = file->private_data;
  372. struct soc_camera_device *icd = icf->icd;
  373. int i;
  374. WARN_ON(priv != file->private_data);
  375. if (!qc->id)
  376. return -EINVAL;
  377. for (i = 0; i < icd->ops->num_controls; i++)
  378. if (qc->id == icd->ops->controls[i].id) {
  379. memcpy(qc, &(icd->ops->controls[i]),
  380. sizeof(*qc));
  381. return 0;
  382. }
  383. return -EINVAL;
  384. }
  385. static int soc_camera_g_ctrl(struct file *file, void *priv,
  386. struct v4l2_control *ctrl)
  387. {
  388. struct soc_camera_file *icf = file->private_data;
  389. struct soc_camera_device *icd = icf->icd;
  390. WARN_ON(priv != file->private_data);
  391. switch (ctrl->id) {
  392. case V4L2_CID_GAIN:
  393. if (icd->gain == (unsigned short)~0)
  394. return -EINVAL;
  395. ctrl->value = icd->gain;
  396. return 0;
  397. case V4L2_CID_EXPOSURE:
  398. if (icd->exposure == (unsigned short)~0)
  399. return -EINVAL;
  400. ctrl->value = icd->exposure;
  401. return 0;
  402. }
  403. if (icd->ops->get_control)
  404. return icd->ops->get_control(icd, ctrl);
  405. return -EINVAL;
  406. }
  407. static int soc_camera_s_ctrl(struct file *file, void *priv,
  408. struct v4l2_control *ctrl)
  409. {
  410. struct soc_camera_file *icf = file->private_data;
  411. struct soc_camera_device *icd = icf->icd;
  412. WARN_ON(priv != file->private_data);
  413. if (icd->ops->set_control)
  414. return icd->ops->set_control(icd, ctrl);
  415. return -EINVAL;
  416. }
  417. static int soc_camera_cropcap(struct file *file, void *fh,
  418. struct v4l2_cropcap *a)
  419. {
  420. struct soc_camera_file *icf = file->private_data;
  421. struct soc_camera_device *icd = icf->icd;
  422. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  423. a->bounds.left = icd->x_min;
  424. a->bounds.top = icd->y_min;
  425. a->bounds.width = icd->width_max;
  426. a->bounds.height = icd->height_max;
  427. a->defrect.left = icd->x_min;
  428. a->defrect.top = icd->y_min;
  429. a->defrect.width = 640;
  430. a->defrect.height = 480;
  431. a->pixelaspect.numerator = 1;
  432. a->pixelaspect.denominator = 1;
  433. return 0;
  434. }
  435. static int soc_camera_g_crop(struct file *file, void *fh,
  436. struct v4l2_crop *a)
  437. {
  438. struct soc_camera_file *icf = file->private_data;
  439. struct soc_camera_device *icd = icf->icd;
  440. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  441. a->c.left = icd->x_current;
  442. a->c.top = icd->y_current;
  443. a->c.width = icd->width;
  444. a->c.height = icd->height;
  445. return 0;
  446. }
  447. static int soc_camera_s_crop(struct file *file, void *fh,
  448. struct v4l2_crop *a)
  449. {
  450. struct soc_camera_file *icf = file->private_data;
  451. struct soc_camera_device *icd = icf->icd;
  452. struct soc_camera_host *ici =
  453. to_soc_camera_host(icd->dev.parent);
  454. int ret;
  455. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  456. return -EINVAL;
  457. ret = ici->set_fmt_cap(icd, 0, &a->c);
  458. if (!ret) {
  459. icd->width = a->c.width;
  460. icd->height = a->c.height;
  461. icd->x_current = a->c.left;
  462. icd->y_current = a->c.top;
  463. }
  464. return ret;
  465. }
  466. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  467. struct v4l2_chip_ident *id)
  468. {
  469. struct soc_camera_file *icf = file->private_data;
  470. struct soc_camera_device *icd = icf->icd;
  471. if (!icd->ops->get_chip_id)
  472. return -EINVAL;
  473. return icd->ops->get_chip_id(icd, id);
  474. }
  475. #ifdef CONFIG_VIDEO_ADV_DEBUG
  476. static int soc_camera_g_register(struct file *file, void *fh,
  477. struct v4l2_register *reg)
  478. {
  479. struct soc_camera_file *icf = file->private_data;
  480. struct soc_camera_device *icd = icf->icd;
  481. if (!icd->ops->get_register)
  482. return -EINVAL;
  483. return icd->ops->get_register(icd, reg);
  484. }
  485. static int soc_camera_s_register(struct file *file, void *fh,
  486. struct v4l2_register *reg)
  487. {
  488. struct soc_camera_file *icf = file->private_data;
  489. struct soc_camera_device *icd = icf->icd;
  490. if (!icd->ops->set_register)
  491. return -EINVAL;
  492. return icd->ops->set_register(icd, reg);
  493. }
  494. #endif
  495. static int device_register_link(struct soc_camera_device *icd)
  496. {
  497. int ret = device_register(&icd->dev);
  498. if (ret < 0) {
  499. /* Prevent calling device_unregister() */
  500. icd->dev.parent = NULL;
  501. dev_err(&icd->dev, "Cannot register device: %d\n", ret);
  502. /* Even if probe() was unsuccessful for all registered drivers,
  503. * device_register() returns 0, and we add the link, just to
  504. * document this camera's control device */
  505. } else if (icd->control)
  506. /* Have to sysfs_remove_link() before device_unregister()? */
  507. if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
  508. "control"))
  509. dev_warn(&icd->dev,
  510. "Failed creating the control symlink\n");
  511. return ret;
  512. }
  513. /* So far this function cannot fail */
  514. static void scan_add_host(struct soc_camera_host *ici)
  515. {
  516. struct soc_camera_device *icd;
  517. mutex_lock(&list_lock);
  518. list_for_each_entry(icd, &devices, list) {
  519. if (icd->iface == ici->nr) {
  520. icd->dev.parent = &ici->dev;
  521. device_register_link(icd);
  522. }
  523. }
  524. mutex_unlock(&list_lock);
  525. }
  526. /* return: 0 if no match found or a match found and
  527. * device_register() successful, error code otherwise */
  528. static int scan_add_device(struct soc_camera_device *icd)
  529. {
  530. struct soc_camera_host *ici;
  531. int ret = 0;
  532. mutex_lock(&list_lock);
  533. list_add_tail(&icd->list, &devices);
  534. /* Watch out for class_for_each_device / class_find_device API by
  535. * Dave Young <hidave.darkstar@gmail.com> */
  536. list_for_each_entry(ici, &hosts, list) {
  537. if (icd->iface == ici->nr) {
  538. ret = 1;
  539. icd->dev.parent = &ici->dev;
  540. break;
  541. }
  542. }
  543. mutex_unlock(&list_lock);
  544. if (ret)
  545. ret = device_register_link(icd);
  546. return ret;
  547. }
  548. static int soc_camera_probe(struct device *dev)
  549. {
  550. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  551. struct soc_camera_host *ici =
  552. to_soc_camera_host(icd->dev.parent);
  553. int ret;
  554. if (!icd->probe)
  555. return -ENODEV;
  556. /* We only call ->add() here to activate and probe the camera.
  557. * We shall ->remove() and deactivate it immediately afterwards. */
  558. ret = ici->add(icd);
  559. if (ret < 0)
  560. return ret;
  561. ret = icd->probe(icd);
  562. if (ret >= 0) {
  563. const struct v4l2_queryctrl *qctrl;
  564. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  565. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  566. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  567. icd->exposure = qctrl ? qctrl->default_value :
  568. (unsigned short)~0;
  569. }
  570. ici->remove(icd);
  571. return ret;
  572. }
  573. /* This is called on device_unregister, which only means we have to disconnect
  574. * from the host, but not remove ourselves from the device list */
  575. static int soc_camera_remove(struct device *dev)
  576. {
  577. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  578. if (icd->remove)
  579. icd->remove(icd);
  580. return 0;
  581. }
  582. static struct bus_type soc_camera_bus_type = {
  583. .name = "soc-camera",
  584. .probe = soc_camera_probe,
  585. .remove = soc_camera_remove,
  586. };
  587. static struct device_driver ic_drv = {
  588. .name = "camera",
  589. .bus = &soc_camera_bus_type,
  590. .owner = THIS_MODULE,
  591. };
  592. /*
  593. * Image capture host - this is a host device, not a bus device, so,
  594. * no bus reference, no probing.
  595. */
  596. static struct class soc_camera_host_class = {
  597. .owner = THIS_MODULE,
  598. .name = "camera_host",
  599. };
  600. static void dummy_release(struct device *dev)
  601. {
  602. }
  603. int soc_camera_host_register(struct soc_camera_host *ici, struct module *owner)
  604. {
  605. int ret;
  606. struct soc_camera_host *ix;
  607. if (!ici->vbq_ops || !ici->add || !ici->remove || !owner)
  608. return -EINVAL;
  609. /* Number might be equal to the platform device ID */
  610. sprintf(ici->dev.bus_id, "camera_host%d", ici->nr);
  611. ici->dev.class = &soc_camera_host_class;
  612. mutex_lock(&list_lock);
  613. list_for_each_entry(ix, &hosts, list) {
  614. if (ix->nr == ici->nr) {
  615. mutex_unlock(&list_lock);
  616. return -EBUSY;
  617. }
  618. }
  619. list_add_tail(&ici->list, &hosts);
  620. mutex_unlock(&list_lock);
  621. ici->owner = owner;
  622. ici->dev.release = dummy_release;
  623. ret = device_register(&ici->dev);
  624. if (ret)
  625. goto edevr;
  626. scan_add_host(ici);
  627. return 0;
  628. edevr:
  629. mutex_lock(&list_lock);
  630. list_del(&ici->list);
  631. mutex_unlock(&list_lock);
  632. return ret;
  633. }
  634. EXPORT_SYMBOL(soc_camera_host_register);
  635. /* Unregister all clients! */
  636. void soc_camera_host_unregister(struct soc_camera_host *ici)
  637. {
  638. struct soc_camera_device *icd;
  639. mutex_lock(&list_lock);
  640. list_del(&ici->list);
  641. list_for_each_entry(icd, &devices, list) {
  642. if (icd->dev.parent == &ici->dev) {
  643. device_unregister(&icd->dev);
  644. /* Not before device_unregister(), .remove
  645. * needs parent to call ici->remove() */
  646. icd->dev.parent = NULL;
  647. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  648. }
  649. }
  650. mutex_unlock(&list_lock);
  651. device_unregister(&ici->dev);
  652. }
  653. EXPORT_SYMBOL(soc_camera_host_unregister);
  654. /* Image capture device */
  655. int soc_camera_device_register(struct soc_camera_device *icd)
  656. {
  657. struct soc_camera_device *ix;
  658. int num = -1, i;
  659. if (!icd)
  660. return -EINVAL;
  661. for (i = 0; i < 256 && num < 0; i++) {
  662. num = i;
  663. list_for_each_entry(ix, &devices, list) {
  664. if (ix->iface == icd->iface && ix->devnum == i) {
  665. num = -1;
  666. break;
  667. }
  668. }
  669. }
  670. if (num < 0)
  671. /* ok, we have 256 cameras on this host...
  672. * man, stay reasonable... */
  673. return -ENOMEM;
  674. icd->devnum = num;
  675. icd->dev.bus = &soc_camera_bus_type;
  676. snprintf(icd->dev.bus_id, sizeof(icd->dev.bus_id),
  677. "%u-%u", icd->iface, icd->devnum);
  678. icd->dev.release = dummy_release;
  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");