soc_camera.c 28 KB

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