soc_camera.c 36 KB

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