soc_camera.c 36 KB

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