soc_camera.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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->dev, "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->dev,
  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->dev,
  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->dev, "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->dev.parent);
  131. struct v4l2_pix_format *pix = &f->fmt.pix;
  132. int ret;
  133. dev_dbg(&icd->dev, "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->dev.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->dev.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->dev.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->dev.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->dev.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->dev.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->dev, "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->dev.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->dev.parent);
  333. struct v4l2_pix_format *pix = &f->fmt.pix;
  334. int ret;
  335. dev_dbg(&icd->dev, "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->dev,
  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->dev, "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 = container_of(vdev->parent,
  367. struct soc_camera_device,
  368. dev);
  369. struct soc_camera_link *icl = to_soc_camera_link(icd);
  370. struct soc_camera_host *ici;
  371. int ret;
  372. if (!icd->ops)
  373. /* No device driver attached */
  374. return -ENODEV;
  375. ici = to_soc_camera_host(icd->dev.parent);
  376. if (!try_module_get(ici->ops->owner)) {
  377. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  378. return -EINVAL;
  379. }
  380. icd->use_count++;
  381. /* Now we really have to activate the camera */
  382. if (icd->use_count == 1) {
  383. /* Restore parameters before the last close() per V4L2 API */
  384. struct v4l2_format f = {
  385. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  386. .fmt.pix = {
  387. .width = icd->user_width,
  388. .height = icd->user_height,
  389. .field = icd->field,
  390. .colorspace = icd->colorspace,
  391. .pixelformat =
  392. icd->current_fmt->host_fmt->fourcc,
  393. },
  394. };
  395. ret = soc_camera_power_set(icd, icl, 1);
  396. if (ret < 0)
  397. goto epower;
  398. /* The camera could have been already on, try to reset */
  399. if (icl->reset)
  400. icl->reset(icd->pdev);
  401. ret = ici->ops->add(icd);
  402. if (ret < 0) {
  403. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  404. goto eiciadd;
  405. }
  406. pm_runtime_enable(&icd->vdev->dev);
  407. ret = pm_runtime_resume(&icd->vdev->dev);
  408. if (ret < 0 && ret != -ENOSYS)
  409. goto eresume;
  410. /*
  411. * Try to configure with default parameters. Notice: this is the
  412. * very first open, so, we cannot race against other calls,
  413. * apart from someone else calling open() simultaneously, but
  414. * .video_lock is protecting us against it.
  415. */
  416. ret = soc_camera_set_fmt(icd, &f);
  417. if (ret < 0)
  418. goto esfmt;
  419. if (ici->ops->init_videobuf) {
  420. ici->ops->init_videobuf(&icd->vb_vidq, icd);
  421. } else {
  422. ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
  423. if (ret < 0)
  424. goto einitvb;
  425. }
  426. }
  427. file->private_data = icd;
  428. dev_dbg(&icd->dev, "camera device open\n");
  429. return 0;
  430. /*
  431. * First four errors are entered with the .video_lock held
  432. * and use_count == 1
  433. */
  434. einitvb:
  435. esfmt:
  436. pm_runtime_disable(&icd->vdev->dev);
  437. eresume:
  438. ici->ops->remove(icd);
  439. eiciadd:
  440. soc_camera_power_set(icd, icl, 0);
  441. epower:
  442. icd->use_count--;
  443. module_put(ici->ops->owner);
  444. return ret;
  445. }
  446. static int soc_camera_close(struct file *file)
  447. {
  448. struct soc_camera_device *icd = file->private_data;
  449. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  450. icd->use_count--;
  451. if (!icd->use_count) {
  452. struct soc_camera_link *icl = to_soc_camera_link(icd);
  453. pm_runtime_suspend(&icd->vdev->dev);
  454. pm_runtime_disable(&icd->vdev->dev);
  455. ici->ops->remove(icd);
  456. if (ici->ops->init_videobuf2)
  457. vb2_queue_release(&icd->vb2_vidq);
  458. soc_camera_power_set(icd, icl, 0);
  459. }
  460. if (icd->streamer == file)
  461. icd->streamer = NULL;
  462. module_put(ici->ops->owner);
  463. dev_dbg(&icd->dev, "camera device close\n");
  464. return 0;
  465. }
  466. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  467. size_t count, loff_t *ppos)
  468. {
  469. struct soc_camera_device *icd = file->private_data;
  470. int err = -EINVAL;
  471. dev_err(&icd->dev, "camera device read not implemented\n");
  472. return err;
  473. }
  474. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  475. {
  476. struct soc_camera_device *icd = file->private_data;
  477. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  478. int err;
  479. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  480. if (icd->streamer != file)
  481. return -EBUSY;
  482. if (ici->ops->init_videobuf)
  483. err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
  484. else
  485. err = vb2_mmap(&icd->vb2_vidq, vma);
  486. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  487. (unsigned long)vma->vm_start,
  488. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  489. err);
  490. return err;
  491. }
  492. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  493. {
  494. struct soc_camera_device *icd = file->private_data;
  495. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  496. if (icd->streamer != file)
  497. return -EBUSY;
  498. if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
  499. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  500. return POLLERR;
  501. }
  502. return ici->ops->poll(file, pt);
  503. }
  504. void soc_camera_lock(struct vb2_queue *vq)
  505. {
  506. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  507. mutex_lock(&icd->video_lock);
  508. }
  509. EXPORT_SYMBOL(soc_camera_lock);
  510. void soc_camera_unlock(struct vb2_queue *vq)
  511. {
  512. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  513. mutex_unlock(&icd->video_lock);
  514. }
  515. EXPORT_SYMBOL(soc_camera_unlock);
  516. static struct v4l2_file_operations soc_camera_fops = {
  517. .owner = THIS_MODULE,
  518. .open = soc_camera_open,
  519. .release = soc_camera_close,
  520. .unlocked_ioctl = video_ioctl2,
  521. .read = soc_camera_read,
  522. .mmap = soc_camera_mmap,
  523. .poll = soc_camera_poll,
  524. };
  525. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  526. struct v4l2_format *f)
  527. {
  528. struct soc_camera_device *icd = file->private_data;
  529. int ret;
  530. WARN_ON(priv != file->private_data);
  531. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  532. dev_warn(&icd->dev, "Wrong buf-type %d\n", f->type);
  533. return -EINVAL;
  534. }
  535. if (icd->streamer && icd->streamer != file)
  536. return -EBUSY;
  537. if (is_streaming(to_soc_camera_host(icd->dev.parent), icd)) {
  538. dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
  539. return -EBUSY;
  540. }
  541. ret = soc_camera_set_fmt(icd, f);
  542. if (!ret && !icd->streamer)
  543. icd->streamer = file;
  544. return ret;
  545. }
  546. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  547. struct v4l2_fmtdesc *f)
  548. {
  549. struct soc_camera_device *icd = file->private_data;
  550. const struct soc_mbus_pixelfmt *format;
  551. WARN_ON(priv != file->private_data);
  552. if (f->index >= icd->num_user_formats)
  553. return -EINVAL;
  554. format = icd->user_formats[f->index].host_fmt;
  555. if (format->name)
  556. strlcpy(f->description, format->name, sizeof(f->description));
  557. f->pixelformat = format->fourcc;
  558. return 0;
  559. }
  560. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  561. struct v4l2_format *f)
  562. {
  563. struct soc_camera_device *icd = file->private_data;
  564. struct v4l2_pix_format *pix = &f->fmt.pix;
  565. WARN_ON(priv != file->private_data);
  566. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  567. return -EINVAL;
  568. pix->width = icd->user_width;
  569. pix->height = icd->user_height;
  570. pix->bytesperline = icd->bytesperline;
  571. pix->sizeimage = icd->sizeimage;
  572. pix->field = icd->field;
  573. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  574. pix->colorspace = icd->colorspace;
  575. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  576. icd->current_fmt->host_fmt->fourcc);
  577. return 0;
  578. }
  579. static int soc_camera_querycap(struct file *file, void *priv,
  580. struct v4l2_capability *cap)
  581. {
  582. struct soc_camera_device *icd = file->private_data;
  583. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  584. WARN_ON(priv != file->private_data);
  585. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  586. return ici->ops->querycap(ici, cap);
  587. }
  588. static int soc_camera_streamon(struct file *file, void *priv,
  589. enum v4l2_buf_type i)
  590. {
  591. struct soc_camera_device *icd = file->private_data;
  592. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  593. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  594. int ret;
  595. WARN_ON(priv != file->private_data);
  596. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  597. return -EINVAL;
  598. if (icd->streamer != file)
  599. return -EBUSY;
  600. /* This calls buf_queue from host driver's videobuf_queue_ops */
  601. if (ici->ops->init_videobuf)
  602. ret = videobuf_streamon(&icd->vb_vidq);
  603. else
  604. ret = vb2_streamon(&icd->vb2_vidq, i);
  605. if (!ret)
  606. v4l2_subdev_call(sd, video, s_stream, 1);
  607. return ret;
  608. }
  609. static int soc_camera_streamoff(struct file *file, void *priv,
  610. enum v4l2_buf_type i)
  611. {
  612. struct soc_camera_device *icd = file->private_data;
  613. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  614. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  615. WARN_ON(priv != file->private_data);
  616. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  617. return -EINVAL;
  618. if (icd->streamer != file)
  619. return -EBUSY;
  620. /*
  621. * This calls buf_release from host driver's videobuf_queue_ops for all
  622. * remaining buffers. When the last buffer is freed, stop capture
  623. */
  624. if (ici->ops->init_videobuf)
  625. videobuf_streamoff(&icd->vb_vidq);
  626. else
  627. vb2_streamoff(&icd->vb2_vidq, i);
  628. v4l2_subdev_call(sd, video, s_stream, 0);
  629. return 0;
  630. }
  631. static int soc_camera_queryctrl(struct file *file, void *priv,
  632. struct v4l2_queryctrl *qc)
  633. {
  634. struct soc_camera_device *icd = file->private_data;
  635. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  636. int i;
  637. WARN_ON(priv != file->private_data);
  638. if (!qc->id)
  639. return -EINVAL;
  640. /* First check host controls */
  641. for (i = 0; i < ici->ops->num_controls; i++)
  642. if (qc->id == ici->ops->controls[i].id) {
  643. memcpy(qc, &(ici->ops->controls[i]),
  644. sizeof(*qc));
  645. return 0;
  646. }
  647. /* Then device controls */
  648. for (i = 0; i < icd->ops->num_controls; i++)
  649. if (qc->id == icd->ops->controls[i].id) {
  650. memcpy(qc, &(icd->ops->controls[i]),
  651. sizeof(*qc));
  652. return 0;
  653. }
  654. return -EINVAL;
  655. }
  656. static int soc_camera_g_ctrl(struct file *file, void *priv,
  657. struct v4l2_control *ctrl)
  658. {
  659. struct soc_camera_device *icd = file->private_data;
  660. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  661. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  662. int ret;
  663. WARN_ON(priv != file->private_data);
  664. if (ici->ops->get_ctrl) {
  665. ret = ici->ops->get_ctrl(icd, ctrl);
  666. if (ret != -ENOIOCTLCMD)
  667. return ret;
  668. }
  669. return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
  670. }
  671. static int soc_camera_s_ctrl(struct file *file, void *priv,
  672. struct v4l2_control *ctrl)
  673. {
  674. struct soc_camera_device *icd = file->private_data;
  675. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  676. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  677. int ret;
  678. WARN_ON(priv != file->private_data);
  679. if (ici->ops->set_ctrl) {
  680. ret = ici->ops->set_ctrl(icd, ctrl);
  681. if (ret != -ENOIOCTLCMD)
  682. return ret;
  683. }
  684. return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
  685. }
  686. static int soc_camera_cropcap(struct file *file, void *fh,
  687. struct v4l2_cropcap *a)
  688. {
  689. struct soc_camera_device *icd = file->private_data;
  690. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  691. return ici->ops->cropcap(icd, a);
  692. }
  693. static int soc_camera_g_crop(struct file *file, void *fh,
  694. struct v4l2_crop *a)
  695. {
  696. struct soc_camera_device *icd = file->private_data;
  697. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  698. int ret;
  699. ret = ici->ops->get_crop(icd, a);
  700. return ret;
  701. }
  702. /*
  703. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  704. * argument with the actual geometry, instead, the user shall use G_CROP to
  705. * retrieve it.
  706. */
  707. static int soc_camera_s_crop(struct file *file, void *fh,
  708. struct v4l2_crop *a)
  709. {
  710. struct soc_camera_device *icd = file->private_data;
  711. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  712. struct v4l2_rect *rect = &a->c;
  713. struct v4l2_crop current_crop;
  714. int ret;
  715. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  716. return -EINVAL;
  717. dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
  718. rect->width, rect->height, rect->left, rect->top);
  719. /* If get_crop fails, we'll let host and / or client drivers decide */
  720. ret = ici->ops->get_crop(icd, &current_crop);
  721. /* Prohibit window size change with initialised buffers */
  722. if (ret < 0) {
  723. dev_err(&icd->dev,
  724. "S_CROP denied: getting current crop failed\n");
  725. } else if ((a->c.width == current_crop.c.width &&
  726. a->c.height == current_crop.c.height) ||
  727. !is_streaming(ici, icd)) {
  728. /* same size or not streaming - use .set_crop() */
  729. ret = ici->ops->set_crop(icd, a);
  730. } else if (ici->ops->set_livecrop) {
  731. ret = ici->ops->set_livecrop(icd, a);
  732. } else {
  733. dev_err(&icd->dev,
  734. "S_CROP denied: queue initialised and sizes differ\n");
  735. ret = -EBUSY;
  736. }
  737. return ret;
  738. }
  739. static int soc_camera_g_parm(struct file *file, void *fh,
  740. struct v4l2_streamparm *a)
  741. {
  742. struct soc_camera_device *icd = file->private_data;
  743. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  744. if (ici->ops->get_parm)
  745. return ici->ops->get_parm(icd, a);
  746. return -ENOIOCTLCMD;
  747. }
  748. static int soc_camera_s_parm(struct file *file, void *fh,
  749. struct v4l2_streamparm *a)
  750. {
  751. struct soc_camera_device *icd = file->private_data;
  752. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  753. if (ici->ops->set_parm)
  754. return ici->ops->set_parm(icd, a);
  755. return -ENOIOCTLCMD;
  756. }
  757. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  758. struct v4l2_dbg_chip_ident *id)
  759. {
  760. struct soc_camera_device *icd = file->private_data;
  761. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  762. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  763. }
  764. #ifdef CONFIG_VIDEO_ADV_DEBUG
  765. static int soc_camera_g_register(struct file *file, void *fh,
  766. struct v4l2_dbg_register *reg)
  767. {
  768. struct soc_camera_device *icd = file->private_data;
  769. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  770. return v4l2_subdev_call(sd, core, g_register, reg);
  771. }
  772. static int soc_camera_s_register(struct file *file, void *fh,
  773. struct v4l2_dbg_register *reg)
  774. {
  775. struct soc_camera_device *icd = file->private_data;
  776. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  777. return v4l2_subdev_call(sd, core, s_register, reg);
  778. }
  779. #endif
  780. /* So far this function cannot fail */
  781. static void scan_add_host(struct soc_camera_host *ici)
  782. {
  783. struct soc_camera_device *icd;
  784. mutex_lock(&list_lock);
  785. list_for_each_entry(icd, &devices, list) {
  786. if (icd->iface == ici->nr) {
  787. int ret;
  788. icd->dev.parent = ici->v4l2_dev.dev;
  789. dev_set_name(&icd->dev, "%u-%u", icd->iface,
  790. icd->devnum);
  791. ret = device_register(&icd->dev);
  792. if (ret < 0) {
  793. icd->dev.parent = NULL;
  794. dev_err(&icd->dev,
  795. "Cannot register device: %d\n", ret);
  796. }
  797. }
  798. }
  799. mutex_unlock(&list_lock);
  800. }
  801. #ifdef CONFIG_I2C_BOARDINFO
  802. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  803. struct soc_camera_link *icl)
  804. {
  805. struct i2c_client *client;
  806. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  807. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  808. struct v4l2_subdev *subdev;
  809. if (!adap) {
  810. dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
  811. icl->i2c_adapter_id);
  812. goto ei2cga;
  813. }
  814. icl->board_info->platform_data = icd;
  815. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  816. icl->board_info, NULL);
  817. if (!subdev)
  818. goto ei2cnd;
  819. client = v4l2_get_subdevdata(subdev);
  820. /* Use to_i2c_client(dev) to recover the i2c client */
  821. dev_set_drvdata(&icd->dev, &client->dev);
  822. return 0;
  823. ei2cnd:
  824. i2c_put_adapter(adap);
  825. ei2cga:
  826. return -ENODEV;
  827. }
  828. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  829. {
  830. struct i2c_client *client =
  831. to_i2c_client(to_soc_camera_control(icd));
  832. struct i2c_adapter *adap = client->adapter;
  833. dev_set_drvdata(&icd->dev, NULL);
  834. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  835. i2c_unregister_device(client);
  836. i2c_put_adapter(adap);
  837. }
  838. #else
  839. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  840. #define soc_camera_free_i2c(icd) do {} while (0)
  841. #endif
  842. static int soc_camera_video_start(struct soc_camera_device *icd);
  843. static int video_dev_create(struct soc_camera_device *icd);
  844. /* Called during host-driver probe */
  845. static int soc_camera_probe(struct device *dev)
  846. {
  847. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  848. struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
  849. struct soc_camera_link *icl = to_soc_camera_link(icd);
  850. struct device *control = NULL;
  851. struct v4l2_subdev *sd;
  852. struct v4l2_mbus_framefmt mf;
  853. int ret;
  854. dev_info(dev, "Probing %s\n", dev_name(dev));
  855. ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
  856. icl->regulators);
  857. if (ret < 0)
  858. goto ereg;
  859. ret = soc_camera_power_set(icd, icl, 1);
  860. if (ret < 0)
  861. goto epower;
  862. /* The camera could have been already on, try to reset */
  863. if (icl->reset)
  864. icl->reset(icd->pdev);
  865. ret = ici->ops->add(icd);
  866. if (ret < 0)
  867. goto eadd;
  868. /* Must have icd->vdev before registering the device */
  869. ret = video_dev_create(icd);
  870. if (ret < 0)
  871. goto evdc;
  872. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  873. if (icl->board_info) {
  874. ret = soc_camera_init_i2c(icd, icl);
  875. if (ret < 0)
  876. goto eadddev;
  877. } else if (!icl->add_device || !icl->del_device) {
  878. ret = -EINVAL;
  879. goto eadddev;
  880. } else {
  881. if (icl->module_name)
  882. ret = request_module(icl->module_name);
  883. ret = icl->add_device(icl, &icd->dev);
  884. if (ret < 0)
  885. goto eadddev;
  886. /*
  887. * FIXME: this is racy, have to use driver-binding notification,
  888. * when it is available
  889. */
  890. control = to_soc_camera_control(icd);
  891. if (!control || !control->driver || !dev_get_drvdata(control) ||
  892. !try_module_get(control->driver->owner)) {
  893. icl->del_device(icl);
  894. goto enodrv;
  895. }
  896. }
  897. sd = soc_camera_to_subdev(icd);
  898. sd->grp_id = (long)icd;
  899. /* At this point client .probe() should have run already */
  900. ret = soc_camera_init_user_formats(icd);
  901. if (ret < 0)
  902. goto eiufmt;
  903. icd->field = V4L2_FIELD_ANY;
  904. icd->vdev->lock = &icd->video_lock;
  905. /*
  906. * ..._video_start() will create a device node, video_register_device()
  907. * itself is protected against concurrent open() calls, but we also have
  908. * to protect our data.
  909. */
  910. mutex_lock(&icd->video_lock);
  911. ret = soc_camera_video_start(icd);
  912. if (ret < 0)
  913. goto evidstart;
  914. /* Try to improve our guess of a reasonable window format */
  915. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  916. icd->user_width = mf.width;
  917. icd->user_height = mf.height;
  918. icd->colorspace = mf.colorspace;
  919. icd->field = mf.field;
  920. }
  921. /* Do we have to sysfs_remove_link() before device_unregister()? */
  922. if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  923. "control"))
  924. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  925. ici->ops->remove(icd);
  926. soc_camera_power_set(icd, icl, 0);
  927. mutex_unlock(&icd->video_lock);
  928. return 0;
  929. evidstart:
  930. mutex_unlock(&icd->video_lock);
  931. soc_camera_free_user_formats(icd);
  932. eiufmt:
  933. if (icl->board_info) {
  934. soc_camera_free_i2c(icd);
  935. } else {
  936. icl->del_device(icl);
  937. module_put(control->driver->owner);
  938. }
  939. enodrv:
  940. eadddev:
  941. video_device_release(icd->vdev);
  942. evdc:
  943. ici->ops->remove(icd);
  944. eadd:
  945. soc_camera_power_set(icd, icl, 0);
  946. epower:
  947. regulator_bulk_free(icl->num_regulators, icl->regulators);
  948. ereg:
  949. return ret;
  950. }
  951. /*
  952. * This is called on device_unregister, which only means we have to disconnect
  953. * from the host, but not remove ourselves from the device list
  954. */
  955. static int soc_camera_remove(struct device *dev)
  956. {
  957. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  958. struct soc_camera_link *icl = to_soc_camera_link(icd);
  959. struct video_device *vdev = icd->vdev;
  960. BUG_ON(!dev->parent);
  961. if (vdev) {
  962. video_unregister_device(vdev);
  963. icd->vdev = NULL;
  964. }
  965. if (icl->board_info) {
  966. soc_camera_free_i2c(icd);
  967. } else {
  968. struct device_driver *drv = to_soc_camera_control(icd) ?
  969. to_soc_camera_control(icd)->driver : NULL;
  970. if (drv) {
  971. icl->del_device(icl);
  972. module_put(drv->owner);
  973. }
  974. }
  975. soc_camera_free_user_formats(icd);
  976. regulator_bulk_free(icl->num_regulators, icl->regulators);
  977. return 0;
  978. }
  979. struct bus_type soc_camera_bus_type = {
  980. .name = "soc-camera",
  981. .probe = soc_camera_probe,
  982. .remove = soc_camera_remove,
  983. };
  984. EXPORT_SYMBOL_GPL(soc_camera_bus_type);
  985. static struct device_driver ic_drv = {
  986. .name = "camera",
  987. .bus = &soc_camera_bus_type,
  988. .owner = THIS_MODULE,
  989. };
  990. static void dummy_release(struct device *dev)
  991. {
  992. }
  993. static int default_cropcap(struct soc_camera_device *icd,
  994. struct v4l2_cropcap *a)
  995. {
  996. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  997. return v4l2_subdev_call(sd, video, cropcap, a);
  998. }
  999. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  1000. {
  1001. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1002. return v4l2_subdev_call(sd, video, g_crop, a);
  1003. }
  1004. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  1005. {
  1006. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1007. return v4l2_subdev_call(sd, video, s_crop, a);
  1008. }
  1009. static int default_g_parm(struct soc_camera_device *icd,
  1010. struct v4l2_streamparm *parm)
  1011. {
  1012. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1013. return v4l2_subdev_call(sd, video, g_parm, parm);
  1014. }
  1015. static int default_s_parm(struct soc_camera_device *icd,
  1016. struct v4l2_streamparm *parm)
  1017. {
  1018. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1019. return v4l2_subdev_call(sd, video, s_parm, parm);
  1020. }
  1021. static int default_enum_fsizes(struct soc_camera_device *icd,
  1022. struct v4l2_frmsizeenum *fsize)
  1023. {
  1024. int ret;
  1025. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1026. const struct soc_camera_format_xlate *xlate;
  1027. __u32 pixfmt = fsize->pixel_format;
  1028. struct v4l2_frmsizeenum fsize_mbus = *fsize;
  1029. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1030. if (!xlate)
  1031. return -EINVAL;
  1032. /* map xlate-code to pixel_format, sensor only handle xlate-code*/
  1033. fsize_mbus.pixel_format = xlate->code;
  1034. ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
  1035. if (ret < 0)
  1036. return ret;
  1037. *fsize = fsize_mbus;
  1038. fsize->pixel_format = pixfmt;
  1039. return 0;
  1040. }
  1041. static void soc_camera_device_init(struct device *dev, void *pdata)
  1042. {
  1043. dev->platform_data = pdata;
  1044. dev->bus = &soc_camera_bus_type;
  1045. dev->release = dummy_release;
  1046. }
  1047. int soc_camera_host_register(struct soc_camera_host *ici)
  1048. {
  1049. struct soc_camera_host *ix;
  1050. int ret;
  1051. if (!ici || !ici->ops ||
  1052. !ici->ops->try_fmt ||
  1053. !ici->ops->set_fmt ||
  1054. !ici->ops->set_bus_param ||
  1055. !ici->ops->querycap ||
  1056. ((!ici->ops->init_videobuf ||
  1057. !ici->ops->reqbufs) &&
  1058. !ici->ops->init_videobuf2) ||
  1059. !ici->ops->add ||
  1060. !ici->ops->remove ||
  1061. !ici->ops->poll ||
  1062. !ici->v4l2_dev.dev)
  1063. return -EINVAL;
  1064. if (!ici->ops->set_crop)
  1065. ici->ops->set_crop = default_s_crop;
  1066. if (!ici->ops->get_crop)
  1067. ici->ops->get_crop = default_g_crop;
  1068. if (!ici->ops->cropcap)
  1069. ici->ops->cropcap = default_cropcap;
  1070. if (!ici->ops->set_parm)
  1071. ici->ops->set_parm = default_s_parm;
  1072. if (!ici->ops->get_parm)
  1073. ici->ops->get_parm = default_g_parm;
  1074. if (!ici->ops->enum_fsizes)
  1075. ici->ops->enum_fsizes = default_enum_fsizes;
  1076. mutex_lock(&list_lock);
  1077. list_for_each_entry(ix, &hosts, list) {
  1078. if (ix->nr == ici->nr) {
  1079. ret = -EBUSY;
  1080. goto edevreg;
  1081. }
  1082. }
  1083. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1084. if (ret < 0)
  1085. goto edevreg;
  1086. list_add_tail(&ici->list, &hosts);
  1087. mutex_unlock(&list_lock);
  1088. scan_add_host(ici);
  1089. return 0;
  1090. edevreg:
  1091. mutex_unlock(&list_lock);
  1092. return ret;
  1093. }
  1094. EXPORT_SYMBOL(soc_camera_host_register);
  1095. /* Unregister all clients! */
  1096. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1097. {
  1098. struct soc_camera_device *icd;
  1099. mutex_lock(&list_lock);
  1100. list_del(&ici->list);
  1101. list_for_each_entry(icd, &devices, list) {
  1102. if (icd->iface == ici->nr) {
  1103. void *pdata = icd->dev.platform_data;
  1104. /* The bus->remove will be called */
  1105. device_unregister(&icd->dev);
  1106. /*
  1107. * Not before device_unregister(), .remove
  1108. * needs parent to call ici->ops->remove().
  1109. * If the host module is loaded again, device_register()
  1110. * would complain "already initialised," since 2.6.32
  1111. * this is also needed to prevent use-after-free of the
  1112. * device private data.
  1113. */
  1114. memset(&icd->dev, 0, sizeof(icd->dev));
  1115. soc_camera_device_init(&icd->dev, pdata);
  1116. }
  1117. }
  1118. mutex_unlock(&list_lock);
  1119. v4l2_device_unregister(&ici->v4l2_dev);
  1120. }
  1121. EXPORT_SYMBOL(soc_camera_host_unregister);
  1122. /* Image capture device */
  1123. static int soc_camera_device_register(struct soc_camera_device *icd)
  1124. {
  1125. struct soc_camera_device *ix;
  1126. int num = -1, i;
  1127. for (i = 0; i < 256 && num < 0; i++) {
  1128. num = i;
  1129. /* Check if this index is available on this interface */
  1130. list_for_each_entry(ix, &devices, list) {
  1131. if (ix->iface == icd->iface && ix->devnum == i) {
  1132. num = -1;
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. if (num < 0)
  1138. /*
  1139. * ok, we have 256 cameras on this host...
  1140. * man, stay reasonable...
  1141. */
  1142. return -ENOMEM;
  1143. icd->devnum = num;
  1144. icd->use_count = 0;
  1145. icd->host_priv = NULL;
  1146. mutex_init(&icd->video_lock);
  1147. list_add_tail(&icd->list, &devices);
  1148. return 0;
  1149. }
  1150. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  1151. {
  1152. list_del(&icd->list);
  1153. }
  1154. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1155. .vidioc_querycap = soc_camera_querycap,
  1156. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1157. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1158. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1159. .vidioc_enum_input = soc_camera_enum_input,
  1160. .vidioc_g_input = soc_camera_g_input,
  1161. .vidioc_s_input = soc_camera_s_input,
  1162. .vidioc_s_std = soc_camera_s_std,
  1163. .vidioc_enum_framesizes = soc_camera_enum_fsizes,
  1164. .vidioc_reqbufs = soc_camera_reqbufs,
  1165. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1166. .vidioc_querybuf = soc_camera_querybuf,
  1167. .vidioc_qbuf = soc_camera_qbuf,
  1168. .vidioc_dqbuf = soc_camera_dqbuf,
  1169. .vidioc_streamon = soc_camera_streamon,
  1170. .vidioc_streamoff = soc_camera_streamoff,
  1171. .vidioc_queryctrl = soc_camera_queryctrl,
  1172. .vidioc_g_ctrl = soc_camera_g_ctrl,
  1173. .vidioc_s_ctrl = soc_camera_s_ctrl,
  1174. .vidioc_cropcap = soc_camera_cropcap,
  1175. .vidioc_g_crop = soc_camera_g_crop,
  1176. .vidioc_s_crop = soc_camera_s_crop,
  1177. .vidioc_g_parm = soc_camera_g_parm,
  1178. .vidioc_s_parm = soc_camera_s_parm,
  1179. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1180. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1181. .vidioc_g_register = soc_camera_g_register,
  1182. .vidioc_s_register = soc_camera_s_register,
  1183. #endif
  1184. };
  1185. static int video_dev_create(struct soc_camera_device *icd)
  1186. {
  1187. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1188. struct video_device *vdev = video_device_alloc();
  1189. if (!vdev)
  1190. return -ENOMEM;
  1191. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1192. vdev->parent = &icd->dev;
  1193. vdev->current_norm = V4L2_STD_UNKNOWN;
  1194. vdev->fops = &soc_camera_fops;
  1195. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1196. vdev->release = video_device_release;
  1197. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1198. icd->vdev = vdev;
  1199. return 0;
  1200. }
  1201. /*
  1202. * Called from soc_camera_probe() above (with .video_lock held???)
  1203. */
  1204. static int soc_camera_video_start(struct soc_camera_device *icd)
  1205. {
  1206. const struct device_type *type = icd->vdev->dev.type;
  1207. int ret;
  1208. if (!icd->dev.parent)
  1209. return -ENODEV;
  1210. if (!icd->ops ||
  1211. !icd->ops->query_bus_param ||
  1212. !icd->ops->set_bus_param)
  1213. return -EINVAL;
  1214. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1215. if (ret < 0) {
  1216. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1217. return ret;
  1218. }
  1219. /* Restore device type, possibly set by the subdevice driver */
  1220. icd->vdev->dev.type = type;
  1221. return 0;
  1222. }
  1223. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1224. {
  1225. struct soc_camera_link *icl = pdev->dev.platform_data;
  1226. struct soc_camera_device *icd;
  1227. int ret;
  1228. if (!icl)
  1229. return -EINVAL;
  1230. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1231. if (!icd)
  1232. return -ENOMEM;
  1233. icd->iface = icl->bus_id;
  1234. icd->pdev = &pdev->dev;
  1235. platform_set_drvdata(pdev, icd);
  1236. ret = soc_camera_device_register(icd);
  1237. if (ret < 0)
  1238. goto escdevreg;
  1239. soc_camera_device_init(&icd->dev, icl);
  1240. icd->user_width = DEFAULT_WIDTH;
  1241. icd->user_height = DEFAULT_HEIGHT;
  1242. return 0;
  1243. escdevreg:
  1244. kfree(icd);
  1245. return ret;
  1246. }
  1247. /*
  1248. * Only called on rmmod for each platform device, since they are not
  1249. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1250. * been unloaded already
  1251. */
  1252. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1253. {
  1254. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1255. if (!icd)
  1256. return -EINVAL;
  1257. soc_camera_device_unregister(icd);
  1258. kfree(icd);
  1259. return 0;
  1260. }
  1261. static struct platform_driver __refdata soc_camera_pdrv = {
  1262. .remove = __devexit_p(soc_camera_pdrv_remove),
  1263. .driver = {
  1264. .name = "soc-camera-pdrv",
  1265. .owner = THIS_MODULE,
  1266. },
  1267. };
  1268. static int __init soc_camera_init(void)
  1269. {
  1270. int ret = bus_register(&soc_camera_bus_type);
  1271. if (ret)
  1272. return ret;
  1273. ret = driver_register(&ic_drv);
  1274. if (ret)
  1275. goto edrvr;
  1276. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1277. if (ret)
  1278. goto epdr;
  1279. return 0;
  1280. epdr:
  1281. driver_unregister(&ic_drv);
  1282. edrvr:
  1283. bus_unregister(&soc_camera_bus_type);
  1284. return ret;
  1285. }
  1286. static void __exit soc_camera_exit(void)
  1287. {
  1288. platform_driver_unregister(&soc_camera_pdrv);
  1289. driver_unregister(&ic_drv);
  1290. bus_unregister(&soc_camera_bus_type);
  1291. }
  1292. module_init(soc_camera_init);
  1293. module_exit(soc_camera_exit);
  1294. MODULE_DESCRIPTION("Image capture bus driver");
  1295. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1296. MODULE_LICENSE("GPL");
  1297. MODULE_ALIAS("platform:soc-camera-pdrv");