soc_camera.c 23 KB

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