v4l2-dev.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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 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->debug);
  48. }
  49. static ssize_t 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->debug = value;
  59. return len;
  60. }
  61. static DEVICE_ATTR_RW(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_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 && v4l2_dev->mdev &&
  164. vdev->vfl_type != VFL_TYPE_SUBDEV)
  165. media_device_unregister_entity(&vdev->entity);
  166. #endif
  167. /* Do not call v4l2_device_put if there is no release callback set.
  168. * Drivers that have no v4l2_device release callback might free the
  169. * v4l2_dev instance in the video_device release callback below, so we
  170. * must perform this check here.
  171. *
  172. * TODO: In the long run all drivers that use v4l2_device should use the
  173. * v4l2_device release callback. This check will then be unnecessary.
  174. */
  175. if (v4l2_dev && v4l2_dev->release == NULL)
  176. v4l2_dev = NULL;
  177. /* Release video_device and perform other
  178. cleanups as needed. */
  179. vdev->release(vdev);
  180. /* Decrease v4l2_device refcount */
  181. if (v4l2_dev)
  182. v4l2_device_put(v4l2_dev);
  183. }
  184. static struct class video_class = {
  185. .name = VIDEO_NAME,
  186. .dev_groups = video_device_groups,
  187. };
  188. struct video_device *video_devdata(struct file *file)
  189. {
  190. return video_device[iminor(file_inode(file))];
  191. }
  192. EXPORT_SYMBOL(video_devdata);
  193. /* Priority handling */
  194. static inline bool prio_is_valid(enum v4l2_priority prio)
  195. {
  196. return prio == V4L2_PRIORITY_BACKGROUND ||
  197. prio == V4L2_PRIORITY_INTERACTIVE ||
  198. prio == V4L2_PRIORITY_RECORD;
  199. }
  200. void v4l2_prio_init(struct v4l2_prio_state *global)
  201. {
  202. memset(global, 0, sizeof(*global));
  203. }
  204. EXPORT_SYMBOL(v4l2_prio_init);
  205. int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
  206. enum v4l2_priority new)
  207. {
  208. if (!prio_is_valid(new))
  209. return -EINVAL;
  210. if (*local == new)
  211. return 0;
  212. atomic_inc(&global->prios[new]);
  213. if (prio_is_valid(*local))
  214. atomic_dec(&global->prios[*local]);
  215. *local = new;
  216. return 0;
  217. }
  218. EXPORT_SYMBOL(v4l2_prio_change);
  219. void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
  220. {
  221. v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
  222. }
  223. EXPORT_SYMBOL(v4l2_prio_open);
  224. void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
  225. {
  226. if (prio_is_valid(local))
  227. atomic_dec(&global->prios[local]);
  228. }
  229. EXPORT_SYMBOL(v4l2_prio_close);
  230. enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
  231. {
  232. if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
  233. return V4L2_PRIORITY_RECORD;
  234. if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
  235. return V4L2_PRIORITY_INTERACTIVE;
  236. if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
  237. return V4L2_PRIORITY_BACKGROUND;
  238. return V4L2_PRIORITY_UNSET;
  239. }
  240. EXPORT_SYMBOL(v4l2_prio_max);
  241. int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
  242. {
  243. return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
  244. }
  245. EXPORT_SYMBOL(v4l2_prio_check);
  246. static ssize_t v4l2_read(struct file *filp, char __user *buf,
  247. size_t sz, loff_t *off)
  248. {
  249. struct video_device *vdev = video_devdata(filp);
  250. int ret = -ENODEV;
  251. if (!vdev->fops->read)
  252. return -EINVAL;
  253. if (video_is_registered(vdev))
  254. ret = vdev->fops->read(filp, buf, sz, off);
  255. if (vdev->debug)
  256. printk(KERN_DEBUG "%s: read: %zd (%d)\n",
  257. video_device_node_name(vdev), sz, ret);
  258. return ret;
  259. }
  260. static ssize_t v4l2_write(struct file *filp, const char __user *buf,
  261. size_t sz, loff_t *off)
  262. {
  263. struct video_device *vdev = video_devdata(filp);
  264. int ret = -ENODEV;
  265. if (!vdev->fops->write)
  266. return -EINVAL;
  267. if (video_is_registered(vdev))
  268. ret = vdev->fops->write(filp, buf, sz, off);
  269. if (vdev->debug)
  270. printk(KERN_DEBUG "%s: write: %zd (%d)\n",
  271. video_device_node_name(vdev), sz, ret);
  272. return ret;
  273. }
  274. static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
  275. {
  276. struct video_device *vdev = video_devdata(filp);
  277. unsigned int res = POLLERR | POLLHUP;
  278. if (!vdev->fops->poll)
  279. return DEFAULT_POLLMASK;
  280. if (video_is_registered(vdev))
  281. res = vdev->fops->poll(filp, poll);
  282. if (vdev->debug)
  283. printk(KERN_DEBUG "%s: poll: %08x\n",
  284. video_device_node_name(vdev), res);
  285. return res;
  286. }
  287. static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  288. {
  289. struct video_device *vdev = video_devdata(filp);
  290. int ret = -ENODEV;
  291. if (vdev->fops->unlocked_ioctl) {
  292. struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
  293. if (lock && mutex_lock_interruptible(lock))
  294. return -ERESTARTSYS;
  295. if (video_is_registered(vdev))
  296. ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
  297. if (lock)
  298. mutex_unlock(lock);
  299. } else if (vdev->fops->ioctl) {
  300. /* This code path is a replacement for the BKL. It is a major
  301. * hack but it will have to do for those drivers that are not
  302. * yet converted to use unlocked_ioctl.
  303. *
  304. * There are two options: if the driver implements struct
  305. * v4l2_device, then the lock defined there is used to
  306. * serialize the ioctls. Otherwise the v4l2 core lock defined
  307. * below is used. This lock is really bad since it serializes
  308. * completely independent devices.
  309. *
  310. * Both variants suffer from the same problem: if the driver
  311. * sleeps, then it blocks all ioctls since the lock is still
  312. * held. This is very common for VIDIOC_DQBUF since that
  313. * normally waits for a frame to arrive. As a result any other
  314. * ioctl calls will proceed very, very slowly since each call
  315. * will have to wait for the VIDIOC_QBUF to finish. Things that
  316. * should take 0.01s may now take 10-20 seconds.
  317. *
  318. * The workaround is to *not* take the lock for VIDIOC_DQBUF.
  319. * This actually works OK for videobuf-based drivers, since
  320. * videobuf will take its own internal lock.
  321. */
  322. static DEFINE_MUTEX(v4l2_ioctl_mutex);
  323. struct mutex *m = vdev->v4l2_dev ?
  324. &vdev->v4l2_dev->ioctl_lock : &v4l2_ioctl_mutex;
  325. if (cmd != VIDIOC_DQBUF && mutex_lock_interruptible(m))
  326. return -ERESTARTSYS;
  327. if (video_is_registered(vdev))
  328. ret = vdev->fops->ioctl(filp, cmd, arg);
  329. if (cmd != VIDIOC_DQBUF)
  330. mutex_unlock(m);
  331. } else
  332. ret = -ENOTTY;
  333. return ret;
  334. }
  335. #ifdef CONFIG_MMU
  336. #define v4l2_get_unmapped_area NULL
  337. #else
  338. static unsigned long v4l2_get_unmapped_area(struct file *filp,
  339. unsigned long addr, unsigned long len, unsigned long pgoff,
  340. unsigned long flags)
  341. {
  342. struct video_device *vdev = video_devdata(filp);
  343. int ret;
  344. if (!vdev->fops->get_unmapped_area)
  345. return -ENOSYS;
  346. if (!video_is_registered(vdev))
  347. return -ENODEV;
  348. ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
  349. if (vdev->debug)
  350. printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n",
  351. video_device_node_name(vdev), ret);
  352. return ret;
  353. }
  354. #endif
  355. static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
  356. {
  357. struct video_device *vdev = video_devdata(filp);
  358. int ret = -ENODEV;
  359. if (!vdev->fops->mmap)
  360. return -ENODEV;
  361. if (video_is_registered(vdev))
  362. ret = vdev->fops->mmap(filp, vm);
  363. if (vdev->debug)
  364. printk(KERN_DEBUG "%s: mmap (%d)\n",
  365. video_device_node_name(vdev), ret);
  366. return ret;
  367. }
  368. /* Override for the open function */
  369. static int v4l2_open(struct inode *inode, struct file *filp)
  370. {
  371. struct video_device *vdev;
  372. int ret = 0;
  373. /* Check if the video device is available */
  374. mutex_lock(&videodev_lock);
  375. vdev = video_devdata(filp);
  376. /* return ENODEV if the video device has already been removed. */
  377. if (vdev == NULL || !video_is_registered(vdev)) {
  378. mutex_unlock(&videodev_lock);
  379. return -ENODEV;
  380. }
  381. /* and increase the device refcount */
  382. video_get(vdev);
  383. mutex_unlock(&videodev_lock);
  384. if (vdev->fops->open) {
  385. if (video_is_registered(vdev))
  386. ret = vdev->fops->open(filp);
  387. else
  388. ret = -ENODEV;
  389. }
  390. if (vdev->debug)
  391. printk(KERN_DEBUG "%s: open (%d)\n",
  392. video_device_node_name(vdev), ret);
  393. /* decrease the refcount in case of an error */
  394. if (ret)
  395. video_put(vdev);
  396. return ret;
  397. }
  398. /* Override for the release function */
  399. static int v4l2_release(struct inode *inode, struct file *filp)
  400. {
  401. struct video_device *vdev = video_devdata(filp);
  402. int ret = 0;
  403. if (vdev->fops->release)
  404. ret = vdev->fops->release(filp);
  405. if (vdev->debug)
  406. printk(KERN_DEBUG "%s: release\n",
  407. video_device_node_name(vdev));
  408. /* decrease the refcount unconditionally since the release()
  409. return value is ignored. */
  410. video_put(vdev);
  411. return ret;
  412. }
  413. static const struct file_operations v4l2_fops = {
  414. .owner = THIS_MODULE,
  415. .read = v4l2_read,
  416. .write = v4l2_write,
  417. .open = v4l2_open,
  418. .get_unmapped_area = v4l2_get_unmapped_area,
  419. .mmap = v4l2_mmap,
  420. .unlocked_ioctl = v4l2_ioctl,
  421. #ifdef CONFIG_COMPAT
  422. .compat_ioctl = v4l2_compat_ioctl32,
  423. #endif
  424. .release = v4l2_release,
  425. .poll = v4l2_poll,
  426. .llseek = no_llseek,
  427. };
  428. /**
  429. * get_index - assign stream index number based on v4l2_dev
  430. * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
  431. *
  432. * Note that when this is called the new device has not yet been registered
  433. * in the video_device array, but it was able to obtain a minor number.
  434. *
  435. * This means that we can always obtain a free stream index number since
  436. * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
  437. * use of the video_device array.
  438. *
  439. * Returns a free index number.
  440. */
  441. static int get_index(struct video_device *vdev)
  442. {
  443. /* This can be static since this function is called with the global
  444. videodev_lock held. */
  445. static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
  446. int i;
  447. bitmap_zero(used, VIDEO_NUM_DEVICES);
  448. for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
  449. if (video_device[i] != NULL &&
  450. video_device[i]->v4l2_dev == vdev->v4l2_dev) {
  451. set_bit(video_device[i]->index, used);
  452. }
  453. }
  454. return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
  455. }
  456. #define SET_VALID_IOCTL(ops, cmd, op) \
  457. if (ops->op) \
  458. set_bit(_IOC_NR(cmd), valid_ioctls)
  459. /* This determines which ioctls are actually implemented in the driver.
  460. It's a one-time thing which simplifies video_ioctl2 as it can just do
  461. a bit test.
  462. Note that drivers can override this by setting bits to 1 in
  463. vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
  464. called, then that ioctl will actually be marked as unimplemented.
  465. It does that by first setting up the local valid_ioctls bitmap, and
  466. at the end do a:
  467. vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
  468. */
  469. static void determine_valid_ioctls(struct video_device *vdev)
  470. {
  471. DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
  472. const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
  473. bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER;
  474. bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
  475. bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
  476. bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
  477. bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
  478. bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
  479. /* vfl_type and vfl_dir independent ioctls */
  480. SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
  481. if (ops->vidioc_g_priority ||
  482. test_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags))
  483. set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
  484. if (ops->vidioc_s_priority ||
  485. test_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags))
  486. set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
  487. SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
  488. SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
  489. /* Note: the control handler can also be passed through the filehandle,
  490. and that can't be tested here. If the bit for these control ioctls
  491. is set, then the ioctl is valid. But if it is 0, then it can still
  492. be valid if the filehandle passed the control handler. */
  493. if (vdev->ctrl_handler || ops->vidioc_queryctrl)
  494. set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
  495. if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
  496. set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
  497. if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
  498. set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
  499. if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
  500. set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
  501. if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
  502. set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
  503. if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
  504. set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
  505. if (vdev->ctrl_handler || ops->vidioc_querymenu)
  506. set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
  507. SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
  508. SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
  509. SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
  510. #ifdef CONFIG_VIDEO_ADV_DEBUG
  511. set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
  512. set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
  513. set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
  514. #endif
  515. /* yes, really vidioc_subscribe_event */
  516. SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
  517. SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
  518. SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
  519. if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
  520. set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
  521. if (is_vid) {
  522. /* video specific ioctls */
  523. if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
  524. ops->vidioc_enum_fmt_vid_cap_mplane ||
  525. ops->vidioc_enum_fmt_vid_overlay)) ||
  526. (is_tx && (ops->vidioc_enum_fmt_vid_out ||
  527. ops->vidioc_enum_fmt_vid_out_mplane)))
  528. set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
  529. if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
  530. ops->vidioc_g_fmt_vid_cap_mplane ||
  531. ops->vidioc_g_fmt_vid_overlay)) ||
  532. (is_tx && (ops->vidioc_g_fmt_vid_out ||
  533. ops->vidioc_g_fmt_vid_out_mplane ||
  534. ops->vidioc_g_fmt_vid_out_overlay)))
  535. set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
  536. if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
  537. ops->vidioc_s_fmt_vid_cap_mplane ||
  538. ops->vidioc_s_fmt_vid_overlay)) ||
  539. (is_tx && (ops->vidioc_s_fmt_vid_out ||
  540. ops->vidioc_s_fmt_vid_out_mplane ||
  541. ops->vidioc_s_fmt_vid_out_overlay)))
  542. set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
  543. if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
  544. ops->vidioc_try_fmt_vid_cap_mplane ||
  545. ops->vidioc_try_fmt_vid_overlay)) ||
  546. (is_tx && (ops->vidioc_try_fmt_vid_out ||
  547. ops->vidioc_try_fmt_vid_out_mplane ||
  548. ops->vidioc_try_fmt_vid_out_overlay)))
  549. set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
  550. SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
  551. SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
  552. SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
  553. SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
  554. SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
  555. SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
  556. SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
  557. SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
  558. SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
  559. SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
  560. SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
  561. SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
  562. } else if (is_vbi) {
  563. /* vbi specific ioctls */
  564. if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
  565. ops->vidioc_g_fmt_sliced_vbi_cap)) ||
  566. (is_tx && (ops->vidioc_g_fmt_vbi_out ||
  567. ops->vidioc_g_fmt_sliced_vbi_out)))
  568. set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
  569. if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
  570. ops->vidioc_s_fmt_sliced_vbi_cap)) ||
  571. (is_tx && (ops->vidioc_s_fmt_vbi_out ||
  572. ops->vidioc_s_fmt_sliced_vbi_out)))
  573. set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
  574. if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
  575. ops->vidioc_try_fmt_sliced_vbi_cap)) ||
  576. (is_tx && (ops->vidioc_try_fmt_vbi_out ||
  577. ops->vidioc_try_fmt_sliced_vbi_out)))
  578. set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
  579. SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
  580. }
  581. if (!is_radio) {
  582. /* ioctls valid for video or vbi */
  583. SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
  584. SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
  585. SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
  586. SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
  587. SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
  588. SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
  589. SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
  590. if (ops->vidioc_s_std)
  591. set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
  592. SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
  593. SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
  594. if (is_rx) {
  595. SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
  596. SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
  597. SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
  598. SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
  599. SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
  600. SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
  601. SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
  602. SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
  603. }
  604. if (is_tx) {
  605. SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
  606. SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
  607. SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
  608. SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
  609. SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
  610. SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
  611. }
  612. if (ops->vidioc_g_crop || ops->vidioc_g_selection)
  613. set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
  614. if (ops->vidioc_s_crop || ops->vidioc_s_selection)
  615. set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
  616. SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
  617. SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
  618. if (ops->vidioc_cropcap || ops->vidioc_g_selection)
  619. set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
  620. if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
  621. ops->vidioc_g_std))
  622. set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
  623. SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
  624. SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
  625. SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
  626. SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
  627. SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
  628. }
  629. if (is_tx) {
  630. /* transmitter only ioctls */
  631. SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
  632. SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
  633. }
  634. if (is_rx) {
  635. /* receiver only ioctls */
  636. SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
  637. SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
  638. SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
  639. }
  640. bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
  641. BASE_VIDIOC_PRIVATE);
  642. }
  643. /**
  644. * __video_register_device - register video4linux devices
  645. * @vdev: video device structure we want to register
  646. * @type: type of device to register
  647. * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
  648. * -1 == first free)
  649. * @warn_if_nr_in_use: warn if the desired device node number
  650. * was already in use and another number was chosen instead.
  651. * @owner: module that owns the video device node
  652. *
  653. * The registration code assigns minor numbers and device node numbers
  654. * based on the requested type and registers the new device node with
  655. * the kernel.
  656. *
  657. * This function assumes that struct video_device was zeroed when it
  658. * was allocated and does not contain any stale date.
  659. *
  660. * An error is returned if no free minor or device node number could be
  661. * found, or if the registration of the device node failed.
  662. *
  663. * Zero is returned on success.
  664. *
  665. * Valid types are
  666. *
  667. * %VFL_TYPE_GRABBER - A frame grabber
  668. *
  669. * %VFL_TYPE_VBI - Vertical blank data (undecoded)
  670. *
  671. * %VFL_TYPE_RADIO - A radio card
  672. *
  673. * %VFL_TYPE_SUBDEV - A subdevice
  674. */
  675. int __video_register_device(struct video_device *vdev, int type, int nr,
  676. int warn_if_nr_in_use, struct module *owner)
  677. {
  678. int i = 0;
  679. int ret;
  680. int minor_offset = 0;
  681. int minor_cnt = VIDEO_NUM_DEVICES;
  682. const char *name_base;
  683. /* A minor value of -1 marks this video device as never
  684. having been registered */
  685. vdev->minor = -1;
  686. /* the release callback MUST be present */
  687. if (WARN_ON(!vdev->release))
  688. return -EINVAL;
  689. /* the v4l2_dev pointer MUST be present */
  690. if (WARN_ON(!vdev->v4l2_dev))
  691. return -EINVAL;
  692. /* v4l2_fh support */
  693. spin_lock_init(&vdev->fh_lock);
  694. INIT_LIST_HEAD(&vdev->fh_list);
  695. /* Part 1: check device type */
  696. switch (type) {
  697. case VFL_TYPE_GRABBER:
  698. name_base = "video";
  699. break;
  700. case VFL_TYPE_VBI:
  701. name_base = "vbi";
  702. break;
  703. case VFL_TYPE_RADIO:
  704. name_base = "radio";
  705. break;
  706. case VFL_TYPE_SUBDEV:
  707. name_base = "v4l-subdev";
  708. break;
  709. default:
  710. printk(KERN_ERR "%s called with unknown type: %d\n",
  711. __func__, type);
  712. return -EINVAL;
  713. }
  714. vdev->vfl_type = type;
  715. vdev->cdev = NULL;
  716. if (vdev->dev_parent == NULL)
  717. vdev->dev_parent = vdev->v4l2_dev->dev;
  718. if (vdev->ctrl_handler == NULL)
  719. vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
  720. /* If the prio state pointer is NULL, then use the v4l2_device
  721. prio state. */
  722. if (vdev->prio == NULL)
  723. vdev->prio = &vdev->v4l2_dev->prio;
  724. /* Part 2: find a free minor, device node number and device index. */
  725. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  726. /* Keep the ranges for the first four types for historical
  727. * reasons.
  728. * Newer devices (not yet in place) should use the range
  729. * of 128-191 and just pick the first free minor there
  730. * (new style). */
  731. switch (type) {
  732. case VFL_TYPE_GRABBER:
  733. minor_offset = 0;
  734. minor_cnt = 64;
  735. break;
  736. case VFL_TYPE_RADIO:
  737. minor_offset = 64;
  738. minor_cnt = 64;
  739. break;
  740. case VFL_TYPE_VBI:
  741. minor_offset = 224;
  742. minor_cnt = 32;
  743. break;
  744. default:
  745. minor_offset = 128;
  746. minor_cnt = 64;
  747. break;
  748. }
  749. #endif
  750. /* Pick a device node number */
  751. mutex_lock(&videodev_lock);
  752. nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
  753. if (nr == minor_cnt)
  754. nr = devnode_find(vdev, 0, minor_cnt);
  755. if (nr == minor_cnt) {
  756. printk(KERN_ERR "could not get a free device node number\n");
  757. mutex_unlock(&videodev_lock);
  758. return -ENFILE;
  759. }
  760. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  761. /* 1-on-1 mapping of device node number to minor number */
  762. i = nr;
  763. #else
  764. /* The device node number and minor numbers are independent, so
  765. we just find the first free minor number. */
  766. for (i = 0; i < VIDEO_NUM_DEVICES; i++)
  767. if (video_device[i] == NULL)
  768. break;
  769. if (i == VIDEO_NUM_DEVICES) {
  770. mutex_unlock(&videodev_lock);
  771. printk(KERN_ERR "could not get a free minor\n");
  772. return -ENFILE;
  773. }
  774. #endif
  775. vdev->minor = i + minor_offset;
  776. vdev->num = nr;
  777. devnode_set(vdev);
  778. /* Should not happen since we thought this minor was free */
  779. WARN_ON(video_device[vdev->minor] != NULL);
  780. vdev->index = get_index(vdev);
  781. mutex_unlock(&videodev_lock);
  782. if (vdev->ioctl_ops)
  783. determine_valid_ioctls(vdev);
  784. /* Part 3: Initialize the character device */
  785. vdev->cdev = cdev_alloc();
  786. if (vdev->cdev == NULL) {
  787. ret = -ENOMEM;
  788. goto cleanup;
  789. }
  790. vdev->cdev->ops = &v4l2_fops;
  791. vdev->cdev->owner = owner;
  792. ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
  793. if (ret < 0) {
  794. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  795. kfree(vdev->cdev);
  796. vdev->cdev = NULL;
  797. goto cleanup;
  798. }
  799. /* Part 4: register the device with sysfs */
  800. vdev->dev.class = &video_class;
  801. vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
  802. vdev->dev.parent = vdev->dev_parent;
  803. dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
  804. ret = device_register(&vdev->dev);
  805. if (ret < 0) {
  806. printk(KERN_ERR "%s: device_register failed\n", __func__);
  807. goto cleanup;
  808. }
  809. /* Register the release callback that will be called when the last
  810. reference to the device goes away. */
  811. vdev->dev.release = v4l2_device_release;
  812. if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
  813. printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
  814. name_base, nr, video_device_node_name(vdev));
  815. /* Increase v4l2_device refcount */
  816. if (vdev->v4l2_dev)
  817. v4l2_device_get(vdev->v4l2_dev);
  818. #if defined(CONFIG_MEDIA_CONTROLLER)
  819. /* Part 5: Register the entity. */
  820. if (vdev->v4l2_dev && vdev->v4l2_dev->mdev &&
  821. vdev->vfl_type != VFL_TYPE_SUBDEV) {
  822. vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
  823. vdev->entity.name = vdev->name;
  824. vdev->entity.info.v4l.major = VIDEO_MAJOR;
  825. vdev->entity.info.v4l.minor = vdev->minor;
  826. ret = media_device_register_entity(vdev->v4l2_dev->mdev,
  827. &vdev->entity);
  828. if (ret < 0)
  829. printk(KERN_WARNING
  830. "%s: media_device_register_entity failed\n",
  831. __func__);
  832. }
  833. #endif
  834. /* Part 6: Activate this minor. The char device can now be used. */
  835. set_bit(V4L2_FL_REGISTERED, &vdev->flags);
  836. mutex_lock(&videodev_lock);
  837. video_device[vdev->minor] = vdev;
  838. mutex_unlock(&videodev_lock);
  839. return 0;
  840. cleanup:
  841. mutex_lock(&videodev_lock);
  842. if (vdev->cdev)
  843. cdev_del(vdev->cdev);
  844. devnode_clear(vdev);
  845. mutex_unlock(&videodev_lock);
  846. /* Mark this video device as never having been registered. */
  847. vdev->minor = -1;
  848. return ret;
  849. }
  850. EXPORT_SYMBOL(__video_register_device);
  851. /**
  852. * video_unregister_device - unregister a video4linux device
  853. * @vdev: the device to unregister
  854. *
  855. * This unregisters the passed device. Future open calls will
  856. * be met with errors.
  857. */
  858. void video_unregister_device(struct video_device *vdev)
  859. {
  860. /* Check if vdev was ever registered at all */
  861. if (!vdev || !video_is_registered(vdev))
  862. return;
  863. mutex_lock(&videodev_lock);
  864. /* This must be in a critical section to prevent a race with v4l2_open.
  865. * Once this bit has been cleared video_get may never be called again.
  866. */
  867. clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
  868. mutex_unlock(&videodev_lock);
  869. device_unregister(&vdev->dev);
  870. }
  871. EXPORT_SYMBOL(video_unregister_device);
  872. /*
  873. * Initialise video for linux
  874. */
  875. static int __init videodev_init(void)
  876. {
  877. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  878. int ret;
  879. printk(KERN_INFO "Linux video capture interface: v2.00\n");
  880. ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
  881. if (ret < 0) {
  882. printk(KERN_WARNING "videodev: unable to get major %d\n",
  883. VIDEO_MAJOR);
  884. return ret;
  885. }
  886. ret = class_register(&video_class);
  887. if (ret < 0) {
  888. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  889. printk(KERN_WARNING "video_dev: class_register failed\n");
  890. return -EIO;
  891. }
  892. return 0;
  893. }
  894. static void __exit videodev_exit(void)
  895. {
  896. dev_t dev = MKDEV(VIDEO_MAJOR, 0);
  897. class_unregister(&video_class);
  898. unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
  899. }
  900. subsys_initcall(videodev_init);
  901. module_exit(videodev_exit)
  902. MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
  903. MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
  904. MODULE_LICENSE("GPL");
  905. MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
  906. /*
  907. * Local variables:
  908. * c-basic-offset: 8
  909. * End:
  910. */