soc_camera.c 27 KB

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