soc_camera.c 36 KB

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