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