v4l2-dev.c 21 KB

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