v4l2-dev.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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 index_show(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 DEVICE_ATTR_RO(index);
  43. static ssize_t dev_debug_show(struct device *cd,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct video_device *vdev = to_video_device(cd);
  47. return sprintf(buf, "%i\n", vdev->dev_debug);
  48. }
  49. static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
  50. const char *buf, size_t len)
  51. {
  52. struct video_device *vdev = to_video_device(cd);
  53. int res = 0;
  54. u16 value;
  55. res = kstrtou16(buf, 0, &value);
  56. if (res)
  57. return res;
  58. vdev->dev_debug = value;
  59. return len;
  60. }
  61. static DEVICE_ATTR_RW(dev_debug);
  62. static ssize_t name_show(struct device *cd,
  63. struct device_attribute *attr, char *buf)
  64. {
  65. struct video_device *vdev = to_video_device(cd);
  66. return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
  67. }
  68. static DEVICE_ATTR_RO(name);
  69. static struct attribute *video_device_attrs[] = {
  70. &dev_attr_name.attr,
  71. &dev_attr_dev_debug.attr,
  72. &dev_attr_index.attr,
  73. NULL,
  74. };
  75. ATTRIBUTE_GROUPS(video_device);
  76. /*
  77. * Active devices
  78. */
  79. static struct video_device *video_device[VIDEO_NUM_DEVICES];
  80. static DEFINE_MUTEX(videodev_lock);
  81. static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
  82. /* Device node utility functions */
  83. /* Note: these utility functions all assume that vfl_type is in the range
  84. [0, VFL_TYPE_MAX-1]. */
  85. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  86. /* Return the bitmap corresponding to vfl_type. */
  87. static inline unsigned long *devnode_bits(int vfl_type)
  88. {
  89. /* Any types not assigned to fixed minor ranges must be mapped to
  90. one single bitmap for the purposes of finding a free node number
  91. since all those unassigned types use the same minor range. */
  92. int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
  93. return devnode_nums[idx];
  94. }
  95. #else
  96. /* Return the bitmap corresponding to vfl_type. */
  97. static inline unsigned long *devnode_bits(int vfl_type)
  98. {
  99. return devnode_nums[vfl_type];
  100. }
  101. #endif
  102. /* Mark device node number vdev->num as used */
  103. static inline void devnode_set(struct video_device *vdev)
  104. {
  105. set_bit(vdev->num, devnode_bits(vdev->vfl_type));
  106. }
  107. /* Mark device node number vdev->num as unused */
  108. static inline void devnode_clear(struct video_device *vdev)
  109. {
  110. clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
  111. }
  112. /* Try to find a free device node number in the range [from, to> */
  113. static inline int devnode_find(struct video_device *vdev, int from, int to)
  114. {
  115. return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
  116. }
  117. struct video_device *video_device_alloc(void)
  118. {
  119. return kzalloc(sizeof(struct video_device), GFP_KERNEL);
  120. }
  121. EXPORT_SYMBOL(video_device_alloc);
  122. void video_device_release(struct video_device *vdev)
  123. {
  124. kfree(vdev);
  125. }
  126. EXPORT_SYMBOL(video_device_release);
  127. void video_device_release_empty(struct video_device *vdev)
  128. {
  129. /* Do nothing */
  130. /* Only valid when the video_device struct is a static. */
  131. }
  132. EXPORT_SYMBOL(video_device_release_empty);
  133. static inline void video_get(struct video_device *vdev)
  134. {
  135. get_device(&vdev->dev);
  136. }
  137. static inline void video_put(struct video_device *vdev)
  138. {
  139. put_device(&vdev->dev);
  140. }
  141. /* Called when the last user of the video device exits. */
  142. static void v4l2_device_release(struct device *cd)
  143. {
  144. struct video_device *vdev = to_video_device(cd);
  145. struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
  146. mutex_lock(&videodev_lock);
  147. if (WARN_ON(video_device[vdev->minor] != vdev)) {
  148. /* should not happen */
  149. mutex_unlock(&videodev_lock);
  150. return;
  151. }
  152. /* Free up this device for reuse */
  153. video_device[vdev->minor] = NULL;
  154. /* Delete the cdev on this minor as well */
  155. cdev_del(vdev->cdev);
  156. /* Just in case some driver tries to access this from
  157. the release() callback. */
  158. vdev->cdev = NULL;
  159. /* Mark device node number as free */
  160. devnode_clear(vdev);
  161. mutex_unlock(&videodev_lock);
  162. #if defined(CONFIG_MEDIA_CONTROLLER)
  163. if (v4l2_dev->mdev) {
  164. /* Remove interfaces and interface links */
  165. media_devnode_remove(vdev->intf_devnode);
  166. if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN)
  167. media_device_unregister_entity(&vdev->entity);
  168. }
  169. #endif
  170. /* Do not call v4l2_device_put if there is no release callback set.
  171. * Drivers that have no v4l2_device release callback might free the
  172. * v4l2_dev instance in the video_device release callback below, so we
  173. * must perform this check here.
  174. *
  175. * TODO: In the long run all drivers that use v4l2_device should use the
  176. * v4l2_device release callback. This check will then be unnecessary.
  177. */
  178. if (v4l2_dev->release == NULL)
  179. v4l2_dev = NULL;
  180. /* Release video_device and perform other
  181. cleanups as needed. */
  182. vdev->release(vdev);
  183. /* Decrease v4l2_device refcount */
  184. if (v4l2_dev)
  185. v4l2_device_put(v4l2_dev);
  186. }
  187. static struct class video_class = {
  188. .name = VIDEO_NAME,
  189. .dev_groups = video_device_groups,
  190. };
  191. struct video_device *video_devdata(struct file *file)
  192. {
  193. return video_device[iminor(file_inode(file))];
  194. }
  195. EXPORT_SYMBOL(video_devdata);
  196. /* Priority handling */
  197. static inline bool prio_is_valid(enum v4l2_priority prio)
  198. {
  199. return prio == V4L2_PRIORITY_BACKGROUND ||
  200. prio == V4L2_PRIORITY_INTERACTIVE ||
  201. prio == V4L2_PRIORITY_RECORD;
  202. }
  203. void v4l2_prio_init(struct v4l2_prio_state *global)
  204. {
  205. memset(global, 0, sizeof(*global));
  206. }
  207. EXPORT_SYMBOL(v4l2_prio_init);
  208. int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
  209. enum v4l2_priority new)
  210. {
  211. if (!prio_is_valid(new))
  212. return -EINVAL;
  213. if (*local == new)
  214. return 0;
  215. atomic_inc(&global->prios[new]);
  216. if (prio_is_valid(*local))
  217. atomic_dec(&global->prios[*local]);
  218. *local = new;
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(v4l2_prio_change);
  222. void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
  223. {
  224. v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
  225. }
  226. EXPORT_SYMBOL(v4l2_prio_open);
  227. void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
  228. {
  229. if (prio_is_valid(local))
  230. atomic_dec(&global->prios[local]);
  231. }
  232. EXPORT_SYMBOL(v4l2_prio_close);
  233. enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
  234. {
  235. if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
  236. return V4L2_PRIORITY_RECORD;
  237. if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
  238. return V4L2_PRIORITY_INTERACTIVE;
  239. if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
  240. return V4L2_PRIORITY_BACKGROUND;
  241. return V4L2_PRIORITY_UNSET;
  242. }
  243. EXPORT_SYMBOL(v4l2_prio_max);
  244. int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
  245. {
  246. return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
  247. }
  248. EXPORT_SYMBOL(v4l2_prio_check);
  249. static ssize_t v4l2_read(struct file *filp, char __user *buf,
  250. size_t sz, loff_t *off)
  251. {
  252. struct video_device *vdev = video_devdata(filp);
  253. int ret = -ENODEV;
  254. if (!vdev->fops->read)
  255. return -EINVAL;
  256. if (video_is_registered(vdev))
  257. ret = vdev->fops->read(filp, buf, sz, off);
  258. if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
  259. (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
  260. printk(KERN_DEBUG "%s: read: %zd (%d)\n",
  261. video_device_node_name(vdev), sz, ret);
  262. return ret;
  263. }
  264. static ssize_t v4l2_write(struct file *filp, const char __user *buf,
  265. size_t sz, loff_t *off)
  266. {
  267. struct video_device *vdev = video_devdata(filp);
  268. int ret = -ENODEV;
  269. if (!vdev->fops->write)
  270. return -EINVAL;
  271. if (video_is_registered(vdev))
  272. ret = vdev->fops->write(filp, buf, sz, off);
  273. if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
  274. (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
  275. printk(KERN_DEBUG "%s: write: %zd (%d)\n",
  276. video_device_node_name(vdev), sz, ret);
  277. return ret;
  278. }
  279. static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
  280. {
  281. struct video_device *vdev = video_devdata(filp);
  282. unsigned int res = POLLERR | POLLHUP;
  283. if (!vdev->fops->poll)
  284. return DEFAULT_POLLMASK;
  285. if (video_is_registered(vdev))
  286. res = vdev->fops->poll(filp, poll);
  287. if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)
  288. printk(KERN_DEBUG "%s: poll: %08x\n",
  289. video_device_node_name(vdev), res);
  290. return res;
  291. }
  292. static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  293. {
  294. struct video_device *vdev = video_devdata(filp);
  295. int ret = -ENODEV;
  296. if (vdev->fops->unlocked_ioctl) {
  297. struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
  298. if (lock && mutex_lock_interruptible(lock))
  299. return -ERESTARTSYS;
  300. if (video_is_registered(vdev))
  301. ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
  302. if (lock)
  303. mutex_unlock(lock);
  304. } else
  305. ret = -ENOTTY;
  306. return ret;
  307. }
  308. #ifdef CONFIG_MMU
  309. #define v4l2_get_unmapped_area NULL
  310. #else
  311. static unsigned long v4l2_get_unmapped_area(struct file *filp,
  312. unsigned long addr, unsigned long len, unsigned long pgoff,
  313. unsigned long flags)
  314. {
  315. struct video_device *vdev = video_devdata(filp);
  316. int ret;
  317. if (!vdev->fops->get_unmapped_area)
  318. return -ENOSYS;
  319. if (!video_is_registered(vdev))
  320. return -ENODEV;
  321. ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
  322. if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
  323. printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n",
  324. video_device_node_name(vdev), ret);
  325. return ret;
  326. }
  327. #endif
  328. static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
  329. {
  330. struct video_device *vdev = video_devdata(filp);
  331. int ret = -ENODEV;
  332. if (!vdev->fops->mmap)
  333. return -ENODEV;
  334. if (video_is_registered(vdev))
  335. ret = vdev->fops->mmap(filp, vm);
  336. if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
  337. printk(KERN_DEBUG "%s: mmap (%d)\n",
  338. video_device_node_name(vdev), ret);
  339. return ret;
  340. }
  341. /* Override for the open function */
  342. static int v4l2_open(struct inode *inode, struct file *filp)
  343. {
  344. struct video_device *vdev;
  345. int ret = 0;
  346. /* Check if the video device is available */
  347. mutex_lock(&videodev_lock);
  348. vdev = video_devdata(filp);
  349. /* return ENODEV if the video device has already been removed. */
  350. if (vdev == NULL || !video_is_registered(vdev)) {
  351. mutex_unlock(&videodev_lock);
  352. return -ENODEV;
  353. }
  354. /* and increase the device refcount */
  355. video_get(vdev);
  356. mutex_unlock(&videodev_lock);
  357. if (vdev->fops->open) {
  358. if (video_is_registered(vdev))
  359. ret = vdev->fops->open(filp);
  360. else
  361. ret = -ENODEV;
  362. }
  363. if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
  364. printk(KERN_DEBUG "%s: open (%d)\n",
  365. video_device_node_name(vdev), ret);
  366. /* decrease the refcount in case of an error */
  367. if (ret)
  368. video_put(vdev);
  369. return ret;
  370. }
  371. /* Override for the release function */
  372. static int v4l2_release(struct inode *inode, struct file *filp)
  373. {
  374. struct video_device *vdev = video_devdata(filp);
  375. int ret = 0;
  376. if (vdev->fops->release)
  377. ret = vdev->fops->release(filp);
  378. if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
  379. printk(KERN_DEBUG "%s: release\n",
  380. video_device_node_name(vdev));
  381. /* decrease the refcount unconditionally since the release()
  382. return value is ignored. */
  383. video_put(vdev);
  384. return ret;
  385. }
  386. static const struct file_operations v4l2_fops = {
  387. .owner = THIS_MODULE,
  388. .read = v4l2_read,
  389. .write = v4l2_write,
  390. .open = v4l2_open,
  391. .get_unmapped_area = v4l2_get_unmapped_area,
  392. .mmap = v4l2_mmap,
  393. .unlocked_ioctl = v4l2_ioctl,
  394. #ifdef CONFIG_COMPAT
  395. .compat_ioctl = v4l2_compat_ioctl32,
  396. #endif
  397. .release = v4l2_release,
  398. .poll = v4l2_poll,
  399. .llseek = no_llseek,
  400. };
  401. /**
  402. * get_index - assign stream index number based on v4l2_dev
  403. * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
  404. *
  405. * Note that when this is called the new device has not yet been registered
  406. * in the video_device array, but it was able to obtain a minor number.
  407. *
  408. * This means that we can always obtain a free stream index number since
  409. * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
  410. * use of the video_device array.
  411. *
  412. * Returns a free index number.
  413. */
  414. static int get_index(struct video_device *vdev)
  415. {
  416. /* This can be static since this function is called with the global
  417. videodev_lock held. */
  418. static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
  419. int i;
  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]->v4l2_dev == vdev->v4l2_dev) {
  424. set_bit(video_device[i]->index, used);
  425. }
  426. }
  427. return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
  428. }
  429. #define SET_VALID_IOCTL(ops, cmd, op) \
  430. if (ops->op) \
  431. set_bit(_IOC_NR(cmd), valid_ioctls)
  432. /* This determines which ioctls are actually implemented in the driver.
  433. It's a one-time thing which simplifies video_ioctl2 as it can just do
  434. a bit test.
  435. Note that drivers can override this by setting bits to 1 in
  436. vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
  437. called, then that ioctl will actually be marked as unimplemented.
  438. It does that by first setting up the local valid_ioctls bitmap, and
  439. at the end do a:
  440. vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
  441. */
  442. static void determine_valid_ioctls(struct video_device *vdev)
  443. {
  444. DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
  445. const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
  446. bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER;
  447. bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
  448. bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
  449. bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
  450. bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH;
  451. bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
  452. bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
  453. bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
  454. /* vfl_type and vfl_dir independent ioctls */
  455. SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
  456. set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
  457. set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
  458. /* Note: the control handler can also be passed through the filehandle,
  459. and that can't be tested here. If the bit for these control ioctls
  460. is set, then the ioctl is valid. But if it is 0, then it can still
  461. be valid if the filehandle passed the control handler. */
  462. if (vdev->ctrl_handler || ops->vidioc_queryctrl)
  463. set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
  464. if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
  465. set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
  466. if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
  467. set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
  468. if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
  469. set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
  470. if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
  471. set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
  472. if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
  473. set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
  474. if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
  475. set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
  476. if (vdev->ctrl_handler || ops->vidioc_querymenu)
  477. set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
  478. SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
  479. SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
  480. SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
  481. #ifdef CONFIG_VIDEO_ADV_DEBUG
  482. set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
  483. set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
  484. set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
  485. #endif
  486. /* yes, really vidioc_subscribe_event */
  487. SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
  488. SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
  489. SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
  490. if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
  491. set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
  492. if (is_vid || is_tch) {
  493. /* video specific ioctls */
  494. if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
  495. ops->vidioc_enum_fmt_vid_cap_mplane ||
  496. ops->vidioc_enum_fmt_vid_overlay)) ||
  497. (is_tx && (ops->vidioc_enum_fmt_vid_out ||
  498. ops->vidioc_enum_fmt_vid_out_mplane)))
  499. set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
  500. if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
  501. ops->vidioc_g_fmt_vid_cap_mplane ||
  502. ops->vidioc_g_fmt_vid_overlay)) ||
  503. (is_tx && (ops->vidioc_g_fmt_vid_out ||
  504. ops->vidioc_g_fmt_vid_out_mplane ||
  505. ops->vidioc_g_fmt_vid_out_overlay)))
  506. set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
  507. if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
  508. ops->vidioc_s_fmt_vid_cap_mplane ||
  509. ops->vidioc_s_fmt_vid_overlay)) ||
  510. (is_tx && (ops->vidioc_s_fmt_vid_out ||
  511. ops->vidioc_s_fmt_vid_out_mplane ||
  512. ops->vidioc_s_fmt_vid_out_overlay)))
  513. set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
  514. if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
  515. ops->vidioc_try_fmt_vid_cap_mplane ||
  516. ops->vidioc_try_fmt_vid_overlay)) ||
  517. (is_tx && (ops->vidioc_try_fmt_vid_out ||
  518. ops->vidioc_try_fmt_vid_out_mplane ||
  519. ops->vidioc_try_fmt_vid_out_overlay)))
  520. set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
  521. SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
  522. SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
  523. SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
  524. SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
  525. SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
  526. SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
  527. SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
  528. SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
  529. SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
  530. SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
  531. SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
  532. SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
  533. if (ops->vidioc_g_crop || ops->vidioc_g_selection)
  534. set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
  535. if (ops->vidioc_s_crop || ops->vidioc_s_selection)
  536. set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
  537. SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
  538. SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
  539. if (ops->vidioc_cropcap || ops->vidioc_g_selection)
  540. set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
  541. } else if (is_vbi) {
  542. /* vbi specific ioctls */
  543. if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
  544. ops->vidioc_g_fmt_sliced_vbi_cap)) ||
  545. (is_tx && (ops->vidioc_g_fmt_vbi_out ||
  546. ops->vidioc_g_fmt_sliced_vbi_out)))
  547. set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
  548. if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
  549. ops->vidioc_s_fmt_sliced_vbi_cap)) ||
  550. (is_tx && (ops->vidioc_s_fmt_vbi_out ||
  551. ops->vidioc_s_fmt_sliced_vbi_out)))
  552. set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
  553. if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
  554. ops->vidioc_try_fmt_sliced_vbi_cap)) ||
  555. (is_tx && (ops->vidioc_try_fmt_vbi_out ||
  556. ops->vidioc_try_fmt_sliced_vbi_out)))
  557. set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
  558. SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
  559. } else if (is_sdr && is_rx) {
  560. /* SDR receiver specific ioctls */
  561. if (ops->vidioc_enum_fmt_sdr_cap)
  562. set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
  563. if (ops->vidioc_g_fmt_sdr_cap)
  564. set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
  565. if (ops->vidioc_s_fmt_sdr_cap)
  566. set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
  567. if (ops->vidioc_try_fmt_sdr_cap)
  568. set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
  569. } else if (is_sdr && is_tx) {
  570. /* SDR transmitter specific ioctls */
  571. if (ops->vidioc_enum_fmt_sdr_out)
  572. set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
  573. if (ops->vidioc_g_fmt_sdr_out)
  574. set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
  575. if (ops->vidioc_s_fmt_sdr_out)
  576. set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
  577. if (ops->vidioc_try_fmt_sdr_out)
  578. set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
  579. }
  580. if (is_vid || is_vbi || is_sdr || is_tch) {
  581. /* ioctls valid for video, vbi or sdr */
  582. SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
  583. SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
  584. SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
  585. SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
  586. SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
  587. SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
  588. SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
  589. SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
  590. SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
  591. }
  592. if (is_vid || is_vbi || is_tch) {
  593. /* ioctls valid for video or vbi */
  594. if (ops->vidioc_s_std)
  595. set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
  596. SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
  597. SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
  598. if (is_rx) {
  599. SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
  600. SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
  601. SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
  602. SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
  603. SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
  604. SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
  605. SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
  606. SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
  607. SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
  608. }
  609. if (is_tx) {
  610. SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
  611. SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
  612. SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
  613. SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
  614. SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
  615. SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
  616. }
  617. if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
  618. ops->vidioc_g_std))
  619. set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
  620. SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
  621. SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
  622. SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
  623. SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
  624. SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
  625. SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
  626. }
  627. if (is_tx && (is_radio || is_sdr)) {
  628. /* radio transmitter only ioctls */
  629. SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
  630. SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
  631. }
  632. if (is_rx) {
  633. /* receiver only ioctls */
  634. SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
  635. SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
  636. SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
  637. }
  638. bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
  639. BASE_VIDIOC_PRIVATE);
  640. }
  641. static int video_register_media_controller(struct video_device *vdev, int type)
  642. {
  643. #if defined(CONFIG_MEDIA_CONTROLLER)
  644. u32 intf_type;
  645. int ret;
  646. if (!vdev->v4l2_dev->mdev)
  647. return 0;
  648. vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
  649. vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
  650. switch (type) {
  651. case VFL_TYPE_GRABBER:
  652. intf_type = MEDIA_INTF_T_V4L_VIDEO;
  653. vdev->entity.function = MEDIA_ENT_F_IO_V4L;
  654. break;
  655. case VFL_TYPE_VBI:
  656. intf_type = MEDIA_INTF_T_V4L_VBI;
  657. vdev->entity.function = MEDIA_ENT_F_IO_VBI;
  658. break;
  659. case VFL_TYPE_SDR:
  660. intf_type = MEDIA_INTF_T_V4L_SWRADIO;
  661. vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO;
  662. break;
  663. case VFL_TYPE_TOUCH:
  664. intf_type = MEDIA_INTF_T_V4L_TOUCH;
  665. vdev->entity.function = MEDIA_ENT_F_IO_V4L;
  666. break;
  667. case VFL_TYPE_RADIO:
  668. intf_type = MEDIA_INTF_T_V4L_RADIO;
  669. /*
  670. * Radio doesn't have an entity at the V4L2 side to represent
  671. * radio input or output. Instead, the audio input/output goes
  672. * via either physical wires or ALSA.
  673. */
  674. break;
  675. case VFL_TYPE_SUBDEV:
  676. intf_type = MEDIA_INTF_T_V4L_SUBDEV;
  677. /* Entity will be created via v4l2_device_register_subdev() */
  678. break;
  679. default:
  680. return 0;
  681. }
  682. if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
  683. vdev->entity.name = vdev->name;
  684. /* Needed just for backward compatibility with legacy MC API */
  685. vdev->entity.info.dev.major = VIDEO_MAJOR;
  686. vdev->entity.info.dev.minor = vdev->minor;
  687. ret = media_device_register_entity(vdev->v4l2_dev->mdev,
  688. &vdev->entity);
  689. if (ret < 0) {
  690. printk(KERN_WARNING
  691. "%s: media_device_register_entity failed\n",
  692. __func__);
  693. return ret;
  694. }
  695. }
  696. vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev,
  697. intf_type,
  698. 0, VIDEO_MAJOR,
  699. vdev->minor);
  700. if (!vdev->intf_devnode) {
  701. media_device_unregister_entity(&vdev->entity);
  702. return -ENOMEM;
  703. }
  704. if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
  705. struct media_link *link;
  706. link = media_create_intf_link(&vdev->entity,
  707. &vdev->intf_devnode->intf,
  708. MEDIA_LNK_FL_ENABLED);
  709. if (!link) {
  710. media_devnode_remove(vdev->intf_devnode);
  711. media_device_unregister_entity(&vdev->entity);
  712. return -ENOMEM;
  713. }
  714. }
  715. /* FIXME: how to create the other interface links? */
  716. #endif
  717. return 0;
  718. }
  719. int __video_register_device(struct video_device *vdev, int type, int nr,
  720. int warn_if_nr_in_use, struct module *owner)
  721. {
  722. int i = 0;
  723. int ret;
  724. int minor_offset = 0;
  725. int minor_cnt = VIDEO_NUM_DEVICES;
  726. const char *name_base;
  727. /* A minor value of -1 marks this video device as never
  728. having been registered */
  729. vdev->minor = -1;
  730. /* the release callback MUST be present */
  731. if (WARN_ON(!vdev->release))
  732. return -EINVAL;
  733. /* the v4l2_dev pointer MUST be present */
  734. if (WARN_ON(!vdev->v4l2_dev))
  735. return -EINVAL;
  736. /* v4l2_fh support */
  737. spin_lock_init(&vdev->fh_lock);
  738. INIT_LIST_HEAD(&vdev->fh_list);
  739. /* Part 1: check device type */
  740. switch (type) {
  741. case VFL_TYPE_GRABBER:
  742. name_base = "video";
  743. break;
  744. case VFL_TYPE_VBI:
  745. name_base = "vbi";
  746. break;
  747. case VFL_TYPE_RADIO:
  748. name_base = "radio";
  749. break;
  750. case VFL_TYPE_SUBDEV:
  751. name_base = "v4l-subdev";
  752. break;
  753. case VFL_TYPE_SDR:
  754. /* Use device name 'swradio' because 'sdr' was already taken. */
  755. name_base = "swradio";
  756. break;
  757. case VFL_TYPE_TOUCH:
  758. name_base = "v4l-touch";
  759. break;
  760. default:
  761. printk(KERN_ERR "%s called with unknown type: %d\n",
  762. __func__, type);
  763. return -EINVAL;
  764. }
  765. vdev->vfl_type = type;
  766. vdev->cdev = NULL;
  767. if (vdev->dev_parent == NULL)
  768. vdev->dev_parent = vdev->v4l2_dev->dev;
  769. if (vdev->ctrl_handler == NULL)
  770. vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
  771. /* If the prio state pointer is NULL, then use the v4l2_device
  772. prio state. */
  773. if (vdev->prio == NULL)
  774. vdev->prio = &vdev->v4l2_dev->prio;
  775. /* Part 2: find a free minor, device node number and device index. */
  776. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  777. /* Keep the ranges for the first four types for historical
  778. * reasons.
  779. * Newer devices (not yet in place) should use the range
  780. * of 128-191 and just pick the first free minor there
  781. * (new style). */
  782. switch (type) {
  783. case VFL_TYPE_GRABBER:
  784. minor_offset = 0;
  785. minor_cnt = 64;
  786. break;
  787. case VFL_TYPE_RADIO:
  788. minor_offset = 64;
  789. minor_cnt = 64;
  790. break;
  791. case VFL_TYPE_VBI:
  792. minor_offset = 224;
  793. minor_cnt = 32;
  794. break;
  795. default:
  796. minor_offset = 128;
  797. minor_cnt = 64;
  798. break;
  799. }
  800. #endif
  801. /* Pick a device node number */
  802. mutex_lock(&videodev_lock);
  803. nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
  804. if (nr == minor_cnt)
  805. nr = devnode_find(vdev, 0, minor_cnt);
  806. if (nr == minor_cnt) {
  807. printk(KERN_ERR "could not get a free device node number\n");
  808. mutex_unlock(&videodev_lock);
  809. return -ENFILE;
  810. }
  811. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  812. /* 1-on-1 mapping of device node number to minor number */
  813. i = nr;
  814. #else
  815. /* The device node number and minor numbers are independent, so
  816. we just find the first free minor number. */
  817. for (i = 0; i < VIDEO_NUM_DEVICES; i++)
  818. if (video_device[i] == NULL)
  819. break;
  820. if (i == VIDEO_NUM_DEVICES) {
  821. mutex_unlock(&videodev_lock);
  822. printk(KERN_ERR "could not get a free minor\n");
  823. return -ENFILE;
  824. }
  825. #endif
  826. vdev->minor = i + minor_offset;
  827. vdev->num = nr;
  828. devnode_set(vdev);
  829. /* Should not happen since we thought this minor was free */
  830. WARN_ON(video_device[vdev->minor] != NULL);
  831. vdev->index = get_index(vdev);
  832. video_device[vdev->minor] = vdev;
  833. mutex_unlock(&videodev_lock);
  834. if (vdev->ioctl_ops)
  835. determine_valid_ioctls(vdev);
  836. /* Part 3: Initialize the character device */
  837. vdev->cdev = cdev_alloc();
  838. if (vdev->cdev == NULL) {
  839. ret = -ENOMEM;
  840. goto cleanup;
  841. }
  842. vdev->cdev->ops = &v4l2_fops;
  843. vdev->cdev->owner = owner;
  844. ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
  845. if (ret < 0) {
  846. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  847. kfree(vdev->cdev);
  848. vdev->cdev = NULL;
  849. goto cleanup;
  850. }
  851. /* Part 4: register the device with sysfs */
  852. vdev->dev.class = &video_class;
  853. vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
  854. vdev->dev.parent = vdev->dev_parent;
  855. dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
  856. ret = device_register(&vdev->dev);
  857. if (ret < 0) {
  858. printk(KERN_ERR "%s: device_register failed\n", __func__);
  859. goto cleanup;
  860. }
  861. /* Register the release callback that will be called when the last
  862. reference to the device goes away. */
  863. vdev->dev.release = v4l2_device_release;
  864. if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
  865. printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
  866. name_base, nr, video_device_node_name(vdev));
  867. /* Increase v4l2_device refcount */
  868. v4l2_device_get(vdev->v4l2_dev);
  869. /* Part 5: Register the entity. */
  870. ret = video_register_media_controller(vdev, type);
  871. /* Part 6: Activate this minor. The char device can now be used. */
  872. set_bit(V4L2_FL_REGISTERED, &vdev->flags);
  873. return 0;
  874. cleanup:
  875. mutex_lock(&videodev_lock);
  876. if (vdev->cdev)
  877. cdev_del(vdev->cdev);
  878. video_device[vdev->minor] = NULL;
  879. devnode_clear(vdev);
  880. mutex_unlock(&videodev_lock);
  881. /* Mark this video device as never having been registered. */
  882. vdev->minor = -1;
  883. return ret;
  884. }
  885. EXPORT_SYMBOL(__video_register_device);
  886. /**
  887. * video_unregister_device - unregister a video4linux device
  888. * @vdev: the device to unregister
  889. *
  890. * This unregisters the passed device. Future open calls will
  891. * be met with errors.
  892. */
  893. void video_unregister_device(struct video_device *vdev)
  894. {
  895. /* Check if vdev was ever registered at all */
  896. if (!vdev || !video_is_registered(vdev))
  897. return;
  898. mutex_lock(&videodev_lock);
  899. /* This must be in a critical section to prevent a race with v4l2_open.
  900. * Once this bit has been cleared video_get may never be called again.
  901. */
  902. clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
  903. mutex_unlock(&videodev_lock);
  904. device_unregister(&vdev->dev);
  905. }
  906. EXPORT_SYMBOL(video_unregister_device);
  907. /*
  908. * Initialise video for linux
  909. */
  910. static int __init videodev_init(void)
  911. {
  912. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  913. int ret;
  914. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  915. ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
  916. if (ret < 0) {
  917. printk(KERN_WARNING "videodev: unable to get major %d\n",
  918. VIDEO_MAJOR);
  919. return ret;
  920. }
  921. ret = class_register(&video_class);
  922. if (ret < 0) {
  923. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  924. printk(KERN_WARNING "video_dev: class_register failed\n");
  925. return -EIO;
  926. }
  927. return 0;
  928. }
  929. static void __exit videodev_exit(void)
  930. {
  931. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  932. class_unregister(&video_class);
  933. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  934. }
  935. subsys_initcall(videodev_init);
  936. module_exit(videodev_exit)
  937. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  938. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  939. MODULE_LICENSE("GPL");
  940. MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);