soc_camera.c 33 KB

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