soc_camera.c 26 KB

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