soc_camera.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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 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 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 v4l2_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. };
  330. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  331. struct v4l2_format *f)
  332. {
  333. struct soc_camera_file *icf = file->private_data;
  334. struct soc_camera_device *icd = icf->icd;
  335. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  336. struct v4l2_pix_format *pix = &f->fmt.pix;
  337. __u32 pixfmt = pix->pixelformat;
  338. int ret;
  339. struct v4l2_rect rect;
  340. WARN_ON(priv != file->private_data);
  341. ret = soc_camera_try_fmt_vid_cap(file, priv, f);
  342. if (ret < 0)
  343. return ret;
  344. mutex_lock(&icf->vb_vidq.vb_lock);
  345. if (videobuf_queue_is_busy(&icf->vb_vidq)) {
  346. dev_err(&icd->dev, "S_FMT denied: queue busy\n");
  347. ret = -EBUSY;
  348. goto unlock;
  349. }
  350. rect.left = icd->x_current;
  351. rect.top = icd->y_current;
  352. rect.width = pix->width;
  353. rect.height = pix->height;
  354. ret = ici->ops->set_fmt(icd, pix->pixelformat, &rect);
  355. if (ret < 0) {
  356. goto unlock;
  357. } else if (!icd->current_fmt ||
  358. icd->current_fmt->fourcc != pixfmt) {
  359. dev_err(&ici->dev,
  360. "Host driver hasn't set up current format correctly!\n");
  361. ret = -EINVAL;
  362. goto unlock;
  363. }
  364. icd->width = rect.width;
  365. icd->height = rect.height;
  366. icf->vb_vidq.field = pix->field;
  367. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  368. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  369. f->type);
  370. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  371. icd->width, icd->height);
  372. /* set physical bus parameters */
  373. ret = ici->ops->set_bus_param(icd, pixfmt);
  374. unlock:
  375. mutex_unlock(&icf->vb_vidq.vb_lock);
  376. return ret;
  377. }
  378. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  379. struct v4l2_fmtdesc *f)
  380. {
  381. struct soc_camera_file *icf = file->private_data;
  382. struct soc_camera_device *icd = icf->icd;
  383. const struct soc_camera_data_format *format;
  384. WARN_ON(priv != file->private_data);
  385. if (f->index >= icd->num_user_formats)
  386. return -EINVAL;
  387. format = icd->user_formats[f->index].host_fmt;
  388. strlcpy(f->description, format->name, sizeof(f->description));
  389. f->pixelformat = format->fourcc;
  390. return 0;
  391. }
  392. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  393. struct v4l2_format *f)
  394. {
  395. struct soc_camera_file *icf = file->private_data;
  396. struct soc_camera_device *icd = icf->icd;
  397. struct v4l2_pix_format *pix = &f->fmt.pix;
  398. WARN_ON(priv != file->private_data);
  399. pix->width = icd->width;
  400. pix->height = icd->height;
  401. pix->field = icf->vb_vidq.field;
  402. pix->pixelformat = icd->current_fmt->fourcc;
  403. pix->bytesperline = pix->width *
  404. DIV_ROUND_UP(icd->current_fmt->depth, 8);
  405. pix->sizeimage = pix->height * pix->bytesperline;
  406. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  407. icd->current_fmt->fourcc);
  408. return 0;
  409. }
  410. static int soc_camera_querycap(struct file *file, void *priv,
  411. struct v4l2_capability *cap)
  412. {
  413. struct soc_camera_file *icf = file->private_data;
  414. struct soc_camera_device *icd = icf->icd;
  415. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  416. WARN_ON(priv != file->private_data);
  417. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  418. return ici->ops->querycap(ici, cap);
  419. }
  420. static int soc_camera_streamon(struct file *file, void *priv,
  421. enum v4l2_buf_type i)
  422. {
  423. struct soc_camera_file *icf = file->private_data;
  424. struct soc_camera_device *icd = icf->icd;
  425. int ret;
  426. WARN_ON(priv != file->private_data);
  427. dev_dbg(&icd->dev, "%s\n", __func__);
  428. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  429. return -EINVAL;
  430. mutex_lock(&icd->video_lock);
  431. icd->ops->start_capture(icd);
  432. /* This calls buf_queue from host driver's videobuf_queue_ops */
  433. ret = videobuf_streamon(&icf->vb_vidq);
  434. mutex_unlock(&icd->video_lock);
  435. return ret;
  436. }
  437. static int soc_camera_streamoff(struct file *file, void *priv,
  438. enum v4l2_buf_type i)
  439. {
  440. struct soc_camera_file *icf = file->private_data;
  441. struct soc_camera_device *icd = icf->icd;
  442. WARN_ON(priv != file->private_data);
  443. dev_dbg(&icd->dev, "%s\n", __func__);
  444. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  445. return -EINVAL;
  446. mutex_lock(&icd->video_lock);
  447. /* This calls buf_release from host driver's videobuf_queue_ops for all
  448. * remaining buffers. When the last buffer is freed, stop capture */
  449. videobuf_streamoff(&icf->vb_vidq);
  450. icd->ops->stop_capture(icd);
  451. mutex_unlock(&icd->video_lock);
  452. return 0;
  453. }
  454. static int soc_camera_queryctrl(struct file *file, void *priv,
  455. struct v4l2_queryctrl *qc)
  456. {
  457. struct soc_camera_file *icf = file->private_data;
  458. struct soc_camera_device *icd = icf->icd;
  459. int i;
  460. WARN_ON(priv != file->private_data);
  461. if (!qc->id)
  462. return -EINVAL;
  463. for (i = 0; i < icd->ops->num_controls; i++)
  464. if (qc->id == icd->ops->controls[i].id) {
  465. memcpy(qc, &(icd->ops->controls[i]),
  466. sizeof(*qc));
  467. return 0;
  468. }
  469. return -EINVAL;
  470. }
  471. static int soc_camera_g_ctrl(struct file *file, void *priv,
  472. struct v4l2_control *ctrl)
  473. {
  474. struct soc_camera_file *icf = file->private_data;
  475. struct soc_camera_device *icd = icf->icd;
  476. WARN_ON(priv != file->private_data);
  477. switch (ctrl->id) {
  478. case V4L2_CID_GAIN:
  479. if (icd->gain == (unsigned short)~0)
  480. return -EINVAL;
  481. ctrl->value = icd->gain;
  482. return 0;
  483. case V4L2_CID_EXPOSURE:
  484. if (icd->exposure == (unsigned short)~0)
  485. return -EINVAL;
  486. ctrl->value = icd->exposure;
  487. return 0;
  488. }
  489. if (icd->ops->get_control)
  490. return icd->ops->get_control(icd, ctrl);
  491. return -EINVAL;
  492. }
  493. static int soc_camera_s_ctrl(struct file *file, void *priv,
  494. struct v4l2_control *ctrl)
  495. {
  496. struct soc_camera_file *icf = file->private_data;
  497. struct soc_camera_device *icd = icf->icd;
  498. WARN_ON(priv != file->private_data);
  499. if (icd->ops->set_control)
  500. return icd->ops->set_control(icd, ctrl);
  501. return -EINVAL;
  502. }
  503. static int soc_camera_cropcap(struct file *file, void *fh,
  504. struct v4l2_cropcap *a)
  505. {
  506. struct soc_camera_file *icf = file->private_data;
  507. struct soc_camera_device *icd = icf->icd;
  508. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  509. a->bounds.left = icd->x_min;
  510. a->bounds.top = icd->y_min;
  511. a->bounds.width = icd->width_max;
  512. a->bounds.height = icd->height_max;
  513. a->defrect.left = icd->x_min;
  514. a->defrect.top = icd->y_min;
  515. a->defrect.width = 640;
  516. a->defrect.height = 480;
  517. a->pixelaspect.numerator = 1;
  518. a->pixelaspect.denominator = 1;
  519. return 0;
  520. }
  521. static int soc_camera_g_crop(struct file *file, void *fh,
  522. struct v4l2_crop *a)
  523. {
  524. struct soc_camera_file *icf = file->private_data;
  525. struct soc_camera_device *icd = icf->icd;
  526. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  527. a->c.left = icd->x_current;
  528. a->c.top = icd->y_current;
  529. a->c.width = icd->width;
  530. a->c.height = icd->height;
  531. return 0;
  532. }
  533. static int soc_camera_s_crop(struct file *file, void *fh,
  534. struct v4l2_crop *a)
  535. {
  536. struct soc_camera_file *icf = file->private_data;
  537. struct soc_camera_device *icd = icf->icd;
  538. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  539. int ret;
  540. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  541. return -EINVAL;
  542. /* Cropping is allowed during a running capture, guard consistency */
  543. mutex_lock(&icf->vb_vidq.vb_lock);
  544. ret = ici->ops->set_fmt(icd, 0, &a->c);
  545. if (!ret) {
  546. icd->width = a->c.width;
  547. icd->height = a->c.height;
  548. icd->x_current = a->c.left;
  549. icd->y_current = a->c.top;
  550. }
  551. mutex_unlock(&icf->vb_vidq.vb_lock);
  552. return ret;
  553. }
  554. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  555. struct v4l2_dbg_chip_ident *id)
  556. {
  557. struct soc_camera_file *icf = file->private_data;
  558. struct soc_camera_device *icd = icf->icd;
  559. if (!icd->ops->get_chip_id)
  560. return -EINVAL;
  561. return icd->ops->get_chip_id(icd, id);
  562. }
  563. #ifdef CONFIG_VIDEO_ADV_DEBUG
  564. static int soc_camera_g_register(struct file *file, void *fh,
  565. struct v4l2_dbg_register *reg)
  566. {
  567. struct soc_camera_file *icf = file->private_data;
  568. struct soc_camera_device *icd = icf->icd;
  569. if (!icd->ops->get_register)
  570. return -EINVAL;
  571. return icd->ops->get_register(icd, reg);
  572. }
  573. static int soc_camera_s_register(struct file *file, void *fh,
  574. struct v4l2_dbg_register *reg)
  575. {
  576. struct soc_camera_file *icf = file->private_data;
  577. struct soc_camera_device *icd = icf->icd;
  578. if (!icd->ops->set_register)
  579. return -EINVAL;
  580. return icd->ops->set_register(icd, reg);
  581. }
  582. #endif
  583. static int device_register_link(struct soc_camera_device *icd)
  584. {
  585. int ret = device_register(&icd->dev);
  586. if (ret < 0) {
  587. /* Prevent calling device_unregister() */
  588. icd->dev.parent = NULL;
  589. dev_err(&icd->dev, "Cannot register device: %d\n", ret);
  590. /* Even if probe() was unsuccessful for all registered drivers,
  591. * device_register() returns 0, and we add the link, just to
  592. * document this camera's control device */
  593. } else if (icd->control)
  594. /* Have to sysfs_remove_link() before device_unregister()? */
  595. if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
  596. "control"))
  597. dev_warn(&icd->dev,
  598. "Failed creating the control symlink\n");
  599. return ret;
  600. }
  601. /* So far this function cannot fail */
  602. static void scan_add_host(struct soc_camera_host *ici)
  603. {
  604. struct soc_camera_device *icd;
  605. mutex_lock(&list_lock);
  606. list_for_each_entry(icd, &devices, list) {
  607. if (icd->iface == ici->nr) {
  608. icd->dev.parent = &ici->dev;
  609. device_register_link(icd);
  610. }
  611. }
  612. mutex_unlock(&list_lock);
  613. }
  614. /* return: 0 if no match found or a match found and
  615. * device_register() successful, error code otherwise */
  616. static int scan_add_device(struct soc_camera_device *icd)
  617. {
  618. struct soc_camera_host *ici;
  619. int ret = 0;
  620. mutex_lock(&list_lock);
  621. list_add_tail(&icd->list, &devices);
  622. /* Watch out for class_for_each_device / class_find_device API by
  623. * Dave Young <hidave.darkstar@gmail.com> */
  624. list_for_each_entry(ici, &hosts, list) {
  625. if (icd->iface == ici->nr) {
  626. ret = 1;
  627. icd->dev.parent = &ici->dev;
  628. break;
  629. }
  630. }
  631. mutex_unlock(&list_lock);
  632. if (ret)
  633. ret = device_register_link(icd);
  634. return ret;
  635. }
  636. static int soc_camera_probe(struct device *dev)
  637. {
  638. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  639. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  640. int ret;
  641. /*
  642. * Possible race scenario:
  643. * modprobe <camera-host-driver> triggers __func__
  644. * at this moment respective <camera-sensor-driver> gets rmmod'ed
  645. * to protect take module references.
  646. */
  647. if (!try_module_get(icd->ops->owner)) {
  648. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  649. ret = -EINVAL;
  650. goto emgd;
  651. }
  652. if (!try_module_get(ici->ops->owner)) {
  653. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  654. ret = -EINVAL;
  655. goto emgi;
  656. }
  657. mutex_lock(&icd->video_lock);
  658. /* We only call ->add() here to activate and probe the camera.
  659. * We shall ->remove() and deactivate it immediately afterwards. */
  660. ret = ici->ops->add(icd);
  661. if (ret < 0)
  662. goto eiadd;
  663. ret = icd->ops->probe(icd);
  664. if (ret >= 0) {
  665. const struct v4l2_queryctrl *qctrl;
  666. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  667. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  668. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  669. icd->exposure = qctrl ? qctrl->default_value :
  670. (unsigned short)~0;
  671. }
  672. ici->ops->remove(icd);
  673. eiadd:
  674. mutex_unlock(&icd->video_lock);
  675. module_put(ici->ops->owner);
  676. emgi:
  677. module_put(icd->ops->owner);
  678. emgd:
  679. return ret;
  680. }
  681. /* This is called on device_unregister, which only means we have to disconnect
  682. * from the host, but not remove ourselves from the device list */
  683. static int soc_camera_remove(struct device *dev)
  684. {
  685. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  686. if (icd->ops->remove)
  687. icd->ops->remove(icd);
  688. return 0;
  689. }
  690. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  691. {
  692. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  693. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  694. int ret = 0;
  695. if (ici->ops->suspend)
  696. ret = ici->ops->suspend(icd, state);
  697. return ret;
  698. }
  699. static int soc_camera_resume(struct device *dev)
  700. {
  701. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  702. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  703. int ret = 0;
  704. if (ici->ops->resume)
  705. ret = ici->ops->resume(icd);
  706. return ret;
  707. }
  708. static struct bus_type soc_camera_bus_type = {
  709. .name = "soc-camera",
  710. .probe = soc_camera_probe,
  711. .remove = soc_camera_remove,
  712. .suspend = soc_camera_suspend,
  713. .resume = soc_camera_resume,
  714. };
  715. static struct device_driver ic_drv = {
  716. .name = "camera",
  717. .bus = &soc_camera_bus_type,
  718. .owner = THIS_MODULE,
  719. };
  720. static void dummy_release(struct device *dev)
  721. {
  722. }
  723. int soc_camera_host_register(struct soc_camera_host *ici)
  724. {
  725. int ret;
  726. struct soc_camera_host *ix;
  727. if (!ici || !ici->ops ||
  728. !ici->ops->try_fmt ||
  729. !ici->ops->set_fmt ||
  730. !ici->ops->set_bus_param ||
  731. !ici->ops->querycap ||
  732. !ici->ops->init_videobuf ||
  733. !ici->ops->reqbufs ||
  734. !ici->ops->add ||
  735. !ici->ops->remove ||
  736. !ici->ops->poll)
  737. return -EINVAL;
  738. /* Number might be equal to the platform device ID */
  739. dev_set_name(&ici->dev, "camera_host%d", ici->nr);
  740. mutex_lock(&list_lock);
  741. list_for_each_entry(ix, &hosts, list) {
  742. if (ix->nr == ici->nr) {
  743. mutex_unlock(&list_lock);
  744. return -EBUSY;
  745. }
  746. }
  747. list_add_tail(&ici->list, &hosts);
  748. mutex_unlock(&list_lock);
  749. ici->dev.release = dummy_release;
  750. ret = device_register(&ici->dev);
  751. if (ret)
  752. goto edevr;
  753. scan_add_host(ici);
  754. return 0;
  755. edevr:
  756. mutex_lock(&list_lock);
  757. list_del(&ici->list);
  758. mutex_unlock(&list_lock);
  759. return ret;
  760. }
  761. EXPORT_SYMBOL(soc_camera_host_register);
  762. /* Unregister all clients! */
  763. void soc_camera_host_unregister(struct soc_camera_host *ici)
  764. {
  765. struct soc_camera_device *icd;
  766. mutex_lock(&list_lock);
  767. list_del(&ici->list);
  768. list_for_each_entry(icd, &devices, list) {
  769. if (icd->dev.parent == &ici->dev) {
  770. device_unregister(&icd->dev);
  771. /* Not before device_unregister(), .remove
  772. * needs parent to call ici->ops->remove() */
  773. icd->dev.parent = NULL;
  774. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  775. }
  776. }
  777. mutex_unlock(&list_lock);
  778. device_unregister(&ici->dev);
  779. }
  780. EXPORT_SYMBOL(soc_camera_host_unregister);
  781. /* Image capture device */
  782. int soc_camera_device_register(struct soc_camera_device *icd)
  783. {
  784. struct soc_camera_device *ix;
  785. int num = -1, i;
  786. if (!icd || !icd->ops ||
  787. !icd->ops->probe ||
  788. !icd->ops->init ||
  789. !icd->ops->release ||
  790. !icd->ops->start_capture ||
  791. !icd->ops->stop_capture ||
  792. !icd->ops->set_fmt ||
  793. !icd->ops->try_fmt ||
  794. !icd->ops->query_bus_param ||
  795. !icd->ops->set_bus_param)
  796. return -EINVAL;
  797. for (i = 0; i < 256 && num < 0; i++) {
  798. num = i;
  799. list_for_each_entry(ix, &devices, list) {
  800. if (ix->iface == icd->iface && ix->devnum == i) {
  801. num = -1;
  802. break;
  803. }
  804. }
  805. }
  806. if (num < 0)
  807. /* ok, we have 256 cameras on this host...
  808. * man, stay reasonable... */
  809. return -ENOMEM;
  810. icd->devnum = num;
  811. icd->dev.bus = &soc_camera_bus_type;
  812. dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
  813. icd->dev.release = dummy_release;
  814. icd->use_count = 0;
  815. icd->host_priv = NULL;
  816. mutex_init(&icd->video_lock);
  817. return scan_add_device(icd);
  818. }
  819. EXPORT_SYMBOL(soc_camera_device_register);
  820. void soc_camera_device_unregister(struct soc_camera_device *icd)
  821. {
  822. mutex_lock(&list_lock);
  823. list_del(&icd->list);
  824. /* The bus->remove will be eventually called */
  825. if (icd->dev.parent)
  826. device_unregister(&icd->dev);
  827. mutex_unlock(&list_lock);
  828. }
  829. EXPORT_SYMBOL(soc_camera_device_unregister);
  830. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  831. .vidioc_querycap = soc_camera_querycap,
  832. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  833. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  834. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  835. .vidioc_enum_input = soc_camera_enum_input,
  836. .vidioc_g_input = soc_camera_g_input,
  837. .vidioc_s_input = soc_camera_s_input,
  838. .vidioc_s_std = soc_camera_s_std,
  839. .vidioc_reqbufs = soc_camera_reqbufs,
  840. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  841. .vidioc_querybuf = soc_camera_querybuf,
  842. .vidioc_qbuf = soc_camera_qbuf,
  843. .vidioc_dqbuf = soc_camera_dqbuf,
  844. .vidioc_streamon = soc_camera_streamon,
  845. .vidioc_streamoff = soc_camera_streamoff,
  846. .vidioc_queryctrl = soc_camera_queryctrl,
  847. .vidioc_g_ctrl = soc_camera_g_ctrl,
  848. .vidioc_s_ctrl = soc_camera_s_ctrl,
  849. .vidioc_cropcap = soc_camera_cropcap,
  850. .vidioc_g_crop = soc_camera_g_crop,
  851. .vidioc_s_crop = soc_camera_s_crop,
  852. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  853. #ifdef CONFIG_VIDEO_ADV_DEBUG
  854. .vidioc_g_register = soc_camera_g_register,
  855. .vidioc_s_register = soc_camera_s_register,
  856. #endif
  857. };
  858. /*
  859. * Usually called from the struct soc_camera_ops .probe() method, i.e., from
  860. * soc_camera_probe() above with .video_lock held
  861. */
  862. int soc_camera_video_start(struct soc_camera_device *icd)
  863. {
  864. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  865. int err = -ENOMEM;
  866. struct video_device *vdev;
  867. if (!icd->dev.parent)
  868. return -ENODEV;
  869. vdev = video_device_alloc();
  870. if (!vdev)
  871. goto evidallocd;
  872. dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
  873. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  874. vdev->parent = &icd->dev;
  875. vdev->current_norm = V4L2_STD_UNKNOWN;
  876. vdev->fops = &soc_camera_fops;
  877. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  878. vdev->release = video_device_release;
  879. vdev->minor = -1;
  880. vdev->tvnorms = V4L2_STD_UNKNOWN,
  881. err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
  882. if (err < 0) {
  883. dev_err(vdev->parent, "video_register_device failed\n");
  884. goto evidregd;
  885. }
  886. icd->vdev = vdev;
  887. return 0;
  888. evidregd:
  889. video_device_release(vdev);
  890. evidallocd:
  891. return err;
  892. }
  893. EXPORT_SYMBOL(soc_camera_video_start);
  894. void soc_camera_video_stop(struct soc_camera_device *icd)
  895. {
  896. struct video_device *vdev = icd->vdev;
  897. dev_dbg(&icd->dev, "%s\n", __func__);
  898. if (!icd->dev.parent || !vdev)
  899. return;
  900. mutex_lock(&icd->video_lock);
  901. video_unregister_device(vdev);
  902. icd->vdev = NULL;
  903. mutex_unlock(&icd->video_lock);
  904. }
  905. EXPORT_SYMBOL(soc_camera_video_stop);
  906. static int __init soc_camera_init(void)
  907. {
  908. int ret = bus_register(&soc_camera_bus_type);
  909. if (ret)
  910. return ret;
  911. ret = driver_register(&ic_drv);
  912. if (ret)
  913. goto edrvr;
  914. return 0;
  915. edrvr:
  916. bus_unregister(&soc_camera_bus_type);
  917. return ret;
  918. }
  919. static void __exit soc_camera_exit(void)
  920. {
  921. driver_unregister(&ic_drv);
  922. bus_unregister(&soc_camera_bus_type);
  923. }
  924. module_init(soc_camera_init);
  925. module_exit(soc_camera_exit);
  926. MODULE_DESCRIPTION("Image capture bus driver");
  927. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  928. MODULE_LICENSE("GPL");