soc_camera.c 24 KB

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