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. /* This calls buf_queue from host driver's videobuf_queue_ops */
  526. ret = videobuf_streamon(&icd->vb_vidq);
  527. if (!ret)
  528. v4l2_subdev_call(sd, video, s_stream, 1);
  529. return ret;
  530. }
  531. static int soc_camera_streamoff(struct file *file, void *priv,
  532. enum v4l2_buf_type i)
  533. {
  534. struct soc_camera_device *icd = file->private_data;
  535. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  536. WARN_ON(priv != file->private_data);
  537. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  538. return -EINVAL;
  539. if (icd->streamer != file)
  540. return -EBUSY;
  541. /*
  542. * This calls buf_release from host driver's videobuf_queue_ops for all
  543. * remaining buffers. When the last buffer is freed, stop capture
  544. */
  545. videobuf_streamoff(&icd->vb_vidq);
  546. v4l2_subdev_call(sd, video, s_stream, 0);
  547. return 0;
  548. }
  549. static int soc_camera_queryctrl(struct file *file, void *priv,
  550. struct v4l2_queryctrl *qc)
  551. {
  552. struct soc_camera_device *icd = file->private_data;
  553. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  554. int i;
  555. WARN_ON(priv != file->private_data);
  556. if (!qc->id)
  557. return -EINVAL;
  558. /* First check host controls */
  559. for (i = 0; i < ici->ops->num_controls; i++)
  560. if (qc->id == ici->ops->controls[i].id) {
  561. memcpy(qc, &(ici->ops->controls[i]),
  562. sizeof(*qc));
  563. return 0;
  564. }
  565. /* Then device controls */
  566. for (i = 0; i < icd->ops->num_controls; i++)
  567. if (qc->id == icd->ops->controls[i].id) {
  568. memcpy(qc, &(icd->ops->controls[i]),
  569. sizeof(*qc));
  570. return 0;
  571. }
  572. return -EINVAL;
  573. }
  574. static int soc_camera_g_ctrl(struct file *file, void *priv,
  575. struct v4l2_control *ctrl)
  576. {
  577. struct soc_camera_device *icd = file->private_data;
  578. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  579. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  580. int ret;
  581. WARN_ON(priv != file->private_data);
  582. if (ici->ops->get_ctrl) {
  583. ret = ici->ops->get_ctrl(icd, ctrl);
  584. if (ret != -ENOIOCTLCMD)
  585. return ret;
  586. }
  587. return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
  588. }
  589. static int soc_camera_s_ctrl(struct file *file, void *priv,
  590. struct v4l2_control *ctrl)
  591. {
  592. struct soc_camera_device *icd = file->private_data;
  593. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  594. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  595. int ret;
  596. WARN_ON(priv != file->private_data);
  597. if (ici->ops->set_ctrl) {
  598. ret = ici->ops->set_ctrl(icd, ctrl);
  599. if (ret != -ENOIOCTLCMD)
  600. return ret;
  601. }
  602. return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
  603. }
  604. static int soc_camera_cropcap(struct file *file, void *fh,
  605. struct v4l2_cropcap *a)
  606. {
  607. struct soc_camera_device *icd = file->private_data;
  608. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  609. return ici->ops->cropcap(icd, a);
  610. }
  611. static int soc_camera_g_crop(struct file *file, void *fh,
  612. struct v4l2_crop *a)
  613. {
  614. struct soc_camera_device *icd = file->private_data;
  615. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  616. int ret;
  617. ret = ici->ops->get_crop(icd, a);
  618. return ret;
  619. }
  620. /*
  621. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  622. * argument with the actual geometry, instead, the user shall use G_CROP to
  623. * retrieve it.
  624. */
  625. static int soc_camera_s_crop(struct file *file, void *fh,
  626. struct v4l2_crop *a)
  627. {
  628. struct soc_camera_device *icd = file->private_data;
  629. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  630. struct v4l2_rect *rect = &a->c;
  631. struct v4l2_crop current_crop;
  632. int ret;
  633. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  634. return -EINVAL;
  635. dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
  636. rect->width, rect->height, rect->left, rect->top);
  637. /* If get_crop fails, we'll let host and / or client drivers decide */
  638. ret = ici->ops->get_crop(icd, &current_crop);
  639. /* Prohibit window size change with initialised buffers */
  640. if (ret < 0) {
  641. dev_err(&icd->dev,
  642. "S_CROP denied: getting current crop failed\n");
  643. } else if (icd->vb_vidq.bufs[0] &&
  644. (a->c.width != current_crop.c.width ||
  645. a->c.height != current_crop.c.height)) {
  646. dev_err(&icd->dev,
  647. "S_CROP denied: queue initialised and sizes differ\n");
  648. ret = -EBUSY;
  649. } else {
  650. ret = ici->ops->set_crop(icd, a);
  651. }
  652. return ret;
  653. }
  654. static int soc_camera_g_parm(struct file *file, void *fh,
  655. struct v4l2_streamparm *a)
  656. {
  657. struct soc_camera_device *icd = file->private_data;
  658. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  659. if (ici->ops->get_parm)
  660. return ici->ops->get_parm(icd, a);
  661. return -ENOIOCTLCMD;
  662. }
  663. static int soc_camera_s_parm(struct file *file, void *fh,
  664. struct v4l2_streamparm *a)
  665. {
  666. struct soc_camera_device *icd = file->private_data;
  667. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  668. if (ici->ops->set_parm)
  669. return ici->ops->set_parm(icd, a);
  670. return -ENOIOCTLCMD;
  671. }
  672. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  673. struct v4l2_dbg_chip_ident *id)
  674. {
  675. struct soc_camera_device *icd = file->private_data;
  676. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  677. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  678. }
  679. #ifdef CONFIG_VIDEO_ADV_DEBUG
  680. static int soc_camera_g_register(struct file *file, void *fh,
  681. struct v4l2_dbg_register *reg)
  682. {
  683. struct soc_camera_device *icd = file->private_data;
  684. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  685. return v4l2_subdev_call(sd, core, g_register, reg);
  686. }
  687. static int soc_camera_s_register(struct file *file, void *fh,
  688. struct v4l2_dbg_register *reg)
  689. {
  690. struct soc_camera_device *icd = file->private_data;
  691. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  692. return v4l2_subdev_call(sd, core, s_register, reg);
  693. }
  694. #endif
  695. /* So far this function cannot fail */
  696. static void scan_add_host(struct soc_camera_host *ici)
  697. {
  698. struct soc_camera_device *icd;
  699. mutex_lock(&list_lock);
  700. list_for_each_entry(icd, &devices, list) {
  701. if (icd->iface == ici->nr) {
  702. int ret;
  703. icd->dev.parent = ici->v4l2_dev.dev;
  704. dev_set_name(&icd->dev, "%u-%u", icd->iface,
  705. icd->devnum);
  706. ret = device_register(&icd->dev);
  707. if (ret < 0) {
  708. icd->dev.parent = NULL;
  709. dev_err(&icd->dev,
  710. "Cannot register device: %d\n", ret);
  711. }
  712. }
  713. }
  714. mutex_unlock(&list_lock);
  715. }
  716. #ifdef CONFIG_I2C_BOARDINFO
  717. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  718. struct soc_camera_link *icl)
  719. {
  720. struct i2c_client *client;
  721. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  722. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  723. struct v4l2_subdev *subdev;
  724. if (!adap) {
  725. dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
  726. icl->i2c_adapter_id);
  727. goto ei2cga;
  728. }
  729. icl->board_info->platform_data = icd;
  730. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  731. icl->board_info, NULL);
  732. if (!subdev)
  733. goto ei2cnd;
  734. client = v4l2_get_subdevdata(subdev);
  735. /* Use to_i2c_client(dev) to recover the i2c client */
  736. dev_set_drvdata(&icd->dev, &client->dev);
  737. return 0;
  738. ei2cnd:
  739. i2c_put_adapter(adap);
  740. ei2cga:
  741. return -ENODEV;
  742. }
  743. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  744. {
  745. struct i2c_client *client =
  746. to_i2c_client(to_soc_camera_control(icd));
  747. dev_set_drvdata(&icd->dev, NULL);
  748. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  749. i2c_unregister_device(client);
  750. i2c_put_adapter(client->adapter);
  751. }
  752. #else
  753. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  754. #define soc_camera_free_i2c(icd) do {} while (0)
  755. #endif
  756. static int soc_camera_video_start(struct soc_camera_device *icd);
  757. static int video_dev_create(struct soc_camera_device *icd);
  758. /* Called during host-driver probe */
  759. static int soc_camera_probe(struct device *dev)
  760. {
  761. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  762. struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
  763. struct soc_camera_link *icl = to_soc_camera_link(icd);
  764. struct device *control = NULL;
  765. struct v4l2_subdev *sd;
  766. struct v4l2_mbus_framefmt mf;
  767. int ret;
  768. dev_info(dev, "Probing %s\n", dev_name(dev));
  769. ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
  770. icl->regulators);
  771. if (ret < 0)
  772. goto ereg;
  773. ret = soc_camera_power_set(icd, icl, 1);
  774. if (ret < 0)
  775. goto epower;
  776. /* The camera could have been already on, try to reset */
  777. if (icl->reset)
  778. icl->reset(icd->pdev);
  779. ret = ici->ops->add(icd);
  780. if (ret < 0)
  781. goto eadd;
  782. /* Must have icd->vdev before registering the device */
  783. ret = video_dev_create(icd);
  784. if (ret < 0)
  785. goto evdc;
  786. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  787. if (icl->board_info) {
  788. ret = soc_camera_init_i2c(icd, icl);
  789. if (ret < 0)
  790. goto eadddev;
  791. } else if (!icl->add_device || !icl->del_device) {
  792. ret = -EINVAL;
  793. goto eadddev;
  794. } else {
  795. if (icl->module_name)
  796. ret = request_module(icl->module_name);
  797. ret = icl->add_device(icl, &icd->dev);
  798. if (ret < 0)
  799. goto eadddev;
  800. /*
  801. * FIXME: this is racy, have to use driver-binding notification,
  802. * when it is available
  803. */
  804. control = to_soc_camera_control(icd);
  805. if (!control || !control->driver || !dev_get_drvdata(control) ||
  806. !try_module_get(control->driver->owner)) {
  807. icl->del_device(icl);
  808. goto enodrv;
  809. }
  810. }
  811. /* At this point client .probe() should have run already */
  812. ret = soc_camera_init_user_formats(icd);
  813. if (ret < 0)
  814. goto eiufmt;
  815. icd->field = V4L2_FIELD_ANY;
  816. icd->vdev->lock = &icd->video_lock;
  817. /*
  818. * ..._video_start() will create a device node, video_register_device()
  819. * itself is protected against concurrent open() calls, but we also have
  820. * to protect our data.
  821. */
  822. mutex_lock(&icd->video_lock);
  823. ret = soc_camera_video_start(icd);
  824. if (ret < 0)
  825. goto evidstart;
  826. /* Try to improve our guess of a reasonable window format */
  827. sd = soc_camera_to_subdev(icd);
  828. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  829. icd->user_width = mf.width;
  830. icd->user_height = mf.height;
  831. icd->colorspace = mf.colorspace;
  832. icd->field = mf.field;
  833. }
  834. /* Do we have to sysfs_remove_link() before device_unregister()? */
  835. if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  836. "control"))
  837. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  838. ici->ops->remove(icd);
  839. soc_camera_power_set(icd, icl, 0);
  840. mutex_unlock(&icd->video_lock);
  841. return 0;
  842. evidstart:
  843. mutex_unlock(&icd->video_lock);
  844. soc_camera_free_user_formats(icd);
  845. eiufmt:
  846. if (icl->board_info) {
  847. soc_camera_free_i2c(icd);
  848. } else {
  849. icl->del_device(icl);
  850. module_put(control->driver->owner);
  851. }
  852. enodrv:
  853. eadddev:
  854. video_device_release(icd->vdev);
  855. evdc:
  856. ici->ops->remove(icd);
  857. eadd:
  858. soc_camera_power_set(icd, icl, 0);
  859. epower:
  860. regulator_bulk_free(icl->num_regulators, icl->regulators);
  861. ereg:
  862. return ret;
  863. }
  864. /*
  865. * This is called on device_unregister, which only means we have to disconnect
  866. * from the host, but not remove ourselves from the device list
  867. */
  868. static int soc_camera_remove(struct device *dev)
  869. {
  870. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  871. struct soc_camera_link *icl = to_soc_camera_link(icd);
  872. struct video_device *vdev = icd->vdev;
  873. BUG_ON(!dev->parent);
  874. if (vdev) {
  875. video_unregister_device(vdev);
  876. icd->vdev = NULL;
  877. }
  878. if (icl->board_info) {
  879. soc_camera_free_i2c(icd);
  880. } else {
  881. struct device_driver *drv = to_soc_camera_control(icd) ?
  882. to_soc_camera_control(icd)->driver : NULL;
  883. if (drv) {
  884. icl->del_device(icl);
  885. module_put(drv->owner);
  886. }
  887. }
  888. soc_camera_free_user_formats(icd);
  889. regulator_bulk_free(icl->num_regulators, icl->regulators);
  890. return 0;
  891. }
  892. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  893. {
  894. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  895. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  896. int ret = 0;
  897. if (ici->ops->suspend)
  898. ret = ici->ops->suspend(icd, state);
  899. return ret;
  900. }
  901. static int soc_camera_resume(struct device *dev)
  902. {
  903. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  904. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  905. int ret = 0;
  906. if (ici->ops->resume)
  907. ret = ici->ops->resume(icd);
  908. return ret;
  909. }
  910. struct bus_type soc_camera_bus_type = {
  911. .name = "soc-camera",
  912. .probe = soc_camera_probe,
  913. .remove = soc_camera_remove,
  914. .suspend = soc_camera_suspend,
  915. .resume = soc_camera_resume,
  916. };
  917. EXPORT_SYMBOL_GPL(soc_camera_bus_type);
  918. static struct device_driver ic_drv = {
  919. .name = "camera",
  920. .bus = &soc_camera_bus_type,
  921. .owner = THIS_MODULE,
  922. };
  923. static void dummy_release(struct device *dev)
  924. {
  925. }
  926. static int default_cropcap(struct soc_camera_device *icd,
  927. struct v4l2_cropcap *a)
  928. {
  929. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  930. return v4l2_subdev_call(sd, video, cropcap, a);
  931. }
  932. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  933. {
  934. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  935. return v4l2_subdev_call(sd, video, g_crop, a);
  936. }
  937. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  938. {
  939. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  940. return v4l2_subdev_call(sd, video, s_crop, a);
  941. }
  942. static int default_g_parm(struct soc_camera_device *icd,
  943. struct v4l2_streamparm *parm)
  944. {
  945. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  946. return v4l2_subdev_call(sd, video, g_parm, parm);
  947. }
  948. static int default_s_parm(struct soc_camera_device *icd,
  949. struct v4l2_streamparm *parm)
  950. {
  951. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  952. return v4l2_subdev_call(sd, video, s_parm, parm);
  953. }
  954. static int default_enum_fsizes(struct soc_camera_device *icd,
  955. struct v4l2_frmsizeenum *fsize)
  956. {
  957. int ret;
  958. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  959. const struct soc_camera_format_xlate *xlate;
  960. __u32 pixfmt = fsize->pixel_format;
  961. struct v4l2_frmsizeenum fsize_mbus = *fsize;
  962. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  963. if (!xlate)
  964. return -EINVAL;
  965. /* map xlate-code to pixel_format, sensor only handle xlate-code*/
  966. fsize_mbus.pixel_format = xlate->code;
  967. ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
  968. if (ret < 0)
  969. return ret;
  970. *fsize = fsize_mbus;
  971. fsize->pixel_format = pixfmt;
  972. return 0;
  973. }
  974. static void soc_camera_device_init(struct device *dev, void *pdata)
  975. {
  976. dev->platform_data = pdata;
  977. dev->bus = &soc_camera_bus_type;
  978. dev->release = dummy_release;
  979. }
  980. int soc_camera_host_register(struct soc_camera_host *ici)
  981. {
  982. struct soc_camera_host *ix;
  983. int ret;
  984. if (!ici || !ici->ops ||
  985. !ici->ops->try_fmt ||
  986. !ici->ops->set_fmt ||
  987. !ici->ops->set_bus_param ||
  988. !ici->ops->querycap ||
  989. !ici->ops->init_videobuf ||
  990. !ici->ops->reqbufs ||
  991. !ici->ops->add ||
  992. !ici->ops->remove ||
  993. !ici->ops->poll ||
  994. !ici->v4l2_dev.dev)
  995. return -EINVAL;
  996. if (!ici->ops->set_crop)
  997. ici->ops->set_crop = default_s_crop;
  998. if (!ici->ops->get_crop)
  999. ici->ops->get_crop = default_g_crop;
  1000. if (!ici->ops->cropcap)
  1001. ici->ops->cropcap = default_cropcap;
  1002. if (!ici->ops->set_parm)
  1003. ici->ops->set_parm = default_s_parm;
  1004. if (!ici->ops->get_parm)
  1005. ici->ops->get_parm = default_g_parm;
  1006. if (!ici->ops->enum_fsizes)
  1007. ici->ops->enum_fsizes = default_enum_fsizes;
  1008. mutex_lock(&list_lock);
  1009. list_for_each_entry(ix, &hosts, list) {
  1010. if (ix->nr == ici->nr) {
  1011. ret = -EBUSY;
  1012. goto edevreg;
  1013. }
  1014. }
  1015. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1016. if (ret < 0)
  1017. goto edevreg;
  1018. list_add_tail(&ici->list, &hosts);
  1019. mutex_unlock(&list_lock);
  1020. scan_add_host(ici);
  1021. return 0;
  1022. edevreg:
  1023. mutex_unlock(&list_lock);
  1024. return ret;
  1025. }
  1026. EXPORT_SYMBOL(soc_camera_host_register);
  1027. /* Unregister all clients! */
  1028. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1029. {
  1030. struct soc_camera_device *icd;
  1031. mutex_lock(&list_lock);
  1032. list_del(&ici->list);
  1033. list_for_each_entry(icd, &devices, list) {
  1034. if (icd->iface == ici->nr) {
  1035. void *pdata = icd->dev.platform_data;
  1036. /* The bus->remove will be called */
  1037. device_unregister(&icd->dev);
  1038. /*
  1039. * Not before device_unregister(), .remove
  1040. * needs parent to call ici->ops->remove().
  1041. * If the host module is loaded again, device_register()
  1042. * would complain "already initialised," since 2.6.32
  1043. * this is also needed to prevent use-after-free of the
  1044. * device private data.
  1045. */
  1046. memset(&icd->dev, 0, sizeof(icd->dev));
  1047. soc_camera_device_init(&icd->dev, pdata);
  1048. }
  1049. }
  1050. mutex_unlock(&list_lock);
  1051. v4l2_device_unregister(&ici->v4l2_dev);
  1052. }
  1053. EXPORT_SYMBOL(soc_camera_host_unregister);
  1054. /* Image capture device */
  1055. static int soc_camera_device_register(struct soc_camera_device *icd)
  1056. {
  1057. struct soc_camera_device *ix;
  1058. int num = -1, i;
  1059. for (i = 0; i < 256 && num < 0; i++) {
  1060. num = i;
  1061. /* Check if this index is available on this interface */
  1062. list_for_each_entry(ix, &devices, list) {
  1063. if (ix->iface == icd->iface && ix->devnum == i) {
  1064. num = -1;
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. if (num < 0)
  1070. /*
  1071. * ok, we have 256 cameras on this host...
  1072. * man, stay reasonable...
  1073. */
  1074. return -ENOMEM;
  1075. icd->devnum = num;
  1076. icd->use_count = 0;
  1077. icd->host_priv = NULL;
  1078. mutex_init(&icd->video_lock);
  1079. list_add_tail(&icd->list, &devices);
  1080. return 0;
  1081. }
  1082. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  1083. {
  1084. list_del(&icd->list);
  1085. }
  1086. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1087. .vidioc_querycap = soc_camera_querycap,
  1088. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1089. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1090. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1091. .vidioc_enum_input = soc_camera_enum_input,
  1092. .vidioc_g_input = soc_camera_g_input,
  1093. .vidioc_s_input = soc_camera_s_input,
  1094. .vidioc_s_std = soc_camera_s_std,
  1095. .vidioc_enum_framesizes = soc_camera_enum_fsizes,
  1096. .vidioc_reqbufs = soc_camera_reqbufs,
  1097. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1098. .vidioc_querybuf = soc_camera_querybuf,
  1099. .vidioc_qbuf = soc_camera_qbuf,
  1100. .vidioc_dqbuf = soc_camera_dqbuf,
  1101. .vidioc_streamon = soc_camera_streamon,
  1102. .vidioc_streamoff = soc_camera_streamoff,
  1103. .vidioc_queryctrl = soc_camera_queryctrl,
  1104. .vidioc_g_ctrl = soc_camera_g_ctrl,
  1105. .vidioc_s_ctrl = soc_camera_s_ctrl,
  1106. .vidioc_cropcap = soc_camera_cropcap,
  1107. .vidioc_g_crop = soc_camera_g_crop,
  1108. .vidioc_s_crop = soc_camera_s_crop,
  1109. .vidioc_g_parm = soc_camera_g_parm,
  1110. .vidioc_s_parm = soc_camera_s_parm,
  1111. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1112. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1113. .vidioc_g_register = soc_camera_g_register,
  1114. .vidioc_s_register = soc_camera_s_register,
  1115. #endif
  1116. };
  1117. static int video_dev_create(struct soc_camera_device *icd)
  1118. {
  1119. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1120. struct video_device *vdev = video_device_alloc();
  1121. if (!vdev)
  1122. return -ENOMEM;
  1123. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1124. vdev->parent = &icd->dev;
  1125. vdev->current_norm = V4L2_STD_UNKNOWN;
  1126. vdev->fops = &soc_camera_fops;
  1127. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1128. vdev->release = video_device_release;
  1129. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1130. icd->vdev = vdev;
  1131. return 0;
  1132. }
  1133. /*
  1134. * Called from soc_camera_probe() above (with .video_lock held???)
  1135. */
  1136. static int soc_camera_video_start(struct soc_camera_device *icd)
  1137. {
  1138. struct device_type *type = icd->vdev->dev.type;
  1139. int ret;
  1140. if (!icd->dev.parent)
  1141. return -ENODEV;
  1142. if (!icd->ops ||
  1143. !icd->ops->query_bus_param ||
  1144. !icd->ops->set_bus_param)
  1145. return -EINVAL;
  1146. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1147. if (ret < 0) {
  1148. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1149. return ret;
  1150. }
  1151. /* Restore device type, possibly set by the subdevice driver */
  1152. icd->vdev->dev.type = type;
  1153. return 0;
  1154. }
  1155. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1156. {
  1157. struct soc_camera_link *icl = pdev->dev.platform_data;
  1158. struct soc_camera_device *icd;
  1159. int ret;
  1160. if (!icl)
  1161. return -EINVAL;
  1162. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1163. if (!icd)
  1164. return -ENOMEM;
  1165. icd->iface = icl->bus_id;
  1166. icd->pdev = &pdev->dev;
  1167. platform_set_drvdata(pdev, icd);
  1168. ret = soc_camera_device_register(icd);
  1169. if (ret < 0)
  1170. goto escdevreg;
  1171. soc_camera_device_init(&icd->dev, icl);
  1172. icd->user_width = DEFAULT_WIDTH;
  1173. icd->user_height = DEFAULT_HEIGHT;
  1174. return 0;
  1175. escdevreg:
  1176. kfree(icd);
  1177. return ret;
  1178. }
  1179. /*
  1180. * Only called on rmmod for each platform device, since they are not
  1181. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1182. * been unloaded already
  1183. */
  1184. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1185. {
  1186. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1187. if (!icd)
  1188. return -EINVAL;
  1189. soc_camera_device_unregister(icd);
  1190. kfree(icd);
  1191. return 0;
  1192. }
  1193. static struct platform_driver __refdata soc_camera_pdrv = {
  1194. .remove = __devexit_p(soc_camera_pdrv_remove),
  1195. .driver = {
  1196. .name = "soc-camera-pdrv",
  1197. .owner = THIS_MODULE,
  1198. },
  1199. };
  1200. static int __init soc_camera_init(void)
  1201. {
  1202. int ret = bus_register(&soc_camera_bus_type);
  1203. if (ret)
  1204. return ret;
  1205. ret = driver_register(&ic_drv);
  1206. if (ret)
  1207. goto edrvr;
  1208. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1209. if (ret)
  1210. goto epdr;
  1211. return 0;
  1212. epdr:
  1213. driver_unregister(&ic_drv);
  1214. edrvr:
  1215. bus_unregister(&soc_camera_bus_type);
  1216. return ret;
  1217. }
  1218. static void __exit soc_camera_exit(void)
  1219. {
  1220. platform_driver_unregister(&soc_camera_pdrv);
  1221. driver_unregister(&ic_drv);
  1222. bus_unregister(&soc_camera_bus_type);
  1223. }
  1224. module_init(soc_camera_init);
  1225. module_exit(soc_camera_exit);
  1226. MODULE_DESCRIPTION("Image capture bus driver");
  1227. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1228. MODULE_LICENSE("GPL");
  1229. MODULE_ALIAS("platform:soc-camera-pdrv");