soc_camera.c 36 KB

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