soc_camera.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. client = subdev->priv;
  687. /* Use to_i2c_client(dev) to recover the i2c client */
  688. dev_set_drvdata(&icd->dev, &client->dev);
  689. return 0;
  690. ei2cnd:
  691. i2c_put_adapter(adap);
  692. ei2cga:
  693. return ret;
  694. }
  695. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  696. {
  697. struct i2c_client *client =
  698. to_i2c_client(to_soc_camera_control(icd));
  699. dev_set_drvdata(&icd->dev, NULL);
  700. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  701. i2c_unregister_device(client);
  702. i2c_put_adapter(client->adapter);
  703. }
  704. #else
  705. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  706. #define soc_camera_free_i2c(icd) do {} while (0)
  707. #endif
  708. static int soc_camera_video_start(struct soc_camera_device *icd);
  709. static int video_dev_create(struct soc_camera_device *icd);
  710. /* Called during host-driver probe */
  711. static int soc_camera_probe(struct device *dev)
  712. {
  713. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  714. struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
  715. struct soc_camera_link *icl = to_soc_camera_link(icd);
  716. struct device *control = NULL;
  717. struct v4l2_subdev *sd;
  718. struct v4l2_format f = {.type = V4L2_BUF_TYPE_VIDEO_CAPTURE};
  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. /*
  754. * FIXME: this is racy, have to use driver-binding notification,
  755. * when it is available
  756. */
  757. control = to_soc_camera_control(icd);
  758. if (!control || !control->driver || !dev_get_drvdata(control) ||
  759. !try_module_get(control->driver->owner)) {
  760. icl->del_device(icl);
  761. goto enodrv;
  762. }
  763. }
  764. /* At this point client .probe() should have run already */
  765. ret = soc_camera_init_user_formats(icd);
  766. if (ret < 0)
  767. goto eiufmt;
  768. icd->field = V4L2_FIELD_ANY;
  769. /* ..._video_start() will create a device node, so we have to protect */
  770. mutex_lock(&icd->video_lock);
  771. ret = soc_camera_video_start(icd);
  772. if (ret < 0)
  773. goto evidstart;
  774. /* Try to improve our guess of a reasonable window format */
  775. sd = soc_camera_to_subdev(icd);
  776. if (!v4l2_subdev_call(sd, video, g_fmt, &f)) {
  777. icd->user_width = f.fmt.pix.width;
  778. icd->user_height = f.fmt.pix.height;
  779. }
  780. /* Do we have to sysfs_remove_link() before device_unregister()? */
  781. if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  782. "control"))
  783. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  784. ici->ops->remove(icd);
  785. if (icl->power)
  786. icl->power(icd->pdev, 0);
  787. mutex_unlock(&icd->video_lock);
  788. return 0;
  789. evidstart:
  790. mutex_unlock(&icd->video_lock);
  791. soc_camera_free_user_formats(icd);
  792. eiufmt:
  793. if (icl->board_info) {
  794. soc_camera_free_i2c(icd);
  795. } else {
  796. icl->del_device(icl);
  797. module_put(control->driver->owner);
  798. }
  799. enodrv:
  800. eadddev:
  801. video_device_release(icd->vdev);
  802. evdc:
  803. ici->ops->remove(icd);
  804. eadd:
  805. if (icl->power)
  806. icl->power(icd->pdev, 0);
  807. epower:
  808. return ret;
  809. }
  810. /* This is called on device_unregister, which only means we have to disconnect
  811. * from the host, but not remove ourselves from the device list */
  812. static int soc_camera_remove(struct device *dev)
  813. {
  814. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  815. struct soc_camera_link *icl = to_soc_camera_link(icd);
  816. struct video_device *vdev = icd->vdev;
  817. BUG_ON(!dev->parent);
  818. if (vdev) {
  819. mutex_lock(&icd->video_lock);
  820. video_unregister_device(vdev);
  821. icd->vdev = NULL;
  822. mutex_unlock(&icd->video_lock);
  823. }
  824. if (icl->board_info) {
  825. soc_camera_free_i2c(icd);
  826. } else {
  827. struct device_driver *drv = to_soc_camera_control(icd) ?
  828. to_soc_camera_control(icd)->driver : NULL;
  829. if (drv) {
  830. icl->del_device(icl);
  831. module_put(drv->owner);
  832. }
  833. }
  834. soc_camera_free_user_formats(icd);
  835. return 0;
  836. }
  837. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  838. {
  839. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  840. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  841. int ret = 0;
  842. if (ici->ops->suspend)
  843. ret = ici->ops->suspend(icd, state);
  844. return ret;
  845. }
  846. static int soc_camera_resume(struct device *dev)
  847. {
  848. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  849. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  850. int ret = 0;
  851. if (ici->ops->resume)
  852. ret = ici->ops->resume(icd);
  853. return ret;
  854. }
  855. static struct bus_type soc_camera_bus_type = {
  856. .name = "soc-camera",
  857. .probe = soc_camera_probe,
  858. .remove = soc_camera_remove,
  859. .suspend = soc_camera_suspend,
  860. .resume = soc_camera_resume,
  861. };
  862. static struct device_driver ic_drv = {
  863. .name = "camera",
  864. .bus = &soc_camera_bus_type,
  865. .owner = THIS_MODULE,
  866. };
  867. static void dummy_release(struct device *dev)
  868. {
  869. }
  870. static int default_cropcap(struct soc_camera_device *icd,
  871. struct v4l2_cropcap *a)
  872. {
  873. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  874. return v4l2_subdev_call(sd, video, cropcap, a);
  875. }
  876. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  877. {
  878. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  879. return v4l2_subdev_call(sd, video, g_crop, a);
  880. }
  881. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  882. {
  883. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  884. return v4l2_subdev_call(sd, video, s_crop, a);
  885. }
  886. static void soc_camera_device_init(struct device *dev, void *pdata)
  887. {
  888. dev->platform_data = pdata;
  889. dev->bus = &soc_camera_bus_type;
  890. dev->release = dummy_release;
  891. }
  892. int soc_camera_host_register(struct soc_camera_host *ici)
  893. {
  894. struct soc_camera_host *ix;
  895. int ret;
  896. if (!ici || !ici->ops ||
  897. !ici->ops->try_fmt ||
  898. !ici->ops->set_fmt ||
  899. !ici->ops->set_bus_param ||
  900. !ici->ops->querycap ||
  901. !ici->ops->init_videobuf ||
  902. !ici->ops->reqbufs ||
  903. !ici->ops->add ||
  904. !ici->ops->remove ||
  905. !ici->ops->poll ||
  906. !ici->v4l2_dev.dev)
  907. return -EINVAL;
  908. if (!ici->ops->set_crop)
  909. ici->ops->set_crop = default_s_crop;
  910. if (!ici->ops->get_crop)
  911. ici->ops->get_crop = default_g_crop;
  912. if (!ici->ops->cropcap)
  913. ici->ops->cropcap = default_cropcap;
  914. mutex_lock(&list_lock);
  915. list_for_each_entry(ix, &hosts, list) {
  916. if (ix->nr == ici->nr) {
  917. ret = -EBUSY;
  918. goto edevreg;
  919. }
  920. }
  921. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  922. if (ret < 0)
  923. goto edevreg;
  924. list_add_tail(&ici->list, &hosts);
  925. mutex_unlock(&list_lock);
  926. scan_add_host(ici);
  927. return 0;
  928. edevreg:
  929. mutex_unlock(&list_lock);
  930. return ret;
  931. }
  932. EXPORT_SYMBOL(soc_camera_host_register);
  933. /* Unregister all clients! */
  934. void soc_camera_host_unregister(struct soc_camera_host *ici)
  935. {
  936. struct soc_camera_device *icd;
  937. mutex_lock(&list_lock);
  938. list_del(&ici->list);
  939. list_for_each_entry(icd, &devices, list) {
  940. if (icd->iface == ici->nr) {
  941. void *pdata = icd->dev.platform_data;
  942. /* The bus->remove will be called */
  943. device_unregister(&icd->dev);
  944. /*
  945. * Not before device_unregister(), .remove
  946. * needs parent to call ici->ops->remove().
  947. * If the host module is loaded again, device_register()
  948. * would complain "already initialised," since 2.6.32
  949. * this is also needed to prevent use-after-free of the
  950. * device private data.
  951. */
  952. memset(&icd->dev, 0, sizeof(icd->dev));
  953. soc_camera_device_init(&icd->dev, pdata);
  954. }
  955. }
  956. mutex_unlock(&list_lock);
  957. v4l2_device_unregister(&ici->v4l2_dev);
  958. }
  959. EXPORT_SYMBOL(soc_camera_host_unregister);
  960. /* Image capture device */
  961. static int soc_camera_device_register(struct soc_camera_device *icd)
  962. {
  963. struct soc_camera_device *ix;
  964. int num = -1, i;
  965. for (i = 0; i < 256 && num < 0; i++) {
  966. num = i;
  967. /* Check if this index is available on this interface */
  968. list_for_each_entry(ix, &devices, list) {
  969. if (ix->iface == icd->iface && ix->devnum == i) {
  970. num = -1;
  971. break;
  972. }
  973. }
  974. }
  975. if (num < 0)
  976. /* ok, we have 256 cameras on this host...
  977. * man, stay reasonable... */
  978. return -ENOMEM;
  979. icd->devnum = num;
  980. icd->use_count = 0;
  981. icd->host_priv = NULL;
  982. mutex_init(&icd->video_lock);
  983. list_add_tail(&icd->list, &devices);
  984. return 0;
  985. }
  986. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  987. {
  988. list_del(&icd->list);
  989. }
  990. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  991. .vidioc_querycap = soc_camera_querycap,
  992. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  993. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  994. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  995. .vidioc_enum_input = soc_camera_enum_input,
  996. .vidioc_g_input = soc_camera_g_input,
  997. .vidioc_s_input = soc_camera_s_input,
  998. .vidioc_s_std = soc_camera_s_std,
  999. .vidioc_reqbufs = soc_camera_reqbufs,
  1000. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1001. .vidioc_querybuf = soc_camera_querybuf,
  1002. .vidioc_qbuf = soc_camera_qbuf,
  1003. .vidioc_dqbuf = soc_camera_dqbuf,
  1004. .vidioc_streamon = soc_camera_streamon,
  1005. .vidioc_streamoff = soc_camera_streamoff,
  1006. .vidioc_queryctrl = soc_camera_queryctrl,
  1007. .vidioc_g_ctrl = soc_camera_g_ctrl,
  1008. .vidioc_s_ctrl = soc_camera_s_ctrl,
  1009. .vidioc_cropcap = soc_camera_cropcap,
  1010. .vidioc_g_crop = soc_camera_g_crop,
  1011. .vidioc_s_crop = soc_camera_s_crop,
  1012. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1013. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1014. .vidioc_g_register = soc_camera_g_register,
  1015. .vidioc_s_register = soc_camera_s_register,
  1016. #endif
  1017. };
  1018. static int video_dev_create(struct soc_camera_device *icd)
  1019. {
  1020. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1021. struct video_device *vdev = video_device_alloc();
  1022. if (!vdev)
  1023. return -ENOMEM;
  1024. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1025. vdev->parent = &icd->dev;
  1026. vdev->current_norm = V4L2_STD_UNKNOWN;
  1027. vdev->fops = &soc_camera_fops;
  1028. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1029. vdev->release = video_device_release;
  1030. vdev->minor = -1;
  1031. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1032. icd->vdev = vdev;
  1033. return 0;
  1034. }
  1035. /*
  1036. * Called from soc_camera_probe() above (with .video_lock held???)
  1037. */
  1038. static int soc_camera_video_start(struct soc_camera_device *icd)
  1039. {
  1040. int ret;
  1041. if (!icd->dev.parent)
  1042. return -ENODEV;
  1043. if (!icd->ops ||
  1044. !icd->ops->query_bus_param ||
  1045. !icd->ops->set_bus_param)
  1046. return -EINVAL;
  1047. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER,
  1048. icd->vdev->minor);
  1049. if (ret < 0) {
  1050. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1051. return ret;
  1052. }
  1053. return 0;
  1054. }
  1055. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1056. {
  1057. struct soc_camera_link *icl = pdev->dev.platform_data;
  1058. struct soc_camera_device *icd;
  1059. int ret;
  1060. if (!icl)
  1061. return -EINVAL;
  1062. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1063. if (!icd)
  1064. return -ENOMEM;
  1065. icd->iface = icl->bus_id;
  1066. icd->pdev = &pdev->dev;
  1067. platform_set_drvdata(pdev, icd);
  1068. ret = soc_camera_device_register(icd);
  1069. if (ret < 0)
  1070. goto escdevreg;
  1071. soc_camera_device_init(&icd->dev, icl);
  1072. icd->user_width = DEFAULT_WIDTH;
  1073. icd->user_height = DEFAULT_HEIGHT;
  1074. return 0;
  1075. escdevreg:
  1076. kfree(icd);
  1077. return ret;
  1078. }
  1079. /* Only called on rmmod for each platform device, since they are not
  1080. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1081. * been unloaded already */
  1082. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1083. {
  1084. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1085. if (!icd)
  1086. return -EINVAL;
  1087. soc_camera_device_unregister(icd);
  1088. kfree(icd);
  1089. return 0;
  1090. }
  1091. static struct platform_driver __refdata soc_camera_pdrv = {
  1092. .remove = __devexit_p(soc_camera_pdrv_remove),
  1093. .driver = {
  1094. .name = "soc-camera-pdrv",
  1095. .owner = THIS_MODULE,
  1096. },
  1097. };
  1098. static int __init soc_camera_init(void)
  1099. {
  1100. int ret = bus_register(&soc_camera_bus_type);
  1101. if (ret)
  1102. return ret;
  1103. ret = driver_register(&ic_drv);
  1104. if (ret)
  1105. goto edrvr;
  1106. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1107. if (ret)
  1108. goto epdr;
  1109. return 0;
  1110. epdr:
  1111. driver_unregister(&ic_drv);
  1112. edrvr:
  1113. bus_unregister(&soc_camera_bus_type);
  1114. return ret;
  1115. }
  1116. static void __exit soc_camera_exit(void)
  1117. {
  1118. platform_driver_unregister(&soc_camera_pdrv);
  1119. driver_unregister(&ic_drv);
  1120. bus_unregister(&soc_camera_bus_type);
  1121. }
  1122. module_init(soc_camera_init);
  1123. module_exit(soc_camera_exit);
  1124. MODULE_DESCRIPTION("Image capture bus driver");
  1125. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1126. MODULE_LICENSE("GPL");
  1127. MODULE_ALIAS("platform:soc-camera-pdrv");