soc_camera.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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,
  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. .pixelformat = icd->current_fmt->fourcc,
  305. .colorspace = icd->current_fmt->colorspace,
  306. },
  307. };
  308. if (icl->power) {
  309. ret = icl->power(icd->pdev, 1);
  310. if (ret < 0)
  311. goto epower;
  312. }
  313. /* The camera could have been already on, try to reset */
  314. if (icl->reset)
  315. icl->reset(icd->pdev);
  316. ret = ici->ops->add(icd);
  317. if (ret < 0) {
  318. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  319. goto eiciadd;
  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. ici->ops->remove(icd);
  337. eiciadd:
  338. if (icl->power)
  339. icl->power(icd->pdev, 0);
  340. epower:
  341. icd->use_count--;
  342. mutex_unlock(&icd->video_lock);
  343. module_put(ici->ops->owner);
  344. emgi:
  345. vfree(icf);
  346. return ret;
  347. }
  348. static int soc_camera_close(struct file *file)
  349. {
  350. struct soc_camera_file *icf = file->private_data;
  351. struct soc_camera_device *icd = icf->icd;
  352. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  353. mutex_lock(&icd->video_lock);
  354. icd->use_count--;
  355. if (!icd->use_count) {
  356. struct soc_camera_link *icl = to_soc_camera_link(icd);
  357. ici->ops->remove(icd);
  358. if (icl->power)
  359. icl->power(icd->pdev, 0);
  360. }
  361. mutex_unlock(&icd->video_lock);
  362. module_put(ici->ops->owner);
  363. vfree(icf);
  364. dev_dbg(&icd->dev, "camera device close\n");
  365. return 0;
  366. }
  367. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  368. size_t count, loff_t *ppos)
  369. {
  370. struct soc_camera_file *icf = file->private_data;
  371. struct soc_camera_device *icd = icf->icd;
  372. int err = -EINVAL;
  373. dev_err(&icd->dev, "camera device read not implemented\n");
  374. return err;
  375. }
  376. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  377. {
  378. struct soc_camera_file *icf = file->private_data;
  379. struct soc_camera_device *icd = icf->icd;
  380. int err;
  381. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  382. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  383. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  384. (unsigned long)vma->vm_start,
  385. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  386. err);
  387. return err;
  388. }
  389. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  390. {
  391. struct soc_camera_file *icf = file->private_data;
  392. struct soc_camera_device *icd = icf->icd;
  393. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  394. if (list_empty(&icf->vb_vidq.stream)) {
  395. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  396. return POLLERR;
  397. }
  398. return ici->ops->poll(file, pt);
  399. }
  400. static struct v4l2_file_operations soc_camera_fops = {
  401. .owner = THIS_MODULE,
  402. .open = soc_camera_open,
  403. .release = soc_camera_close,
  404. .ioctl = video_ioctl2,
  405. .read = soc_camera_read,
  406. .mmap = soc_camera_mmap,
  407. .poll = soc_camera_poll,
  408. };
  409. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  410. struct v4l2_format *f)
  411. {
  412. struct soc_camera_file *icf = file->private_data;
  413. struct soc_camera_device *icd = icf->icd;
  414. int ret;
  415. WARN_ON(priv != file->private_data);
  416. mutex_lock(&icf->vb_vidq.vb_lock);
  417. if (icf->vb_vidq.bufs[0]) {
  418. dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
  419. ret = -EBUSY;
  420. goto unlock;
  421. }
  422. ret = soc_camera_set_fmt(icf, f);
  423. unlock:
  424. mutex_unlock(&icf->vb_vidq.vb_lock);
  425. return ret;
  426. }
  427. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  428. struct v4l2_fmtdesc *f)
  429. {
  430. struct soc_camera_file *icf = file->private_data;
  431. struct soc_camera_device *icd = icf->icd;
  432. const struct soc_camera_data_format *format;
  433. WARN_ON(priv != file->private_data);
  434. if (f->index >= icd->num_user_formats)
  435. return -EINVAL;
  436. format = icd->user_formats[f->index].host_fmt;
  437. strlcpy(f->description, format->name, sizeof(f->description));
  438. f->pixelformat = format->fourcc;
  439. return 0;
  440. }
  441. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  442. struct v4l2_format *f)
  443. {
  444. struct soc_camera_file *icf = file->private_data;
  445. struct soc_camera_device *icd = icf->icd;
  446. struct v4l2_pix_format *pix = &f->fmt.pix;
  447. WARN_ON(priv != file->private_data);
  448. pix->width = icd->user_width;
  449. pix->height = icd->user_height;
  450. pix->field = icf->vb_vidq.field;
  451. pix->pixelformat = icd->current_fmt->fourcc;
  452. pix->bytesperline = pix->width *
  453. DIV_ROUND_UP(icd->current_fmt->depth, 8);
  454. pix->sizeimage = pix->height * pix->bytesperline;
  455. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  456. icd->current_fmt->fourcc);
  457. return 0;
  458. }
  459. static int soc_camera_querycap(struct file *file, void *priv,
  460. struct v4l2_capability *cap)
  461. {
  462. struct soc_camera_file *icf = file->private_data;
  463. struct soc_camera_device *icd = icf->icd;
  464. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  465. WARN_ON(priv != file->private_data);
  466. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  467. return ici->ops->querycap(ici, cap);
  468. }
  469. static int soc_camera_streamon(struct file *file, void *priv,
  470. enum v4l2_buf_type i)
  471. {
  472. struct soc_camera_file *icf = file->private_data;
  473. struct soc_camera_device *icd = icf->icd;
  474. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  475. int ret;
  476. WARN_ON(priv != file->private_data);
  477. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  478. return -EINVAL;
  479. mutex_lock(&icd->video_lock);
  480. v4l2_subdev_call(sd, video, s_stream, 1);
  481. /* This calls buf_queue from host driver's videobuf_queue_ops */
  482. ret = videobuf_streamon(&icf->vb_vidq);
  483. mutex_unlock(&icd->video_lock);
  484. return ret;
  485. }
  486. static int soc_camera_streamoff(struct file *file, void *priv,
  487. enum v4l2_buf_type i)
  488. {
  489. struct soc_camera_file *icf = file->private_data;
  490. struct soc_camera_device *icd = icf->icd;
  491. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  492. WARN_ON(priv != file->private_data);
  493. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  494. return -EINVAL;
  495. mutex_lock(&icd->video_lock);
  496. /* This calls buf_release from host driver's videobuf_queue_ops for all
  497. * remaining buffers. When the last buffer is freed, stop capture */
  498. videobuf_streamoff(&icf->vb_vidq);
  499. v4l2_subdev_call(sd, video, s_stream, 0);
  500. mutex_unlock(&icd->video_lock);
  501. return 0;
  502. }
  503. static int soc_camera_queryctrl(struct file *file, void *priv,
  504. struct v4l2_queryctrl *qc)
  505. {
  506. struct soc_camera_file *icf = file->private_data;
  507. struct soc_camera_device *icd = icf->icd;
  508. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  509. int i;
  510. WARN_ON(priv != file->private_data);
  511. if (!qc->id)
  512. return -EINVAL;
  513. /* First check host controls */
  514. for (i = 0; i < ici->ops->num_controls; i++)
  515. if (qc->id == ici->ops->controls[i].id) {
  516. memcpy(qc, &(ici->ops->controls[i]),
  517. sizeof(*qc));
  518. return 0;
  519. }
  520. /* Then device controls */
  521. for (i = 0; i < icd->ops->num_controls; i++)
  522. if (qc->id == icd->ops->controls[i].id) {
  523. memcpy(qc, &(icd->ops->controls[i]),
  524. sizeof(*qc));
  525. return 0;
  526. }
  527. return -EINVAL;
  528. }
  529. static int soc_camera_g_ctrl(struct file *file, void *priv,
  530. struct v4l2_control *ctrl)
  531. {
  532. struct soc_camera_file *icf = file->private_data;
  533. struct soc_camera_device *icd = icf->icd;
  534. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  535. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  536. int ret;
  537. WARN_ON(priv != file->private_data);
  538. if (ici->ops->get_ctrl) {
  539. ret = ici->ops->get_ctrl(icd, ctrl);
  540. if (ret != -ENOIOCTLCMD)
  541. return ret;
  542. }
  543. return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
  544. }
  545. static int soc_camera_s_ctrl(struct file *file, void *priv,
  546. struct v4l2_control *ctrl)
  547. {
  548. struct soc_camera_file *icf = file->private_data;
  549. struct soc_camera_device *icd = icf->icd;
  550. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  551. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  552. int ret;
  553. WARN_ON(priv != file->private_data);
  554. if (ici->ops->set_ctrl) {
  555. ret = ici->ops->set_ctrl(icd, ctrl);
  556. if (ret != -ENOIOCTLCMD)
  557. return ret;
  558. }
  559. return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
  560. }
  561. static int soc_camera_cropcap(struct file *file, void *fh,
  562. struct v4l2_cropcap *a)
  563. {
  564. struct soc_camera_file *icf = file->private_data;
  565. struct soc_camera_device *icd = icf->icd;
  566. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  567. return ici->ops->cropcap(icd, a);
  568. }
  569. static int soc_camera_g_crop(struct file *file, void *fh,
  570. struct v4l2_crop *a)
  571. {
  572. struct soc_camera_file *icf = file->private_data;
  573. struct soc_camera_device *icd = icf->icd;
  574. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  575. int ret;
  576. mutex_lock(&icf->vb_vidq.vb_lock);
  577. ret = ici->ops->get_crop(icd, a);
  578. mutex_unlock(&icf->vb_vidq.vb_lock);
  579. return ret;
  580. }
  581. /*
  582. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  583. * argument with the actual geometry, instead, the user shall use G_CROP to
  584. * retrieve it. However, we expect camera host and client drivers to update
  585. * the argument, which we then use internally, but do not return to the user.
  586. */
  587. static int soc_camera_s_crop(struct file *file, void *fh,
  588. struct v4l2_crop *a)
  589. {
  590. struct soc_camera_file *icf = file->private_data;
  591. struct soc_camera_device *icd = icf->icd;
  592. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  593. struct v4l2_rect *rect = &a->c;
  594. struct v4l2_crop current_crop;
  595. int ret;
  596. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  597. return -EINVAL;
  598. dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
  599. rect->width, rect->height, rect->left, rect->top);
  600. /* Cropping is allowed during a running capture, guard consistency */
  601. mutex_lock(&icf->vb_vidq.vb_lock);
  602. /* If get_crop fails, we'll let host and / or client drivers decide */
  603. ret = ici->ops->get_crop(icd, &current_crop);
  604. /* Prohibit window size change with initialised buffers */
  605. if (icf->vb_vidq.bufs[0] && !ret &&
  606. (a->c.width != current_crop.c.width ||
  607. a->c.height != current_crop.c.height)) {
  608. dev_err(&icd->dev,
  609. "S_CROP denied: queue initialised and sizes differ\n");
  610. ret = -EBUSY;
  611. } else {
  612. ret = ici->ops->set_crop(icd, a);
  613. }
  614. mutex_unlock(&icf->vb_vidq.vb_lock);
  615. return ret;
  616. }
  617. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  618. struct v4l2_dbg_chip_ident *id)
  619. {
  620. struct soc_camera_file *icf = file->private_data;
  621. struct soc_camera_device *icd = icf->icd;
  622. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  623. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  624. }
  625. #ifdef CONFIG_VIDEO_ADV_DEBUG
  626. static int soc_camera_g_register(struct file *file, void *fh,
  627. struct v4l2_dbg_register *reg)
  628. {
  629. struct soc_camera_file *icf = file->private_data;
  630. struct soc_camera_device *icd = icf->icd;
  631. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  632. return v4l2_subdev_call(sd, core, g_register, reg);
  633. }
  634. static int soc_camera_s_register(struct file *file, void *fh,
  635. struct v4l2_dbg_register *reg)
  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, s_register, reg);
  641. }
  642. #endif
  643. /* So far this function cannot fail */
  644. static void scan_add_host(struct soc_camera_host *ici)
  645. {
  646. struct soc_camera_device *icd;
  647. mutex_lock(&list_lock);
  648. list_for_each_entry(icd, &devices, list) {
  649. if (icd->iface == ici->nr) {
  650. int ret;
  651. icd->dev.parent = ici->v4l2_dev.dev;
  652. dev_set_name(&icd->dev, "%u-%u", icd->iface,
  653. icd->devnum);
  654. ret = device_register(&icd->dev);
  655. if (ret < 0) {
  656. icd->dev.parent = NULL;
  657. dev_err(&icd->dev,
  658. "Cannot register device: %d\n", ret);
  659. }
  660. }
  661. }
  662. mutex_unlock(&list_lock);
  663. }
  664. #ifdef CONFIG_I2C_BOARDINFO
  665. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  666. struct soc_camera_link *icl)
  667. {
  668. struct i2c_client *client;
  669. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  670. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  671. struct v4l2_subdev *subdev;
  672. int ret;
  673. if (!adap) {
  674. ret = -ENODEV;
  675. dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
  676. icl->i2c_adapter_id);
  677. goto ei2cga;
  678. }
  679. icl->board_info->platform_data = icd;
  680. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  681. icl->module_name, icl->board_info, NULL);
  682. if (!subdev) {
  683. ret = -ENOMEM;
  684. goto ei2cnd;
  685. }
  686. client = subdev->priv;
  687. /* Use to_i2c_client(dev) to recover the i2c client */
  688. dev_set_drvdata(&icd->dev, &client->dev);
  689. return 0;
  690. ei2cnd:
  691. i2c_put_adapter(adap);
  692. ei2cga:
  693. return ret;
  694. }
  695. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  696. {
  697. struct i2c_client *client =
  698. to_i2c_client(to_soc_camera_control(icd));
  699. dev_set_drvdata(&icd->dev, NULL);
  700. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  701. i2c_unregister_device(client);
  702. i2c_put_adapter(client->adapter);
  703. }
  704. #else
  705. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  706. #define soc_camera_free_i2c(icd) do {} while (0)
  707. #endif
  708. static int soc_camera_video_start(struct soc_camera_device *icd);
  709. static int video_dev_create(struct soc_camera_device *icd);
  710. /* Called during host-driver probe */
  711. static int soc_camera_probe(struct device *dev)
  712. {
  713. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  714. struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
  715. struct soc_camera_link *icl = to_soc_camera_link(icd);
  716. struct device *control = NULL;
  717. struct v4l2_subdev *sd;
  718. struct v4l2_format f = {.type = V4L2_BUF_TYPE_VIDEO_CAPTURE};
  719. int ret;
  720. dev_info(dev, "Probing %s\n", dev_name(dev));
  721. if (icl->power) {
  722. ret = icl->power(icd->pdev, 1);
  723. if (ret < 0) {
  724. dev_err(dev,
  725. "Platform failed to power-on the camera.\n");
  726. goto epower;
  727. }
  728. }
  729. /* The camera could have been already on, try to reset */
  730. if (icl->reset)
  731. icl->reset(icd->pdev);
  732. ret = ici->ops->add(icd);
  733. if (ret < 0)
  734. goto eadd;
  735. /* Must have icd->vdev before registering the device */
  736. ret = video_dev_create(icd);
  737. if (ret < 0)
  738. goto evdc;
  739. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  740. if (icl->board_info) {
  741. ret = soc_camera_init_i2c(icd, icl);
  742. if (ret < 0)
  743. goto eadddev;
  744. } else if (!icl->add_device || !icl->del_device) {
  745. ret = -EINVAL;
  746. goto eadddev;
  747. } else {
  748. if (icl->module_name)
  749. ret = request_module(icl->module_name);
  750. ret = icl->add_device(icl, &icd->dev);
  751. if (ret < 0)
  752. goto eadddev;
  753. /*
  754. * FIXME: this is racy, have to use driver-binding notification,
  755. * when it is available
  756. */
  757. control = to_soc_camera_control(icd);
  758. if (!control || !control->driver || !dev_get_drvdata(control) ||
  759. !try_module_get(control->driver->owner)) {
  760. icl->del_device(icl);
  761. goto enodrv;
  762. }
  763. }
  764. /* At this point client .probe() should have run already */
  765. ret = soc_camera_init_user_formats(icd);
  766. if (ret < 0)
  767. goto eiufmt;
  768. icd->field = V4L2_FIELD_ANY;
  769. /* ..._video_start() will create a device node, so we have to protect */
  770. mutex_lock(&icd->video_lock);
  771. ret = soc_camera_video_start(icd);
  772. if (ret < 0)
  773. goto evidstart;
  774. /* Try to improve our guess of a reasonable window format */
  775. sd = soc_camera_to_subdev(icd);
  776. if (!v4l2_subdev_call(sd, video, g_fmt, &f)) {
  777. icd->user_width = f.fmt.pix.width;
  778. icd->user_height = f.fmt.pix.height;
  779. }
  780. /* Do we have to sysfs_remove_link() before device_unregister()? */
  781. if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  782. "control"))
  783. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  784. ici->ops->remove(icd);
  785. if (icl->power)
  786. icl->power(icd->pdev, 0);
  787. mutex_unlock(&icd->video_lock);
  788. return 0;
  789. evidstart:
  790. mutex_unlock(&icd->video_lock);
  791. soc_camera_free_user_formats(icd);
  792. eiufmt:
  793. if (icl->board_info) {
  794. soc_camera_free_i2c(icd);
  795. } else {
  796. icl->del_device(icl);
  797. module_put(control->driver->owner);
  798. }
  799. enodrv:
  800. eadddev:
  801. video_device_release(icd->vdev);
  802. evdc:
  803. ici->ops->remove(icd);
  804. eadd:
  805. if (icl->power)
  806. icl->power(icd->pdev, 0);
  807. epower:
  808. return ret;
  809. }
  810. /* This is called on device_unregister, which only means we have to disconnect
  811. * from the host, but not remove ourselves from the device list */
  812. static int soc_camera_remove(struct device *dev)
  813. {
  814. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  815. struct soc_camera_link *icl = to_soc_camera_link(icd);
  816. struct video_device *vdev = icd->vdev;
  817. BUG_ON(!dev->parent);
  818. if (vdev) {
  819. mutex_lock(&icd->video_lock);
  820. video_unregister_device(vdev);
  821. icd->vdev = NULL;
  822. mutex_unlock(&icd->video_lock);
  823. }
  824. if (icl->board_info) {
  825. soc_camera_free_i2c(icd);
  826. } else {
  827. struct device_driver *drv = to_soc_camera_control(icd) ?
  828. to_soc_camera_control(icd)->driver : NULL;
  829. if (drv) {
  830. icl->del_device(icl);
  831. module_put(drv->owner);
  832. }
  833. }
  834. soc_camera_free_user_formats(icd);
  835. return 0;
  836. }
  837. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  838. {
  839. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  840. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  841. int ret = 0;
  842. if (ici->ops->suspend)
  843. ret = ici->ops->suspend(icd, state);
  844. return ret;
  845. }
  846. static int soc_camera_resume(struct device *dev)
  847. {
  848. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  849. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  850. int ret = 0;
  851. if (ici->ops->resume)
  852. ret = ici->ops->resume(icd);
  853. return ret;
  854. }
  855. static struct bus_type soc_camera_bus_type = {
  856. .name = "soc-camera",
  857. .probe = soc_camera_probe,
  858. .remove = soc_camera_remove,
  859. .suspend = soc_camera_suspend,
  860. .resume = soc_camera_resume,
  861. };
  862. static struct device_driver ic_drv = {
  863. .name = "camera",
  864. .bus = &soc_camera_bus_type,
  865. .owner = THIS_MODULE,
  866. };
  867. static void dummy_release(struct device *dev)
  868. {
  869. }
  870. static int default_cropcap(struct soc_camera_device *icd,
  871. struct v4l2_cropcap *a)
  872. {
  873. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  874. return v4l2_subdev_call(sd, video, cropcap, a);
  875. }
  876. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  877. {
  878. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  879. return v4l2_subdev_call(sd, video, g_crop, a);
  880. }
  881. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  882. {
  883. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  884. return v4l2_subdev_call(sd, video, s_crop, a);
  885. }
  886. int soc_camera_host_register(struct soc_camera_host *ici)
  887. {
  888. struct soc_camera_host *ix;
  889. int ret;
  890. if (!ici || !ici->ops ||
  891. !ici->ops->try_fmt ||
  892. !ici->ops->set_fmt ||
  893. !ici->ops->set_bus_param ||
  894. !ici->ops->querycap ||
  895. !ici->ops->init_videobuf ||
  896. !ici->ops->reqbufs ||
  897. !ici->ops->add ||
  898. !ici->ops->remove ||
  899. !ici->ops->poll ||
  900. !ici->v4l2_dev.dev)
  901. return -EINVAL;
  902. if (!ici->ops->set_crop)
  903. ici->ops->set_crop = default_s_crop;
  904. if (!ici->ops->get_crop)
  905. ici->ops->get_crop = default_g_crop;
  906. if (!ici->ops->cropcap)
  907. ici->ops->cropcap = default_cropcap;
  908. mutex_lock(&list_lock);
  909. list_for_each_entry(ix, &hosts, list) {
  910. if (ix->nr == ici->nr) {
  911. ret = -EBUSY;
  912. goto edevreg;
  913. }
  914. }
  915. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  916. if (ret < 0)
  917. goto edevreg;
  918. list_add_tail(&ici->list, &hosts);
  919. mutex_unlock(&list_lock);
  920. scan_add_host(ici);
  921. return 0;
  922. edevreg:
  923. mutex_unlock(&list_lock);
  924. return ret;
  925. }
  926. EXPORT_SYMBOL(soc_camera_host_register);
  927. /* Unregister all clients! */
  928. void soc_camera_host_unregister(struct soc_camera_host *ici)
  929. {
  930. struct soc_camera_device *icd;
  931. mutex_lock(&list_lock);
  932. list_del(&ici->list);
  933. list_for_each_entry(icd, &devices, list) {
  934. if (icd->iface == ici->nr) {
  935. /* The bus->remove will be called */
  936. device_unregister(&icd->dev);
  937. /*
  938. * Not before device_unregister(), .remove
  939. * needs parent to call ici->ops->remove().
  940. * If the host module is loaded again, device_register()
  941. * would complain "already initialised," since 2.6.32
  942. * this is also needed to prevent use-after-free of the
  943. * device private data.
  944. */
  945. memset(&icd->dev, 0, sizeof(icd->dev));
  946. }
  947. }
  948. mutex_unlock(&list_lock);
  949. v4l2_device_unregister(&ici->v4l2_dev);
  950. }
  951. EXPORT_SYMBOL(soc_camera_host_unregister);
  952. /* Image capture device */
  953. static int soc_camera_device_register(struct soc_camera_device *icd)
  954. {
  955. struct soc_camera_device *ix;
  956. int num = -1, i;
  957. for (i = 0; i < 256 && num < 0; i++) {
  958. num = i;
  959. /* Check if this index is available on this interface */
  960. list_for_each_entry(ix, &devices, list) {
  961. if (ix->iface == icd->iface && ix->devnum == i) {
  962. num = -1;
  963. break;
  964. }
  965. }
  966. }
  967. if (num < 0)
  968. /* ok, we have 256 cameras on this host...
  969. * man, stay reasonable... */
  970. return -ENOMEM;
  971. icd->devnum = num;
  972. icd->dev.bus = &soc_camera_bus_type;
  973. icd->dev.release = dummy_release;
  974. icd->use_count = 0;
  975. icd->host_priv = NULL;
  976. mutex_init(&icd->video_lock);
  977. list_add_tail(&icd->list, &devices);
  978. return 0;
  979. }
  980. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  981. {
  982. list_del(&icd->list);
  983. }
  984. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  985. .vidioc_querycap = soc_camera_querycap,
  986. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  987. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  988. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  989. .vidioc_enum_input = soc_camera_enum_input,
  990. .vidioc_g_input = soc_camera_g_input,
  991. .vidioc_s_input = soc_camera_s_input,
  992. .vidioc_s_std = soc_camera_s_std,
  993. .vidioc_reqbufs = soc_camera_reqbufs,
  994. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  995. .vidioc_querybuf = soc_camera_querybuf,
  996. .vidioc_qbuf = soc_camera_qbuf,
  997. .vidioc_dqbuf = soc_camera_dqbuf,
  998. .vidioc_streamon = soc_camera_streamon,
  999. .vidioc_streamoff = soc_camera_streamoff,
  1000. .vidioc_queryctrl = soc_camera_queryctrl,
  1001. .vidioc_g_ctrl = soc_camera_g_ctrl,
  1002. .vidioc_s_ctrl = soc_camera_s_ctrl,
  1003. .vidioc_cropcap = soc_camera_cropcap,
  1004. .vidioc_g_crop = soc_camera_g_crop,
  1005. .vidioc_s_crop = soc_camera_s_crop,
  1006. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1007. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1008. .vidioc_g_register = soc_camera_g_register,
  1009. .vidioc_s_register = soc_camera_s_register,
  1010. #endif
  1011. };
  1012. static int video_dev_create(struct soc_camera_device *icd)
  1013. {
  1014. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1015. struct video_device *vdev = video_device_alloc();
  1016. if (!vdev)
  1017. return -ENOMEM;
  1018. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1019. vdev->parent = &icd->dev;
  1020. vdev->current_norm = V4L2_STD_UNKNOWN;
  1021. vdev->fops = &soc_camera_fops;
  1022. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1023. vdev->release = video_device_release;
  1024. vdev->minor = -1;
  1025. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1026. icd->vdev = vdev;
  1027. return 0;
  1028. }
  1029. /*
  1030. * Called from soc_camera_probe() above (with .video_lock held???)
  1031. */
  1032. static int soc_camera_video_start(struct soc_camera_device *icd)
  1033. {
  1034. int ret;
  1035. if (!icd->dev.parent)
  1036. return -ENODEV;
  1037. if (!icd->ops ||
  1038. !icd->ops->query_bus_param ||
  1039. !icd->ops->set_bus_param)
  1040. return -EINVAL;
  1041. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER,
  1042. icd->vdev->minor);
  1043. if (ret < 0) {
  1044. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1045. return ret;
  1046. }
  1047. return 0;
  1048. }
  1049. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1050. {
  1051. struct soc_camera_link *icl = pdev->dev.platform_data;
  1052. struct soc_camera_device *icd;
  1053. int ret;
  1054. if (!icl)
  1055. return -EINVAL;
  1056. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1057. if (!icd)
  1058. return -ENOMEM;
  1059. icd->iface = icl->bus_id;
  1060. icd->pdev = &pdev->dev;
  1061. platform_set_drvdata(pdev, icd);
  1062. icd->dev.platform_data = icl;
  1063. ret = soc_camera_device_register(icd);
  1064. if (ret < 0)
  1065. goto escdevreg;
  1066. icd->user_width = DEFAULT_WIDTH;
  1067. icd->user_height = DEFAULT_HEIGHT;
  1068. return 0;
  1069. escdevreg:
  1070. kfree(icd);
  1071. return ret;
  1072. }
  1073. /* Only called on rmmod for each platform device, since they are not
  1074. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1075. * been unloaded already */
  1076. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1077. {
  1078. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1079. if (!icd)
  1080. return -EINVAL;
  1081. soc_camera_device_unregister(icd);
  1082. kfree(icd);
  1083. return 0;
  1084. }
  1085. static struct platform_driver __refdata soc_camera_pdrv = {
  1086. .remove = __devexit_p(soc_camera_pdrv_remove),
  1087. .driver = {
  1088. .name = "soc-camera-pdrv",
  1089. .owner = THIS_MODULE,
  1090. },
  1091. };
  1092. static int __init soc_camera_init(void)
  1093. {
  1094. int ret = bus_register(&soc_camera_bus_type);
  1095. if (ret)
  1096. return ret;
  1097. ret = driver_register(&ic_drv);
  1098. if (ret)
  1099. goto edrvr;
  1100. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1101. if (ret)
  1102. goto epdr;
  1103. return 0;
  1104. epdr:
  1105. driver_unregister(&ic_drv);
  1106. edrvr:
  1107. bus_unregister(&soc_camera_bus_type);
  1108. return ret;
  1109. }
  1110. static void __exit soc_camera_exit(void)
  1111. {
  1112. platform_driver_unregister(&soc_camera_pdrv);
  1113. driver_unregister(&ic_drv);
  1114. bus_unregister(&soc_camera_bus_type);
  1115. }
  1116. module_init(soc_camera_init);
  1117. module_exit(soc_camera_exit);
  1118. MODULE_DESCRIPTION("Image capture bus driver");
  1119. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1120. MODULE_LICENSE("GPL");
  1121. MODULE_ALIAS("platform:soc-camera-pdrv");