soc_camera.c 35 KB

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