v4l2-dev.c 21 KB

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