soc_camera.c 39 KB

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