soc_camera.c 34 KB

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