soc_camera.c 23 KB

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