soc_camera.c 37 KB

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