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