soc_camera.c 38 KB

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