soc_camera.c 35 KB

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