v4l2-dev.c 21 KB

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