soc_camera.c 37 KB

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