soc_camera.c 34 KB

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