soc_camera.c 36 KB

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