soc_camera.c 24 KB

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