v4l2-dev.c 21 KB

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