soc_camera.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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/device.h>
  19. #include <linux/err.h>
  20. #include <linux/i2c.h>
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/mutex.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/vmalloc.h>
  29. #include <media/soc_camera.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-dev.h>
  33. #include <media/videobuf-core.h>
  34. #include <media/soc_mediabus.h>
  35. /* Default to VGA resolution */
  36. #define DEFAULT_WIDTH 640
  37. #define DEFAULT_HEIGHT 480
  38. static LIST_HEAD(hosts);
  39. static LIST_HEAD(devices);
  40. static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
  41. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  42. struct soc_camera_device *icd, unsigned int fourcc)
  43. {
  44. unsigned int i;
  45. for (i = 0; i < icd->num_user_formats; i++)
  46. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  47. return icd->user_formats + i;
  48. return NULL;
  49. }
  50. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  51. /**
  52. * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  53. * @icl: camera platform parameters
  54. * @flags: flags to be inverted according to platform configuration
  55. * @return: resulting flags
  56. */
  57. unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
  58. unsigned long flags)
  59. {
  60. unsigned long f;
  61. /* If only one of the two polarities is supported, switch to the opposite */
  62. if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  63. f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
  64. if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
  65. flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
  66. }
  67. if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  68. f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
  69. if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
  70. flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
  71. }
  72. if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
  73. f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
  74. if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
  75. flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
  76. }
  77. return flags;
  78. }
  79. EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
  80. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  81. struct v4l2_format *f)
  82. {
  83. struct soc_camera_file *icf = file->private_data;
  84. struct soc_camera_device *icd = icf->icd;
  85. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  86. WARN_ON(priv != file->private_data);
  87. /* limit format to hardware capabilities */
  88. return ici->ops->try_fmt(icd, f);
  89. }
  90. static int soc_camera_enum_input(struct file *file, void *priv,
  91. struct v4l2_input *inp)
  92. {
  93. struct soc_camera_file *icf = file->private_data;
  94. struct soc_camera_device *icd = icf->icd;
  95. int ret = 0;
  96. if (inp->index != 0)
  97. return -EINVAL;
  98. if (icd->ops->enum_input)
  99. ret = icd->ops->enum_input(icd, inp);
  100. else {
  101. /* default is camera */
  102. inp->type = V4L2_INPUT_TYPE_CAMERA;
  103. inp->std = V4L2_STD_UNKNOWN;
  104. strcpy(inp->name, "Camera");
  105. }
  106. return ret;
  107. }
  108. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  109. {
  110. *i = 0;
  111. return 0;
  112. }
  113. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  114. {
  115. if (i > 0)
  116. return -EINVAL;
  117. return 0;
  118. }
  119. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
  120. {
  121. struct soc_camera_file *icf = file->private_data;
  122. struct soc_camera_device *icd = icf->icd;
  123. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  124. return v4l2_subdev_call(sd, core, s_std, *a);
  125. }
  126. static int soc_camera_reqbufs(struct file *file, void *priv,
  127. struct v4l2_requestbuffers *p)
  128. {
  129. int ret;
  130. struct soc_camera_file *icf = file->private_data;
  131. struct soc_camera_device *icd = icf->icd;
  132. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  133. WARN_ON(priv != file->private_data);
  134. ret = videobuf_reqbufs(&icf->vb_vidq, p);
  135. if (ret < 0)
  136. return ret;
  137. return ici->ops->reqbufs(icf, p);
  138. }
  139. static int soc_camera_querybuf(struct file *file, void *priv,
  140. struct v4l2_buffer *p)
  141. {
  142. struct soc_camera_file *icf = file->private_data;
  143. WARN_ON(priv != file->private_data);
  144. return videobuf_querybuf(&icf->vb_vidq, p);
  145. }
  146. static int soc_camera_qbuf(struct file *file, void *priv,
  147. struct v4l2_buffer *p)
  148. {
  149. struct soc_camera_file *icf = file->private_data;
  150. WARN_ON(priv != file->private_data);
  151. return videobuf_qbuf(&icf->vb_vidq, p);
  152. }
  153. static int soc_camera_dqbuf(struct file *file, void *priv,
  154. struct v4l2_buffer *p)
  155. {
  156. struct soc_camera_file *icf = file->private_data;
  157. WARN_ON(priv != file->private_data);
  158. return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
  159. }
  160. /* Always entered with .video_lock held */
  161. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  162. {
  163. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  164. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  165. unsigned int i, fmts = 0, raw_fmts = 0;
  166. int ret;
  167. enum v4l2_mbus_pixelcode code;
  168. while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
  169. raw_fmts++;
  170. if (!ici->ops->get_formats)
  171. /*
  172. * Fallback mode - the host will have to serve all
  173. * sensor-provided formats one-to-one to the user
  174. */
  175. fmts = raw_fmts;
  176. else
  177. /*
  178. * First pass - only count formats this host-sensor
  179. * configuration can provide
  180. */
  181. for (i = 0; i < raw_fmts; i++) {
  182. ret = ici->ops->get_formats(icd, i, NULL);
  183. if (ret < 0)
  184. return ret;
  185. fmts += ret;
  186. }
  187. if (!fmts)
  188. return -ENXIO;
  189. icd->user_formats =
  190. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  191. if (!icd->user_formats)
  192. return -ENOMEM;
  193. icd->num_user_formats = fmts;
  194. dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
  195. /* Second pass - actually fill data formats */
  196. fmts = 0;
  197. for (i = 0; i < raw_fmts; i++)
  198. if (!ici->ops->get_formats) {
  199. v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
  200. icd->user_formats[i].host_fmt =
  201. soc_mbus_get_fmtdesc(code);
  202. icd->user_formats[i].code = code;
  203. } else {
  204. ret = ici->ops->get_formats(icd, i,
  205. &icd->user_formats[fmts]);
  206. if (ret < 0)
  207. goto egfmt;
  208. fmts += ret;
  209. }
  210. icd->current_fmt = &icd->user_formats[0];
  211. return 0;
  212. egfmt:
  213. icd->num_user_formats = 0;
  214. vfree(icd->user_formats);
  215. return ret;
  216. }
  217. /* Always entered with .video_lock held */
  218. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  219. {
  220. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  221. if (ici->ops->put_formats)
  222. ici->ops->put_formats(icd);
  223. icd->current_fmt = NULL;
  224. icd->num_user_formats = 0;
  225. vfree(icd->user_formats);
  226. icd->user_formats = NULL;
  227. }
  228. #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
  229. ((x) >> 24) & 0xff
  230. /* Called with .vb_lock held, or from the first open(2), see comment there */
  231. static int soc_camera_set_fmt(struct soc_camera_file *icf,
  232. struct v4l2_format *f)
  233. {
  234. struct soc_camera_device *icd = icf->icd;
  235. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  236. struct v4l2_pix_format *pix = &f->fmt.pix;
  237. int ret;
  238. dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
  239. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  240. /* We always call try_fmt() before set_fmt() or set_crop() */
  241. ret = ici->ops->try_fmt(icd, f);
  242. if (ret < 0)
  243. return ret;
  244. ret = ici->ops->set_fmt(icd, f);
  245. if (ret < 0) {
  246. return ret;
  247. } else if (!icd->current_fmt ||
  248. icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
  249. dev_err(&icd->dev,
  250. "Host driver hasn't set up current format correctly!\n");
  251. return -EINVAL;
  252. }
  253. icd->user_width = pix->width;
  254. icd->user_height = pix->height;
  255. icd->colorspace = pix->colorspace;
  256. icf->vb_vidq.field =
  257. icd->field = pix->field;
  258. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  259. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  260. f->type);
  261. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  262. icd->user_width, icd->user_height);
  263. /* set physical bus parameters */
  264. return ici->ops->set_bus_param(icd, pix->pixelformat);
  265. }
  266. static int soc_camera_open(struct file *file)
  267. {
  268. struct video_device *vdev = video_devdata(file);
  269. struct soc_camera_device *icd = container_of(vdev->parent,
  270. struct soc_camera_device,
  271. dev);
  272. struct soc_camera_link *icl = to_soc_camera_link(icd);
  273. struct soc_camera_host *ici;
  274. struct soc_camera_file *icf;
  275. int ret;
  276. if (!icd->ops)
  277. /* No device driver attached */
  278. return -ENODEV;
  279. ici = to_soc_camera_host(icd->dev.parent);
  280. icf = vmalloc(sizeof(*icf));
  281. if (!icf)
  282. return -ENOMEM;
  283. if (!try_module_get(ici->ops->owner)) {
  284. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  285. ret = -EINVAL;
  286. goto emgi;
  287. }
  288. /*
  289. * Protect against icd->ops->remove() until we module_get() both
  290. * drivers.
  291. */
  292. mutex_lock(&icd->video_lock);
  293. icf->icd = icd;
  294. icd->use_count++;
  295. /* Now we really have to activate the camera */
  296. if (icd->use_count == 1) {
  297. /* Restore parameters before the last close() per V4L2 API */
  298. struct v4l2_format f = {
  299. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  300. .fmt.pix = {
  301. .width = icd->user_width,
  302. .height = icd->user_height,
  303. .field = icd->field,
  304. .colorspace = icd->colorspace,
  305. .pixelformat =
  306. icd->current_fmt->host_fmt->fourcc,
  307. },
  308. };
  309. if (icl->power) {
  310. ret = icl->power(icd->pdev, 1);
  311. if (ret < 0)
  312. goto epower;
  313. }
  314. /* The camera could have been already on, try to reset */
  315. if (icl->reset)
  316. icl->reset(icd->pdev);
  317. ret = ici->ops->add(icd);
  318. if (ret < 0) {
  319. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  320. goto eiciadd;
  321. }
  322. pm_runtime_enable(&icd->vdev->dev);
  323. ret = pm_runtime_resume(&icd->vdev->dev);
  324. if (ret < 0 && ret != -ENOSYS)
  325. goto eresume;
  326. /*
  327. * Try to configure with default parameters. Notice: this is the
  328. * very first open, so, we cannot race against other calls,
  329. * apart from someone else calling open() simultaneously, but
  330. * .video_lock is protecting us against it.
  331. */
  332. ret = soc_camera_set_fmt(icf, &f);
  333. if (ret < 0)
  334. goto esfmt;
  335. }
  336. file->private_data = icf;
  337. dev_dbg(&icd->dev, "camera device open\n");
  338. ici->ops->init_videobuf(&icf->vb_vidq, icd);
  339. mutex_unlock(&icd->video_lock);
  340. return 0;
  341. /*
  342. * First four errors are entered with the .video_lock held
  343. * and use_count == 1
  344. */
  345. esfmt:
  346. pm_runtime_disable(&icd->vdev->dev);
  347. eresume:
  348. ici->ops->remove(icd);
  349. eiciadd:
  350. if (icl->power)
  351. icl->power(icd->pdev, 0);
  352. epower:
  353. icd->use_count--;
  354. mutex_unlock(&icd->video_lock);
  355. module_put(ici->ops->owner);
  356. emgi:
  357. vfree(icf);
  358. return ret;
  359. }
  360. static int soc_camera_close(struct file *file)
  361. {
  362. struct soc_camera_file *icf = file->private_data;
  363. struct soc_camera_device *icd = icf->icd;
  364. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  365. mutex_lock(&icd->video_lock);
  366. icd->use_count--;
  367. if (!icd->use_count) {
  368. struct soc_camera_link *icl = to_soc_camera_link(icd);
  369. pm_runtime_suspend(&icd->vdev->dev);
  370. pm_runtime_disable(&icd->vdev->dev);
  371. ici->ops->remove(icd);
  372. if (icl->power)
  373. icl->power(icd->pdev, 0);
  374. }
  375. mutex_unlock(&icd->video_lock);
  376. module_put(ici->ops->owner);
  377. vfree(icf);
  378. dev_dbg(&icd->dev, "camera device close\n");
  379. return 0;
  380. }
  381. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  382. size_t count, loff_t *ppos)
  383. {
  384. struct soc_camera_file *icf = file->private_data;
  385. struct soc_camera_device *icd = icf->icd;
  386. int err = -EINVAL;
  387. dev_err(&icd->dev, "camera device read not implemented\n");
  388. return err;
  389. }
  390. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  391. {
  392. struct soc_camera_file *icf = file->private_data;
  393. struct soc_camera_device *icd = icf->icd;
  394. int err;
  395. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  396. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  397. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  398. (unsigned long)vma->vm_start,
  399. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  400. err);
  401. return err;
  402. }
  403. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  404. {
  405. struct soc_camera_file *icf = file->private_data;
  406. struct soc_camera_device *icd = icf->icd;
  407. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  408. if (list_empty(&icf->vb_vidq.stream)) {
  409. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  410. return POLLERR;
  411. }
  412. return ici->ops->poll(file, pt);
  413. }
  414. static struct v4l2_file_operations soc_camera_fops = {
  415. .owner = THIS_MODULE,
  416. .open = soc_camera_open,
  417. .release = soc_camera_close,
  418. .ioctl = video_ioctl2,
  419. .read = soc_camera_read,
  420. .mmap = soc_camera_mmap,
  421. .poll = soc_camera_poll,
  422. };
  423. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  424. struct v4l2_format *f)
  425. {
  426. struct soc_camera_file *icf = file->private_data;
  427. struct soc_camera_device *icd = icf->icd;
  428. int ret;
  429. WARN_ON(priv != file->private_data);
  430. mutex_lock(&icf->vb_vidq.vb_lock);
  431. if (icf->vb_vidq.bufs[0]) {
  432. dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
  433. ret = -EBUSY;
  434. goto unlock;
  435. }
  436. ret = soc_camera_set_fmt(icf, f);
  437. unlock:
  438. mutex_unlock(&icf->vb_vidq.vb_lock);
  439. return ret;
  440. }
  441. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  442. struct v4l2_fmtdesc *f)
  443. {
  444. struct soc_camera_file *icf = file->private_data;
  445. struct soc_camera_device *icd = icf->icd;
  446. const struct soc_mbus_pixelfmt *format;
  447. WARN_ON(priv != file->private_data);
  448. if (f->index >= icd->num_user_formats)
  449. return -EINVAL;
  450. format = icd->user_formats[f->index].host_fmt;
  451. if (format->name)
  452. strlcpy(f->description, format->name, sizeof(f->description));
  453. f->pixelformat = format->fourcc;
  454. return 0;
  455. }
  456. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  457. struct v4l2_format *f)
  458. {
  459. struct soc_camera_file *icf = file->private_data;
  460. struct soc_camera_device *icd = icf->icd;
  461. struct v4l2_pix_format *pix = &f->fmt.pix;
  462. WARN_ON(priv != file->private_data);
  463. pix->width = icd->user_width;
  464. pix->height = icd->user_height;
  465. pix->field = icf->vb_vidq.field;
  466. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  467. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  468. icd->current_fmt->host_fmt);
  469. pix->colorspace = icd->colorspace;
  470. if (pix->bytesperline < 0)
  471. return pix->bytesperline;
  472. pix->sizeimage = pix->height * pix->bytesperline;
  473. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  474. icd->current_fmt->host_fmt->fourcc);
  475. return 0;
  476. }
  477. static int soc_camera_querycap(struct file *file, void *priv,
  478. struct v4l2_capability *cap)
  479. {
  480. struct soc_camera_file *icf = file->private_data;
  481. struct soc_camera_device *icd = icf->icd;
  482. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  483. WARN_ON(priv != file->private_data);
  484. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  485. return ici->ops->querycap(ici, cap);
  486. }
  487. static int soc_camera_streamon(struct file *file, void *priv,
  488. enum v4l2_buf_type i)
  489. {
  490. struct soc_camera_file *icf = file->private_data;
  491. struct soc_camera_device *icd = icf->icd;
  492. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  493. int ret;
  494. WARN_ON(priv != file->private_data);
  495. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  496. return -EINVAL;
  497. mutex_lock(&icd->video_lock);
  498. v4l2_subdev_call(sd, video, s_stream, 1);
  499. /* This calls buf_queue from host driver's videobuf_queue_ops */
  500. ret = videobuf_streamon(&icf->vb_vidq);
  501. mutex_unlock(&icd->video_lock);
  502. return ret;
  503. }
  504. static int soc_camera_streamoff(struct file *file, void *priv,
  505. enum v4l2_buf_type i)
  506. {
  507. struct soc_camera_file *icf = file->private_data;
  508. struct soc_camera_device *icd = icf->icd;
  509. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  510. WARN_ON(priv != file->private_data);
  511. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  512. return -EINVAL;
  513. mutex_lock(&icd->video_lock);
  514. /*
  515. * This calls buf_release from host driver's videobuf_queue_ops for all
  516. * remaining buffers. When the last buffer is freed, stop capture
  517. */
  518. videobuf_streamoff(&icf->vb_vidq);
  519. v4l2_subdev_call(sd, video, s_stream, 0);
  520. mutex_unlock(&icd->video_lock);
  521. return 0;
  522. }
  523. static int soc_camera_queryctrl(struct file *file, void *priv,
  524. struct v4l2_queryctrl *qc)
  525. {
  526. struct soc_camera_file *icf = file->private_data;
  527. struct soc_camera_device *icd = icf->icd;
  528. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  529. int i;
  530. WARN_ON(priv != file->private_data);
  531. if (!qc->id)
  532. return -EINVAL;
  533. /* First check host controls */
  534. for (i = 0; i < ici->ops->num_controls; i++)
  535. if (qc->id == ici->ops->controls[i].id) {
  536. memcpy(qc, &(ici->ops->controls[i]),
  537. sizeof(*qc));
  538. return 0;
  539. }
  540. /* Then device controls */
  541. for (i = 0; i < icd->ops->num_controls; i++)
  542. if (qc->id == icd->ops->controls[i].id) {
  543. memcpy(qc, &(icd->ops->controls[i]),
  544. sizeof(*qc));
  545. return 0;
  546. }
  547. return -EINVAL;
  548. }
  549. static int soc_camera_g_ctrl(struct file *file, void *priv,
  550. struct v4l2_control *ctrl)
  551. {
  552. struct soc_camera_file *icf = file->private_data;
  553. struct soc_camera_device *icd = icf->icd;
  554. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  555. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  556. int ret;
  557. WARN_ON(priv != file->private_data);
  558. if (ici->ops->get_ctrl) {
  559. ret = ici->ops->get_ctrl(icd, ctrl);
  560. if (ret != -ENOIOCTLCMD)
  561. return ret;
  562. }
  563. return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
  564. }
  565. static int soc_camera_s_ctrl(struct file *file, void *priv,
  566. struct v4l2_control *ctrl)
  567. {
  568. struct soc_camera_file *icf = file->private_data;
  569. struct soc_camera_device *icd = icf->icd;
  570. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  571. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  572. int ret;
  573. WARN_ON(priv != file->private_data);
  574. if (ici->ops->set_ctrl) {
  575. ret = ici->ops->set_ctrl(icd, ctrl);
  576. if (ret != -ENOIOCTLCMD)
  577. return ret;
  578. }
  579. return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
  580. }
  581. static int soc_camera_cropcap(struct file *file, void *fh,
  582. struct v4l2_cropcap *a)
  583. {
  584. struct soc_camera_file *icf = file->private_data;
  585. struct soc_camera_device *icd = icf->icd;
  586. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  587. return ici->ops->cropcap(icd, a);
  588. }
  589. static int soc_camera_g_crop(struct file *file, void *fh,
  590. struct v4l2_crop *a)
  591. {
  592. struct soc_camera_file *icf = file->private_data;
  593. struct soc_camera_device *icd = icf->icd;
  594. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  595. int ret;
  596. mutex_lock(&icf->vb_vidq.vb_lock);
  597. ret = ici->ops->get_crop(icd, a);
  598. mutex_unlock(&icf->vb_vidq.vb_lock);
  599. return ret;
  600. }
  601. /*
  602. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  603. * argument with the actual geometry, instead, the user shall use G_CROP to
  604. * retrieve it.
  605. */
  606. static int soc_camera_s_crop(struct file *file, void *fh,
  607. struct v4l2_crop *a)
  608. {
  609. struct soc_camera_file *icf = file->private_data;
  610. struct soc_camera_device *icd = icf->icd;
  611. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  612. struct v4l2_rect *rect = &a->c;
  613. struct v4l2_crop current_crop;
  614. int ret;
  615. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  616. return -EINVAL;
  617. dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
  618. rect->width, rect->height, rect->left, rect->top);
  619. /* Cropping is allowed during a running capture, guard consistency */
  620. mutex_lock(&icf->vb_vidq.vb_lock);
  621. /* If get_crop fails, we'll let host and / or client drivers decide */
  622. ret = ici->ops->get_crop(icd, &current_crop);
  623. /* Prohibit window size change with initialised buffers */
  624. if (icf->vb_vidq.bufs[0] && !ret &&
  625. (a->c.width != current_crop.c.width ||
  626. a->c.height != current_crop.c.height)) {
  627. dev_err(&icd->dev,
  628. "S_CROP denied: queue initialised and sizes differ\n");
  629. ret = -EBUSY;
  630. } else {
  631. ret = ici->ops->set_crop(icd, a);
  632. }
  633. mutex_unlock(&icf->vb_vidq.vb_lock);
  634. return ret;
  635. }
  636. static int soc_camera_g_parm(struct file *file, void *fh,
  637. struct v4l2_streamparm *a)
  638. {
  639. struct soc_camera_file *icf = file->private_data;
  640. struct soc_camera_device *icd = icf->icd;
  641. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  642. if (ici->ops->get_parm)
  643. return ici->ops->get_parm(icd, a);
  644. return -ENOIOCTLCMD;
  645. }
  646. static int soc_camera_s_parm(struct file *file, void *fh,
  647. struct v4l2_streamparm *a)
  648. {
  649. struct soc_camera_file *icf = file->private_data;
  650. struct soc_camera_device *icd = icf->icd;
  651. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  652. if (ici->ops->set_parm)
  653. return ici->ops->set_parm(icd, a);
  654. return -ENOIOCTLCMD;
  655. }
  656. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  657. struct v4l2_dbg_chip_ident *id)
  658. {
  659. struct soc_camera_file *icf = file->private_data;
  660. struct soc_camera_device *icd = icf->icd;
  661. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  662. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  663. }
  664. #ifdef CONFIG_VIDEO_ADV_DEBUG
  665. static int soc_camera_g_register(struct file *file, void *fh,
  666. struct v4l2_dbg_register *reg)
  667. {
  668. struct soc_camera_file *icf = file->private_data;
  669. struct soc_camera_device *icd = icf->icd;
  670. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  671. return v4l2_subdev_call(sd, core, g_register, reg);
  672. }
  673. static int soc_camera_s_register(struct file *file, void *fh,
  674. struct v4l2_dbg_register *reg)
  675. {
  676. struct soc_camera_file *icf = file->private_data;
  677. struct soc_camera_device *icd = icf->icd;
  678. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  679. return v4l2_subdev_call(sd, core, s_register, reg);
  680. }
  681. #endif
  682. /* So far this function cannot fail */
  683. static void scan_add_host(struct soc_camera_host *ici)
  684. {
  685. struct soc_camera_device *icd;
  686. mutex_lock(&list_lock);
  687. list_for_each_entry(icd, &devices, list) {
  688. if (icd->iface == ici->nr) {
  689. int ret;
  690. icd->dev.parent = ici->v4l2_dev.dev;
  691. dev_set_name(&icd->dev, "%u-%u", icd->iface,
  692. icd->devnum);
  693. ret = device_register(&icd->dev);
  694. if (ret < 0) {
  695. icd->dev.parent = NULL;
  696. dev_err(&icd->dev,
  697. "Cannot register device: %d\n", ret);
  698. }
  699. }
  700. }
  701. mutex_unlock(&list_lock);
  702. }
  703. #ifdef CONFIG_I2C_BOARDINFO
  704. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  705. struct soc_camera_link *icl)
  706. {
  707. struct i2c_client *client;
  708. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  709. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  710. struct v4l2_subdev *subdev;
  711. if (!adap) {
  712. dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
  713. icl->i2c_adapter_id);
  714. goto ei2cga;
  715. }
  716. icl->board_info->platform_data = icd;
  717. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  718. icl->module_name, icl->board_info, NULL);
  719. if (!subdev)
  720. goto ei2cnd;
  721. client = subdev->priv;
  722. /* Use to_i2c_client(dev) to recover the i2c client */
  723. dev_set_drvdata(&icd->dev, &client->dev);
  724. return 0;
  725. ei2cnd:
  726. i2c_put_adapter(adap);
  727. ei2cga:
  728. return -ENODEV;
  729. }
  730. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  731. {
  732. struct i2c_client *client =
  733. to_i2c_client(to_soc_camera_control(icd));
  734. dev_set_drvdata(&icd->dev, NULL);
  735. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  736. i2c_unregister_device(client);
  737. i2c_put_adapter(client->adapter);
  738. }
  739. #else
  740. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  741. #define soc_camera_free_i2c(icd) do {} while (0)
  742. #endif
  743. static int soc_camera_video_start(struct soc_camera_device *icd);
  744. static int video_dev_create(struct soc_camera_device *icd);
  745. /* Called during host-driver probe */
  746. static int soc_camera_probe(struct device *dev)
  747. {
  748. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  749. struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
  750. struct soc_camera_link *icl = to_soc_camera_link(icd);
  751. struct device *control = NULL;
  752. struct v4l2_subdev *sd;
  753. struct v4l2_mbus_framefmt mf;
  754. int ret;
  755. dev_info(dev, "Probing %s\n", dev_name(dev));
  756. if (icl->power) {
  757. ret = icl->power(icd->pdev, 1);
  758. if (ret < 0) {
  759. dev_err(dev,
  760. "Platform failed to power-on the camera.\n");
  761. goto epower;
  762. }
  763. }
  764. /* The camera could have been already on, try to reset */
  765. if (icl->reset)
  766. icl->reset(icd->pdev);
  767. ret = ici->ops->add(icd);
  768. if (ret < 0)
  769. goto eadd;
  770. /* Must have icd->vdev before registering the device */
  771. ret = video_dev_create(icd);
  772. if (ret < 0)
  773. goto evdc;
  774. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  775. if (icl->board_info) {
  776. ret = soc_camera_init_i2c(icd, icl);
  777. if (ret < 0)
  778. goto eadddev;
  779. } else if (!icl->add_device || !icl->del_device) {
  780. ret = -EINVAL;
  781. goto eadddev;
  782. } else {
  783. if (icl->module_name)
  784. ret = request_module(icl->module_name);
  785. ret = icl->add_device(icl, &icd->dev);
  786. if (ret < 0)
  787. goto eadddev;
  788. /*
  789. * FIXME: this is racy, have to use driver-binding notification,
  790. * when it is available
  791. */
  792. control = to_soc_camera_control(icd);
  793. if (!control || !control->driver || !dev_get_drvdata(control) ||
  794. !try_module_get(control->driver->owner)) {
  795. icl->del_device(icl);
  796. goto enodrv;
  797. }
  798. }
  799. /* At this point client .probe() should have run already */
  800. ret = soc_camera_init_user_formats(icd);
  801. if (ret < 0)
  802. goto eiufmt;
  803. icd->field = V4L2_FIELD_ANY;
  804. /* ..._video_start() will create a device node, so we have to protect */
  805. mutex_lock(&icd->video_lock);
  806. ret = soc_camera_video_start(icd);
  807. if (ret < 0)
  808. goto evidstart;
  809. /* Try to improve our guess of a reasonable window format */
  810. sd = soc_camera_to_subdev(icd);
  811. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  812. icd->user_width = mf.width;
  813. icd->user_height = mf.height;
  814. icd->colorspace = mf.colorspace;
  815. icd->field = mf.field;
  816. }
  817. /* Do we have to sysfs_remove_link() before device_unregister()? */
  818. if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  819. "control"))
  820. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  821. ici->ops->remove(icd);
  822. if (icl->power)
  823. icl->power(icd->pdev, 0);
  824. mutex_unlock(&icd->video_lock);
  825. return 0;
  826. evidstart:
  827. mutex_unlock(&icd->video_lock);
  828. soc_camera_free_user_formats(icd);
  829. eiufmt:
  830. if (icl->board_info) {
  831. soc_camera_free_i2c(icd);
  832. } else {
  833. icl->del_device(icl);
  834. module_put(control->driver->owner);
  835. }
  836. enodrv:
  837. eadddev:
  838. video_device_release(icd->vdev);
  839. evdc:
  840. ici->ops->remove(icd);
  841. eadd:
  842. if (icl->power)
  843. icl->power(icd->pdev, 0);
  844. epower:
  845. return ret;
  846. }
  847. /*
  848. * This is called on device_unregister, which only means we have to disconnect
  849. * from the host, but not remove ourselves from the device list
  850. */
  851. static int soc_camera_remove(struct device *dev)
  852. {
  853. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  854. struct soc_camera_link *icl = to_soc_camera_link(icd);
  855. struct video_device *vdev = icd->vdev;
  856. BUG_ON(!dev->parent);
  857. if (vdev) {
  858. mutex_lock(&icd->video_lock);
  859. video_unregister_device(vdev);
  860. icd->vdev = NULL;
  861. mutex_unlock(&icd->video_lock);
  862. }
  863. if (icl->board_info) {
  864. soc_camera_free_i2c(icd);
  865. } else {
  866. struct device_driver *drv = to_soc_camera_control(icd) ?
  867. to_soc_camera_control(icd)->driver : NULL;
  868. if (drv) {
  869. icl->del_device(icl);
  870. module_put(drv->owner);
  871. }
  872. }
  873. soc_camera_free_user_formats(icd);
  874. return 0;
  875. }
  876. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  877. {
  878. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  879. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  880. int ret = 0;
  881. if (ici->ops->suspend)
  882. ret = ici->ops->suspend(icd, state);
  883. return ret;
  884. }
  885. static int soc_camera_resume(struct device *dev)
  886. {
  887. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  888. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  889. int ret = 0;
  890. if (ici->ops->resume)
  891. ret = ici->ops->resume(icd);
  892. return ret;
  893. }
  894. static struct bus_type soc_camera_bus_type = {
  895. .name = "soc-camera",
  896. .probe = soc_camera_probe,
  897. .remove = soc_camera_remove,
  898. .suspend = soc_camera_suspend,
  899. .resume = soc_camera_resume,
  900. };
  901. static struct device_driver ic_drv = {
  902. .name = "camera",
  903. .bus = &soc_camera_bus_type,
  904. .owner = THIS_MODULE,
  905. };
  906. static void dummy_release(struct device *dev)
  907. {
  908. }
  909. static int default_cropcap(struct soc_camera_device *icd,
  910. struct v4l2_cropcap *a)
  911. {
  912. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  913. return v4l2_subdev_call(sd, video, cropcap, a);
  914. }
  915. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  916. {
  917. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  918. return v4l2_subdev_call(sd, video, g_crop, a);
  919. }
  920. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  921. {
  922. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  923. return v4l2_subdev_call(sd, video, s_crop, a);
  924. }
  925. static void soc_camera_device_init(struct device *dev, void *pdata)
  926. {
  927. dev->platform_data = pdata;
  928. dev->bus = &soc_camera_bus_type;
  929. dev->release = dummy_release;
  930. }
  931. int soc_camera_host_register(struct soc_camera_host *ici)
  932. {
  933. struct soc_camera_host *ix;
  934. int ret;
  935. if (!ici || !ici->ops ||
  936. !ici->ops->try_fmt ||
  937. !ici->ops->set_fmt ||
  938. !ici->ops->set_bus_param ||
  939. !ici->ops->querycap ||
  940. !ici->ops->init_videobuf ||
  941. !ici->ops->reqbufs ||
  942. !ici->ops->add ||
  943. !ici->ops->remove ||
  944. !ici->ops->poll ||
  945. !ici->v4l2_dev.dev)
  946. return -EINVAL;
  947. if (!ici->ops->set_crop)
  948. ici->ops->set_crop = default_s_crop;
  949. if (!ici->ops->get_crop)
  950. ici->ops->get_crop = default_g_crop;
  951. if (!ici->ops->cropcap)
  952. ici->ops->cropcap = default_cropcap;
  953. mutex_lock(&list_lock);
  954. list_for_each_entry(ix, &hosts, list) {
  955. if (ix->nr == ici->nr) {
  956. ret = -EBUSY;
  957. goto edevreg;
  958. }
  959. }
  960. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  961. if (ret < 0)
  962. goto edevreg;
  963. list_add_tail(&ici->list, &hosts);
  964. mutex_unlock(&list_lock);
  965. scan_add_host(ici);
  966. return 0;
  967. edevreg:
  968. mutex_unlock(&list_lock);
  969. return ret;
  970. }
  971. EXPORT_SYMBOL(soc_camera_host_register);
  972. /* Unregister all clients! */
  973. void soc_camera_host_unregister(struct soc_camera_host *ici)
  974. {
  975. struct soc_camera_device *icd;
  976. mutex_lock(&list_lock);
  977. list_del(&ici->list);
  978. list_for_each_entry(icd, &devices, list) {
  979. if (icd->iface == ici->nr) {
  980. void *pdata = icd->dev.platform_data;
  981. /* The bus->remove will be called */
  982. device_unregister(&icd->dev);
  983. /*
  984. * Not before device_unregister(), .remove
  985. * needs parent to call ici->ops->remove().
  986. * If the host module is loaded again, device_register()
  987. * would complain "already initialised," since 2.6.32
  988. * this is also needed to prevent use-after-free of the
  989. * device private data.
  990. */
  991. memset(&icd->dev, 0, sizeof(icd->dev));
  992. soc_camera_device_init(&icd->dev, pdata);
  993. }
  994. }
  995. mutex_unlock(&list_lock);
  996. v4l2_device_unregister(&ici->v4l2_dev);
  997. }
  998. EXPORT_SYMBOL(soc_camera_host_unregister);
  999. /* Image capture device */
  1000. static int soc_camera_device_register(struct soc_camera_device *icd)
  1001. {
  1002. struct soc_camera_device *ix;
  1003. int num = -1, i;
  1004. for (i = 0; i < 256 && num < 0; i++) {
  1005. num = i;
  1006. /* Check if this index is available on this interface */
  1007. list_for_each_entry(ix, &devices, list) {
  1008. if (ix->iface == icd->iface && ix->devnum == i) {
  1009. num = -1;
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. if (num < 0)
  1015. /*
  1016. * ok, we have 256 cameras on this host...
  1017. * man, stay reasonable...
  1018. */
  1019. return -ENOMEM;
  1020. icd->devnum = num;
  1021. icd->use_count = 0;
  1022. icd->host_priv = NULL;
  1023. mutex_init(&icd->video_lock);
  1024. list_add_tail(&icd->list, &devices);
  1025. return 0;
  1026. }
  1027. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  1028. {
  1029. list_del(&icd->list);
  1030. }
  1031. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1032. .vidioc_querycap = soc_camera_querycap,
  1033. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1034. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1035. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1036. .vidioc_enum_input = soc_camera_enum_input,
  1037. .vidioc_g_input = soc_camera_g_input,
  1038. .vidioc_s_input = soc_camera_s_input,
  1039. .vidioc_s_std = soc_camera_s_std,
  1040. .vidioc_reqbufs = soc_camera_reqbufs,
  1041. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1042. .vidioc_querybuf = soc_camera_querybuf,
  1043. .vidioc_qbuf = soc_camera_qbuf,
  1044. .vidioc_dqbuf = soc_camera_dqbuf,
  1045. .vidioc_streamon = soc_camera_streamon,
  1046. .vidioc_streamoff = soc_camera_streamoff,
  1047. .vidioc_queryctrl = soc_camera_queryctrl,
  1048. .vidioc_g_ctrl = soc_camera_g_ctrl,
  1049. .vidioc_s_ctrl = soc_camera_s_ctrl,
  1050. .vidioc_cropcap = soc_camera_cropcap,
  1051. .vidioc_g_crop = soc_camera_g_crop,
  1052. .vidioc_s_crop = soc_camera_s_crop,
  1053. .vidioc_g_parm = soc_camera_g_parm,
  1054. .vidioc_s_parm = soc_camera_s_parm,
  1055. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1056. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1057. .vidioc_g_register = soc_camera_g_register,
  1058. .vidioc_s_register = soc_camera_s_register,
  1059. #endif
  1060. };
  1061. static int video_dev_create(struct soc_camera_device *icd)
  1062. {
  1063. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1064. struct video_device *vdev = video_device_alloc();
  1065. if (!vdev)
  1066. return -ENOMEM;
  1067. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1068. vdev->parent = &icd->dev;
  1069. vdev->current_norm = V4L2_STD_UNKNOWN;
  1070. vdev->fops = &soc_camera_fops;
  1071. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1072. vdev->release = video_device_release;
  1073. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1074. icd->vdev = vdev;
  1075. return 0;
  1076. }
  1077. /*
  1078. * Called from soc_camera_probe() above (with .video_lock held???)
  1079. */
  1080. static int soc_camera_video_start(struct soc_camera_device *icd)
  1081. {
  1082. struct device_type *type = icd->vdev->dev.type;
  1083. int ret;
  1084. if (!icd->dev.parent)
  1085. return -ENODEV;
  1086. if (!icd->ops ||
  1087. !icd->ops->query_bus_param ||
  1088. !icd->ops->set_bus_param)
  1089. return -EINVAL;
  1090. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1091. if (ret < 0) {
  1092. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1093. return ret;
  1094. }
  1095. /* Restore device type, possibly set by the subdevice driver */
  1096. icd->vdev->dev.type = type;
  1097. return 0;
  1098. }
  1099. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1100. {
  1101. struct soc_camera_link *icl = pdev->dev.platform_data;
  1102. struct soc_camera_device *icd;
  1103. int ret;
  1104. if (!icl)
  1105. return -EINVAL;
  1106. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1107. if (!icd)
  1108. return -ENOMEM;
  1109. icd->iface = icl->bus_id;
  1110. icd->pdev = &pdev->dev;
  1111. platform_set_drvdata(pdev, icd);
  1112. ret = soc_camera_device_register(icd);
  1113. if (ret < 0)
  1114. goto escdevreg;
  1115. soc_camera_device_init(&icd->dev, icl);
  1116. icd->user_width = DEFAULT_WIDTH;
  1117. icd->user_height = DEFAULT_HEIGHT;
  1118. return 0;
  1119. escdevreg:
  1120. kfree(icd);
  1121. return ret;
  1122. }
  1123. /*
  1124. * Only called on rmmod for each platform device, since they are not
  1125. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1126. * been unloaded already
  1127. */
  1128. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1129. {
  1130. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1131. if (!icd)
  1132. return -EINVAL;
  1133. soc_camera_device_unregister(icd);
  1134. kfree(icd);
  1135. return 0;
  1136. }
  1137. static struct platform_driver __refdata soc_camera_pdrv = {
  1138. .remove = __devexit_p(soc_camera_pdrv_remove),
  1139. .driver = {
  1140. .name = "soc-camera-pdrv",
  1141. .owner = THIS_MODULE,
  1142. },
  1143. };
  1144. static int __init soc_camera_init(void)
  1145. {
  1146. int ret = bus_register(&soc_camera_bus_type);
  1147. if (ret)
  1148. return ret;
  1149. ret = driver_register(&ic_drv);
  1150. if (ret)
  1151. goto edrvr;
  1152. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1153. if (ret)
  1154. goto epdr;
  1155. return 0;
  1156. epdr:
  1157. driver_unregister(&ic_drv);
  1158. edrvr:
  1159. bus_unregister(&soc_camera_bus_type);
  1160. return ret;
  1161. }
  1162. static void __exit soc_camera_exit(void)
  1163. {
  1164. platform_driver_unregister(&soc_camera_pdrv);
  1165. driver_unregister(&ic_drv);
  1166. bus_unregister(&soc_camera_bus_type);
  1167. }
  1168. module_init(soc_camera_init);
  1169. module_exit(soc_camera_exit);
  1170. MODULE_DESCRIPTION("Image capture bus driver");
  1171. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1172. MODULE_LICENSE("GPL");
  1173. MODULE_ALIAS("platform:soc-camera-pdrv");