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