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