soc_camera.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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 (icf->vb_vidq.bufs[0] && !ret &&
  625. (a->c.width != current_crop.c.width ||
  626. a->c.height != current_crop.c.height)) {
  627. dev_err(&icd->dev,
  628. "S_CROP denied: queue initialised and sizes differ\n");
  629. ret = -EBUSY;
  630. } else {
  631. ret = ici->ops->set_crop(icd, a);
  632. }
  633. mutex_unlock(&icf->vb_vidq.vb_lock);
  634. return ret;
  635. }
  636. static int soc_camera_g_parm(struct file *file, void *fh,
  637. struct v4l2_streamparm *a)
  638. {
  639. struct soc_camera_file *icf = file->private_data;
  640. struct soc_camera_device *icd = icf->icd;
  641. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  642. if (ici->ops->get_parm)
  643. return ici->ops->get_parm(icd, a);
  644. return -ENOIOCTLCMD;
  645. }
  646. static int soc_camera_s_parm(struct file *file, void *fh,
  647. struct v4l2_streamparm *a)
  648. {
  649. struct soc_camera_file *icf = file->private_data;
  650. struct soc_camera_device *icd = icf->icd;
  651. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  652. if (ici->ops->set_parm)
  653. return ici->ops->set_parm(icd, a);
  654. return -ENOIOCTLCMD;
  655. }
  656. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  657. struct v4l2_dbg_chip_ident *id)
  658. {
  659. struct soc_camera_file *icf = file->private_data;
  660. struct soc_camera_device *icd = icf->icd;
  661. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  662. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  663. }
  664. #ifdef CONFIG_VIDEO_ADV_DEBUG
  665. static int soc_camera_g_register(struct file *file, void *fh,
  666. struct v4l2_dbg_register *reg)
  667. {
  668. struct soc_camera_file *icf = file->private_data;
  669. struct soc_camera_device *icd = icf->icd;
  670. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  671. return v4l2_subdev_call(sd, core, g_register, reg);
  672. }
  673. static int soc_camera_s_register(struct file *file, void *fh,
  674. struct v4l2_dbg_register *reg)
  675. {
  676. struct soc_camera_file *icf = file->private_data;
  677. struct soc_camera_device *icd = icf->icd;
  678. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  679. return v4l2_subdev_call(sd, core, s_register, reg);
  680. }
  681. #endif
  682. /* So far this function cannot fail */
  683. static void scan_add_host(struct soc_camera_host *ici)
  684. {
  685. struct soc_camera_device *icd;
  686. mutex_lock(&list_lock);
  687. list_for_each_entry(icd, &devices, list) {
  688. if (icd->iface == ici->nr) {
  689. int ret;
  690. icd->dev.parent = ici->v4l2_dev.dev;
  691. dev_set_name(&icd->dev, "%u-%u", icd->iface,
  692. icd->devnum);
  693. ret = device_register(&icd->dev);
  694. if (ret < 0) {
  695. icd->dev.parent = NULL;
  696. dev_err(&icd->dev,
  697. "Cannot register device: %d\n", ret);
  698. }
  699. }
  700. }
  701. mutex_unlock(&list_lock);
  702. }
  703. #ifdef CONFIG_I2C_BOARDINFO
  704. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  705. struct soc_camera_link *icl)
  706. {
  707. struct i2c_client *client;
  708. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  709. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  710. struct v4l2_subdev *subdev;
  711. if (!adap) {
  712. dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
  713. icl->i2c_adapter_id);
  714. goto ei2cga;
  715. }
  716. icl->board_info->platform_data = icd;
  717. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  718. icl->module_name, icl->board_info, NULL);
  719. if (!subdev)
  720. goto ei2cnd;
  721. client = subdev->priv;
  722. /* Use to_i2c_client(dev) to recover the i2c client */
  723. dev_set_drvdata(&icd->dev, &client->dev);
  724. return 0;
  725. ei2cnd:
  726. i2c_put_adapter(adap);
  727. ei2cga:
  728. return -ENODEV;
  729. }
  730. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  731. {
  732. struct i2c_client *client =
  733. to_i2c_client(to_soc_camera_control(icd));
  734. dev_set_drvdata(&icd->dev, NULL);
  735. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  736. i2c_unregister_device(client);
  737. i2c_put_adapter(client->adapter);
  738. }
  739. #else
  740. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  741. #define soc_camera_free_i2c(icd) do {} while (0)
  742. #endif
  743. static int soc_camera_video_start(struct soc_camera_device *icd);
  744. static int video_dev_create(struct soc_camera_device *icd);
  745. /* Called during host-driver probe */
  746. static int soc_camera_probe(struct device *dev)
  747. {
  748. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  749. struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
  750. struct soc_camera_link *icl = to_soc_camera_link(icd);
  751. struct device *control = NULL;
  752. struct v4l2_subdev *sd;
  753. struct v4l2_mbus_framefmt mf;
  754. int ret;
  755. dev_info(dev, "Probing %s\n", dev_name(dev));
  756. if (icl->power) {
  757. ret = icl->power(icd->pdev, 1);
  758. if (ret < 0) {
  759. dev_err(dev,
  760. "Platform failed to power-on the camera.\n");
  761. goto epower;
  762. }
  763. }
  764. /* The camera could have been already on, try to reset */
  765. if (icl->reset)
  766. icl->reset(icd->pdev);
  767. ret = ici->ops->add(icd);
  768. if (ret < 0)
  769. goto eadd;
  770. /* Must have icd->vdev before registering the device */
  771. ret = video_dev_create(icd);
  772. if (ret < 0)
  773. goto evdc;
  774. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  775. if (icl->board_info) {
  776. ret = soc_camera_init_i2c(icd, icl);
  777. if (ret < 0)
  778. goto eadddev;
  779. } else if (!icl->add_device || !icl->del_device) {
  780. ret = -EINVAL;
  781. goto eadddev;
  782. } else {
  783. if (icl->module_name)
  784. ret = request_module(icl->module_name);
  785. ret = icl->add_device(icl, &icd->dev);
  786. if (ret < 0)
  787. goto eadddev;
  788. /*
  789. * FIXME: this is racy, have to use driver-binding notification,
  790. * when it is available
  791. */
  792. control = to_soc_camera_control(icd);
  793. if (!control || !control->driver || !dev_get_drvdata(control) ||
  794. !try_module_get(control->driver->owner)) {
  795. icl->del_device(icl);
  796. goto enodrv;
  797. }
  798. }
  799. /* At this point client .probe() should have run already */
  800. ret = soc_camera_init_user_formats(icd);
  801. if (ret < 0)
  802. goto eiufmt;
  803. icd->field = V4L2_FIELD_ANY;
  804. /* ..._video_start() will create a device node, so we have to protect */
  805. mutex_lock(&icd->video_lock);
  806. ret = soc_camera_video_start(icd);
  807. if (ret < 0)
  808. goto evidstart;
  809. /* Try to improve our guess of a reasonable window format */
  810. sd = soc_camera_to_subdev(icd);
  811. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  812. icd->user_width = mf.width;
  813. icd->user_height = mf.height;
  814. icd->colorspace = mf.colorspace;
  815. icd->field = mf.field;
  816. }
  817. /* Do we have to sysfs_remove_link() before device_unregister()? */
  818. if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  819. "control"))
  820. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  821. ici->ops->remove(icd);
  822. if (icl->power)
  823. icl->power(icd->pdev, 0);
  824. mutex_unlock(&icd->video_lock);
  825. return 0;
  826. evidstart:
  827. mutex_unlock(&icd->video_lock);
  828. soc_camera_free_user_formats(icd);
  829. eiufmt:
  830. if (icl->board_info) {
  831. soc_camera_free_i2c(icd);
  832. } else {
  833. icl->del_device(icl);
  834. module_put(control->driver->owner);
  835. }
  836. enodrv:
  837. eadddev:
  838. video_device_release(icd->vdev);
  839. evdc:
  840. ici->ops->remove(icd);
  841. eadd:
  842. if (icl->power)
  843. icl->power(icd->pdev, 0);
  844. epower:
  845. return ret;
  846. }
  847. /*
  848. * This is called on device_unregister, which only means we have to disconnect
  849. * from the host, but not remove ourselves from the device list
  850. */
  851. static int soc_camera_remove(struct device *dev)
  852. {
  853. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  854. struct soc_camera_link *icl = to_soc_camera_link(icd);
  855. struct video_device *vdev = icd->vdev;
  856. BUG_ON(!dev->parent);
  857. if (vdev) {
  858. mutex_lock(&icd->video_lock);
  859. video_unregister_device(vdev);
  860. icd->vdev = NULL;
  861. mutex_unlock(&icd->video_lock);
  862. }
  863. if (icl->board_info) {
  864. soc_camera_free_i2c(icd);
  865. } else {
  866. struct device_driver *drv = to_soc_camera_control(icd) ?
  867. to_soc_camera_control(icd)->driver : NULL;
  868. if (drv) {
  869. icl->del_device(icl);
  870. module_put(drv->owner);
  871. }
  872. }
  873. soc_camera_free_user_formats(icd);
  874. return 0;
  875. }
  876. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  877. {
  878. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  879. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  880. int ret = 0;
  881. if (ici->ops->suspend)
  882. ret = ici->ops->suspend(icd, state);
  883. return ret;
  884. }
  885. static int soc_camera_resume(struct device *dev)
  886. {
  887. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  888. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  889. int ret = 0;
  890. if (ici->ops->resume)
  891. ret = ici->ops->resume(icd);
  892. return ret;
  893. }
  894. struct bus_type soc_camera_bus_type = {
  895. .name = "soc-camera",
  896. .probe = soc_camera_probe,
  897. .remove = soc_camera_remove,
  898. .suspend = soc_camera_suspend,
  899. .resume = soc_camera_resume,
  900. };
  901. EXPORT_SYMBOL_GPL(soc_camera_bus_type);
  902. static struct device_driver ic_drv = {
  903. .name = "camera",
  904. .bus = &soc_camera_bus_type,
  905. .owner = THIS_MODULE,
  906. };
  907. static void dummy_release(struct device *dev)
  908. {
  909. }
  910. static int default_cropcap(struct soc_camera_device *icd,
  911. struct v4l2_cropcap *a)
  912. {
  913. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  914. return v4l2_subdev_call(sd, video, cropcap, a);
  915. }
  916. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  917. {
  918. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  919. return v4l2_subdev_call(sd, video, g_crop, a);
  920. }
  921. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  922. {
  923. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  924. return v4l2_subdev_call(sd, video, s_crop, a);
  925. }
  926. static void soc_camera_device_init(struct device *dev, void *pdata)
  927. {
  928. dev->platform_data = pdata;
  929. dev->bus = &soc_camera_bus_type;
  930. dev->release = dummy_release;
  931. }
  932. int soc_camera_host_register(struct soc_camera_host *ici)
  933. {
  934. struct soc_camera_host *ix;
  935. int ret;
  936. if (!ici || !ici->ops ||
  937. !ici->ops->try_fmt ||
  938. !ici->ops->set_fmt ||
  939. !ici->ops->set_bus_param ||
  940. !ici->ops->querycap ||
  941. !ici->ops->init_videobuf ||
  942. !ici->ops->reqbufs ||
  943. !ici->ops->add ||
  944. !ici->ops->remove ||
  945. !ici->ops->poll ||
  946. !ici->v4l2_dev.dev)
  947. return -EINVAL;
  948. if (!ici->ops->set_crop)
  949. ici->ops->set_crop = default_s_crop;
  950. if (!ici->ops->get_crop)
  951. ici->ops->get_crop = default_g_crop;
  952. if (!ici->ops->cropcap)
  953. ici->ops->cropcap = default_cropcap;
  954. mutex_lock(&list_lock);
  955. list_for_each_entry(ix, &hosts, list) {
  956. if (ix->nr == ici->nr) {
  957. ret = -EBUSY;
  958. goto edevreg;
  959. }
  960. }
  961. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  962. if (ret < 0)
  963. goto edevreg;
  964. list_add_tail(&ici->list, &hosts);
  965. mutex_unlock(&list_lock);
  966. scan_add_host(ici);
  967. return 0;
  968. edevreg:
  969. mutex_unlock(&list_lock);
  970. return ret;
  971. }
  972. EXPORT_SYMBOL(soc_camera_host_register);
  973. /* Unregister all clients! */
  974. void soc_camera_host_unregister(struct soc_camera_host *ici)
  975. {
  976. struct soc_camera_device *icd;
  977. mutex_lock(&list_lock);
  978. list_del(&ici->list);
  979. list_for_each_entry(icd, &devices, list) {
  980. if (icd->iface == ici->nr) {
  981. void *pdata = icd->dev.platform_data;
  982. /* The bus->remove will be called */
  983. device_unregister(&icd->dev);
  984. /*
  985. * Not before device_unregister(), .remove
  986. * needs parent to call ici->ops->remove().
  987. * If the host module is loaded again, device_register()
  988. * would complain "already initialised," since 2.6.32
  989. * this is also needed to prevent use-after-free of the
  990. * device private data.
  991. */
  992. memset(&icd->dev, 0, sizeof(icd->dev));
  993. soc_camera_device_init(&icd->dev, pdata);
  994. }
  995. }
  996. mutex_unlock(&list_lock);
  997. v4l2_device_unregister(&ici->v4l2_dev);
  998. }
  999. EXPORT_SYMBOL(soc_camera_host_unregister);
  1000. /* Image capture device */
  1001. static int soc_camera_device_register(struct soc_camera_device *icd)
  1002. {
  1003. struct soc_camera_device *ix;
  1004. int num = -1, i;
  1005. for (i = 0; i < 256 && num < 0; i++) {
  1006. num = i;
  1007. /* Check if this index is available on this interface */
  1008. list_for_each_entry(ix, &devices, list) {
  1009. if (ix->iface == icd->iface && ix->devnum == i) {
  1010. num = -1;
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. if (num < 0)
  1016. /*
  1017. * ok, we have 256 cameras on this host...
  1018. * man, stay reasonable...
  1019. */
  1020. return -ENOMEM;
  1021. icd->devnum = num;
  1022. icd->use_count = 0;
  1023. icd->host_priv = NULL;
  1024. mutex_init(&icd->video_lock);
  1025. list_add_tail(&icd->list, &devices);
  1026. return 0;
  1027. }
  1028. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  1029. {
  1030. list_del(&icd->list);
  1031. }
  1032. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1033. .vidioc_querycap = soc_camera_querycap,
  1034. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1035. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1036. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1037. .vidioc_enum_input = soc_camera_enum_input,
  1038. .vidioc_g_input = soc_camera_g_input,
  1039. .vidioc_s_input = soc_camera_s_input,
  1040. .vidioc_s_std = soc_camera_s_std,
  1041. .vidioc_reqbufs = soc_camera_reqbufs,
  1042. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1043. .vidioc_querybuf = soc_camera_querybuf,
  1044. .vidioc_qbuf = soc_camera_qbuf,
  1045. .vidioc_dqbuf = soc_camera_dqbuf,
  1046. .vidioc_streamon = soc_camera_streamon,
  1047. .vidioc_streamoff = soc_camera_streamoff,
  1048. .vidioc_queryctrl = soc_camera_queryctrl,
  1049. .vidioc_g_ctrl = soc_camera_g_ctrl,
  1050. .vidioc_s_ctrl = soc_camera_s_ctrl,
  1051. .vidioc_cropcap = soc_camera_cropcap,
  1052. .vidioc_g_crop = soc_camera_g_crop,
  1053. .vidioc_s_crop = soc_camera_s_crop,
  1054. .vidioc_g_parm = soc_camera_g_parm,
  1055. .vidioc_s_parm = soc_camera_s_parm,
  1056. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1057. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1058. .vidioc_g_register = soc_camera_g_register,
  1059. .vidioc_s_register = soc_camera_s_register,
  1060. #endif
  1061. };
  1062. static int video_dev_create(struct soc_camera_device *icd)
  1063. {
  1064. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1065. struct video_device *vdev = video_device_alloc();
  1066. if (!vdev)
  1067. return -ENOMEM;
  1068. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1069. vdev->parent = &icd->dev;
  1070. vdev->current_norm = V4L2_STD_UNKNOWN;
  1071. vdev->fops = &soc_camera_fops;
  1072. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1073. vdev->release = video_device_release;
  1074. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1075. icd->vdev = vdev;
  1076. return 0;
  1077. }
  1078. /*
  1079. * Called from soc_camera_probe() above (with .video_lock held???)
  1080. */
  1081. static int soc_camera_video_start(struct soc_camera_device *icd)
  1082. {
  1083. struct device_type *type = icd->vdev->dev.type;
  1084. int ret;
  1085. if (!icd->dev.parent)
  1086. return -ENODEV;
  1087. if (!icd->ops ||
  1088. !icd->ops->query_bus_param ||
  1089. !icd->ops->set_bus_param)
  1090. return -EINVAL;
  1091. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1092. if (ret < 0) {
  1093. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1094. return ret;
  1095. }
  1096. /* Restore device type, possibly set by the subdevice driver */
  1097. icd->vdev->dev.type = type;
  1098. return 0;
  1099. }
  1100. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1101. {
  1102. struct soc_camera_link *icl = pdev->dev.platform_data;
  1103. struct soc_camera_device *icd;
  1104. int ret;
  1105. if (!icl)
  1106. return -EINVAL;
  1107. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1108. if (!icd)
  1109. return -ENOMEM;
  1110. icd->iface = icl->bus_id;
  1111. icd->pdev = &pdev->dev;
  1112. platform_set_drvdata(pdev, icd);
  1113. ret = soc_camera_device_register(icd);
  1114. if (ret < 0)
  1115. goto escdevreg;
  1116. soc_camera_device_init(&icd->dev, icl);
  1117. icd->user_width = DEFAULT_WIDTH;
  1118. icd->user_height = DEFAULT_HEIGHT;
  1119. return 0;
  1120. escdevreg:
  1121. kfree(icd);
  1122. return ret;
  1123. }
  1124. /*
  1125. * Only called on rmmod for each platform device, since they are not
  1126. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1127. * been unloaded already
  1128. */
  1129. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1130. {
  1131. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1132. if (!icd)
  1133. return -EINVAL;
  1134. soc_camera_device_unregister(icd);
  1135. kfree(icd);
  1136. return 0;
  1137. }
  1138. static struct platform_driver __refdata soc_camera_pdrv = {
  1139. .remove = __devexit_p(soc_camera_pdrv_remove),
  1140. .driver = {
  1141. .name = "soc-camera-pdrv",
  1142. .owner = THIS_MODULE,
  1143. },
  1144. };
  1145. static int __init soc_camera_init(void)
  1146. {
  1147. int ret = bus_register(&soc_camera_bus_type);
  1148. if (ret)
  1149. return ret;
  1150. ret = driver_register(&ic_drv);
  1151. if (ret)
  1152. goto edrvr;
  1153. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1154. if (ret)
  1155. goto epdr;
  1156. return 0;
  1157. epdr:
  1158. driver_unregister(&ic_drv);
  1159. edrvr:
  1160. bus_unregister(&soc_camera_bus_type);
  1161. return ret;
  1162. }
  1163. static void __exit soc_camera_exit(void)
  1164. {
  1165. platform_driver_unregister(&soc_camera_pdrv);
  1166. driver_unregister(&ic_drv);
  1167. bus_unregister(&soc_camera_bus_type);
  1168. }
  1169. module_init(soc_camera_init);
  1170. module_exit(soc_camera_exit);
  1171. MODULE_DESCRIPTION("Image capture bus driver");
  1172. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1173. MODULE_LICENSE("GPL");
  1174. MODULE_ALIAS("platform:soc-camera-pdrv");