soc_camera.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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/module.h>
  19. #include <linux/init.h>
  20. #include <linux/device.h>
  21. #include <linux/list.h>
  22. #include <linux/err.h>
  23. #include <linux/mutex.h>
  24. #include <linux/vmalloc.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <media/v4l2-dev.h>
  28. #include <media/videobuf-core.h>
  29. #include <media/soc_camera.h>
  30. static LIST_HEAD(hosts);
  31. static LIST_HEAD(devices);
  32. static DEFINE_MUTEX(list_lock);
  33. static DEFINE_MUTEX(video_lock);
  34. const static struct soc_camera_data_format*
  35. format_by_fourcc(struct soc_camera_device *icd, unsigned int fourcc)
  36. {
  37. unsigned int i;
  38. for (i = 0; i < icd->num_formats; i++)
  39. if (icd->formats[i].fourcc == fourcc)
  40. return icd->formats + i;
  41. return NULL;
  42. }
  43. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  44. struct v4l2_format *f)
  45. {
  46. struct soc_camera_file *icf = file->private_data;
  47. struct soc_camera_device *icd = icf->icd;
  48. struct soc_camera_host *ici =
  49. to_soc_camera_host(icd->dev.parent);
  50. enum v4l2_field field;
  51. const struct soc_camera_data_format *fmt;
  52. int ret;
  53. WARN_ON(priv != file->private_data);
  54. fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
  55. if (!fmt) {
  56. dev_dbg(&icd->dev, "invalid format 0x%08x\n",
  57. f->fmt.pix.pixelformat);
  58. return -EINVAL;
  59. }
  60. dev_dbg(&icd->dev, "fmt: 0x%08x\n", fmt->fourcc);
  61. field = f->fmt.pix.field;
  62. if (field == V4L2_FIELD_ANY) {
  63. field = V4L2_FIELD_NONE;
  64. } else if (V4L2_FIELD_NONE != field) {
  65. dev_err(&icd->dev, "Field type invalid.\n");
  66. return -EINVAL;
  67. }
  68. /* test physical bus parameters */
  69. ret = ici->ops->try_bus_param(icd, f->fmt.pix.pixelformat);
  70. if (ret)
  71. return ret;
  72. /* limit format to hardware capabilities */
  73. ret = ici->ops->try_fmt_cap(icd, f);
  74. /* calculate missing fields */
  75. f->fmt.pix.field = field;
  76. f->fmt.pix.bytesperline =
  77. (f->fmt.pix.width * fmt->depth) >> 3;
  78. f->fmt.pix.sizeimage =
  79. f->fmt.pix.height * f->fmt.pix.bytesperline;
  80. return ret;
  81. }
  82. static int soc_camera_enum_input(struct file *file, void *priv,
  83. struct v4l2_input *inp)
  84. {
  85. if (inp->index != 0)
  86. return -EINVAL;
  87. inp->type = V4L2_INPUT_TYPE_CAMERA;
  88. inp->std = V4L2_STD_UNKNOWN;
  89. strcpy(inp->name, "Camera");
  90. return 0;
  91. }
  92. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  93. {
  94. *i = 0;
  95. return 0;
  96. }
  97. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  98. {
  99. if (i > 0)
  100. return -EINVAL;
  101. return 0;
  102. }
  103. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
  104. {
  105. return 0;
  106. }
  107. static int soc_camera_reqbufs(struct file *file, void *priv,
  108. struct v4l2_requestbuffers *p)
  109. {
  110. int ret;
  111. struct soc_camera_file *icf = file->private_data;
  112. struct soc_camera_device *icd = icf->icd;
  113. struct soc_camera_host *ici =
  114. to_soc_camera_host(icd->dev.parent);
  115. WARN_ON(priv != file->private_data);
  116. dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
  117. ret = videobuf_reqbufs(&icf->vb_vidq, p);
  118. if (ret < 0)
  119. return ret;
  120. return ici->ops->reqbufs(icf, p);
  121. }
  122. static int soc_camera_querybuf(struct file *file, void *priv,
  123. struct v4l2_buffer *p)
  124. {
  125. struct soc_camera_file *icf = file->private_data;
  126. WARN_ON(priv != file->private_data);
  127. return videobuf_querybuf(&icf->vb_vidq, p);
  128. }
  129. static int soc_camera_qbuf(struct file *file, void *priv,
  130. struct v4l2_buffer *p)
  131. {
  132. struct soc_camera_file *icf = file->private_data;
  133. WARN_ON(priv != file->private_data);
  134. return videobuf_qbuf(&icf->vb_vidq, p);
  135. }
  136. static int soc_camera_dqbuf(struct file *file, void *priv,
  137. struct v4l2_buffer *p)
  138. {
  139. struct soc_camera_file *icf = file->private_data;
  140. WARN_ON(priv != file->private_data);
  141. return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
  142. }
  143. static int soc_camera_open(struct inode *inode, struct file *file)
  144. {
  145. struct video_device *vdev;
  146. struct soc_camera_device *icd;
  147. struct soc_camera_host *ici;
  148. struct soc_camera_file *icf;
  149. int ret;
  150. icf = vmalloc(sizeof(*icf));
  151. if (!icf)
  152. return -ENOMEM;
  153. /* Protect against icd->remove() until we module_get() both drivers. */
  154. mutex_lock(&video_lock);
  155. vdev = video_devdata(file);
  156. icd = container_of(vdev->parent, struct soc_camera_device, dev);
  157. ici = to_soc_camera_host(icd->dev.parent);
  158. if (!try_module_get(icd->ops->owner)) {
  159. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  160. ret = -EINVAL;
  161. goto emgd;
  162. }
  163. if (!try_module_get(ici->ops->owner)) {
  164. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  165. ret = -EINVAL;
  166. goto emgi;
  167. }
  168. icf->icd = icd;
  169. icd->use_count++;
  170. /* Now we really have to activate the camera */
  171. if (icd->use_count == 1) {
  172. ret = ici->ops->add(icd);
  173. if (ret < 0) {
  174. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  175. icd->use_count--;
  176. goto eiciadd;
  177. }
  178. }
  179. mutex_unlock(&video_lock);
  180. file->private_data = icf;
  181. dev_dbg(&icd->dev, "camera device open\n");
  182. ici->ops->init_videobuf(&icf->vb_vidq, icd);
  183. return 0;
  184. /* All errors are entered with the video_lock held */
  185. eiciadd:
  186. module_put(ici->ops->owner);
  187. emgi:
  188. module_put(icd->ops->owner);
  189. emgd:
  190. mutex_unlock(&video_lock);
  191. vfree(icf);
  192. return ret;
  193. }
  194. static int soc_camera_close(struct inode *inode, struct file *file)
  195. {
  196. struct soc_camera_file *icf = file->private_data;
  197. struct soc_camera_device *icd = icf->icd;
  198. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  199. struct video_device *vdev = icd->vdev;
  200. mutex_lock(&video_lock);
  201. icd->use_count--;
  202. if (!icd->use_count)
  203. ici->ops->remove(icd);
  204. module_put(icd->ops->owner);
  205. module_put(ici->ops->owner);
  206. mutex_unlock(&video_lock);
  207. vfree(icf);
  208. dev_dbg(vdev->parent, "camera device close\n");
  209. return 0;
  210. }
  211. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  212. size_t count, loff_t *ppos)
  213. {
  214. struct soc_camera_file *icf = file->private_data;
  215. struct soc_camera_device *icd = icf->icd;
  216. struct video_device *vdev = icd->vdev;
  217. int err = -EINVAL;
  218. dev_err(vdev->parent, "camera device read not implemented\n");
  219. return err;
  220. }
  221. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  222. {
  223. struct soc_camera_file *icf = file->private_data;
  224. struct soc_camera_device *icd = icf->icd;
  225. int err;
  226. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  227. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  228. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  229. (unsigned long)vma->vm_start,
  230. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  231. err);
  232. return err;
  233. }
  234. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  235. {
  236. struct soc_camera_file *icf = file->private_data;
  237. struct soc_camera_device *icd = icf->icd;
  238. struct soc_camera_host *ici =
  239. to_soc_camera_host(icd->dev.parent);
  240. if (list_empty(&icf->vb_vidq.stream)) {
  241. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  242. return POLLERR;
  243. }
  244. return ici->ops->poll(file, pt);
  245. }
  246. static struct file_operations soc_camera_fops = {
  247. .owner = THIS_MODULE,
  248. .open = soc_camera_open,
  249. .release = soc_camera_close,
  250. .ioctl = video_ioctl2,
  251. .read = soc_camera_read,
  252. .mmap = soc_camera_mmap,
  253. .poll = soc_camera_poll,
  254. .llseek = no_llseek,
  255. };
  256. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  257. struct v4l2_format *f)
  258. {
  259. struct soc_camera_file *icf = file->private_data;
  260. struct soc_camera_device *icd = icf->icd;
  261. struct soc_camera_host *ici =
  262. to_soc_camera_host(icd->dev.parent);
  263. int ret;
  264. struct v4l2_rect rect;
  265. const static struct soc_camera_data_format *data_fmt;
  266. WARN_ON(priv != file->private_data);
  267. data_fmt = format_by_fourcc(icd, f->fmt.pix.pixelformat);
  268. if (!data_fmt)
  269. return -EINVAL;
  270. /* buswidth may be further adjusted by the ici */
  271. icd->buswidth = data_fmt->depth;
  272. ret = soc_camera_try_fmt_vid_cap(file, icf, f);
  273. if (ret < 0)
  274. return ret;
  275. rect.left = icd->x_current;
  276. rect.top = icd->y_current;
  277. rect.width = f->fmt.pix.width;
  278. rect.height = f->fmt.pix.height;
  279. ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
  280. if (ret < 0)
  281. return ret;
  282. icd->current_fmt = data_fmt;
  283. icd->width = rect.width;
  284. icd->height = rect.height;
  285. icf->vb_vidq.field = f->fmt.pix.field;
  286. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
  287. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  288. f->type);
  289. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  290. icd->width, icd->height);
  291. /* set physical bus parameters */
  292. return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
  293. }
  294. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  295. struct v4l2_fmtdesc *f)
  296. {
  297. struct soc_camera_file *icf = file->private_data;
  298. struct soc_camera_device *icd = icf->icd;
  299. const struct soc_camera_data_format *format;
  300. WARN_ON(priv != file->private_data);
  301. if (f->index >= icd->num_formats)
  302. return -EINVAL;
  303. format = &icd->formats[f->index];
  304. strlcpy(f->description, format->name, sizeof(f->description));
  305. f->pixelformat = format->fourcc;
  306. return 0;
  307. }
  308. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  309. struct v4l2_format *f)
  310. {
  311. struct soc_camera_file *icf = file->private_data;
  312. struct soc_camera_device *icd = icf->icd;
  313. WARN_ON(priv != file->private_data);
  314. f->fmt.pix.width = icd->width;
  315. f->fmt.pix.height = icd->height;
  316. f->fmt.pix.field = icf->vb_vidq.field;
  317. f->fmt.pix.pixelformat = icd->current_fmt->fourcc;
  318. f->fmt.pix.bytesperline =
  319. (f->fmt.pix.width * icd->current_fmt->depth) >> 3;
  320. f->fmt.pix.sizeimage =
  321. f->fmt.pix.height * f->fmt.pix.bytesperline;
  322. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  323. icd->current_fmt->fourcc);
  324. return 0;
  325. }
  326. static int soc_camera_querycap(struct file *file, void *priv,
  327. struct v4l2_capability *cap)
  328. {
  329. struct soc_camera_file *icf = file->private_data;
  330. struct soc_camera_device *icd = icf->icd;
  331. struct soc_camera_host *ici =
  332. to_soc_camera_host(icd->dev.parent);
  333. WARN_ON(priv != file->private_data);
  334. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  335. return ici->ops->querycap(ici, cap);
  336. }
  337. static int soc_camera_streamon(struct file *file, void *priv,
  338. enum v4l2_buf_type i)
  339. {
  340. struct soc_camera_file *icf = file->private_data;
  341. struct soc_camera_device *icd = icf->icd;
  342. WARN_ON(priv != file->private_data);
  343. dev_dbg(&icd->dev, "%s\n", __func__);
  344. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  345. return -EINVAL;
  346. icd->ops->start_capture(icd);
  347. /* This calls buf_queue from host driver's videobuf_queue_ops */
  348. return videobuf_streamon(&icf->vb_vidq);
  349. }
  350. static int soc_camera_streamoff(struct file *file, void *priv,
  351. enum v4l2_buf_type i)
  352. {
  353. struct soc_camera_file *icf = file->private_data;
  354. struct soc_camera_device *icd = icf->icd;
  355. WARN_ON(priv != file->private_data);
  356. dev_dbg(&icd->dev, "%s\n", __func__);
  357. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  358. return -EINVAL;
  359. /* This calls buf_release from host driver's videobuf_queue_ops for all
  360. * remaining buffers. When the last buffer is freed, stop capture */
  361. videobuf_streamoff(&icf->vb_vidq);
  362. icd->ops->stop_capture(icd);
  363. return 0;
  364. }
  365. static int soc_camera_queryctrl(struct file *file, void *priv,
  366. struct v4l2_queryctrl *qc)
  367. {
  368. struct soc_camera_file *icf = file->private_data;
  369. struct soc_camera_device *icd = icf->icd;
  370. int i;
  371. WARN_ON(priv != file->private_data);
  372. if (!qc->id)
  373. return -EINVAL;
  374. for (i = 0; i < icd->ops->num_controls; i++)
  375. if (qc->id == icd->ops->controls[i].id) {
  376. memcpy(qc, &(icd->ops->controls[i]),
  377. sizeof(*qc));
  378. return 0;
  379. }
  380. return -EINVAL;
  381. }
  382. static int soc_camera_g_ctrl(struct file *file, void *priv,
  383. struct v4l2_control *ctrl)
  384. {
  385. struct soc_camera_file *icf = file->private_data;
  386. struct soc_camera_device *icd = icf->icd;
  387. WARN_ON(priv != file->private_data);
  388. switch (ctrl->id) {
  389. case V4L2_CID_GAIN:
  390. if (icd->gain == (unsigned short)~0)
  391. return -EINVAL;
  392. ctrl->value = icd->gain;
  393. return 0;
  394. case V4L2_CID_EXPOSURE:
  395. if (icd->exposure == (unsigned short)~0)
  396. return -EINVAL;
  397. ctrl->value = icd->exposure;
  398. return 0;
  399. }
  400. if (icd->ops->get_control)
  401. return icd->ops->get_control(icd, ctrl);
  402. return -EINVAL;
  403. }
  404. static int soc_camera_s_ctrl(struct file *file, void *priv,
  405. struct v4l2_control *ctrl)
  406. {
  407. struct soc_camera_file *icf = file->private_data;
  408. struct soc_camera_device *icd = icf->icd;
  409. WARN_ON(priv != file->private_data);
  410. if (icd->ops->set_control)
  411. return icd->ops->set_control(icd, ctrl);
  412. return -EINVAL;
  413. }
  414. static int soc_camera_cropcap(struct file *file, void *fh,
  415. struct v4l2_cropcap *a)
  416. {
  417. struct soc_camera_file *icf = file->private_data;
  418. struct soc_camera_device *icd = icf->icd;
  419. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  420. a->bounds.left = icd->x_min;
  421. a->bounds.top = icd->y_min;
  422. a->bounds.width = icd->width_max;
  423. a->bounds.height = icd->height_max;
  424. a->defrect.left = icd->x_min;
  425. a->defrect.top = icd->y_min;
  426. a->defrect.width = 640;
  427. a->defrect.height = 480;
  428. a->pixelaspect.numerator = 1;
  429. a->pixelaspect.denominator = 1;
  430. return 0;
  431. }
  432. static int soc_camera_g_crop(struct file *file, void *fh,
  433. struct v4l2_crop *a)
  434. {
  435. struct soc_camera_file *icf = file->private_data;
  436. struct soc_camera_device *icd = icf->icd;
  437. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  438. a->c.left = icd->x_current;
  439. a->c.top = icd->y_current;
  440. a->c.width = icd->width;
  441. a->c.height = icd->height;
  442. return 0;
  443. }
  444. static int soc_camera_s_crop(struct file *file, void *fh,
  445. struct v4l2_crop *a)
  446. {
  447. struct soc_camera_file *icf = file->private_data;
  448. struct soc_camera_device *icd = icf->icd;
  449. struct soc_camera_host *ici =
  450. to_soc_camera_host(icd->dev.parent);
  451. int ret;
  452. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  453. return -EINVAL;
  454. ret = ici->ops->set_fmt_cap(icd, 0, &a->c);
  455. if (!ret) {
  456. icd->width = a->c.width;
  457. icd->height = a->c.height;
  458. icd->x_current = a->c.left;
  459. icd->y_current = a->c.top;
  460. }
  461. return ret;
  462. }
  463. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  464. struct v4l2_chip_ident *id)
  465. {
  466. struct soc_camera_file *icf = file->private_data;
  467. struct soc_camera_device *icd = icf->icd;
  468. if (!icd->ops->get_chip_id)
  469. return -EINVAL;
  470. return icd->ops->get_chip_id(icd, id);
  471. }
  472. #ifdef CONFIG_VIDEO_ADV_DEBUG
  473. static int soc_camera_g_register(struct file *file, void *fh,
  474. struct v4l2_register *reg)
  475. {
  476. struct soc_camera_file *icf = file->private_data;
  477. struct soc_camera_device *icd = icf->icd;
  478. if (!icd->ops->get_register)
  479. return -EINVAL;
  480. return icd->ops->get_register(icd, reg);
  481. }
  482. static int soc_camera_s_register(struct file *file, void *fh,
  483. struct v4l2_register *reg)
  484. {
  485. struct soc_camera_file *icf = file->private_data;
  486. struct soc_camera_device *icd = icf->icd;
  487. if (!icd->ops->set_register)
  488. return -EINVAL;
  489. return icd->ops->set_register(icd, reg);
  490. }
  491. #endif
  492. static int device_register_link(struct soc_camera_device *icd)
  493. {
  494. int ret = device_register(&icd->dev);
  495. if (ret < 0) {
  496. /* Prevent calling device_unregister() */
  497. icd->dev.parent = NULL;
  498. dev_err(&icd->dev, "Cannot register device: %d\n", ret);
  499. /* Even if probe() was unsuccessful for all registered drivers,
  500. * device_register() returns 0, and we add the link, just to
  501. * document this camera's control device */
  502. } else if (icd->control)
  503. /* Have to sysfs_remove_link() before device_unregister()? */
  504. if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
  505. "control"))
  506. dev_warn(&icd->dev,
  507. "Failed creating the control symlink\n");
  508. return ret;
  509. }
  510. /* So far this function cannot fail */
  511. static void scan_add_host(struct soc_camera_host *ici)
  512. {
  513. struct soc_camera_device *icd;
  514. mutex_lock(&list_lock);
  515. list_for_each_entry(icd, &devices, list) {
  516. if (icd->iface == ici->nr) {
  517. icd->dev.parent = &ici->dev;
  518. device_register_link(icd);
  519. }
  520. }
  521. mutex_unlock(&list_lock);
  522. }
  523. /* return: 0 if no match found or a match found and
  524. * device_register() successful, error code otherwise */
  525. static int scan_add_device(struct soc_camera_device *icd)
  526. {
  527. struct soc_camera_host *ici;
  528. int ret = 0;
  529. mutex_lock(&list_lock);
  530. list_add_tail(&icd->list, &devices);
  531. /* Watch out for class_for_each_device / class_find_device API by
  532. * Dave Young <hidave.darkstar@gmail.com> */
  533. list_for_each_entry(ici, &hosts, list) {
  534. if (icd->iface == ici->nr) {
  535. ret = 1;
  536. icd->dev.parent = &ici->dev;
  537. break;
  538. }
  539. }
  540. mutex_unlock(&list_lock);
  541. if (ret)
  542. ret = device_register_link(icd);
  543. return ret;
  544. }
  545. static int soc_camera_probe(struct device *dev)
  546. {
  547. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  548. struct soc_camera_host *ici =
  549. to_soc_camera_host(icd->dev.parent);
  550. int ret;
  551. if (!icd->ops->probe)
  552. return -ENODEV;
  553. /* We only call ->add() here to activate and probe the camera.
  554. * We shall ->remove() and deactivate it immediately afterwards. */
  555. ret = ici->ops->add(icd);
  556. if (ret < 0)
  557. return ret;
  558. ret = icd->ops->probe(icd);
  559. if (ret >= 0) {
  560. const struct v4l2_queryctrl *qctrl;
  561. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  562. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  563. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  564. icd->exposure = qctrl ? qctrl->default_value :
  565. (unsigned short)~0;
  566. }
  567. ici->ops->remove(icd);
  568. return ret;
  569. }
  570. /* This is called on device_unregister, which only means we have to disconnect
  571. * from the host, but not remove ourselves from the device list */
  572. static int soc_camera_remove(struct device *dev)
  573. {
  574. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  575. if (icd->ops->remove)
  576. icd->ops->remove(icd);
  577. return 0;
  578. }
  579. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  580. {
  581. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  582. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  583. int ret = 0;
  584. if (ici->ops->suspend)
  585. ret = ici->ops->suspend(icd, state);
  586. return ret;
  587. }
  588. static int soc_camera_resume(struct device *dev)
  589. {
  590. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  591. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  592. int ret = 0;
  593. if (ici->ops->resume)
  594. ret = ici->ops->resume(icd);
  595. return ret;
  596. }
  597. static struct bus_type soc_camera_bus_type = {
  598. .name = "soc-camera",
  599. .probe = soc_camera_probe,
  600. .remove = soc_camera_remove,
  601. .suspend = soc_camera_suspend,
  602. .resume = soc_camera_resume,
  603. };
  604. static struct device_driver ic_drv = {
  605. .name = "camera",
  606. .bus = &soc_camera_bus_type,
  607. .owner = THIS_MODULE,
  608. };
  609. static void dummy_release(struct device *dev)
  610. {
  611. }
  612. int soc_camera_host_register(struct soc_camera_host *ici)
  613. {
  614. int ret;
  615. struct soc_camera_host *ix;
  616. if (!ici->ops->init_videobuf || !ici->ops->add || !ici->ops->remove)
  617. return -EINVAL;
  618. /* Number might be equal to the platform device ID */
  619. dev_set_name(&ici->dev, "camera_host%d", ici->nr);
  620. mutex_lock(&list_lock);
  621. list_for_each_entry(ix, &hosts, list) {
  622. if (ix->nr == ici->nr) {
  623. mutex_unlock(&list_lock);
  624. return -EBUSY;
  625. }
  626. }
  627. list_add_tail(&ici->list, &hosts);
  628. mutex_unlock(&list_lock);
  629. ici->dev.release = dummy_release;
  630. ret = device_register(&ici->dev);
  631. if (ret)
  632. goto edevr;
  633. scan_add_host(ici);
  634. return 0;
  635. edevr:
  636. mutex_lock(&list_lock);
  637. list_del(&ici->list);
  638. mutex_unlock(&list_lock);
  639. return ret;
  640. }
  641. EXPORT_SYMBOL(soc_camera_host_register);
  642. /* Unregister all clients! */
  643. void soc_camera_host_unregister(struct soc_camera_host *ici)
  644. {
  645. struct soc_camera_device *icd;
  646. mutex_lock(&list_lock);
  647. list_del(&ici->list);
  648. list_for_each_entry(icd, &devices, list) {
  649. if (icd->dev.parent == &ici->dev) {
  650. device_unregister(&icd->dev);
  651. /* Not before device_unregister(), .remove
  652. * needs parent to call ici->ops->remove() */
  653. icd->dev.parent = NULL;
  654. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  655. }
  656. }
  657. mutex_unlock(&list_lock);
  658. device_unregister(&ici->dev);
  659. }
  660. EXPORT_SYMBOL(soc_camera_host_unregister);
  661. /* Image capture device */
  662. int soc_camera_device_register(struct soc_camera_device *icd)
  663. {
  664. struct soc_camera_device *ix;
  665. int num = -1, i;
  666. if (!icd)
  667. return -EINVAL;
  668. for (i = 0; i < 256 && num < 0; i++) {
  669. num = i;
  670. list_for_each_entry(ix, &devices, list) {
  671. if (ix->iface == icd->iface && ix->devnum == i) {
  672. num = -1;
  673. break;
  674. }
  675. }
  676. }
  677. if (num < 0)
  678. /* ok, we have 256 cameras on this host...
  679. * man, stay reasonable... */
  680. return -ENOMEM;
  681. icd->devnum = num;
  682. icd->dev.bus = &soc_camera_bus_type;
  683. dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
  684. icd->dev.release = dummy_release;
  685. return scan_add_device(icd);
  686. }
  687. EXPORT_SYMBOL(soc_camera_device_register);
  688. void soc_camera_device_unregister(struct soc_camera_device *icd)
  689. {
  690. mutex_lock(&list_lock);
  691. list_del(&icd->list);
  692. /* The bus->remove will be eventually called */
  693. if (icd->dev.parent)
  694. device_unregister(&icd->dev);
  695. mutex_unlock(&list_lock);
  696. }
  697. EXPORT_SYMBOL(soc_camera_device_unregister);
  698. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  699. .vidioc_querycap = soc_camera_querycap,
  700. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  701. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  702. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  703. .vidioc_enum_input = soc_camera_enum_input,
  704. .vidioc_g_input = soc_camera_g_input,
  705. .vidioc_s_input = soc_camera_s_input,
  706. .vidioc_s_std = soc_camera_s_std,
  707. .vidioc_reqbufs = soc_camera_reqbufs,
  708. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  709. .vidioc_querybuf = soc_camera_querybuf,
  710. .vidioc_qbuf = soc_camera_qbuf,
  711. .vidioc_dqbuf = soc_camera_dqbuf,
  712. .vidioc_streamon = soc_camera_streamon,
  713. .vidioc_streamoff = soc_camera_streamoff,
  714. .vidioc_queryctrl = soc_camera_queryctrl,
  715. .vidioc_g_ctrl = soc_camera_g_ctrl,
  716. .vidioc_s_ctrl = soc_camera_s_ctrl,
  717. .vidioc_cropcap = soc_camera_cropcap,
  718. .vidioc_g_crop = soc_camera_g_crop,
  719. .vidioc_s_crop = soc_camera_s_crop,
  720. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  721. #ifdef CONFIG_VIDEO_ADV_DEBUG
  722. .vidioc_g_register = soc_camera_g_register,
  723. .vidioc_s_register = soc_camera_s_register,
  724. #endif
  725. };
  726. int soc_camera_video_start(struct soc_camera_device *icd)
  727. {
  728. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  729. int err = -ENOMEM;
  730. struct video_device *vdev;
  731. if (!icd->dev.parent)
  732. return -ENODEV;
  733. vdev = video_device_alloc();
  734. if (!vdev)
  735. goto evidallocd;
  736. dev_dbg(&ici->dev, "Allocated video_device %p\n", vdev);
  737. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  738. /* Maybe better &ici->dev */
  739. vdev->parent = &icd->dev;
  740. vdev->current_norm = V4L2_STD_UNKNOWN;
  741. vdev->fops = &soc_camera_fops;
  742. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  743. vdev->release = video_device_release;
  744. vdev->minor = -1;
  745. vdev->tvnorms = V4L2_STD_UNKNOWN,
  746. icd->current_fmt = &icd->formats[0];
  747. err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
  748. if (err < 0) {
  749. dev_err(vdev->parent, "video_register_device failed\n");
  750. goto evidregd;
  751. }
  752. icd->vdev = vdev;
  753. return 0;
  754. evidregd:
  755. video_device_release(vdev);
  756. evidallocd:
  757. return err;
  758. }
  759. EXPORT_SYMBOL(soc_camera_video_start);
  760. void soc_camera_video_stop(struct soc_camera_device *icd)
  761. {
  762. struct video_device *vdev = icd->vdev;
  763. dev_dbg(&icd->dev, "%s\n", __func__);
  764. if (!icd->dev.parent || !vdev)
  765. return;
  766. mutex_lock(&video_lock);
  767. video_unregister_device(vdev);
  768. icd->vdev = NULL;
  769. mutex_unlock(&video_lock);
  770. }
  771. EXPORT_SYMBOL(soc_camera_video_stop);
  772. static int __init soc_camera_init(void)
  773. {
  774. int ret = bus_register(&soc_camera_bus_type);
  775. if (ret)
  776. return ret;
  777. ret = driver_register(&ic_drv);
  778. if (ret)
  779. goto edrvr;
  780. return 0;
  781. edrvr:
  782. bus_unregister(&soc_camera_bus_type);
  783. return ret;
  784. }
  785. static void __exit soc_camera_exit(void)
  786. {
  787. driver_unregister(&ic_drv);
  788. bus_unregister(&soc_camera_bus_type);
  789. }
  790. module_init(soc_camera_init);
  791. module_exit(soc_camera_exit);
  792. MODULE_DESCRIPTION("Image capture bus driver");
  793. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  794. MODULE_LICENSE("GPL");