soc_camera.c 35 KB

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