v4l2-dev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * Video capture interface for Linux version 2
  3. *
  4. * A generic video device interface for the LINUX operating system
  5. * using a set of device structures/vectors for low level operations.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
  13. * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
  14. *
  15. * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
  16. * - Added procfs support
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/string.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/kmod.h>
  26. #include <linux/slab.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/system.h>
  29. #include <media/v4l2-common.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-ioctl.h>
  32. #define VIDEO_NUM_DEVICES 256
  33. #define VIDEO_NAME "video4linux"
  34. /*
  35. * sysfs stuff
  36. */
  37. static ssize_t show_index(struct device *cd,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. struct video_device *vdev = to_video_device(cd);
  41. return sprintf(buf, "%i\n", vdev->index);
  42. }
  43. static ssize_t show_name(struct device *cd,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct video_device *vdev = to_video_device(cd);
  47. return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
  48. }
  49. static struct device_attribute video_device_attrs[] = {
  50. __ATTR(name, S_IRUGO, show_name, NULL),
  51. __ATTR(index, S_IRUGO, show_index, NULL),
  52. __ATTR_NULL
  53. };
  54. /*
  55. * Active devices
  56. */
  57. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  58. static DEFINE_MUTEX(videodev_lock);
  59. static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
  60. /* Device node utility functions */
  61. /* Note: these utility functions all assume that vfl_type is in the range
  62. [0, VFL_TYPE_MAX-1]. */
  63. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  64. /* Return the bitmap corresponding to vfl_type. */
  65. static inline unsigned long *devnode_bits(int vfl_type)
  66. {
  67. /* Any types not assigned to fixed minor ranges must be mapped to
  68. one single bitmap for the purposes of finding a free node number
  69. since all those unassigned types use the same minor range. */
  70. int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
  71. return devnode_nums[idx];
  72. }
  73. #else
  74. /* Return the bitmap corresponding to vfl_type. */
  75. static inline unsigned long *devnode_bits(int vfl_type)
  76. {
  77. return devnode_nums[vfl_type];
  78. }
  79. #endif
  80. /* Mark device node number vdev->num as used */
  81. static inline void devnode_set(struct video_device *vdev)
  82. {
  83. set_bit(vdev->num, devnode_bits(vdev->vfl_type));
  84. }
  85. /* Mark device node number vdev->num as unused */
  86. static inline void devnode_clear(struct video_device *vdev)
  87. {
  88. clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
  89. }
  90. /* Try to find a free device node number in the range [from, to> */
  91. static inline int devnode_find(struct video_device *vdev, int from, int to)
  92. {
  93. return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
  94. }
  95. struct video_device *video_device_alloc(void)
  96. {
  97. return kzalloc(sizeof(struct video_device), GFP_KERNEL);
  98. }
  99. EXPORT_SYMBOL(video_device_alloc);
  100. void video_device_release(struct video_device *vdev)
  101. {
  102. kfree(vdev);
  103. }
  104. EXPORT_SYMBOL(video_device_release);
  105. void video_device_release_empty(struct video_device *vdev)
  106. {
  107. /* Do nothing */
  108. /* Only valid when the video_device struct is a static. */
  109. }
  110. EXPORT_SYMBOL(video_device_release_empty);
  111. static inline void video_get(struct video_device *vdev)
  112. {
  113. get_device(&vdev->dev);
  114. }
  115. static inline void video_put(struct video_device *vdev)
  116. {
  117. put_device(&vdev->dev);
  118. }
  119. /* Called when the last user of the video device exits. */
  120. static void v4l2_device_release(struct device *cd)
  121. {
  122. struct video_device *vdev = to_video_device(cd);
  123. struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
  124. mutex_lock(&videodev_lock);
  125. if (video_device[vdev->minor] != vdev) {
  126. mutex_unlock(&videodev_lock);
  127. /* should not happen */
  128. WARN_ON(1);
  129. return;
  130. }
  131. /* Free up this device for reuse */
  132. video_device[vdev->minor] = NULL;
  133. /* Delete the cdev on this minor as well */
  134. cdev_del(vdev->cdev);
  135. /* Just in case some driver tries to access this from
  136. the release() callback. */
  137. vdev->cdev = NULL;
  138. /* Mark device node number as free */
  139. devnode_clear(vdev);
  140. mutex_unlock(&videodev_lock);
  141. #if defined(CONFIG_MEDIA_CONTROLLER)
  142. if (vdev->v4l2_dev && vdev->v4l2_dev->mdev &&
  143. vdev->vfl_type != VFL_TYPE_SUBDEV)
  144. media_device_unregister_entity(&vdev->entity);
  145. #endif
  146. /* Release video_device and perform other
  147. cleanups as needed. */
  148. vdev->release(vdev);
  149. /* Decrease v4l2_device refcount */
  150. if (v4l2_dev)
  151. v4l2_device_put(v4l2_dev);
  152. }
  153. static struct class video_class = {
  154. .name = VIDEO_NAME,
  155. .dev_attrs = video_device_attrs,
  156. };
  157. struct video_device *video_devdata(struct file *file)
  158. {
  159. return video_device[iminor(file->f_path.dentry->d_inode)];
  160. }
  161. EXPORT_SYMBOL(video_devdata);
  162. /* Priority handling */
  163. static inline bool prio_is_valid(enum v4l2_priority prio)
  164. {
  165. return prio == V4L2_PRIORITY_BACKGROUND ||
  166. prio == V4L2_PRIORITY_INTERACTIVE ||
  167. prio == V4L2_PRIORITY_RECORD;
  168. }
  169. void v4l2_prio_init(struct v4l2_prio_state *global)
  170. {
  171. memset(global, 0, sizeof(*global));
  172. }
  173. EXPORT_SYMBOL(v4l2_prio_init);
  174. int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
  175. enum v4l2_priority new)
  176. {
  177. if (!prio_is_valid(new))
  178. return -EINVAL;
  179. if (*local == new)
  180. return 0;
  181. atomic_inc(&global->prios[new]);
  182. if (prio_is_valid(*local))
  183. atomic_dec(&global->prios[*local]);
  184. *local = new;
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(v4l2_prio_change);
  188. void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
  189. {
  190. v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
  191. }
  192. EXPORT_SYMBOL(v4l2_prio_open);
  193. void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
  194. {
  195. if (prio_is_valid(local))
  196. atomic_dec(&global->prios[local]);
  197. }
  198. EXPORT_SYMBOL(v4l2_prio_close);
  199. enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
  200. {
  201. if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
  202. return V4L2_PRIORITY_RECORD;
  203. if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
  204. return V4L2_PRIORITY_INTERACTIVE;
  205. if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
  206. return V4L2_PRIORITY_BACKGROUND;
  207. return V4L2_PRIORITY_UNSET;
  208. }
  209. EXPORT_SYMBOL(v4l2_prio_max);
  210. int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
  211. {
  212. return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
  213. }
  214. EXPORT_SYMBOL(v4l2_prio_check);
  215. static ssize_t v4l2_read(struct file *filp, char __user *buf,
  216. size_t sz, loff_t *off)
  217. {
  218. struct video_device *vdev = video_devdata(filp);
  219. int ret = -ENODEV;
  220. if (!vdev->fops->read)
  221. return -EINVAL;
  222. if (vdev->lock && mutex_lock_interruptible(vdev->lock))
  223. return -ERESTARTSYS;
  224. if (video_is_registered(vdev))
  225. ret = vdev->fops->read(filp, buf, sz, off);
  226. if (vdev->lock)
  227. mutex_unlock(vdev->lock);
  228. return ret;
  229. }
  230. static ssize_t v4l2_write(struct file *filp, const char __user *buf,
  231. size_t sz, loff_t *off)
  232. {
  233. struct video_device *vdev = video_devdata(filp);
  234. int ret = -ENODEV;
  235. if (!vdev->fops->write)
  236. return -EINVAL;
  237. if (vdev->lock && mutex_lock_interruptible(vdev->lock))
  238. return -ERESTARTSYS;
  239. if (video_is_registered(vdev))
  240. ret = vdev->fops->write(filp, buf, sz, off);
  241. if (vdev->lock)
  242. mutex_unlock(vdev->lock);
  243. return ret;
  244. }
  245. static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
  246. {
  247. struct video_device *vdev = video_devdata(filp);
  248. int ret = POLLERR | POLLHUP;
  249. if (!vdev->fops->poll)
  250. return DEFAULT_POLLMASK;
  251. if (vdev->lock)
  252. mutex_lock(vdev->lock);
  253. if (video_is_registered(vdev))
  254. ret = vdev->fops->poll(filp, poll);
  255. if (vdev->lock)
  256. mutex_unlock(vdev->lock);
  257. return ret;
  258. }
  259. static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  260. {
  261. struct video_device *vdev = video_devdata(filp);
  262. int ret = -ENODEV;
  263. if (vdev->fops->unlocked_ioctl) {
  264. if (vdev->lock && mutex_lock_interruptible(vdev->lock))
  265. return -ERESTARTSYS;
  266. if (video_is_registered(vdev))
  267. ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
  268. if (vdev->lock)
  269. mutex_unlock(vdev->lock);
  270. } else if (vdev->fops->ioctl) {
  271. /* This code path is a replacement for the BKL. It is a major
  272. * hack but it will have to do for those drivers that are not
  273. * yet converted to use unlocked_ioctl.
  274. *
  275. * There are two options: if the driver implements struct
  276. * v4l2_device, then the lock defined there is used to
  277. * serialize the ioctls. Otherwise the v4l2 core lock defined
  278. * below is used. This lock is really bad since it serializes
  279. * completely independent devices.
  280. *
  281. * Both variants suffer from the same problem: if the driver
  282. * sleeps, then it blocks all ioctls since the lock is still
  283. * held. This is very common for VIDIOC_DQBUF since that
  284. * normally waits for a frame to arrive. As a result any other
  285. * ioctl calls will proceed very, very slowly since each call
  286. * will have to wait for the VIDIOC_QBUF to finish. Things that
  287. * should take 0.01s may now take 10-20 seconds.
  288. *
  289. * The workaround is to *not* take the lock for VIDIOC_DQBUF.
  290. * This actually works OK for videobuf-based drivers, since
  291. * videobuf will take its own internal lock.
  292. */
  293. static DEFINE_MUTEX(v4l2_ioctl_mutex);
  294. struct mutex *m = vdev->v4l2_dev ?
  295. &vdev->v4l2_dev->ioctl_lock : &v4l2_ioctl_mutex;
  296. if (cmd != VIDIOC_DQBUF && mutex_lock_interruptible(m))
  297. return -ERESTARTSYS;
  298. if (video_is_registered(vdev))
  299. ret = vdev->fops->ioctl(filp, cmd, arg);
  300. if (cmd != VIDIOC_DQBUF)
  301. mutex_unlock(m);
  302. } else
  303. ret = -ENOTTY;
  304. return ret;
  305. }
  306. #ifdef CONFIG_MMU
  307. #define v4l2_get_unmapped_area NULL
  308. #else
  309. static unsigned long v4l2_get_unmapped_area(struct file *filp,
  310. unsigned long addr, unsigned long len, unsigned long pgoff,
  311. unsigned long flags)
  312. {
  313. struct video_device *vdev = video_devdata(filp);
  314. if (!vdev->fops->get_unmapped_area)
  315. return -ENOSYS;
  316. if (!video_is_registered(vdev))
  317. return -ENODEV;
  318. return vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
  319. }
  320. #endif
  321. static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
  322. {
  323. struct video_device *vdev = video_devdata(filp);
  324. int ret = -ENODEV;
  325. if (!vdev->fops->mmap)
  326. return ret;
  327. if (vdev->lock && mutex_lock_interruptible(vdev->lock))
  328. return -ERESTARTSYS;
  329. if (video_is_registered(vdev))
  330. ret = vdev->fops->mmap(filp, vm);
  331. if (vdev->lock)
  332. mutex_unlock(vdev->lock);
  333. return ret;
  334. }
  335. /* Override for the open function */
  336. static int v4l2_open(struct inode *inode, struct file *filp)
  337. {
  338. struct video_device *vdev;
  339. int ret = 0;
  340. /* Check if the video device is available */
  341. mutex_lock(&videodev_lock);
  342. vdev = video_devdata(filp);
  343. /* return ENODEV if the video device has already been removed. */
  344. if (vdev == NULL || !video_is_registered(vdev)) {
  345. mutex_unlock(&videodev_lock);
  346. return -ENODEV;
  347. }
  348. /* and increase the device refcount */
  349. video_get(vdev);
  350. mutex_unlock(&videodev_lock);
  351. if (vdev->fops->open) {
  352. if (vdev->lock && mutex_lock_interruptible(vdev->lock)) {
  353. ret = -ERESTARTSYS;
  354. goto err;
  355. }
  356. if (video_is_registered(vdev))
  357. ret = vdev->fops->open(filp);
  358. else
  359. ret = -ENODEV;
  360. if (vdev->lock)
  361. mutex_unlock(vdev->lock);
  362. }
  363. err:
  364. /* decrease the refcount in case of an error */
  365. if (ret)
  366. video_put(vdev);
  367. return ret;
  368. }
  369. /* Override for the release function */
  370. static int v4l2_release(struct inode *inode, struct file *filp)
  371. {
  372. struct video_device *vdev = video_devdata(filp);
  373. int ret = 0;
  374. if (vdev->fops->release) {
  375. if (vdev->lock)
  376. mutex_lock(vdev->lock);
  377. vdev->fops->release(filp);
  378. if (vdev->lock)
  379. mutex_unlock(vdev->lock);
  380. }
  381. /* decrease the refcount unconditionally since the release()
  382. return value is ignored. */
  383. video_put(vdev);
  384. return ret;
  385. }
  386. static const struct file_operations v4l2_fops = {
  387. .owner = THIS_MODULE,
  388. .read = v4l2_read,
  389. .write = v4l2_write,
  390. .open = v4l2_open,
  391. .get_unmapped_area = v4l2_get_unmapped_area,
  392. .mmap = v4l2_mmap,
  393. .unlocked_ioctl = v4l2_ioctl,
  394. #ifdef CONFIG_COMPAT
  395. .compat_ioctl = v4l2_compat_ioctl32,
  396. #endif
  397. .release = v4l2_release,
  398. .poll = v4l2_poll,
  399. .llseek = no_llseek,
  400. };
  401. /**
  402. * get_index - assign stream index number based on parent device
  403. * @vdev: video_device to assign index number to, vdev->parent should be assigned
  404. *
  405. * Note that when this is called the new device has not yet been registered
  406. * in the video_device array, but it was able to obtain a minor number.
  407. *
  408. * This means that we can always obtain a free stream index number since
  409. * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
  410. * use of the video_device array.
  411. *
  412. * Returns a free index number.
  413. */
  414. static int get_index(struct video_device *vdev)
  415. {
  416. /* This can be static since this function is called with the global
  417. videodev_lock held. */
  418. static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
  419. int i;
  420. /* Some drivers do not set the parent. In that case always return 0. */
  421. if (vdev->parent == NULL)
  422. return 0;
  423. bitmap_zero(used, VIDEO_NUM_DEVICES);
  424. for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
  425. if (video_device[i] != NULL &&
  426. video_device[i]->parent == vdev->parent) {
  427. set_bit(video_device[i]->index, used);
  428. }
  429. }
  430. return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
  431. }
  432. /**
  433. * __video_register_device - register video4linux devices
  434. * @vdev: video device structure we want to register
  435. * @type: type of device to register
  436. * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
  437. * -1 == first free)
  438. * @warn_if_nr_in_use: warn if the desired device node number
  439. * was already in use and another number was chosen instead.
  440. * @owner: module that owns the video device node
  441. *
  442. * The registration code assigns minor numbers and device node numbers
  443. * based on the requested type and registers the new device node with
  444. * the kernel.
  445. *
  446. * This function assumes that struct video_device was zeroed when it
  447. * was allocated and does not contain any stale date.
  448. *
  449. * An error is returned if no free minor or device node number could be
  450. * found, or if the registration of the device node failed.
  451. *
  452. * Zero is returned on success.
  453. *
  454. * Valid types are
  455. *
  456. * %VFL_TYPE_GRABBER - A frame grabber
  457. *
  458. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  459. *
  460. * %VFL_TYPE_RADIO - A radio card
  461. *
  462. * %VFL_TYPE_SUBDEV - A subdevice
  463. */
  464. int __video_register_device(struct video_device *vdev, int type, int nr,
  465. int warn_if_nr_in_use, struct module *owner)
  466. {
  467. int i = 0;
  468. int ret;
  469. int minor_offset = 0;
  470. int minor_cnt = VIDEO_NUM_DEVICES;
  471. const char *name_base;
  472. /* A minor value of -1 marks this video device as never
  473. having been registered */
  474. vdev->minor = -1;
  475. /* the release callback MUST be present */
  476. WARN_ON(!vdev->release);
  477. if (!vdev->release)
  478. return -EINVAL;
  479. /* v4l2_fh support */
  480. spin_lock_init(&vdev->fh_lock);
  481. INIT_LIST_HEAD(&vdev->fh_list);
  482. /* Part 1: check device type */
  483. switch (type) {
  484. case VFL_TYPE_GRABBER:
  485. name_base = "video";
  486. break;
  487. case VFL_TYPE_VBI:
  488. name_base = "vbi";
  489. break;
  490. case VFL_TYPE_RADIO:
  491. name_base = "radio";
  492. break;
  493. case VFL_TYPE_SUBDEV:
  494. name_base = "v4l-subdev";
  495. break;
  496. default:
  497. printk(KERN_ERR "%s called with unknown type: %d\n",
  498. __func__, type);
  499. return -EINVAL;
  500. }
  501. vdev->vfl_type = type;
  502. vdev->cdev = NULL;
  503. if (vdev->v4l2_dev) {
  504. if (vdev->v4l2_dev->dev)
  505. vdev->parent = vdev->v4l2_dev->dev;
  506. if (vdev->ctrl_handler == NULL)
  507. vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
  508. /* If the prio state pointer is NULL, then use the v4l2_device
  509. prio state. */
  510. if (vdev->prio == NULL)
  511. vdev->prio = &vdev->v4l2_dev->prio;
  512. }
  513. /* Part 2: find a free minor, device node number and device index. */
  514. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  515. /* Keep the ranges for the first four types for historical
  516. * reasons.
  517. * Newer devices (not yet in place) should use the range
  518. * of 128-191 and just pick the first free minor there
  519. * (new style). */
  520. switch (type) {
  521. case VFL_TYPE_GRABBER:
  522. minor_offset = 0;
  523. minor_cnt = 64;
  524. break;
  525. case VFL_TYPE_RADIO:
  526. minor_offset = 64;
  527. minor_cnt = 64;
  528. break;
  529. case VFL_TYPE_VBI:
  530. minor_offset = 224;
  531. minor_cnt = 32;
  532. break;
  533. default:
  534. minor_offset = 128;
  535. minor_cnt = 64;
  536. break;
  537. }
  538. #endif
  539. /* Pick a device node number */
  540. mutex_lock(&videodev_lock);
  541. nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
  542. if (nr == minor_cnt)
  543. nr = devnode_find(vdev, 0, minor_cnt);
  544. if (nr == minor_cnt) {
  545. printk(KERN_ERR "could not get a free device node number\n");
  546. mutex_unlock(&videodev_lock);
  547. return -ENFILE;
  548. }
  549. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  550. /* 1-on-1 mapping of device node number to minor number */
  551. i = nr;
  552. #else
  553. /* The device node number and minor numbers are independent, so
  554. we just find the first free minor number. */
  555. for (i = 0; i < VIDEO_NUM_DEVICES; i++)
  556. if (video_device[i] == NULL)
  557. break;
  558. if (i == VIDEO_NUM_DEVICES) {
  559. mutex_unlock(&videodev_lock);
  560. printk(KERN_ERR "could not get a free minor\n");
  561. return -ENFILE;
  562. }
  563. #endif
  564. vdev->minor = i + minor_offset;
  565. vdev->num = nr;
  566. devnode_set(vdev);
  567. /* Should not happen since we thought this minor was free */
  568. WARN_ON(video_device[vdev->minor] != NULL);
  569. vdev->index = get_index(vdev);
  570. mutex_unlock(&videodev_lock);
  571. /* Part 3: Initialize the character device */
  572. vdev->cdev = cdev_alloc();
  573. if (vdev->cdev == NULL) {
  574. ret = -ENOMEM;
  575. goto cleanup;
  576. }
  577. vdev->cdev->ops = &v4l2_fops;
  578. vdev->cdev->owner = owner;
  579. ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
  580. if (ret < 0) {
  581. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  582. kfree(vdev->cdev);
  583. vdev->cdev = NULL;
  584. goto cleanup;
  585. }
  586. /* Part 4: register the device with sysfs */
  587. vdev->dev.class = &video_class;
  588. vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
  589. if (vdev->parent)
  590. vdev->dev.parent = vdev->parent;
  591. dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
  592. ret = device_register(&vdev->dev);
  593. if (ret < 0) {
  594. printk(KERN_ERR "%s: device_register failed\n", __func__);
  595. goto cleanup;
  596. }
  597. /* Register the release callback that will be called when the last
  598. reference to the device goes away. */
  599. vdev->dev.release = v4l2_device_release;
  600. if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
  601. printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
  602. name_base, nr, video_device_node_name(vdev));
  603. /* Increase v4l2_device refcount */
  604. if (vdev->v4l2_dev)
  605. v4l2_device_get(vdev->v4l2_dev);
  606. #if defined(CONFIG_MEDIA_CONTROLLER)
  607. /* Part 5: Register the entity. */
  608. if (vdev->v4l2_dev && vdev->v4l2_dev->mdev &&
  609. vdev->vfl_type != VFL_TYPE_SUBDEV) {
  610. vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
  611. vdev->entity.name = vdev->name;
  612. vdev->entity.v4l.major = VIDEO_MAJOR;
  613. vdev->entity.v4l.minor = vdev->minor;
  614. ret = media_device_register_entity(vdev->v4l2_dev->mdev,
  615. &vdev->entity);
  616. if (ret < 0)
  617. printk(KERN_WARNING
  618. "%s: media_device_register_entity failed\n",
  619. __func__);
  620. }
  621. #endif
  622. /* Part 6: Activate this minor. The char device can now be used. */
  623. set_bit(V4L2_FL_REGISTERED, &vdev->flags);
  624. mutex_lock(&videodev_lock);
  625. video_device[vdev->minor] = vdev;
  626. mutex_unlock(&videodev_lock);
  627. return 0;
  628. cleanup:
  629. mutex_lock(&videodev_lock);
  630. if (vdev->cdev)
  631. cdev_del(vdev->cdev);
  632. devnode_clear(vdev);
  633. mutex_unlock(&videodev_lock);
  634. /* Mark this video device as never having been registered. */
  635. vdev->minor = -1;
  636. return ret;
  637. }
  638. EXPORT_SYMBOL(__video_register_device);
  639. /**
  640. * video_unregister_device - unregister a video4linux device
  641. * @vdev: the device to unregister
  642. *
  643. * This unregisters the passed device. Future open calls will
  644. * be met with errors.
  645. */
  646. void video_unregister_device(struct video_device *vdev)
  647. {
  648. /* Check if vdev was ever registered at all */
  649. if (!vdev || !video_is_registered(vdev))
  650. return;
  651. mutex_lock(&videodev_lock);
  652. /* This must be in a critical section to prevent a race with v4l2_open.
  653. * Once this bit has been cleared video_get may never be called again.
  654. */
  655. clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
  656. mutex_unlock(&videodev_lock);
  657. device_unregister(&vdev->dev);
  658. }
  659. EXPORT_SYMBOL(video_unregister_device);
  660. /*
  661. * Initialise video for linux
  662. */
  663. static int __init videodev_init(void)
  664. {
  665. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  666. int ret;
  667. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  668. ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
  669. if (ret < 0) {
  670. printk(KERN_WARNING "videodev: unable to get major %d\n",
  671. VIDEO_MAJOR);
  672. return ret;
  673. }
  674. ret = class_register(&video_class);
  675. if (ret < 0) {
  676. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  677. printk(KERN_WARNING "video_dev: class_register failed\n");
  678. return -EIO;
  679. }
  680. return 0;
  681. }
  682. static void __exit videodev_exit(void)
  683. {
  684. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  685. class_unregister(&video_class);
  686. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  687. }
  688. module_init(videodev_init)
  689. module_exit(videodev_exit)
  690. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  691. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  692. MODULE_LICENSE("GPL");
  693. MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
  694. /*
  695. * Local variables:
  696. * c-basic-offset: 8
  697. * End:
  698. */