soc_camera.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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/vmalloc.h>
  28. #include <media/soc_camera.h>
  29. #include <media/v4l2-common.h>
  30. #include <media/v4l2-ioctl.h>
  31. #include <media/v4l2-dev.h>
  32. #include <media/videobuf-core.h>
  33. #include <media/soc_mediabus.h>
  34. /* Default to VGA resolution */
  35. #define DEFAULT_WIDTH 640
  36. #define DEFAULT_HEIGHT 480
  37. static LIST_HEAD(hosts);
  38. static LIST_HEAD(devices);
  39. static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
  40. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  41. struct soc_camera_device *icd, unsigned int fourcc)
  42. {
  43. unsigned int i;
  44. for (i = 0; i < icd->num_user_formats; i++)
  45. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  46. return icd->user_formats + i;
  47. return NULL;
  48. }
  49. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  50. /**
  51. * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  52. * @icl: camera platform parameters
  53. * @flags: flags to be inverted according to platform configuration
  54. * @return: resulting flags
  55. */
  56. unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
  57. unsigned long flags)
  58. {
  59. unsigned long f;
  60. /* If only one of the two polarities is supported, switch to the opposite */
  61. if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  62. f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
  63. if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
  64. flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
  65. }
  66. if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  67. f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
  68. if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
  69. flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
  70. }
  71. if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
  72. f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
  73. if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
  74. flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
  75. }
  76. return flags;
  77. }
  78. EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
  79. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  80. struct v4l2_format *f)
  81. {
  82. struct soc_camera_file *icf = file->private_data;
  83. struct soc_camera_device *icd = icf->icd;
  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_file *icf = file->private_data;
  93. struct soc_camera_device *icd = icf->icd;
  94. int ret = 0;
  95. if (inp->index != 0)
  96. return -EINVAL;
  97. if (icd->ops->enum_input)
  98. ret = icd->ops->enum_input(icd, inp);
  99. else {
  100. /* default is camera */
  101. inp->type = V4L2_INPUT_TYPE_CAMERA;
  102. inp->std = V4L2_STD_UNKNOWN;
  103. strcpy(inp->name, "Camera");
  104. }
  105. return ret;
  106. }
  107. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  108. {
  109. *i = 0;
  110. return 0;
  111. }
  112. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  113. {
  114. if (i > 0)
  115. return -EINVAL;
  116. return 0;
  117. }
  118. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
  119. {
  120. struct soc_camera_file *icf = file->private_data;
  121. struct soc_camera_device *icd = icf->icd;
  122. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  123. return v4l2_subdev_call(sd, core, s_std, *a);
  124. }
  125. static int soc_camera_reqbufs(struct file *file, void *priv,
  126. struct v4l2_requestbuffers *p)
  127. {
  128. int ret;
  129. struct soc_camera_file *icf = file->private_data;
  130. struct soc_camera_device *icd = icf->icd;
  131. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  132. WARN_ON(priv != file->private_data);
  133. ret = videobuf_reqbufs(&icf->vb_vidq, p);
  134. if (ret < 0)
  135. return ret;
  136. return ici->ops->reqbufs(icf, p);
  137. }
  138. static int soc_camera_querybuf(struct file *file, void *priv,
  139. struct v4l2_buffer *p)
  140. {
  141. struct soc_camera_file *icf = file->private_data;
  142. WARN_ON(priv != file->private_data);
  143. return videobuf_querybuf(&icf->vb_vidq, p);
  144. }
  145. static int soc_camera_qbuf(struct file *file, void *priv,
  146. struct v4l2_buffer *p)
  147. {
  148. struct soc_camera_file *icf = file->private_data;
  149. WARN_ON(priv != file->private_data);
  150. return videobuf_qbuf(&icf->vb_vidq, p);
  151. }
  152. static int soc_camera_dqbuf(struct file *file, void *priv,
  153. struct v4l2_buffer *p)
  154. {
  155. struct soc_camera_file *icf = file->private_data;
  156. WARN_ON(priv != file->private_data);
  157. return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
  158. }
  159. /* Always entered with .video_lock held */
  160. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  161. {
  162. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  163. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  164. int i, fmts = 0, raw_fmts = 0, ret;
  165. enum v4l2_mbus_pixelcode code;
  166. while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
  167. raw_fmts++;
  168. if (!ici->ops->get_formats)
  169. /*
  170. * Fallback mode - the host will have to serve all
  171. * sensor-provided formats one-to-one to the user
  172. */
  173. fmts = raw_fmts;
  174. else
  175. /*
  176. * First pass - only count formats this host-sensor
  177. * configuration can provide
  178. */
  179. for (i = 0; i < raw_fmts; i++) {
  180. ret = ici->ops->get_formats(icd, i, NULL);
  181. if (ret < 0)
  182. return ret;
  183. fmts += ret;
  184. }
  185. if (!fmts)
  186. return -ENXIO;
  187. icd->user_formats =
  188. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  189. if (!icd->user_formats)
  190. return -ENOMEM;
  191. icd->num_user_formats = fmts;
  192. dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
  193. /* Second pass - actually fill data formats */
  194. fmts = 0;
  195. for (i = 0; i < raw_fmts; i++)
  196. if (!ici->ops->get_formats) {
  197. v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
  198. icd->user_formats[i].host_fmt =
  199. soc_mbus_get_fmtdesc(code);
  200. icd->user_formats[i].code = code;
  201. } else {
  202. ret = ici->ops->get_formats(icd, i,
  203. &icd->user_formats[fmts]);
  204. if (ret < 0)
  205. goto egfmt;
  206. fmts += ret;
  207. }
  208. icd->current_fmt = &icd->user_formats[0];
  209. return 0;
  210. egfmt:
  211. icd->num_user_formats = 0;
  212. vfree(icd->user_formats);
  213. return ret;
  214. }
  215. /* Always entered with .video_lock held */
  216. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  217. {
  218. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  219. if (ici->ops->put_formats)
  220. ici->ops->put_formats(icd);
  221. icd->current_fmt = NULL;
  222. icd->num_user_formats = 0;
  223. vfree(icd->user_formats);
  224. icd->user_formats = NULL;
  225. }
  226. #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
  227. ((x) >> 24) & 0xff
  228. /* Called with .vb_lock held, or from the first open(2), see comment there */
  229. static int soc_camera_set_fmt(struct soc_camera_file *icf,
  230. struct v4l2_format *f)
  231. {
  232. struct soc_camera_device *icd = icf->icd;
  233. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  234. struct v4l2_pix_format *pix = &f->fmt.pix;
  235. int ret;
  236. dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
  237. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  238. /* We always call try_fmt() before set_fmt() or set_crop() */
  239. ret = ici->ops->try_fmt(icd, f);
  240. if (ret < 0)
  241. return ret;
  242. ret = ici->ops->set_fmt(icd, f);
  243. if (ret < 0) {
  244. return ret;
  245. } else if (!icd->current_fmt ||
  246. icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
  247. dev_err(&icd->dev,
  248. "Host driver hasn't set up current format correctly!\n");
  249. return -EINVAL;
  250. }
  251. icd->user_width = pix->width;
  252. icd->user_height = pix->height;
  253. icd->colorspace = pix->colorspace;
  254. icf->vb_vidq.field =
  255. icd->field = pix->field;
  256. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  257. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  258. f->type);
  259. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  260. icd->user_width, icd->user_height);
  261. /* set physical bus parameters */
  262. return ici->ops->set_bus_param(icd, pix->pixelformat);
  263. }
  264. static int soc_camera_open(struct file *file)
  265. {
  266. struct video_device *vdev = video_devdata(file);
  267. struct soc_camera_device *icd = container_of(vdev->parent,
  268. struct soc_camera_device,
  269. dev);
  270. struct soc_camera_link *icl = to_soc_camera_link(icd);
  271. struct soc_camera_host *ici;
  272. struct soc_camera_file *icf;
  273. int ret;
  274. if (!icd->ops)
  275. /* No device driver attached */
  276. return -ENODEV;
  277. ici = to_soc_camera_host(icd->dev.parent);
  278. icf = vmalloc(sizeof(*icf));
  279. if (!icf)
  280. return -ENOMEM;
  281. if (!try_module_get(ici->ops->owner)) {
  282. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  283. ret = -EINVAL;
  284. goto emgi;
  285. }
  286. /*
  287. * Protect against icd->ops->remove() until we module_get() both
  288. * drivers.
  289. */
  290. mutex_lock(&icd->video_lock);
  291. icf->icd = icd;
  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. /*
  321. * Try to configure with default parameters. Notice: this is the
  322. * very first open, so, we cannot race against other calls,
  323. * apart from someone else calling open() simultaneously, but
  324. * .video_lock is protecting us against it.
  325. */
  326. ret = soc_camera_set_fmt(icf, &f);
  327. if (ret < 0)
  328. goto esfmt;
  329. }
  330. file->private_data = icf;
  331. dev_dbg(&icd->dev, "camera device open\n");
  332. ici->ops->init_videobuf(&icf->vb_vidq, icd);
  333. mutex_unlock(&icd->video_lock);
  334. return 0;
  335. /*
  336. * First five errors are entered with the .video_lock held
  337. * and use_count == 1
  338. */
  339. esfmt:
  340. ici->ops->remove(icd);
  341. eiciadd:
  342. if (icl->power)
  343. icl->power(icd->pdev, 0);
  344. epower:
  345. icd->use_count--;
  346. mutex_unlock(&icd->video_lock);
  347. module_put(ici->ops->owner);
  348. emgi:
  349. vfree(icf);
  350. return ret;
  351. }
  352. static int soc_camera_close(struct file *file)
  353. {
  354. struct soc_camera_file *icf = file->private_data;
  355. struct soc_camera_device *icd = icf->icd;
  356. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  357. mutex_lock(&icd->video_lock);
  358. icd->use_count--;
  359. if (!icd->use_count) {
  360. struct soc_camera_link *icl = to_soc_camera_link(icd);
  361. ici->ops->remove(icd);
  362. if (icl->power)
  363. icl->power(icd->pdev, 0);
  364. }
  365. mutex_unlock(&icd->video_lock);
  366. module_put(ici->ops->owner);
  367. vfree(icf);
  368. dev_dbg(&icd->dev, "camera device close\n");
  369. return 0;
  370. }
  371. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  372. size_t count, loff_t *ppos)
  373. {
  374. struct soc_camera_file *icf = file->private_data;
  375. struct soc_camera_device *icd = icf->icd;
  376. int err = -EINVAL;
  377. dev_err(&icd->dev, "camera device read not implemented\n");
  378. return err;
  379. }
  380. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  381. {
  382. struct soc_camera_file *icf = file->private_data;
  383. struct soc_camera_device *icd = icf->icd;
  384. int err;
  385. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  386. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  387. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  388. (unsigned long)vma->vm_start,
  389. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  390. err);
  391. return err;
  392. }
  393. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  394. {
  395. struct soc_camera_file *icf = file->private_data;
  396. struct soc_camera_device *icd = icf->icd;
  397. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  398. if (list_empty(&icf->vb_vidq.stream)) {
  399. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  400. return POLLERR;
  401. }
  402. return ici->ops->poll(file, pt);
  403. }
  404. static struct v4l2_file_operations soc_camera_fops = {
  405. .owner = THIS_MODULE,
  406. .open = soc_camera_open,
  407. .release = soc_camera_close,
  408. .ioctl = video_ioctl2,
  409. .read = soc_camera_read,
  410. .mmap = soc_camera_mmap,
  411. .poll = soc_camera_poll,
  412. };
  413. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  414. struct v4l2_format *f)
  415. {
  416. struct soc_camera_file *icf = file->private_data;
  417. struct soc_camera_device *icd = icf->icd;
  418. int ret;
  419. WARN_ON(priv != file->private_data);
  420. mutex_lock(&icf->vb_vidq.vb_lock);
  421. if (icf->vb_vidq.bufs[0]) {
  422. dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
  423. ret = -EBUSY;
  424. goto unlock;
  425. }
  426. ret = soc_camera_set_fmt(icf, f);
  427. unlock:
  428. mutex_unlock(&icf->vb_vidq.vb_lock);
  429. return ret;
  430. }
  431. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  432. struct v4l2_fmtdesc *f)
  433. {
  434. struct soc_camera_file *icf = file->private_data;
  435. struct soc_camera_device *icd = icf->icd;
  436. const struct soc_mbus_pixelfmt *format;
  437. WARN_ON(priv != file->private_data);
  438. if (f->index >= icd->num_user_formats)
  439. return -EINVAL;
  440. format = icd->user_formats[f->index].host_fmt;
  441. if (format->name)
  442. strlcpy(f->description, format->name, sizeof(f->description));
  443. f->pixelformat = format->fourcc;
  444. return 0;
  445. }
  446. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  447. struct v4l2_format *f)
  448. {
  449. struct soc_camera_file *icf = file->private_data;
  450. struct soc_camera_device *icd = icf->icd;
  451. struct v4l2_pix_format *pix = &f->fmt.pix;
  452. WARN_ON(priv != file->private_data);
  453. pix->width = icd->user_width;
  454. pix->height = icd->user_height;
  455. pix->field = icf->vb_vidq.field;
  456. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  457. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  458. icd->current_fmt->host_fmt);
  459. pix->colorspace = icd->colorspace;
  460. if (pix->bytesperline < 0)
  461. return pix->bytesperline;
  462. pix->sizeimage = pix->height * pix->bytesperline;
  463. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  464. icd->current_fmt->host_fmt->fourcc);
  465. return 0;
  466. }
  467. static int soc_camera_querycap(struct file *file, void *priv,
  468. struct v4l2_capability *cap)
  469. {
  470. struct soc_camera_file *icf = file->private_data;
  471. struct soc_camera_device *icd = icf->icd;
  472. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  473. WARN_ON(priv != file->private_data);
  474. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  475. return ici->ops->querycap(ici, cap);
  476. }
  477. static int soc_camera_streamon(struct file *file, void *priv,
  478. enum v4l2_buf_type i)
  479. {
  480. struct soc_camera_file *icf = file->private_data;
  481. struct soc_camera_device *icd = icf->icd;
  482. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  483. int ret;
  484. WARN_ON(priv != file->private_data);
  485. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  486. return -EINVAL;
  487. mutex_lock(&icd->video_lock);
  488. v4l2_subdev_call(sd, video, s_stream, 1);
  489. /* This calls buf_queue from host driver's videobuf_queue_ops */
  490. ret = videobuf_streamon(&icf->vb_vidq);
  491. mutex_unlock(&icd->video_lock);
  492. return ret;
  493. }
  494. static int soc_camera_streamoff(struct file *file, void *priv,
  495. enum v4l2_buf_type i)
  496. {
  497. struct soc_camera_file *icf = file->private_data;
  498. struct soc_camera_device *icd = icf->icd;
  499. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  500. WARN_ON(priv != file->private_data);
  501. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  502. return -EINVAL;
  503. mutex_lock(&icd->video_lock);
  504. /*
  505. * This calls buf_release from host driver's videobuf_queue_ops for all
  506. * remaining buffers. When the last buffer is freed, stop capture
  507. */
  508. videobuf_streamoff(&icf->vb_vidq);
  509. v4l2_subdev_call(sd, video, s_stream, 0);
  510. mutex_unlock(&icd->video_lock);
  511. return 0;
  512. }
  513. static int soc_camera_queryctrl(struct file *file, void *priv,
  514. struct v4l2_queryctrl *qc)
  515. {
  516. struct soc_camera_file *icf = file->private_data;
  517. struct soc_camera_device *icd = icf->icd;
  518. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  519. int i;
  520. WARN_ON(priv != file->private_data);
  521. if (!qc->id)
  522. return -EINVAL;
  523. /* First check host controls */
  524. for (i = 0; i < ici->ops->num_controls; i++)
  525. if (qc->id == ici->ops->controls[i].id) {
  526. memcpy(qc, &(ici->ops->controls[i]),
  527. sizeof(*qc));
  528. return 0;
  529. }
  530. /* Then device controls */
  531. for (i = 0; i < icd->ops->num_controls; i++)
  532. if (qc->id == icd->ops->controls[i].id) {
  533. memcpy(qc, &(icd->ops->controls[i]),
  534. sizeof(*qc));
  535. return 0;
  536. }
  537. return -EINVAL;
  538. }
  539. static int soc_camera_g_ctrl(struct file *file, void *priv,
  540. struct v4l2_control *ctrl)
  541. {
  542. struct soc_camera_file *icf = file->private_data;
  543. struct soc_camera_device *icd = icf->icd;
  544. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  545. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  546. int ret;
  547. WARN_ON(priv != file->private_data);
  548. if (ici->ops->get_ctrl) {
  549. ret = ici->ops->get_ctrl(icd, ctrl);
  550. if (ret != -ENOIOCTLCMD)
  551. return ret;
  552. }
  553. return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
  554. }
  555. static int soc_camera_s_ctrl(struct file *file, void *priv,
  556. struct v4l2_control *ctrl)
  557. {
  558. struct soc_camera_file *icf = file->private_data;
  559. struct soc_camera_device *icd = icf->icd;
  560. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  561. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  562. int ret;
  563. WARN_ON(priv != file->private_data);
  564. if (ici->ops->set_ctrl) {
  565. ret = ici->ops->set_ctrl(icd, ctrl);
  566. if (ret != -ENOIOCTLCMD)
  567. return ret;
  568. }
  569. return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
  570. }
  571. static int soc_camera_cropcap(struct file *file, void *fh,
  572. struct v4l2_cropcap *a)
  573. {
  574. struct soc_camera_file *icf = file->private_data;
  575. struct soc_camera_device *icd = icf->icd;
  576. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  577. return ici->ops->cropcap(icd, a);
  578. }
  579. static int soc_camera_g_crop(struct file *file, void *fh,
  580. struct v4l2_crop *a)
  581. {
  582. struct soc_camera_file *icf = file->private_data;
  583. struct soc_camera_device *icd = icf->icd;
  584. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  585. int ret;
  586. mutex_lock(&icf->vb_vidq.vb_lock);
  587. ret = ici->ops->get_crop(icd, a);
  588. mutex_unlock(&icf->vb_vidq.vb_lock);
  589. return ret;
  590. }
  591. /*
  592. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  593. * argument with the actual geometry, instead, the user shall use G_CROP to
  594. * retrieve it. However, we expect camera host and client drivers to update
  595. * the argument, which we then use internally, but do not return to the user.
  596. */
  597. static int soc_camera_s_crop(struct file *file, void *fh,
  598. struct v4l2_crop *a)
  599. {
  600. struct soc_camera_file *icf = file->private_data;
  601. struct soc_camera_device *icd = icf->icd;
  602. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  603. struct v4l2_rect *rect = &a->c;
  604. struct v4l2_crop current_crop;
  605. int ret;
  606. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  607. return -EINVAL;
  608. dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
  609. rect->width, rect->height, rect->left, rect->top);
  610. /* Cropping is allowed during a running capture, guard consistency */
  611. mutex_lock(&icf->vb_vidq.vb_lock);
  612. /* If get_crop fails, we'll let host and / or client drivers decide */
  613. ret = ici->ops->get_crop(icd, &current_crop);
  614. /* Prohibit window size change with initialised buffers */
  615. if (icf->vb_vidq.bufs[0] && !ret &&
  616. (a->c.width != current_crop.c.width ||
  617. a->c.height != current_crop.c.height)) {
  618. dev_err(&icd->dev,
  619. "S_CROP denied: queue initialised and sizes differ\n");
  620. ret = -EBUSY;
  621. } else {
  622. ret = ici->ops->set_crop(icd, a);
  623. }
  624. mutex_unlock(&icf->vb_vidq.vb_lock);
  625. return ret;
  626. }
  627. static int soc_camera_g_parm(struct file *file, void *fh,
  628. struct v4l2_streamparm *a)
  629. {
  630. struct soc_camera_file *icf = file->private_data;
  631. struct soc_camera_device *icd = icf->icd;
  632. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  633. if (ici->ops->get_parm)
  634. return ici->ops->get_parm(icd, a);
  635. return -ENOIOCTLCMD;
  636. }
  637. static int soc_camera_s_parm(struct file *file, void *fh,
  638. struct v4l2_streamparm *a)
  639. {
  640. struct soc_camera_file *icf = file->private_data;
  641. struct soc_camera_device *icd = icf->icd;
  642. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  643. if (ici->ops->set_parm)
  644. return ici->ops->set_parm(icd, a);
  645. return -ENOIOCTLCMD;
  646. }
  647. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  648. struct v4l2_dbg_chip_ident *id)
  649. {
  650. struct soc_camera_file *icf = file->private_data;
  651. struct soc_camera_device *icd = icf->icd;
  652. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  653. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  654. }
  655. #ifdef CONFIG_VIDEO_ADV_DEBUG
  656. static int soc_camera_g_register(struct file *file, void *fh,
  657. struct v4l2_dbg_register *reg)
  658. {
  659. struct soc_camera_file *icf = file->private_data;
  660. struct soc_camera_device *icd = icf->icd;
  661. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  662. return v4l2_subdev_call(sd, core, g_register, reg);
  663. }
  664. static int soc_camera_s_register(struct file *file, void *fh,
  665. struct v4l2_dbg_register *reg)
  666. {
  667. struct soc_camera_file *icf = file->private_data;
  668. struct soc_camera_device *icd = icf->icd;
  669. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  670. return v4l2_subdev_call(sd, core, 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->module_name, icl->board_info, NULL);
  710. if (!subdev)
  711. goto ei2cnd;
  712. client = subdev->priv;
  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. static 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. static struct device_driver ic_drv = {
  893. .name = "camera",
  894. .bus = &soc_camera_bus_type,
  895. .owner = THIS_MODULE,
  896. };
  897. static void dummy_release(struct device *dev)
  898. {
  899. }
  900. static int default_cropcap(struct soc_camera_device *icd,
  901. struct v4l2_cropcap *a)
  902. {
  903. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  904. return v4l2_subdev_call(sd, video, cropcap, a);
  905. }
  906. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  907. {
  908. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  909. return v4l2_subdev_call(sd, video, g_crop, a);
  910. }
  911. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  912. {
  913. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  914. return v4l2_subdev_call(sd, video, s_crop, a);
  915. }
  916. static void soc_camera_device_init(struct device *dev, void *pdata)
  917. {
  918. dev->platform_data = pdata;
  919. dev->bus = &soc_camera_bus_type;
  920. dev->release = dummy_release;
  921. }
  922. int soc_camera_host_register(struct soc_camera_host *ici)
  923. {
  924. struct soc_camera_host *ix;
  925. int ret;
  926. if (!ici || !ici->ops ||
  927. !ici->ops->try_fmt ||
  928. !ici->ops->set_fmt ||
  929. !ici->ops->set_bus_param ||
  930. !ici->ops->querycap ||
  931. !ici->ops->init_videobuf ||
  932. !ici->ops->reqbufs ||
  933. !ici->ops->add ||
  934. !ici->ops->remove ||
  935. !ici->ops->poll ||
  936. !ici->v4l2_dev.dev)
  937. return -EINVAL;
  938. if (!ici->ops->set_crop)
  939. ici->ops->set_crop = default_s_crop;
  940. if (!ici->ops->get_crop)
  941. ici->ops->get_crop = default_g_crop;
  942. if (!ici->ops->cropcap)
  943. ici->ops->cropcap = default_cropcap;
  944. mutex_lock(&list_lock);
  945. list_for_each_entry(ix, &hosts, list) {
  946. if (ix->nr == ici->nr) {
  947. ret = -EBUSY;
  948. goto edevreg;
  949. }
  950. }
  951. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  952. if (ret < 0)
  953. goto edevreg;
  954. list_add_tail(&ici->list, &hosts);
  955. mutex_unlock(&list_lock);
  956. scan_add_host(ici);
  957. return 0;
  958. edevreg:
  959. mutex_unlock(&list_lock);
  960. return ret;
  961. }
  962. EXPORT_SYMBOL(soc_camera_host_register);
  963. /* Unregister all clients! */
  964. void soc_camera_host_unregister(struct soc_camera_host *ici)
  965. {
  966. struct soc_camera_device *icd;
  967. mutex_lock(&list_lock);
  968. list_del(&ici->list);
  969. list_for_each_entry(icd, &devices, list) {
  970. if (icd->iface == ici->nr) {
  971. void *pdata = icd->dev.platform_data;
  972. /* The bus->remove will be called */
  973. device_unregister(&icd->dev);
  974. /*
  975. * Not before device_unregister(), .remove
  976. * needs parent to call ici->ops->remove().
  977. * If the host module is loaded again, device_register()
  978. * would complain "already initialised," since 2.6.32
  979. * this is also needed to prevent use-after-free of the
  980. * device private data.
  981. */
  982. memset(&icd->dev, 0, sizeof(icd->dev));
  983. soc_camera_device_init(&icd->dev, pdata);
  984. }
  985. }
  986. mutex_unlock(&list_lock);
  987. v4l2_device_unregister(&ici->v4l2_dev);
  988. }
  989. EXPORT_SYMBOL(soc_camera_host_unregister);
  990. /* Image capture device */
  991. static int soc_camera_device_register(struct soc_camera_device *icd)
  992. {
  993. struct soc_camera_device *ix;
  994. int num = -1, i;
  995. for (i = 0; i < 256 && num < 0; i++) {
  996. num = i;
  997. /* Check if this index is available on this interface */
  998. list_for_each_entry(ix, &devices, list) {
  999. if (ix->iface == icd->iface && ix->devnum == i) {
  1000. num = -1;
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. if (num < 0)
  1006. /*
  1007. * ok, we have 256 cameras on this host...
  1008. * man, stay reasonable...
  1009. */
  1010. return -ENOMEM;
  1011. icd->devnum = num;
  1012. icd->use_count = 0;
  1013. icd->host_priv = NULL;
  1014. mutex_init(&icd->video_lock);
  1015. list_add_tail(&icd->list, &devices);
  1016. return 0;
  1017. }
  1018. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  1019. {
  1020. list_del(&icd->list);
  1021. }
  1022. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1023. .vidioc_querycap = soc_camera_querycap,
  1024. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1025. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1026. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1027. .vidioc_enum_input = soc_camera_enum_input,
  1028. .vidioc_g_input = soc_camera_g_input,
  1029. .vidioc_s_input = soc_camera_s_input,
  1030. .vidioc_s_std = soc_camera_s_std,
  1031. .vidioc_reqbufs = soc_camera_reqbufs,
  1032. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1033. .vidioc_querybuf = soc_camera_querybuf,
  1034. .vidioc_qbuf = soc_camera_qbuf,
  1035. .vidioc_dqbuf = soc_camera_dqbuf,
  1036. .vidioc_streamon = soc_camera_streamon,
  1037. .vidioc_streamoff = soc_camera_streamoff,
  1038. .vidioc_queryctrl = soc_camera_queryctrl,
  1039. .vidioc_g_ctrl = soc_camera_g_ctrl,
  1040. .vidioc_s_ctrl = soc_camera_s_ctrl,
  1041. .vidioc_cropcap = soc_camera_cropcap,
  1042. .vidioc_g_crop = soc_camera_g_crop,
  1043. .vidioc_s_crop = soc_camera_s_crop,
  1044. .vidioc_g_parm = soc_camera_g_parm,
  1045. .vidioc_s_parm = soc_camera_s_parm,
  1046. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1047. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1048. .vidioc_g_register = soc_camera_g_register,
  1049. .vidioc_s_register = soc_camera_s_register,
  1050. #endif
  1051. };
  1052. static int video_dev_create(struct soc_camera_device *icd)
  1053. {
  1054. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1055. struct video_device *vdev = video_device_alloc();
  1056. if (!vdev)
  1057. return -ENOMEM;
  1058. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1059. vdev->parent = &icd->dev;
  1060. vdev->current_norm = V4L2_STD_UNKNOWN;
  1061. vdev->fops = &soc_camera_fops;
  1062. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1063. vdev->release = video_device_release;
  1064. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1065. icd->vdev = vdev;
  1066. return 0;
  1067. }
  1068. /*
  1069. * Called from soc_camera_probe() above (with .video_lock held???)
  1070. */
  1071. static int soc_camera_video_start(struct soc_camera_device *icd)
  1072. {
  1073. int ret;
  1074. if (!icd->dev.parent)
  1075. return -ENODEV;
  1076. if (!icd->ops ||
  1077. !icd->ops->query_bus_param ||
  1078. !icd->ops->set_bus_param)
  1079. return -EINVAL;
  1080. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1081. if (ret < 0) {
  1082. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1083. return ret;
  1084. }
  1085. return 0;
  1086. }
  1087. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1088. {
  1089. struct soc_camera_link *icl = pdev->dev.platform_data;
  1090. struct soc_camera_device *icd;
  1091. int ret;
  1092. if (!icl)
  1093. return -EINVAL;
  1094. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1095. if (!icd)
  1096. return -ENOMEM;
  1097. icd->iface = icl->bus_id;
  1098. icd->pdev = &pdev->dev;
  1099. platform_set_drvdata(pdev, icd);
  1100. ret = soc_camera_device_register(icd);
  1101. if (ret < 0)
  1102. goto escdevreg;
  1103. soc_camera_device_init(&icd->dev, icl);
  1104. icd->user_width = DEFAULT_WIDTH;
  1105. icd->user_height = DEFAULT_HEIGHT;
  1106. return 0;
  1107. escdevreg:
  1108. kfree(icd);
  1109. return ret;
  1110. }
  1111. /*
  1112. * Only called on rmmod for each platform device, since they are not
  1113. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1114. * been unloaded already
  1115. */
  1116. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1117. {
  1118. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1119. if (!icd)
  1120. return -EINVAL;
  1121. soc_camera_device_unregister(icd);
  1122. kfree(icd);
  1123. return 0;
  1124. }
  1125. static struct platform_driver __refdata soc_camera_pdrv = {
  1126. .remove = __devexit_p(soc_camera_pdrv_remove),
  1127. .driver = {
  1128. .name = "soc-camera-pdrv",
  1129. .owner = THIS_MODULE,
  1130. },
  1131. };
  1132. static int __init soc_camera_init(void)
  1133. {
  1134. int ret = bus_register(&soc_camera_bus_type);
  1135. if (ret)
  1136. return ret;
  1137. ret = driver_register(&ic_drv);
  1138. if (ret)
  1139. goto edrvr;
  1140. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1141. if (ret)
  1142. goto epdr;
  1143. return 0;
  1144. epdr:
  1145. driver_unregister(&ic_drv);
  1146. edrvr:
  1147. bus_unregister(&soc_camera_bus_type);
  1148. return ret;
  1149. }
  1150. static void __exit soc_camera_exit(void)
  1151. {
  1152. platform_driver_unregister(&soc_camera_pdrv);
  1153. driver_unregister(&ic_drv);
  1154. bus_unregister(&soc_camera_bus_type);
  1155. }
  1156. module_init(soc_camera_init);
  1157. module_exit(soc_camera_exit);
  1158. MODULE_DESCRIPTION("Image capture bus driver");
  1159. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1160. MODULE_LICENSE("GPL");
  1161. MODULE_ALIAS("platform:soc-camera-pdrv");