soc_camera.c 38 KB

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