soc_camera.c 28 KB

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